- 工程形态:@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>
252 lines
9.2 KiB
TypeScript
252 lines
9.2 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
||
import {
|
||
computeEdit,
|
||
countOccurrences,
|
||
normalizeForFuzzyMatch,
|
||
isStrictByDefault,
|
||
STRICT_BY_DEFAULT_EXTENSIONS,
|
||
validateEditChain,
|
||
type EditMode,
|
||
} from "../src/extensions/edit-strict.js";
|
||
|
||
describe("edit-strict exact 模式 — 唯一精确匹配", () => {
|
||
it("唯一命中:成功替换,replacement mode=exact", () => {
|
||
const content = "foo bar baz";
|
||
const r = computeEdit(content, "bar", "BAR", "exact");
|
||
expect(r.error).toBeUndefined();
|
||
expect(r.newContent).toBe("foo BAR baz");
|
||
expect(r.replacements).toHaveLength(1);
|
||
expect(r.replacements[0]).toMatchObject({ mode: "exact", index: 4, length: 3 });
|
||
});
|
||
|
||
it("多匹配报错(fail-closed,绝不模糊替换)", () => {
|
||
const content = "&i2c0{\n&i2c1{\n&i2c1{\n";
|
||
const r = computeEdit(content, "&i2c1{", "X", "exact");
|
||
expect(r.error).toMatch(/unique/i);
|
||
expect(r.replacements).toHaveLength(0);
|
||
// newContent 不变
|
||
expect(r.newContent).toBe(content);
|
||
});
|
||
|
||
it("零匹配报错(not found)", () => {
|
||
const r = computeEdit("hello world", "goodbye", "hi", "exact");
|
||
expect(r.error).toMatch(/not found/i);
|
||
expect(r.replacements).toHaveLength(0);
|
||
expect(r.newContent).toBe("hello world");
|
||
});
|
||
|
||
it("空 oldString 报错(fail-closed no-op/歧义)", () => {
|
||
const r = computeEdit("abc", "", "X", "exact");
|
||
expect(r.error).toMatch(/empty/i);
|
||
expect(r.newContent).toBe("abc");
|
||
});
|
||
|
||
it("未改行尾随空白逐字节不变(.dts 对齐安全)", () => {
|
||
// keep 行与 end 行各有 3 个尾随空格;只改中间行
|
||
const content = "keep \nfoo bar\nend \n";
|
||
const r = computeEdit(content, "foo bar", "FOO BAR", "exact");
|
||
expect(r.error).toBeUndefined();
|
||
expect(r.newContent).toBe("keep \nFOO BAR\nend \n");
|
||
});
|
||
|
||
it("默认 mode=exact(不传 mode)", () => {
|
||
const r = computeEdit("a b c", "b", "B");
|
||
expect(r.error).toBeUndefined();
|
||
expect(r.newContent).toBe("a B c");
|
||
expect(r.replacements[0]?.mode).toBe("exact");
|
||
});
|
||
|
||
it(".dts 场景:oldText 缺尾随空格而原文有 → exact not-found 而非错配到相邻节点", () => {
|
||
const content = "&i2c0 {\n&i2c1 {\n";
|
||
// agent 给的 oldText 无空格,原文 "&i2c1 {" 有空格
|
||
const r = computeEdit(content, "&i2c1{", "X", "exact");
|
||
expect(r.error).toMatch(/not found/i);
|
||
expect(r.newContent).toBe(content); // 原文不动,不误改 &i2c0
|
||
});
|
||
|
||
it(".ld 场景:段名前缀重复时 oldText 不足 → exact not-found 拒绝错配", () => {
|
||
const content = ".text.init : { *(.text.init) }\n.text.startup : { *(.text.startup) }\n";
|
||
// oldText 只给 ".text" 会在多处命中 → unique 错误
|
||
const r = computeEdit(content, ".text", "X", "exact");
|
||
expect(r.error).toMatch(/unique/i);
|
||
expect(r.newContent).toBe(content);
|
||
});
|
||
});
|
||
|
||
describe("edit-strict fuzzy 模式 — 兜底最近匹配", () => {
|
||
it("exact 已唯一命中时直接用原文(mode=exact,不归一)", () => {
|
||
const r = computeEdit("foo bar baz", "bar", "BAR", "fuzzy");
|
||
expect(r.error).toBeUndefined();
|
||
expect(r.newContent).toBe("foo BAR baz");
|
||
expect(r.replacements[0]?.mode).toBe("exact");
|
||
});
|
||
|
||
it("exact 零命中时 fuzzy 归一化兜底命中(mode=fuzzy)", () => {
|
||
// 原文中间行有尾随空格,agent oldText 无尾随空格含换行
|
||
const content = "keep \nfoo bar \nend \n";
|
||
const r = computeEdit(content, "foo bar\n", "BAZ\n", "fuzzy");
|
||
expect(r.error).toBeUndefined();
|
||
expect(r.replacements).toHaveLength(1);
|
||
expect(r.replacements[0]?.mode).toBe("fuzzy");
|
||
});
|
||
|
||
it("fuzzy 替换基于原文:未改行对齐空白零变化", () => {
|
||
const content = "keep \nfoo bar \nend \n";
|
||
const r = computeEdit(content, "foo bar\n", "BAZ\n", "fuzzy");
|
||
expect(r.error).toBeUndefined();
|
||
// keep 行与 end 行的 3 个尾随空格完整保留
|
||
expect(r.newContent).toBe("keep \nBAZ\nend \n");
|
||
});
|
||
|
||
it("fuzzy 多次 exact 命中仍 fail-closed(.dts 重复节点)", () => {
|
||
const content = "&i2c1{\n&i2c1{\n";
|
||
const r = computeEdit(content, "&i2c1{", "X", "fuzzy");
|
||
expect(r.error).toMatch(/unique/i);
|
||
expect(r.replacements).toHaveLength(0);
|
||
});
|
||
|
||
it("fuzzy 归一后仍零命中 → 报错", () => {
|
||
const r = computeEdit("alpha beta gamma", "zzz", "X", "fuzzy");
|
||
expect(r.error).toMatch(/not found/i);
|
||
expect(r.newContent).toBe("alpha beta gamma");
|
||
});
|
||
|
||
it("fuzzy 智能 quote 差异能兜底命中", () => {
|
||
// 原文用智能引号 “foo”,agent oldText 用 ASCII "foo
|
||
const content = 'line “foo” end\n';
|
||
const r = computeEdit(content, '"foo"', '"BAR"', "fuzzy");
|
||
expect(r.error).toBeUndefined();
|
||
expect(r.replacements[0]?.mode).toBe("fuzzy");
|
||
expect(r.newContent).toBe('line "BAR" end\n');
|
||
});
|
||
|
||
it("fuzzy 归一后为空(oldText 全空白)→ fail-closed", () => {
|
||
const r = computeEdit("foo bar", " ", "X", "fuzzy");
|
||
expect(r.error).toMatch(/empty/i);
|
||
expect(r.newContent).toBe("foo bar");
|
||
});
|
||
});
|
||
|
||
describe("edit-strict countOccurrences", () => {
|
||
it("非重叠计数", () => {
|
||
expect(countOccurrences("aaaa", "aa")).toBe(2);
|
||
});
|
||
it("零次", () => {
|
||
expect(countOccurrences("abc", "z")).toBe(0);
|
||
});
|
||
it("空 needle 返回 0", () => {
|
||
expect(countOccurrences("abc", "")).toBe(0);
|
||
});
|
||
});
|
||
|
||
describe("edit-strict normalizeForFuzzyMatch — 位置映射", () => {
|
||
it("trimEnd 移除行尾空白但 map 不含被移除位置", () => {
|
||
const { text, map } = normalizeForFuzzyMatch("foo \n");
|
||
// 归一化文本无尾随空格
|
||
expect(text).toBe("foo\n");
|
||
// map 的每个值指向原文非空格字符
|
||
expect(map).toEqual([0, 1, 2, 6]);
|
||
});
|
||
|
||
it("智能引号 1:1 替换且 map 指向原位置", () => {
|
||
const { text, map } = normalizeForFuzzyMatch("“x”");
|
||
expect(text).toBe('"x"');
|
||
expect(map).toEqual([0, 1, 2]);
|
||
});
|
||
});
|
||
|
||
describe("edit-strict 文件类型自动门", () => {
|
||
it(".dts/.dtsi/.ld/.lds/.sct/.icf 默认 strict", () => {
|
||
expect(isStrictByDefault("board.dts")).toBe(true);
|
||
expect(isStrictByDefault("/path/to/soc.dtsi")).toBe(true);
|
||
expect(isStrictByDefault("linker.ld")).toBe(true);
|
||
expect(isStrictByDefault("boot.lds")).toBe(true);
|
||
expect(isStrictByDefault("scatter.sct")).toBe(true);
|
||
expect(isStrictByDefault("config.icf")).toBe(true);
|
||
});
|
||
it(".ts/.py/.c/.h 非默认 strict", () => {
|
||
expect(isStrictByDefault("app.ts")).toBe(false);
|
||
expect(isStrictByDefault("main.py")).toBe(false);
|
||
expect(isStrictByDefault("main.c")).toBe(false);
|
||
});
|
||
it("扩展名大小写不敏感", () => {
|
||
expect(isStrictByDefault("BOARD.DTS")).toBe(true);
|
||
expect(isStrictByDefault("linker.LD")).toBe(true);
|
||
});
|
||
it("规则集覆盖 6 类扩展名", () => {
|
||
expect(STRICT_BY_DEFAULT_EXTENSIONS.length).toBeGreaterThanOrEqual(6);
|
||
});
|
||
});
|
||
|
||
describe("edit-strict validateEditChain — edit 链预校验(钩子纯逻辑内核)", () => {
|
||
it("多条全通过:逐条喂入并累计替换", () => {
|
||
const content = "foo bar\nbaz qux\n";
|
||
const r = validateEditChain(content, [
|
||
{ oldText: "foo", newText: "FOO" },
|
||
{ oldText: "baz", newText: "BAZ" },
|
||
], "exact");
|
||
expect(r.error).toBeUndefined();
|
||
expect(r.content).toBe("FOO bar\nBAZ qux\n");
|
||
});
|
||
|
||
it("第二条 fail-closed:原文不动,仅上报错误", () => {
|
||
const content = "foo bar\nbaz qux\n";
|
||
const r = validateEditChain(content, [
|
||
{ oldText: "foo", newText: "FOO" },
|
||
{ oldText: "missing", newText: "X" }, // 零命中
|
||
], "exact");
|
||
expect(r.error).toMatch(/not found/i);
|
||
// fail-closed:原文不动(非半改状态)
|
||
expect(r.content).toBe(content);
|
||
});
|
||
|
||
it("重复 oldText fail-closed:原文不动", () => {
|
||
const content = "&i2c1{\n&i2c1{\n";
|
||
const r = validateEditChain(content, [{ oldText: "&i2c1{", newText: "X" }], "exact");
|
||
expect(r.error).toMatch(/unique/i);
|
||
expect(r.content).toBe(content);
|
||
});
|
||
|
||
it("空 edits 列表:原文不动无错误(no-op 放行)", () => {
|
||
const content = "unchanged\n";
|
||
const r = validateEditChain(content, [], "exact");
|
||
expect(r.error).toBeUndefined();
|
||
expect(r.content).toBe(content);
|
||
});
|
||
|
||
it("缺省 oldText/newText 字段视为空串 → fail-closed", () => {
|
||
const content = "abc\n";
|
||
const r = validateEditChain(content, [{}], "exact");
|
||
expect(r.error).toMatch(/empty/i);
|
||
expect(r.content).toBe(content);
|
||
});
|
||
|
||
it("默认 mode=exact(不传 mode)", () => {
|
||
const r = validateEditChain("a b c", [{ oldText: "b", newText: "B" }]);
|
||
expect(r.error).toBeUndefined();
|
||
expect(r.content).toBe("a B c");
|
||
});
|
||
|
||
it("链式替换允许前一条产生的新内容被后一条匹配", () => {
|
||
// 第一条把 foo→bar,第二条再把 bar→BAR
|
||
const r = validateEditChain("foo\n", [
|
||
{ oldText: "foo", newText: "bar" },
|
||
{ oldText: "bar", newText: "BAR" },
|
||
], "exact");
|
||
expect(r.error).toBeUndefined();
|
||
expect(r.content).toBe("BAR\n");
|
||
});
|
||
|
||
it(".dts 多节点链式:逐条唯一命中全通过", () => {
|
||
const content = "&i2c0 {\n\tstatus = \"okay\";\n};\n&i2c1 {\n\tstatus = \"okay\";\n};\n";
|
||
// 每条 oldText 含足够上下文唯一命中
|
||
const r = validateEditChain(content, [
|
||
{ oldText: '&i2c0 {\n\tstatus = "okay";', newText: '&i2c0 {\n\tstatus = "disabled";' },
|
||
{ oldText: '&i2c1 {\n\tstatus = "okay";', newText: '&i2c1 {\n\tstatus = "disabled";' },
|
||
], "exact");
|
||
expect(r.error).toBeUndefined();
|
||
expect(r.content).toContain('"disabled"');
|
||
expect(r.content).not.toContain('"okay"');
|
||
});
|
||
});
|