- 工程形态:@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>
441 lines
14 KiB
TypeScript
441 lines
14 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
||
import {
|
||
BuildTracker,
|
||
OutputAccumulator,
|
||
planBuild,
|
||
shouldSkip,
|
||
type SpawnFn,
|
||
type SpawnedProcess,
|
||
} from "../src/extensions/build-tracker.js";
|
||
|
||
// ============================================================================
|
||
// 测试用 Fake 进程:可控的 stdout/stderr/close 注入。
|
||
// ============================================================================
|
||
|
||
class FakeProcess implements SpawnedProcess {
|
||
pid: number;
|
||
private stdoutHandlers: Array<(data: string) => void> = [];
|
||
private stderrHandlers: Array<(data: string) => void> = [];
|
||
private closeHandlers: Array<(code: number | null, signal?: string) => void> = [];
|
||
|
||
constructor(pid: number) {
|
||
this.pid = pid;
|
||
}
|
||
onStdout(h: (data: string) => void): void {
|
||
this.stdoutHandlers.push(h);
|
||
}
|
||
onStderr(h: (data: string) => void): void {
|
||
this.stderrHandlers.push(h);
|
||
}
|
||
onClose(h: (code: number | null, signal?: string) => void): void {
|
||
this.closeHandlers.push(h);
|
||
}
|
||
/** 测试驱动:模拟进程输出。 */
|
||
emitStdout(data: string): void {
|
||
for (const h of this.stdoutHandlers) h(data);
|
||
}
|
||
emitStderr(data: string): void {
|
||
for (const h of this.stderrHandlers) h(data);
|
||
}
|
||
/** 测试驱动:模拟进程结束。 */
|
||
emitClose(code: number | null, signal?: string): void {
|
||
for (const h of this.closeHandlers) h(code, signal);
|
||
}
|
||
}
|
||
|
||
function makeSpawnFn(proc: FakeProcess): SpawnFn {
|
||
return () => proc;
|
||
}
|
||
|
||
// ============================================================================
|
||
// planBuild — 构建命令规划
|
||
// ============================================================================
|
||
|
||
describe("build-tracker planBuild — 构建命令规划", () => {
|
||
it("buildroot → make all", () => {
|
||
const p = planBuild("buildroot", "all");
|
||
expect(p.command).toBe("make");
|
||
expect(p.args).toEqual(["all"]);
|
||
});
|
||
|
||
it("buildroot 默认 target=all", () => {
|
||
const p = planBuild("buildroot");
|
||
expect(p.args).toEqual(["all"]);
|
||
});
|
||
|
||
it("yocto → bitbake core-image-minimal", () => {
|
||
const p = planBuild("yocto");
|
||
expect(p.command).toBe("bitbake");
|
||
expect(p.args).toEqual(["core-image-minimal"]);
|
||
});
|
||
|
||
it("yocto 自定义 target", () => {
|
||
const p = planBuild("yocto", "linux-rdk");
|
||
expect(p.args).toEqual(["linux-rdk"]);
|
||
});
|
||
|
||
it("cmake → cmake --build . --target all", () => {
|
||
const p = planBuild("cmake");
|
||
expect(p.command).toBe("cmake");
|
||
expect(p.args).toEqual(["--build", ".", "--target", "all"]);
|
||
});
|
||
|
||
it("cmake 带 target", () => {
|
||
const p = planBuild("cmake", "firmware");
|
||
expect(p.args).toEqual(["--build", ".", "--target", "firmware"]);
|
||
});
|
||
|
||
it("kconfig → make defconfig", () => {
|
||
const p = planBuild("kconfig");
|
||
expect(p.command).toBe("make");
|
||
expect(p.args).toEqual(["defconfig"]);
|
||
});
|
||
|
||
it("buildDir 透传到 cwd", () => {
|
||
const p = planBuild("cmake", "all", { buildDir: "/build/root" });
|
||
expect(p.cwd).toBe("/build/root");
|
||
});
|
||
});
|
||
|
||
// ============================================================================
|
||
// shouldSkip — 增量感知 fail-closed
|
||
// ============================================================================
|
||
|
||
describe("build-tracker shouldSkip — 增量跳过(fail-closed)", () => {
|
||
it("源码未变(srcMtime <= lastBuildMtime)→ 跳过", () => {
|
||
expect(shouldSkip(1000, 2000)).toBe(true);
|
||
});
|
||
|
||
it("源码在上次构建后变了(srcMtime > lastBuildMtime)→ 不跳过", () => {
|
||
expect(shouldSkip(3000, 2000)).toBe(false);
|
||
});
|
||
|
||
it("无上次构建(lastBuildMtime=0)→ 不跳过(fail-closed)", () => {
|
||
expect(shouldSkip(1000, 0)).toBe(false);
|
||
});
|
||
|
||
it("源码与上次构建同时戳 → 跳过(边界 ==)", () => {
|
||
expect(shouldSkip(2000, 2000)).toBe(true);
|
||
});
|
||
|
||
it("无上次构建且负数 mtime → 不跳过", () => {
|
||
expect(shouldSkip(500, -1)).toBe(false);
|
||
});
|
||
});
|
||
|
||
// ============================================================================
|
||
// OutputAccumulator — 有界缓冲
|
||
// ============================================================================
|
||
|
||
describe("build-tracker OutputAccumulator — 简化缓冲", () => {
|
||
it("append 多行后 tail 返回尾部 N 行", () => {
|
||
const acc = new OutputAccumulator("/tmp/b.log");
|
||
acc.append("line1\nline2\nline3\n");
|
||
expect(acc.tail(2)).toEqual(["line2", "line3"]);
|
||
});
|
||
|
||
it("tail(0) 返回空数组", () => {
|
||
const acc = new OutputAccumulator("/tmp/b.log");
|
||
acc.append("a\nb\n");
|
||
expect(acc.tail(0)).toEqual([]);
|
||
});
|
||
|
||
it("tail 超过已有行数 → 返回全部", () => {
|
||
const acc = new OutputAccumulator("/tmp/b.log");
|
||
acc.append("only1\nonly2\n");
|
||
expect(acc.tail(50)).toEqual(["only1", "only2"]);
|
||
});
|
||
|
||
it("跨 chunk 半行残片拼接", () => {
|
||
const acc = new OutputAccumulator("/tmp/b.log");
|
||
acc.append("half-"); // 无换行 → 残片
|
||
acc.append("line\nsecond\n"); // 拼成 half-line
|
||
expect(acc.allLines()).toEqual(["half-line", "second"]);
|
||
});
|
||
|
||
it("stderr 与 stdout 混合追加", () => {
|
||
const acc = new OutputAccumulator("/tmp/b.log");
|
||
acc.append("stdout-line\n");
|
||
acc.append("stderr-line\n");
|
||
expect(acc.lineCount).toBe(2);
|
||
expect(acc.tail(2)).toEqual(["stdout-line", "stderr-line"]);
|
||
});
|
||
|
||
it("tempPath 暴露给扩展层", () => {
|
||
const acc = new OutputAccumulator("/tmp/build-1.log");
|
||
expect(acc.tempPath).toBe("/tmp/build-1.log");
|
||
});
|
||
});
|
||
|
||
// ============================================================================
|
||
// BuildTracker — start / status
|
||
// ============================================================================
|
||
|
||
describe("build-tracker BuildTracker — start/status", () => {
|
||
it("start 立即返回 buildId+pid+logFile+running,不阻塞", () => {
|
||
const proc = new FakeProcess(4242);
|
||
const tracker = new BuildTracker(makeSpawnFn(proc), {
|
||
clock: () => 1000,
|
||
});
|
||
const res = tracker.start("cmake", "all");
|
||
expect(res.status).toBe("running");
|
||
expect(res.pid).toBe(4242);
|
||
expect(res.buildId).toMatch(/^build-\d+$/);
|
||
expect(res.logFile).toContain(res.buildId);
|
||
expect(res.skipped).toBeUndefined();
|
||
// 进程未结束 → 仍 running
|
||
expect(tracker.status(res.buildId)?.status).toBe("running");
|
||
});
|
||
|
||
it("进程 close code=0 → status=done,exitCode=0", () => {
|
||
const proc = new FakeProcess(1);
|
||
let t = 1000;
|
||
const tracker = new BuildTracker(makeSpawnFn(proc), { clock: () => t });
|
||
const res = tracker.start("buildroot");
|
||
t = 5000;
|
||
proc.emitClose(0);
|
||
const st = tracker.status(res.buildId);
|
||
expect(st?.status).toBe("done");
|
||
expect(st?.exitCode).toBe(0);
|
||
expect(st?.durationMs).toBe(4000);
|
||
});
|
||
|
||
it("进程 close code!=0 → status=failed", () => {
|
||
const proc = new FakeProcess(2);
|
||
const tracker = new BuildTracker(makeSpawnFn(proc));
|
||
const res = tracker.start("make", "all");
|
||
proc.emitClose(2);
|
||
expect(tracker.status(res.buildId)?.status).toBe("failed");
|
||
expect(tracker.status(res.buildId)?.exitCode).toBe(2);
|
||
});
|
||
|
||
it("进程被信号杀(code=null)→ status=failed", () => {
|
||
const proc = new FakeProcess(3);
|
||
const tracker = new BuildTracker(makeSpawnFn(proc));
|
||
const res = tracker.start("yocto", "core-image-minimal");
|
||
proc.emitClose(null, "SIGKILL");
|
||
const st = tracker.status(res.buildId);
|
||
expect(st?.status).toBe("failed");
|
||
expect(st?.exitCode).toBe(-1);
|
||
expect(st?.signal).toBe("SIGKILL");
|
||
});
|
||
|
||
it("status 未知 buildId → null(fail-closed)", () => {
|
||
const tracker = new BuildTracker(makeSpawnFn(new FakeProcess(0)));
|
||
expect(tracker.status("nonexistent")).toBeNull();
|
||
});
|
||
|
||
it("buildId 单调递增", () => {
|
||
const proc = new FakeProcess(10);
|
||
const tracker = new BuildTracker(makeSpawnFn(proc));
|
||
const a = tracker.start("cmake");
|
||
const b = tracker.start("cmake");
|
||
expect(a.buildId).not.toBe(b.buildId);
|
||
});
|
||
|
||
it("running 时 durationMs = now - startedAt", () => {
|
||
const proc = new FakeProcess(11);
|
||
let t = 100;
|
||
const tracker = new BuildTracker(makeSpawnFn(proc), { clock: () => t });
|
||
const res = tracker.start("cmake");
|
||
t = 350;
|
||
expect(tracker.status(res.buildId)?.durationMs).toBe(250);
|
||
});
|
||
});
|
||
|
||
// ============================================================================
|
||
// BuildTracker — tail 尾部日志
|
||
// ============================================================================
|
||
|
||
describe("build-tracker BuildTracker — tail 尾部日志", () => {
|
||
it("tail 返回尾部 N 行(stdout+stderr 混合)", () => {
|
||
const proc = new FakeProcess(20);
|
||
const tracker = new BuildTracker(makeSpawnFn(proc));
|
||
const res = tracker.start("cmake");
|
||
proc.emitStdout("[ 10%] Building CXX foo\n[ 50%] Building CXX bar\n");
|
||
proc.emitStderr("warning: unused variable\n");
|
||
proc.emitClose(0);
|
||
const tail = tracker.tail(res.buildId, 2);
|
||
expect(tail).toEqual(["[ 50%] Building CXX bar", "warning: unused variable"]);
|
||
});
|
||
|
||
it("tail 默认 50 行", () => {
|
||
const proc = new FakeProcess(21);
|
||
const tracker = new BuildTracker(makeSpawnFn(proc));
|
||
const res = tracker.start("make");
|
||
// 60 行
|
||
let out = "";
|
||
for (let i = 0; i < 60; i++) out += `line${i}\n`;
|
||
proc.emitStdout(out);
|
||
proc.emitClose(0);
|
||
const tail = tracker.tail(res.buildId);
|
||
expect(tail).toHaveLength(50);
|
||
expect(tail[0]).toBe("line10");
|
||
expect(tail[49]).toBe("line59");
|
||
});
|
||
|
||
it("tail 未知 buildId → 空数组(fail-closed)", () => {
|
||
const tracker = new BuildTracker(makeSpawnFn(new FakeProcess(0)));
|
||
expect(tracker.tail("nope")).toEqual([]);
|
||
});
|
||
});
|
||
|
||
// ============================================================================
|
||
// BuildTracker — start 增量跳过(注入 mtime provider)
|
||
// ============================================================================
|
||
|
||
describe("build-tracker BuildTracker — start 增量跳过", () => {
|
||
it("源码未变 + incremental:true + clean:false → skipped,不 spawn", () => {
|
||
const proc = new FakeProcess(30);
|
||
const spawnCalled = { n: 0 };
|
||
const spawnFn: SpawnFn = () => {
|
||
spawnCalled.n++;
|
||
return proc;
|
||
};
|
||
const tracker = new BuildTracker(spawnFn, {
|
||
getSrcMtime: () => 1000,
|
||
getLastBuildMtime: () => 2000,
|
||
});
|
||
const res = tracker.start("cmake", "all", {
|
||
incremental: true,
|
||
clean: false,
|
||
buildDir: "/build",
|
||
});
|
||
expect(res.skipped).toBe(true);
|
||
expect(res.status).toBe("done");
|
||
expect(res.pid).toBe(0);
|
||
expect(res.skipReason).toContain("No source changes");
|
||
expect(spawnCalled.n).toBe(0);
|
||
});
|
||
|
||
it("源码变了 → 不跳过,正常 spawn", () => {
|
||
const proc = new FakeProcess(31);
|
||
const spawnCalled = { n: 0 };
|
||
const spawnFn: SpawnFn = () => {
|
||
spawnCalled.n++;
|
||
return proc;
|
||
};
|
||
const tracker = new BuildTracker(spawnFn, {
|
||
getSrcMtime: () => 3000,
|
||
getLastBuildMtime: () => 2000,
|
||
});
|
||
const res = tracker.start("cmake", "all", {
|
||
incremental: true,
|
||
buildDir: "/build",
|
||
});
|
||
expect(res.skipped).toBeUndefined();
|
||
expect(res.status).toBe("running");
|
||
expect(spawnCalled.n).toBe(1);
|
||
});
|
||
|
||
it("clean:true → 绝不跳过(即使源码未变)", () => {
|
||
const proc = new FakeProcess(32);
|
||
const spawnCalled = { n: 0 };
|
||
const spawnFn: SpawnFn = () => {
|
||
spawnCalled.n++;
|
||
return proc;
|
||
};
|
||
const tracker = new BuildTracker(spawnFn, {
|
||
getSrcMtime: () => 1000,
|
||
getLastBuildMtime: () => 2000,
|
||
});
|
||
const res = tracker.start("cmake", "all", {
|
||
incremental: true,
|
||
clean: true,
|
||
buildDir: "/build",
|
||
});
|
||
expect(res.skipped).toBeUndefined();
|
||
expect(res.status).toBe("running");
|
||
expect(spawnCalled.n).toBe(1);
|
||
});
|
||
|
||
it("incremental:false → 绝不跳过(禁用增量感知)", () => {
|
||
const proc = new FakeProcess(33);
|
||
const spawnCalled = { n: 0 };
|
||
const spawnFn: SpawnFn = () => {
|
||
spawnCalled.n++;
|
||
return proc;
|
||
};
|
||
const tracker = new BuildTracker(spawnFn, {
|
||
getSrcMtime: () => 1000,
|
||
getLastBuildMtime: () => 2000,
|
||
});
|
||
const res = tracker.start("cmake", "all", {
|
||
incremental: false,
|
||
buildDir: "/build",
|
||
});
|
||
expect(res.skipped).toBeUndefined();
|
||
expect(spawnCalled.n).toBe(1);
|
||
});
|
||
|
||
it("无 buildDir → 不判定跳过(fail-closed,正常 spawn)", () => {
|
||
const proc = new FakeProcess(34);
|
||
const spawnCalled = { n: 0 };
|
||
const spawnFn: SpawnFn = () => {
|
||
spawnCalled.n++;
|
||
return proc;
|
||
};
|
||
const tracker = new BuildTracker(spawnFn, {
|
||
getSrcMtime: () => 1000,
|
||
getLastBuildMtime: () => 2000,
|
||
});
|
||
const res = tracker.start("cmake", "all", { incremental: true });
|
||
expect(res.skipped).toBeUndefined();
|
||
expect(spawnCalled.n).toBe(1);
|
||
});
|
||
|
||
it("源码 mtime 未知(getSrcMtime 返回 0)→ fail-closed 不跳过", () => {
|
||
const proc = new FakeProcess(35);
|
||
const spawnCalled = { n: 0 };
|
||
const spawnFn: SpawnFn = () => {
|
||
spawnCalled.n++;
|
||
return proc;
|
||
};
|
||
const tracker = new BuildTracker(spawnFn, {
|
||
getSrcMtime: () => 0,
|
||
getLastBuildMtime: () => 2000,
|
||
});
|
||
const res = tracker.start("cmake", "all", {
|
||
incremental: true,
|
||
clean: false,
|
||
buildDir: "/build",
|
||
});
|
||
expect(res.skipped).toBeUndefined();
|
||
expect(res.status).toBe("running");
|
||
expect(spawnCalled.n).toBe(1);
|
||
});
|
||
});
|
||
|
||
// ============================================================================
|
||
// BuildTracker — 辅助方法
|
||
// ============================================================================
|
||
|
||
describe("build-tracker BuildTracker — accumulatorOf / listBuilds", () => {
|
||
it("accumulatorOf 返回对应构建的累积器", () => {
|
||
const proc = new FakeProcess(40);
|
||
const tracker = new BuildTracker(makeSpawnFn(proc));
|
||
const res = tracker.start("cmake");
|
||
proc.emitStdout("hello\n");
|
||
const acc = tracker.accumulatorOf(res.buildId);
|
||
expect(acc).not.toBeNull();
|
||
expect(acc?.lineCount).toBe(1);
|
||
expect(acc?.allLines()).toEqual(["hello"]);
|
||
});
|
||
|
||
it("accumulatorOf 未知 buildId → null", () => {
|
||
const tracker = new BuildTracker(makeSpawnFn(new FakeProcess(0)));
|
||
expect(tracker.accumulatorOf("nope")).toBeNull();
|
||
});
|
||
|
||
it("listBuilds 返回所有已知 buildId", () => {
|
||
const proc = new FakeProcess(41);
|
||
const tracker = new BuildTracker(makeSpawnFn(proc));
|
||
const a = tracker.start("cmake");
|
||
const b = tracker.start("make");
|
||
const ids = tracker.listBuilds();
|
||
expect(ids).toContain(a.buildId);
|
||
expect(ids).toContain(b.buildId);
|
||
expect(ids).toHaveLength(2);
|
||
});
|
||
});
|