#!/usr/bin/env bun /** * 把一条已批准的 RSI 实验写进代码常量 —— 「批准上线」之后的落代码一步。 * * bun scripts/rsi-ship.ts <实验 id> 改本地文件,打印建议的提交信息 * bun scripts/rsi-ship.ts <实验 id> --dry-run 只算、只校验,不写文件 * * 做的事: * 1. 从控制面只读接口 /api/rsi-ship/experiments/ 取上线材料(上线范围、各端基线快照与候选 / 旋钮)。 * 2. 在**本地**代码上重算(本地可能比线上新):prompt 实验按锚点变基,单端上线把新增行包进平台段, * 并校验另一端展开后的正文逐字节不变(shared/src/rsi_ship.ts::planPromptShip);运行时实验改 * shared/src/executor_profile.ts 的 PRODUCTION_PROFILE。 * 3. 改源码后另起一个 bun 进程重新 import,核对常量的值就是算出来的那一份;不一致就还原文件。 * * 不提交、不推送、不部署:改完照常 review → 提交(提交信息带 RSI-Experiment 行)→ cpd / streamline-deploy。 * 部署后控制面判定任务按运行中的代码核对,把实验记为「已上线」。 * * 控制面地址:STREAMLINE_API,默认内网只读 API。 */ import { readFileSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { parseArgs } from "node:util"; import { PROMPT_NODES } from "../shared/src/prompt_registry"; import { DEFAULT_RUN_TEMPLATES, type RunPromptStage } from "../shared/src/run_prompt"; import { PRODUCTION_PROFILE, type ExecutorProfile } from "../shared/src/executor_profile"; import { isShipScope, planProfileShip, planPromptShip, promptShipped, replaceLiteralConst, profileShipped, type ShipMember, type ShipScope, } from "../shared/src/rsi_ship"; import { isPromptPlatform, type PromptPlatform } from "../shared/src/prompt_platform"; const ROOT = join(import.meta.dir, ".."); const API = (process.env.STREAMLINE_API ?? "https://streamline-internal-api.bytedance.net").replace(/\/+$/, ""); interface Bundle { id: string; title: string; stage: string; kind: "prompt" | "runtime"; shipScope: string; members: { id: string; platform: string; status: string; base?: string; candidate?: string; profile?: ExecutorProfile }[]; } const die = (msg: string): never => { console.error(`✗ ${msg}`); process.exit(1); }; const label = (p: string) => ({ ios: "iOS", android: "Android", both: "两端" } as Record)[p] ?? p; const { values, positionals } = parseArgs({ args: Bun.argv.slice(2), options: { "dry-run": { type: "boolean" } }, allowPositionals: true }); const id = positionals[0] || die("用法:bun scripts/rsi-ship.ts <实验 id> [--dry-run]"); const dry = !!values["dry-run"]; const res = await fetch(`${API}/api/rsi-ship/experiments/${encodeURIComponent(id)}`); const bundle = (await res.json().catch(() => ({}))) as Bundle & { error?: string }; if (!res.ok) die(`取上线材料失败(${res.status}):${bundle.error ?? "?"}`); const scope = bundle.shipScope; if (!isShipScope(scope)) die(`上线范围不对:${scope}`); console.log(`实验「${bundle.title}」 · ${bundle.stage} · ${bundle.kind === "runtime" ? "运行时旋钮" : "prompt"} · 上线范围:${label(scope)}`); /** 改一个源文件,另起进程 import 核对 —— 不一致就还原。 */ async function writeVerified(file: string, next: string, check: string) { const path = join(ROOT, file); const prev = readFileSync(path, "utf-8"); if (prev === next) return console.log(` ${file}:没有变化`); if (dry) return console.log(` ${file}:会改(--dry-run,未写)`); writeFileSync(path, next); const proc = Bun.spawnSync(["bun", "-e", check], { cwd: ROOT, stdout: "pipe", stderr: "pipe" }); if (proc.exitCode !== 0) { writeFileSync(path, prev); die(`改完 import 回来核对不一致,已还原 ${file}:${proc.stderr.toString().slice(0, 400)}`); } console.log(` ${file}:已改,import 核对一致`); } if (bundle.kind === "runtime") { const members = bundle.members.filter((m) => isPromptPlatform(m.platform)) .map((m) => ({ platform: m.platform as PromptPlatform, profile: m.profile ?? {} })); if (members.every((m) => profileShipped(PRODUCTION_PROFILE, m.platform, m.profile))) { console.log("本地代码的 PRODUCTION_PROFILE 已经带上了,不用改。"); process.exit(0); } const plan = planProfileShip(PRODUCTION_PROFILE, members, scope as ShipScope); if (!plan.ok) die(plan.error); const file = "shared/src/executor_profile.ts"; const src = readFileSync(join(ROOT, file), "utf-8"); const body = Object.entries(plan.profiles).filter(([, v]) => Object.keys(v).length) .sort(([a], [b]) => a.localeCompare(b)) .map(([p, v]) => ` ${p}: { ${Object.entries(v).map(([k, x]) => `${k}: ${JSON.stringify(x)}`).join(", ")} },`).join("\n"); const re = /(export const PRODUCTION_PROFILE: Readonly> = \{)[\s\S]*?(\n\};)/; if (!re.test(src)) die(`${file} 里找不到 PRODUCTION_PROFILE 的定义(格式被改过?)`); const next = src.replace(re, (_, a, b) => `${a}${body ? `\n${body}` : ""}${b}`); const want = JSON.stringify(plan.profiles); await writeVerified(file, next, `import { PRODUCTION_PROFILE as P } from "./${file}"; const norm = (o) => JSON.stringify(Object.fromEntries(Object.entries(o).filter(([, v]) => Object.keys(v).length).sort())); if (norm(P) !== norm(${want})) { console.error(JSON.stringify(P)); process.exit(1); }`); for (const m of members) console.log(` ${label(m.platform)}:${JSON.stringify(plan.profiles[m.platform])}`); } else { const stage = bundle.stage as RunPromptStage; const local = DEFAULT_RUN_TEMPLATES[stage] ?? die(`不认识的 stage ${stage}`); const node = PROMPT_NODES.find((n) => n.id === stage) ?? die(`提示词注册表里没有 ${stage}`); const members = bundle.members.filter((m) => isPromptPlatform(m.platform) && m.base && m.candidate) .map((m) => ({ platform: m.platform, base: m.base!, candidate: m.candidate! }) as ShipMember); if (members.length && members.every((m) => promptShipped(local, m))) { console.log("本地代码已经带上这条改动了,不用改。"); process.exit(0); } const plan = planPromptShip(local, members, scope as ShipScope); if (!plan.ok) die(`补丁不可用:${plan.error}`); for (const c of plan.checks) console.log(` ${c.ok ? "✓" : "✗"} ${label(c.platform)}:${c.note}`); for (const d of plan.drift) console.log(` · ${label(d.platform)}:实验之后模板改过(差 ${d.lines} 行),已按锚点变基`); const src = readFileSync(join(ROOT, node.source), "utf-8"); const r = replaceLiteralConst(src, node.symbol, plan.template); if ("error" in r) die(r.error); const tmp = join(ROOT, ".rsi-ship-expected.json"); if (!dry) writeFileSync(tmp, JSON.stringify(plan.template)); try { await writeVerified(node.source, r.source, `import { ${node.symbol} as T } from "./${node.source}"; const want = JSON.parse(require("node:fs").readFileSync(${JSON.stringify(tmp)}, "utf-8")); if (T !== want) process.exit(1);`); } finally { if (!dry) Bun.spawnSync(["rm", "-f", tmp]); } console.log(`\n新增(${plan.added.length} 行${scope === "both" ? ",两端共用" : `,包在 {{#${scope}}} 段里`}):`); for (const l of plan.added) console.log(` + ${l}`); } console.log(`\n建议的提交信息:\n\n[RSI] ${bundle.stage} 上线「${bundle.title}」(${label(scope)})\n\n复跑通过、管理员批准的 RSI 实验。\n\nRSI-Experiment: ${bundle.id}\n`); console.log("提交后照常部署(cpd / streamline-deploy);判定任务核对到运行中的代码带上了它,面板记为「已上线」。");