tianxuan / src /station /executor.ts
masterofthestars's picture
Tianxuan: full project + data (checkpoints, world_model, data without raw) with correct directory structure
db7bc06
Raw History Blame Contribute Delete
12.4 kB
import { AuditLog } from "./audit";
import { Blackboard } from "./blackboard";
import { HealthMonitor } from "./health";
import { OnDemandEconomics } from "./economics";
import { CollaborationProbe } from "./probe";
import { Registry } from "./registry";
import { Task, TaskResult, ForwardState } from "./types";
export interface ExecutorOptions {
/** 置信度低于该值,在预算内继续扩深 */
cascadeThreshold?: number;
/** 中央神经点火低于该值(且样本数≥warmup) → 预算转向跳过该深度 */
coldThreshold?: number;
/** 点火评估暖机样本数 */
warmup?: number;
/** 最大执行深度 (默认4) */
maxDepth?: number;
/** 是否启用审计 (默认true) */
enableAudit?: boolean;
/** 是否启用探针 (默认true) */
enableProbe?: boolean;
}
/** 默认配置 */
export const DEFAULT_EXECUTOR_OPTIONS: ExecutorOptions = {
cascadeThreshold: 0.6,
coldThreshold: 0.15,
warmup: 8,
maxDepth: 4,
enableAudit: true,
enableProbe: true,
};
/**
* MoD(Mixture-of-Depths)式执行器
* 共享同一模型: 按任务复杂度决定初始深度,置信不足则在预算内扩展深度
* 不做"小网/大网"两套网络,路径裁剪替代网络复制(依据 DeepMind MoD 论文)
*/
export class Executor {
private readonly cascadeThreshold: number;
private readonly coldThreshold: number;
private readonly warmup: number;
private readonly maxDepth: number;
private readonly enableAudit: boolean;
private readonly enableProbe: boolean;
/** 健康监控 (宪章第4条): 心跳/NaN 降级, 默认自建 */
readonly health: HealthMonitor;
/** 按需调用经济性探针 (宪章第8条): 真实调用成本/置信度增量记录 */
readonly economics: OnDemandEconomics;
constructor(
private readonly registry: Registry,
private readonly blackboard: Blackboard,
private readonly audit: AuditLog,
private readonly probe: CollaborationProbe,
options: ExecutorOptions = {},
health?: HealthMonitor,
economics?: OnDemandEconomics,
) {
this.cascadeThreshold = options.cascadeThreshold ?? DEFAULT_EXECUTOR_OPTIONS.cascadeThreshold!;
this.coldThreshold = options.coldThreshold ?? DEFAULT_EXECUTOR_OPTIONS.coldThreshold!;
this.warmup = options.warmup ?? DEFAULT_EXECUTOR_OPTIONS.warmup!;
this.maxDepth = options.maxDepth ?? DEFAULT_EXECUTOR_OPTIONS.maxDepth!;
this.enableAudit = options.enableAudit ?? DEFAULT_EXECUTOR_OPTIONS.enableAudit!;
this.enableProbe = options.enableProbe ?? DEFAULT_EXECUTOR_OPTIONS.enableProbe!;
this.health = health ?? new HealthMonitor(registry, audit);
this.economics = economics ?? new OnDemandEconomics();
}
async execute(task: Task): Promise<TaskResult> {
const state = this.blackboard.worldState();
const fullCost = this.registry.computeUnits(task.type).reduce((s, u) => s + u.cost, 0);
// L0 — 记忆直出
// [P0-B 修复 2026-10-08 第一性审核] 命中键绑定「上下文指纹」而非任务序号 —
// 旧键 `mem:${task.id}` 在 generate() 入口从 m0 重编号, 黑板不清空 → 服务器常驻进程
// 必然用上一请求残留键制造假命中 (审核实测 m1/m2 本轮零召回却报 L0 命中)。
// 新键 `mem:<ctxFp>` 仅当「相同上下文」时命中, 不同上下文的残留键天然失效。
// ctxFp 由 engine.generate 在 task.id 中编码 (`${i}:${ctxFp}`), executor 据此读键。
const memKey = `mem:${task.id.includes(":") ? task.id.split(":").slice(1).join(":") : task.id}`;
const hit = this.blackboard.read(memKey);
if (this.registry.get("titans")?.enabled && hit !== undefined) {
// 2026-10-08 第一性审核: L0 置信度不再写死 1.0 — 由命中来源写入的点火强度推导
// (z:l0:act: titans=命中槽占比, vector/repeat 缓存=深链曾以≥0.3置信出过该字的 0.5 标记,
// engine 在命中时写入; 无写入 → 0, 冷启动不伪造满置信, 宪章第 1/7 条)
const ignition = Number(this.blackboard.read("z:l0:act") ?? 0);
const conf = Math.min(1, ignition * 2); // 点火 0.5+ → 满置信, 线性归一
this.audit.route({ taskId: task.id, depth: 0, units: [], confidence: conf, memoryHit: true });
this.economics.recordTask({
taskId: task.id, complexity: task.complexity, budget: task.budget,
actualCost: 0, fullCost, finalConf: conf, unitsRun: 0, forwards: 0, realForwards: 0, earlyStopped: false, memoryHit: true,
});
return { taskId: task.id, depth: 0, output: hit, confidence: conf, units: [], memoryHit: true };
}
const units = this.registry.computeUnits(task.type);
if (units.length === 0) {
this.audit.route({ taskId: task.id, depth: 0, units: [], confidence: 0, memoryHit: false });
this.economics.recordTask({
taskId: task.id, complexity: task.complexity, budget: task.budget,
actualCost: 0, fullCost: 0, finalConf: 0, unitsRun: 0, forwards: 0, realForwards: 0, earlyStopped: false, memoryHit: false,
});
return { taskId: task.id, depth: 0, output: undefined, confidence: 0, units: [], memoryHit: false };
}
const maxDepth = this.maxDepth;
const budget = Math.max(1, Math.min(task.budget || maxDepth, maxDepth));
let targetDepth = Math.min(maxDepth, Math.max(1, Math.round(task.complexity * maxDepth)));
const ranUnits: string[] = [];
let lastConfidence = 0;
let taskCost = 0; // 本任务实际执行单元成本之和 (按需经济性)
let taskForwards = 0; // [P1-I] 单元前向次数 (每单元 forward 含 1 次骨干前向, titans:depth0=0)
let taskRealForwards = 0; // [R1-B1] 真实 model.step = 单元前向 + 推进 advance + L0 探针 deepTop1At
const finishTask = (earlyStopped: boolean): TaskResult => {
const res = this.result(task, ranUnits, units, lastConfidence, state);
// [R1-B1] 真实 model.step = 单元前向 + 推进 advance (L0 命中步与非 L0 步都推进 1 次)
// + L0 一致性探针 deepTop1At (engine 侧当 L0 召回且探针开时 +1; executor 侧按
// memoryHit + blackboard mem 键存在近似)。旧实现 realForwards 恒 0 (未接) → 节省率失真。
const advanceCount = 1; // 每个任务收尾前必推进真实状态 1 次 (L0 命中步由 engine P0-A 分支推进)
const probeCount = res.memoryHit ? 1 : 0; // L0 命中步 engine 跑 deepTop1At 记探针 (非命中不跑)
taskRealForwards = taskForwards + advanceCount + probeCount;
this.economics.recordTask({
taskId: task.id, complexity: task.complexity, budget,
actualCost: taskCost, fullCost, finalConf: lastConfidence,
unitsRun: ranUnits.length, forwards: taskForwards, realForwards: taskRealForwards, earlyStopped, memoryHit: res.memoryHit,
});
return res;
};
for (let used = 0; used < budget && targetDepth <= maxDepth; used++) {
let next = units.find((unit) => {
if (unit.depth > targetDepth || ranUnits.includes(unit.unitId)) return false;
// [P2 修复 2026-10-13 第一性审核] 健康监控按 unitId 键控 — 旧实现按 kind 键控,
// 多单元共用 kind (gru:shallow + GRU L2) 时 NaN 计数被混算稀释。现: 精确到单元。
return !this.health.shouldSkip(unit.unitId);
});
if (!next) {
targetDepth += 1; // 该深度无单元: 预算内扩一层
used -= 1;
continue;
}
// [P1-K 修复 2026-10-08 第一性审核] sys/confBefore 在 forward 之前声明 (冷中央判定用),
// forward 之后重读当次点火值 (z:act 由单元 forward 写入, 旧实现在 forward 之前读
// 记到上一任务陈旧值/占位 0 → 首样本恒 0, 审核 P1-J/P1-K)。
const unitKind = this.registry.unitSystem(next.unitId)?.kind;
const sys = unitKind ? this.registry.get(unitKind) : undefined;
const confBefore = lastConfidence;
// 预算转向(根据多神经实际点火调整): 中央神经长期不点火 → 该深度让路, 预算留给他神经
if (sys?.roleTag === "central" && this.enableProbe && this.probe.sampleCount(next.unitId) >= this.warmup && this.probe.ignitionAvg(next.unitId, 32) < this.coldThreshold) {
if (this.enableAudit) {
this.audit.route({
taskId: task.id,
depth: next.depth,
units: [...ranUnits, next.unitId],
confidence: 0,
memoryHit: false,
note: "cold-central",
});
}
targetDepth = Math.min(maxDepth, next.depth + 1); // 让路: 跳到该深度之后
continue;
}
// 点火探针: 先 forward, 再读当次 z:act (前向写入的点火值), 保证探针记录的是本任务真实值
// [P0-A 修复 2026-10-08 第一性审核] ranUnits.push 旧实现完全缺失 — 已执行单元从不记录
// → result() 的 depth 恒 0 / units 恒空 / economics.unitsRun 恒 0 (审计记"零算力"而真实
// 跑 N 次前向, 审核 P0-A/P1-I 根因)。
// [P1-I] 每个非 L0 单元 forward 含 1 次完整骨干前向 (peekLogits)。
// titans:depth0 cost=0 但本处是「单元 forward 是否跑了深链」计数, L0 直出步
// 根本进不到单元循环 (早停返回), 故只计实际执行的单元。
taskForwards += next.cost === 0 ? 0 : 1; // 零成本单元 (titans:depth0) 不计前向
ranUnits.push(next.unitId);
const confidence = await next.forward(task, state);
lastConfidence = confidence;
// [P1-K] forward 后读取点火值 (z:act:<unitId> 由单元 forward 写入, 当次值非陈旧)
const ignition = Number(this.blackboard.read(`z:act:${next.unitId}`) ?? confBefore);
if (this.enableProbe) {
this.probe.record(task.id, next.unitId, sys?.kind ?? "?", ignition);
}
// 按需经济性: 本次调用的真实成本 + 置信度增量 (宪章第8条: 按需调用可量化)
taskCost += next.cost;
this.economics.recordUnit(task.id, next.unitId, next.cost, confBefore, confidence);
// 健康监控: 单元心跳 + NaN 降级 (宪章第4条); 非有限置信度 → 不刷新 (不降级假健康, P0-F)
// [P2 修复 2026-10-13 第一性审核] 按 unitId 刷新 — 旧按 kind, 多单元混算
if (Number.isFinite(confidence)) this.health.refresh(next.unitId, confidence);
if (this.enableAudit) {
this.audit.route({
taskId: task.id,
depth: next.depth,
units: [...ranUnits],
confidence: Number.isFinite(confidence) ? confidence : 0,
memoryHit: false,
note: Number.isFinite(confidence) ? undefined : "nan-output",
});
}
// [P0-F] 非有限置信度 → 不触发提前停止, 也不扩深 (本单元结果不可信, 交给下一层兜底)
if (Number.isFinite(confidence) && confidence >= this.cascadeThreshold) {
return finishTask(true); // 置信达标: 未耗完预算即停 (按需直接证据)
}
targetDepth = Math.min(maxDepth, next.depth + 1); // 置信不足/非有限 → 预算内扩到下一层
}
return finishTask(false);
}
/**
* [P0-A 修复 2026-10-08 第一性审核] result() 须返回「实际执行到的最大深度」
* (宪章第2条 MoD 式分级: depth=1/2/3/4 由路由实际推进, 非写死 0)。
* 旧实现 ranUnits 为空时恒 depth=0 → 所有深链任务审计记 depth=0/零算力,
* 而真实输出是 4 次完整前向 (审核 P0-A: "L0 直出在真实路径上产生输出字,
* 但审计记 depth=0/units=[]" 的同一根因)。现: 用实际推进的 targetDepth。
*/
private result(task: Task, ranUnits: string[], allUnits: { depth: number; unitId: string }[], confidence: number, state: ForwardState): TaskResult {
// 实际执行到的最大深度: ranUnits 非空 → 已执行单元的最大 depth;
// 空 (零单元, 全早停/预算 0) → 0 (真实零算力, 审计记 0 正确)
const executedDepth = ranUnits.length === 0 ? 0 : Math.max(...allUnits.filter((u) => ranUnits.includes(u.unitId)).map((u) => u.depth));
return {
taskId: task.id,
depth: executedDepth,
output: this.blackboard.read(`o:${task.id}`) ?? null,
confidence,
units: ranUnits,
memoryHit: false,
};
}
}