Files
streamline/scripts/kb_rank_replay.ts
T
2026-11-16 17:26:44 -08:00

82 lines
4.4 KiB
TypeScript

/**
* 把新的检索排序放回历史注入上重跑一遍,回答一个问题:
*
* 同样这些任务、同样这些候选知识,新排序**少塞多少条**,又**漏掉多少条真的被采纳过的**?
*
* 实现在 `control-server/src/kb_effectiveness.ts::replayRanking` —— 与面板「效果」页
* 用的是同一段代码。命令行和面板算出两套数字,是这类度量最常见的烂尾方式。
*
* ## 为什么自己算 tier 而不是读那一列
*
* 这个脚本要能在**迁移落库之前**跑 —— 它正是用来决定那次迁移值不值得做的。所以它
* 按 `earnsSemanticTier` 的同一套规则,从证据表和引用记录里现算一遍分层,等价于
* `0019` 回填之后的状态。
*
* ## 怎么读结果
*
* - `保留的引用` 越高越好:闸门不能把起草者真的用过的东西挡在外面。
* - `注入量` 越低越好:提示词里每多一段"参考资料",模型对铁律与 NEEDS_INPUT 哨兵的
* 遵守度就掉一点 —— 这个 tradeoff 是真实的,不是洁癖。
* - 排序那一节是**前序式**打分(先验只看更早的注入),所以「有用度」不会自证。
*
* 只读。用法:bun scripts/kb_rank_replay.ts
*/
import { db } from "../control-server/src/db_client";
import { kb_injections, kb_claims, kb_evidence } from "../control-server/src/schema";
import { replayRanking } from "../control-server/src/kb_effectiveness";
import { earnsSemanticTier } from "../control-server/src/kb_db";
import { sql } from "drizzle-orm";
const idList = (raw: string | null): string[] => {
if (!raw) return [];
try {
const v = JSON.parse(raw) as unknown;
return Array.isArray(v) ? v.filter((x): x is string => typeof x === "string") : [];
} catch { return []; }
};
async function main() {
// Rebuild what 0019 would have written, from the same sources it reads.
const witnesses = new Map<string, number>();
for (const r of await db.select({
claimId: kb_evidence.claimId,
n: sql<number>`count(distinct ${kb_evidence.targetId})::int`,
}).from(kb_evidence).groupBy(kb_evidence.claimId)) witnesses.set(r.claimId, r.n);
const cited = new Map<string, number>();
for (const inj of await db.select({ usedClaimIds: kb_injections.usedClaimIds }).from(kb_injections))
for (const id of idList(inj.usedClaimIds)) cited.set(id, (cited.get(id) ?? 0) + 1);
const tierOf = (c: { id: string; kind: string; source: string }) =>
earnsSemanticTier({
kind: c.kind, source: c.source as "distilled" | "human",
witnessedTargets: witnesses.get(c.id) ?? 0, citedCount: cited.get(c.id) ?? 0,
}) ? "semantic" : "episodic";
const r = await replayRanking({ tierOf });
const pct = (a: number, b: number) => `${(a / Math.max(1, b) * 100).toFixed(0)}%`;
console.log(`\n样本:${r.injections} 次有回报的注入,${r.slots} 个条目位,其中被引用 ${r.cited} 个 (${pct(r.cited, r.slots)})\n`);
console.log("① 分层闸(tier)");
console.log(` 判为 episodic、今后不再注入:${r.tierDropped}/${r.slots} (${pct(r.tierDropped, r.slots)})`);
console.log(` 其中曾被引用过的:${r.tierDroppedCited} ← 设计上应为 0(引用过即晋升 semantic)\n`);
console.log("② 端到端(分层 + 相关性 + 分类配额)");
console.log(` 注入量:${r.slots} → ${r.keptSlots} 个条目位 (${pct(r.keptSlots, r.slots)}),`
+ ` 每次注入 ${(r.slots / Math.max(1, r.injections)).toFixed(1)} → ${(r.keptSlots / Math.max(1, r.injections)).toFixed(1)} 条`);
console.log(` 被引用过的知识:${r.keptCited}/${r.cited} 仍在注入里 (${pct(r.keptCited, r.cited)})`);
console.log(" 注:重放只能在「当时检索出来的候选」里筛;生产上常驻知识不再要求命中工单词,");
console.log(" 候选集只会更大,所以这一行是下限。\n");
console.log("③ 排序质量(同一批候选,只换排序;有用度先验前序计算)");
console.log(" 老排序(置信度) 新排序(常驻优先)");
console.log(` 被引用知识平均名次 ${r.meanRankOld.toFixed(2)}`.padEnd(45) + r.meanRankNew.toFixed(2));
console.log(` MRR ${r.mrrOld.toFixed(3)}`.padEnd(45) + r.mrrNew.toFixed(3));
console.log(` 前 3 条内命中 ${r.top3Old}/${r.cited} (${pct(r.top3Old, r.cited)})`.padEnd(45)
+ `${r.top3New}/${r.cited} (${pct(r.top3New, r.cited)})`);
console.log("");
process.exit(0);
}
void main();