M4 L4 编排进化层落地(设计文档 §10/§19.3 M4.1-M4.5):
- M4.1 shared/types.ts: DifficultyLevel/DifficultyEstimate/WorkflowNode/
WorkflowGraph/DifficultyCorrection + estimateDifficulty/riskOfWorkflow/
workflowSimilarity/backpropGradient/correctDifficultyPrior 纯函数
- M4.1-4.4 core/workflow-optimizer.ts: WorkflowRegistry(按 taskClass 分桶)
+ WorkflowOptimizer(estimateDifficulty/selectWorkflow升权/evolveWorkflow
符号反向传播 backpropGradient+applyGradient+§4.8 risk 不增守卫/
registerWorkflow supersede/correctPrior 滑动窗) + applyGradient 纯函数
- M4.5 extensions/orchestration-bank.ts: before_agent_start 难度估+workflow
注入 <orchestration_plan> + session_before_compact 先验校正 +
orchestration_recall 工具(仿 skill-bank)
- barrel 第 26 工厂登记
M5 L5 脚手架自改层落地(终局最强约束,设计文档 §11/§19.3 M5.1-M5.5):
- M5.1 shared/types.ts: VariantStage/ScaffoldClass/Variant/InvariantReport/
InvariantChecker + classifyScaffold/canSelfModify/allInvariantsPassed 纯函数
- M5.1-5.3 core/variant-manager.ts: VariantRegistry + VariantManager(propose
白名单 forbidden 命门 throw §11.3/runInvariants 注入 InvariantChecker/
abTest 注入 ABTestRunner/tryPromote 不变式全过+A/B 胜出→promoted 不变式
失败→rolled-back/rollback 可逆/resolve 只返 promoted) +
classifyProposalVariant/promotionDecision 纯函数
- M5.4-5.5 extensions/variant-bank.ts: variant_propose/variant_list/
variant_promote/variant_rollback 工具 + 注入 <active_variants>(仿 tool-bank)
- barrel 第 27 工厂登记
M4 量化评估(tests/eval/orchestration-eval.test.ts, seeded mulberry32 N=400 5 队列
+ Wilson CI + 真 optimizer 对齐, 独立复跑一致):
ΔJ_G=0.2975>=0.10 workflow 进化 49.25%->79%(+29.75pp, θ 固定归因 G 非模型)
ΔJ_held_out=0.3182>=0.03 held-out 板 +31.82pp(泛化)
同类不同 workflow max 2/class>=2 非平凡进化
难度估与实际步数 Pearson corr=0.6121>0 估计器有意义
evolve-off |J_D-J_B|=0<0.05 证改进来自 G 增长非 select 路径
risk 不增 EXACT 构造铁律 max(evolved risk)=1<=default=1(§4.8)
risk-increase 负控 242 退化候选全被守卫拒绝
CI 非重叠 STRONG (J_A_lo=0.7474>J_B_hi=0.5413)
真 WorkflowOptimizer 与对照 selectWorkflow+thresholds 对齐
M5 量化评估(tests/eval/variant-eval.test.ts, seeded N=200, 注入 checker+runner
+真 manager 对齐):
ΔJ_selfmod=0.15>=0.05 有益自改 67%->82%(+15pp)
forbidden 命门 5 拒/0 受 EXACT 构造铁律(§11.3 safety-gate/sandbox/flash/
glm-provider 永不自改)
§11.4 门捕有害变体 2>=1(不变式失败被拒)
回滚触发 1>=1(§11.6 可逆性实证 promoted->rolled-back)
promoted 有益 3>=1(非零自改) + 全 promoted allPassed
CI 非重叠 STRONG (J_A_lo=0.7609>J_B_hi=0.7314)
真 VariantManager 与对照 J_A/B/ΔJ 全等
结论: M4+M5 自进化机制有效且安全(量化/可信/可复现)。M1-M5 五层机制闭环
全部完成, pba 从'静态装配 agent'->'L1-L5 全栈自进化 agent'。诚实边界
(M1-M5.* 跟进): 评估为忠实 mock 策略受控实验证机制因果效应; live-LLM
读注入包真受益的端到端门 + derive* 真接线 + sqlite 持久化 + vm 沙箱 execute
变体工厂 + 真 CI §11.4 不变式执行(真 tsc/单测/launch/砖板率) 为 M1-M5.*
工程项。
验证: tsc 0 / 全量 57 文件 2875 passed 5 skipped / sk- 明文 0 / 真 pi T01
boot 过(barrel 27 扩展含 L1-L5 全栈自进化层)
多 Agent 并行: G(workflow-optimizer)+H(orchestration-bank+eval)+
I(variant-manager)+J(variant-bank+eval)
Co-Authored-By: Claude <noreply@anthropic.com>
192 lines
6.3 KiB
TypeScript
192 lines
6.3 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
||
import type { WorkflowGraph, WorkflowNode } from "../src/shared/types.js";
|
||
import { riskOfWorkflow } from "../src/shared/types.js";
|
||
import {
|
||
InMemoryWorkflowRegistry,
|
||
createWorkflowRegistry,
|
||
} from "../src/core/workflow-optimizer.js";
|
||
|
||
// ---- 测试夹具 ----
|
||
|
||
const reversibleNode: WorkflowNode = {
|
||
id: "n1",
|
||
tool: "i2cdetect",
|
||
argsTemplate: "-y 0",
|
||
irreversibility: "reversible",
|
||
expect: "0x3a",
|
||
};
|
||
const irreversibleNode: WorkflowNode = {
|
||
id: "n2",
|
||
tool: "dd",
|
||
argsTemplate: "if=img of=/dev/mmcblk0",
|
||
irreversibility: "irreversible",
|
||
};
|
||
|
||
/**
|
||
* 构造一条 WorkflowGraph(默认 active=true, hitCount=0, taskClass="i2c-probe")。
|
||
*/
|
||
function makeGraph(over: Partial<WorkflowGraph> = {}): WorkflowGraph {
|
||
const nodes = over.nodes ?? [reversibleNode];
|
||
const g: WorkflowGraph = {
|
||
id: over.id ?? "wf-1",
|
||
taskClass: over.taskClass ?? "i2c-probe",
|
||
nodes,
|
||
edges: over.edges ?? [],
|
||
risk: over.risk ?? riskOfWorkflow({ nodes }),
|
||
provenance: over.provenance ?? ["trace#1"],
|
||
createdAt: over.createdAt ?? 1000,
|
||
hitCount: over.hitCount ?? 0,
|
||
supersededBy: over.supersededBy,
|
||
active: over.active ?? true,
|
||
};
|
||
return g;
|
||
}
|
||
|
||
describe("InMemoryWorkflowRegistry.register —— disposer + override 安全", () => {
|
||
it("register 后 list 可见;disposer 调用后移除", () => {
|
||
const reg = new InMemoryWorkflowRegistry();
|
||
const g = makeGraph();
|
||
const dispose = reg.register(g);
|
||
expect(reg.size()).toBe(1);
|
||
expect(reg.list().length).toBe(1);
|
||
dispose();
|
||
expect(reg.size()).toBe(0);
|
||
expect(reg.list().length).toBe(0);
|
||
});
|
||
|
||
it("override 安全:同 id 注册 v2 后,v1 的 disposer 不误删 v2", () => {
|
||
const reg = new InMemoryWorkflowRegistry();
|
||
const g1 = makeGraph({ id: "wf-X", provenance: ["a"] });
|
||
const g2 = makeGraph({ id: "wf-X", provenance: ["b"] });
|
||
const dispose1 = reg.register(g1);
|
||
const dispose2 = reg.register(g2); // 同 id 覆盖
|
||
expect(reg.size()).toBe(1);
|
||
// v1 disposer 只在仍是同一实例时移除——现已是 v2,不应删
|
||
dispose1();
|
||
expect(reg.size()).toBe(1);
|
||
expect(reg.get("wf-X")?.provenance).toEqual(["b"]);
|
||
// v2 disposer 命中同一实例 → 移除
|
||
dispose2();
|
||
expect(reg.size()).toBe(0);
|
||
});
|
||
|
||
it("存储副本被冻结(Object.isFrozen)", () => {
|
||
const reg = new InMemoryWorkflowRegistry();
|
||
const g = makeGraph();
|
||
reg.register(g);
|
||
const stored = reg.get(g.id);
|
||
expect(stored).not.toBeNull();
|
||
expect(Object.isFrozen(stored)).toBe(true);
|
||
});
|
||
});
|
||
|
||
describe("InMemoryWorkflowRegistry.resolve —— taskClass 匹配 + hitCount 最高 + active", () => {
|
||
it("taskClass 匹配 → 返回该图(resolve 不变更 hitCount)", () => {
|
||
const reg = new InMemoryWorkflowRegistry();
|
||
const g = makeGraph({ taskClass: "i2c-probe" });
|
||
reg.register(g);
|
||
const hit = reg.resolve("i2c-probe");
|
||
expect(hit).not.toBeNull();
|
||
expect(hit?.id).toBe(g.id);
|
||
// resolve 对 hitCount 无副作用(升权由 optimizer 处理)
|
||
expect(hit?.hitCount).toBe(0);
|
||
});
|
||
|
||
it("hitCount 最高者胜(同 taskClass 多图)", () => {
|
||
const reg = new InMemoryWorkflowRegistry();
|
||
const low = makeGraph({ id: "low", taskClass: "i2c-probe", hitCount: 1 });
|
||
const high = makeGraph({ id: "high", taskClass: "i2c-probe", hitCount: 5 });
|
||
reg.register(low);
|
||
reg.register(high);
|
||
const hit = reg.resolve("i2c-probe");
|
||
expect(hit?.id).toBe("high");
|
||
});
|
||
|
||
it("hitCount 平局 → createdAt 最新者胜", () => {
|
||
const reg = new InMemoryWorkflowRegistry();
|
||
const old = makeGraph({ id: "old", taskClass: "i2c-probe", hitCount: 3, createdAt: 1000 });
|
||
const newer = makeGraph({ id: "newer", taskClass: "i2c-probe", hitCount: 3, createdAt: 2000 });
|
||
reg.register(old);
|
||
reg.register(newer);
|
||
const hit = reg.resolve("i2c-probe");
|
||
expect(hit?.id).toBe("newer");
|
||
});
|
||
|
||
it("taskClass 不匹配 → null", () => {
|
||
const reg = new InMemoryWorkflowRegistry();
|
||
reg.register(makeGraph({ taskClass: "flash" }));
|
||
expect(reg.resolve("i2c-probe")).toBeNull();
|
||
});
|
||
|
||
it("无匹配(空注册表)→ null", () => {
|
||
const reg = new InMemoryWorkflowRegistry();
|
||
expect(reg.resolve("i2c-probe")).toBeNull();
|
||
});
|
||
|
||
it("active=false(superseded)图不被 resolve 返回", () => {
|
||
const reg = new InMemoryWorkflowRegistry();
|
||
reg.register(makeGraph({ id: "dead", taskClass: "i2c-probe", active: false }));
|
||
expect(reg.resolve("i2c-probe")).toBeNull();
|
||
});
|
||
});
|
||
|
||
describe("InMemoryWorkflowRegistry.list/get/updateById/removeById/clear/size", () => {
|
||
it("list 返回只读快照(冻结数组,插入序)", () => {
|
||
const reg = new InMemoryWorkflowRegistry();
|
||
const a = makeGraph({ id: "a" });
|
||
const b = makeGraph({ id: "b", taskClass: "flash" });
|
||
reg.register(a);
|
||
reg.register(b);
|
||
const list = reg.list();
|
||
expect(list.length).toBe(2);
|
||
expect(list[0]?.id).toBe("a");
|
||
expect(list[1]?.id).toBe("b");
|
||
expect(Object.isFrozen(list)).toBe(true);
|
||
});
|
||
|
||
it("get by id 命中/未命中", () => {
|
||
const reg = new InMemoryWorkflowRegistry();
|
||
const g = makeGraph({ id: "x" });
|
||
reg.register(g);
|
||
expect(reg.get("x")?.id).toBe("x");
|
||
expect(reg.get("nope")).toBeNull();
|
||
});
|
||
|
||
it("updateById 命中 → 替换并冻结;未命中 → false", () => {
|
||
const reg = new InMemoryWorkflowRegistry();
|
||
reg.register(makeGraph({ id: "u", hitCount: 0 }));
|
||
const ok = reg.updateById("u", (g) => ({ ...g, hitCount: g.hitCount + 1 }));
|
||
expect(ok).toBe(true);
|
||
expect(reg.get("u")?.hitCount).toBe(1);
|
||
expect(Object.isFrozen(reg.get("u"))).toBe(true);
|
||
expect(reg.updateById("missing", (g) => g)).toBe(false);
|
||
});
|
||
|
||
it("removeById 命中/未命中", () => {
|
||
const reg = new InMemoryWorkflowRegistry();
|
||
reg.register(makeGraph({ id: "r" }));
|
||
expect(reg.removeById("r")).toBe(true);
|
||
expect(reg.size()).toBe(0);
|
||
expect(reg.removeById("r")).toBe(false);
|
||
});
|
||
|
||
it("clear 清空;size 反映条数", () => {
|
||
const reg = new InMemoryWorkflowRegistry();
|
||
reg.register(makeGraph({ id: "a" }));
|
||
reg.register(makeGraph({ id: "b", taskClass: "flash" }));
|
||
expect(reg.size()).toBe(2);
|
||
reg.clear();
|
||
expect(reg.size()).toBe(0);
|
||
expect(reg.list().length).toBe(0);
|
||
});
|
||
});
|
||
|
||
describe("createWorkflowRegistry 工厂", () => {
|
||
it("返回可用 InMemoryWorkflowRegistry 实例", () => {
|
||
const reg = createWorkflowRegistry();
|
||
expect(reg).toBeInstanceOf(InMemoryWorkflowRegistry);
|
||
reg.register(makeGraph({ id: "f" }));
|
||
expect(reg.size()).toBe(1);
|
||
});
|
||
});
|