- 工程形态:@d-robotics/physical-byte-agent npm 库包
- 单一 barrel src/extensions/index.ts 装配 21 域扩展(package.json pi.extensions 清单 + .pi/extensions/index.ts 重导出,pi 自动加载)
- src/agent.ts PhysicalByteAgent(launch() 程序化调 createAgentSession + verifyAssembly() 离线自检)
- bin/pba.ts CLI、models.json(glm-5.2 条目)、README.md、AGENTS.md
- 命名:全部域命名(glm-provider/safety-gate/flash/jtag-debugger/loop-orchestrator…),去掉 F 编号
- 架构:内联逻辑 + 顶层仅 import type(esbuild 擦除)+ 工厂内动态 import defineTool/Type
- 逻辑离线可单测(不解析 pi),工厂在真 pi 内装配;fail-closed 语义保留
- 两命门:glm-provider.ts buildGlmModelConfig()(thinkingFormat:qwen + maxTokens:8192 + thinkingLevelMap 全 null)+ detectEmptyOutput();normalizeHexArg() + makeHexNormalizingPrepareArguments()
- 验证:tsc --noEmit 0 错(对 src/types/pi.d.ts 类型镜像)+ vitest 732 用例/21 文件全绿 + tsx 离线装配冒烟 21 扩展全就位
- 密钥铁律:grep sk-nAm 在 src/tests/bin/.pi = 0,key 全程 ${DROBOTICS_LLM_KEY} 占位
Co-Authored-By: Claude <noreply@anthropic.com>
315 lines
10 KiB
TypeScript
315 lines
10 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
||
import {
|
||
CheckpointMachine,
|
||
classifyCommand,
|
||
STEP_ORDER,
|
||
STEP_PREREQUISITES,
|
||
assertCommandOrder,
|
||
type CheckpointDecision,
|
||
} from "../src/extensions/loop-orchestrator.js";
|
||
import type { CheckpointStep, CheckpointState } from "../src/shared/types.js";
|
||
|
||
// ============================================================================
|
||
// classifyCommand — bash 命令分类到 checkpoint step
|
||
// ============================================================================
|
||
|
||
describe("loop-orchestrator classifyCommand", () => {
|
||
it("aarch64-linux-gnu-gcc → compile", () => {
|
||
expect(classifyCommand("aarch64-linux-gnu-gcc -o app main.c")).toBe("compile");
|
||
});
|
||
|
||
it("cmake --build → compile", () => {
|
||
expect(classifyCommand("cmake --build . --config Release")).toBe("compile");
|
||
});
|
||
|
||
it("make → compile", () => {
|
||
expect(classifyCommand("make -j4")).toBe("compile");
|
||
});
|
||
|
||
it("dd of=/dev/mmcblk0 → flash", () => {
|
||
expect(classifyCommand("dd if=image.bin of=/dev/mmcblk0 bs=4M")).toBe("flash");
|
||
});
|
||
|
||
it("fastboot flash → flash", () => {
|
||
expect(classifyCommand("fastboot flash boot boot.img")).toBe("flash");
|
||
});
|
||
|
||
it("scp to board → flash", () => {
|
||
expect(classifyCommand("scp app root@192.168.1.100:/root/")).toBe("flash");
|
||
});
|
||
|
||
it("dmesg → check", () => {
|
||
expect(classifyCommand("dmesg | grep i2c")).toBe("check");
|
||
});
|
||
|
||
it("journalctl → check", () => {
|
||
expect(classifyCommand("journalctl -u my-service -n 50")).toBe("check");
|
||
});
|
||
|
||
it("无关命令 ls → null", () => {
|
||
expect(classifyCommand("ls -la")).toBeNull();
|
||
});
|
||
|
||
it("空命令 → null", () => {
|
||
expect(classifyCommand("")).toBeNull();
|
||
expect(classifyCommand(" ")).toBeNull();
|
||
});
|
||
|
||
it("sudo dd 也能识别 flash", () => {
|
||
expect(classifyCommand("sudo dd if=image.bin of=/dev/mmcblk0")).toBe("flash");
|
||
});
|
||
});
|
||
|
||
// ============================================================================
|
||
// CheckpointMachine — 顺序违规 block(fail-closed 核心)
|
||
// ============================================================================
|
||
|
||
describe("loop-orchestrator 顺序违规 block (fail-closed)", () => {
|
||
it("flash 前未编译 → assertCommandOrder block", () => {
|
||
const m = new CheckpointMachine();
|
||
// 初始在 edit,compile 未通过
|
||
const d = assertCommandOrder(m, "dd if=image.bin of=/dev/mmcblk0");
|
||
expect(d.block).toBe(true);
|
||
expect(d.reason).toContain("compile");
|
||
});
|
||
|
||
it("flash 前已编译通过 → 放行", () => {
|
||
const m = new CheckpointMachine();
|
||
m.advance("compile", true);
|
||
const d = assertCommandOrder(m, "dd if=image.bin of=/dev/mmcblk0");
|
||
expect(d.block).toBe(false);
|
||
});
|
||
|
||
it("verify 前未 flash → block", () => {
|
||
const m = new CheckpointMachine();
|
||
m.advance("compile", true);
|
||
// flash 还没过,直接 verify 应 block
|
||
const d = m.assertOrder("flash", "verify-board-id-abc");
|
||
expect(d.block).toBe(true);
|
||
expect(d.reason).toContain("flash");
|
||
});
|
||
|
||
it("check 前未 flash → block(dmesg 前须烧过板)", () => {
|
||
const m = new CheckpointMachine();
|
||
m.advance("compile", true);
|
||
const d = assertCommandOrder(m, "dmesg | grep i2c");
|
||
expect(d.block).toBe(true);
|
||
expect(d.reason).toContain("flash");
|
||
});
|
||
|
||
it("compile 无前置条件 → 放行", () => {
|
||
const m = new CheckpointMachine();
|
||
const d = assertCommandOrder(m, "aarch64-linux-gnu-gcc -o app main.c");
|
||
expect(d.block).toBe(false);
|
||
});
|
||
|
||
it("无关命令(ls)→ 放行(null step 不收 checkpoint)", () => {
|
||
const m = new CheckpointMachine();
|
||
const d = assertCommandOrder(m, "ls -la");
|
||
expect(d.block).toBe(false);
|
||
});
|
||
|
||
it("assertOrder 显式指定前置:未满足时 block", () => {
|
||
const m = new CheckpointMachine();
|
||
// before=compile,但 compile 未通过
|
||
const d = m.assertOrder("compile", "dd if=img of=/dev/mmcblk0");
|
||
expect(d.block).toBe(true);
|
||
expect(d.reason).toContain("Compile checkpoint not passed");
|
||
});
|
||
|
||
it("assertOrder 显式前置满足后放行", () => {
|
||
const m = new CheckpointMachine();
|
||
m.advance("compile", true);
|
||
const d = m.assertOrder("compile", "dd if=img of=/dev/mmcblk0");
|
||
expect(d.block).toBe(false);
|
||
});
|
||
|
||
it("check 前已 flash 通过 → 放行", () => {
|
||
const m = new CheckpointMachine();
|
||
m.advance("compile", true);
|
||
m.advance("flash", true);
|
||
const d = assertCommandOrder(m, "dmesg | grep i2c");
|
||
expect(d.block).toBe(false);
|
||
expect(d.step).toBe("check");
|
||
});
|
||
});
|
||
|
||
// ============================================================================
|
||
// CheckpointMachine — advance 推进
|
||
// ============================================================================
|
||
|
||
describe("loop-orchestrator advance 推进", () => {
|
||
it("初始状态 currentStep=edit, stepPassed=false", () => {
|
||
const m = new CheckpointMachine();
|
||
expect(m.getCurrentStep()).toBe("edit");
|
||
expect(m.isStepPassed()).toBe(false);
|
||
expect(m.getHistory()).toHaveLength(0);
|
||
});
|
||
|
||
it("compile 通过 → currentStep 推进到 flash, stepPassed 重置 false", () => {
|
||
const m = new CheckpointMachine();
|
||
m.advance("compile", true);
|
||
expect(m.getCurrentStep()).toBe("flash");
|
||
expect(m.isStepPassed()).toBe(false);
|
||
expect(m.isPassed("compile")).toBe(true);
|
||
});
|
||
|
||
it("compile 失败 → 留在 compile, stepPassed false", () => {
|
||
const m = new CheckpointMachine();
|
||
m.advance("compile", false);
|
||
expect(m.getCurrentStep()).toBe("compile");
|
||
expect(m.isStepPassed()).toBe(false);
|
||
expect(m.isPassed("compile")).toBe(false);
|
||
});
|
||
|
||
it("完整闭环 edit→compile→flash→verify→check→done", () => {
|
||
const m = new CheckpointMachine();
|
||
m.advance("edit", true);
|
||
expect(m.getCurrentStep()).toBe("compile");
|
||
m.advance("compile", true);
|
||
expect(m.getCurrentStep()).toBe("flash");
|
||
m.advance("flash", true);
|
||
expect(m.getCurrentStep()).toBe("verify");
|
||
m.advance("verify", true);
|
||
expect(m.getCurrentStep()).toBe("check");
|
||
m.advance("check", true);
|
||
expect(m.getCurrentStep()).toBe("done");
|
||
});
|
||
|
||
it("history 累积每步记录", () => {
|
||
const m = new CheckpointMachine();
|
||
m.advance("compile", true);
|
||
m.advance("flash", true);
|
||
const h = m.getHistory();
|
||
expect(h).toHaveLength(2);
|
||
expect(h[0]?.step).toBe("compile");
|
||
expect(h[0]?.passed).toBe(true);
|
||
expect(h[1]?.step).toBe("flash");
|
||
expect(h[1]?.passed).toBe(true);
|
||
});
|
||
|
||
it("失败也入 history(passed=false)", () => {
|
||
const m = new CheckpointMachine();
|
||
m.advance("compile", false);
|
||
m.advance("compile", true);
|
||
const h = m.getHistory();
|
||
expect(h).toHaveLength(2);
|
||
expect(h[0]?.passed).toBe(false);
|
||
expect(h[1]?.passed).toBe(true);
|
||
});
|
||
|
||
it("重复通过同一步(重入)不重复推进 currentStep", () => {
|
||
const m = new CheckpointMachine();
|
||
m.advance("compile", true);
|
||
expect(m.getCurrentStep()).toBe("flash");
|
||
// 重入 compile 通过:记 history 但不回退 currentStep
|
||
m.advance("compile", true);
|
||
expect(m.getCurrentStep()).toBe("flash");
|
||
expect(m.isPassed("compile")).toBe(true);
|
||
});
|
||
|
||
it("done 通过后停在 done 且 stepPassed=true", () => {
|
||
const m = new CheckpointMachine();
|
||
m.advance("edit", true);
|
||
m.advance("compile", true);
|
||
m.advance("flash", true);
|
||
m.advance("verify", true);
|
||
m.advance("check", true);
|
||
m.advance("done", true);
|
||
expect(m.getCurrentStep()).toBe("done");
|
||
expect(m.isStepPassed()).toBe(true);
|
||
});
|
||
});
|
||
|
||
// ============================================================================
|
||
// CheckpointMachine — serialize / restore
|
||
// ============================================================================
|
||
|
||
describe("loop-orchestrator serialize/restore", () => {
|
||
it("serialize 返回完整 CheckpointState", () => {
|
||
const m = new CheckpointMachine();
|
||
m.advance("compile", true);
|
||
m.advance("flash", false);
|
||
const s = m.serialize();
|
||
expect(s.currentStep).toBe("flash");
|
||
expect(s.stepPassed).toBe(false);
|
||
expect(s.history).toHaveLength(2);
|
||
expect(s.history[0]?.step).toBe("compile");
|
||
});
|
||
|
||
it("restore 重建后状态一致", () => {
|
||
const m1 = new CheckpointMachine();
|
||
m1.advance("edit", true);
|
||
m1.advance("compile", true);
|
||
m1.advance("flash", true);
|
||
const snap = m1.serialize();
|
||
|
||
const m2 = new CheckpointMachine();
|
||
m2.restore(snap);
|
||
expect(m2.getCurrentStep()).toBe("verify");
|
||
expect(m2.isPassed("compile")).toBe(true);
|
||
expect(m2.isPassed("flash")).toBe(true);
|
||
expect(m2.getHistory()).toHaveLength(3);
|
||
});
|
||
|
||
it("restore 空状态安全", () => {
|
||
const m = new CheckpointMachine();
|
||
m.restore({ currentStep: "edit", stepPassed: false, history: [] });
|
||
expect(m.getCurrentStep()).toBe("edit");
|
||
expect(m.getHistory()).toHaveLength(0);
|
||
});
|
||
|
||
it("restore 非法 currentStep 不崩溃(兜底回 edit)", () => {
|
||
const m = new CheckpointMachine();
|
||
m.restore({ currentStep: "unknown" as CheckpointStep, stepPassed: false, history: [] } as CheckpointState);
|
||
expect(m.getCurrentStep()).toBe("edit");
|
||
});
|
||
|
||
it("serialize 后修改不影响原状态(深拷贝)", () => {
|
||
const m = new CheckpointMachine();
|
||
m.advance("compile", true);
|
||
const s = m.serialize();
|
||
// 篡改序列化结果
|
||
s.history.push({ step: "done", passed: true, timestamp: 0 });
|
||
s.currentStep = "done";
|
||
// 原状态不受影响
|
||
expect(m.getCurrentStep()).toBe("flash");
|
||
expect(m.getHistory()).toHaveLength(1);
|
||
});
|
||
});
|
||
|
||
// ============================================================================
|
||
// 常量与结构
|
||
// ============================================================================
|
||
|
||
describe("loop-orchestrator 常量", () => {
|
||
it("STEP_ORDER 是 6 步闭环", () => {
|
||
expect(STEP_ORDER).toEqual(["edit", "compile", "flash", "verify", "check", "done"]);
|
||
});
|
||
|
||
it("STEP_PREREQUISITES: flash 须 compile", () => {
|
||
expect(STEP_PREREQUISITES.flash).toBe("compile");
|
||
});
|
||
|
||
it("STEP_PREREQUISITES: verify 须 flash", () => {
|
||
expect(STEP_PREREQUISITES.verify).toBe("flash");
|
||
});
|
||
|
||
it("STEP_PREREQUISITES: check 须 flash", () => {
|
||
expect(STEP_PREREQUISITES.check).toBe("flash");
|
||
});
|
||
|
||
it("STEP_PREREQUISITES: compile 无前置", () => {
|
||
expect(STEP_PREREQUISITES.compile).toBeNull();
|
||
});
|
||
|
||
it("CheckpointDecision block 时携带 reason", () => {
|
||
const m = new CheckpointMachine();
|
||
const d: CheckpointDecision = assertCommandOrder(m, "dd if=img of=/dev/mmcblk0");
|
||
expect(d.block).toBe(true);
|
||
expect(typeof d.reason).toBe("string");
|
||
expect(d.reason!.length).toBeGreaterThan(0);
|
||
expect(d.step).toBe("flash");
|
||
});
|
||
});
|