L1 落地(设计文档 §7/§18/§19.2 全工作项): - M1.1 shared/types.ts: ExperienceEntry/BoardMemory/TaskSignature/Validity/ ValidationKind + assertAsymmetry(§4.6 不对称运行时强制) + similarityTask/ matchBoard/validityWeight/experienceId 纯函数 - M1.2/1.5/1.6 core/experience-store.ts: ExperienceStore (append 幂等去重+ §4.6 强制 / query 打分排序+检索升权 / evict 时间淘汰+β失配降权+低命中衰减 / BoardMemory upsert 聚合); 可注入后端(in-memory/sqlite)+可注入时钟 - M1.3/1.4 extensions/experience-bank.ts: before_agent_start 检索 topK + 注入 experiencePack(仿 env-probe) + session_before_compact 蒸馏 distillEntry (v_t 分级+§4.6 强制入库)+板级记忆聚合 - barrel 登记(第 23 工厂); agent.ts 计数 21→23 M1.7 量化评估(tests/eval/, seeded mulberry32 N=400 受控队列 A/B/C/D/E + Wilson 95% CI, 独立复跑一致): ΔJ_full=0.34>=0.15 同板 61%→95%(+34pp) ΔJ_held_out=0.1545>=0.05 held-out 板 +15.5pp(排除 F4 记忆) 因果检索增益=0.3534>=0.20 distill-off |J_D-J_B|=0<0.05 证改进来自记忆增长 F4 负控 ΔJ_held_out_E=0<=0.02 证 held-out 门有效 CI 非重叠 STRONG (J_A_lo=0.9240>J_B_hi=0.6565) 真 store 与对照实现 5 指标全等 结论: M1 机制有效(量化/可信/可复现)。 验证: tsc 0 / 全量 42 文件 2522 passed 5 skipped / sk- 明文 0 / 真 pi T01 boot 过 Co-Authored-By: Claude <noreply@anthropic.com>
349 lines
13 KiB
TypeScript
349 lines
13 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
||
import type {
|
||
BoardFingerprint,
|
||
ExperienceEntry,
|
||
ExperienceQuery,
|
||
TaskSignature,
|
||
} from "../src/shared/types.js";
|
||
import {
|
||
assertAsymmetry,
|
||
experienceId,
|
||
matchBoard,
|
||
similarityTask,
|
||
validityWeight,
|
||
} from "../src/shared/types.js";
|
||
import {
|
||
createExperienceStore,
|
||
distillEntry,
|
||
InMemoryExperienceBackend,
|
||
rankHits,
|
||
scoreEntry,
|
||
scoreHit,
|
||
DEFAULT_DECAY,
|
||
DEFAULT_MAX_AGE_MS,
|
||
WEIGHT_TASK,
|
||
WEIGHT_BOARD,
|
||
} from "../src/core/experience-store.js";
|
||
import type { ExperienceStore, DistillInput } from "../src/core/experience-store.js";
|
||
|
||
// ---- 测试夹具 ----
|
||
const beta1: BoardFingerprint = { soc: "rdk-x5", kernel: "6.1.83", image: "v1", mac: "AA:01" };
|
||
const beta2: BoardFingerprint = { soc: "rdk-x5", kernel: "6.1.83", image: "v2", mac: "BB:02" };
|
||
const beta3: BoardFingerprint = { soc: "s100", kernel: "5.4", image: "v1", mac: "CC:03" };
|
||
|
||
const sigFlash: TaskSignature = { actionClass: "flash", target: "bootloader", constraints: ["aarch64", "low-power"] };
|
||
const sigFlash2: TaskSignature = { actionClass: "flash", target: "kernel", constraints: ["aarch64"] };
|
||
const sigI2c: TaskSignature = { actionClass: "i2c-probe", target: "pmic 0x3a", constraints: ["aarch64"] };
|
||
|
||
function makeEntry(over: Partial<ExperienceEntry> = {}): ExperienceEntry {
|
||
const e: ExperienceEntry = {
|
||
id: "",
|
||
boardFingerprint: beta1,
|
||
taskSignature: sigFlash,
|
||
outcome: "success",
|
||
actionable: "dd if=image of=/dev/mmcblk0",
|
||
antiPattern: undefined,
|
||
evidenceRef: "seq#1-3",
|
||
contextTags: ["flash", "bootloader"],
|
||
validity: "repeatable",
|
||
usage: "actionable",
|
||
createdAt: 1000,
|
||
hitCount: 0,
|
||
validatedBy: "physical-ground-truth",
|
||
...over,
|
||
};
|
||
e.id = e.id || experienceId(e);
|
||
return e;
|
||
}
|
||
|
||
describe("ExperienceStore.append —— 幂等 + §4.6 不对称", () => {
|
||
it("同内容二次 append → 去重,hitCount 累积(复发升权 + 检索升权)", () => {
|
||
const s = createExperienceStore();
|
||
const e = makeEntry();
|
||
s.append(e); // 插入 hitCount=0
|
||
s.append(e); // 幂等复发 → hitCount=1
|
||
expect(s.size()).toBe(1);
|
||
const hits = s.query({ taskSignature: sigFlash, boardFingerprint: beta1, topK: 5 });
|
||
expect(hits.length).toBe(1);
|
||
// append 复发 → 1;query 检索升权 → 2
|
||
expect(hits[0]?.entry.hitCount).toBe(2);
|
||
});
|
||
|
||
it("不同内容 → 两条", () => {
|
||
const s = createExperienceStore();
|
||
s.append(makeEntry({ taskSignature: sigFlash }));
|
||
s.append(makeEntry({ taskSignature: sigI2c }));
|
||
expect(s.size()).toBe(2);
|
||
});
|
||
|
||
it("§4.6 违例抛错:irreversible-fail + actionable 拒绝入库", () => {
|
||
const s = createExperienceStore();
|
||
const bad = makeEntry({ validity: "irreversible-fail", usage: "actionable" });
|
||
expect(() => s.append(bad)).toThrow(/§4.6/);
|
||
expect(s.size()).toBe(0);
|
||
});
|
||
|
||
it("§4.6 违例抛错:inferred + actionable 拒绝入库", () => {
|
||
const s = createExperienceStore();
|
||
const bad = makeEntry({ validity: "inferred", usage: "actionable" });
|
||
expect(() => s.append(bad)).toThrow(/§4.6/);
|
||
});
|
||
|
||
it("§4.6 合法:irreversible-fail + anti-pattern 允许", () => {
|
||
const s = createExperienceStore();
|
||
const ok = makeEntry({
|
||
outcome: "failure",
|
||
validity: "irreversible-fail",
|
||
usage: "anti-pattern",
|
||
actionable: "",
|
||
antiPattern: "切勿对 emmc 执行 dd if=/dev/zero",
|
||
});
|
||
expect(() => s.append(ok)).not.toThrow();
|
||
expect(s.size()).toBe(1);
|
||
});
|
||
});
|
||
|
||
describe("ExperienceStore.query —— 排序 + topK + 升权", () => {
|
||
it("同板同类同类目标优先;异类降权", () => {
|
||
const s = createExperienceStore();
|
||
s.append(makeEntry({ taskSignature: sigI2c, actionable: "i2cdump -y 0 0x3a" })); // 异类
|
||
s.append(makeEntry({ taskSignature: sigFlash, actionable: "dd if=img of=/dev/mmcblk0" })); // 同类同目标
|
||
const hits = s.query({ taskSignature: sigFlash, boardFingerprint: beta1, topK: 5 });
|
||
expect(hits[0]?.entry.taskSignature.actionClass).toBe("flash");
|
||
expect(hits[0]?.simTask).toBeGreaterThan(hits[1]?.simTask ?? 0);
|
||
});
|
||
|
||
it("同 SoC 异镜像(beta2)降权但可返回;异板(beta3)更狠降权", () => {
|
||
const s = createExperienceStore();
|
||
s.append(makeEntry({ boardFingerprint: beta1 })); // 同板
|
||
s.append(makeEntry({ boardFingerprint: beta2 })); // 同 SoC 异镜像
|
||
const hits = s.query({ taskSignature: sigFlash, boardFingerprint: beta1, topK: 5 });
|
||
expect(hits[0]?.entry.boardFingerprint.mac).toBe("AA:01");
|
||
expect(hits[0]?.matchBoard).toBe(1.0);
|
||
const sameSocDiffImg = hits.find((h) => h.entry.boardFingerprint.mac === "BB:02");
|
||
expect(sameSocDiffImg?.matchBoard).toBe(0.6);
|
||
});
|
||
|
||
it("topK 截断", () => {
|
||
const s = createExperienceStore();
|
||
for (let i = 0; i < 6; i++) s.append(makeEntry({ taskSignature: { ...sigFlash, target: `tgt${i}` } }));
|
||
const hits = s.query({ taskSignature: sigFlash, boardFingerprint: beta1, topK: 3 });
|
||
expect(hits.length).toBeLessThanOrEqual(3);
|
||
});
|
||
|
||
it("检索即升权:query 后命中条目 hitCount++", () => {
|
||
const s = createExperienceStore();
|
||
s.append(makeEntry({ taskSignature: sigFlash }));
|
||
const before = s.query({ taskSignature: sigFlash, boardFingerprint: beta1, topK: 5 });
|
||
expect(before[0]?.entry.hitCount).toBe(1); // append 0 + query 1
|
||
const after = s.query({ taskSignature: sigFlash, boardFingerprint: beta1, topK: 5 });
|
||
expect(after[0]?.entry.hitCount).toBe(2); // 再 query +1
|
||
});
|
||
|
||
it("irreversible-fail 同板可作 anti-pattern 提示(即便 simTask 低)", () => {
|
||
const s = createExperienceStore();
|
||
s.append(
|
||
makeEntry({
|
||
outcome: "failure",
|
||
validity: "irreversible-fail",
|
||
usage: "anti-pattern",
|
||
actionable: "",
|
||
antiPattern: "切勿擦除 emmc 引导区",
|
||
taskSignature: { actionClass: "flash", target: "emmc-boot", constraints: [] },
|
||
}),
|
||
);
|
||
// 查不同 target 但同板 → matchBoard=1,irreversible-fail 仍应返回
|
||
const hits = s.query({
|
||
taskSignature: { actionClass: "flash", target: "kernel", constraints: [] },
|
||
boardFingerprint: beta1,
|
||
topK: 5,
|
||
});
|
||
const ap = hits.find((h) => h.entry.validity === "irreversible-fail");
|
||
expect(ap).toBeDefined();
|
||
expect(ap?.entry.usage).toBe("anti-pattern");
|
||
});
|
||
});
|
||
|
||
describe("ExperienceStore.evict —— 时间淘汰 + β 失配降权", () => {
|
||
it("超时条目删除,近期保留", () => {
|
||
let clock = 10_000;
|
||
const s = createExperienceStore(new InMemoryExperienceBackend(), () => clock);
|
||
// 两条不同内容(不同 actionable → 不同 id,避免幂等合并)
|
||
s.append(makeEntry({ actionable: "dd if=old of=/dev/mmcblk0", createdAt: 1_000 })); // 老
|
||
s.append(makeEntry({ actionable: "dd if=new of=/dev/mmcblk0", createdAt: 9_000 })); // 近
|
||
expect(s.size()).toBe(2);
|
||
const n = s.evict({ maxAgeMs: 5000 }); // 阈值 5000:老(1000)删,近(9000)留
|
||
expect(n).toBe(1);
|
||
expect(s.size()).toBe(1);
|
||
});
|
||
|
||
it("β 失配降权:异板条目 hitCount 被衰减", () => {
|
||
let clock = 10_000;
|
||
const s = createExperienceStore(new InMemoryExperienceBackend(), () => clock);
|
||
// 板1 经验 hitCount 拉高
|
||
s.append(makeEntry({ boardFingerprint: beta1, hitCount: 10 }));
|
||
s.append(makeEntry({ boardFingerprint: beta3, hitCount: 10 })); // 异板 s100
|
||
const before = s.query({ taskSignature: sigFlash, boardFingerprint: beta1, topK: 5 });
|
||
// 现在 evict 用 currentBoard=beta1 → beta3 失配降权
|
||
s.evict({ currentBoard: beta1, decay: 0.5 });
|
||
// 重查看 hitCount:beta1 不降,beta3 降
|
||
const after = s.query({ taskSignature: sigFlash, boardFingerprint: beta1, topK: 5 });
|
||
const b1 = after.find((h) => h.entry.boardFingerprint.mac === "AA:01");
|
||
const b3 = after.find((h) => h.entry.boardFingerprint.mac === "CC:03");
|
||
// beta1 未被 evict 降权(matchBoard=1),beta3 被降(matchBoard=0)
|
||
// 注意 query 本身 +1,故只比相对:beta3 的 hitCount 应明显小于 beta1
|
||
expect(b3).toBeDefined();
|
||
if (b1 && b3) {
|
||
expect(b3.entry.hitCount).toBeLessThan(b1.entry.hitCount);
|
||
}
|
||
});
|
||
|
||
it("默认 maxAgeMs(DEFAULT_MAX_AGE_MS)不删近期条目", () => {
|
||
let clock = 1_000_000;
|
||
const s = createExperienceStore(new InMemoryExperienceBackend(), () => clock);
|
||
s.append(makeEntry({ createdAt: 999_000 })); // 近期
|
||
s.evict(); // 默认 30 天
|
||
expect(s.size()).toBe(1);
|
||
});
|
||
});
|
||
|
||
describe("ExperienceStore.BoardMemory —— upsert 聚合", () => {
|
||
it("upsert 两次 → 聚合 + knownGood 去重", () => {
|
||
const s = createExperienceStore();
|
||
s.upsertBoardMemory({
|
||
boardFingerprint: beta1,
|
||
moduleFunctionality: { "i2c-2": "PMIC 0x3a" },
|
||
knownGoodCommands: ["uname -m", "i2cdetect -y 0"],
|
||
knownBadPatterns: ["dd if=/dev/zero of=/dev/mmcblk0"],
|
||
peripheralState: { "i2c-2": "online" },
|
||
lastSeen: 1000,
|
||
});
|
||
s.upsertBoardMemory({
|
||
boardFingerprint: beta1,
|
||
moduleFunctionality: { "gpiochip379": "舵机腿" },
|
||
knownGoodCommands: ["i2cdetect -y 0", "gpioget gpiochip379 5"],
|
||
knownBadPatterns: [],
|
||
peripheralState: { "gpiochip379": "online" },
|
||
lastSeen: 2000,
|
||
});
|
||
const m = s.getBoardMemory(beta1);
|
||
expect(m).not.toBeNull();
|
||
expect(m?.moduleFunctionality["i2c-2"]).toBe("PMIC 0x3a");
|
||
expect(m?.moduleFunctionality["gpiochip379"]).toBe("舵机腿");
|
||
expect(m?.knownGoodCommands).toEqual(["uname -m", "i2cdetect -y 0", "gpioget gpiochip379 5"]);
|
||
expect(m?.knownBadPatterns).toEqual(["dd if=/dev/zero of=/dev/mmcblk0"]);
|
||
expect(m?.lastSeen).toBe(2000);
|
||
});
|
||
|
||
it("异板记忆隔离(key=mac)", () => {
|
||
const s = createExperienceStore();
|
||
s.upsertBoardMemory({
|
||
boardFingerprint: beta1,
|
||
moduleFunctionality: { x: "1" },
|
||
knownGoodCommands: [],
|
||
knownBadPatterns: [],
|
||
peripheralState: {},
|
||
lastSeen: 1,
|
||
});
|
||
expect(s.getBoardMemory(beta2)).toBeNull();
|
||
});
|
||
});
|
||
|
||
describe("distillEntry —— §4.3 蒸馏 + §4.6 分级", () => {
|
||
const base: DistillInput = {
|
||
boardFingerprint: beta1,
|
||
taskSignature: sigFlash,
|
||
outcome: "success",
|
||
actionable: "dd if=image of=/dev/mmcblk0",
|
||
antiPattern: undefined,
|
||
evidenceRef: "seq#1",
|
||
contextTags: ["flash"],
|
||
validatedBy: "physical-ground-truth",
|
||
irreversible: false,
|
||
createdAt: 1000,
|
||
};
|
||
|
||
it("success + physical-ground-truth → repeatable / actionable", () => {
|
||
const e = distillEntry({ ...base });
|
||
expect(e.validity).toBe("repeatable");
|
||
expect(e.usage).toBe("actionable");
|
||
});
|
||
|
||
it("success + model-claim → inferred / hint(不可 actionable,§4.6)", () => {
|
||
const e = distillEntry({ ...base, validatedBy: "model-claim" });
|
||
expect(e.validity).toBe("inferred");
|
||
expect(e.usage).toBe("hint");
|
||
});
|
||
|
||
it("failure + irreversible → irreversible-fail / anti-pattern", () => {
|
||
const e = distillEntry({ ...base, outcome: "failure", irreversible: true, actionable: "", antiPattern: "烧错偏移" });
|
||
expect(e.validity).toBe("irreversible-fail");
|
||
expect(e.usage).toBe("anti-pattern");
|
||
});
|
||
|
||
it("failure + reversible → once / anti-pattern", () => {
|
||
const e = distillEntry({ ...base, outcome: "failure", irreversible: false, actionable: "", antiPattern: "i2c 地址错" });
|
||
expect(e.validity).toBe("once");
|
||
expect(e.usage).toBe("anti-pattern");
|
||
});
|
||
|
||
it("partial → once / hint", () => {
|
||
const e = distillEntry({ ...base, outcome: "partial" });
|
||
expect(e.validity).toBe("once");
|
||
expect(e.usage).toBe("hint");
|
||
});
|
||
|
||
it("确定性 id(同入参 → 同 id)", () => {
|
||
expect(distillEntry({ ...base }).id).toBe(distillEntry({ ...base }).id);
|
||
});
|
||
});
|
||
|
||
describe("纯函数 scoreEntry / rankHits / scoreHit", () => {
|
||
it("scoreHit = WEIGHT_TASK*sim + WEIGHT_BOARD*match", () => {
|
||
expect(scoreHit(1, 0)).toBeCloseTo(WEIGHT_TASK, 5);
|
||
expect(scoreHit(0, 1)).toBeCloseTo(WEIGHT_BOARD, 5);
|
||
expect(scoreHit(1, 1)).toBeCloseTo(1, 5);
|
||
});
|
||
|
||
it("rankHits 排序 + topK", () => {
|
||
const q: ExperienceQuery = { taskSignature: sigFlash, boardFingerprint: beta1, topK: 2 };
|
||
const h1 = scoreEntry(makeEntry({ taskSignature: sigFlash, boardFingerprint: beta1 }), q);
|
||
const h2 = scoreEntry(makeEntry({ taskSignature: sigI2c, boardFingerprint: beta1 }), q);
|
||
const ranked = rankHits([h2, h1], 2);
|
||
expect(ranked[0]?.entry.taskSignature.actionClass).toBe("flash");
|
||
});
|
||
|
||
it("score=0 且非 irreversible-fail 被滤除", () => {
|
||
const q: ExperienceQuery = { taskSignature: sigFlash, boardFingerprint: beta1, topK: 5 };
|
||
// 异类 + 异板 + 无任何 token/constraint 重叠 → simTask=0, matchBoard=0 → score=0 → 滤除
|
||
const zero = makeEntry({
|
||
taskSignature: { actionClass: "build", target: "zzz-unique", constraints: ["none"] },
|
||
boardFingerprint: beta3,
|
||
});
|
||
const h = scoreEntry(zero, q);
|
||
expect(h.score).toBe(0);
|
||
expect(rankHits([h], 5).length).toBe(0);
|
||
});
|
||
});
|
||
|
||
describe("类型层纯函数(sanity)", () => {
|
||
it("similarityTask 同类 > 异类", () => {
|
||
expect(similarityTask(sigFlash, sigFlash)).toBeGreaterThan(similarityTask(sigFlash, sigI2c));
|
||
});
|
||
it("matchBoard 同板=1,同SoC异镜像=0.6,异板=0", () => {
|
||
expect(matchBoard(beta1, beta1)).toBe(1);
|
||
expect(matchBoard(beta1, beta2)).toBe(0.6);
|
||
expect(matchBoard(beta1, beta3)).toBe(0);
|
||
});
|
||
it("validityWeight 优先级 repeatable>once>inferred;irreversible-fail 高", () => {
|
||
expect(validityWeight("repeatable")).toBeGreaterThan(validityWeight("once"));
|
||
expect(validityWeight("once")).toBeGreaterThan(validityWeight("inferred"));
|
||
expect(validityWeight("irreversible-fail")).toBeGreaterThan(validityWeight("inferred"));
|
||
});
|
||
it("assertAsymmetry 不抛合法组合", () => {
|
||
expect(() => assertAsymmetry("repeatable", "actionable")).not.toThrow();
|
||
expect(() => assertAsymmetry("irreversible-fail", "anti-pattern")).not.toThrow();
|
||
expect(() => assertAsymmetry("inferred", "hint")).not.toThrow();
|
||
});
|
||
});
|