proposal(CP2-F): cypher 二~四刀拆分提案——bundle 528KB 實測解剖+引擎留原地方案(待 leo confirm)

偵察實測(wrangler dry-run+sourcemap byte 歸因,非推測):
- 現碼 11,050 行/bundle 528.1KB——頂層 CP2-F 記載(15,446 行/748KB)是第一刀前舊數,提案 §0 更正
- zod 佔 130.6KB(24.7%),只服務兩條入口驗證鏈=最大單一減重點
- 引擎真身僅 ~180KB,其餘 ~350KB 是管理/前端 API 面

方案:引擎留 cypher-executor 原地(webhook 觸發 URL 外部焊死、零改動),
新開 arcrun-api 收管理面;三刀漸進、每刀獨立回退;13 顆 SVC binding 不動。
依 D35 只寫 proposal 停下,不動 code、不部署。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
uncle6me-web
2026-07-24 15:37:51 +08:00
parent ad367e450d
commit ab8e07201f
+121 -1
View File
@@ -8,7 +8,127 @@
## 待裁決
### P-2026-07-21:registry 寫入端+搜尋端修復(leo 親自交辦,最高優先
### P-2026-07-24:CP2-F 第二~四刀——執行引擎獨立成精簡 worker(cypher 瘦身收官
> 提案人:總管交辦之 arcrun 偵察 subagent(唯讀分析,未動 code、未部署)。
> 觸發:頂層 CRITICAL-PATH.md CP2-Fw=9P3)——「免費起步」承諾不成立;
> leo 定調:workflow 執行引擎獨立成精簡 worker(只做調度),console/portal/credentials/webhooks 拆出去。
> **依 D35:現行 active SDDportal-auth,與本題不同卷,故只寫 proposal 停下等 confirm,不動 code。**
#### 0. 先更正頂層 CP2-F 的過時記載(本次實測核實)
| 項目 | CP2-F 記載 | 2026-07-24 實測 | 出入原因 |
|---|---|---|---|
| 原始碼行數 | 15,446 行 | **11,050 行**`find src -name '*.ts' \| xargs wc -l` | 第一刀已砍 |
| bundle | 748KB | **528.1KB**540,742 bytes`wrangler deploy --dry-run --outdir` 實跑) | 同上 |
| 「ingest 必爆」 | 隱含現在式 | **已穩定**(第一刀驗收:連灌 10 張全過、同卡 6 連跑全過) | 5a16484 已修 |
| /health 5-7ms、9ms/節點 | 現在式 | **拆分前舊數,第一刀後未重測**——本提案每刀驗法含重測 | 未更新 |
第一刀=commit `5a16484`07-21):console.ts(1623行)portal-ui.ts(1390行) 搬去 `console-ui/` CF Pages
arcrun-console-ui.pages.dev),748→528KB-29%)。07-22 `ad367e4` 補 console-ui build 修復+
`deploy.targets.json` 具名部署目標(personal/enterprise 兩 profile,帳號+apiBase+專案名收一檔)。
**CP2-F 條目該由總管在頂層更新為「第一刀 ✅、二~四刀待裁」**,狀態從 ❌ 斷改 ◐ 半通。
#### 1. Bundle 實測解剖(528.1KBsourcemap byte 級歸因,非推測)
方法:`npx wrangler deploy --dry-run --outdir=<scratch>` 產 index.jsindex.js.map →
自寫 VLQ 解碼器把每個 byte 歸因到原始檔(mapped 507.5KBbundler glue 20.5KB)。
| 模組 | KB | 佔比 | 主要檔案(行數) | 去向提議 |
|---|---|---|---|---|
| **dep: zod** | 130.6 | 24.7% | 只被 `lib/schemas.ts``lib/prompt-recipe-schema.ts` import | 刀④逐出引擎 |
| **dep: hono** | 76.9 | 14.6% | 框架 | 兩邊都要,留 |
| **執行引擎核心** | 74.3 | 14.1% | graph-executor(702)、component-loader(370)、wasi-shim(674)、execute/executions/resume/cypher routes | **留(引擎本體)** |
| Credentials/Auth | 48.0 | 9.1% | auth.ts(467)、credentials.ts(322)、auth-dispatcher(301)、auth-recipe-seeds(748行/21.9KB) | CRUDseeds 搬;dispatcher 讀路徑留 |
| PortalRAG 多人) | 42.3 | 8.0% | portal.ts(783)、portal-data.ts(457)、portal-auth、portal-seeds | 刀②搬 |
| Console API | 31.8 | 6.0% | console-dashboard(526)、console-auth(165)、兩 model(616) | 刀②搬 |
| dep: unenv/polyfill | 29.1 | 5.5% | nodejs_compat 代價 | 留(兩邊皆有) |
| Webhooks CRUD | 25.6 | 4.8% | webhooks-named(521)含觸發+管理混一檔 | **拆檔**trigger/query 留、CRUD 搬 |
| Recipes CRUD/seeds | 24.1 | 4.6% | recipes.ts(563)、api-recipe-seeds、init-seed | CRUD/seeds 搬;`resolveRecipe` 讀路徑留 |
| Docs/OpenAPI | 12.5 | 2.4% | openapi.ts(306) | 刀③搬 |
| KBDB proxy | 8.4 | 1.6% | kbdb-proxy.ts(272) | 刀②搬 |
| scheduled/cron | 3.7 | 0.7% | scheduled.ts、cron-* | 留(觸發面) |
**三個關鍵事實**
- zod 一家=24.7%,卻只服務「入口驗證」兩條鏈(execute→schemas、recipe-loader→prompt-recipe-schema)。
- 引擎真身(核心+hono+unenv)只要 ~180KB;其餘 ~350KB 全是管理/前端 API 面。
- 耦合點:`graph-executor.ts:6` import `resolveRecipe` from `routes/recipes.ts:406`——
**讀路徑長在 CRUD 路由檔裡**,拆分前要先抽到 lib(刀②-0 前置小步)。
#### 2. 拆分方案(Option A:引擎留原地,管理面搬出——推薦)
**為什麼引擎留在 cypher-executor 原 worker 不搬家**webhook 觸發 URL
`/webhooks/:token/trigger``/webhooks/named/:ns/:name/trigger``/q/:ns/:name`
被外部 callerTelegram webhook、Routine、CLI、MCP)焊死;引擎留原地=**外部零改動**。
反向(引擎搬新 worker)要全網改 URL,風險大得多,不採。
**佈局**:既有 25 顆(22 零件+cypherkbdbmcp)→ **26 顆**:新增 1 顆 `arcrun-api`(管理/控制面 TS worker)。
arcrun-api 是框架自身的 API 面、非業務零件,與零件須 WASM 的鐵律不衝突——cypher 本身同理。)
| 端點群 | 去向 |
|---|---|
| `/execute``/cypher/*``/validate`(刀④前)、`/resume``/executions/*``/workflows/:name/executions`、webhook **trigger/query**`/health`、scheduled cron | **cypher-executor(引擎)留** |
| `/portal/*``/portal/data/*``/console/*``/kbdb/*`proxy)、`/credentials/*``/auth/*` CRUD、`/recipes/*` CRUD、`/webhooks` CRUDnamed 管理(backfill/migrate/delete/list)、`/init-seed``/docs`OpenAPI | **arcrun-api(新)搬** |
**Service binding 過渡(13 顆 SVC_\* 懶載現況)**:零風險——13 個 binding 服務的是
`component-loader.ts:73-85` LOGIC_BINDING_MAP 的邏輯零件調度,**全屬引擎**;引擎不搬家=
wrangler.toml `[[services]]` 原封不動。arcrun-api 完全不需要 SVC binding。
未配置時 fallback 公網 workers.dev 的既有機制(component-loader.ts:249-272)也不動。
self-hosted 的 deploy.ts 注入機制(D1 database_id 等)需對 arcrun-api 的 toml 複用同款注入——既有機制,非新開發。
**與 07-22 第一刀銜接**console-ui Pages 的 `ARCRUN_API_BASE`build 期參數)刀②時改指
arcrun-api`deploy.targets.json` 兩 profile 各加一欄,部署命令不變。
**刀法(分三刀,每刀獨立可回退)**
- **刀②(先抽 resolveRecipe 到 lib**PortalConsole APIkbdb-proxy 搬 arcrun-api。
預估 -82.5KB → 引擎 ~446KB。console-ui apiBase 同步切。
- **刀③**credentials/auth CRUDwebhooks 管理面(先把 webhooks-named.ts 拆成 trigger/管理兩檔)+
recipes CRUDinit-seed(含兩包 seeds 30KB)+docs/openapi 搬過去。預估 -90KB → 引擎 ~355KB。
- **刀④(zod 逐出引擎)**`/validate` 搬 arcrun-apizod 隨行);`/execute` 入口驗證改手寫窄驗證
graph shape 檢查 ~50 行)或換 valibot~10KB);prompt-recipe-schema 同款處理。
預估 -130KB → **引擎 ~200-225KB(對 528KB 減 ~60%**
**目標與誠實邊界**/health <2ms 主要靠 bundle 縮(冷啟 parse+常駐面縮)可期;
但**單節點 10ms 內不保證光靠瘦身達標**——9ms/節點若主要來自 per-node KV put
graph-executor.ts:352 每節點 `kvSetNodeOutput`)+JSON 序列化,則需**刀④.5(備案)**:
node output 改「僅斷點/暫停時寫 KV、其餘記憶體傳遞」。是否需要動,以刀②後的 cpuTime 實測決定,不預先動。
#### 3. 每刀驗法(免費層 cpuTime 實測法)
每刀收工四件、缺一不算完成(守 CP 使用規則 6/7):
1. `wrangler deploy --dry-run` bundle size 對照表(貼實跑輸出,驗預估)。
2. vitest 全綠+tsc 0(現基線:cypher 169/170,唯一失敗=executor「不存在的零件」pre-existing)。
3. **免費層 cpuTime 實測**:部署後 `npx wrangler tail arcrun-cypher-executor --format=json`
`cpuTime`,三個探針各打 10 次取 P50/health、單節點 workflowhttp_request×1)、
km_wiki_ingest6 節點)。記進 CP2-F 條目當證據。
4. 端到端頭尾驗:console-ui 兩 profile 登入+搜尋(打 arcrun-api)、portal 登入、
`acr push``acr run` 走引擎、**webhook trigger 原 URL 打通**(外部 caller 不改的承諾)。
**回退**:每刀=一個 PRarcrun-api 是加法(新 worker),引擎側只刪路由掛載——
回退=revert PR+重部引擎(arcrun-api 留著不礙事)。KV/D1 零 schema 變更,無資料遷移,無不可逆步驟。
#### 4. 影響分析(D35 第 3 條)
- **portal-auth(現行 active**P1-P4 已完成待部署排練。刀②把 `/portal/*` 路由搬 arcrun-api
**只搬家不改行為**,其測試(portal 三套 56/56)隨檔案搬移後應原樣全綠;
但其「部署清單」要加一行(portal 部署目標從 cypher 變 arcrun-api)。**不作廢任何任務**。
- **第一刀遺留(5a16484「未做(第二刀)」清單)**console-dashboard/portal/portal-data=本提案刀②,正式立案銷帳。
- **library-mapdraft**M5 console 首頁區塊打 `/kbdb/map`——kbdb-proxy 刀②搬家後 console-ui apiBase 同步切,不改功能。
- **P-2026-07-21 registry proposal 的 B1**(「在 cypher-executor 開 GET /search」):若兩案都 confirm
`/search` 統一面該開在 **arcrun-api** 而非引擎(搜尋是管理面不是執行面)——兩案不衝突,落點修一字。
- **不受影響**22 顆零件、kbdb worker、mcp、CLI`acr` 打的執行端點全留原 URL)、
webhook 外部 caller、13 個 service binding、artifact-sharingconfirmed)各 Phase。
- **新增維護面(誠實記)**:多一顆 worker 要部署(官方+self-hosted 各一次);
deploy 腳本/文件要加 arcrun-api;兩 worker 共用 KV/D1 binding(同一批 id,讀寫面分離)。
#### 5. 待 leo 拍板點
1. **開工排序**CP 排 P3P1=CP3-A、P1↑=A4 OAuth 在前)。本提案只求 confirm 方案,開工時點照 CP 排序走——除非 leo 要提前。
2. **刀④驗證庫選擇**:手寫窄驗證(零依賴、-130KB 全拿)vs valibot(保留 schema 風格、-120KB)——品味題。
3. **arcrun-api 命名**`arcrun-api`?或併入既有規劃中的其他 worker?(跨 repo 佈局=總管/leo 層決策)
4. **頂層 CP2-F 條目更正**(§0 表)由總管執行——本 repo 只能在此記載,不碰頂層文件。
> 提案人:總管交辦之 arcrun subagent。
> 觸發:leo 2026-07-21 —「一旦推進一個零件,就自動進 registryrecipe、workflow、app 都應該可以 registry