feat(cli): code 零件接進 acr init/update 部署流程(自足 Worker 進部署清單 + vendored wasm 進 repo)

動機(Arcrun#4 後續,leo 批准):code 零件只有原始碼(registry/components/code),
downloadAndDeploy 完全沒涵蓋它——tier1 只掃 .component-builds/*(TinyGo 家族,要求
component.wasm),tier2 寫死四個引擎。merge 後用戶跑 acr update 應真的裝上 code。

接法(實查後裁定):
- code 是自足 Worker(quickjs-emscripten wasmfile variant,非 TinyGo;見其 index.ts 頭註),
  「缺 wasm」的真相是「這一類根本不在部署清單」+「vendored quickjs.wasm 是 gitignored
  build 產物、不進 archive」。
- deploy.ts 新增 SELF_CONTAINED_COMPONENT_WORKERS(目錄 + 必要產物 gate,比照 tier1
  component.wasm gate 的誠實跳過精神),discoverWorkerDirs 將其排進 tier1(零件先於引擎)。
- vendored quickjs.wasm(491KB)commit 進 repo:.gitignore 放行(完全比照
  !.component-builds/**/component.wasm 的「部署物 wasm 例外」先例)→ acr update 從
  Gitea archive 直接拿到,更新不需 npm build 工具鏈。
- 共享依賴抽成 SHARED_DEPLOY_DEPS 並補 quickjs-emscripten-core + wasmfile variant
  (版本對齊零件 package.json,測試看守 drift)→ root 裝一次、esbuild 往上 resolve。
- 注入零改動:既有 stripOfficialOnlyBindings 剝掉 code.arcrun.dev 官方 route、
  workers_dev=true 保留 → self-hosted 自動落 arcrun-code.<sub>.workers.dev。
