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
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process'
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
3
|
+
import { join } from 'node:path'
|
|
4
|
+
import { MODULES, META_FILES } from './modules-list.mjs'
|
|
5
|
+
import { MODULES_DOC, renderModules } from './modules-doc.mjs'
|
|
6
|
+
import { lineOf, linesOf } from './where.mjs'
|
|
7
|
+
|
|
8
|
+
// 渲染器住在 modules-doc.mjs,清单住在 modules-list.mjs,这里只管「怎么算」。
|
|
9
|
+
export { MODULES, META_FILES, MODULES_DOC, renderModules }
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 模块修订台账(tools/lib/modules.mjs,2026-11-19)
|
|
13
|
+
*
|
|
14
|
+
* ## 它防的是什么
|
|
15
|
+
*
|
|
16
|
+
* 不是「代码坏了」,是「**某块地方很久没人看,也没人记得它存在**」。
|
|
17
|
+
* 这类腐化没有报错、没有红测试,只是慢慢和其余部分脱节:
|
|
18
|
+
* 别人重构了调用方,它还按旧契约写着;接口变了,它还在按老假设兜底。
|
|
19
|
+
*
|
|
20
|
+
* ## 为什么一个日期都不存
|
|
21
|
+
*
|
|
22
|
+
* `docs/INDEX.md` 手抄过一份「适用版本 / 最后核对」,实测 **18 处与文档自身状态头不符**,
|
|
23
|
+
* 分叉二十多天无人发现——手写的真值一定会腐化,且没有判据能发现。
|
|
24
|
+
* 所以这里全部现算 git。手写的只有一样:**哪些路径算一个模块**(`modules-list.mjs`)。
|
|
25
|
+
*
|
|
26
|
+
* ## 判据为什么用「其后全仓提交」而不是天数
|
|
27
|
+
*
|
|
28
|
+
* 初版按「距今 120 天 / 400 次提交」判定,跑出 13 个模块全绿;收紧到 30 天仍全绿。
|
|
29
|
+
* 因为这个仓只有 51 天(397 次提交、日均 7.6 次)——照「几年的仓」设的天数线,
|
|
30
|
+
* 等于永远量不到东西。第二版改用「模块自身提交间隔的中位数」,更糟:多数模块是被
|
|
31
|
+
* 一批连续 commit 引入的,中位数被压到 0.0x 天;而提交不足 3 次的模块算不出中位数、
|
|
32
|
+
* 退回宽松兜底,**越该被提醒的越绿**——方向是反的。
|
|
33
|
+
*
|
|
34
|
+
* 最终用:**该文件最后一次修订之后,全仓又提交了多少次**。
|
|
35
|
+
* 它随仓库节奏自伸缩(日更十次的仓里 30 次是三天,月更的仓里是半年)、单调、动一次归零,
|
|
36
|
+
* 而且不需要给任何模块配数字——没有第二份手写真值。
|
|
37
|
+
*
|
|
38
|
+
* ## 为什么按「中位文件」判,不按「最新文件」
|
|
39
|
+
*
|
|
40
|
+
* 2026-11-19 第二处返工。模块整体取最新,等于**只统计最勤快的那一个文件**:
|
|
41
|
+
* 实测 `tools` 最新文件 0 天、中位文件 16 天;`src/core` 最新 0 天、中位 21 天。
|
|
42
|
+
* 一个 61 文件的目录里 2 个在动,整块就显示「活跃」——正是「个别文件活跃,其余很少动」
|
|
43
|
+
* 被遮住的地方。所以判定看**该模块所有文件「其后提交数」的中位数**:
|
|
44
|
+
* 一半以上的文件多久没动,才是这块真实的陈旧程度。最新与最久照列,供人看分布。
|
|
45
|
+
*
|
|
46
|
+
* ## 判定不拦构建
|
|
47
|
+
*
|
|
48
|
+
* 「久未动」不等于「坏了」。设成硬红就是一个永远红的闸门——「永久红会训练人忽略告警」。
|
|
49
|
+
* 所以表里加粗提示,`--strict` 也不因它 exit 1。真正会红的是**确定是错**的三件事:
|
|
50
|
+
* 清单路径不存在、两个模块抢同一个文件、有功能文件没被任何模块认领。
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
/** 等级由高到低。排序用它,`无法定位` 排最前——路径在 git 里找不到任何提交,本身就要看。 */
|
|
54
|
+
export const LEVEL_RANK = { 无法定位: 4, 久未动: 3, 临期: 2, 偏冷: 1, 活跃: 0 }
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 病灶位置:`modules-*` 的每一类错都出在这份清单里,所以报点必须指到清单的某一行。
|
|
58
|
+
*
|
|
59
|
+
* 此前这里写的是 `line: 1`——那不是"位置",是"文件第一行"。一条命令跳到文件开头,
|
|
60
|
+
* 然后你还得自己在 350 行里找那个 id,等于没有定位。
|
|
61
|
+
* 现按病灶类型分别取真行号:id 重复取**第二次**出现的 `id:`,路径重叠取**后登记**的那条字面量。
|
|
62
|
+
*/
|
|
63
|
+
export const LIST_FILE = 'packages/core/tools/lib/modules-list.mjs'
|
|
64
|
+
|
|
65
|
+
/** 一次运行里清单会被四道判据各读一遍,缓存按 repo 分桶(测试会指到夹具目录)。 */
|
|
66
|
+
const listCache = new Map()
|
|
67
|
+
function listSource(repo) {
|
|
68
|
+
if (!listCache.has(repo)) {
|
|
69
|
+
let text = ''
|
|
70
|
+
try {
|
|
71
|
+
text = readFileSync(join(repo, LIST_FILE), 'utf8')
|
|
72
|
+
} catch {
|
|
73
|
+
text = ''
|
|
74
|
+
}
|
|
75
|
+
listCache.set(repo, text)
|
|
76
|
+
}
|
|
77
|
+
return listCache.get(repo)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** `id: 'xxx'` 的行号;`nth` 取第几次出现(重复 id 要报后来那条)。 */
|
|
81
|
+
function idLine(repo, id, nth = 0) {
|
|
82
|
+
const hits = linesOf(listSource(repo), new RegExp(`id:\\s*['"]${id.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}['"]`))
|
|
83
|
+
return hits.length ? hits[Math.min(nth, hits.length - 1)] : 0
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** 路径字面量的行号;`nth` 取第几次出现(重叠要报后登记的那张)。 */
|
|
87
|
+
function pathLine(repo, p, nth = 0) {
|
|
88
|
+
for (const q of [`'${p}'`, `"${p}"`]) {
|
|
89
|
+
const hits = linesOf(listSource(repo), q)
|
|
90
|
+
if (hits.length) return hits[Math.min(nth, hits.length - 1)]
|
|
91
|
+
}
|
|
92
|
+
return 0
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** 数组声明行:有文件谁都不认领时,病灶就是"这里少了一条",指回清单开头是对的。 */
|
|
96
|
+
function modulesHeadLine(repo) {
|
|
97
|
+
return lineOf(listSource(repo), /export const MODULES\s*=\s*\[/)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 阈值:**中位文件「其后全仓提交」的次数**。
|
|
102
|
+
*
|
|
103
|
+
* `30` 的意思是「仓库又往前走了三十步,这块一半以上的文件一步没挪」。
|
|
104
|
+
* 定这个值时的仓况:51 天 / 397 次提交 / 日均 7.6 次,所以 30 次 ≈ 四天。
|
|
105
|
+
* 仓的节奏变了不必急着调——它是相对量,会跟着一起伸缩。
|
|
106
|
+
*/
|
|
107
|
+
const SINCE = { cold: 30, due: 80, stale: 150 }
|
|
108
|
+
|
|
109
|
+
/** 累计提交少于此数视为「历史浅」:样本不足以谈节奏,本身也是遗忘风险。 */
|
|
110
|
+
const THIN = 3
|
|
111
|
+
|
|
112
|
+
const DAYS = 86400
|
|
113
|
+
|
|
114
|
+
/** `git` 调用统一出口:失败一律返回 null,**不抛**——没装 git 的机器不该因此炸掉。 */
|
|
115
|
+
function git(repo, args) {
|
|
116
|
+
try {
|
|
117
|
+
return execFileSync('git', args, { cwd: repo, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim()
|
|
118
|
+
} catch {
|
|
119
|
+
return null
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 一次读完整部历史:新 → 旧,每条带时间戳、短 hash、标题、触及的文件。
|
|
125
|
+
*
|
|
126
|
+
* 用 `--name-status` 而非 `--name-only`:改名会输出 `R100\t旧\t新`,只取新路径就会
|
|
127
|
+
* 漏掉「某文件被移出本模块」那次提交(实测 adapters 少算 1 次)。文件名那栏可能有
|
|
128
|
+
* 两个字段,所以 `parts.slice(1)` 全收。
|
|
129
|
+
*/
|
|
130
|
+
function scanHistory(repo) {
|
|
131
|
+
const raw = git(repo, ['log', '--format=%x00%ct%x00%h%x00%s', '--name-status'])
|
|
132
|
+
if (!raw) return []
|
|
133
|
+
const out = []
|
|
134
|
+
let cur = null
|
|
135
|
+
for (const line of raw.split('\n')) {
|
|
136
|
+
if (line.startsWith('\u0000')) {
|
|
137
|
+
const [, ts, hash, subject] = line.split('\u0000')
|
|
138
|
+
cur = { ts: Number(ts), hash, subject, files: new Set() }
|
|
139
|
+
out.push(cur)
|
|
140
|
+
continue
|
|
141
|
+
}
|
|
142
|
+
if (!cur || !line) continue
|
|
143
|
+
for (const f of line.split('\t').slice(1)) cur.files.add(f)
|
|
144
|
+
}
|
|
145
|
+
return out
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* 排除项的两种写法:`:(exclude)path` 与 `:!path`。**必须按整段前缀切**。
|
|
150
|
+
*
|
|
151
|
+
* 2026-11-19 首版写成 `p.slice(3, -1)`,本想把 `:(exclude)foo` 切成 `foo`,
|
|
152
|
+
* 实际切出 `xclude)fo`——排除项从此一条都没生效:`dsh` 把 `guard`/`seams` 的提交
|
|
153
|
+
* 算进自己(101 次,git 的答案是 98),`core` 把 `prefixStability` 算进自己(68,实为 64)。
|
|
154
|
+
* 手工复刻 git 的 pathspec 就是在重写它的解析器,**任何一处差一点都会静默算错**,
|
|
155
|
+
* 所以 `tests/modules-attribution.test.mjs` 拿 git 自己的结果当基准比对。
|
|
156
|
+
*/
|
|
157
|
+
const EXCLUDE = /^:(?:\(exclude\)|!)/
|
|
158
|
+
|
|
159
|
+
/** 路径是否落在某组 pathspec 里(含排除项)。目录按 `/` 边界匹配,防 `tools-x` 误配 `tools`。 */
|
|
160
|
+
function matches(paths, file) {
|
|
161
|
+
let hit = false
|
|
162
|
+
for (const p of paths) {
|
|
163
|
+
const neg = EXCLUDE.exec(p)
|
|
164
|
+
const raw = neg ? p.slice(neg[0].length) : p
|
|
165
|
+
const inside = file === raw || file.startsWith(`${raw}/`)
|
|
166
|
+
if (!inside) continue
|
|
167
|
+
if (neg) return false
|
|
168
|
+
hit = true
|
|
169
|
+
}
|
|
170
|
+
return hit
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** 中位数(偶数取偏上)。空数组返回 null。 */
|
|
174
|
+
function median(nums) {
|
|
175
|
+
if (!nums.length) return null
|
|
176
|
+
const s = [...nums].sort((a, b) => a - b)
|
|
177
|
+
return s[Math.floor(s.length / 2)]
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function collectModules(repo, modules = MODULES, now = new Date()) {
|
|
181
|
+
const commits = scanHistory(repo)
|
|
182
|
+
const head = commits[0]?.hash ?? null
|
|
183
|
+
const first = commits[commits.length - 1]?.ts ?? null
|
|
184
|
+
const age = first ? Math.floor((now.getTime() / 1000 - first) / DAYS) : null
|
|
185
|
+
// 每个文件最后一次被碰的**提交下标**(0 = HEAD)。下标本身就是「其后全仓提交数」。
|
|
186
|
+
const lastAt = new Map()
|
|
187
|
+
for (let i = 0; i < commits.length; i += 1) for (const f of commits[i].files) if (!lastAt.has(f)) lastAt.set(f, i)
|
|
188
|
+
const tracked = (git(repo, ['ls-files']) || '').split('\n').filter(Boolean)
|
|
189
|
+
const dirtySet = new Set(
|
|
190
|
+
(git(repo, ['status', '--porcelain', '-uall']) || '')
|
|
191
|
+
.split('\n')
|
|
192
|
+
.filter(Boolean)
|
|
193
|
+
.map((l) => l.slice(3).replace(/^.* -> /, '')),
|
|
194
|
+
)
|
|
195
|
+
// 台账的「适用版本」要填**产品版本号**,不是 commit hash——hash 进不了版本面比较,
|
|
196
|
+
// 在 `docs/STATUS.md` 里会被当成「无版本面」。hash 放在正文里就够定位。
|
|
197
|
+
let version = null
|
|
198
|
+
try {
|
|
199
|
+
version = JSON.parse(readFileSync(join(repo, 'packages/core/package.json'), 'utf8')).version
|
|
200
|
+
} catch {
|
|
201
|
+
version = null
|
|
202
|
+
}
|
|
203
|
+
const ago = (i) => (commits[i] ? Math.floor((now.getTime() / 1000 - commits[i].ts) / DAYS) : null)
|
|
204
|
+
const rows = []
|
|
205
|
+
for (const m of modules) {
|
|
206
|
+
const files = tracked.filter((f) => matches(m.paths, f))
|
|
207
|
+
const idxs = files.map((f) => lastAt.get(f)).filter((v) => v !== undefined).sort((a, b) => a - b)
|
|
208
|
+
// 提交归属按 pathspec 语义逐条判,不看文件是否还存在——git 的 `log -- path`
|
|
209
|
+
// 连已删除/改名的旧路径也算,只拿现存文件比对会让累计数比 `rev-list --count` 少。
|
|
210
|
+
let touches = 0
|
|
211
|
+
let hit = null
|
|
212
|
+
for (let i = 0; i < commits.length; i += 1) {
|
|
213
|
+
if (![...commits[i].files].some((f) => matches(m.paths, f))) continue
|
|
214
|
+
touches += 1
|
|
215
|
+
if (hit === null) hit = i
|
|
216
|
+
}
|
|
217
|
+
const mid = median(idxs)
|
|
218
|
+
rows.push({
|
|
219
|
+
id: m.id,
|
|
220
|
+
name: m.name,
|
|
221
|
+
note: m.note,
|
|
222
|
+
files: files.length,
|
|
223
|
+
latest: idxs.length ? idxs[0] : null,
|
|
224
|
+
median: mid,
|
|
225
|
+
oldest: idxs.length ? idxs[idxs.length - 1] : null,
|
|
226
|
+
latestDays: idxs.length ? ago(idxs[0]) : null,
|
|
227
|
+
medianDays: mid === null ? null : ago(mid),
|
|
228
|
+
oldestDays: idxs.length ? ago(idxs[idxs.length - 1]) : null,
|
|
229
|
+
since: hit,
|
|
230
|
+
touches,
|
|
231
|
+
hash: hit === null ? null : commits[hit].hash,
|
|
232
|
+
date: hit === null ? null : new Date(commits[hit].ts * 1000).toISOString(),
|
|
233
|
+
dirty: files.some((f) => dirtySet.has(f)),
|
|
234
|
+
level:
|
|
235
|
+
mid === null
|
|
236
|
+
? '无法定位'
|
|
237
|
+
: mid >= SINCE.stale
|
|
238
|
+
? '久未动'
|
|
239
|
+
: mid >= SINCE.due
|
|
240
|
+
? '临期'
|
|
241
|
+
: mid >= SINCE.cold
|
|
242
|
+
? '偏冷'
|
|
243
|
+
: '活跃',
|
|
244
|
+
})
|
|
245
|
+
}
|
|
246
|
+
// 按风险排,不按「有没有未提交改动」排——那个只该是标记。
|
|
247
|
+
// 首版把 dirty 排最前,结果前两行全是「有未提交改动」,后面一片绿,看着像全活跃。
|
|
248
|
+
rows.sort((a, b) => LEVEL_RANK[b.level] - LEVEL_RANK[a.level] || (b.median ?? -1) - (a.median ?? -1))
|
|
249
|
+
return { head, version, age, total: commits.length, rows }
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* 覆盖情况:有没有功能文件没被任何模块认领。
|
|
254
|
+
*
|
|
255
|
+
* 这是「全活跃」那次翻车的真正原因——清单里少了整个 `packages/extended`(外接三包),
|
|
256
|
+
* 而它 43 天没动、史上只有 5 次提交,**最该被提醒的那块根本不在表里**。
|
|
257
|
+
* 仓根那些 LICENSE / package.json / CI 配置列在 `META_FILES` 里,是「本来就不归属」,
|
|
258
|
+
* 不算漏;除它们之外还有文件没归属,就是清单真的漏了。
|
|
259
|
+
*/
|
|
260
|
+
export function collectCoverage(repo, modules = MODULES) {
|
|
261
|
+
const tracked = (git(repo, ['ls-files']) || '').split('\n').filter(Boolean)
|
|
262
|
+
const unowned = tracked.filter((f) => !matches(META_FILES, f) && !modules.some((m) => matches(m.paths, f)))
|
|
263
|
+
return { total: tracked.length, meta: tracked.filter((f) => matches(META_FILES, f)).length, unowned: unowned.length, sample: unowned.slice(0, 5) }
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* 判据①:清单里的路径必须存在。
|
|
268
|
+
*
|
|
269
|
+
* 这是**真错**:目录搬走/改名而清单没跟着改,表就成了假话,而且看起来一切正常。
|
|
270
|
+
* 与「久未动」不同——久未动是信息,路径不存在是缺陷。
|
|
271
|
+
*/
|
|
272
|
+
export function checkModulePaths(repo, modules = MODULES) {
|
|
273
|
+
const findings = []
|
|
274
|
+
for (const m of modules) {
|
|
275
|
+
for (const p of m.paths) {
|
|
276
|
+
if (EXCLUDE.test(p)) continue // 排除项不是路径,别拿它去 existsSync
|
|
277
|
+
if (!existsSync(join(repo, p))) {
|
|
278
|
+
findings.push({ rule: 'modules-path-missing', file: LIST_FILE, line: pathLine(repo, p), detail: `${m.id} 的路径不存在:${p}` })
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return findings
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** 判据②:模块 `id` 不许重复——重复会让 `--json` 的消费方拿到两条同 id 的记录。 */
|
|
286
|
+
export function checkModuleIds(repo, modules = MODULES) {
|
|
287
|
+
const seen = new Map()
|
|
288
|
+
const findings = []
|
|
289
|
+
for (const m of modules) {
|
|
290
|
+
if (seen.has(m.id)) {
|
|
291
|
+
findings.push({ rule: 'modules-id-duplicate', file: LIST_FILE, line: idLine(repo, m.id, 1), detail: `id \`${m.id}\` 重复(${seen.get(m.id)} 与 ${m.name})` })
|
|
292
|
+
} else {
|
|
293
|
+
seen.set(m.id, m.name)
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return findings
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* 判据③:一个文件只能属于一个模块。
|
|
301
|
+
*
|
|
302
|
+
* 重叠的后果是同一份改动同时点亮两行,看着像「两块都在动」,判定就假了。
|
|
303
|
+
* 三十个模块全手写路径,重叠是很容易犯的错——所以由机器查。
|
|
304
|
+
*/
|
|
305
|
+
export function checkModuleOverlap(repo, modules = MODULES) {
|
|
306
|
+
const tracked = (git(repo, ['ls-files']) || '').split('\n').filter(Boolean)
|
|
307
|
+
const owner = new Map()
|
|
308
|
+
const findings = []
|
|
309
|
+
for (const m of modules) {
|
|
310
|
+
for (const f of tracked) {
|
|
311
|
+
if (!matches(m.paths, f)) continue
|
|
312
|
+
if (owner.has(f) && owner.get(f) !== m.id) {
|
|
313
|
+
findings.push({ rule: 'modules-path-overlap', file: LIST_FILE, line: pathLine(repo, f, 1), detail: `${f} 同时属于 ${owner.get(f)} 与 ${m.id}` })
|
|
314
|
+
} else {
|
|
315
|
+
owner.set(f, m.id)
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return findings
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** 判据④:不能有功能文件落在所有模块之外——漏掉一块,那块就永远不会被提醒。 */
|
|
323
|
+
export function checkModuleCoverage(repo, modules = MODULES) {
|
|
324
|
+
const { unowned, sample } = collectCoverage(repo, modules)
|
|
325
|
+
if (!unowned) return []
|
|
326
|
+
return [
|
|
327
|
+
{
|
|
328
|
+
rule: 'modules-unowned-path',
|
|
329
|
+
file: LIST_FILE,
|
|
330
|
+
line: modulesHeadLine(repo),
|
|
331
|
+
detail: `${unowned} 个文件不属于任何模块:${sample.join('、')}${unowned > sample.length ? ' 等' : ''}`,
|
|
332
|
+
},
|
|
333
|
+
]
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* 台账是否与现算一致。
|
|
338
|
+
*
|
|
339
|
+
* **刻意不进 `docs:lint`**:这张表的「其后提交」每次提交都会变,一旦当成硬闸,
|
|
340
|
+
* 每次 commit 都会红一次——那正是「永久红的闸门等于没有闸门」。
|
|
341
|
+
* 改为由 `.githooks/pre-commit` 自动重新生成并 `git add`,人不用记着跑。
|
|
342
|
+
*/
|
|
343
|
+
export function checkModulesDoc(repo) {
|
|
344
|
+
const want = renderModules(collectModules(repo), collectCoverage(repo))
|
|
345
|
+
let got = ''
|
|
346
|
+
try {
|
|
347
|
+
got = readFileSync(join(repo, MODULES_DOC), 'utf8')
|
|
348
|
+
} catch {
|
|
349
|
+
// 文件都不存在,没有行号可给——`line: 0` 会被渲染成只有文件名,不会退化成"第 1 行"。
|
|
350
|
+
return [{ rule: 'modules-doc-stale', file: MODULES_DOC, line: 0, detail: '台账不存在,跑 `npm run modules:sync` 生成' }]
|
|
351
|
+
}
|
|
352
|
+
// 只比结构指纹,不比逐字:表里全是会随「现在」变的量,逐字比对会让开发中途的 check 无故变红。
|
|
353
|
+
// **换行先归一**:Windows runner 检出时 `core.autocrlf` 会把 LF 变 CRLF,而 `renderModules` 现算的是 LF——
|
|
354
|
+
// 指纹里混进一个 `\r` 就整块不等,2026-11-30 的发布闸正是这么红的(本地绿、CI 红,同一条判据两份答案)。
|
|
355
|
+
const keyOf = (t) => (t.replace(/\r\n/g, '\n').match(/<!-- modules-key[^\n]*\n([\s\S]*?)\n-->/) || [, ''])[1]
|
|
356
|
+
if (keyOf(got) !== keyOf(want)) {
|
|
357
|
+
return [{ rule: 'modules-doc-stale', file: MODULES_DOC, line: lineOf(got, '<!-- modules-key'), detail: '台账与模块清单不一致(清单改了没重生成),跑 `npm run modules:sync`' }]
|
|
358
|
+
}
|
|
359
|
+
return []
|
|
360
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 变异算子与生成/应用(2026-11-27,3.5.4 从 tools/mutate.mjs 抽出)。
|
|
3
|
+
*
|
|
4
|
+
* 抽出的理由:`tools/mutate.mjs` 到 484 行,撞上结构异味阈值 400。
|
|
5
|
+
* 而这一段的边界是干净的——**纯函数,只依赖 `stripLiterals`**,不碰文件系统、不碰进程。
|
|
6
|
+
* 编排(副本、跑测试、报分)留在 mutate.mjs,算子在这里。
|
|
7
|
+
*/
|
|
8
|
+
import { stripLiterals } from './source-text.mjs'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 变异算子。每个算子:正则 + 替换规则。
|
|
12
|
+
*
|
|
13
|
+
* 全部作用在 `stripLiterals` 之后的文本上——否则 `'== 分布 =='` 这种字符串里的运算符
|
|
14
|
+
* 会被当成代码变异,跑出来的"存活"全是假的。去字面量保留长度与换行,
|
|
15
|
+
* 所以 `plain` 里的下标可以直接映射回原文件(下面有长度断言兜底)。
|
|
16
|
+
*/
|
|
17
|
+
const OPERATORS = [
|
|
18
|
+
{ id: 'comparison', re: /(?<![<>=!])(<=|>=|<|>)(?![=<>])/g, flip: { '<': '<=', '<=': '<', '>': '>=', '>=': '>' } },
|
|
19
|
+
{ id: 'equality', re: /(?<![=!<>])(===|!==)(?!=)/g, flip: { '===': '!==', '!==': '===' } },
|
|
20
|
+
{ id: 'boolean', re: /(?<![\w$.])(&&|\|\|)(?![\w$])/g, flip: { '&&': '||', '||': '&&' } },
|
|
21
|
+
{ id: 'literal-bool', re: /(?<![\w$.])(true|false)(?![\w$])/g, flip: { true: 'false', false: 'true' } },
|
|
22
|
+
{ id: 'nullish', re: /(?<![\w$.])\?\?(?![\w$])/g, flip: { '??': '||' } },
|
|
23
|
+
// 去掉一个 `!`:`if (!reason)` → `if (reason)`。闸门类代码里这条最能暴露"没人断言"。
|
|
24
|
+
// 捕获组不能省:`m[1]` 取的是 token,没有捕获组时是 undefined,整条算子会**静默失效**
|
|
25
|
+
// (这个 bug 是 `mutate.test.mjs` 抓出来的——自己写的算子也会犯"看起来跑了"的错)。
|
|
26
|
+
{ id: 'negation', re: /(?<![!=<>])(!)(?!=)/g, flip: { '!': '' } },
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
/** 生成变异体:{ op, index, token, replacement, line } */
|
|
30
|
+
export function generateMutants(src) {
|
|
31
|
+
const plain = stripLiterals(src)
|
|
32
|
+
if (plain.length !== src.length) {
|
|
33
|
+
throw new Error(`stripLiterals 改变了长度(${src.length} → ${plain.length}),下标无法映射,拒绝变异`)
|
|
34
|
+
}
|
|
35
|
+
const lineStarts = [0]
|
|
36
|
+
for (let i = 0; i < src.length; i += 1) if (src[i] === '\n') lineStarts.push(i + 1)
|
|
37
|
+
const lineOf = (idx) => {
|
|
38
|
+
let lo = 0
|
|
39
|
+
let hi = lineStarts.length - 1
|
|
40
|
+
while (lo < hi) {
|
|
41
|
+
const mid = (lo + hi + 1) >> 1
|
|
42
|
+
if (lineStarts[mid] <= idx) lo = mid
|
|
43
|
+
else hi = mid - 1
|
|
44
|
+
}
|
|
45
|
+
return lo + 1
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const out = []
|
|
49
|
+
for (const op of OPERATORS) {
|
|
50
|
+
for (const m of plain.matchAll(op.re)) {
|
|
51
|
+
const token = m[1]
|
|
52
|
+
const replacement = op.flip[token]
|
|
53
|
+
if (replacement === undefined) continue
|
|
54
|
+
out.push({ op: op.id, index: m.index, token, replacement, line: lineOf(m.index) })
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return out.sort((a, b) => a.index - b.index)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** 把第 i 个变异体应用到源码上,返回新源码。 */
|
|
61
|
+
export function applyMutant(src, mutant) {
|
|
62
|
+
return src.slice(0, mutant.index) + mutant.replacement + src.slice(mutant.index + mutant.token.length)
|
|
63
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 积木文件名解析(tools/lib/parse-block.mjs,2026-10-31 建,2026-11-29 降级)
|
|
3
|
+
*
|
|
4
|
+
* 这是**纯函数库**,不写盘、不产索引。调用方只有 `knowledge.mjs`。
|
|
5
|
+
*
|
|
6
|
+
* ## 为什么从「索引工具」降成「解析库」
|
|
7
|
+
*
|
|
8
|
+
* 原文件(`tools/library-index.mjs`)会扫 `.ai/output/library` 落一份 `index.json`。
|
|
9
|
+
* 2026-11-29 实测:`knowledge.json` 里 `block:*` 条目的字段是
|
|
10
|
+
* `{ ns, id, file, grade, topic, source, chars, path }`,而 `index.json` 的条目是
|
|
11
|
+
* `{ id, file, grade, topic, source, chars }`——**知识索引是它的严格超集**,
|
|
12
|
+
* 后者不再被任何代码读取。
|
|
13
|
+
*
|
|
14
|
+
* 而 `library-index.mjs` 的首版注释里自己写着:「两套索引源 = 两个真相源,那是 3.3 修过的病」。
|
|
15
|
+
* 它防住了「内容索引 vs 文件名索引」,却没防住自己与 `knowledge` 撞车——因为两个工具
|
|
16
|
+
* 扫的是同一个目录。所以不是再抽一层,是把其中一个的职责收窄到它唯一还被需要的那一半:
|
|
17
|
+
* `parseBlock`——`knowledge` 复用它的解析,不另写一套。
|
|
18
|
+
*
|
|
19
|
+
* 目录 `INDEX.md`(人读的清单)仍由 `library-build.mjs` 生成,与本文件无关。
|
|
20
|
+
*
|
|
21
|
+
* ## 命名规律(实测 28 块)
|
|
22
|
+
*
|
|
23
|
+
* B001-s-级-改改措辞就能进-rules-注入前缀.md 分级结论:s/a/b 级
|
|
24
|
+
* B006-核心机制-anthropic.md 主题 + 来源
|
|
25
|
+
*
|
|
26
|
+
* 认不出的写法不猜:整段原样进 `topic`,`grade`/`source` 留 null。
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
const SOURCES = ['anthropic', 'google', 'misc', 'openai', 'others', 'xai']
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 「什么算一块积木」:`B001-….md`。
|
|
33
|
+
*
|
|
34
|
+
* `knowledge.mjs` 的源过滤与 `lib/index-registry.mjs` 的落后检查都用这一条,
|
|
35
|
+
* 不各写一份——2026-11-29 之前那里是 `/^B\d{3}-.+\.md$/` 的副本,
|
|
36
|
+
* 改命名规则时必有一处漏改。
|
|
37
|
+
*/
|
|
38
|
+
export const BLOCK_RE = /^B(\d{3})-(.+)\.md$/
|
|
39
|
+
|
|
40
|
+
/** 文件名 → 索引项。认不出的一律不猜,整段当 topic。 */
|
|
41
|
+
export function parseBlock(file, chars) {
|
|
42
|
+
const m = BLOCK_RE.exec(file)
|
|
43
|
+
if (!m) return null
|
|
44
|
+
const [, num, rest] = m
|
|
45
|
+
const item = { id: `B${num}`, file, grade: null, topic: rest, source: null, chars }
|
|
46
|
+
const graded = /^([sab])-级-(.+)$/.exec(rest)
|
|
47
|
+
if (graded) {
|
|
48
|
+
item.grade = graded[1]
|
|
49
|
+
item.topic = graded[2]
|
|
50
|
+
return item
|
|
51
|
+
}
|
|
52
|
+
const tail = new RegExp(`^(.+)-(${SOURCES.join('|')})$`).exec(rest)
|
|
53
|
+
if (tail) {
|
|
54
|
+
item.topic = tail[1]
|
|
55
|
+
item.source = tail[2]
|
|
56
|
+
}
|
|
57
|
+
return item
|
|
58
|
+
}
|
|
@@ -20,9 +20,10 @@ import { calcCost } from '../../src/peak-cost.mjs'
|
|
|
20
20
|
export function runCost({ sessionsDir, limit = 8 } = {}) {
|
|
21
21
|
const TOK = (chars) => Math.round(chars / 4)
|
|
22
22
|
const rows = []
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
// 2026-11-20 修选样:原先先 `slice(0, limit)` 再筛「≥5 步」,等于只在前 8 个文件里碰运气——
|
|
24
|
+
// 而落盘目录里靠前的是 hermes-loop-review 那批短会话,结果恒报「找不到 ≥5 步的会话」。
|
|
25
|
+
// 现在遍历全部、合格才计数,够 `limit` 个就停。与 `prefix-audit` 的选会话口径对齐。
|
|
26
|
+
for (const cand of walkSessionFiles(sessionsDir).filter((x) => x.size < 3 * 1024 * 1024)) {
|
|
26
27
|
let lines
|
|
27
28
|
try {
|
|
28
29
|
lines = decodeSession(cand.f)
|
|
@@ -83,6 +84,7 @@ export function runCost({ sessionsDir, limit = 8 } = {}) {
|
|
|
83
84
|
beforeTotal: before + after,
|
|
84
85
|
afterTotal: after,
|
|
85
86
|
})
|
|
87
|
+
if (rows.length >= limit) break
|
|
86
88
|
}
|
|
87
89
|
return rows
|
|
88
90
|
}
|
package/tools/lib/ratchet.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { existsSync, readFileSync } from 'node:fs'
|
|
2
2
|
import { execFileSync } from 'node:child_process'
|
|
3
3
|
import { join } from 'node:path'
|
|
4
|
+
import { lineOf } from './where.mjs'
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* 存量棘轮台账(3.3 B,2026-10-30)
|
|
@@ -19,9 +20,9 @@ import { join } from 'node:path'
|
|
|
19
20
|
* ① 实测 ≤ 本版记账值 —— 防反弹(台账写完又被改坏)
|
|
20
21
|
* ② 本版总和 < 上一版总和 —— 每版至少还 1 条
|
|
21
22
|
* ③ 首版免 ② —— 总得有个起点
|
|
22
|
-
* ④ 自上次记账起 ≥10 次提交仍未降 —— 还款期限(2026-10-30
|
|
23
|
-
*
|
|
24
|
-
*
|
|
23
|
+
* ④ 自上次记账起 ≥10 次提交仍未降 —— 还款期限(2026-10-30 定:每 N 次提交必须降 1)。
|
|
24
|
+
* 按版本记账的问题是:版本可以很久不发,于是 8/8 能一直挂着不动。
|
|
25
|
+
* 改成按提交数催,才管得住「不发版就不还债」。
|
|
25
26
|
*
|
|
26
27
|
* ## 为什么不塞进 smells.mjs
|
|
27
28
|
*
|
|
@@ -32,6 +33,9 @@ import { join } from 'node:path'
|
|
|
32
33
|
/** 还款期限:自上次记账 commit 起,多少提交内必须还 1 条 */
|
|
33
34
|
export const COMMITS_PER_PAYBACK = 10
|
|
34
35
|
|
|
36
|
+
/** 台账文件(报点用):四条规则红的时候,人要改的都是这张表里的某一行。 */
|
|
37
|
+
export const LEDGER_FILE = 'packages/core/docs/RATCHET.json'
|
|
38
|
+
|
|
35
39
|
/**
|
|
36
40
|
* 自某 commit 起的提交数。取不到就返回 null(浅克隆 / 非 git 仓库 / 无该 sha)。
|
|
37
41
|
* 返回 null 时规则④跳过——**缺数据允许粗判,但必须在输出里写明**,不能假装查过。
|
|
@@ -147,26 +151,36 @@ export function paybackNote(check) {
|
|
|
147
151
|
*/
|
|
148
152
|
export function loadRatchet(repo, actual) {
|
|
149
153
|
const pkgPath = join(repo, 'package.json')
|
|
150
|
-
const ledgerPath = join(repo,
|
|
154
|
+
const ledgerPath = join(repo, LEDGER_FILE)
|
|
151
155
|
if (!existsSync(pkgPath)) return { findings: [], entry: null, total: null, skipped: '无 package.json(夹具仓库)' }
|
|
152
156
|
if (!existsSync(ledgerPath)) {
|
|
153
|
-
return { findings: [{ rule: 'no-ledger', detail: '台账 RATCHET.json 缺失' }], entry: null, total: null }
|
|
157
|
+
return { findings: [{ rule: 'no-ledger', file: LEDGER_FILE, line: 0, detail: '台账 RATCHET.json 缺失' }], entry: null, total: null }
|
|
154
158
|
}
|
|
155
159
|
try {
|
|
156
160
|
const version = JSON.parse(readFileSync(pkgPath, 'utf8')).version
|
|
157
|
-
const
|
|
161
|
+
const text = readFileSync(ledgerPath, 'utf8')
|
|
162
|
+
const ledger = JSON.parse(text)
|
|
158
163
|
const history = Array.isArray(ledger.history) ? ledger.history : []
|
|
159
164
|
const last = history.length ? history[history.length - 1] : null
|
|
160
165
|
const n = commitsSince(repo, last && last.commit)
|
|
166
|
+
const result = checkRatchet(ledger, version, actual, { commitsSinceLast: n })
|
|
167
|
+
// 报点:红的是「本版那一行」还是「整张表缺一条」——前者能直接跳过去改数字,后者只能指回 history 开头。
|
|
168
|
+
// checkRatchet 本身保持纯函数(测试直接喂对象),定位只在这一层做。
|
|
169
|
+
const versionLine = lineOf(text, `"version": "${version}"`)
|
|
170
|
+
const historyLine = lineOf(text, '"history"')
|
|
171
|
+
for (const f of result.findings) {
|
|
172
|
+
f.file = LEDGER_FILE
|
|
173
|
+
f.line = f.rule === 'no-ledger-entry' ? historyLine : versionLine || historyLine
|
|
174
|
+
}
|
|
161
175
|
return {
|
|
162
|
-
...
|
|
176
|
+
...result,
|
|
163
177
|
version,
|
|
164
178
|
// 取不到提交数时明说「只能粗判」——假装精确比承认不确定更糟
|
|
165
179
|
commitsKnown: n !== null,
|
|
166
180
|
}
|
|
167
181
|
} catch (error) {
|
|
168
182
|
return {
|
|
169
|
-
findings: [{ rule: 'ledger-unreadable', detail: String(error && error.message) }],
|
|
183
|
+
findings: [{ rule: 'ledger-unreadable', file: LEDGER_FILE, line: 0, detail: String(error && error.message) }],
|
|
170
184
|
entry: null,
|
|
171
185
|
total: null,
|
|
172
186
|
}
|