physical_byte_agent/tests/system-integration.test.ts
d-robotics 6d6a805d46
Some checks are pending
CI / quality-gate (push) Waiting to run
CI / type-drift (push) Waiting to run
feat(v3): P1+P2 全落地 — T06–T17 + 系统集成测试
12 模块多 agent 并行开发(dev+test+fix 每,两层 + barrier):
- T06 PbaSession 事件溯源 + deriveMessages + logged 不变量
- T07 PbaEventBus typed event bus + waterfall + ScopeKey 隔离
- T08 能力接缝三角色(board-link + flash Provider/Registry/consumer)
- T09 compaction 完整工程(token-meter/pruner/summarizer/spill)
- T10 CI 质量门 + snapshot + doc-sync
- T11 类型镜像漂移检测(tsconfig.realpi + type-drift-check)
- T12 子代理 + 工作流编排(parallel/pipeline/InlineSubagentProvider)
- T13 上下文注入三段(inject/steer/followup)
- T14 plan/goal/todo checkpoint logged state(look-ahead 状态机)
- T15 profile/bundle 分层(维修诊断 vs 嵌入式开发,共享 base)
- T16 多 LLM provider 适配(ProviderRegistry + glm-5.2/deepseek)
- T17 审批接缝(fail-closed auto-deny)+ credentials/settings/lsp

集成(串行):
- src/index.ts 公共 re-export(board-link/flash 具名避 shared/types 重名)
- tests/system-integration.test.ts 跨模块 10 闭环(事件溯源→compaction→bus→
  接缝→checkpoint→inject→workflow→approval→provider→profile)
- 全量 tsc=0;vitest 39 文件 2449 passed/5 skipped(1044 baseline +~1400 模块 + 10 系统)
- 密钥铁律:\${DROBOTICS_LLM_KEY} 占位,grep sk- 明文 0

68 workflow agent / 0 error / ~3M token;mutation 测试每模块 3-9 seeded bug 全杀。

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-14 15:35:49 +08:00

