arcrun — AI workflow execution engine (clean history)

Self-hosted 開源:WASM 零件 + recipe + cypher-executor,跑在你自己的 Cloudflare。

此為重建的乾淨歷史起點(移除曾誤 commit 的 GCP SA 金鑰,舊歷史保留在
richblack/arcrun 與本地 backup 分支)。含:
- acr init --self-hosted installer(建 KV/R2 + codeload 拉預編譯 wasm + wrangler deploy + seed recipe)
- recipe push 把關(資料外流提醒 + 打通檢查)
- 19 個正當零件預編譯 wasm(claude_api/km_writer/kbdb_upsert_block 排除:違反 DECISIONS §1)
- CLI / cypher-executor / registry / 完整 SDD

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
uncle6me-web
2026-06-03 15:52:38 +08:00
commit 922a57fe34
485 changed files with 89356 additions and 0 deletions
+194
View File
@@ -0,0 +1,194 @@
// Cypher Executor 端到端測試
import { SELF } from 'cloudflare:test';
import { describe, it, expect } from 'vitest';
describe('GET /', () => {
it('回傳服務狀態', async () => {
const res = await SELF.fetch('http://localhost/');
const data = await res.json() as Record<string, unknown>;
expect(res.status).toBe(200);
expect(data.service).toBe('arcrun-cypher-executor');
});
});
describe('POST /validate', () => {
it('驗證合法的圖定義', async () => {
const res = await SELF.fetch('http://localhost/validate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
id: 'test-graph',
name: '測試圖',
nodes: [
{ id: 'n1', type: 'Input' },
{ id: 'n2', type: 'Output' },
],
edges: [
{ from: 'n1', to: 'n2', type: 'PIPE' },
],
}),
});
const data = await res.json() as { valid: boolean };
expect(res.status).toBe(200);
expect(data.valid).toBe(true);
});
it('偵測無效邊', async () => {
const res = await SELF.fetch('http://localhost/validate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
id: 'bad-graph',
name: '壞圖',
nodes: [{ id: 'n1', type: 'Input' }],
edges: [{ from: 'n1', to: 'n999', type: 'PIPE' }],
}),
});
expect(res.status).toBe(400);
});
});
describe('POST /execute', () => {
it('PIPE 鏈: Input → passthrough → Output', async () => {
const res = await SELF.fetch('http://localhost/execute', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
graph: {
id: 'g1',
name: 'PIPE 測試',
nodes: [
{ id: 'input', type: 'Input', data: { message: 'hello' } },
{ id: 'pass', type: 'Component', componentId: 'comp_passthrough' },
{ id: 'output', type: 'Output' },
],
edges: [
{ from: 'input', to: 'pass', type: 'PIPE' },
{ from: 'pass', to: 'output', type: 'PIPE' },
],
},
context: {},
}),
});
const data = await res.json() as { success: boolean; data: { message: string }; trace: unknown[] };
expect(res.status).toBe(200);
expect(data.success).toBe(true);
expect(data.data.message).toBe('hello');
expect(data.trace.length).toBeGreaterThanOrEqual(3);
});
it('Component 執行: uppercase 轉換', async () => {
const res = await SELF.fetch('http://localhost/execute', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
graph: {
id: 'g2',
name: 'uppercase 測試',
nodes: [
{ id: 'input', type: 'Input', data: { text: 'hello world' } },
{ id: 'upper', type: 'Component', componentId: 'comp_uppercase' },
{ id: 'output', type: 'Output' },
],
edges: [
{ from: 'input', to: 'upper', type: 'PIPE' },
{ from: 'upper', to: 'output', type: 'PIPE' },
],
},
context: {},
}),
});
const data = await res.json() as { success: boolean; data: { text: string } };
expect(data.success).toBe(true);
expect(data.data.text).toBe('HELLO WORLD');
});
it('PIPE 鏈: 多層 counter 累加', async () => {
const res = await SELF.fetch('http://localhost/execute', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
graph: {
id: 'g3',
name: 'counter 測試',
nodes: [
{ id: 'input', type: 'Input', data: { count: 0 } },
{ id: 'c1', type: 'Component', componentId: 'comp_counter' },
{ id: 'c2', type: 'Component', componentId: 'comp_counter' },
{ id: 'c3', type: 'Component', componentId: 'comp_counter' },
{ id: 'output', type: 'Output' },
],
edges: [
{ from: 'input', to: 'c1', type: 'PIPE' },
{ from: 'c1', to: 'c2', type: 'PIPE' },
{ from: 'c2', to: 'c3', type: 'PIPE' },
{ from: 'c3', to: 'output', type: 'PIPE' },
],
},
context: {},
}),
});
const data = await res.json() as { success: boolean; data: { count: number } };
expect(data.success).toBe(true);
expect(data.data.count).toBe(3);
});
it('IF 條件分支', async () => {
const res = await SELF.fetch('http://localhost/execute', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
graph: {
id: 'g4',
name: 'IF 測試',
nodes: [
{ id: 'input', type: 'Input', data: { valid: true, text: 'go' } },
{ id: 'upper', type: 'Component', componentId: 'comp_uppercase' },
{ id: 'output', type: 'Output' },
],
edges: [
{ from: 'input', to: 'upper', type: 'IF', condition: 'result.valid === true' },
{ from: 'upper', to: 'output', type: 'PIPE' },
],
},
context: {},
}),
});
const data = await res.json() as { success: boolean; data: { text: string } };
expect(data.success).toBe(true);
expect(data.data.text).toBe('GO');
});
it('不存在的零件回傳失敗', async () => {
const res = await SELF.fetch('http://localhost/execute', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
graph: {
id: 'g5',
name: '失敗測試',
nodes: [
{ id: 'input', type: 'Input', data: {} },
{ id: 'bad', type: 'Component', componentId: 'comp_not_exist' },
],
edges: [
{ from: 'input', to: 'bad', type: 'PIPE' },
],
},
context: {},
}),
});
const data = await res.json() as { success: boolean; error: string };
expect(data.success).toBe(false);
expect(data.error).toContain('不存在');
});
it('缺少必填欄位回傳 400', async () => {
const res = await SELF.fetch('http://localhost/execute', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ graph: { id: 'x' } }),
});
expect(res.status).toBe(400);
});
});
+215
View File
@@ -0,0 +1,215 @@
/**
* WASI shim 單元測試
* Task 2.2 — Requirements: 3.1, 3.3
*/
import { describe, it, expect } from 'vitest';
import { createWasiShim } from '../src/lib/wasi-shim';
// 建立一個最小的 fake WebAssembly.Memory(用 ArrayBuffer 模擬)
function makeFakeMemory(size = 65536): WebAssembly.Memory {
// 用真實的 WebAssembly.MemoryVitest 環境支援)
return new WebAssembly.Memory({ initial: 1 });
}
/** 在 memory 中寫入 iovec 陣列,回傳 iovs 指標 */
function writeIovecs(
view: DataView,
iovecs: Array<{ buf: number; buf_len: number }>,
startPtr: number,
): number {
for (let i = 0; i < iovecs.length; i++) {
view.setUint32(startPtr + i * 8, iovecs[i].buf, true);
view.setUint32(startPtr + i * 8 + 4, iovecs[i].buf_len, true);
}
return startPtr;
}
describe('createWasiShim', () => {
describe('fd_readstdin', () => {
it('一次讀取完整 stdin', () => {
const input = '{"key":"value"}';
const shim = createWasiShim(input);
const mem = makeFakeMemory();
shim.setMemory(mem);
const view = new DataView(mem.buffer);
const inputBytes = new TextEncoder().encode(input);
// 配置 buffer 區域(offset 100)和 iovecoffset 0
const bufPtr = 100;
const iovsPtr = 0;
const nreadPtr = 50;
writeIovecs(view, [{ buf: bufPtr, buf_len: inputBytes.length }], iovsPtr);
const fd_read = (shim.imports.wasi_snapshot_preview1 as Record<string, Function>).fd_read;
const result = fd_read(0, iovsPtr, 1, nreadPtr);
expect(result).toBe(0); // ESUCCESS
const nread = view.getUint32(nreadPtr, true);
expect(nread).toBe(inputBytes.length);
// 驗證讀取的內容
const readBytes = new Uint8Array(mem.buffer, bufPtr, nread);
expect(new TextDecoder().decode(readBytes)).toBe(input);
});
it('分多次讀取 stdin', () => {
const input = 'hello';
const shim = createWasiShim(input);
const mem = makeFakeMemory();
shim.setMemory(mem);
const view = new DataView(mem.buffer);
const fd_read = (shim.imports.wasi_snapshot_preview1 as Record<string, Function>).fd_read;
// 第一次讀 3 bytes
writeIovecs(view, [{ buf: 200, buf_len: 3 }], 0);
fd_read(0, 0, 1, 50);
expect(view.getUint32(50, true)).toBe(3);
expect(new TextDecoder().decode(new Uint8Array(mem.buffer, 200, 3))).toBe('hel');
// 第二次讀剩餘 2 bytes
writeIovecs(view, [{ buf: 300, buf_len: 10 }], 0);
fd_read(0, 0, 1, 50);
expect(view.getUint32(50, true)).toBe(2);
expect(new TextDecoder().decode(new Uint8Array(mem.buffer, 300, 2))).toBe('lo');
// 第三次讀:stdin 已耗盡,nread = 0
writeIovecs(view, [{ buf: 400, buf_len: 10 }], 0);
fd_read(0, 0, 1, 50);
expect(view.getUint32(50, true)).toBe(0);
});
it('非 stdin fd 回傳 ENOSYS', () => {
const shim = createWasiShim('');
const mem = makeFakeMemory();
shim.setMemory(mem);
const view = new DataView(mem.buffer);
writeIovecs(view, [{ buf: 100, buf_len: 10 }], 0);
const fd_read = (shim.imports.wasi_snapshot_preview1 as Record<string, Function>).fd_read;
expect(fd_read(1, 0, 1, 50)).toBe(76); // ENOSYS
expect(fd_read(2, 0, 1, 50)).toBe(76);
});
});
describe('fd_writestdout/stderr', () => {
it('寫入 stdoutfd=1)並可透過 getStdout 讀取', () => {
const shim = createWasiShim('');
const mem = makeFakeMemory();
shim.setMemory(mem);
const view = new DataView(mem.buffer);
const data = new TextEncoder().encode('{"valid":true}');
const bufPtr = 100;
new Uint8Array(mem.buffer).set(data, bufPtr);
writeIovecs(view, [{ buf: bufPtr, buf_len: data.length }], 0);
const fd_write = (shim.imports.wasi_snapshot_preview1 as Record<string, Function>).fd_write;
const result = fd_write(1, 0, 1, 50);
expect(result).toBe(0);
expect(view.getUint32(50, true)).toBe(data.length);
expect(shim.getStdout()).toBe('{"valid":true}');
});
it('寫入 stderrfd=2)並可透過 getStderr 讀取', () => {
const shim = createWasiShim('');
const mem = makeFakeMemory();
shim.setMemory(mem);
const view = new DataView(mem.buffer);
const data = new TextEncoder().encode('error message');
const bufPtr = 100;
new Uint8Array(mem.buffer).set(data, bufPtr);
writeIovecs(view, [{ buf: bufPtr, buf_len: data.length }], 0);
const fd_write = (shim.imports.wasi_snapshot_preview1 as Record<string, Function>).fd_write;
fd_write(2, 0, 1, 50);
expect(shim.getStderr()).toBe('error message');
expect(shim.getStdout()).toBe(''); // stdout 不受影響
});
it('多次寫入 stdout 會合併', () => {
const shim = createWasiShim('');
const mem = makeFakeMemory();
shim.setMemory(mem);
const view = new DataView(mem.buffer);
const fd_write = (shim.imports.wasi_snapshot_preview1 as Record<string, Function>).fd_write;
const write = (text: string, bufPtr: number) => {
const data = new TextEncoder().encode(text);
new Uint8Array(mem.buffer).set(data, bufPtr);
writeIovecs(view, [{ buf: bufPtr, buf_len: data.length }], 0);
fd_write(1, 0, 1, 50);
};
write('{"valid":', 100);
write('true}', 200);
expect(shim.getStdout()).toBe('{"valid":true}');
});
it('非 stdout/stderr fd 回傳 ENOSYS', () => {
const shim = createWasiShim('');
const mem = makeFakeMemory();
shim.setMemory(mem);
const view = new DataView(mem.buffer);
writeIovecs(view, [{ buf: 100, buf_len: 5 }], 0);
const fd_write = (shim.imports.wasi_snapshot_preview1 as Record<string, Function>).fd_write;
expect(fd_write(0, 0, 1, 50)).toBe(76); // stdin 不能寫
expect(fd_write(3, 0, 1, 50)).toBe(76); // 其他 fd
});
});
describe('proc_exit', () => {
it('proc_exit(0) 拋出 Error(正常結束)', () => {
const shim = createWasiShim('');
const proc_exit = (shim.imports.wasi_snapshot_preview1 as Record<string, Function>).proc_exit;
expect(() => proc_exit(0)).toThrow('wasm exit: 0');
});
it('proc_exit(1) 拋出 Error(錯誤結束)', () => {
const shim = createWasiShim('');
const proc_exit = (shim.imports.wasi_snapshot_preview1 as Record<string, Function>).proc_exit;
expect(() => proc_exit(1)).toThrow('wasm exit: 1');
});
});
describe('其餘 syscall 回傳 ENOSYS', () => {
it('fd_seek 回傳 ENOSYS', () => {
const shim = createWasiShim('');
const fd_seek = (shim.imports.wasi_snapshot_preview1 as Record<string, Function>).fd_seek;
expect(fd_seek(1, 0, 0, 0)).toBe(76);
});
it('sock_connect 相關 syscall 回傳 ENOSYS', () => {
const shim = createWasiShim('');
const imports = shim.imports.wasi_snapshot_preview1 as Record<string, Function>;
expect(imports.sock_recv()).toBe(76);
expect(imports.sock_send()).toBe(76);
expect(imports.sock_shutdown()).toBe(76);
});
it('path_open 回傳 ENOSYS', () => {
const shim = createWasiShim('');
const imports = shim.imports.wasi_snapshot_preview1 as Record<string, Function>;
expect(imports.path_open()).toBe(76);
expect(imports.path_create_directory()).toBe(76);
});
});
describe('setMemory 未呼叫時', () => {
it('fd_write 在 memory 未設定時拋出錯誤', () => {
const shim = createWasiShim('');
// 不呼叫 setMemory
const fd_write = (shim.imports.wasi_snapshot_preview1 as Record<string, Function>).fd_write;
expect(() => fd_write(1, 0, 1, 50)).toThrow('WASI memory not set');
});
});
});