focus-guard 3.5.2 → 3.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -3
- package/README.zh-CN.md +14 -4
- package/assets/NOTICE.md +3 -1
- package/docs/ARCH-MAP.md +302 -74
- package/docs/BENCH.md +28 -13
- package/docs/RELEASE-POLICY.md +74 -17
- package/docs/RULES.md +98 -60
- package/docs/THREAT-MODEL.md +3 -3
- package/docs/WRITING-STYLE.md +40 -2
- package/package.json +11 -5
- package/skills/focus-plan/SKILL.md +77 -0
- package/skills/focus-rules/SKILL.md +267 -0
- package/skills/focus-thinking/SKILL.md +8 -255
- package/src/adapters/dsh/eligibility-gate.mjs +75 -35
- package/src/adapters/dsh/session.mjs +22 -0
- package/src/{dsh → adapters/dsh}/toolName.mjs +6 -0
- package/src/core/approvals.mjs +10 -4
- package/src/core/audit-writer.mjs +5 -2
- package/src/core/auditActions.mjs +214 -0
- package/src/core/checkEligibility.mjs +193 -130
- package/src/core/constants.mjs +8 -4
- package/src/core/decisionEngine.mjs +16 -13
- package/src/core/env.mjs +37 -2
- package/src/core/patterns.mjs +3 -1
- package/src/core/prefixStability.mjs +27 -3
- package/src/core/profileLoader.mjs +96 -3
- package/src/core/scratch.mjs +288 -0
- package/src/core/session.mjs +23 -0
- package/src/core/state.mjs +3 -3
- package/src/dsh/audit.mjs +45 -170
- package/src/dsh/bridgeCheck.mjs +81 -0
- package/src/dsh/compactionWatch/announce.mjs +2 -1
- package/src/dsh/compactionWatch.mjs +2 -1
- package/src/dsh/cost-ledger.mjs +174 -0
- package/src/dsh/guard/authz.mjs +3 -3
- package/src/dsh/guard/evidence.mjs +19 -0
- package/src/dsh/guard/retryGuard.mjs +96 -0
- package/src/dsh/guard/risk.mjs +61 -16
- package/src/dsh/guard/tooling.mjs +20 -13
- package/src/dsh/{value-gate.mjs → guard/value-gate.mjs} +6 -0
- package/src/dsh/guard/writing-gate.mjs +117 -0
- package/src/dsh/index.js +4 -7
- package/src/dsh/pipeline.mjs +72 -8
- package/src/dsh/postProgress.mjs +81 -40
- package/src/dsh/preGuard.mjs +12 -3
- package/src/dsh/repeatGuard.mjs +5 -4
- package/src/dsh/seams/pre-step.mjs +74 -23
- package/src/dsh/seams/prompt-rules.mjs +91 -5
- package/src/dsh/{guard → seams}/runtime.mjs +41 -10
- package/src/dsh/seams/session-start.mjs +33 -10
- package/src/dsh/seams/shared.mjs +8 -5
- package/src/dsh/search-hints.mjs +15 -1
- package/src/dsh/sessionScope.mjs +11 -11
- package/src/dsh/stopGuard.mjs +2 -7
- package/src/dsh/tools.mjs +39 -1
- package/src/dsh/traceMeta.mjs +5 -11
- package/src/memory/index.mjs +7 -5
- package/src/memory/workspace.mjs +41 -19
- package/src/profiles/deepseek-flash.mjs +10 -8
- package/src/profiles/deepseek-pro.mjs +9 -8
- package/src/profiles/default.mjs +14 -8
- package/src/profiles/glm.mjs +9 -8
- package/src/profiles/gpt-astra.mjs +11 -9
- package/tools/audit-chain.mjs +29 -4
- package/tools/collect-panel.mjs +3 -24
- package/tools/dead-export.mjs +2 -17
- package/tools/docs-book.mjs +234 -0
- package/tools/docs-drift.mjs +143 -0
- package/tools/docs-lint.mjs +76 -9
- package/tools/docs-refs.mjs +343 -0
- package/tools/gate-audit.mjs +3 -24
- package/tools/handoff.mjs +4 -2
- package/tools/index-stale.mjs +124 -0
- package/tools/install-check.mjs +107 -9
- package/tools/knowledge.mjs +53 -4
- package/tools/layering-audit.mjs +7 -1
- package/tools/legacy-audit.mjs +83 -0
- package/tools/lib/book-style.mjs +42 -1
- package/tools/lib/case-tool.mjs +9 -3
- package/tools/lib/cli.mjs +35 -0
- package/tools/lib/docs-ack.mjs +97 -0
- package/tools/lib/docs-core.mjs +84 -45
- package/tools/lib/docs-files.mjs +73 -0
- package/tools/lib/docs-scope.mjs +46 -0
- package/tools/lib/docs-status.mjs +148 -0
- package/tools/lib/docs-typography.mjs +91 -0
- package/tools/lib/docs-version.mjs +8 -5
- package/tools/lib/dup-block.mjs +117 -0
- package/tools/lib/idioms.mjs +149 -0
- package/tools/lib/import-graph.mjs +8 -3
- package/tools/lib/index-io.mjs +4 -4
- package/tools/lib/index-registry.mjs +101 -0
- package/tools/lib/legacy-core.mjs +178 -0
- package/tools/lib/markdown.mjs +202 -0
- package/tools/lib/modules-doc.mjs +103 -0
- package/tools/lib/modules-list.mjs +396 -0
- package/tools/lib/modules.mjs +360 -0
- package/tools/lib/mutation-ops.mjs +63 -0
- package/tools/lib/parse-block.mjs +58 -0
- package/tools/lib/prefix-cost.mjs +5 -3
- package/tools/lib/ratchet.mjs +22 -8
- package/tools/lib/release-faces.mjs +144 -0
- package/tools/lib/repo-copy.mjs +34 -0
- package/tools/lib/survey-report.mjs +57 -0
- package/tools/lib/where.mjs +139 -0
- package/tools/library-book.mjs +7 -92
- package/tools/library-build.mjs +15 -3
- package/tools/lint.mjs +24 -13
- package/tools/log-audit.mjs +76 -0
- package/tools/memory-audit.mjs +32 -6
- package/tools/memory-distill.mjs +14 -1
- package/tools/memory-recall-eval.mjs +408 -0
- package/tools/module-audit.mjs +2 -17
- package/tools/modules-audit.mjs +110 -0
- package/tools/mutate.mjs +120 -74
- package/tools/panel-baseline.mjs +172 -0
- package/tools/prefix-audit.mjs +10 -2
- package/tools/prefix-drift.mjs +10 -5
- package/tools/registry-check.mjs +200 -0
- package/tools/release.mjs +322 -0
- package/tools/smells.mjs +97 -139
- package/tools/version.mjs +12 -8
- package/docs/AB-PROMPTS.md +0 -123
- package/docs/AB-SESSION.md +0 -161
- package/docs/BENCH-BASELINE.json +0 -95
- package/docs/BRAIN-VIZ.md +0 -94
- package/docs/GATE-INPUTS.json +0 -10
- package/docs/HANDOFF-2026-10-27.md +0 -736
- package/docs/HOOK-ORDERING.md +0 -139
- package/docs/LEGISLATION-LAW.md +0 -104
- package/docs/MCP-4.0-PLAN.md +0 -184
- package/docs/MEMORY-4.5-PLAN.md +0 -415
- package/docs/PROFILE-TUNING.md +0 -147
- package/docs/RATCHET.json +0 -80
- package/docs/TOOL-SCHEMA.json +0 -6
- package/docs/VERSION-PLAN.md +0 -604
- package/docs/architecture-map.mermaid +0 -186
- package/docs/deepseek-pricing-audit.md +0 -65
- package/src/core/staleCleanup.mjs +0 -224
- package/src/dsh/guard/superpowers.mjs +0 -70
- package/src/dsh/workspace.mjs +0 -5
- package/tools/library-index.mjs +0 -91
package/tools/log-audit.mjs
CHANGED
|
@@ -106,3 +106,79 @@ console.log(` 因果链:${ok.filter((r) => r.chain).length} 行带 chain(
|
|
|
106
106
|
if (Object.keys(shapes).length > 1) {
|
|
107
107
|
console.log(' ↑ 超过 1 种说明写入点还没收敛到同一个构造点(buildAuditRow)')
|
|
108
108
|
}
|
|
109
|
+
|
|
110
|
+
// ── 拦截闭环(2026-11-28)──────────────────────────────────────────────────
|
|
111
|
+
// 这一节只回答「拦截有没有被消费」,**不回答「拦截值不值」**。
|
|
112
|
+
//
|
|
113
|
+
// 能算的只有成本下界:同一 (会话, 工具, 命令) 被拒 ≥2 次 = 原样重试 = 纯成本
|
|
114
|
+
//(成功重试不会产生第二条 deny,所以重复 deny 一定是白拦)。
|
|
115
|
+
// 收益("这次拦对了")需要人的裁决,而日志里 appeal / grant / approval 一直 0 条——
|
|
116
|
+
// 通道都建好了(fg_apply / fg_appeal / approvals 目录),就是没人走。
|
|
117
|
+
//
|
|
118
|
+
// 别把这一节读成"拦截有效"。它只说明「浪费了几次」,说不出「救了几次」。
|
|
119
|
+
const BLOCK_RE = /^(deny|platform-deny|deny-|violation-|reject-|inv-pool-exceeded|ctx-pollution|library-write-deny)/
|
|
120
|
+
const CLOSURE_RE = /^(approval-|grants-|grant$|appeal|fg-apply|apply-)/
|
|
121
|
+
// 「拦了本来也会失败」:命令语义层面就注定报错,拦与不拦结果相同。
|
|
122
|
+
// 这类重复是纯成本,且顺带证明「拦」没有学习效果——同一条命令能被拦 9 次。
|
|
123
|
+
const FUTILE_TRIGGER_RE = /platform-deny/
|
|
124
|
+
|
|
125
|
+
const blocks = ok.filter((r) => BLOCK_RE.test(r.action || ''))
|
|
126
|
+
const closures = ok.filter((r) => CLOSURE_RE.test(r.action || ''))
|
|
127
|
+
|
|
128
|
+
/** 按 key 分组,只留出现 ≥2 次的组。 */
|
|
129
|
+
function groupsOf(rows, keyOf) {
|
|
130
|
+
const m = new Map()
|
|
131
|
+
for (const r of rows) {
|
|
132
|
+
const k = keyOf(r)
|
|
133
|
+
if (!m.has(k)) m.set(k, [])
|
|
134
|
+
m.get(k).push(r)
|
|
135
|
+
}
|
|
136
|
+
return [...m.entries()].filter(([, g]) => g.length > 1)
|
|
137
|
+
}
|
|
138
|
+
const extraOf = (groups) => groups.reduce((a, [, g]) => a + (g.length - 1), 0)
|
|
139
|
+
|
|
140
|
+
// 口径一:字段指纹(2026-11-28 起的新行才有 tool/command)。
|
|
141
|
+
const withPrint = blocks.filter((r) => r.tool && r.command !== undefined)
|
|
142
|
+
const byField = groupsOf(withPrint, (r) => `${r.session}|${r.tool}|${r.command}`)
|
|
143
|
+
|
|
144
|
+
// 口径二:文本指纹,回溯历史。trigger + evidence 前 80 字。
|
|
145
|
+
// 不解析命令是刻意的:两行 evidence 逐字相同就意味着同一件事,不会把一件事拆成两组。
|
|
146
|
+
// 代价是「同一理由、不同命令」会被并成一组,所以它只作**下界**,不当精确值。
|
|
147
|
+
const byText = groupsOf(blocks, (r) => `${r.trigger || ''}|${String(r.evidence || '').slice(0, 80)}`)
|
|
148
|
+
const futile = byText.filter(([, g]) => FUTILE_TRIGGER_RE.test(g[0].trigger || g[0].action || ''))
|
|
149
|
+
|
|
150
|
+
console.log('\n== 拦截闭环 ==')
|
|
151
|
+
console.log(
|
|
152
|
+
` 拦截/记档 ${blocks.length} 行,占全部 ${((blocks.length / Math.max(ok.length, 1)) * 100).toFixed(1)}%` +
|
|
153
|
+
`(其余是自查/自报/自记录——日志本身也是元工作)`,
|
|
154
|
+
)
|
|
155
|
+
console.log(
|
|
156
|
+
` 带 tool+command 指纹 ${withPrint.length}/${blocks.length} 行` +
|
|
157
|
+
(withPrint.length < blocks.length ? '(2026-11-28 之前的行没有指纹,字段口径从本轮起才准)' : ''),
|
|
158
|
+
)
|
|
159
|
+
if (byField.length) {
|
|
160
|
+
console.log(` 字段口径重复 ${byField.length} 组,多拦 ${extraOf(byField)} 次`)
|
|
161
|
+
}
|
|
162
|
+
console.log(' ── 回溯口径(trigger + evidence 前 80 字,历史行也算得出)──')
|
|
163
|
+
if (!byText.length) {
|
|
164
|
+
console.log(' 同文本指纹重复:0 组(无数据时不要读成"零浪费")')
|
|
165
|
+
} else {
|
|
166
|
+
console.log(
|
|
167
|
+
` 同文本指纹重复 ${byText.length} 组,多拦 ${extraOf(byText)} 次 ← **纯成本下界**(成功重试不会留下第二条 deny)`,
|
|
168
|
+
)
|
|
169
|
+
if (futile.length) {
|
|
170
|
+
console.log(
|
|
171
|
+
` 其中「拦了本来也会失败」${futile.length} 组、多拦 ${extraOf(futile)} 次` +
|
|
172
|
+
' ← 这类应改成「教一次」,拦没有学习效果',
|
|
173
|
+
)
|
|
174
|
+
for (const [, g] of futile.slice(0, 2)) {
|
|
175
|
+
console.log(` ×${g.length} ${String(g[0].evidence || '').slice(0, 72)}`)
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
const useful = extraOf(byText) - extraOf(futile)
|
|
179
|
+
console.log(` 其余 ${useful} 次 ← 可能是必要成本(真防住了),但也说明模型健忘`)
|
|
180
|
+
}
|
|
181
|
+
console.log(
|
|
182
|
+
` 闭环行(申请/批准/申诉)${closures.length} 条` +
|
|
183
|
+
(closures.length === 0 ? ' ← 通道建好了没人走,收益侧仍然无数据' : ''),
|
|
184
|
+
)
|
package/tools/memory-audit.mjs
CHANGED
|
@@ -5,10 +5,18 @@
|
|
|
5
5
|
* 收益:M1 验收要求一条命令说清「记忆存在哪、多大、索引有没有」。
|
|
6
6
|
* 改动面:本文件 + package.json 一行 script,不跨层。
|
|
7
7
|
* 替代:无更小做法——验收命令需要一个真实入口。
|
|
8
|
+
*
|
|
9
|
+
* 状态口径(2026-11-26 收紧):`OK` 只说明**文件在**,不说明记忆健康。
|
|
10
|
+
* 卷宗里若还有下架前的无标签 `decisions`(自动落账已于 2026-11-26 下架,见
|
|
11
|
+
* `src/memory/workspace.mjs` 头部),标 `STALE` 并写明条数——那些是指令原文,不是结论。
|
|
12
|
+
* 早期版本把它们算进「条目 N」并盖 OK,等于让体检报告替半成品背书。
|
|
13
|
+
*
|
|
14
|
+
* 退出码:0 = 全 OK;1 = 有载体缺失;2 = 文件都在但有存量待清理(不是坏了,是脏)。
|
|
8
15
|
*/
|
|
9
16
|
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'
|
|
10
17
|
import { dirname, join, resolve } from 'node:path'
|
|
11
18
|
import { fileURLToPath } from 'node:url'
|
|
19
|
+
import { parseTag } from '../src/memory/tags.mjs'
|
|
12
20
|
|
|
13
21
|
const HERE = dirname(fileURLToPath(import.meta.url))
|
|
14
22
|
const REPO = resolve(HERE, '..', '..', '..')
|
|
@@ -21,8 +29,17 @@ const carriers = [
|
|
|
21
29
|
describe: (p) => {
|
|
22
30
|
const raw = readFileSync(p, 'utf8')
|
|
23
31
|
const j = JSON.parse(raw)
|
|
24
|
-
const
|
|
25
|
-
|
|
32
|
+
const decisions = (j.decisions || []).map((x) => String(x ?? '').trim()).filter(Boolean)
|
|
33
|
+
const conclusions = decisions.filter((x) => parseTag(x)).length
|
|
34
|
+
const legacy = decisions.length - conclusions
|
|
35
|
+
const constraints = (j.constraints || []).length
|
|
36
|
+
// 旧版读的是 `j.next`(字段名不存在,恒算 0),实际键是 `next_action`。
|
|
37
|
+
const next = (j.next_action || []).length
|
|
38
|
+
const detail =
|
|
39
|
+
`${raw.length} 字节 · workspace_hash=${j.workspace_hash || '—'} · ` +
|
|
40
|
+
`带标签结论 ${conclusions} · 被否命令 ${constraints} · 下一步 ${next}` +
|
|
41
|
+
(legacy ? ` · 存量指令原文 ${legacy}(待清理,不注入)` : '')
|
|
42
|
+
return { status: legacy ? 'STALE' : 'OK', detail }
|
|
26
43
|
},
|
|
27
44
|
},
|
|
28
45
|
{
|
|
@@ -32,7 +49,7 @@ const carriers = [
|
|
|
32
49
|
describe: (p) => {
|
|
33
50
|
const raw = readFileSync(p, 'utf8')
|
|
34
51
|
const sections = (raw.match(/^#{1,6}\s*【[一二三四五六七八九十]+】/gm) || []).length
|
|
35
|
-
return `${raw.length} 字节 · 章节 ${sections}`
|
|
52
|
+
return { status: 'OK', detail: `${raw.length} 字节 · 章节 ${sections}` }
|
|
36
53
|
},
|
|
37
54
|
},
|
|
38
55
|
{
|
|
@@ -49,12 +66,13 @@ const carriers = [
|
|
|
49
66
|
try { archive += readdirSync(join(archiveRoot, d)).filter((f) => f.endsWith('.md')).length } catch {}
|
|
50
67
|
}
|
|
51
68
|
}
|
|
52
|
-
return `现行 ${live} 份 · 归档 ${archive} 份 · 索引 ${existsSync(idx) ? '有' : '无'}`
|
|
69
|
+
return { status: 'OK', detail: `现行 ${live} 份 · 归档 ${archive} 份 · 索引 ${existsSync(idx) ? '有' : '无'}` }
|
|
53
70
|
},
|
|
54
71
|
},
|
|
55
72
|
]
|
|
56
73
|
|
|
57
74
|
let bad = 0
|
|
75
|
+
let stale = 0
|
|
58
76
|
for (const c of carriers) {
|
|
59
77
|
if (!existsSync(c.path)) {
|
|
60
78
|
process.stdout.write(`${c.key}\t${c.label}\tMISSING\t${c.path}\n`)
|
|
@@ -62,6 +80,14 @@ for (const c of carriers) {
|
|
|
62
80
|
continue
|
|
63
81
|
}
|
|
64
82
|
statSync(c.path)
|
|
65
|
-
|
|
83
|
+
const { status, detail } = c.describe(c.path)
|
|
84
|
+
if (status === 'STALE') stale++
|
|
85
|
+
process.stdout.write(`${c.key}\t${c.label}\t${status}\t${detail}\n`)
|
|
86
|
+
}
|
|
87
|
+
if (stale) {
|
|
88
|
+
process.stdout.write(
|
|
89
|
+
`\nSTALE ≠ 文件坏了:卷宗里还留着下架前的指令原文。` +
|
|
90
|
+
`它们既不注入也不进评测语料;清掉用 \`fg case rm <序号>\` 逐条删,或删 \`.focus-guard/workspace.json\` 让下次落账重建。\n`,
|
|
91
|
+
)
|
|
66
92
|
}
|
|
67
|
-
process.exitCode = bad ? 1 : 0
|
|
93
|
+
process.exitCode = bad ? 1 : stale ? 2 : 0
|
package/tools/memory-distill.mjs
CHANGED
|
@@ -50,7 +50,20 @@ const FILE_OF_REGION = {
|
|
|
50
50
|
left: { file: "lessons.md", kind: "lesson", region: "left" },
|
|
51
51
|
};
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
/* 只沉淀 `tool`,**不沉淀 `action`**(2026-11-28 改)。
|
|
54
|
+
*
|
|
55
|
+
* 原先是 `Set(["tool", "action"])`。判据错在把"审计里出现得多"当成"人在反复做":
|
|
56
|
+
* 审计的 `action` **全部由 FG 自己写**——每回合重置、每步残留核验、KPI 结算、拦截、
|
|
57
|
+
* 心跳。把它们沉淀成「方法」,等于让系统把自己的心跳当成本事。
|
|
58
|
+
*
|
|
59
|
+
* 实测代价(`self-improvement/memory/methods.md`,10 行里 8 行是这种条目):
|
|
60
|
+
* 「反复做 residue-check 7356 次」「random-audit 485 次」「rules-registered 230 次」——
|
|
61
|
+
* 没有一条是人的程序性知识。而这 7356 里绝大多数是**每 step 重复记账**的假象
|
|
62
|
+
* (同批已改为回合级留痕,真实回合数约 63)——假日志喂假记忆,记忆再指导判断。
|
|
63
|
+
*
|
|
64
|
+
* 为什么不写"排除名单":action 名会随版本增删,名单一定会漂;而"action 由 FG 自己写"
|
|
65
|
+
* 是结构性事实,不随版本变。 */
|
|
66
|
+
const PROMOTE_TYPES = new Set(["tool"]);
|
|
54
67
|
|
|
55
68
|
const HEADERS = {
|
|
56
69
|
"methods.md": "# 方法(程序性记忆 · 小脑)\n\n由 `memory-distill` 从审计日志自动沉淀;也可以手写。\n" +
|
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* memory:recall(2026-11-24)—— 结构化记忆 vs 向量检索 的离线召回评测
|
|
4
|
+
*
|
|
5
|
+
* 目的:回答「FG 的结构化记忆注入」与「按查询检索记忆」哪个更准、哪个更省 token。
|
|
6
|
+
*
|
|
7
|
+
* 语料(memory units,两种方法用同一份):
|
|
8
|
+
* - `.focus-guard/workspace.json` 的 decisions / constraints / next_action(每条一个 unit)
|
|
9
|
+
* - `.ai/CASE_FILE.md` 按 `##`/`###` 切段(每段一个 unit)
|
|
10
|
+
*
|
|
11
|
+
* 语料口径(2026-11-26 收紧):`decisions` 里**只收带标签的条目**(`[goal]` / `[done]` / `[note]`,
|
|
12
|
+
* 复用 `src/memory/tags.mjs` 的 `parseTag`/`stripTag`)。无标签的一律**排除并只计数**——
|
|
13
|
+
* 自动落账已于 2026-11-26 下架(`memory/workspace.mjs` 头部有下架声明),存量那些是
|
|
14
|
+
* 「本轮人类指令原文」,拿它们当语料等于用垃圾测垃圾:测出来的 100% 只能证明「全量注入包含全量」。
|
|
15
|
+
*
|
|
16
|
+
* 空语料不算通过:`units` 或可标注查询为 0 时**不打印召回率**,直接报「结论无效」并 exit 2。
|
|
17
|
+
* 理由同上——这张表会被断章取义成「检索方案已验证」。
|
|
18
|
+
*
|
|
19
|
+
* 查询(queries):本工作区历史会话里的用户消息(每条 ≥ 12 字、去重),来自 session.v4 日志。
|
|
20
|
+
*
|
|
21
|
+
* 相关性标注(qrels,判定与两种检索都独立):
|
|
22
|
+
* 查询与 unit 的**词项 Jaccard** ≥ `--jaccard`(默认 0.12);没有命中的查询跳过并计数。
|
|
23
|
+
* 这是启发式标注,不是人工 qrels——报告里必须写明,结论只能看相对差。
|
|
24
|
+
*
|
|
25
|
+
* 两种方法:
|
|
26
|
+
* S 结构化:把**全部** memory units 注入(模拟当前 FG 行为),token = 全量单位字数/3;
|
|
27
|
+
* V 向量:TF-IDF 稀疏向量(离线、零依赖;不是神经语义向量),按余弦取 top-k,token = 命中单位字数/3。
|
|
28
|
+
*
|
|
29
|
+
* 指标:Recall@k、MRR、平均注入 token;另报检索耗时。
|
|
30
|
+
*
|
|
31
|
+
* 用法:
|
|
32
|
+
* node packages/core/tools/memory-recall-eval.mjs
|
|
33
|
+
* node packages/core/tools/memory-recall-eval.mjs --k 5 --jaccard 0.1 --json
|
|
34
|
+
*/
|
|
35
|
+
import { readFileSync, existsSync } from 'node:fs'
|
|
36
|
+
import { dirname, join, resolve } from 'node:path'
|
|
37
|
+
import { fileURLToPath } from 'node:url'
|
|
38
|
+
import { decodeSession, walkSessionFiles } from './lib/sessionDump.mjs'
|
|
39
|
+
import { parseTag, stripTag } from '../src/memory/tags.mjs'
|
|
40
|
+
|
|
41
|
+
const HERE = dirname(fileURLToPath(import.meta.url))
|
|
42
|
+
const ROOT = resolve(HERE, '..', '..', '..')
|
|
43
|
+
const args = process.argv.slice(2)
|
|
44
|
+
const pick = (f) => {
|
|
45
|
+
const i = args.indexOf(f)
|
|
46
|
+
return i >= 0 ? args[i + 1] : null
|
|
47
|
+
}
|
|
48
|
+
const K = Number(pick('--k') || 3)
|
|
49
|
+
const JACCARD = Number(pick('--jaccard') || 0.12)
|
|
50
|
+
const asJson = args.includes('--json')
|
|
51
|
+
|
|
52
|
+
const STOP = new Set(
|
|
53
|
+
('the a an and or of to in for on with is are was were be been it this that these those i you we they he she ' +
|
|
54
|
+
'我 你 他 她 它 我们 你们 他们 这 那 这个 那个 的 了 是 在 和 与 或 就 都 也 还 把 被 对 从 到 向 给 让 一个 一些 什么 怎么 为什么 不 没 有 要 能 会 可以 已经 需要 应该 现在 然后 但是 因为 所以')
|
|
55
|
+
.split(/\s+/)
|
|
56
|
+
.filter(Boolean),
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
function terms(text) {
|
|
60
|
+
const t = String(text || '')
|
|
61
|
+
.toLowerCase()
|
|
62
|
+
.replace(/```[\s\S]*?```/g, ' ')
|
|
63
|
+
.replace(/[^\p{L}\p{N}_$-]+/gu, ' ')
|
|
64
|
+
return t
|
|
65
|
+
.split(/\s+/)
|
|
66
|
+
.filter((w) => w.length >= 2 && !STOP.has(w))
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function jaccard(a, b) {
|
|
70
|
+
const A = new Set(a)
|
|
71
|
+
const B = new Set(b)
|
|
72
|
+
if (!A.size || !B.size) return 0
|
|
73
|
+
let inter = 0
|
|
74
|
+
for (const w of A) if (B.has(w)) inter += 1
|
|
75
|
+
return inter / (A.size + B.size - inter)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// ── 1) memory units ──
|
|
79
|
+
const units = []
|
|
80
|
+
function addUnit(kind, text) {
|
|
81
|
+
const body = String(text || '').trim()
|
|
82
|
+
if (!body) return
|
|
83
|
+
units.push({ id: `${kind}#${units.length}`, kind, text: body, terms: terms(body) })
|
|
84
|
+
}
|
|
85
|
+
// 下架前的存量:无标签的 decisions 一律不进语料,只计数(见文件头「语料口径」)。
|
|
86
|
+
let legacyPrompts = 0
|
|
87
|
+
const wsFile = process.env.FG_WORKSPACE_FILE || join(ROOT, '.focus-guard', 'workspace.json')
|
|
88
|
+
if (existsSync(wsFile)) {
|
|
89
|
+
const ws = JSON.parse(readFileSync(wsFile, 'utf8'))
|
|
90
|
+
for (const x of ws.decisions || []) {
|
|
91
|
+
const s = String(x ?? '').trim()
|
|
92
|
+
if (!s) continue
|
|
93
|
+
if (parseTag(s)) addUnit('decision', stripTag(s))
|
|
94
|
+
else legacyPrompts += 1
|
|
95
|
+
}
|
|
96
|
+
for (const x of ws.constraints || []) addUnit('constraint', x)
|
|
97
|
+
for (const x of ws.next_action || []) addUnit('next_action', x)
|
|
98
|
+
}
|
|
99
|
+
const caseFile = process.env.FG_CASE_FILE || join(ROOT, '.ai', 'CASE_FILE.md')
|
|
100
|
+
if (existsSync(caseFile)) {
|
|
101
|
+
const sections = readFileSync(caseFile, 'utf8').split(/^#{2,3} /m).slice(1)
|
|
102
|
+
for (const sec of sections) addUnit('case', sec.slice(0, 1200))
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ── 2) queries ──
|
|
106
|
+
const queries = []
|
|
107
|
+
const seen = new Set()
|
|
108
|
+
for (const { f } of walkSessionFiles(join(ROOT, '..', '.dsh', 'sessions'))) {
|
|
109
|
+
let lines
|
|
110
|
+
try {
|
|
111
|
+
lines = decodeSession(f)
|
|
112
|
+
} catch {
|
|
113
|
+
continue
|
|
114
|
+
}
|
|
115
|
+
for (const j of lines) {
|
|
116
|
+
if (j.type !== 'user/message') continue
|
|
117
|
+
const msg = j.data && (j.data.message || j.data)
|
|
118
|
+
const content = msg && msg.content
|
|
119
|
+
const text =
|
|
120
|
+
typeof content === 'string'
|
|
121
|
+
? content
|
|
122
|
+
: Array.isArray(content)
|
|
123
|
+
? content
|
|
124
|
+
.filter((b) => b && b.type === 'text')
|
|
125
|
+
.map((b) => b.text || '')
|
|
126
|
+
.join(' ')
|
|
127
|
+
: ''
|
|
128
|
+
const q = String(text || '').trim()
|
|
129
|
+
if ([...q].length < 12) continue
|
|
130
|
+
if (seen.has(q)) continue
|
|
131
|
+
seen.add(q)
|
|
132
|
+
const sid = f.replace(/\\/g, '/').split('/').slice(-2, -1)[0] || ''
|
|
133
|
+
queries.push({ q, sid, terms: terms(q) })
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ── 3) qrels ──
|
|
138
|
+
const labelled = []
|
|
139
|
+
for (const q of queries) {
|
|
140
|
+
const rel = units.filter((u) => jaccard(q.terms, u.terms) >= JACCARD).map((u) => u.id)
|
|
141
|
+
if (rel.length) labelled.push({ ...q, rel })
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ── 4) 结构化基线:全量注入 ──
|
|
145
|
+
const allTokens = Math.round(units.reduce((a, u) => a + u.text.length, 0) / 3)
|
|
146
|
+
const structured = { name: 'S 结构化全量', recallAtK: null, mrr: null, tokensPerQuery: allTokens, latencyMs: 0 }
|
|
147
|
+
{
|
|
148
|
+
let hit = 0
|
|
149
|
+
for (const q of labelled) {
|
|
150
|
+
// 全量注入:所有 relevant 都在上下文里 → Recall 恒为 100%;
|
|
151
|
+
// MRR 对"全量"无意义(没有排序),留空而不是写 1.0 假装。
|
|
152
|
+
hit += 1
|
|
153
|
+
}
|
|
154
|
+
structured.recallAtK = labelled.length ? hit / labelled.length : null
|
|
155
|
+
structured.mrr = null
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// ── 5) 向量检索:TF-IDF 稀疏向量 + 余弦 ──
|
|
159
|
+
const df = new Map()
|
|
160
|
+
for (const u of units) for (const w of new Set(u.terms)) df.set(w, (df.get(w) || 0) + 1)
|
|
161
|
+
const N = units.length || 1
|
|
162
|
+
const idf = (w) => Math.log((N + 1) / ((df.get(w) || 0) + 1)) + 1
|
|
163
|
+
function embed(t) {
|
|
164
|
+
const m = new Map()
|
|
165
|
+
for (const w of t) m.set(w, (m.get(w) || 0) + idf(w))
|
|
166
|
+
let norm = 0
|
|
167
|
+
for (const v of m.values()) norm += v * v
|
|
168
|
+
norm = Math.sqrt(norm) || 1
|
|
169
|
+
for (const [w, v] of m) m.set(w, v / norm)
|
|
170
|
+
return m
|
|
171
|
+
}
|
|
172
|
+
const unitVec = new Map(units.map((u) => [u.id, embed(u.terms)]))
|
|
173
|
+
function cosine(a, b) {
|
|
174
|
+
let s = 0
|
|
175
|
+
for (const [w, v] of a) {
|
|
176
|
+
const o = b.get(w)
|
|
177
|
+
if (o) s += v * o
|
|
178
|
+
}
|
|
179
|
+
return s
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const t0 = process.hrtime.bigint()
|
|
183
|
+
const vectorHits = []
|
|
184
|
+
for (const q of labelled) {
|
|
185
|
+
const qv = embed(q.terms)
|
|
186
|
+
const ranked = units
|
|
187
|
+
.map((u) => ({ id: u.id, score: cosine(qv, unitVec.get(u.id)), len: u.text.length }))
|
|
188
|
+
.sort((a, b) => b.score - a.score)
|
|
189
|
+
.slice(0, K)
|
|
190
|
+
const relSet = new Set(q.rel)
|
|
191
|
+
const hit = ranked.filter((r) => relSet.has(r.id))
|
|
192
|
+
vectorHits.push({
|
|
193
|
+
hit: hit.length ? 1 : 0,
|
|
194
|
+
rr: hit.length ? 1 / (ranked.findIndex((r) => relSet.has(r.id)) + 1) : 0,
|
|
195
|
+
tokens: Math.round(ranked.reduce((a, r) => a + r.len, 0) / 3),
|
|
196
|
+
})
|
|
197
|
+
}
|
|
198
|
+
const elapsed = Number(process.hrtime.bigint() - t0) / 1e6
|
|
199
|
+
const vector = {
|
|
200
|
+
name: `V 向量 top-${K}`,
|
|
201
|
+
recallAtK: vectorHits.length ? vectorHits.reduce((a, r) => a + r.hit, 0) / vectorHits.length : null,
|
|
202
|
+
mrr: vectorHits.length ? vectorHits.reduce((a, r) => a + r.rr, 0) / vectorHits.length : null,
|
|
203
|
+
tokensPerQuery: vectorHits.length ? Math.round(vectorHits.reduce((a, r) => a + r.tokens, 0) / vectorHits.length) : null,
|
|
204
|
+
latencyMs: Number((elapsed / Math.max(1, vectorHits.length)).toFixed(3)),
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// ── 5.5) 混合:常驻「必须保证」的条目 + 其余走向量 top-k ──
|
|
208
|
+
const PIN_RE = /红线|禁止|不得|必须|不要|不能|约束|goal|目标/i
|
|
209
|
+
const decisionUnits = units.filter((u) => u.kind === 'decision')
|
|
210
|
+
// 钉选策略(可调):约束/下一步 + **最近 3 条决策**。理由是近期决定比远期更可能被引用。
|
|
211
|
+
const recentDecisions = new Set(decisionUnits.slice(-3).map((u) => u.id))
|
|
212
|
+
const pinned = units.filter(
|
|
213
|
+
(u) => u.kind === 'constraint' || u.kind === 'next_action' || recentDecisions.has(u.id) || PIN_RE.test(u.text),
|
|
214
|
+
)
|
|
215
|
+
const pinnedIds = new Set(pinned.map((u) => u.id))
|
|
216
|
+
const retrievable = units.filter((u) => !pinnedIds.has(u.id))
|
|
217
|
+
const pinnedTokens = Math.round(pinned.reduce((a, u) => a + u.text.length, 0) / 3)
|
|
218
|
+
const rVec = new Map(retrievable.map((u) => [u.id, embed(u.terms)]))
|
|
219
|
+
const hybridHits = []
|
|
220
|
+
const th0 = process.hrtime.bigint()
|
|
221
|
+
for (const q of labelled) {
|
|
222
|
+
const qv = embed(q.terms)
|
|
223
|
+
const ranked = retrievable
|
|
224
|
+
.map((u) => ({ id: u.id, score: cosine(qv, rVec.get(u.id)), len: u.text.length }))
|
|
225
|
+
.sort((a, b) => b.score - a.score)
|
|
226
|
+
.slice(0, K)
|
|
227
|
+
const relSet = new Set(q.rel)
|
|
228
|
+
const pinnedHit = q.rel.some((id) => pinnedIds.has(id))
|
|
229
|
+
const vectorHit = ranked.some((r) => relSet.has(r.id))
|
|
230
|
+
hybridHits.push({
|
|
231
|
+
hit: pinnedHit || vectorHit ? 1 : 0,
|
|
232
|
+
tokens: pinnedTokens + Math.round(ranked.reduce((a, r) => a + r.len, 0) / 3),
|
|
233
|
+
})
|
|
234
|
+
}
|
|
235
|
+
const hybridElapsed = Number(process.hrtime.bigint() - th0) / 1e6
|
|
236
|
+
const hybrid = {
|
|
237
|
+
name: `H 混合(钉 ${pinned.length} 条 + top-${K})`,
|
|
238
|
+
recallAtK: hybridHits.length ? hybridHits.reduce((a, r) => a + r.hit, 0) / hybridHits.length : null,
|
|
239
|
+
mrr: null,
|
|
240
|
+
tokensPerQuery: hybridHits.length ? Math.round(hybridHits.reduce((a, r) => a + r.tokens, 0) / hybridHits.length) : null,
|
|
241
|
+
latencyMs: Number((hybridElapsed / Math.max(1, hybridHits.length)).toFixed(3)),
|
|
242
|
+
pinned: pinned.map((u) => u.kind),
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const byKind = {}
|
|
246
|
+
for (const u of units) byKind[u.kind] = (byKind[u.kind] || 0) + 1
|
|
247
|
+
// 结论无效的两种情形:语料空 / 标注空。都不打印召回率——空集上的 100% 是假象。
|
|
248
|
+
const invalidReason =
|
|
249
|
+
units.length === 0
|
|
250
|
+
? '语料为空(无带标签结论、无约束、无 CASE 段)'
|
|
251
|
+
: labelled.length === 0
|
|
252
|
+
? `语料 ${units.length} 条,但没有一条与查询相关(标注 0 条)`
|
|
253
|
+
: null
|
|
254
|
+
|
|
255
|
+
const report = {
|
|
256
|
+
generatedAt: new Date().toISOString(),
|
|
257
|
+
units: units.length,
|
|
258
|
+
byKind,
|
|
259
|
+
legacyPrompts,
|
|
260
|
+
queries: queries.length,
|
|
261
|
+
labelled: labelled.length,
|
|
262
|
+
k: K,
|
|
263
|
+
jaccardThreshold: JACCARD,
|
|
264
|
+
note: '向量=TF-IDF 稀疏向量(离线),不是神经语义向量;qrels 为词项 Jaccard 启发式,结论只看相对差',
|
|
265
|
+
conclusionInvalid: invalidReason,
|
|
266
|
+
methods: [structured, vector, hybrid],
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// ── 6) 可选:会话级语料(更大、更像真实跨会话召回)──
|
|
270
|
+
if (args.includes('--sessions')) {
|
|
271
|
+
const chunks = []
|
|
272
|
+
for (const { f } of walkSessionFiles(join(ROOT, '..', '.dsh', 'sessions'))) {
|
|
273
|
+
let lines
|
|
274
|
+
try {
|
|
275
|
+
lines = decodeSession(f)
|
|
276
|
+
} catch {
|
|
277
|
+
continue
|
|
278
|
+
}
|
|
279
|
+
const sid = f.replace(/\\/g, '/').split('/').slice(-2, -1)[0] || ''
|
|
280
|
+
let buf = ''
|
|
281
|
+
let lastTurn = null
|
|
282
|
+
const flush = () => {
|
|
283
|
+
const text = buf.trim()
|
|
284
|
+
if ([...text].length >= 40) chunks.push({ id: `chunk#${chunks.length}`, sid, text: text.slice(0, 800), terms: terms(text) })
|
|
285
|
+
buf = ''
|
|
286
|
+
}
|
|
287
|
+
for (const j of lines) {
|
|
288
|
+
if (j.type !== 'user/message' && j.type !== 'assistant/message') continue
|
|
289
|
+
const msg = j.data && (j.data.message || j.data)
|
|
290
|
+
const content = msg && msg.content
|
|
291
|
+
const text =
|
|
292
|
+
typeof content === 'string'
|
|
293
|
+
? content
|
|
294
|
+
: Array.isArray(content)
|
|
295
|
+
? content.filter((b) => b && b.type === 'text').map((b) => b.text || '').join(' ')
|
|
296
|
+
: ''
|
|
297
|
+
if (!text) continue
|
|
298
|
+
const turn = j.data && j.data.turn
|
|
299
|
+
if (lastTurn !== null && turn !== lastTurn) {
|
|
300
|
+
flush()
|
|
301
|
+
}
|
|
302
|
+
lastTurn = turn
|
|
303
|
+
buf += ' ' + text
|
|
304
|
+
if ([...buf].length > 800) flush()
|
|
305
|
+
}
|
|
306
|
+
flush()
|
|
307
|
+
}
|
|
308
|
+
const cdf = new Map()
|
|
309
|
+
for (const c of chunks) for (const w of new Set(c.terms)) cdf.set(w, (cdf.get(w) || 0) + 1)
|
|
310
|
+
const CN = chunks.length || 1
|
|
311
|
+
const cidf = (w) => Math.log((CN + 1) / ((cdf.get(w) || 0) + 1)) + 1
|
|
312
|
+
const cvec = (t) => {
|
|
313
|
+
const m = new Map()
|
|
314
|
+
for (const w of t) m.set(w, (m.get(w) || 0) + cidf(w))
|
|
315
|
+
let n = 0
|
|
316
|
+
for (const v of m.values()) n += v * v
|
|
317
|
+
n = Math.sqrt(n) || 1
|
|
318
|
+
for (const [w, v] of m) m.set(w, v / n)
|
|
319
|
+
return m
|
|
320
|
+
}
|
|
321
|
+
const chunkVec = new Map(chunks.map((c) => [c.id, cvec(c.terms)]))
|
|
322
|
+
const relabel = []
|
|
323
|
+
for (const q of queries) {
|
|
324
|
+
const rel = chunks.filter((c) => c.sid !== q.sid && jaccard(q.terms, c.terms) >= JACCARD).map((c) => c.id)
|
|
325
|
+
if (rel.length) relabel.push({ ...q, rel })
|
|
326
|
+
}
|
|
327
|
+
const hits = []
|
|
328
|
+
let tokens = 0
|
|
329
|
+
let structHits = 0
|
|
330
|
+
const hybHits = []
|
|
331
|
+
let hybTokens = 0
|
|
332
|
+
const t1 = process.hrtime.bigint()
|
|
333
|
+
for (const q of relabel) {
|
|
334
|
+
// 结构化基线在同一任务上的"召回归属":qrel 命中的会话片段,其词项是否被 FG 记忆覆盖
|
|
335
|
+
// (记忆全是短条目,命中按 0.3 Jaccard 算覆盖;这是摘要保真度代理,不是逐字包含)。
|
|
336
|
+
const chunkById = new Map(chunks.map((c) => [c.id, c]))
|
|
337
|
+
const covered = q.rel.some((id) => {
|
|
338
|
+
const c = chunkById.get(id)
|
|
339
|
+
if (!c) return false
|
|
340
|
+
return units.some((u) => jaccard(c.terms, u.terms) >= 0.3)
|
|
341
|
+
})
|
|
342
|
+
structHits += covered ? 1 : 0
|
|
343
|
+
const qv = cvec(q.terms)
|
|
344
|
+
const ranked = chunks
|
|
345
|
+
.map((c) => ({ id: c.id, score: cosine(qv, chunkVec.get(c.id)), len: c.text.length }))
|
|
346
|
+
.sort((a, b) => b.score - a.score)
|
|
347
|
+
.slice(0, K)
|
|
348
|
+
const relSet = new Set(q.rel)
|
|
349
|
+
const idx = ranked.findIndex((r) => relSet.has(r.id))
|
|
350
|
+
const vhit = idx >= 0 ? 1 : 0
|
|
351
|
+
hits.push(vhit)
|
|
352
|
+
const chunkTokens = ranked.reduce((a, r) => a + r.len, 0) / 3
|
|
353
|
+
tokens += chunkTokens
|
|
354
|
+
hybHits.push(covered || vhit ? 1 : 0)
|
|
355
|
+
hybTokens += pinnedTokens + chunkTokens
|
|
356
|
+
}
|
|
357
|
+
const el = Number(process.hrtime.bigint() - t1) / 1e6
|
|
358
|
+
report.sessionCorpus = {
|
|
359
|
+
chunks: chunks.length,
|
|
360
|
+
labelled: relabel.length,
|
|
361
|
+
recallAtK: relabel.length ? hits.reduce((a, b) => a + b, 0) / relabel.length : null,
|
|
362
|
+
structuredCoverage: relabel.length ? structHits / relabel.length : null,
|
|
363
|
+
hybridRecall: relabel.length ? hybHits.reduce((a, b) => a + b, 0) / relabel.length : null,
|
|
364
|
+
hybridTokensPerQuery: relabel.length ? Math.round(hybTokens / relabel.length) : null,
|
|
365
|
+
tokensPerQuery: relabel.length ? Math.round(tokens / relabel.length) : null,
|
|
366
|
+
latencyMs: Number((el / Math.max(1, relabel.length)).toFixed(3)),
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
if (asJson) console.log(JSON.stringify(report, null, 2))
|
|
371
|
+
else {
|
|
372
|
+
console.log(`[memory:recall] units=${units.length} queries=${queries.length} labelled=${labelled.length} k=${K} jaccard=${JACCARD}`)
|
|
373
|
+
console.log(
|
|
374
|
+
` 语料构成:带标签结论 ${byKind.decision || 0} · 被否命令 ${byKind.constraint || 0} · ` +
|
|
375
|
+
`下一步 ${byKind.next_action || 0} · CASE 段 ${byKind.case || 0}`,
|
|
376
|
+
)
|
|
377
|
+
if (legacyPrompts) {
|
|
378
|
+
console.log(
|
|
379
|
+
` 已排除:无标签 decisions ${legacyPrompts} 条(自动落账已于 2026-11-26 下架,指令原文不再算语料)`,
|
|
380
|
+
)
|
|
381
|
+
}
|
|
382
|
+
if (invalidReason) {
|
|
383
|
+
console.log(`\n ⚠ 结论无效:${invalidReason}。`)
|
|
384
|
+
console.log(' 不打印召回率——空语料上的 100% 会被读成「检索已验证」。')
|
|
385
|
+
console.log(' 要有可用语料,先等 `fg_memory_note` 写出带标签的结论(VERSION-PLAN §1.8 步 1)。')
|
|
386
|
+
} else {
|
|
387
|
+
for (const m of report.methods) {
|
|
388
|
+
console.log(
|
|
389
|
+
` ${m.name.padEnd(14)} Recall@${K}=${m.recallAtK == null ? '-' : (m.recallAtK * 100).toFixed(1) + '%'} ` +
|
|
390
|
+
`MRR=${m.mrr == null ? '-' : m.mrr.toFixed(3)} tokens/query=${m.tokensPerQuery} latency=${m.latencyMs}ms`,
|
|
391
|
+
)
|
|
392
|
+
}
|
|
393
|
+
console.log(` 注:${report.note}`)
|
|
394
|
+
}
|
|
395
|
+
if (report.sessionCorpus) {
|
|
396
|
+
const s = report.sessionCorpus
|
|
397
|
+
console.log(
|
|
398
|
+
` [会话语料] chunks=${s.chunks} labelled=${s.labelled} ` +
|
|
399
|
+
`V Recall@${K}=${s.recallAtK == null ? '-' : (s.recallAtK * 100).toFixed(1) + '%'} ` +
|
|
400
|
+
`H Recall=${s.hybridRecall == null ? '-' : (s.hybridRecall * 100).toFixed(1) + '%'} ` +
|
|
401
|
+
`S 覆盖=${s.structuredCoverage == null ? '-' : (s.structuredCoverage * 100).toFixed(1) + '%'} ` +
|
|
402
|
+
`V tokens=${s.tokensPerQuery} H tokens=${s.hybridTokensPerQuery} latency=${s.latencyMs}ms`,
|
|
403
|
+
)
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// exit 2 专指「没测出东西」,与 1(脚本自身出错)区分开:方便外面一眼分辨是缺语料还是坏了。
|
|
408
|
+
if (invalidReason) process.exitCode = 2
|
package/tools/module-audit.mjs
CHANGED
|
@@ -4,30 +4,15 @@
|
|
|
4
4
|
* 输出:每个模块的行数/导出符号/TODO/空 catch/console 调用;测试覆盖线索。
|
|
5
5
|
* 只读,不改代码。
|
|
6
6
|
*/
|
|
7
|
-
import {
|
|
7
|
+
import { readFileSync } from 'node:fs'
|
|
8
8
|
import { dirname, join, relative } from 'node:path'
|
|
9
9
|
import { fileURLToPath } from 'node:url'
|
|
10
|
+
import { walk } from './lib/import-graph.mjs'
|
|
10
11
|
|
|
11
12
|
const HERE = dirname(fileURLToPath(import.meta.url))
|
|
12
13
|
const CORE = join(HERE, '..')
|
|
13
14
|
const ROOTS = ['src/core', 'src/adapters/dsh', 'src/dsh', 'hooks', 'tools', 'tests']
|
|
14
15
|
|
|
15
|
-
function walk(dir, out = []) {
|
|
16
|
-
let entries = []
|
|
17
|
-
try {
|
|
18
|
-
entries = readdirSync(dir)
|
|
19
|
-
} catch {
|
|
20
|
-
return out
|
|
21
|
-
}
|
|
22
|
-
for (const e of entries) {
|
|
23
|
-
const p = join(dir, e)
|
|
24
|
-
const st = statSync(p)
|
|
25
|
-
if (st.isDirectory()) walk(p, out)
|
|
26
|
-
else if (/\.(mjs|js)$/.test(e)) out.push(p)
|
|
27
|
-
}
|
|
28
|
-
return out
|
|
29
|
-
}
|
|
30
|
-
|
|
31
16
|
const files = ROOTS.flatMap((r) => walk(join(CORE, r)))
|
|
32
17
|
const rows = []
|
|
33
18
|
for (const f of files) {
|