Files
llm-atlas/scripts/check-representation-browser.mjs
T

305 lines
16 KiB
JavaScript

import { writeFileSync } from "node:fs";
const cdpPort = process.env.CDP_PORT ?? "9227";
const baseUrl = process.env.SITE_URL ?? "http://127.0.0.1:4327";
const pages = await fetch(`http://127.0.0.1:${cdpPort}/json/list`).then((response) => response.json());
const page = pages.find((entry) => entry.type === "page");
if (!page) throw new Error(`CDP ${cdpPort} 没有可用页面`);
const socket = new WebSocket(page.webSocketDebuggerUrl);
await new Promise((resolve, reject) => {
socket.addEventListener("open", resolve, { once: true });
socket.addEventListener("error", reject, { once: true });
});
let nextId = 0;
const pending = new Map();
const exceptions = [];
socket.addEventListener("message", (event) => {
const message = JSON.parse(event.data);
if (message.id && pending.has(message.id)) {
const { resolve, reject } = pending.get(message.id);
pending.delete(message.id);
if (message.error) reject(new Error(message.error.message));
else resolve(message.result);
}
if (message.method === "Runtime.exceptionThrown") {
exceptions.push(message.params.exceptionDetails.exception?.description ?? message.params.exceptionDetails.text);
}
});
const command = (method, params = {}) => new Promise((resolve, reject) => {
const id = ++nextId;
pending.set(id, { resolve, reject });
socket.send(JSON.stringify({ id, method, params }));
});
const pause = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));
const evaluate = async (expression) => {
const result = await command("Runtime.evaluate", { expression, returnByValue: true, awaitPromise: true });
if (result.exceptionDetails) throw new Error(result.exceptionDetails.exception?.description ?? result.exceptionDetails.text);
return result.result.value;
};
const navigate = async (path) => {
await command("Page.navigate", { url: `${baseUrl}${path}` });
for (let attempt = 0; attempt < 70; attempt += 1) {
await pause(100);
if (await evaluate("document.readyState === 'complete'")) return;
}
throw new Error(`${path} 加载超时`);
};
const screenshot = async (path) => {
const result = await command("Page.captureScreenshot", { format: "png", captureBeyondViewport: false });
writeFileSync(path, Buffer.from(result.data, "base64"));
};
await command("Page.enable");
await command("Runtime.enable");
await command("Emulation.setDeviceMetricsOverride", {
width: 1440,
height: 1100,
deviceScaleFactor: 1,
mobile: false,
});
await navigate("/architecture/representation/");
await screenshot("/tmp/llm-atlas-representation-desktop.png");
const overview = await evaluate(`(() => ({
title: document.querySelector("h1")?.textContent.trim(),
sections: document.querySelectorAll(".article-section").length,
tocLinks: document.querySelectorAll(".side-rail a").length,
paperLinks: document.querySelectorAll(".paper-chain a").length,
ledgers: document.querySelectorAll(".rep-ledgers > article").length,
labTabs: document.querySelectorAll("[data-rep-tab]").length,
labPanels: document.querySelectorAll("[data-rep-panel]").length,
navLinks: document.querySelectorAll(".top-nav a").length,
activeNav: document.querySelector('.top-nav a[aria-current="page"]')?.textContent.trim(),
documentOverflow: document.documentElement.scrollWidth - document.documentElement.clientWidth,
}))()`);
const token = await evaluate(`(() => {
const root = document.querySelector("[data-representation-lab]");
const read = () => ({
visible: root.querySelector("[data-rep-panel]:not([hidden])").dataset.repPanel,
steps: root.querySelector("[data-token-steps]").textContent.trim(),
embedding: root.querySelector("[data-embedding-params]").textContent.trim(),
io: root.querySelector("[data-io-params]").textContent.trim(),
memory: root.querySelector("[data-embedding-memory]").textContent.trim(),
vector: root.querySelector("[data-context-vector]").textContent.trim(),
shift: root.querySelector("[data-vector-shift]").textContent.trim(),
explain: root.querySelector("[data-vector-explain]").textContent.trim(),
boundary: root.querySelector("[data-token-boundary]").textContent.trim(),
});
const initial = read();
const unit = root.querySelector("[data-token-unit]");
unit.value = "byte";
unit.dispatchEvent(new Event("input", { bubbles: true }));
const tying = root.querySelector("[data-weight-tying]");
tying.checked = false;
tying.dispatchEvent(new Event("input", { bubbles: true }));
const context = root.querySelector("[data-context]");
context.value = "finance";
context.dispatchEvent(new Event("input", { bubbles: true }));
const changed = read();
return { initial, changed };
})()`);
const position = await evaluate(`(() => {
const root = document.querySelector("[data-representation-lab]");
root.querySelector('[data-rep-tab="position"]').click();
const read = () => ({
visible: root.querySelector("[data-rep-panel]:not([hidden])").dataset.repPanel,
regime: root.querySelector("[data-position-regime]").textContent.trim(),
term: root.querySelector("[data-explicit-term]").textContent.trim(),
mode: root.querySelector("[data-position-mode]").textContent.trim(),
equation: root.querySelector("[data-position-equation]").textContent.trim(),
score: root.querySelector("[data-position-score]").textContent.trim(),
explain: root.querySelector("[data-position-explain]").textContent.trim(),
boundary: root.querySelector("[data-position-boundary]").textContent.trim(),
});
const initial = read();
const key = root.querySelector("[data-key-position]");
const train = root.querySelector("[data-train-window]");
train.value = "64";
train.dispatchEvent(new Event("input", { bubbles: true }));
key.value = "192";
key.dispatchEvent(new Event("input", { bubbles: true }));
const ropeOutside = read();
root.querySelector('[data-position-scheme="alibi"]').click();
const alibi = read();
root.querySelector('[data-position-scheme="nope"]').click();
const nope = read();
return { initial, ropeOutside, alibi, nope };
})()`);
const norm = await evaluate(`(() => {
const root = document.querySelector("[data-representation-lab]");
root.querySelector('[data-rep-tab="norm"]').click();
const read = () => ({
visible: root.querySelector("[data-rep-panel]:not([hidden])").dataset.repPanel,
rms: root.querySelector("[data-final-rms]").textContent.trim(),
identity: root.querySelector("[data-identity-gain]").textContent.trim(),
peak: root.querySelector("[data-softmax-peak]").textContent.trim(),
target: root.querySelector("[data-norm-target]").textContent.trim(),
equation: root.querySelector("[data-norm-equation]").textContent.trim(),
boundary: root.querySelector("[data-norm-boundary]").textContent.trim(),
});
const initial = read();
const depth = root.querySelector("[data-depth]");
const logits = root.querySelector("[data-logit-scale]");
depth.value = "192";
depth.dispatchEvent(new Event("input", { bubbles: true }));
logits.value = "160";
logits.dispatchEvent(new Event("input", { bubbles: true }));
const unbounded = read();
root.querySelector('[data-norm-topology="qk"]').click();
const qk = read();
root.querySelector('[data-norm-topology="post"]').click();
const post = read();
return { initial, unbounded, qk, post };
})()`);
const residual = await evaluate(`(() => {
const root = document.querySelector("[data-representation-lab]");
root.querySelector('[data-rep-tab="residual"]').click();
const read = () => ({
visible: root.querySelector("[data-rep-panel]:not([hidden])").dataset.repPanel,
route: root.querySelector("[data-route-name]").textContent.trim(),
states: root.querySelector("[data-live-states]").textContent.trim(),
choice: root.querySelector("[data-depth-choice]").textContent.trim(),
routeExplain: root.querySelector("[data-route-explain]").textContent.trim(),
activation: root.querySelector("[data-activation-name]").textContent.trim(),
max: root.querySelector("[data-activation-max]").textContent.trim(),
bound: root.querySelector("[data-activation-bound]").textContent.trim(),
activationExplain: root.querySelector("[data-activation-explain]").textContent.trim(),
});
const initial = read();
root.querySelector('[data-route="mhc"]').click();
const mhc = read();
root.querySelector('[data-route="attnres"]').click();
const attnres = read();
root.querySelector('[data-activation="clamp"]').click();
const clamp = read();
root.querySelector('[data-activation="situ"]').click();
const situ = read();
const firstTab = root.querySelector('[data-rep-tab="token"]');
firstTab.focus();
firstTab.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowRight", bubbles: true }));
return {
initial,
mhc,
attnres,
clamp,
situ,
keyboardSelected: root.querySelector('[data-rep-tab][aria-selected="true"]').dataset.repTab,
keyboardVisible: root.querySelector("[data-rep-panel]:not([hidden])").dataset.repPanel,
};
})()`);
await evaluate(`(() => {
document.querySelector("[data-representation-lab]").scrollIntoView({ block: "start", behavior: "instant" });
window.scrollBy(0, -82);
})()`);
await pause(180);
await screenshot("/tmp/llm-atlas-representation-lab-desktop.png");
await navigate("/");
const home = await evaluate(`(() => ({
releaseCards: document.querySelectorAll(".release-card").length,
firstRelease: document.querySelector(".release-card h2").textContent.trim(),
firstHref: document.querySelector(".release-card").getAttribute("href"),
paperCount: document.querySelector(".hero-stats div:nth-child(3) b").textContent.trim(),
topicCount: document.querySelector(".hero-stats div:nth-child(1) b").textContent.trim(),
navLinks: document.querySelectorAll(".top-nav a").length,
}))()`);
await navigate("/papers/");
const papers = await evaluate(`(() => {
const button = [...document.querySelectorAll("[data-filter]")].find((node) => node.textContent.trim() === "表示");
button?.click();
return {
total: document.querySelectorAll("[data-paper]").length,
visible: document.querySelectorAll("[data-paper]:not([hidden])").length,
hasFilter: Boolean(button),
};
})()`);
await command("Emulation.setDeviceMetricsOverride", {
width: 390,
height: 844,
deviceScaleFactor: 1,
mobile: true,
});
await navigate("/architecture/representation/");
const mobile = await evaluate(`(() => {
const root = document.querySelector("[data-representation-lab]");
root.scrollIntoView({ block: "start", behavior: "instant" });
const toggle = document.querySelector("#menu-toggle");
toggle?.click();
return {
documentOverflow: document.documentElement.scrollWidth - document.documentElement.clientWidth,
menuVisible: getComputedStyle(toggle).display !== "none",
menuOpen: toggle.getAttribute("aria-expanded"),
mobileLinks: document.querySelectorAll("#mobile-nav a").length,
tabs: root.querySelectorAll("[data-rep-tab]").length,
offenders: [...document.querySelectorAll("body *")]
.filter((node) => !node.closest(".paper-chain, .rep-ledgers, .position-table"))
.filter((node) => node.getBoundingClientRect().right > document.documentElement.clientWidth + 1)
.slice(0, 12)
.map((node) => ({
tag: node.tagName,
className: typeof node.className === "string" ? node.className : "",
right: Math.round(node.getBoundingClientRect().right),
width: Math.round(node.getBoundingClientRect().width),
})),
};
})()`);
await evaluate(`(() => {
document.querySelector("#menu-toggle")?.click();
window.scrollBy(0, -82);
})()`);
await pause(180);
await screenshot("/tmp/llm-atlas-representation-mobile.png");
const report = { overview, token, position, norm, residual, home, papers, mobile, exceptions };
console.log(JSON.stringify(report, null, 2));
const numeric = (text) => Number.parseFloat(text.replaceAll(",", ""));
const failures = [];
if (!overview.title.includes("表示") || !overview.title.includes("残差")) failures.push("章节标题异常");
if (overview.sections !== 29 || overview.tocLinks !== 29) failures.push("章节 / 目录数量异常");
if (overview.paperLinks !== 66) failures.push("正式论文链不是 66 个节点");
if (overview.ledgers !== 20) failures.push("二十张问题账结构异常");
if (overview.labTabs !== 4 || overview.labPanels !== 4) failures.push("四联实验结构异常");
if (overview.navLinks !== 20 || home.navLinks !== 20 || mobile.mobileLinks !== 20 || overview.activeNav !== "表示深度") failures.push("全站导航未同步表示专题");
if (overview.documentOverflow > 1 || mobile.documentOverflow > 1) failures.push("桌面或移动端存在文档级横向溢出");
if (token.initial.visible !== "token" || numeric(token.initial.steps) !== 9 || !token.initial.memory.includes("GiB")) failures.push("Token 实验初始状态异常");
if (numeric(token.changed.steps) <= numeric(token.initial.steps) || numeric(token.changed.io) <= numeric(token.initial.io) || !token.changed.explain.includes("金融")) failures.push("Byte / untie / context 操作没有改变表示账");
if (position.initial.visible !== "position" || position.initial.regime !== "INTERPOLATION") failures.push("位置实验初始状态异常");
if (position.ropeOutside.regime !== "EXTRAPOLATION" || !position.ropeOutside.boundary.includes("192")) failures.push("位置外推窗口没有被识别");
if (!position.alibi.term.includes("bias") || !position.alibi.explain.includes("logit")) failures.push("ALiBi 机制展示异常");
if (!position.nope.mode.includes("NO EXPLICIT") || !position.nope.explain.includes("隐式")) failures.push("NoPE 边界说明异常");
if (norm.initial.visible !== "norm" || numeric(norm.unbounded.rms) <= numeric(norm.initial.rms)) failures.push("深度 / RMS 教学曲线异常");
if (numeric(norm.qk.peak) >= numeric(norm.unbounded.peak) || !norm.qk.boundary.includes("QK-Norm")) failures.push("QK-Norm 没有压低 toy softmax 峰值");
if (numeric(norm.post.rms) !== 1 || numeric(norm.post.identity) >= 1) failures.push("Post-LN 拓扑展示异常");
if (residual.initial.visible !== "residual" || !residual.initial.route.includes("ATTENTION")) failures.push("Residual 实验初始状态异常");
if (!residual.mhc.route.includes("MANIFOLD") || numeric(residual.mhc.states) !== 4 || !residual.mhc.routeExplain.includes("Birkhoff")) failures.push("mHC 教学状态异常");
if (numeric(residual.attnres.states) !== 9 || !residual.attnres.routeExplain.includes("93")) failures.push("K3 Block AttnRes 来源数异常");
if (!residual.clamp.activation.includes("V4") || !residual.clamp.bound.includes("100")) failures.push("DeepSeek-V4 clamp 展示异常");
if (!residual.situ.activation.includes("KIMI") || !residual.situ.bound.includes("100")) failures.push("K3 SiTU 上界展示异常");
if (residual.keyboardSelected !== "position" || residual.keyboardVisible !== "position") failures.push("实验键盘 tab 导航异常");
if (home.releaseCards !== 17 || !home.firstRelease.includes("局部 uniform routing 进入完整训练后") || home.firstHref !== "/k3/") failures.push("首页 K3 首发入口异常");
if (home.paperCount !== "486" || home.topicCount !== "17" || papers.total !== 486 || !papers.hasFilter || papers.visible < 30) failures.push("首页 / 论文库表示索引异常");
if (!mobile.menuVisible || mobile.menuOpen !== "true" || mobile.tabs !== 4) failures.push("移动端导航或实验异常");
if (mobile.offenders.length) failures.push(`移动端越界元素:${JSON.stringify(mobile.offenders)}`);
if (exceptions.length) failures.push(`浏览器异常:${exceptions.join(" | ")}`);
if (failures.length) {
console.error(`\nFAIL\n- ${failures.join("\n- ")}`);
process.exitCode = 1;
} else {
console.log("\nPASS representation browser regression");
}
socket.close();