/** * Local preview proxy for UI work — real fleet data, zero production risk. * * bun scripts/dev-preview.ts # then: API_TARGET=http://127.0.0.1:5999 bun run dev:web * * Why this exists rather than just running the control server locally: the * control plane is not a read-only process. Its node sweep marks satellites * offline and rolls their in-flight work back, and the health sweep sends Lark * alerts to the real group. Pointing a laptop copy at the production DATABASE_URL * to look at some CSS would do both. So this proxies the read-only internal API * instead and REFUSES anything that isn't a GET — the guarantee is structural, * not a promise to be careful. * * It also synthesises node metrics. The deployed control plane predates 节点健康, * so /api/nodes carries no `metrics` or `health` yet and the panel would render * its (correct, but not very informative) 「卫星版本过旧」 empty state. With * --synth the proxy fills those two fields in so the panel can be reviewed * before anything ships. The numbers are FABRICATED and labelled as such in the * console; everything else on screen is the real fleet. */ import { defaultProductProfiles } from "../shared/src/product_profiles"; const UPSTREAM = (process.env.PREVIEW_UPSTREAM || "https://streamline-internal-api.bytedance.net").replace(/\/$/, ""); const PORT = Number(process.env.PREVIEW_PORT || 5999); const SYNTH = process.argv.includes("--synth"); /** Identity /api/me reports locally, so the 管理 overlay is reachable. */ const PREVIEW_ADMIN = process.env.PREVIEW_ADMIN || "jiaopengwen"; /** In-memory Bits token flag for this preview process only. */ let previewHasBitsToken = false; interface Node { id: string; clusters?: string[]; live?: boolean; lastSeenAt: number; [k: string]: unknown } /** * Deterministic per-node pseudo-metrics: same node, same numbers on every * reload, so a review isn't chasing values that move under it. Seeded off the * node id, with a couple of deliberately unhealthy nodes so the alert states * (and the ordering that floats them up) are actually visible. */ function synthMetrics(node: Node, index: number) { let h = 2166136261; for (const ch of node.id) { h ^= ch.charCodeAt(0); h = Math.imul(h, 16777619); } const r = (n: number, lo: number, hi: number) => lo + (Math.abs(h >> n) % (hi - lo)); // Make exactly one node disk-critical and one logged-out, chosen by POSITION // rather than by a hash of the name: a hash left it to chance whether any node // landed in an unhealthy branch at all, and the first run produced seven // healthy nodes — which is precisely the state a review of the alert UI cannot // use. Position guarantees both states are on screen for any fleet of ≥ 5. const sickDisk = index === 1; const loggedOut = index === 4; const now = Date.now(); const diskUsedPct = sickDisk ? 93.4 : r(3, 38, 82); return { at: now, cores: r(5, 8, 16), cpuPct: r(7, 4, 78), loadPct: r(9, 10, 90), uptimeSec: r(11, 3600, 3_000_000), diskUsedPct, diskFreeBytes: Math.round((100 - diskUsedPct) / 100 * 500e9), memTotalBytes: 32 * 2 ** 30, memUsedPct: r(13, 35, 88), swapUsedBytes: r(15, 0, 9) * 2 ** 30, slowAt: now - 12_000, slowError: null, slowMs: r(17, 120, 400), agent: { mode: "pty", loggedIn: loggedOut ? false : true, authMethod: "claude.ai", checkedAt: now - 12_000, error: null, authGateMs: 0, }, claudeUsage: { at: now - 40_000, ms: 280 + (index * 17) % 90, error: null, email: `sat-${index}@bytedance.com`, plan: "max", // Every other synthetic node shares a machine with its predecessor, so // the dedup path is what the preview actually exercises. machineId: `mach-${Math.floor(index / 2)}`, fiveHour: { usedPct: loggedOut ? 0 : r(19, 8, 92), resetsAt: now + 3 * 3600_000 }, sevenDay: { usedPct: loggedOut ? 12 : r(21, 20, 80), resetsAt: now + 2 * 86400_000 }, sevenDaySonnet: null, sevenDayOpus: null, quotaAt: now - 40_000, tokens: { input: 2_000_000 + index * 750_000, output: 180_000 + index * 40_000, cacheRead: 8_000_000 + index * 1_200_000, cacheCreation: 400_000 + index * 80_000, }, tokensAsOf: new Date(now).toISOString().slice(0, 10), startedAt: now - (60 + index * 20) * 86_400_000, apiCostUsd: null, daily: synthDaily(now, index), dailyFromAt: now - Math.min(60 + index * 20, 24) * 86_400_000, }, }; } /** A ~24-day series with a couple of idle days, so 覆盖/活跃 differ in the preview. */ function synthDaily(now: number, index: number) { const out = []; for (let back = 23; back >= 0; back--) { const idle = (back + index) % 7 === 0; const scale = idle ? 0 : 1 + ((back * 7 + index * 13) % 9) / 4; out.push({ date: new Date(now - back * 86_400_000).toISOString().slice(0, 10), tokens: { input: Math.round(60_000 * scale), output: Math.round(9_000 * scale), cacheRead: Math.round(1_400_000 * scale), cacheCreation: Math.round(70_000 * scale), }, apiCostUsd: 1.1 * scale, }); } return out; } function synthHealth(node: Node, m: ReturnType) { const alerts: unknown[] = []; if (!node.live) { alerts.push({ rule: "offline", severity: "critical", message: `节点离线,最后心跳 ${Math.round((Date.now() - node.lastSeenAt) / 60000)} 分钟前`, since: Date.now() - 600_000 }); } else { if (m.diskUsedPct >= 90) { alerts.push({ rule: "disk", severity: "critical", message: `磁盘占用 ${m.diskUsedPct.toFixed(1)}%(阈值 90%),剩余 ${(m.diskFreeBytes / 2 ** 30).toFixed(0)}GB`, since: Date.now() - 900_000 }); } if (m.agent.loggedIn === false) { alerts.push({ rule: "agent", severity: "critical", message: "claude 未登录(本地凭据检查未通过),需在该节点执行 claude /login", since: Date.now() - 300_000 }); } } const level = alerts.length ? "critical" : "ok"; return { level, alerts }; } const server = Bun.serve({ port: PORT, hostname: "127.0.0.1", async fetch(req) { const url = new URL(req.url); // Structural read-only guarantee: a mutating request never reaches upstream. if (req.method !== "GET" && req.method !== "HEAD") { // Local-only: remember a pasted Bits token so the MR-help card can hide // step 3 after save. Never forwarded upstream. if (req.method === "PUT" && url.pathname === "/api/me/bits-token") { const body = await req.json().catch(() => ({})) as { token?: unknown }; const raw = typeof body.token === "string" ? body.token.trim().replace(/^Bearer\s+/i, "") : ""; if (!raw || raw.length < 8) { return Response.json({ error: "token 格式不对:粘贴 Bits「My Token」里 Bearer 后面那一串即可" }, { status: 400 }); } previewHasBitsToken = true; console.log("[preview] saved Bits token in memory (not sent upstream)"); return Response.json({ ok: true, hasToken: true }); } console.warn(`[preview] blocked ${req.method} ${url.pathname}`); return Response.json({ error: "preview proxy is read-only" }, { status: 405 }); } const target = UPSTREAM + url.pathname + url.search; let res: Response; try { res = await fetch(target, { headers: { accept: "application/json" } }); } catch (err) { console.error(`[preview] upstream failed: ${(err as Error).message}`); return Response.json({ error: "upstream unreachable" }, { status: 502 }); } // Grant admin locally. // // The internal read-only API resolves no identity (it is token-free by // design), so /api/me answers isAdmin:false and the app hides 管理 entirely — // which makes every admin-only panel, including 节点健康, impossible to review // locally. This is a fabricated identity for a localhost-bound preview // proxy; it grants nothing anywhere else, because the real enforcement is // server-side on each mutation route and every mutation is 405'd above. if (url.pathname === "/api/me") { return Response.json({ email: `${PREVIEW_ADMIN}@bytedance.com`, isAdmin: true, isSuper: true, isLab: true, hasBitsToken: previewHasBitsToken, }); } if (url.pathname === "/api/me/bits-token") { return Response.json({ hasToken: previewHasBitsToken }); } // Admin-gated upstream (403 without a session) — serve the list the panel // expects so 通用设置 renders instead of erroring. if (url.pathname === "/api/admins") { return Response.json({ supers: ["jiaopengwen", "daizheng.l"], admins: [] }); } // Product profiles are not on production yet; the SPA fallback is HTML. if (url.pathname === "/api/product-profiles" || url.pathname === "/api/admin/product-profiles") { const ct = res.headers.get("content-type") || ""; if (!res.ok || !ct.includes("json")) { const config = defaultProductProfiles(); return Response.json(url.pathname === "/api/product-profiles" ? { ...config, profiles: config.profiles.filter((p) => p.enabled) } : config); } } if (SYNTH && url.pathname === "/api/nodes" && res.ok) { const snap = await res.json() as { nodes: Node[] }; snap.nodes = snap.nodes.map((n, i) => { const metrics = synthMetrics(n, i); return { ...n, metrics, health: synthHealth(n, metrics) }; }); return Response.json(snap); } if (SYNTH && /^\/api\/nodes\/[^/]+\/metrics$/.test(url.pathname)) { // Per-minute history for the trend lines: a plausible walk, not real data. const now = Date.now(); const rows = Array.from({ length: 360 }, (_, i) => { const t = now - (360 - i) * 60_000; const wobble = Math.sin(i / 24) * 8; return { nodeId: url.pathname.split("/")[3], at: Math.floor(t / 60_000) * 60_000, cpuAvg: 40 + wobble, cpuMax: 52 + wobble, memAvg: 60 + wobble / 2, memMax: 66 + wobble / 2, diskAvg: 70 + i / 60, diskMax: 71 + i / 60, loadMax: 55 + wobble, swapMaxBytes: 2 * 2 ** 30, samples: 4, }; }); return Response.json({ rows }); } return new Response(res.body, { status: res.status, headers: { "content-type": res.headers.get("content-type") || "application/json" }, }); }, }); console.log(`[preview] 只读代理 http://127.0.0.1:${server.port} → ${UPSTREAM}`); console.log(`[preview] 非 GET 请求一律 405,不会对生产产生任何写入`); console.log(`[preview] /api/me 伪装成管理员 ${PREVIEW_ADMIN}(否则内网 API 无身份,管理入口不显示)`); if (SYNTH) { console.log(`[preview] \x1b[33m--synth 已开启:节点指标与健康状态是伪造的\x1b[0m(集群标签、节点名、在线状态仍是真实的)`); } else { console.log(`[preview] 未开 --synth:节点健康面板会显示「未上报指标」(线上控制面还没这个功能,属正常)`); } console.log(`\n下一步,另开一个终端:\n API_TARGET=http://127.0.0.1:${server.port} bun run dev:web\n`);