";
const T_eval = parseInt($("inspector-T_eval").value) || preset.T_train;
const params = {
theta: preset.theta, T_train: preset.T_train, T_eval: T_eval,
n_attention_heads: preset.n_attention_heads,
n_kv_heads: preset.n_kv_heads,
d_head: preset.d_head, n_layers: preset.n_layers,
n_params: preset.n_params, has_SWA: preset.has_SWA,
};
state.pyodide.globals.set("__pp", state.pyodide.toPy(params));
const json = state.pyodide.runPython(`
import json
result = profile_model(**__pp)
json.dumps(result)
`);
const profile = JSON.parse(json);
renderProfile(profile, params);
state.lastResult = { type: "profile", params };
state.lastFullResult = profile;
$("inspector-status").innerHTML = `✅ Profiled: ${preset._family} (${preset.n_params.toExponential(2)} params)`;
} catch (err) {
$("inspector-status").textContent = `❌ ${err.message}`;
console.error(err);
} finally {
$("inspector-btn").disabled = false;
}
});
// ════════════════════════════════════════════════════════════════════
// What-if T_eval slider — interactive exploration
// ════════════════════════════════════════════════════════════════════
function renderWhatIfSlider(profile, params, targetEl) {
if (!profile || !params) return;
const minL = 256;
const maxL = Math.max(params.T_eval * 4, 200000);
const initialL = params.T_eval;
targetEl.innerHTML = `
🎚 What-if: drag T_eval to see γ change live
Pure JS recompute (no Pyodide call). Shows the geometric γ_Padé and d_horizon as you slide. The full chain re-runs on click.
T_eval ${initialL.toLocaleString()}
γ_Padé —
d_horizon —
L_NIAH ceiling —
Predicted geometric verdict —
↻ Recompute full chain at this T_eval
`;
if (window.__taf_applyTranslations) window.__taf_applyTranslations();
const update = () => {
const T = parseInt($("whatif-slider").value);
const sqrt2 = Math.SQRT2;
const g_pade = (2 * params.theta - T * sqrt2) / (2 * params.theta + T * sqrt2);
// Apply same decomposition as Python
const g_corr = g_pade
+ (params.n_kv_heads < params.n_attention_heads ? 0.11 : 0)
+ (params.has_SWA ? -0.21 : 0)
+ (params.n_params >= 4e8 ? -0.15 : 0);
let dh = null, niah = null, verdict, vClass;
if (g_corr > 0 && g_corr < 1) {
dh = params.theta * (1 - g_corr) * sqrt2 / (1 + g_corr);
niah = 2 * dh;
if (T < dh) { verdict = `✅ YES (margin ${((1 - T / dh) * 100).toFixed(0)}%)`; vClass = "yes"; }
else if (T < niah) { verdict = `⚠ DEGRADED`; vClass = "deg"; }
else { verdict = `❌ NO (past NIAH ceiling)`; vClass = "no"; }
} else {
verdict = `❌ NO (Phase B)`; vClass = "no";
}
$("whatif-T_eval").textContent = T.toLocaleString();
$("whatif-gamma").textContent = g_pade.toFixed(4) + (g_corr !== g_pade ? ` → ${g_corr.toFixed(4)}` : "");
$("whatif-dh").textContent = dh !== null ? Math.round(dh).toLocaleString() : "n/a (Phase B)";
$("whatif-niah").textContent = niah !== null ? Math.round(niah).toLocaleString() : "n/a";
const vEl = $("whatif-verdict");
vEl.textContent = verdict;
vEl.className = "verdict-text " + vClass;
};
$("whatif-slider").addEventListener("input", update);
$("whatif-rerun").addEventListener("click", () => {
const T = parseInt($("whatif-slider").value);
// Update params and trigger full re-profile
$("profile-T_eval").value = T;
$("profile-btn").click();
});
update();
}
// ════════════════════════════════════════════════════════════════════
// FALSIFICATION dashboard inline
// ════════════════════════════════════════════════════════════════════
const FALSIFICATION_STATUS = [
{ id: "F1", claim: "γ_Padé MAE < 5% on non-anomalous Phase A models", status: "confirmed", evidence: "n=9, paper Tab. 4" },
{ id: "F2", claim: "d_horizon predicts NIAH collapse within 1% (pythia-70m)", status: "confirmed", evidence: "predicted 4078, observed 4096" },
{ id: "F3", claim: "Fisher info predicts forward-hook recovery within 0.2%", status: "confirmed", evidence: "12.5% predicted vs 12.3% observed" },
{ id: "F4", claim: "Layer asymmetry early/late ratio ≈ 13.5× (pythia-70m)", status: "confirmed", evidence: "F2 thermostat experiment" },
{ id: "F5", claim: "Area law S_γ = O(log N) for all γ > 0", status: "confirmed", evidence: "n=56, r=-0.954" },
{ id: "F6", claim: "KV truncation at D_f gives ΔPPL ≤ 0 in γ ∈ [0.65, 0.85]", status: "confirmed", evidence: "pythia-2.8b ΔPPL=-0.51" },
{ id: "F7", claim: "Linear pruning cost: ΔPPL ≈ 0.18 × %Q/K_pruned", status: "confirmed", evidence: "pythia-1b 0.17, 2.8b 0.18" },
{ id: "F8", claim: "Padé saturates at [1,1] in LLM regime z<<1", status: "confirmed", evidence: "sage round 4" },
{ id: "F9", claim: "RoPE attention is Euclidean fractional (d_eff=1/γ), not hyperbolic", status: "confirmed", evidence: "EXP-METRIC-RoPE sage" },
{ id: "F10", claim: "Δγ < -0.1 in models ≥ 400M ⇒ GQA / induction-head dominance", status: "confirmed", evidence: "n=20+ models" },
{ id: "F11", claim: "Δγ > +0.3 ⇒ alternating SWA (Gemma family signature)", status: "confirmed", evidence: "Gemma-2-9b Δγ=+0.51" },
{ id: "F12", claim: "Mamba L_crit = 45, α = 0.703", status: "confirmed", evidence: "3 seeds" },
{ id: "F13", claim: "Phase boundary at γ = 1 (Hagedorn)", status: "confirmed", evidence: "χ → ∞" },
{ id: "F14", claim: "RLHF Δγ shift ≤ 0.072 (recipe-specific)", status: "partial", evidence: "n=8 recipe-locked" },
{ id: "F15", claim: "R_c boundary at R_c★ ≈ 1.68", status: "refuted", evidence: "overlap zone [0.92, 3.08] n=9" },
{ id: "F16", claim: "Holographic pruning: alive bands in ℓ > L_crit ΔPPL ≈ 0", status: "refuted", evidence: "linear cost law instead" },
{ id: "F17", claim: "Soft d_horizon decay beats hard in regime d_h ≳ T_train/2", status: "partial", evidence: "n=2/3 (pythia-1b refuted)" },
{ id: "F18", claim: "Mittag-Leffler prefactor 1/Γ(1-γ) governs A_0", status: "refuted", evidence: "n=39, ratio 0.23" },
{ id: "F19", claim: "γ_Padé predicts γ_obs across-model variance", status: "partial", evidence: "centroid OK, ~0.1% var explained, see §sec:gamma_decomposition" },
{ id: "F20", claim: "β-flow exactly equivalent to logistic ODE", status: "confirmed", evidence: "sage symbolic check" },
{ id: "F21", claim: "tanh trajectory γ(t)~tanh(log step) on pythia-1b checkpoints", status: "refuted", evidence: "R²=0.15 on 4 checkpoints" },
{ id: "F22", claim: "χ(z*) = (5+√17)/4 closed form at Cayley fixed point", status: "confirmed", evidence: "sage symbolic, minimal poly 2y²-5y+1" },
{ id: "F23", claim: "T ↔ d_horizon involution: θ_design ∘ γ_Padé = id", status: "confirmed", evidence: "sage symbolic" },
];
function renderFalsificationDashboard() {
const target = $("falsification-table");
if (!target) return;
const counts = { confirmed: 0, partial: 0, refuted: 0, untested: 0 };
FALSIFICATION_STATUS.forEach(f => counts[f.status]++);
const summary = `
✅ ${counts.confirmed} confirmed ·
⚠ ${counts.partial} partial ·
❌ ${counts.refuted} refuted ·
⏳ ${counts.untested} untested
(out of ${FALSIFICATION_STATUS.length} total predictions)
`;
let table = `
ID Claim Status Evidence
`;
FALSIFICATION_STATUS.forEach(f => {
const icon = ({ confirmed: "✅", partial: "⚠", refuted: "❌", untested: "⏳" })[f.status];
table += `
${f.id}
${escapeHtml(f.claim)}
${icon} ${f.status}
${escapeHtml(f.evidence)}
`;
});
table += "
";
target.innerHTML = summary + table;
}
// ════════════════════════════════════════════════════════════════════
// Browse community submissions (live from GitHub Issues API)
// ════════════════════════════════════════════════════════════════════
async function loadCommunityFeed() {
const target = $("community-feed");
if (!target) return;
try {
const resp = await fetch(`https://api.github.com/repos/${REGISTRY_REPO}/issues?state=open&per_page=15&sort=created&direction=desc`);
if (!resp.ok) {
if (resp.status === 404) {
target.innerHTML = `The registry repo isn't created yet. Once ${REGISTRY_REPO} exists with submissions, they'll appear here live. `;
return;
}
throw new Error(`HTTP ${resp.status}`);
}
const issues = await resp.json();
if (!issues || issues.length === 0) {
target.innerHTML = `No submissions yet. Be the first — generate a Profile and click 📤 Submit to registry . `;
return;
}
const html = issues.map(issue => {
const verdict = extractVerdictFromTitle(issue.title);
const vClass = verdictClass(verdict);
const time = relativeTime(new Date(issue.created_at));
return ``;
}).join("");
target.innerHTML = html;
} catch (err) {
target.innerHTML = `⚠ Couldn't load community feed: ${escapeHtml(err.message)} `;
}
}
function extractVerdictFromTitle(title) {
const m = title.match(/→\s*(\S+)/);
if (m) return m[1];
if (title.includes("YES")) return "YES";
if (title.includes("NO")) return "NO";
if (title.includes("DEGRADED")) return "DEG";
if (title.includes("Profile")) return "📇";
if (title.includes("Compare")) return "🆚";
return "?";
}
function verdictClass(v) {
if (v.startsWith("YES") || v === "GO") return "yes";
if (v.startsWith("NO")) return "no";
if (v === "DEG" || v === "DEGRADED") return "deg";
return "";
}
function relativeTime(d) {
const sec = Math.floor((Date.now() - d.getTime()) / 1000);
if (sec < 60) return `${sec}s ago`;
if (sec < 3600) return `${Math.floor(sec / 60)}m ago`;
if (sec < 86400) return `${Math.floor(sec / 3600)}h ago`;
return `${Math.floor(sec / 86400)}d ago`;
}
// ════════════════════════════════════════════════════════════════════
// PROFILE mode
// ════════════════════════════════════════════════════════════════════
$("profile-preset").addEventListener("change", (e) => {
if (!e.target.value) return;
const modelId = e.target.value;
state.lastModelId = modelId; // remember for filename/hash
// Preset keys ARE valid HF model ids (e.g. "meta-llama/Llama-3.2-1B"). Auto-fill
// the HF id input so the user can also click 📥 Fetch to refresh from HF Hub
// without retyping. Status hint clarifies the dual source of truth.
if ($("profile-hf-id")) {
$("profile-hf-id").value = modelId;
if ($("profile-hf-status")) {
$("profile-hf-status").textContent = tFmt("profile.preset_loaded", { id: modelId });
}
}
const proxy = state.pyodide.runPython(`get_preset(${JSON.stringify(modelId)})`);
const p = proxy.toJs ? proxy.toJs({ dict_converter: Object.fromEntries }) : proxy;
if (!p || Object.keys(p).length === 0) return;
$("profile-theta").value = p.theta;
$("profile-T_train").value = p.T_train;
$("profile-n_attn").value = p.n_attention_heads;
$("profile-n_kv").value = p.n_kv_heads;
$("profile-d_head").value = p.d_head;
$("profile-n_layers").value = p.n_layers;
$("profile-n_params").value = p.n_params.toExponential(2);
$("profile-has_swa").value = String(p.has_SWA);
});
$("profile-fetch-btn").addEventListener("click", async () => {
const id = $("profile-hf-id").value.trim();
if (!id) { $("profile-hf-status").textContent = "⚠ Enter a model id"; return; }
$("profile-hf-status").textContent = `⏳ Fetching ${id}...`;
$("profile-fetch-btn").disabled = true;
state.lastModelId = id; // remember for filename/hash
try {
const cfg = await fetchHfConfig(id);
const p = configToPreset(cfg, id);
$("profile-theta").value = p.theta;
$("profile-T_train").value = p.T_train;
$("profile-n_attn").value = p.n_attention_heads;
$("profile-n_kv").value = p.n_kv_heads;
$("profile-d_head").value = p.d_head;
$("profile-n_layers").value = p.n_layers;
$("profile-n_params").value = p.n_params.toExponential(2);
$("profile-has_swa").value = String(p.has_SWA);
$("profile-hf-status").innerHTML = `✅ ${id} (${p._family})`;
} catch (err) {
$("profile-hf-status").textContent = `❌ ${err.message}`;
} finally {
$("profile-fetch-btn").disabled = false;
}
});
$("profile-btn").addEventListener("click", async () => {
const params = {
theta: parseFloat($("profile-theta").value),
T_train: parseInt($("profile-T_train").value),
T_eval: parseInt($("profile-T_eval").value),
n_attention_heads: parseInt($("profile-n_attn").value),
n_kv_heads: parseInt($("profile-n_kv").value),
d_head: parseInt($("profile-d_head").value),
n_layers: parseInt($("profile-n_layers").value),
n_params: parseFloat($("profile-n_params").value),
has_SWA: $("profile-has_swa").value === "true",
};
setStatus("🧮 Profiling — running all 5 recipes...");
$("profile-btn").disabled = true;
try {
state.pyodide.globals.set("__pp", state.pyodide.toPy(params));
const json = state.pyodide.runPython(`
import json
result = profile_model(**__pp)
json.dumps(result)
`);
const profile = JSON.parse(json);
renderProfile(profile, params);
state.lastResult = { type: "profile", params };
state.lastFullResult = profile;
setStatus("✅ Profile ready.");
} catch (err) {
setStatus(`❌ ${err.message}`);
console.error(err);
} finally {
$("profile-btn").disabled = false;
}
});
function renderProfile(p, params) {
$("profile-output").style.display = "block";
// Hide other outputs
$("output-section").style.display = "none";
$("compare-output").style.display = "none";
const verdictClass = (v) => {
if (v.startsWith("YES") || v === "GO" || v.startsWith("USE SOFT")) return "v-yes";
if (v.startsWith("NO") || v.startsWith("MEMORY") || v === "TINY-MODEL") return "v-no";
return "v-deg";
};
const verdictEmoji = (v) => verdictClass(v) === "v-yes" ? "✅"
: verdictClass(v) === "v-no" ? "❌" : "⚠";
const ms = p.model_summary;
const kn = p.key_numbers;
const formatN = (x) => x === null || x === undefined ? "n/a"
: (typeof x === "number" ? x.toLocaleString(undefined, { maximumFractionDigits: 4 }) : String(x));
const recipesHtml = Object.entries(p.recipes).map(([rid, r]) => `
${escapeHtml(r.reason || "")}
${r.mitigation && r.mitigation !== "None required." && r.mitigation !== "None — proceed with Chinchilla-optimal recipe."
? `
Action: ${escapeHtml(r.mitigation)}
`
: ""}
`).join("");
// Reusable tooltip helper — keeps tooltip pattern uniform across the card
const ttip = (key, fallback) =>
`${fallback} `;
const numbersHtml = `
γ_Padé(T_eval) ${ttip("tooltip.gamma_pade", "Closed-form prediction (2−z)/(2+z), z = T√2/θ. Paper §sec:gamma_decomposition.")} ${formatN(kn.gamma_pade)}
γ_decomposed ${ttip("tooltip.gamma_decomposed", "γ from full architectural decomposition: Padé baseline + GQA shift + SWA shift + post-IH shift.")} ${formatN(kn.gamma_decomposed)}
d_horizon ${ttip("tooltip.d_horizon", "Effective attention horizon at T_eval. Beyond this, attention scores fall below the noise floor (paper §26).")} ${formatN(kn.d_horizon)}
L_NIAH ceiling ${ttip("tooltip.L_NIAH", "Predicted ceiling for needle-in-a-haystack retrieval reliability at the current d_horizon.")} ${formatN(kn.L_NIAH_ceiling)}
χ susceptibility ${ttip("tooltip.chi", "Susceptibility exponent χ = 1/(1−γ). Diverges at the Hagedorn line γ=1.")} ${formatN(kn.chi_susceptibility)}
KV memory @ T_eval (BF16) ${ttip("tooltip.kv_memory", "Per-request KV cache memory at T_eval in BF16 = 2 · n_layers · n_kv_heads · d_head · T_eval bytes.")} ${formatN(kn.kv_memory_per_request_GB)} GB
`;
const falsHtml = (p.falsification_status || []).map(f =>
`${escapeHtml(f.id)} — ${escapeHtml(f.claim)}: ${escapeHtml(f.status)}
`
).join("");
// Per-verdict count breakdown — recipes test orthogonal axes (long-context,
// budget, hardware, custom-vs-API, KV-compression). Worst-of-N would conflate
// a "use API" recommendation with a long-context failure, so we show counts.
const verdictCounts = Object.values(p.recipes).reduce((acc, r) => {
const c = verdictClass(r.verdict);
acc[c] = (acc[c] || 0) + 1;
return acc;
}, {});
const nYes = verdictCounts["v-yes"] || 0;
const nDeg = verdictCounts["v-deg"] || 0;
const nNo = verdictCounts["v-no"] || 0;
const breakdownCls = nNo ? "v-no" : nDeg ? "v-deg" : "v-yes";
const gammaForPill = kn.gamma_decomposed ?? kn.gamma_pade;
const recipeCount = Object.keys(p.recipes).length;
// v0.8.9: pre-compute γ Validity Gate state so we can auto-expand Diagnostics when banner fires.
const _initGObs = kn.gamma_decomposed ?? kn.gamma_pade;
const _initGCheck = (typeof _initGObs === "number" && Number.isFinite(_initGObs))
? gammaCheckAll({ theta: params.theta, T: params.T_eval, gObs: _initGObs, isRandom: false })
: null;
const _validitySet = new Set(["fraud", "compressed", "overpade", "swa", "unknown"]);
const initialBannerActive = _initGCheck && (
_validitySet.has(_initGCheck.regime) ||
(Number.isFinite(_initGCheck.efficiency) && (_initGCheck.efficiency < 0.85 || _initGCheck.efficiency > 1.15))
);
const diagOpenAttr = initialBannerActive ? " open" : "";
$("profile-box").innerHTML = `
${escapeHtml(ms.architecture_class)}
n_params=${formatN(ms.n_params)} ·
T_train=${ms.T_train} · T_eval=${ms.T_eval} ·
θ=${formatN(ms.rope_theta)} ·
${ms.has_GQA ? "GQA" : "MHA"}${ms.has_SWA ? " + SWA" : ""}
✅ ${nYes} · ⚠ ${nDeg} · ❌ ${nNo} ${ttip("tooltip.verdict_breakdown", "Per-recipe breakdown across the orthogonal axes (long-context, budget, hardware, custom-vs-API, KV-compression). Recipes are independent decisions — a ❌ on X-1 means \"use API\" not \"model fails\". Open the Recipes section for per-axis verdict.")}
${gammaForPill !== null && gammaForPill !== undefined
? `γ = ${formatN(gammaForPill)} ${ttip("tooltip.gamma_pill", "γ_decomposed (full architectural decomposition) or γ_Padé as fallback. Range (0,1) = Phase A (anti-Ising). γ ≥ 1 = Hagedorn / Phase B.")} `
: ''}
${gammaForPill > 0 && gammaForPill < 1
? `🧲 Anti-Ising (β=γ−1<0, machine-verified) ${ttip("tooltip.anti_ising", "Phase A class: β = γ−1 < 0 (anti-Ising). Machine-verified by Sage Groebner basis + Lean Mathlib4. See §35 v0.5.")} ${badgesForUiBinding("anti_ising_pill")} `
: ''}
📋 Recipes — verdict per dimension
${recipeCount} ${t("tafcard.recipes_count_label", "dimensions")}
🔬 Diagnostics — numbers + γ check + what-if
⚠ Validity gate
🔢 Key numbers (paper §26)
${numbersHtml}
🔍 γ predicted vs observed
Enter your empirically measured γ. Tool detects regime: fraud (θ inflated) / compressed / over-Padé / SWA-random / normal.
🎚️ What-if explorer
✓ Verification — Lean + Sage + falsification
📑 Lean+Mathlib theorem table
Every entry below is machine-proven against Lean 4 + Mathlib4. Click any L# link to jump to the source line on GitHub. The table is grouped by topic; click a header to expand.
🔬 Algebraic consistency (Sage + Lean v0.5)
Verifies 12 D-SAGE algebraic identities of TAF critical exponents (machine-proof Sage Groebner basis + Lean Mathlib4). Pass = framework intact. Fail = bf16 outlier / quantization artifact.
${badgesForUiBinding("algebraic_consistency_check")}
🔬 Verify algebraic consistency
🔬 Falsification status (F1-F23)
${falsHtml || '
No falsifications applicable.
'}
📂 Provenance & share
🔬 v0.5.3 — Calibration audit (2026-05-02)
🔗 Copy share link
💾 Download JSON
📝 Markdown
📜 LaTeX
📤 Submit to registry
`;
// Render the what-if slider for interactive exploration
renderWhatIfSlider(p, params, $("whatif-container"));
// Render Lean+Mathlib theorem table (graceful no-op if manifest missed).
// Loaded async at bootstrap; if Profile clicked before fetch resolves we
// wait once and then render.
const renderLeanTable = () => {
const host = $("lean-table-host");
if (!host) return;
if (getManifest()) {
host.innerHTML = renderTheoremTable();
if (window.__taf_applyTranslations) window.__taf_applyTranslations();
} else {
host.innerHTML = `Loading Lean manifest…
`;
loadLeanManifest()
.then(() => { host.innerHTML = renderTheoremTable(); if (window.__taf_applyTranslations) window.__taf_applyTranslations(); })
.catch(err => { host.innerHTML = `Lean manifest unavailable: ${err.message}
`; });
}
};
renderLeanTable();
// Re-apply translations to dynamically inserted buttons
if (window.__taf_applyTranslations) window.__taf_applyTranslations();
// Wire share/download/submit buttons
$("profile-share-btn").addEventListener("click", () => copyShareLink("profile", params));
$("profile-download-btn").addEventListener("click", async () => {
const filename = await makeFilename("profile", p);
const data = await exportableData("profile", p);
downloadJSON(filename, data);
$("profile-share-status").textContent = `✅ Downloaded ${filename}`;
setTimeout(() => $("profile-share-status").textContent = "", 5000);
});
$("profile-download-md-btn").addEventListener("click", async () => {
const hash = await inputHash("profile", p);
const base = (await makeFilename("profile", p)).replace(/\.json$/, "");
downloadText(`${base}.md`, profileToMarkdown(p, hash), "text/markdown;charset=utf-8");
$("profile-share-status").textContent = `✅ Downloaded ${base}.md`;
setTimeout(() => $("profile-share-status").textContent = "", 5000);
});
$("profile-download-tex-btn").addEventListener("click", async () => {
const hash = await inputHash("profile", p);
const base = (await makeFilename("profile", p)).replace(/\.json$/, "");
downloadText(`${base}.tex`, profileToLatex(p, hash), "application/x-tex;charset=utf-8");
$("profile-share-status").textContent = `✅ Downloaded ${base}.tex`;
setTimeout(() => $("profile-share-status").textContent = "", 5000);
});
$("profile-submit-btn").addEventListener("click", async () => {
await submitToRegistry("profile", p, $("profile-share-status"));
setTimeout(() => $("profile-share-status").textContent = "", 8000);
});
// v0.6: γ predicted-vs-observed panel — interactive
const updateGammaCheck = () => {
const gObs = parseFloat($("gc-gobs").value);
const isRandom = $("gc-random").value === "true";
const r = gammaCheckAll({ theta: params.theta, T: params.T_eval, gObs, isRandom });
const meta = REGIME_META[r.regime] || REGIME_META.unknown;
const fmt = (x, d=4) => (x === null || x === undefined || Number.isNaN(x))
? "n/a"
: (!Number.isFinite(x) ? "∞" : Number(x).toLocaleString(undefined, { maximumFractionDigits: d }));
const validityRegimes = new Set(["fraud", "compressed", "overpade", "swa", "unknown"]);
const effOutOfBand = Number.isFinite(r.efficiency) && (r.efficiency < 0.85 || r.efficiency > 1.15);
const showValidity = validityRegimes.has(r.regime) || effOutOfBand;
const validityBanner = showValidity ? `
⚠ Closed-form γ may not apply to this model
` : "";
$("gamma-check-results").innerHTML = `
γ_Padé(T_eval) ${ttip("tooltip.gamma_pade", "Closed-form prediction (2−z)/(2+z), z = T√2/θ.")} ${fmt(r.gammaPade)}
θ_eff (observed) ${ttip("tooltip.theta_eff_obs", "Effective θ implied by your γ_observed: T√2 / (1 − γ_obs).")} ${fmt(r.thetaEffObs, 1)}
θ_eff (Padé) ${ttip("tooltip.theta_eff_pade", "Effective θ predicted by closed-form: θ + T/√2.")} ${fmt(r.thetaEffPade, 1)}
η = θ_eff_obs / θ_eff_Padé ${ttip("tooltip.efficiency", "Efficiency ratio. ≈1 = normal · <0.01 = fraud · <0.5 = compressed · >1.5 = over-Padé.")} ${fmt(r.efficiency)}
ΔH_Cardy = log(θ_eff_obs / θ_nominal) ${ttip("tooltip.delta_h_cardy", "Cardy entropy shift. Negative = compression entropy. ~0 = nominal match.")} ${fmt(r.deltaHCardy)}
${validityBanner}
ⓘ What do these mean?
`;
// v0.8.9: keep summary pill + Diagnostics auto-open in sync with current γ_obs.
const diagSection = document.getElementById("diag-section");
const validityPill = document.getElementById("diag-validity-pill");
if (validityPill) {
validityPill.style.cssText = showValidity
? "background:rgba(210,153,34,0.18); color:#d29922; border:1px solid rgba(210,153,34,0.4);"
: "display:none;";
}
if (diagSection && showValidity && !diagSection.open) {
diagSection.open = true;
}
if (window.__taf_applyTranslations) window.__taf_applyTranslations();
};
$("gc-gobs").addEventListener("input", updateGammaCheck);
$("gc-random").addEventListener("change", updateGammaCheck);
updateGammaCheck();
// v0.5.1: Algebraic consistency check button
$("verify-consistency-btn").addEventListener("click", () => {
const gammaVal = kn.gamma_decomposed ?? kn.gamma_pade;
if (gammaVal === null || gammaVal === undefined) {
$("consistency-result").innerHTML = `⚠ No γ value available for verification.
`;
return;
}
if (gammaVal <= 0 || gammaVal >= 1) {
$("consistency-result").innerHTML = `
⚠ γ = ${gammaVal.toFixed(4)} out of Phase A — verification requires γ ∈ (0, 1).
${gammaVal >= 1 ? "Hagedorn boundary reached." : "Phase B / negative regime."}
`;
return;
}
try {
const json = state.pyodide.runPython(`
import json
result = verify_algebraic_consistency(${gammaVal})
json.dumps(result)
`);
const r = JSON.parse(json);
const passed = r.n_checks_passed;
const total = r.n_checks_total;
const allOk = r.all_consistent;
const tooltipText = (id) => {
const key = `v05.tooltip.${id.replace(/[^a-zA-Z0-9]/g, '_')}`;
const tip = t(key);
return (tip === key) ? '' : tip;
};
const checksRows = Object.entries(r.checks).map(([id, c]) => {
const tip = tooltipText(id);
return `
${escapeHtml(id)}: ${escapeHtml(c.claim)}
${c.passes ? "✓" : "✗"}
`;
}).join("");
$("consistency-result").innerHTML = `
${allOk ? "✅" : "❌"} ${passed}/${total} D-SAGE identities ${allOk ? "consistent" : "FAILED"}
${escapeHtml(r.interpretation)}
Verified by: ${escapeHtml(r.framework_verified_by)}
🔍 Per-identity details (${total} checks)
${checksRows}
`;
} catch (err) {
$("consistency-result").innerHTML = `❌ Error: ${escapeHtml(err.message || String(err))}
`;
console.error(err);
}
});
}
// ════════════════════════════════════════════════════════════════════
// COMPARE mode
// ════════════════════════════════════════════════════════════════════
$("compare-recipe").addEventListener("change", () => {
$("compare-btn").disabled = !$("compare-recipe").value;
});
document.querySelectorAll(".compare-preset").forEach(sel => {
sel.addEventListener("change", (e) => {
const slot = e.target.closest(".compare-slot");
if (e.target.value) {
slot.querySelector(".compare-hf-id").value = e.target.value;
}
});
});
$("compare-btn").addEventListener("click", async () => {
const recipeId = $("compare-recipe").value;
if (!recipeId) { alert("Pick a recipe first."); return; }
const T_eval = parseInt($("compare-T_eval").value);
const slots = document.querySelectorAll(".compare-slot");
const specs = [];
setStatus("⏳ Fetching configs for compared models...");
$("compare-btn").disabled = true;
for (const slot of slots) {
const id = slot.querySelector(".compare-hf-id").value.trim();
if (!id) continue;
try {
let preset = null;
const presetProxy = state.pyodide.runPython(`get_preset(${JSON.stringify(id)})`);
const p = presetProxy.toJs ? presetProxy.toJs({ dict_converter: Object.fromEntries }) : presetProxy;
if (p && Object.keys(p).length > 0) {
preset = p;
} else {
const cfg = await fetchHfConfig(id);
preset = configToPreset(cfg, id);
}
specs.push({ ...preset, label: id.split("/").pop() });
} catch (err) {
console.error("compare fetch fail for", id, err);
setStatus(`⚠ Skipped ${id}: ${err.message}`);
}
}
if (specs.length < 2) {
setStatus("❌ Need at least 2 models to compare.");
$("compare-btn").disabled = false;
return;
}
setStatus(`🧮 Comparing ${specs.length} models on ${recipeId}...`);
try {
state.pyodide.globals.set("__cspecs", state.pyodide.toPy(specs));
state.pyodide.globals.set("__crid", recipeId);
state.pyodide.globals.set("__cshared", state.pyodide.toPy({ T_eval }));
const json = state.pyodide.runPython(`
import json
result = compare_models(__cspecs.to_py(), __crid, __cshared.to_py())
json.dumps(result)
`);
const cmp = JSON.parse(json);
renderCompare(cmp);
state.lastResult = { type: "compare", recipeId, T_eval, specs };
state.lastFullResult = cmp;
setStatus("✅ Comparison ready.");
} catch (err) {
setStatus(`❌ ${err.message}`);
console.error(err);
} finally {
$("compare-btn").disabled = false;
}
});
function renderCompare(cmp) {
$("compare-output").style.display = "block";
$("output-section").style.display = "none";
$("profile-output").style.display = "none";
const verdictClass = (v) => {
if (v.startsWith("YES") || v === "GO" || v.startsWith("USE SOFT")) return "v-yes";
if (v.startsWith("NO") || v.startsWith("MEMORY")) return "v-no";
return "v-deg";
};
// Collect all unique key_numbers across rows
const allKeys = new Set();
cmp.rows.forEach(r => Object.keys(r.key_numbers || {}).forEach(k => allKeys.add(k)));
let html = `
Recipe: ${escapeHtml(cmp.recipe_id)} — ${escapeHtml(cmp.recipe_name)}
Shared params: ${escapeHtml(JSON.stringify(cmp.shared_params))}
Model Verdict Reason
`;
allKeys.forEach(k => html += `${escapeHtml(k)} `);
html += " ";
cmp.rows.forEach(r => {
const cls = verdictClass(r.verdict);
html += `${escapeHtml(r.label)} `;
html += `${escapeHtml(r.verdict)} `;
html += `${escapeHtml(r.reason)} `;
allKeys.forEach(k => {
const v = r.key_numbers ? r.key_numbers[k] : null;
html += `${v === undefined || v === null ? "—" : (typeof v === "number" ? v.toLocaleString(undefined, { maximumFractionDigits: 2 }) : escapeHtml(String(v)))} `;
});
html += " ";
});
html += `
🔗 Copy share link
💾 Download JSON
📝 Markdown
📜 LaTeX
📤 Submit to registry
`;
$("compare-box").innerHTML = html;
if (window.__taf_applyTranslations) window.__taf_applyTranslations();
$("compare-share-btn").addEventListener("click", () => {
const params = { recipeId: cmp.recipe_id, T_eval: cmp.shared_params.T_eval,
models: cmp.rows.map(r => r.label) };
copyShareLink("compare", params);
});
$("compare-download-btn").addEventListener("click", async () => {
const filename = await makeFilename("compare", cmp);
const data = await exportableData("compare", cmp);
downloadJSON(filename, data);
$("compare-share-status").textContent = `✅ Downloaded ${filename}`;
setTimeout(() => $("compare-share-status").textContent = "", 5000);
});
$("compare-download-md-btn").addEventListener("click", async () => {
const hash = await inputHash("compare", cmp);
const base = (await makeFilename("compare", cmp)).replace(/\.json$/, "");
downloadText(`${base}.md`, compareToMarkdown(cmp, hash), "text/markdown;charset=utf-8");
$("compare-share-status").textContent = `✅ Downloaded ${base}.md`;
setTimeout(() => $("compare-share-status").textContent = "", 5000);
});
$("compare-download-tex-btn").addEventListener("click", async () => {
const hash = await inputHash("compare", cmp);
const base = (await makeFilename("compare", cmp)).replace(/\.json$/, "");
downloadText(`${base}.tex`, compareToLatex(cmp, hash), "application/x-tex;charset=utf-8");
$("compare-share-status").textContent = `✅ Downloaded ${base}.tex`;
setTimeout(() => $("compare-share-status").textContent = "", 5000);
});
$("compare-submit-btn").addEventListener("click", async () => {
await submitToRegistry("compare", cmp, $("compare-share-status"));
setTimeout(() => $("compare-share-status").textContent = "", 8000);
});
}
// ════════════════════════════════════════════════════════════════════
// SHARE — encode current state to URL
// ════════════════════════════════════════════════════════════════════
function copyShareLink(mode, params) {
const url = new URL(window.location.href.split("?")[0]);
url.searchParams.set("mode", mode);
url.searchParams.set("p", btoa(JSON.stringify(params)));
navigator.clipboard.writeText(url.toString()).then(
() => {
const tgt = $("share-status") || $("profile-share-status") || $("compare-share-status");
if (tgt) {
tgt.textContent = "✅ Copied to clipboard!";
setTimeout(() => tgt.textContent = "", 3000);
}
},
() => alert("Copy failed. Manually copy: " + url.toString())
);
}
function parseUrlState() {
const params = new URLSearchParams(window.location.search);
const mode = params.get("mode");
const pData = params.get("p");
if (!mode || !pData) return;
try {
const decoded = JSON.parse(atob(pData));
// Switch to right mode tab
const btn = document.querySelector(`.mode-btn[data-mode="${mode}"]`);
if (btn) btn.click();
// Wait a tick for tab to render
setTimeout(() => {
if (mode === "profile") {
Object.entries(decoded).forEach(([k, v]) => {
const map = { theta: "profile-theta", T_train: "profile-T_train",
T_eval: "profile-T_eval",
n_attention_heads: "profile-n_attn", n_kv_heads: "profile-n_kv",
d_head: "profile-d_head", n_layers: "profile-n_layers",
n_params: "profile-n_params", has_SWA: "profile-has_swa" };
const id = map[k];
if (id && $(id)) $(id).value = String(v);
});
setTimeout(() => $("profile-btn").click(), 200);
}
// Other modes: future
}, 200);
} catch (e) {
console.warn("Failed to parse URL state:", e);
}
}
// Wire single-recipe share/download/submit buttons
$("share-btn").addEventListener("click", () => {
if (!state.lastResult) return;
copyShareLink(state.lastResult.type || "recipe", state.lastResult.params || {});
});
$("recipe-download-btn").addEventListener("click", async () => {
if (!state.lastFullResult) return;
const filename = await makeFilename("recipe", state.lastFullResult);
const data = await exportableData("recipe", state.lastFullResult);
downloadJSON(filename, data);
$("share-status").textContent = `✅ Downloaded ${filename}`;
setTimeout(() => $("share-status").textContent = "", 5000);
});
$("recipe-download-md-btn").addEventListener("click", async () => {
if (!state.lastFullResult) return;
const r = state.lastFullResult;
const hash = await inputHash("recipe", r);
const base = (await makeFilename("recipe", r)).replace(/\.json$/, "");
downloadText(`${base}.md`, recipeToMarkdown(r, hash), "text/markdown;charset=utf-8");
$("share-status").textContent = `✅ Downloaded ${base}.md`;
setTimeout(() => $("share-status").textContent = "", 5000);
});
$("recipe-download-tex-btn").addEventListener("click", async () => {
if (!state.lastFullResult) return;
const r = state.lastFullResult;
const hash = await inputHash("recipe", r);
const base = (await makeFilename("recipe", r)).replace(/\.json$/, "");
downloadText(`${base}.tex`, recipeToLatex(r, hash), "application/x-tex;charset=utf-8");
$("share-status").textContent = `✅ Downloaded ${base}.tex`;
setTimeout(() => $("share-status").textContent = "", 5000);
});
$("recipe-submit-btn").addEventListener("click", async () => {
if (!state.lastFullResult) return;
await submitToRegistry("recipe", state.lastFullResult, $("share-status"));
setTimeout(() => $("share-status").textContent = "", 8000);
});
// ════════════════════════════════════════════════════════════════════
// Help modal
// ════════════════════════════════════════════════════════════════════
// a11y: focus trap + restore + Esc handling, generalized to any modal that follows
// the [role="dialog"] + .open pattern. Each call to wireModal() returns { open, close }
// and registers the modal so the global keyboard handler can find the active one.
const __modalCloseFns = new Map();
function wireModal(modalId, btnId, closeId) {
const modal = $(modalId);
if (!modal) return null;
let returnFocus = null;
const open = () => {
returnFocus = document.activeElement;
modal.classList.add("open");
modal.setAttribute("aria-hidden", "false");
setTimeout(() => $(closeId)?.focus(), 0);
};
const close = () => {
modal.classList.remove("open");
modal.setAttribute("aria-hidden", "true");
if (returnFocus && typeof returnFocus.focus === "function") returnFocus.focus();
returnFocus = null;
};
$(btnId)?.addEventListener("click", open);
$(closeId)?.addEventListener("click", close);
modal.addEventListener("click", (e) => { if (e.target.id === modalId) close(); });
__modalCloseFns.set(modalId, close);
return { open, close };
}
wireModal("help-modal", "help-btn", "help-close");
wireModal("quickstart-modal", "quickstart-btn", "quickstart-close");
wireModal("inventory-modal", "inventory-btn", "inventory-close");
// Quick-start modal "↓ Start now" link should also close the modal so user lands on mode-section.
$("qs-start-link")?.addEventListener("click", () => __modalCloseFns.get("quickstart-modal")?.());
// Esc closes whichever modal is open; Tab cycles within it.
document.addEventListener("keydown", (e) => {
const openModal = document.querySelector('[role="dialog"].open');
if (!openModal) return;
if (e.key === "Escape") {
e.preventDefault();
__modalCloseFns.get(openModal.id)?.();
return;
}
if (e.key !== "Tab") return;
const focusables = openModal.querySelectorAll(
'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])'
);
if (!focusables.length) return;
const first = focusables[0];
const last = focusables[focusables.length - 1];
if (e.shiftKey && document.activeElement === first) { e.preventDefault(); last.focus(); }
else if (!e.shiftKey && document.activeElement === last) { e.preventDefault(); first.focus(); }
});
// ════════════════════════════════════════════════════════════════════
// SHARING — Download / Upload / Submit to registry
// ════════════════════════════════════════════════════════════════════
const REGISTRY_REPO = "karlesmarin/tafagent-registry";
function downloadJSON(filename, data) {
const blob = new Blob([JSON.stringify(data, null, 2)], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(() => { document.body.removeChild(a); URL.revokeObjectURL(url); }, 100);
}
function downloadText(filename, text, mime = "text/plain;charset=utf-8") {
const blob = new Blob([text], { type: mime });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(() => { document.body.removeChild(a); URL.revokeObjectURL(url); }, 100);
}
// LaTeX-escape a plain string for inclusion in a tabular cell.
function latexEscape(s) {
return String(s ?? "")
.replace(/\\/g, "\\textbackslash{}")
.replace(/[#$%&_{}]/g, m => "\\" + m)
.replace(/~/g, "\\textasciitilde{}")
.replace(/\^/g, "\\textasciicircum{}")
.replace(//g, "\\textgreater{}");
}
function profileToLatex(p, hash = "") {
const ms = p.model_summary || {};
const kn = p.key_numbers || {};
let tex = `% TAF Profile — auto-generated by TAF Agent\n`;
if (hash) tex += `% input hash: #${hash}\n`;
tex += `\\begin{table}[ht]\n\\centering\n`;
tex += `\\caption{TAF Profile — ${latexEscape(ms.architecture_class || "?")}${hash ? ` (\\#${latexEscape(hash)})` : ""}}\n`;
tex += `\\begin{tabular}{lll}\n\\toprule\nRecipe & Verdict & Reason \\\\\n\\midrule\n`;
Object.entries(p.recipes || {}).forEach(([rid, r]) => {
tex += `${latexEscape(rid)} & ${latexEscape(r.verdict || "")} & ${latexEscape((r.reason || "").slice(0, 80))} \\\\\n`;
});
tex += `\\bottomrule\n\\end{tabular}\n\\end{table}\n\n`;
tex += `% Key numbers (JSON):\n`;
for (const [k, v] of Object.entries(kn)) {
tex += `% ${k} = ${typeof v === "object" ? JSON.stringify(v) : v}\n`;
}
return tex;
}
function compareToLatex(c, hash = "") {
let tex = `% TAF Comparison — ${c.recipe_id} (${c.recipe_name})\n`;
if (hash) tex += `% input hash: #${hash}\n`;
tex += `\\begin{table}[ht]\n\\centering\n`;
tex += `\\caption{TAF Comparison — ${latexEscape(c.recipe_id)} ${latexEscape(c.recipe_name || "")}${hash ? ` (\\#${latexEscape(hash)})` : ""}}\n`;
tex += `\\begin{tabular}{lll}\n\\toprule\nModel & Verdict & Reason \\\\\n\\midrule\n`;
c.rows.forEach(r => {
tex += `${latexEscape(r.label)} & ${latexEscape(r.verdict)} & ${latexEscape((r.reason || "").slice(0, 80))} \\\\\n`;
});
tex += `\\bottomrule\n\\end{tabular}\n\\end{table}\n`;
return tex;
}
function recipeToLatex(r, hash = "") {
let tex = `% TAF Recipe ${r.recipe_id} — ${r.recipe_name}\n`;
if (hash) tex += `% input hash: #${hash}\n`;
tex += `\\begin{table}[ht]\n\\centering\n`;
tex += `\\caption{TAF Recipe \\texttt{${latexEscape(r.recipe_id)}} — verdict: ${latexEscape(r.verdict)}${hash ? ` (\\#${latexEscape(hash)})` : ""}}\n`;
tex += `\\begin{tabular}{rll}\n\\toprule\nStep & Formula & Result \\\\\n\\midrule\n`;
(r.chain || []).forEach(s => {
tex += `${latexEscape(s.step)} & \\texttt{${latexEscape(s.formula || "")}} & ${latexEscape(formatResultPlain(s.result))} \\\\\n`;
});
tex += `\\bottomrule\n\\end{tabular}\n\\end{table}\n\n`;
tex += `% Reason: ${latexEscape(r.reason || "")}\n`;
if (r.mitigation) tex += `% Mitigation: ${latexEscape(r.mitigation)}\n`;
return tex;
}
// Sort object keys recursively for deterministic JSON
function sortKeys(o) {
if (Array.isArray(o)) return o.map(sortKeys);
if (o && typeof o === "object") {
return Object.keys(o).sort().reduce((acc, k) => { acc[k] = sortKeys(o[k]); return acc; }, {});
}
return o;
}
// Compute 8-char hex hash of canonical inputs.
// Identical inputs → identical hash (forever). Different inputs → different hash.
async function inputHash(type, data) {
let canonical;
if (type === "profile") {
const ms = data.model_summary || data;
canonical = sortKeys({
type: "profile",
theta: ms.rope_theta ?? ms.theta,
T_train: ms.T_train,
T_eval: ms.T_eval,
n_attn: ms.n_attention_heads ?? ms.n_attn,
n_kv: ms.n_kv_heads ?? ms.n_kv,
d_head: ms.d_head,
n_layers: ms.n_layers,
n_params: ms.n_params,
has_SWA: ms.has_SWA,
});
} else if (type === "compare") {
canonical = sortKeys({
type: "compare",
recipe: data.recipe_id,
T_eval: (data.shared_params || {}).T_eval,
models: (data.rows || []).map(r => r.label).sort(),
});
} else {
canonical = sortKeys({
type: "recipe",
recipe: data.recipe_id,
inputs: data.inputs || {},
});
}
const text = JSON.stringify(canonical);
const buf = new TextEncoder().encode(text);
const hashBuf = await crypto.subtle.digest("SHA-256", buf);
return Array.from(new Uint8Array(hashBuf)).slice(0, 4)
.map(b => b.toString(16).padStart(2, "0")).join("");
}
function safeFilename(s) {
return String(s).replace(/[/\\?%*:|"<>]/g, "-").replace(/^-+|-+$/g, "").slice(0, 60);
}
function modelShortName(data, fallback="model") {
// Try to get from various places
if (state.lastModelId) return safeFilename(state.lastModelId);
if (data && data.model_summary) {
const ms = data.model_summary;
return safeFilename(`m${ms.n_params || 0}-θ${ms.rope_theta || 0}`);
}
if (data && data.inputs) {
const i = data.inputs;
return safeFilename(`m${i.n_params || ""}-θ${i.theta || ""}`);
}
return fallback;
}
async function exportableData(type, data) {
const hash = await inputHash(type, data);
return {
_taf_export: true,
_taf_type: type,
_taf_version: "0.2",
_taf_input_hash: hash, // identical inputs ⇒ identical hash
_taf_timestamp: new Date().toISOString(),
payload: data,
};
}
async function makeFilename(type, data) {
const hash = await inputHash(type, data);
const name = modelShortName(data);
let suffix;
if (type === "profile" && data.model_summary?.T_eval) suffix = `T${data.model_summary.T_eval}`;
else if (type === "compare" && data.shared_params?.T_eval) suffix = `T${data.shared_params.T_eval}`;
else if (type === "recipe" && data.inputs?.T_eval) suffix = `T${data.inputs.T_eval}`;
else suffix = data.recipe_id || "result";
return `taf-${type}-${name}-${suffix}-${hash}.json`;
}
// v0.6 privacy fix: previously placed full JSON body in URL params → GH server logs +
// referer headers captured user data. Now copy body to clipboard, open issue page
// with title only, user pastes body manually. Title is non-sensitive (model name +
// hash). On clipboard failure, fall back to console log so user can grab body.
async function submitToRegistry(type, data, statusEl) {
const hash = await inputHash(type, data);
const modelName = modelShortName(data, "model");
let title, body;
if (type === "profile") {
const ms = data.model_summary || {};
title = `[TAF Profile] ${modelName} @ T=${ms.T_eval || "?"} #${hash}`;
body = profileToMarkdown(data, hash);
} else if (type === "compare") {
title = `[TAF Compare] ${data.recipe_id} × ${data.rows.length} models #${hash}`;
body = compareToMarkdown(data, hash);
} else {
title = `[TAF ${data.recipe_id}] ${modelName} → ${data.verdict} #${hash}`;
body = recipeToMarkdown(data, hash);
}
const dedupNote = `\n\n> **Input hash**: \`#${hash}\` — search this hash in registry issues to find independent verifications. Same inputs always produce the same hash.`;
const fullBody = body + dedupNote + "\n\n---\n*Submitted via [TAF Agent](https://karlesmarin.github.io/tafagent)*";
let clipboardOk = false;
try {
await navigator.clipboard.writeText(fullBody);
clipboardOk = true;
} catch (e) {
console.warn("Clipboard write failed; body logged below:", e);
console.log("[TAF Agent] Issue body to paste:\n\n" + fullBody);
}
// Title-only URL — body intentionally omitted to avoid leaking via GH server logs / referer.
const params = new URLSearchParams({ title });
window.open(`https://github.com/${REGISTRY_REPO}/issues/new?${params.toString()}`, "_blank");
if (statusEl) {
statusEl.textContent = clipboardOk
? (t("share.submit_clip_ok") || "↗ Opened GitHub. Body copied to clipboard — paste it into the issue body.")
: (t("share.submit_clip_fail") || "↗ Opened GitHub. Clipboard blocked — body logged in browser console (F12).");
}
}
function profileToMarkdown(p, hash="") {
const ms = p.model_summary || {};
const kn = p.key_numbers || {};
let md = `## TAF Profile`;
if (hash) md += ` \`#${hash}\``;
md += `\n\n`;
md += `**Architecture**: ${ms.architecture_class || "?"}\n`;
md += `**Params**: ${ms.n_params}, **T_train**: ${ms.T_train}, **T_eval**: ${ms.T_eval}\n`;
md += `**θ**: ${ms.rope_theta}, GQA=${ms.has_GQA}, SWA=${ms.has_SWA}\n\n`;
md += `### Recipes\n\n`;
Object.entries(p.recipes || {}).forEach(([rid, r]) => {
md += `- **${rid}** (${r.name || ""}): ${r.verdict} — ${r.reason}\n`;
});
md += `\n### Key numbers\n\n\`\`\`json\n${JSON.stringify(kn, null, 2)}\n\`\`\`\n`;
md += `\n### Full data\n\nClick to expand \n\n\`\`\`json\n${JSON.stringify(p, null, 2)}\n\`\`\`\n\n \n`;
return md;
}
function compareToMarkdown(c, hash="") {
let md = `## TAF Comparison — ${c.recipe_id} (${c.recipe_name})`;
if (hash) md += ` \`#${hash}\``;
md += `\n\n`;
md += `**Shared params**: \`${JSON.stringify(c.shared_params)}\`\n\n`;
md += `| Model | Verdict | Reason |\n|-------|---------|--------|\n`;
c.rows.forEach(r => {
md += `| ${r.label} | ${r.verdict} | ${r.reason.slice(0, 80)}${r.reason.length > 80 ? "..." : ""} |\n`;
});
md += `\nFull data \n\n\`\`\`json\n${JSON.stringify(c, null, 2)}\n\`\`\`\n\n \n`;
return md;
}
function recipeToMarkdown(r, hash="") {
let md = `## TAF Recipe ${r.recipe_id} — ${r.recipe_name}`;
if (hash) md += ` \`#${hash}\``;
md += `\n\n`;
md += `**Verdict**: ${r.verdict}\n`;
md += `**Reason**: ${r.reason}\n`;
if (r.mitigation) md += `**Action**: ${r.mitigation}\n`;
md += `\n### Inputs\n\n\`\`\`json\n${JSON.stringify(r.inputs, null, 2)}\n\`\`\`\n`;
md += `\n### Computation chain\n\n`;
(r.chain || []).forEach(s => {
md += `**Step ${s.step} ${s.section}** — ${s.name}: \`${s.formula}\` → ${formatResultPlain(s.result)}\n`;
});
md += `\nFull data \n\n\`\`\`json\n${JSON.stringify(r, null, 2)}\n\`\`\`\n\n \n`;
return md;
}
function importJSON(file, statusEl) {
const reader = new FileReader();
reader.onload = (e) => {
try {
const data = JSON.parse(e.target.result);
if (!data._taf_export) {
statusEl.innerHTML = "❌ Not a TAF export file (missing _taf_export marker).";
return;
}
const type = data._taf_type;
const payload = data.payload;
if (type === "profile") {
renderProfile(payload, payload.model_summary || {});
statusEl.innerHTML = `✅ Profile loaded (${data._taf_timestamp || "?"})`;
} else if (type === "compare") {
renderCompare(payload);
statusEl.innerHTML = `✅ Comparison loaded (${data._taf_timestamp || "?"})`;
} else if (type === "recipe") {
renderResult(payload);
$("output-section").style.display = "block";
statusEl.innerHTML = `✅ Recipe result loaded (${data._taf_timestamp || "?"})`;
} else {
statusEl.innerHTML = `❌ Unknown TAF type: ${type}`;
}
} catch (err) {
statusEl.innerHTML = `❌ Failed to parse JSON: ${err.message}`;
}
};
reader.readAsText(file);
}
// Wire import button (always available)
document.addEventListener("DOMContentLoaded", () => {
const importBtn = document.getElementById("import-btn");
const importFile = document.getElementById("import-file");
if (importBtn && importFile) {
importBtn.addEventListener("click", () => importFile.click());
importFile.addEventListener("change", (e) => {
const file = e.target.files[0];
if (file) importJSON(file, document.getElementById("import-status"));
});
}
// Lean+Mathlib manifest — load in parallel with everything else; badges
// appear once it resolves, but app stays usable if it fails.
loadLeanManifest().catch(err => console.warn("Lean manifest unavailable:", err));
});
// ════════════════════════════════════════════════════════════════════
// Language switcher
// ════════════════════════════════════════════════════════════════════
document.querySelectorAll(".lang-btn").forEach(btn => {
btn.addEventListener("click", () => setLang(btn.dataset.lang));
});
// ════════════════════════════════════════════════════════════════════
// 📈 Benchmark Saturation Detector (v0.8.0 anti-bullshit pack #6)
// ════════════════════════════════════════════════════════════════════
const SATURATION_VERDICT_COLOR = {
saturated: "#f85149",
near_saturated: "#d29922",
discriminative: "#3fb950",
sparse_data: "#8b949e",
unknown_benchmark: "#8b949e",
};
let __saturationInited = false;
async function initSaturation() {
if (__saturationInited) return;
__saturationInited = true;
try {
await loadSaturationKB();
} catch (e) {
$("saturation-status").textContent = (t("saturation.status.kb_fail") || "⚠ Could not load saturation KB.") + " " + (e.message || e);
return;
}
const sel = $("saturation-select");
if (sel) {
sel.innerHTML = "";
const allOpt = document.createElement("option");
allOpt.value = "__all__";
allOpt.textContent = t("saturation.select.all") || "— show all benchmarks —";
sel.appendChild(allOpt);
listBenchmarks().forEach(name => {
const opt = document.createElement("option");
opt.value = name;
opt.textContent = name;
sel.appendChild(opt);
});
}
// Try live fetch in the background; results that come back update _liveData.
// If CORS / network fails the tool transparently uses the baked snapshot.
tryFetchLive().then(live => {
if (live) {
$("saturation-status").textContent = tFmt("saturation.status.live", { count: live.model_count || (live.models?.length ?? 0) });
} else {
$("saturation-status").textContent = t("saturation.status.baked") || "ℹ Using baked snapshot (live fetch unavailable).";
}
});
}
function renderSaturationCard(result) {
if (result.code === "unknown_benchmark") {
return `${t("saturation.unknown") || "Unknown benchmark."}
`;
}
const color = SATURATION_VERDICT_COLOR[result.code] || "#8b949e";
const verdictLabel = t(`saturation.verdict.${result.code}`) || result.code;
const top3Rows = (result.top3 || [])
.filter(x => typeof x.score === "number")
.map((x, i) => `${i + 1} ${x.model} ${x.score.toFixed(1)} `)
.join("");
const recoItems = (result.recommendations || [])
.map(r => `${r} `)
.join("");
const borderlineNote = result.borderline
? `⚠ ${t("saturation.borderline") || "Borderline — within ±1pp of a threshold cutoff. Treat verdict as 'check carefully'."}
`
: "";
const sourceTag = result.source === "live"
? `live `
: (result.source === "baked_consensus"
? `consensus `
: `baked `);
const spreadStr = result.params.spread != null ? `${result.params.spread.toFixed(1)} pp` : "n/a";
const meanStr = result.params.mean != null ? `${result.params.mean.toFixed(1)}%` : "n/a";
return `
${result.params.name} — ${verdictLabel} ${sourceTag}
${t("saturation.col.spread") || "Top-3 spread"} ${spreadStr}
${t("saturation.col.mean") || "Top-3 mean"} ${meanStr}
${t("saturation.col.n") || "Models"} ${result.params.n || 0}
${borderlineNote}
${top3Rows ? `
${t("saturation.section.top3") || "Top-3 frontier scores"}
#
${t("saturation.col.model") || "Model"}
${t("saturation.col.score") || "Score"}
${top3Rows}
` : ""}
${recoItems ? `
${t("saturation.section.recommendations") || "Recommended alternatives"}
` : ""}
${result.note ? `
${t("saturation.section.note") || "Notes"}
${result.note}
` : ""}
`;
}
function renderSaturationAll(results) {
const rows = results.map(r => {
if (r.code === "unknown_benchmark") return "";
const color = SATURATION_VERDICT_COLOR[r.code] || "#8b949e";
const verdictLabel = t(`saturation.verdict.${r.code}`) || r.code;
const spread = r.params.spread != null ? r.params.spread.toFixed(1) + " pp" : "—";
const mean = r.params.mean != null ? r.params.mean.toFixed(1) + "%" : "—";
const reco = (r.recommendations || []).slice(0, 2).join(", ") || "—";
const borderlineMark = r.borderline ? " ⚠" : "";
return `
${r.params.name}
${spread}
${mean}
${verdictLabel}${borderlineMark}
${reco}
`;
}).join("");
return `
${t("saturation.section.all") || "All tracked benchmarks"}
${t("saturation.col.bench") || "Benchmark"}
${t("saturation.col.spread") || "Spread"}
${t("saturation.col.mean") || "Mean"}
${t("saturation.col.verdict") || "Verdict"}
${t("saturation.col.reco") || "Top reco"}
${rows}
`;
}
function runSaturationOne() {
const sel = $("saturation-select");
const name = sel?.value;
if (!name || name === "__all__") { runSaturationAll(); return; }
const result = classifyBenchmark(name);
$("saturation-output").innerHTML = renderSaturationCard(result);
$("saturation-status").textContent = tFmt("saturation.status.done", {
name,
verdict: t(`saturation.verdict.${result.code}`) || result.code,
});
}
function runSaturationAll() {
const results = classifyAll();
$("saturation-output").innerHTML = renderSaturationAll(results);
$("saturation-status").textContent = tFmt("saturation.status.all_done", { n: results.length });
}
$("saturation-run-btn")?.addEventListener("click", runSaturationOne);
$("saturation-all-btn")?.addEventListener("click", runSaturationAll);
// ════════════════════════════════════════════════════════════════════
// 🧭 Solutions Hub (v0.8.1) — integrator portal
// ════════════════════════════════════════════════════════════════════
const HUB_TYPE_BADGE = {
tool: "🔧",
leaderboard: "📊",
paper: "📄",
article: "📝",
docs: "📘",
issue: "🐛",
spec: "📐",
benchmark: "🧪",
};
let __hubInited = false;
async function initHub() {
if (__hubInited) return;
__hubInited = true;
try {
await loadHub();
} catch (e) {
$("hub-status").textContent = (t("hub.status.fail") || "⚠ Could not load Solutions Hub.") + " " + (e.message || e);
return;
}
const stats = hubStats();
$("hub-status").textContent = tFmt("hub.status.loaded", stats);
renderHubAll();
}
function renderEntry(e) {
const modeBadge = e.tafagent_mode
? `${e.tafagent_mode} `
: (e.tafagent_planned_mode
? `${t("hub.planned") || "planned:"} ${e.tafagent_planned_mode} `
: `${t("hub.no_mode") || "external"} `);
const tools = (e.external_tools || [])
.map(tl => {
const icon = HUB_TYPE_BADGE[tl.type] || "🔗";
return `${icon} ${tl.name} (${tl.type}) `;
})
.join("");
const bestFor = e.best_for ? `${t("hub.best_for") || "Best for"}: ${e.best_for}
` : "";
const notFor = e.not_for ? `${t("hub.not_for") || "Not for"}: ${e.not_for}
` : "";
return `
${e.pain} ${modeBadge}
${bestFor}
${notFor}
${tools ? `${t("hub.tools") || "External tools"}:
` : ""}
`;
}
function renderHubAll() {
const cats = listCategories();
const html = cats.map(c => {
const entries = listEntries(c.key);
if (entries.length === 0) return "";
const inner = entries.map(renderEntry).join("");
return `
${c.icon} ${c.label} (${c.count})
${c.description}
${inner}
`;
}).join("");
$("hub-output").innerHTML = `${html}
`;
}
function renderHubSearch(query) {
const matches = searchEntries(query);
if (matches.length === 0) {
$("hub-output").innerHTML = `${tFmt("hub.search.empty", { query })}
`;
return;
}
const html = matches.map(renderEntry).join("");
$("hub-output").innerHTML = `
${tFmt("hub.search.results", { n: matches.length, query })}
${html}
`;
}
let __hubSearchTimer = null;
$("hub-search")?.addEventListener("input", (e) => {
clearTimeout(__hubSearchTimer);
const q = e.target.value;
__hubSearchTimer = setTimeout(() => {
if (!q.trim()) renderHubAll();
else renderHubSearch(q);
}, 200);
});
$("hub-clear-btn")?.addEventListener("click", () => {
$("hub-search").value = "";
renderHubAll();
});
// ════════════════════════════════════════════════════════════════════
// 📋 JSON CoT-aware Linter (v0.8.2 anti-bullshit pack #8)
// ════════════════════════════════════════════════════════════════════
const COT_FIELD_TYPE_BADGE = {
reasoning: "🧠",
answer: "🎯",
other: "·",
};
const COT_VERDICT_BADGE_BG = {
good_order: "#3fb950", // green
anti_pattern: "#f85149", // red
missing_reasoning: "#d29922", // amber
missing_answer: "#d29922", // amber
no_cot_fields: "#8b949e", // gray
non_object: "#8b949e",
empty_fields: "#8b949e",
invalid_json: "#f85149", // red
};
let __cotInited = false;
function initCot() {
if (__cotInited) return;
__cotInited = true;
// No-op (no async data); placeholder kept for symmetry with other modes.
}
function renderCotResult(result, originalText) {
const verdict = t(`cot.verdict.${result.code}`) || result.code;
const verdictBg = COT_VERDICT_BADGE_BG[result.code] || "#8b949e";
const verdictBadge = `${verdict} `;
// Failure cases short-circuit: just show the verdict + reason.
if (result.code === "invalid_json") {
const reason = result.params?.error || "";
return `
${verdictBadge}
${escapeHtml(reason)}
`;
}
if (result.code === "empty_fields" || result.code === "non_object") {
return `
${verdictBadge}
${t(`cot.hint.${result.code}`) || ""}
`;
}
const fields = result.params?.fields || [];
const fieldRows = fields.map(f => {
const icon = COT_FIELD_TYPE_BADGE[f.type] || "·";
const typeLabel = t(`cot.field.${f.type}`) || f.type;
const color = f.type === "reasoning" ? "#3fb950"
: f.type === "answer" ? "#f0883e"
: "#8b949e";
return `
${f.idx}
${escapeHtml(f.name)}
${icon} ${typeLabel}
`;
}).join("");
const fieldTable = `
#
Field
Type
${fieldRows}
`;
// Suggested-fix block — only when there's a meaningful reorder.
let fixBlock = "";
if (result.code === "anti_pattern") {
const suggested = result.params?.suggested_order || [];
const fixed = reorderJsonText(originalText, suggested);
if (fixed) {
fixBlock = `
${t("cot.suggested_fix.title") || "✓ Suggested fix"}
${t("cot.suggested_fix.desc") || ""}
${escapeHtml(fixed)}
{this.textContent='${t("cot.suggested_fix.copy") || "📋 Copy"}';},1500);})">${t("cot.suggested_fix.copy") || "📋 Copy"}
`;
}
}
// Verdict explainer
const explainer = t(`cot.explain.${result.code}`) || "";
const explainerBlock = explainer
? `${explainer}
`
: "";
// Source attribution footer
const attribution = `
${t("cot.attribution") || ""}
collinwilkins.com ·
JSONSchemaBench ·
llguidance
`;
return `
${verdictBadge}
(${tFmt("cot.field_count", { n: result.params.field_count }) || `${result.params.field_count} fields`})
${explainerBlock}
${fieldTable}
${fixBlock}
${attribution}
`;
}
function runCotLint() {
const text = $("cot-input")?.value || "";
const result = lintJsonCot(text);
$("cot-output").innerHTML = renderCotResult(result, text);
$("cot-status").textContent = tFmt("cot.status.done", {
verdict: t(`cot.verdict.${result.code}`) || result.code,
});
}
const COT_EXAMPLE_GOOD = JSON.stringify({
type: "object",
properties: {
reasoning: {
type: "string",
description: "Step-by-step rationale before committing to an answer.",
},
answer: {
type: "string",
description: "Final answer, derived from the reasoning above.",
},
},
required: ["reasoning", "answer"],
}, null, 2);
const COT_EXAMPLE_BAD = JSON.stringify({
type: "object",
properties: {
final_answer: {
type: "string",
description: "The model's final answer.",
},
chain_of_thought: {
type: "string",
description: "Justification for the answer above.",
},
},
required: ["final_answer", "chain_of_thought"],
}, null, 2);
$("cot-lint-btn")?.addEventListener("click", runCotLint);
$("cot-example-good-btn")?.addEventListener("click", () => {
$("cot-input").value = COT_EXAMPLE_GOOD;
runCotLint();
});
$("cot-example-bad-btn")?.addEventListener("click", () => {
$("cot-input").value = COT_EXAMPLE_BAD;
runCotLint();
});
// ════════════════════════════════════════════════════════════════════
// 🔧 PEFT Anti-Pattern Checker (v0.8.3 anti-bullshit pack #9)
// ════════════════════════════════════════════════════════════════════
const PEFT_SEVERITY_BG = {
error: "#f85149",
warning: "#d29922",
info: "#58a6ff",
};
const PEFT_VERDICT_BG = {
errors_found: "#f85149",
warnings_only: "#d29922",
info_only: "#58a6ff",
clean: "#3fb950",
no_peft_calls: "#8b949e",
empty_input: "#8b949e",
};
let __peftInited = false;
function initPeft() {
if (__peftInited) return;
__peftInited = true;
// No-op (no async data); placeholder kept for symmetry with other modes.
}
function renderPeftFinding(f) {
const sevBg = PEFT_SEVERITY_BG[f.severity] || "#8b949e";
const sevBadge = `${f.severity.toUpperCase()} `;
const ruleLabel = t(`peft.rule.${f.rule}.label`) || f.rule;
const lineLabel = f.line != null
? `${tFmt("peft.line", { n: f.line }) || `line ${f.line}`} `
: "";
const explainer = t(`peft.rule.${f.rule}.explain`) || "";
const fixHint = t(`peft.rule.${f.rule}.fix`) || "";
// Per-rule rendering details
let detail = "";
if (f.rule === "silent_base_load") {
detail = `${escapeHtml(f.params.checkpoint_hint)} ${t("peft.detected_at_line") || "appears at line"} ${f.params.checkpoint_line}
${t("peft.suggested_fix") || "Suggested:"} ${escapeHtml(f.params.fix)}
`;
} else if (f.rule === "qlora_order") {
detail = `${tFmt("peft.qlora_order.detail", f.params) || `prepare_model_for_kbit_training (line ${f.params.prepare_line}) runs AFTER get_peft_model (line ${f.params.get_peft_model_line}). Reverse the order.`}
`;
} else if (f.rule === "target_modules_mismatch") {
detail = `
${t("peft.detected_arch") || "Detected arch"}: ${escapeHtml(f.params.detected_arch)} ${t("peft.from_model_id") || "(from model id"} ${escapeHtml(f.params.detected_from)})
${t("peft.your_modules") || "Your target_modules"}: ${escapeHtml(f.params.user_modules.join(", "))}
${t("peft.expected_modules") || "Expected for this arch"}: ${escapeHtml(f.params.expected_modules.join(", "))}
${tFmt("peft.match_ratio", f.params) || `${f.params.hits} of ${f.params.total} match.`}
`;
} else if (f.rule === "alpha_not_2r") {
detail = `r=${f.params.r}, lora_alpha=${f.params.lora_alpha} → ${t("peft.ratio") || "ratio"} ${f.params.ratio}× (${t("peft.alpha.convention") || "convention is α=2r or α=r"})
`;
} else if (f.rule === "no_peft_calls") {
detail = `${t("peft.no_peft_calls.detail") || "No get_peft_model / PeftModel.from_pretrained / LoraConfig calls detected. Paste a PEFT/LoRA setup snippet."}
`;
}
return `
${sevBadge} ${ruleLabel} ${lineLabel}
${explainer ? `${explainer}
` : ""}
${detail}
${fixHint ? `${fixHint}
` : ""}
`;
}
function renderPeftResult(result) {
const verdict = t(`peft.verdict.${result.code}`) || result.code;
const verdictBg = PEFT_VERDICT_BG[result.code] || "#8b949e";
const verdictBadge = `${verdict} `;
const findings = result.findings || [];
const findingsHtml = findings.map(renderPeftFinding).join("");
const summary = result.summary
? `${tFmt("peft.summary", result.summary) || `${result.summary.total} finding(s)`}
`
: "";
// Source attribution
const attribution = `
${t("peft.attribution") || "Refs:"}
peft #2115 ·
PEFT troubleshooting ·
get_layer_status / get_model_status
`;
return `
${verdictBadge}
${summary}
${findingsHtml}
${attribution}
`;
}
function runPeftLint() {
const text = $("peft-input")?.value || "";
const result = lintPeftCode(text);
$("peft-output").innerHTML = renderPeftResult(result);
$("peft-status").textContent = tFmt("peft.status.done", {
verdict: t(`peft.verdict.${result.code}`) || result.code,
n: result.findings?.length || 0,
});
}
const PEFT_EXAMPLE_BUG = `from peft import LoraConfig, get_peft_model
from transformers import AutoModelForCausalLM
base = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3-8B")
config = LoraConfig(
r=16,
lora_alpha=32,
target_modules=["q_proj", "v_proj"],
)
model = get_peft_model(base, config)
# resume from saved checkpoint?
model.load_state_dict("./outputs/checkpoint-1000/adapter_model.bin")
`;
const PEFT_EXAMPLE_QLORA = `from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
from transformers import AutoModelForCausalLM, BitsAndBytesConfig
bnb = BitsAndBytesConfig(load_in_4bit=True)
base = AutoModelForCausalLM.from_pretrained(
"meta-llama/Llama-3-8B",
quantization_config=bnb,
)
config = LoraConfig(r=16, lora_alpha=32, target_modules=["q_proj", "v_proj"])
model = get_peft_model(base, config)
# WRONG ORDER: prepare_model_for_kbit_training must come BEFORE get_peft_model
model = prepare_model_for_kbit_training(model)
`;
const PEFT_EXAMPLE_CLEAN = `from peft import PeftModel
from transformers import AutoModelForCausalLM
base = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3-8B")
# Resume from saved adapter — correct PEFT pattern.
model = PeftModel.from_pretrained(base, "./outputs/checkpoint-1000")
`;
$("peft-lint-btn")?.addEventListener("click", runPeftLint);
$("peft-example-bug-btn")?.addEventListener("click", () => {
$("peft-input").value = PEFT_EXAMPLE_BUG;
runPeftLint();
});
$("peft-example-qlora-btn")?.addEventListener("click", () => {
$("peft-input").value = PEFT_EXAMPLE_QLORA;
runPeftLint();
});
$("peft-example-clean-btn")?.addEventListener("click", () => {
$("peft-input").value = PEFT_EXAMPLE_CLEAN;
runPeftLint();
});
// ════════════════════════════════════════════════════════════════════
// 🔁 Prompt-Cache Diff Predictor (v0.8.4 anti-bullshit pack #10)
// ════════════════════════════════════════════════════════════════════
const CACHE_VERDICT_BG = {
identical: "#3fb950",
divergent_can_cache: "#d29922",
divergent_below_min: "#f0883e",
fully_divergent: "#f85149",
empty_input: "#8b949e",
};
let __cacheInited = false;
function initCacheDiff() {
if (__cacheInited) return;
__cacheInited = true;
// No-op (no async data); placeholder kept for symmetry.
}
function fmtUsd(n) {
if (n == null || isNaN(n)) return "—";
if (n === 0) return "$0";
if (n < 0.01) return `$${n.toFixed(6)}`;
if (n < 1) return `$${n.toFixed(4)}`;
return `$${n.toFixed(2)}`;
}
function fmtPct(n) {
if (n == null || isNaN(n)) return "—";
return `${Math.round(n * 100)}%`;
}
function renderCacheProvider(p) {
const bgRow = p.reason === "below_min" ? "#21262d" : "#161b22";
const noteHtml = [];
if (p.requires_explicit && p.reason !== "below_min") {
noteHtml.push(`${t("cache.note.requires_marker") || "(requires cache_control marker)"} `);
}
if (p.reason === "below_min") {
noteHtml.push(`${tFmt("cache.note.below_min", { min: p.min_cache_tokens.toLocaleString() }) || `(prefix < ${p.min_cache_tokens.toLocaleString()} tokens — provider min)`} `);
}
const noteCell = noteHtml.length ? ` ${noteHtml.join(" ")}` : "";
const ttlMin = p.cache_ttl_seconds >= 3600
? `${Math.round(p.cache_ttl_seconds / 3600)}h`
: `${Math.round(p.cache_ttl_seconds / 60)}min`;
const savingsColor = p.savings_usd > 0 ? "#3fb950" : (p.reason ? "#8b949e" : "#d29922");
const writeRow = p.cache_write_surcharge_usd && p.cache_write_surcharge_usd > 0
? `${tFmt("cache.write_surcharge", { cost: fmtUsd(p.cache_write_surcharge_usd) }) || `+ ${fmtUsd(p.cache_write_surcharge_usd)} cache-write surcharge first time (Anthropic)`} `
: "";
return `
${escapeHtml(p.provider_name)} ${noteCell}TTL ${ttlMin}
${fmtPct(p.hit_ratio)}
${fmtUsd(p.base_cost_usd)} → ${fmtUsd(p.cached_cost_usd)}
${fmtUsd(p.savings_usd)} (${fmtPct(p.savings_pct ?? 0)})
${writeRow}
`;
}
function renderCacheDiffVisualization(oldText, newText, lcpChars) {
// Truncate context — show last 200 chars of common prefix, and the
// first 200 chars of each diverging suffix. Keeps UI tight.
const ctxBefore = 200;
const startCommon = Math.max(0, lcpChars - ctxBefore);
const commonTail = oldText.slice(startCommon, lcpChars);
const oldDiv = oldText.slice(lcpChars);
const newDiv = newText.slice(lcpChars);
const commonLeader = startCommon > 0 ? "…" : "";
return `
${t("cache.diff.title") || "Where the cache breaks"}
${escapeHtml(commonLeader + commonTail)} ${escapeHtml(oldDiv.slice(0, 200))} ← old
${escapeHtml(commonLeader + commonTail)} ${escapeHtml(newDiv.slice(0, 200))} ← new
${t("cache.diff.legend") || "Green = shared prefix (cacheable). Red = first edit (everything from here is re-billed)."}
`;
}
function renderCacheResult(result, oldText, newText) {
const verdict = t(`cache.verdict.${result.code}`) || result.code;
const verdictBg = CACHE_VERDICT_BG[result.code] || "#8b949e";
const verdictBadge = `${verdict} `;
if (result.code === "empty_input") {
return `
${verdictBadge}
${t("cache.hint.empty") || "Paste two prompts, then Predict."}
`;
}
const p = result.params;
const summary = `
${tFmt("cache.summary.tokens", { common: p.tokens_common.toLocaleString(), total: p.tokens_total.toLocaleString(), pct: Math.round(p.hit_ratio * 100) })
|| `Common prefix ${p.tokens_common.toLocaleString()} / ${p.tokens_total.toLocaleString()} tokens (${Math.round(p.hit_ratio * 100)}% theoretical hit ratio).`}
${tFmt("cache.summary.diff_at", { line: p.diff_point.line }) || `First difference at line ${p.diff_point.line}.`}
`;
const rows = (result.providers || []).map(renderCacheProvider).join("");
const table = rows ? `
${t("cache.col.provider") || "Provider"}
${t("cache.col.hit") || "Hit"}
${t("cache.col.cost") || "Base → cached"}
${t("cache.col.savings") || "Savings"}
${rows}
` : "";
const diffViz = result.code !== "identical"
? renderCacheDiffVisualization(oldText, newText, p.lcp_chars)
: "";
const attribution = `
${t("cache.attribution") || "Refs:"}
Anthropic prompt caching ·
OpenAI prompt caching ·
Gemini context caching
${t("cache.attribution.snapshot") || "Prices snapshot 2026-01; verify against current provider docs before acting on $."}
`;
return `
${verdictBadge}
${summary}
${table}
${diffViz}
${attribution}
`;
}
function runCacheDiff() {
const oldText = $("cache-old")?.value || "";
const newText = $("cache-new")?.value || "";
const profile = $("cache-profile")?.value || "english";
const outputTokens = parseInt($("cache-output-tokens")?.value || "500", 10);
const result = diffPromptCache(oldText, newText, {
profile,
outputTokensEstimate: outputTokens,
});
$("cache-output").innerHTML = renderCacheResult(result, oldText, newText);
$("cache-status").textContent = tFmt("cache.status.done", {
verdict: t(`cache.verdict.${result.code}`) || result.code,
hit: Math.round((result.params?.hit_ratio || 0) * 100),
});
}
const CACHE_LONG_SYS = "You are a helpful, harmless, and honest assistant. " +
"Always cite your sources. ".repeat(40) +
"Always show your reasoning step by step. ".repeat(40) +
"Be concise. Format code with backticks. ".repeat(40) +
"\n\nUser tools available:\n- search\n- calculator\n- code_runner\n";
const CACHE_EXAMPLE_GOOD_OLD = CACHE_LONG_SYS + "\nUser: What is 2 + 2?";
const CACHE_EXAMPLE_GOOD_NEW = CACHE_LONG_SYS + "\nUser: What is 2 + 3?";
const CACHE_EXAMPLE_BROKEN_OLD = CACHE_LONG_SYS.replace("helpful, harmless, and honest", "helpful AND honest")
+ "\nUser: What is 2 + 2?";
const CACHE_EXAMPLE_BROKEN_NEW = CACHE_LONG_SYS + "\nUser: What is 2 + 2?";
const CACHE_EXAMPLE_BELOWMIN_OLD = "Q: name 3 colors";
const CACHE_EXAMPLE_BELOWMIN_NEW = "Q: name 4 colors";
$("cache-diff-btn")?.addEventListener("click", runCacheDiff);
$("cache-example-good-btn")?.addEventListener("click", () => {
$("cache-old").value = CACHE_EXAMPLE_GOOD_OLD;
$("cache-new").value = CACHE_EXAMPLE_GOOD_NEW;
runCacheDiff();
});
$("cache-example-broken-btn")?.addEventListener("click", () => {
$("cache-old").value = CACHE_EXAMPLE_BROKEN_OLD;
$("cache-new").value = CACHE_EXAMPLE_BROKEN_NEW;
runCacheDiff();
});
$("cache-example-belowmin-btn")?.addEventListener("click", () => {
$("cache-old").value = CACHE_EXAMPLE_BELOWMIN_OLD;
$("cache-new").value = CACHE_EXAMPLE_BELOWMIN_NEW;
runCacheDiff();
});
// ════════════════════════════════════════════════════════════════════
// 🔬 Speculative-Decode Compatibility (v0.8.5 anti-bullshit pack #11)
// ════════════════════════════════════════════════════════════════════
const SPEC_VERDICT_BG = {
compatible: "#3fb950",
compatible_with_caveats: "#3fb950",
partial_compatible: "#d29922",
type_mismatch: "#f85149",
vocab_size_mismatch: "#f85149",
incompatible: "#f85149",
fetch_failed: "#8b949e",
identical_models: "#58a6ff",
missing_input: "#8b949e",
};
let __specInited = false;
function initSpeculative() {
if (__specInited) return;
__specInited = true;
// No-op (no async preload); placeholder kept for symmetry.
}
function fmtParams(p) {
if (!p) return "—";
if (p >= 1e9) return `${(p / 1e9).toFixed(1)}B`;
if (p >= 1e6) return `${(p / 1e6).toFixed(1)}M`;
return p.toLocaleString();
}
function renderSpecResult(result) {
const verdict = t(`speculative.verdict.${result.code}`) || result.code;
const verdictBg = SPEC_VERDICT_BG[result.code] || "#8b949e";
const verdictBadge = `${verdict} `;
// Failure-mode short-circuits
if (result.code === "missing_input" || result.code === "identical_models") {
return `
${verdictBadge}
${t(`speculative.hint.${result.code}`) || ""}
`;
}
if (result.code === "fetch_failed") {
const errs = (result.errors || []).map(e => {
const sideLabel = e.side === "target" ? (t("speculative.side.target") || "Target") : (t("speculative.side.draft") || "Draft");
const reason = t(`speculative.fetch_error.${e.error}`) || e.error;
return `${sideLabel} : ${reason}${e.status ? ` (HTTP ${e.status})` : ""} `;
}).join("");
return `
${verdictBadge}
${t("speculative.fetch_error.hint") || "Check the model id spelling. For gated models you'll need to view the tokenizer file via your HF account — this tool can't auth."}
`;
}
const p = result.params;
// Mirror banner — when a gated model was fetched via an open mirror.
let mirrorBanner = "";
if (p.target_via_mirror || p.draft_via_mirror) {
const lines = [];
if (p.target_via_mirror) {
lines.push(tFmt("speculative.mirror.target_used", {
original: escapeHtml(p.targetId),
mirror: escapeHtml(p.target_via_mirror),
}) || `Target was gated; used mirror ${escapeHtml(p.target_via_mirror)}.`);
}
if (p.draft_via_mirror) {
lines.push(tFmt("speculative.mirror.draft_used", {
original: escapeHtml(p.draftId),
mirror: escapeHtml(p.draft_via_mirror),
}) || `Draft was gated; used mirror ${escapeHtml(p.draft_via_mirror)}.`);
}
mirrorBanner = `
ℹ ${t("speculative.mirror.heading") || "Open-mirror fallback"}
${lines.map(l => ` ${l}`).join("")}
${t("speculative.mirror.warn") || "Mirror tokenizers (e.g. unsloth/) are usually byte-identical to the gated original because quantization touches weights, not tokens. Verify chat-template if exact match is required."}
`;
}
// Section 1 — vocab summary
const typeBadge = (label, val, bg) =>
`${label}: ${val ?? "—"} `;
const typeRow = `
${typeBadge(t("speculative.target_label_short") || "target", p.target_type, p.type_match ? "#3fb950" : "#f85149")}
${typeBadge(t("speculative.draft_label_short") || "draft", p.draft_type, p.type_match ? "#3fb950" : "#f85149")}
${p.type_match ? "" : ` ← ${t("speculative.type_mismatch_note") || "tokenizer types differ; spec-dec impossible"} `}
`;
const sizeRow = `
${t("speculative.vocab_size") || "Vocab size"}:
target = ${p.target_vocab_size.toLocaleString()},
draft = ${p.draft_vocab_size.toLocaleString()}
${p.vocab_size_match ? "" : ` ← ${t("speculative.size_diff") || "differ — every reused id is a misalignment"} `}
`;
// Sampled match
const matchPct = p.sampled_total > 0 ? Math.round(p.sampled_match_ratio * 100) : 0;
const matchColor = matchPct >= 99.9 ? "#3fb950" : matchPct >= 95 ? "#d29922" : "#f85149";
const sampleRow = `
${t("speculative.sampled") || "Token-id sample match"}:
${matchPct}%
(${p.sampled_match_count.toLocaleString()} / ${p.sampled_total.toLocaleString()} tokens)
${p.first_mismatch ? `${t("speculative.first_mismatch") || "First mismatch"}: ${escapeHtml(p.first_mismatch.token).slice(0, 40)} → target id ${p.first_mismatch.target_id ?? "—"}, draft id ${p.first_mismatch.draft_id ?? "—"} ` : ""}
`;
// Special / added token diffs
const specDiffRows = (p.special_tokens_diff || []).map(d =>
`${d.name}: target=${escapeHtml(String(d.target ?? "—"))}, draft=${escapeHtml(String(d.draft ?? "—"))} `
).join("");
const specDiffBlock = specDiffRows
? `${t("speculative.special_diff") || "Special-token differences"} (${p.special_tokens_diff.length}) `
: "";
const addedDiffPreview = (p.added_tokens_diff || []).slice(0, 12).map(d =>
`${d.side === "target_only" ? "target only" : "draft only"}: ${escapeHtml(d.token).slice(0, 40)} `
).join("");
const addedDiffBlock = addedDiffPreview
? `${t("speculative.added_diff") || "Added-token differences"} (${(p.added_tokens_diff||[]).length}) ${addedDiffPreview}${p.added_tokens_diff.length > 12 ? `${t("speculative.added_diff_more") || "+ more …"} ` : ""} `
: "";
// Section 2 — speedup band (only when compatible-ish)
let speedupBlock = "";
if (p.speedup_expected != null) {
const ratio = p.param_ratio ? `${(p.param_ratio * 100).toFixed(1)}%` : "—";
speedupBlock = `
${t("speculative.speedup.title") || "Estimated speedup band"}
${tFmt("speculative.speedup.params", { target: fmtParams(p.target_params), draft: fmtParams(p.draft_params), ratio }) || `target ${fmtParams(p.target_params)} / draft ${fmtParams(p.draft_params)} (param ratio ${ratio})`}
${t("speculative.speedup.low") || "Low (α=0.50)"}:${p.speedup_low}×
${t("speculative.speedup.expected") || "Expected (α=0.70)"}:${p.speedup_expected}×
${t("speculative.speedup.high") || "High (α=0.85)"}:${p.speedup_high}×
${t("speculative.speedup.disclaimer") || "α = draft acceptance rate. Real speedup depends on prompt domain, lookahead K, and engine overhead. Bands assume ideal verifier batching."}
`;
} else if (p.target_params && p.draft_params && p.param_ratio >= 1) {
speedupBlock = `${t("speculative.speedup.draft_not_smaller") || "Draft is not smaller than target — spec-dec is misuse here."}
`;
}
// Attribution
const attribution = `
${t("speculative.attribution") || "Refs:"}
vLLM spec-dec docs ·
SGLang ·
transformers assistant_model ·
Leviathan et al. 2022
`;
return `
${verdictBadge}
${mirrorBanner}
${typeRow}
${sizeRow}
${sampleRow}
${specDiffBlock}
${addedDiffBlock}
${speedupBlock}
${attribution}
`;
}
async function runSpecCheck() {
const targetId = $("spec-target-id")?.value?.trim() || "";
const draftId = $("spec-draft-id")?.value?.trim() || "";
$("spec-status").textContent = t("speculative.status.fetching") || "🔄 Fetching tokenizer.json from HF Hub for both models…";
$("spec-output").innerHTML = "";
try {
const result = await specCheckCompat(targetId, draftId);
$("spec-output").innerHTML = renderSpecResult(result);
$("spec-status").textContent = tFmt("speculative.status.done", {
verdict: t(`speculative.verdict.${result.code}`) || result.code,
});
} catch (e) {
$("spec-status").textContent = (t("speculative.status.error") || "❌ Error") + " " + (e.message || e);
}
}
$("spec-check-btn")?.addEventListener("click", runSpecCheck);
// Examples mix gated + open: gated ids (Llama) trigger the open-mirror
// fallback (unsloth/...) so the user sees both the demo result AND the
// mirror-resolution mechanism. Pure open-weight pairs (Qwen + Phi)
// stay as the "no fallback needed" path for the second example.
$("spec-example-good-btn")?.addEventListener("click", () => {
// Gated → triggers unsloth mirror fallback for both sides.
$("spec-target-id").value = "meta-llama/Llama-3.1-70B-Instruct";
$("spec-draft-id").value = "meta-llama/Llama-3.1-8B-Instruct";
runSpecCheck();
});
$("spec-example-bad-btn")?.addEventListener("click", () => {
// Open-weight cross-family → no fallback, plain incompatibility demo.
$("spec-target-id").value = "Qwen/Qwen2.5-7B-Instruct";
$("spec-draft-id").value = "microsoft/Phi-3.5-mini-instruct";
runSpecCheck();
});
// (HF autocomplete on spec-target-id / spec-draft-id is registered via
// the known-id list in hf_autocomplete.js; no extra wiring needed here.)
// ════════════════════════════════════════════════════════════════════
// 🌍 Multilingual Tokenizer Tax (v0.8.7 anti-bullshit pack #13)
// ════════════════════════════════════════════════════════════════════
let __taxInited = false;
function initTax() {
if (__taxInited) return;
__taxInited = true;
// No async preload — transformers.js + tokenizer.json are lazy-loaded
// on the first Tokenize click so users don't pay download cost just
// for opening the tab. Status string explains the wait.
}
function fmtBlocks(blocks) {
// Build a compact "60% latin · 35% cjk · 5% other" string from the
// detector output. Drops zero-counts and orders by descending size.
if (!blocks || !blocks.blocks || !blocks.total_chars) return "";
const total = blocks.total_chars;
const entries = Object.entries(blocks.blocks)
.filter(([, n]) => n > 0)
.sort((a, b) => b[1] - a[1]);
if (entries.length === 0) return "";
const parts = entries.map(([name, n]) => {
const pct = Math.round((n / total) * 100);
return `${pct}% ${name}`;
});
return parts.join(" · ");
}
function renderTaxResult(res, presetMeta) {
if (res.code === "empty_input") {
return `${t("tax.hint.empty") || "Paste some text and click Tokenize."}
`;
}
if (res.code === "all_failed") {
const errLines = res.results.map(r => {
const meta = presetMeta.find(p => p.id === r.modelId);
return `${escapeHtml(r.modelId)} ${meta ? `(${escapeHtml(meta.label)}) ` : ""}: ${t(`tax.error.${r.error}`) || r.error} `;
}).join("");
return `❌ ${t("tax.all_failed") || "All tokenizers failed to load."}
`;
}
const baselineCount = res.baseline_count;
const blocks = detectLanguageBlocks($("tax-input").value);
const ratioColor = (r) => {
if (r == null) return "#8b949e";
if (r >= 1.5) return "#f85149"; // big tax — red
if (r >= 1.15) return "#f0883e"; // moderate
if (r >= 0.85) return "#3fb950"; // about same
return "#58a6ff"; // BETTER than baseline (rare)
};
const fmtRatio = (r) => r == null ? "—" : `${r.toFixed(2)}×`;
const rows = res.results.map(r => {
const meta = presetMeta.find(p => p.id === r.modelId) || { label: r.modelId, family: "" };
if (!r.ok) {
return `
${escapeHtml(meta.label)} ${escapeHtml(meta.family)}
${t(`tax.error.${r.error}`) || r.error}
`;
}
const isBaseline = r.modelId === res.baseline_id;
const baselineMark = isBaseline ? ` (baseline) ` : "";
return `
${escapeHtml(meta.label)} ${baselineMark}${escapeHtml(meta.family)}
${r.token_count.toLocaleString()}
${r.chars_per_token != null ? r.chars_per_token.toFixed(2) : "—"}
${fmtRatio(r.ratio_vs_baseline)}
`;
}).join("");
// Worst-tax explanation — find the tokenizer that scored ≥1.5× baseline.
const worst = res.results
.filter(r => r.ok && r.ratio_vs_baseline != null)
.sort((a, b) => b.ratio_vs_baseline - a.ratio_vs_baseline)[0];
let interpretation = "";
if (worst && worst.ratio_vs_baseline >= 1.3) {
const meta = presetMeta.find(p => p.id === worst.modelId);
const pct = Math.round((worst.ratio_vs_baseline - 1) * 100);
interpretation = `⚠ ${tFmt("tax.interp.worst", {
label: meta?.label || worst.modelId,
pct,
}) || `${meta?.label || worst.modelId} costs ${pct}% more tokens than baseline for this text.`}
`;
} else if (worst && worst.ratio_vs_baseline <= 1.05) {
interpretation = `${t("tax.interp.uniform") || "✓ All tokenizers within ±5% — text is well-handled across vendors."}
`;
}
return `
${tFmt("tax.summary.input", { chars: res.chars.toLocaleString(), bytes: res.bytes.toLocaleString() }) || `Input: ${res.chars.toLocaleString()} chars, ${res.bytes.toLocaleString()} bytes`}
${blocks.dominant ? ` · ${t("tax.script_breakdown") || "scripts"}: ${fmtBlocks(blocks)} ` : ""}
${interpretation}
${t("tax.col.tokenizer") || "Tokenizer"}
${t("tax.col.tokens") || "Tokens"}
${t("tax.col.cpt") || "Chars/tok"}
${t("tax.col.ratio") || "Ratio"}
${rows}
${t("tax.attribution") || "Tokenizers via"}
@huggingface/transformers
(browser BPE runtime).
${t("tax.attribution.privacy") || "Text is tokenized locally — never leaves the browser."}
`;
}
async function runTaxTokenize() {
const text = $("tax-input")?.value || "";
if (!text) {
$("tax-status").textContent = t("tax.hint.empty") || "⚠ Paste some text first.";
return;
}
$("tax-status").textContent = t("tax.status.loading") || "⏳ Loading transformers.js + tokenizers (first run can take 5-15s)…";
$("tax-output").innerHTML = "";
const ids = TAX_PRESETS.map(p => p.id);
try {
const t0 = Date.now();
const res = await tokenizeAll(ids, text);
const ms = Date.now() - t0;
$("tax-output").innerHTML = renderTaxResult(res, TAX_PRESETS);
const okN = res.results.filter(r => r.ok).length;
$("tax-status").textContent = tFmt("tax.status.done", {
n: okN, total: ids.length, ms,
}) || `✅ ${okN}/${ids.length} tokenizers ran in ${ms}ms`;
} catch (e) {
$("tax-status").textContent = `❌ ${e.message || e}`;
}
}
$("tax-tokenize-btn")?.addEventListener("click", runTaxTokenize);
$("tax-sample-en-btn")?.addEventListener("click", () => {
$("tax-input").value = TAX_SAMPLES.english;
runTaxTokenize();
});
$("tax-sample-zh-btn")?.addEventListener("click", () => {
$("tax-input").value = TAX_SAMPLES.chinese;
runTaxTokenize();
});
$("tax-sample-ar-btn")?.addEventListener("click", () => {
$("tax-input").value = TAX_SAMPLES.arabic;
runTaxTokenize();
});
$("tax-sample-mixed-btn")?.addEventListener("click", () => {
$("tax-input").value = TAX_SAMPLES.mixed;
runTaxTokenize();
});
$("tax-sample-code-btn")?.addEventListener("click", () => {
$("tax-input").value = TAX_SAMPLES.code;
runTaxTokenize();
});
// ════════════════════════════════════════════════════════════════════
// LongScore mode (v0.8.8 anti-bullshit pack #14)
// ════════════════════════════════════════════════════════════════════
let __longscoreInited = false;
function initLongscore() {
if (__longscoreInited) return;
__longscoreInited = true;
// Eager-load KB so the first lookup is instant (KB is ~70KB, no real cost)
loadLongscoreKB().catch(e => {
console.warn("longscore_kb preload failed", e);
});
}
function lsFmtPct(x, sign) {
if (x == null) return "—";
const v = (x * 100);
return `${sign && v >= 0 ? "+" : ""}${v.toFixed(1)}%`;
}
function lcColor(avg) {
if (avg == null) return "#8b949e";
if (avg >= -0.02) return "#3fb950"; // green: no degradation
if (avg >= -0.10) return "#a5d36a"; // light green
if (avg >= -0.20) return "#f0883e"; // orange
if (avg >= -0.30) return "#f85149"; // red
return "#a01b1b"; // dark red: extreme
}
function renderLongscoreResult(res) {
if (res.code === "miss") {
return `
${t("longscore.miss.title") || "Model not found in KB"}
${tFmt("longscore.miss.body", { id: res.normalized_id, n: res.n_kb_total }) || `Looked up ${res.normalized_id}. KB has ${res.n_kb_total} models. Try a canonical HF id (e.g. Qwen2.5-72B-Instruct, Llama-3.1-70B-Instruct, Jamba-1.5-Mini).`}
${t("longscore.miss.suggest") || "Check coverage at"} RULER · HELMET .
`;
}
const verdictMap = {
no_degradation: { color: "#3fb950", label: t("longscore.verdict.no_degradation") || "✅ No degradation past short context" },
mild: { color: "#a5d36a", label: t("longscore.verdict.mild") || "🟢 Mild degradation (<10%)" },
moderate: { color: "#f0883e", label: t("longscore.verdict.moderate") || "🟠 Moderate degradation (10-20%)" },
severe: { color: "#f85149", label: t("longscore.verdict.severe") || "🔴 Severe degradation (20-30%)" },
extreme: { color: "#a01b1b", label: t("longscore.verdict.extreme") || "🚨 Extreme degradation (>30%)" },
};
let html = ``;
html += `
${escapeHtml(res.display_name)} `;
if (res.params_b) html += ` · ${res.params_b}B params `;
if (res.recipe_class) html += ` · ${escapeHtml(res.recipe_class)} `;
if (res.native_context_k) html += ` · native ctx ${res.native_context_k}K `;
html += `
`;
// RULER per-length + LongScore
if (res.ruler_long_score) {
const ls = res.ruler_long_score;
const v = verdictMap[res.verdict] || { color: "#8b949e", label: res.verdict };
html += `
${t("longscore.score_label") || "LongScore"}:
${lsFmtPct(ls.avg_lc, true)}
· Base = ${ls.base.toFixed(1)}% (mean of 4K, 8K)
`;
html += `
${v.label}
`;
// Per-length bars
html += `
${t("longscore.col.ctx") || "Context"}
${t("longscore.col.score") || "Score"}
${t("longscore.col.lc") || "LC"}
`;
const ctxKeys = ["4k", "8k", "16k", "32k", "64k", "128k"];
for (const k of ctxKeys) {
const score = res.ruler_per_ctx?.[k];
if (score == null) continue;
const isShort = k === "4k" || k === "8k";
const lc = ls.per_length_lc?.[k];
html += `
${k.toUpperCase()} ${isShort ? ` (base) ` : ""}
${score.toFixed(1)}%
${lc != null ? lsFmtPct(lc, true) : "—"}
`;
}
html += `
`;
} else {
// Helmet-only or partial
html += `
${t("longscore.no_ruler") || "⚠ No per-length data — LongScore not computable. Showing HELMET aggregate at 128K instead."}
`;
}
// HELMET breakdown if available
if (res.helmet) {
html += `
${t("longscore.helmet_label") || "HELMET 7-task breakdown"} (at 128K)
${t("longscore.col.task") || "Task"}
${t("longscore.col.score") || "Score"}
`;
if (res.helmet.overall != null) {
html += `Overall ${res.helmet.overall.toFixed(1)} `;
}
if (res.helmet.categories) {
for (const [task, score] of Object.entries(res.helmet.categories)) {
html += `${escapeHtml(task)} ${score != null ? score.toFixed(1) : "—"} `;
}
}
html += `
`;
}
html += `
${t("longscore.source_note") || "Data source"}: ${escapeHtml(res.source)} ·
LongScore metric
`;
html += `
`;
return html;
}
async function runLongscoreLookup() {
const id = $("longscore-input")?.value?.trim();
if (!id) {
$("longscore-status").textContent = t("longscore.hint.empty") || "⚠ Paste a model id first.";
return;
}
$("longscore-status").textContent = t("longscore.status.lookup") || "⏳ Looking up…";
$("longscore-output").innerHTML = "";
try {
const res = await longscoreLookup(id);
$("longscore-output").innerHTML = renderLongscoreResult(res);
if (res.code === "miss") {
$("longscore-status").textContent = t("longscore.status.miss") || "ℹ Model not in KB";
} else if (res.code === "ruler_hit") {
$("longscore-status").textContent = t("longscore.status.ruler_hit") || "✅ RULER per-length data found";
} else {
$("longscore-status").textContent = t("longscore.status.helmet_only") || "ℹ HELMET aggregate only (no per-length data)";
}
} catch (e) {
$("longscore-status").textContent = `❌ ${e.message || e}`;
console.error(e);
}
}
$("longscore-lookup-btn")?.addEventListener("click", runLongscoreLookup);
$("longscore-input")?.addEventListener("keydown", e => {
if (e.key === "Enter") {
e.preventDefault();
runLongscoreLookup();
}
});
$("longscore-example-good-btn")?.addEventListener("click", () => {
$("longscore-input").value = "Jamba-1.5-Large";
runLongscoreLookup();
});
$("longscore-example-mid-btn")?.addEventListener("click", () => {
$("longscore-input").value = "Llama-3.1-70B-Instruct";
runLongscoreLookup();
});
$("longscore-example-bad-btn")?.addEventListener("click", () => {
$("longscore-input").value = "dbrx";
runLongscoreLookup();
});
// ════════════════════════════════════════════════════════════════════
// ════════════════════════════════════════════════════════════════════
// 🧵 YaRN / RoPE Context-Extension Planner (v0.9)
// ════════════════════════════════════════════════════════════════════
let _yarnWired = false;
function initYarn() {
if (_yarnWired) return;
_yarnWired = true;
const fetchBtn = $("yarn-fetch-btn");
const planBtn = $("yarn-plan-btn");
fetchBtn?.addEventListener("click", async () => {
const modelId = ($("yarn-model").value || "").trim();
if (!modelId) { $("yarn-status").textContent = "⚠ " + t("yarn.need_id"); return; }
$("yarn-status").textContent = "⏳ " + t("yarn.fetching");
fetchBtn.disabled = true;
state.lastModelId = modelId;
try {
const cfg = await fetchHfConfig(modelId);
const rs = (cfg.rope_scaling && typeof cfg.rope_scaling === "object") ? cfg.rope_scaling : {};
// If the model already ships a rope_scaling block, original_max_position_embeddings
// is the TRUE trained context; max_position_embeddings is the already-extended figure.
const orig = rs.original_max_position_embeddings ?? cfg.max_position_embeddings ?? null;
const theta = cfg.rope_theta ?? 10000;
if (orig) $("yarn-orig").value = orig;
$("yarn-theta").value = theta;
const via = cfg.__via_mirror ? ` (via ${escapeHtml(cfg.__via_mirror)})` : "";
$("yarn-status").innerHTML =
`✅ ${escapeHtml(modelId)} ${via}: θ=${theta}, orig=${orig ?? "?"}. ${t("yarn.loaded_hint")}`;
} catch (err) {
$("yarn-status").textContent = `❌ ${err.message}`;
} finally {
fetchBtn.disabled = false;
}
});
planBtn?.addEventListener("click", () => {
const plan = planExtension({
originalCtx: parseFloat($("yarn-orig").value),
theta: parseFloat($("yarn-theta").value),
targetCtx: parseFloat($("yarn-target").value),
ropeType: $("yarn-type").value || null,
});
renderYarnPlan(plan);
});
}
// Context / horizon lengths: binary-K so 32768→32K, 131072→128K, 8192→8K
// (the convention everyone uses for context windows), not decimal-K (→33K).
function _yarnFmtK(n) {
if (n == null || !Number.isFinite(n)) return "—";
if (n >= 1048576) return (n / 1048576).toFixed(1) + "M";
if (n >= 1024) return Math.round(n / 1024) + "K";
return String(Math.round(n));
}
// RoPE θ is an arbitrary base, not a power of two → decimal M/K reads naturally
// (1000000→1M, 500000→500K, 40000→40K).
function _thetaFmt(n) {
if (n == null || !Number.isFinite(n)) return "—";
if (n >= 1e6) return (n / 1e6).toFixed(n % 1e6 === 0 ? 0 : 1) + "M";
if (n >= 1000) return (n / 1000).toFixed(n % 1000 === 0 ? 0 : 1) + "K";
return String(Math.round(n));
}
function _yarnFmtG(g) {
return (g == null || !Number.isFinite(g)) ? "—" : g.toFixed(3);
}
function _yarnWarnText(w) {
switch (w.code) {
case "theta_eff_estimate": return t("yarn.warn.theta_eff_estimate");
case "aggressive_factor": return `${t("yarn.warn.aggressive")} (${w.params.factor}×)`;
case "horizon_short": return `${t("yarn.warn.horizon_short")} (d_horizon ${_yarnFmtK(w.params.dHorizon)} < L ${_yarnFmtK(w.params.target)}, γ_eff ${_yarnFmtG(w.params.gammaEff)})`;
case "finetune_note": return t("yarn.warn.finetune");
default: return w.code;
}
}
function renderYarnPlan(p) {
const out = $("yarn-output");
if (!out) return;
out.style.display = "";
const errMap = {
no_original_ctx: "yarn.err.no_orig",
no_theta: "yarn.err.no_theta",
no_target: "yarn.err.no_target",
};
if (errMap[p.verdict]) {
out.innerHTML = `⚠ ${t(errMap[p.verdict])}
`;
return;
}
if (p.verdict === "no_extension_needed") {
out.innerHTML =
`✅ ${t("yarn.verdict.no_extension_needed")}
L=${_yarnFmtK(p.targetCtx)} ≤ trained ${_yarnFmtK(p.originalCtx)}. γ_Padé=${_yarnFmtG(p.gammaNaive)}, d_horizon=${_yarnFmtK(p.dHorizonNaive)}.
`;
return;
}
const meta = ({
healthy: { emoji: "✅", cls: "v-yes" },
usable_with_care: { emoji: "⚠️", cls: "v-deg" },
needs_finetune: { emoji: "🔧", cls: "v-deg" },
degrades: { emoji: "🚨", cls: "v-no" },
})[p.verdict] || { emoji: "❓", cls: "v-deg" };
const cfgJson = JSON.stringify({ rope_scaling: p.config }, null, 2);
const horizonOk = p.dHorizonEff != null && p.dHorizonEff >= p.targetCtx;
const warnHtml = p.warnings.map(w => `${_yarnWarnText(w)} `).join("");
const td = "padding:3px 10px 3px 0;";
out.innerHTML = `
${meta.emoji} ${t("yarn.verdict." + p.verdict)}
${t("yarn.r.factor")} ${p.factor}× (${_yarnFmtK(p.originalCtx)} → ${_yarnFmtK(p.targetCtx)})
${t("yarn.r.method")} ${p.ropeType}
γ ${t("yarn.r.naive")} ${_yarnFmtG(p.gammaNaive)}${p.gammaNaive <= 0 ? ` 🚨 ${t("yarn.r.collapsed")}` : ""}
γ ${t("yarn.r.eff")} ${_yarnFmtG(p.gammaEff)}
θ_eff ${_thetaFmt(p.thetaEff)}${p.thetaEff > p.theta ? ` (↑ ${t("yarn.r.from")} ${_thetaFmt(p.theta)})` : ""}
d_horizon ${t("yarn.r.eff")} ${_yarnFmtK(p.dHorizonEff)} ${horizonOk ? "✅ ≥ L" : "⚠ < L"}
${t("yarn.r.snippet")}
${escapeHtml(cfgJson)}
📋 ${t("yarn.copy_btn")}
${warnHtml ? `` : ""}`;
$("yarn-copy-btn")?.addEventListener("click", async () => {
try {
await navigator.clipboard.writeText(cfgJson);
$("yarn-copy-btn").textContent = "✓ " + t("yarn.copied");
} catch (e) { /* clipboard blocked */ }
});
}
// ════════════════════════════════════════════════════════════════════
// 🧊 GGUF Validity Bridge (v0.9.1)
// ════════════════════════════════════════════════════════════════════
let _ggufWired = false;
let _ggufFiles = [];
let _ggufCfgCache = {}; // "repo|file" → ggufToConfig result (geometry is shared across quants)
// Parse a .gguf header once and cache. The architecture/θ/context/head geometry
// is identical across every quant of the same model — only the quant scheme
// differs — so one parsed file is enough to score the whole repo.
async function ggufGetCfg(repo, file) {
const key = `${repo}|${file}`;
if (_ggufCfgCache[key]) return _ggufCfgCache[key];
const url = `https://huggingface.co/${repo}/resolve/main/${file}`;
const meta = await fetchGgufMetadata(url);
const cfg = ggufToConfig(meta);
if (!cfg.quant_scheme) {
const q = quantFromFilename(file);
cfg.quant_label = cfg.quant_label || q.label;
cfg.quant_scheme = q.scheme;
}
cfg.__bytesRead = meta.bytesRead;
_ggufCfgCache[key] = cfg;
return cfg;
}
function initGguf() {
if (_ggufWired) return;
_ggufWired = true;
const listBtn = $("gguf-list-btn");
const analyzeBtn = $("gguf-analyze-btn");
const allBtn = $("gguf-all-btn");
const fileSel = $("gguf-file");
listBtn?.addEventListener("click", async () => {
const repo = ($("gguf-repo").value || "").trim();
if (!repo) { $("gguf-status").textContent = "⚠ " + t("gguf.need_repo"); return; }
$("gguf-status").textContent = "⏳ " + t("gguf.listing");
listBtn.disabled = true;
state.lastModelId = repo;
try {
const files = await listGgufFiles(repo);
if (!files.length) { $("gguf-status").textContent = "⚠ " + t("gguf.no_files"); fileSel.disabled = true; analyzeBtn.disabled = true; return; }
fileSel.innerHTML = files.map(f => `${escapeHtml(f)} `).join("");
// Default-select a Q4_K_M (the community sweet spot) if present.
const def = files.find(f => /q4_k_m/i.test(f)) || files[0];
fileSel.value = def;
fileSel.disabled = false;
analyzeBtn.disabled = false;
$("gguf-all-btn").disabled = false;
_ggufFiles = files;
$("gguf-status").innerHTML = `✅ ${files.length} ${t("gguf.found")} — ${t("gguf.pick_hint")}`;
} catch (err) {
$("gguf-status").textContent = `❌ ${err.message}`;
} finally {
listBtn.disabled = false;
}
});
analyzeBtn?.addEventListener("click", async () => {
const repo = ($("gguf-repo").value || "").trim();
const file = fileSel.value;
if (!repo || !file) return;
$("gguf-status").textContent = "⏳ " + t("gguf.reading");
analyzeBtn.disabled = true;
try {
const cfg = await ggufGetCfg(repo, file);
const target = parseFloat($("gguf-target").value) || null;
const result = analyzeGguf(cfg, target);
$("gguf-status").innerHTML = `✅ ${t("gguf.read_ok")} (${(cfg.__bytesRead / 1024 / 1024).toFixed(1)} MB header)`;
renderGgufResult(cfg, result);
} catch (err) {
$("gguf-status").textContent = `❌ ${ggufErrMsg(err)}`;
} finally {
analyzeBtn.disabled = false;
}
});
allBtn?.addEventListener("click", async () => {
const repo = ($("gguf-repo").value || "").trim();
const file = fileSel.value;
if (!repo || !file) return;
$("gguf-status").textContent = "⏳ " + t("gguf.reading");
allBtn.disabled = true; analyzeBtn.disabled = true;
try {
// One header parse gives the shared geometry; score every quant from it.
const cfg = await ggufGetCfg(repo, file);
const target = parseFloat($("gguf-target").value) || null;
// Dedupe repo files to one row per quant label (drop shard suffixes).
const seen = new Set();
const rows = [];
for (const f of _ggufFiles) {
const q = quantFromFilename(f);
if (q.label === "?" || seen.has(q.label)) continue;
seen.add(q.label);
const res = analyzeGguf({ ...cfg, quant_label: q.label, quant_scheme: q.scheme }, target);
rows.push({ label: q.label, scheme: q.scheme, res });
}
// Best precision first: lowest γ-shift (baseline F16 = 0) at the top.
rows.sort((a, b) => (a.res.quant?.gamma_shift ?? 0) - (b.res.quant?.gamma_shift ?? 0));
$("gguf-status").innerHTML = `✅ ${t("gguf.read_ok")} (${(cfg.__bytesRead / 1024 / 1024).toFixed(1)} MB header)`;
renderGgufComparison(cfg, rows);
} catch (err) {
$("gguf-status").textContent = `❌ ${ggufErrMsg(err)}`;
} finally {
allBtn.disabled = false; analyzeBtn.disabled = false;
}
});
}
function ggufErrMsg(err) {
return ({
not_a_gguf_file: t("gguf.err.not_gguf"),
gguf_metadata_too_large: t("gguf.err.too_large"),
})[err.message] || err.message;
}
function renderGgufResult(cfg, r) {
const out = $("gguf-output");
if (!out) return;
out.style.display = "";
if (r.verdict === "incomplete") {
out.innerHTML = `⚠ ${t("gguf.err.incomplete")}
`;
return;
}
const meta = ({
healthy: { emoji: "✅", cls: "v-yes" },
usable_with_care: { emoji: "⚠️", cls: "v-deg" },
degrades: { emoji: "🚨", cls: "v-no" },
})[r.verdict] || { emoji: "❓", cls: "v-deg" };
const td = "padding:3px 12px 3px 0;";
const gqa = (cfg.num_attention_heads && cfg.num_key_value_heads && cfg.num_key_value_heads < cfg.num_attention_heads)
? `GQA ${cfg.num_attention_heads}:${cfg.num_key_value_heads}` : "MHA";
// Quant block (may be null for F16/F32 files).
let quantHtml = "";
if (r.quant) {
const regimeEmoji = ({ safe: "✅", mild: "🟡", significant: "🟠", cliff: "🚨" })[r.quant.regime] || "";
const dp = r.quant.delta_ppl;
quantHtml = `
${t("gguf.r.quant")} ${r.quantLabel || "?"}
${t("gguf.r.gamma_shift")} −${_yarnFmtG(r.quant.gamma_shift)} ${regimeEmoji} ${t("quant.regime." + r.quant.regime) || r.quant.regime}
ΔPPL ≈ +${dp.mid} (${dp.low}–${dp.high}) `;
} else {
quantHtml = `${t("gguf.r.quant")} ${r.quantLabel || "F16/F32"} ${t("gguf.r.no_quant_shift")} `;
}
out.innerHTML = `
${meta.emoji} ${t("gguf.verdict." + r.verdict)}
${t("gguf.r.arch")} ${escapeHtml(r.arch)} · ${gqa} · θ=${_thetaFmt(r.theta)}
${t("gguf.r.ctx_train")} ${_yarnFmtK(r.nCtx)}
${t("gguf.r.horizon_fp16")} ${_yarnFmtK(r.dHoriz)} (γ=${_yarnFmtG(r.gammaTrain)})
${quantHtml}
γ @ L=${_yarnFmtK(r.L)} ${t("gguf.r.after_quant")}${_yarnFmtG(r.gammaQuant)} (fp16: ${_yarnFmtG(r.gammaAtL)})
${t("gguf.r.note")}
`;
}
function renderGgufComparison(cfg, rows) {
const out = $("gguf-output");
if (!out) return;
out.style.display = "";
const gqa = (cfg.num_attention_heads && cfg.num_key_value_heads && cfg.num_key_value_heads < cfg.num_attention_heads)
? `GQA ${cfg.num_attention_heads}:${cfg.num_key_value_heads}` : "MHA";
// Short verdict label = the word before the em-dash of the full verdict string
// (works in every language: "HEALTHY — …", "SANO — …", "健康 —— …").
const short = v => (t("gguf.verdict." + v) || v).split(/——|—| - /)[0].trim();
const emo = v => ({ healthy: "✅", usable_with_care: "⚠️", degrades: "🚨" })[v] || "❓";
const td = "padding:3px 14px 3px 0;";
const head = `
${t("gguf.r.quant")} ${t("gguf.r.gamma_shift")}
${t("gguf.col.gamma_at_l")} ${t("gguf.col.verdict")} `;
const body = rows.map(({ label, res }) => {
const shift = res.quant ? "−" + _yarnFmtG(res.quant.gamma_shift) : "—";
return `${escapeHtml(label)}${shift}
${_yarnFmtG(res.gammaQuant)}
${emo(res.verdict)} ${short(res.verdict)} `;
}).join("");
// d_horizon is θ-set → identical for every quant; show it once in the header line.
out.innerHTML = `${t("gguf.compare_title")}
${escapeHtml(cfg.architecture)} · ${gqa} · θ=${_thetaFmt(cfg.rope_theta)} · ctx ${_yarnFmtK(cfg.context_length)} · horizon ${_yarnFmtK(rows[0]?.res.dHoriz)} · L=${_yarnFmtK(rows[0]?.res.L)}
${t("gguf.r.note")}
`;
}
// ════════════════════════════════════════════════════════════════════
// Bootstrap
// ════════════════════════════════════════════════════════════════════
initI18n();
// Pyodide-independent panels: render immediately so they survive a Pyodide
// load failure (CDN blocked / offline / slow region). These use only fetch +
// DOM, never state.pyodide — must NOT be gated behind enableUI().
renderFalsificationDashboard();
loadCommunityFeed();
loadPyodideAndTaf().catch(err => {
setStatus(`❌ Failed to initialise: ${err.message || err}`);
console.error(err);
});