244 lines
11 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @module system-integration
*
* 系统级集成测试 —— T06–T17 落地后的跨模块闭环验证。
*
* 不是单模块单测(那些各模块自己有 1400+ 测试),而是验证模块间组合的【真实接线】:
* ① 事件溯源闭环:PbaSession.append → deriveMessages 投影 → assertModelVisibleLogged
* ② compaction 接 PbaSession:i2cdump 大输出 prune → spill 句柄 → recall
* ③ typed event bus:waterfall 链委托 + ScopeKey 隔离(子 scope 事件不串主)
* ④ 能力接缝:BoardLinkRegistry/FlashRegistry resolve fail-closed + provider 装配
* ⑤ 闭环 checkpoint:CheckpointRecorder.record → rebuild 从 log 重建
* ⑥ 上下文注入:inject(不唤醒)vs steer(换板唤醒)语义区分
* ⑦ 工作流:parallel(并发 barrier)+ pipeline(串行 stage 抛错落 null)
* ⑧ 审批 fail-closed:无人值守 auto-deny
* ⑨ provider registry:buildProviderConfig 产出 glm-5.2 配置(key 占位)
* ⑩ profile 分层:resolveProfile 共享 base + 叠差异
*
* 密钥铁律:所有 key 断言为 ${DROBOTICS_LLM_KEY} 占位,绝不出现真 key。
*/
import { describe, it, expect } from "vitest";
import { PbaSession } from "../src/core/pba-session.js";
import { PbaEventBus } from "../src/core/pba-event-bus.js";
import { pruneToolResult } from "../src/compaction/tool-result-pruner.js";
import { SpillStore } from "../src/compaction/spill.js";
import { BoardLinkRegistry, RDK_X5_SSH_PROVIDER, RDK_S100_SERIAL_PROVIDER, LOCAL_PROVIDER } from "../src/capabilities/board-link/index.js";
import { FlashRegistry, DD_PROVIDER, FASTBOOT_PROVIDER } from "../src/capabilities/flash/index.js";
import { CheckpointRecorder } from "../src/core/checkpoint-state.js";
import { ContextInjector } from "../src/core/context-injector.js";
import { parallel, pipeline } from "../src/capabilities/workflow/index.js";
import { ApprovalGate } from "../src/capabilities/approval.js";
import { ProviderRegistry, buildProviderConfig, DEFAULT_GLM_DESCRIPTOR } from "../src/core/provider-registry.js";
import { resolveProfile, BASE_PROFILE, REPAIR_DIAGNOSIS_PROFILE, EMBEDDED_DEV_PROFILE } from "../src/profiles/index.js";
import type { BoardSpec } from "../src/shared/types.js";
// 工具:最小 appendEntry 收集器(注入 PbaSession,替代 pi 运行时)
function makeCollector() {
const entries: Array<{ type: string; data: unknown }> = [];
return {
appendEntry: (type: string, data: unknown) => entries.push({ type, data }),
entries,
};
}
describe("system integration — T06–T17 跨模块闭环", () => {
it("① PbaSession 事件溯源:append → deriveMessages 投影 → logged 不变量", () => {
const col = makeCollector();
const sess = new PbaSession({ appendEntry: col.appendEntry });
sess.append({ type: "user-message", data: { content: "读 0x40020000" } });
sess.append({ type: "assistant-message", data: { content: "ok" } });
const msgs = sess.deriveMessages();
expect(Array.isArray(msgs)).toBe(true);
expect(msgs.length).toBeGreaterThan(0);
// 第二次 derive 命中增量缓存(不抛、结果一致)
expect(sess.deriveMessages()).toEqual(msgs);
// logged 不变量:投影里出现的 type 都在 log 里 append 过 → 不抛
expect(() => sess.assertModelVisibleLogged(["user-message", "assistant-message"])).not.toThrow();
// 伪造:声称投影里有但实际没 append 的 type → 抛
expect(() => sess.assertModelVisibleLogged(["verify-record"])).toThrow();
});
it("② compaction 接 PbaSession:i2cdump 大输出 prune 寄存器地址不丢 + spill recall", () => {
const i2cdump = [
" 0 1 2 3 4 5 6 7 8 9 a b c d e f",
"00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
"10: 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10",
"20: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff",
].join("\n");
const pruned = pruneToolResult(i2cdump, { tool: "i2cdump", maxLines: 2 });
// 寄存器地址行(0xXX:)保留
expect(pruned.pruned).toMatch(/10:|20:/);
expect(pruned.dropped).toBeGreaterThan(0);
// spill 句柄 → recall 取回
const store = new SpillStore();
const loc = store.spill("datasheet-page-42", { reg: "0x40020000", desc: "GPIO_CTRL" });
expect(store.has(loc)).toBe(true);
const recalled = store.recall(loc);
expect(recalled).toEqual({ reg: "0x40020000", desc: "GPIO_CTRL" });
});
it("③ PbaEventBus:waterfall 链委托 + ScopeKey 隔离(子 scope 事件不串主)", () => {
const bus = new PbaEventBus();
const order: string[] = [];
bus.on("boardlink:connect", (_args, next) => {
order.push("global-1");
next();
});
bus.on("boardlink:connect", () => {
order.push("global-2-terminal");
});
bus.waterfall("boardlink:connect", { board: "x5" });
// 链顺序:global-1 调 next → global-2-terminal
expect(order).toEqual(["global-1", "global-2-terminal"]);
// scope 隔离:子 scope 注册的监听不响应父 scope 的事件(子不串父)
const subScope = "subagent:child-1";
const seen: string[] = [];
bus.on("verify:passed", () => seen.push("child"), subScope);
// 父 scope(无 scope / 祖先)发事件 → 子 scope 监听不应被触发(单向冒泡:子→父,非父→子)
bus.emit("verify:passed", { ok: true });
expect(seen).toEqual([]);
// 子 scope 自己发事件 → 子监听收(+ 冒泡到父,但这里只断言子收到)
bus.emit("verify:passed", { ok: true }, subScope);
expect(seen).toEqual(["child"]);
});
it("④ 能力接缝:BoardLinkRegistry + FlashRegistry resolve fail-closed + provider 装配", () => {
const bl = new BoardLinkRegistry();
bl.register(RDK_X5_SSH_PROVIDER);
bl.register(RDK_S100_SERIAL_PROVIDER);
expect(bl.list().length).toBe(2);
// register LOCAL → 3(三 provider 各一;按 id 去重不重复)
const dispose = bl.register(LOCAL_PROVIDER);
expect(bl.list().length).toBe(3);
// disposer 后回到 before(2)
dispose();
expect(bl.list().length).toBe(2);
const x5: BoardSpec = { id: "x5", name: "RDK X5", link: "ssh", address: "robot@10.0.0.1", fingerprint: "fp" };
const resolved = bl.resolve(x5);
expect(resolved.id).toBeTruthy();
expect(resolved.linkType).toBe("ssh");
const fl = new FlashRegistry();
fl.register(DD_PROVIDER);
fl.register(FASTBOOT_PROVIDER);
expect(fl.resolve("dd")).toBeDefined();
// 未注册 method(dfu 没装)→ fail-closed 抛错
expect(() => fl.resolve("dfu")).toThrow();
});
it("⑤ CheckpointRecorder:record(look-ahead 推进)→ rebuild 从 log 重建", () => {
const col = makeCollector();
const rec = new CheckpointRecorder({ append: (e) => col.appendEntry(e.type, e.data) });
rec.record("edit", true, 1000);
rec.record("compile", true, 2000);
rec.record("flash", true, 3000);
const cur = rec.current();
// look-ahead 语义:flash 通过后推进到下一 pending 步 verify(stepPassed=false)
expect(cur.currentStep).toBe("verify");
expect(cur.stepPassed).toBe(false);
expect(cur.history.length).toBe(3);
// 从 log 重建等价
const rebuilt = rec.rebuild(col.entries);
expect(rebuilt.currentStep).toBe("verify");
expect(rebuilt.history.length).toBe(3);
// 跑完全程到 done passed → 终态 stepPassed=true(完成态)
rec.record("verify", true, 4000);
rec.record("check", true, 5000);
rec.record("done", true, 6000);
expect(rec.current().currentStep).toBe("done");
expect(rec.current().stepPassed).toBe(true);
});
it("⑥ ContextInjector:inject(不唤醒)vs steer(换板唤醒)语义区分", () => {
const inj = new ContextInjector();
const a = inj.inject("寄存器表 next-step 不唤醒");
expect(a.nextStep).toBe(true);
expect(a.nextTurn).toBe(false);
const s = inj.steer("换板到 S100", "s100-board");
expect(s.boardId).toBe("s100-board");
expect(s.nextStep).toBe(true);
const f = inj.followup("新任务");
expect(f.nextTurn).toBe(true);
// 最新 snapshot 取代更早
const act = inj.deriveActiveContext();
expect(act).toBeDefined();
});
it("⑦ workflow:parallel(并发 barrier)+ pipeline(串行 stage 抛错落 null)", async () => {
const results: number[] = [];
const out = await parallel([
async () => { results.push(1); return "a"; },
async () => { results.push(2); return "b"; },
async () => { results.push(3); return "c"; },
]);
expect(out).toEqual(["a", "b", "c"]);
expect(results.length).toBe(3);
// pipeline 串行:stage2 抛错 → 该 item 落 null,其它不受影响
// stage 签名:(prev, item, i);stage1 的 prev 为 undefined → 用 item
const pip = await pipeline(
[1, 2, 3],
async (_prev, item: number) => item * 10,
async (prev) => { const n = prev as number; return n === 20 ? throwErr() : n + 1; },
);
expect(pip[0]).toBe(11); // 1*10=10 → 11
expect(pip[1]).toBeNull(); // 2*10=20 → 抛错落 null
expect(pip[2]).toBe(31); // 3*10=30 → 31
});
it("⑧ ApprovalGate fail-closed:无人值守 auto-deny", async () => {
const gate = new ApprovalGate({ mode: "auto-deny" });
const r = await gate.request({ action: "flash /dev/mmcblk0", risk: "high" });
expect(r.decision).toBe("deny");
// interactive 模式无 approver → fail-closed deny
gate.setMode("interactive");
const r2 = await gate.request({ action: "rm -rf", risk: "high", timeoutMs: 100 });
expect(r2.decision).toBe("deny");
});
it("⑨ ProviderRegistry:buildProviderConfig 产出 glm-5.2 配置(key 占位)", () => {
const reg = new ProviderRegistry();
reg.register(DEFAULT_GLM_DESCRIPTOR);
const desc = reg.resolve("glm-5.2");
expect(desc).toBeDefined();
const { name, config } = buildProviderConfig(desc);
expect(name).toBeTruthy();
// 密钥铁律:key 必须是 ${DROBOTICS_LLM_KEY} 占位,绝不出现 sk- 明文
const keyStr = JSON.stringify(config);
expect(keyStr).toContain("${DROBOTICS_LLM_KEY}");
expect(keyStr).not.toMatch(/sk-nAm/i);
expect(/sk-[A-Za-z0-9]{20,}/.test(keyStr)).toBe(false);
});
it("⑩ profile 分层:两 profile 共享 base + 各叠差异", () => {
expect(BASE_PROFILE.extensions.length).toBeGreaterThan(0);
// 维修诊断有 pdf-reader/datasheet-rag;嵌入式开发有 cross-compile/flash
const repair = REPAIR_DIAGNOSIS_PROFILE.extensions as readonly string[];
const emb = EMBEDDED_DEV_PROFILE.extensions as readonly string[];
expect(repair.some((e) => e.includes("pdf") || e.includes("datasheet"))).toBe(true);
expect(emb.some((e) => e.includes("cross-compile") || e.includes("flash"))).toBe(true);
// resolveProfile 未知 id fail-closed
expect(() => resolveProfile("nonexistent-profile")).toThrow();
});
});
function throwErr(): never {
throw new Error("boom");
}