- parts.ts BUILTIN_COMPONENTS 加 code 條目(issue #13 W3:零件=靜態清單)→ acr parts 可見。
  實查:init 本來就不對 registry 註冊任何零件(registry index 是官方 backfill 腳本的事),
  故「比照其他零件」=進 BUILTIN_COMPONENTS 即對齊。

測試:cli/tests/deploy-code-component.test.ts 9 顆全綠(node --test,零新依賴):
部署清單含 code / 缺產物誠實跳過 / wasm 已 git 追蹤(會進 archive)/ SHARED_DEPLOY_DEPS
涵蓋零件全部 runtime deps + 版本一致 / route 剝除與 [vars] 保留 / parts 清單含 code。
tsc --noEmit 綠;零件自身 12 顆 vitest 綠(沙箱行為未動)。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015d5jDbuqT5Htwv3Q88XXKk
This commit is contained in:
Leo
2026-07-07 08:37:41 +00:00
parent 020ccdf085
commit 621cb8d948
5 changed files with 200 additions and 10 deletions
+17
View File
@@ -109,6 +109,23 @@ export const BUILTIN_COMPONENTS: ComponentDef[] = [
component: wait
ms: 1000`,
},
{
// code 是自足 Workerquickjs 沙箱,registry/components/code),非 TinyGo-wasm 家族,
// 但同屬靜態零件(走 PR + 人類閘門 merge 新增)→ 進本清單正確(issue #13 W3 原則)。
// 部署:acr init/update 的 downloadAndDeploy 自動部署(deploy.ts SELF_CONTAINED_COMPONENT_WORKERS)。
canonical_id: 'code',
display_name: '程式碼(沙箱 inline JS',
category: 'logic',
description: 'QuickJS-wasm 沙箱執行 inline JS:讀 input、return JSON-able 值;碰不到網路/檔案/env',
config_example:
` my_code:
component: code
code: |
const doubled = input.items.map(x => x * 2);
return { doubled, count: doubled.length };
input:
items: [1, 2, 3]`,
},
// ── 資料類(Data) ─────────────────────────────────────────────────────────
{
canonical_id: 'set',
+41 -10
View File
@@ -136,6 +136,24 @@ export const SECRET_TARGET_WORKERS = [
'arcrun-auth-service-account',
] as const;
/** 共享部署依賴(downloadAndDeploy 2.5tarball root 裝一次,各 worker 往上 resolve)。
* 含全部 worker 的 runtime depstier1 component 只要 honotier2 cypher/registry/mcp/kbdb
* 另需 zod / @hono/zod-openapi / @modelcontextprotocol/sdk / js-yaml / yaml
* code 自足 Workerregistry/components/code)另需 quickjs-emscripten-core + wasmfile variant
* (版本對齊該零件 package.jsondrift 由 cli/tests/deploy-code-component.test.ts 看守)。
* 漏一個會讓該 worker deploy 失敗,故寧可多列。export 供離線測試驗清單完整。*/
export const SHARED_DEPLOY_DEPS: Record<string, string> = {
hono: '^4.7.0',
wrangler: '^4.0.0',
zod: '^3.23.0',
'@hono/zod-openapi': '^0.18.0',
'@modelcontextprotocol/sdk': '^1.0.0',
'js-yaml': '^4.1.0',
yaml: '^2.4.0',
'quickjs-emscripten-core': '^0.31.0',
'@jitl/quickjs-wasmfile-release-sync': '^0.32.0',
};
export interface DeployContext {
accountId: string;
apiToken: string;
@@ -215,17 +233,11 @@ export async function downloadAndDeploy(
let sharedBin = '';
try {
process.stdout.write(chalk.gray(' → 安裝共享部署依賴(一次,取代每個 worker 各裝)...'));
// 含全部 worker 的 runtime depstier1 component 只要 honotier2 cypher/registry/mcp/kbdb
// 另需 zod / @hono/zod-openapi / @modelcontextprotocol/sdk / js-yaml / yaml)→ 全裝 root
// 各 worker 往上 resolveesbuild bundle 找得到。漏一個會讓該 worker deploy 失敗,故寧可多列。
// 依賴清單抽出成 SHARED_DEPLOY_DEPSexport 供離線測試看守,見常數 doc)。
writeFileSync(
join(root, 'package.json'),
JSON.stringify({ name: 'arcrun-deploy-shared', private: true, type: 'module',
dependencies: {
hono: '^4.7.0', wrangler: '^4.0.0', zod: '^3.23.0',
'@hono/zod-openapi': '^0.18.0', '@modelcontextprotocol/sdk': '^1.0.0',
'js-yaml': '^4.1.0', yaml: '^2.4.0',
} }),
dependencies: SHARED_DEPLOY_DEPS }),
);
execFileSync('npm', ['install', '--no-audit', '--no-fund'],
{ cwd: root, stdio: ['ignore', 'ignore', 'pipe'] });
@@ -479,8 +491,17 @@ async function downloadRepoTarball(ref: string): Promise<string> {
return join(dir, top);
}
/** 掃解壓出的部署物,回傳 tier1.component-builds/*)與 tier2cypher-executor/registry)目錄清單。*/
function discoverWorkerDirs(root: string): { tier1: string[]; tier2: string[] } {
/** 自足 Worker 零件(非 TinyGo-wasm 家族):目錄相對 root + 部署 gate 必要產物(相對該目錄)。
* 目前只有 codequickjs 沙箱,registry/components/code)。gate 精神比照 tier1 的 component.wasm
* 必要產物(vendored quickjs.wasm,需 commit 進 repo)缺 → 誠實跳過,不讓 wrangler deploy 因缺檔失敗。
* export 供離線測試驗「部署清單含 code + 產物 gate 正確」。*/
export const SELF_CONTAINED_COMPONENT_WORKERS: ReadonlyArray<{ dir: string[]; requires: string[][] }> = [
{ dir: ['registry', 'components', 'code'], requires: [['vendor', 'quickjs.wasm']] },
];
/** 掃解壓出的部署物,回傳 tier1.component-builds/* + 自足 Worker 零件)與
* tier2cypher-executor/registry/kbdb/mcp 引擎)目錄清單。export 供離線測試。*/
export function discoverWorkerDirs(root: string): { tier1: string[]; tier2: string[] } {
const tier1: string[] = [];
const tier2: string[] = [];
@@ -497,6 +518,16 @@ function discoverWorkerDirs(root: string): { tier1: string[]; tier2: string[] }
}
}
}
// 自足 Worker 零件(如 code):與 TinyGo 家族不同(自帶 index.ts + 相依 npm 套件 +
// vendored quickjs.wasm),但同屬 tier1「零件」語義 → 一起先於引擎部署。
// depsquickjs-emscripten-core / wasmfile variant)由 root 共享安裝提供(SHARED_DEPLOY_DEPS),
// wrangler 對相對路徑 .wasm import 自動綁 CompiledWasm(見該零件 index.ts 頭註)。
for (const { dir: rel, requires } of SELF_CONTAINED_COMPONENT_WORKERS) {
const dir = join(root, ...rel);
const complete = existsSync(join(dir, 'wrangler.toml'))
&& requires.every(r => existsSync(join(dir, ...r)));
if (complete) tier1.push(dir);
}
// self-hosted 也部署自己的 MCP workermcp-account-source §5carchive 主庫即得 MCP
// .mcp.json 指自己的 mcp 而非官方 mcp.arcrun.dev)。
// kbdbMCP 的 partnerAuthMiddleware 透過 KBDB service binding 打 arcrun-kbdb workermcp/wrangler.toml)。