feat(build): 一個實例會用到的每一顆 worker 都在 Arcrun 編(D91/Arcrun#125)

官方編譯點從 5 顆擴到 23 顆,並把 portal 前端的產生器從 arcrun-rag 搬回來。

為什麼不是「順手多編幾顆」:
- 只編 5 顆 ⇒ 下游 bundle 裡就只有 5 顆 ⇒「零件用到才下載」沒有貨可以下載。
  而 auth_static_key 是每一支產品工作流解 {{credential.X}} 都會打的那一顆
  ⇒ 走網頁安裝器裝出來的實例,工作流一跑就 500(Arcrun#124 的 error code: 1042)。
- portal 前端(arcrun-rag-ui)當時仍由 arcrun-rag 自己拼裝原始碼
  ⇒ 使用者拿到的那顆 worker,沒有任何 Arcrun commit 說得出它的來源。
  leo:「今天開始出貨一律不准在 arcrun rag 或任何別的地方 build,這就是 arcrun 的專屬工作。」

做法:
- 零件 worker 改用**掃描** .component-builds/(不是手寫清單——手寫清單就是
  arcrun-rag#27/D48 那個病的形狀),worker 名一律讀該零件自己的 wrangler.toml。
- 可出貨的判準=**部署位元組在版控裡**。.gitignore 明文排除的三顆
  (claude_api/km_writer/kbdb_upsert_block,DECISIONS §1「錯做成零件」)
  因此自動落在公庫外,而且是**寫進 manifest.excluded 的**,不是安靜跳過。
- 輸出目錄改成每次只留這一輪該有的,清掉舊成品(拔掉棘輪)。
- UI 產生器原樣搬過來,兩道閘(世代閘、前端 JS 語法閘)一起搬——
  它們驗的是本 repo 的 console-ui/public,本來就該長在來源這一側。

實測(本機 worktree,依 lockfile 重裝依賴後):
- 23/23 編成功;四顆既有引擎成品**位元組完全重現** committed 值
  (cypher 8e6487478bc8/kbdb f15e16cca6ce/code 751634a3fc9a/mcp 9bc8dffb34d0/
   http-request cdd97364f277)⇒ 這次擴充沒有動到既有零件。
- arcrun-rag-ui 的產出與搬家前逐位元比對,**只差第一行註解**
  (那行必須改:它原本指向一支不再產生它的腳本)。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
uncle6me-web
2026-08-15 00:17:03 +08:00
parent 5dd1d1e7a9
commit 45169ab25e
2 changed files with 357 additions and 10 deletions
+149 -10
View File
@@ -1,6 +1,8 @@
#!/usr/bin/env node
/**
* build-worker-artifacts.mjs — Arcrun#80tier2 workerTS→ 可部署 JS)的**唯一官方編譯點**。
* build-worker-artifacts.mjs — **一個 Arcrun 實例會用到的每一顆 worker,都在這裡編**。
* 這是 D49/D91 的那個「唯一官方編譯點」:成品放 `.worker-builds/`、commit 進 repo
* 任何人(安裝器、出貨線、self-host 使用者)要用零件都來這裡拿,不准自己編一份。
*
* 背景(Arcrun#80arcrun-rag#39):這個 repo 過去只把 tier1 零件(TinyGo→wasm)的成品
* commit 進 `.component-builds/{name}/component.wasm`tier2cypher-executor / kbdb /
@@ -9,6 +11,14 @@
* 結果同一份原始碼在不同機器編出不同位元組(見下「已知踩坑」),
* 而且「這顆成品是哪個 commit 編的」只有整包一個 `source` 欄位,答不出單顆的來源。
*
* 🔴 2026-08-15D91Arcrun#125)本檔的涵蓋範圍從 5 顆擴到 23 顆,理由不是「順手多編幾顆」:
* · 只編 5 顆 ⇒ 下游 bundle 裡就只有 5 顆 ⇒ **「零件用到才下載」沒有貨可以下載**。
* 而 `auth_static_key` 是每一支產品工作流解 `{{credential.X}}` 都會打的那一顆
* ⇒ 走網頁安裝器裝出來的實例,工作流一跑就 500(Arcrun#124)。
* · portal 前端(`arcrun-rag-ui`)當時仍由 arcrun-rag 自己拼裝
* ⇒ 使用者拿到的那顆 worker,沒有任何 Arcrun commit 說得出它的來源。
* ⇒ 現在三種東西走同一條路、同一份 manifest:引擎 worker/零件 worker/內嵌 UI worker。
*
* 本腳本要解的:
* 1. 編譯只發生在這裡(Arcrun 本體),成品放固定位置 `.worker-builds/`commit 進 repo——
* 與 `.component-builds/*.wasm` 同一個既有慣例(self-host 用戶從 repo 直接拿部署來源)。
@@ -37,13 +47,15 @@
* 輸出:.worker-builds/<name>/worker.mjs (+ *.wasm)、.worker-builds/manifest.json
*/
import esbuild from 'esbuild';
import { readFileSync, writeFileSync, mkdirSync, copyFileSync, existsSync, rmSync, readdirSync } from 'node:fs';
import { readFileSync, writeFileSync, mkdirSync, copyFileSync, existsSync, rmSync, readdirSync, statSync } from 'node:fs';
import { join, resolve, basename, relative } from 'node:path';
import { fileURLToPath } from 'node:url';
import { execSync } from 'node:child_process';
import { createHash } from 'node:crypto';
// Arcrun#108 出貨閘(見 main() 內註解)。規則本體與掃描器住在 cypher-executor/scripts/。
import { scanProject as scanTenantSources } from '../cypher-executor/scripts/check-tenant-source.mjs';
// UI worker 的內嵌器(2026-08-15 從 arcrun-rag 搬回來,D91)。
import { buildUiWorker } from './build-ui-worker.mjs';
// REPO 一律用「本檔自己的位置」推導,不吃 cwd/env——這是踩坑①解法的地基:
// 不管這個 clone 被放在磁碟哪個絕對路徑,REPO 永遠是「這個 repo 的根目錄」,
@@ -52,17 +64,96 @@ const REPO = resolve(fileURLToPath(new URL('.', import.meta.url)), '..');
const OUT = join(REPO, '.worker-builds');
const CHECK_ONLY = process.argv.includes('--check-only');
/** 五顆 tier2 worker——與 arcrun-rag `installer/scripts/bundle-components.mjs`
* CORE_COMPONENTS 的 build 參數對齊(那邊的 esbuild 呼叫即將被本腳本的成品取代)。
/** 引擎與工具 worker(各自的 dir/entry 不成規律,所以逐顆寫)。
* name 用 arcrun-rag 那邊的慣例(`arcrun-<kebab>`),方便安裝器直接對號。 */
const WORKERS = [
const ENGINE_WORKERS = [
{ name: 'arcrun-cypher-executor', dir: 'cypher-executor', entry: 'src/index.ts', stripServices: true },
{ name: 'arcrun-kbdb', dir: 'kbdb', entry: 'src/index.ts' },
{ name: 'arcrun-http-request', dir: '.component-builds/http_request', entry: 'src/index.ts' },
{ name: 'arcrun-code', dir: 'registry/components/code', entry: 'index.ts' },
{ name: 'arcrun-mcp', dir: 'mcp', entry: 'src/index.ts' },
];
/** 零件 worker 的家。底下每個子目錄=一顆零件(TinyGo 編好的 `component.wasm`
* +一層 TS 外殼),格式一致,所以用**掃描**而不是手寫清單。 */
const COMPONENT_BUILDS = '.component-builds';
/**
* 掃出所有零件 worker。
*
* 🔴 2026-08-15Arcrun#125D91):這裡以前只手寫了 `http_request` **一顆**
* 其餘 18 顆零件從來沒有官方成品。後果不是「少編了幾顆」,是
* **「用到才下載」沒有貨可以下載**——arcrun-rag 的 bundle 只裝得到本檔編出來的東西,
* 本檔沒編,公庫裡就永遠沒有它。而 `auth_static_key` 正是每一支產品工作流
* 解 `{{credential.X}}` 都會打的那一顆 ⇒ 凡走網頁安裝器裝出來的實例,
* 工作流一跑就 500Arcrun#124 的 `error code: 1042`)。
*
* 🔴 **不准改回手寫清單**:手寫清單就是 arcrun-rag#27D48 那個病的形狀
* ——「一份人維護的清單」與「磁碟上真的有什麼」必然漂移,而漂移的方向只有一個:
* 新增的零件沒人記得加進去,於是它對使用者而言不存在。
* worker 名一律讀該零件自己的 `wrangler.toml`(那是它部署時真正會叫的名字),
* 不由本檔從目錄名推導——推導=再造一份會漂的真相。
*/
function discoverComponentWorkers() {
const base = join(REPO, COMPONENT_BUILDS);
if (!existsSync(base)) return { workers: [], excluded: [] };
const workers = [];
const excluded = [];
for (const id of readdirSync(base).sort()) {
const dir = join(COMPONENT_BUILDS, id);
const abs = join(REPO, dir);
if (!statSync(abs).isDirectory()) continue;
const entry = 'src/index.ts';
if (!existsSync(join(abs, entry))) continue; // 不是一顆 worker(例如暫存目錄)
const name = readWorkerName(join(abs, 'wrangler.toml'));
if (!name) throw new Error(`${dir}/wrangler.toml 沒有 name=這顆零件部署後會叫什麼名字沒人說得出來`);
// 🔴 可出貨的判準=**它的部署位元組在版控裡**,不是「這台機器的磁碟上有」。
// `.gitignore` 明文排除三顆(claude_apikm_writerkbdb_upsert_block)——理由不是
// 「還沒編」,是 DECISIONS §1「它們被錯做成零件,該降級成工作流/recipe」。
// 本機磁碟上仍留著上次編的 wasm,所以「有沒有檔案」分辨不出這件事,「有沒有進版控」可以。
// ⇒ 用這個判準,未來任何一顆被判定不該出貨的零件,**不必回來改本檔**就會自動落在公庫外。
if (!isTracked(join(dir, 'component.wasm'))) {
excluded.push({ name, dir, reason: 'component.wasm 不在版控(.gitignore 明文排除=這顆不是可出貨的零件)' });
continue;
}
workers.push({ name, dir, entry, component: id });
}
return { workers, excluded };
}
/** 這個路徑有沒有被 git 追蹤(=任何 clone 都拿得到)。 */
function isTracked(relPath) {
try {
const out = execSync(`git ls-files -- ${JSON.stringify(relPath)}`, { cwd: REPO, encoding: 'utf8' }).trim();
return !!out;
} catch {
return false;
}
}
/** 從 wrangler.toml 讀 `name = "..."`worker 部署後的真名)。 */
function readWorkerName(tomlPath) {
if (!existsSync(tomlPath)) return null;
const m = readFileSync(tomlPath, 'utf8').match(/^\s*name\s*=\s*["']([^"']+)["']/m);
return m ? m[1] : null;
}
/** 使用者實例的 GUI:沒有原始碼要打包,是把 `console-ui/public` 內嵌成單檔 worker。
* 2026-08-15 從 arcrun-rag 搬回來(D91)——見 scripts/build-ui-worker.mjs 檔頭。 */
const UI_WORKER = {
name: 'arcrun-rag-ui',
dir: 'console-ui/public',
builder: 'ui',
// 這顆沒有 wrangler.toml(它不由 wrangler 部署,是安裝器用 CF API 上傳的),
// 所以 compat 值寫在這裡。**沿用搬家前 arcrun-rag manifest 條目的原值**
// 讓這次搬家只改「誰產生它」,不改「它是什麼」。
compat_date: '2026-07-01',
compat_flags: [],
};
const { workers: COMPONENT_WORKERS, excluded: EXCLUDED_COMPONENTS } = discoverComponentWorkers();
const WORKERS = [...ENGINE_WORKERS, ...COMPONENT_WORKERS, UI_WORKER];
function sha256(buf) {
return createHash('sha256').update(buf).digest('hex');
}
@@ -143,7 +234,38 @@ function repoHead() {
}
}
/** UI worker:沒有原始碼要打包,是把 `console-ui/public` 內嵌成一顆單檔 worker。
* 產出格式與 esbuild 那條一致(worker.mjs manifest 條目),下游因此不必分辨它是哪種。 */
async function buildUiOne(w) {
const outDir = join(OUT, w.name);
mkdirSync(outDir, { recursive: true });
const { source, fingerprint, fileCount } = await buildUiWorker({ uiDir: join(REPO, w.dir) });
const outFile = join(outDir, 'worker.mjs');
writeFileSync(outFile, source);
const jsBuf = readFileSync(outFile);
return {
name: w.name,
source_dir: w.dir,
source_commit: sourceCommitFor(w.dir),
main_module: 'worker.mjs',
main_file: `${w.name}/worker.mjs`,
js_bytes: jsBuf.length,
content_sha256: sha256(jsBuf),
modules: [],
compat_date: w.compat_date,
compat_flags: w.compat_flags,
// 內嵌 UI 不需要任何 binding;它只吃兩個由安裝器注入的 var
// WORKER_SUBDOMAIN 算 apiBase、ARCRUN_BUNDLE_VERSION 回報版本)。
requires: { kv: [], d1: [], vectorize: 0, ai: false, vars: {} },
// UI 自己的內容指紋——安裝器用它判斷「這台實例的前端要不要重推」。
ui_fingerprint: fingerprint,
ui_file_count: fileCount,
warnings: [],
};
}
async function buildOne(w) {
if (w.builder === 'ui') return buildUiOne(w);
const dirAbs = join(REPO, w.dir);
const entryAbs = join(dirAbs, w.entry);
if (!existsSync(entryAbs)) throw new Error(`entry 不存在: ${entryAbs}`);
@@ -205,7 +327,8 @@ async function buildOne(w) {
async function main() {
console.log(`REPO = ${REPO}`);
const precheck = WORKERS.map((w) => ({ w, chk: checkNodeModules(w.dir) }));
// UI worker 沒有依賴要鎖(它內嵌的是靜態檔,不 import 任何套件)⇒ 不進這道閘。
const precheck = WORKERS.filter((w) => w.builder !== 'ui').map((w) => ({ w, chk: checkNodeModules(w.dir) }));
const failed = precheck.filter((p) => !p.chk.ok);
if (failed.length) {
console.error('\n❌ 建置中止:以下 worker 尚未按 lockfile 裝好依賴(踩坑②的閘):\n');
@@ -213,6 +336,10 @@ async function main() {
console.error('\n這是刻意設計:本腳本不自己跑 install,避免「install 方式不同 → 依賴版本不同 → 位元組不同」。');
process.exit(1);
}
if (EXCLUDED_COMPONENTS.length) {
console.log(`\n️ 刻意不進公庫的零件(${EXCLUDED_COMPONENTS.length}):`);
for (const e of EXCLUDED_COMPONENTS) console.log(` - ${e.name}${e.reason}`);
}
console.log('✔ node_modules 檢查通過:');
for (const p of precheck) console.log(` ${p.w.dir} (${p.chk.via})`);
@@ -245,9 +372,17 @@ async function main() {
}
mkdirSync(OUT, { recursive: true });
for (const w of WORKERS) {
const d = join(OUT, w.name);
if (existsSync(d)) rmSync(d, { recursive: true, force: true });
// 🔴 輸出目錄要**只**留這次編出來的東西——包含「上次編了、這次不該再編」的那些。
// 不清掉就是棘輪(D48arcrun-rag#27 的真兇):東西一旦掉進成品目錄就永遠留著,
// 沒有任何路徑會把它拿掉,於是「公庫裡有什麼」跟「本檔說該有什麼」慢慢分家。
// 實撞(2026-08-15):claude_api 被判定不該出貨後,它上一輪的成品仍留在 .worker-builds/。
const want = new Set(WORKERS.map((w) => w.name));
for (const name of readdirSync(OUT)) {
if (name === 'manifest.json') continue;
const d = join(OUT, name);
if (!statSync(d).isDirectory()) continue;
if (!want.has(name)) console.log(` 🧹 清掉不該在公庫裡的舊成品:${name}`);
rmSync(d, { recursive: true, force: true });
}
const head = repoHead();
@@ -258,6 +393,10 @@ async function main() {
repo_head: head.sha,
repo_dirty: head.dirty,
workers: [],
// 被刻意排除在公庫之外的零件——**寫出來**,不要安靜跳過:
// 下游(arcrun-rag 的懶載清單)看得到「這顆不在公庫、以及為什麼」,
// 才不會像 2026-08-14 那樣把「沒有貨」誤讀成「機制壞了」。
excluded: EXCLUDED_COMPONENTS,
notes: [],
};