focus-guard 3.5.3 → 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 +2 -2
- package/README.zh-CN.md +1 -1
- package/assets/NOTICE.md +1 -1
- package/docs/ARCH-MAP.md +298 -83
- package/docs/BENCH.md +10 -4
- package/docs/RELEASE-POLICY.md +25 -12
- package/docs/RULES.md +34 -16
- package/docs/THREAT-MODEL.md +3 -3
- package/docs/WRITING-STYLE.md +40 -2
- package/package.json +1 -1
- package/skills/focus-rules/SKILL.md +9 -9
- package/skills/focus-thinking/SKILL.md +3 -3
- package/src/adapters/dsh/eligibility-gate.mjs +32 -12
- package/src/{dsh → adapters/dsh}/toolName.mjs +6 -0
- package/src/core/auditActions.mjs +214 -0
- package/src/core/checkEligibility.mjs +3 -1
- package/src/core/constants.mjs +8 -4
- package/src/core/env.mjs +21 -4
- package/src/core/profileLoader.mjs +96 -3
- package/src/core/scratch.mjs +288 -0
- package/src/core/state.mjs +3 -3
- package/src/dsh/audit.mjs +24 -163
- package/src/dsh/cost-ledger.mjs +174 -0
- package/src/dsh/guard/authz.mjs +1 -1
- package/src/dsh/guard/evidence.mjs +19 -0
- package/src/dsh/guard/tooling.mjs +15 -4
- package/src/dsh/{value-gate.mjs → guard/value-gate.mjs} +6 -0
- package/src/dsh/index.js +1 -1
- package/src/dsh/pipeline.mjs +6 -3
- package/src/dsh/postProgress.mjs +81 -40
- package/src/dsh/preGuard.mjs +1 -1
- 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 +12 -6
- package/src/dsh/seams/session-start.mjs +14 -0
- package/src/dsh/tools.mjs +1 -1
- package/src/memory/index.mjs +3 -1
- 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/docs-drift.mjs +7 -29
- package/tools/docs-lint.mjs +54 -11
- package/tools/docs-refs.mjs +343 -0
- package/tools/handoff.mjs +1 -1
- package/tools/index-stale.mjs +124 -0
- package/tools/knowledge.mjs +53 -4
- package/tools/lib/book-style.mjs +42 -1
- package/tools/lib/case-tool.mjs +1 -1
- package/tools/lib/cli.mjs +35 -0
- package/tools/lib/docs-ack.mjs +4 -2
- package/tools/lib/docs-core.mjs +46 -23
- package/tools/lib/docs-scope.mjs +46 -0
- package/tools/lib/docs-status.mjs +8 -3
- 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/index-io.mjs +4 -4
- package/tools/lib/index-registry.mjs +101 -0
- package/tools/lib/modules-doc.mjs +8 -1
- package/tools/lib/modules-list.mjs +22 -8
- package/tools/lib/modules.mjs +57 -9
- package/tools/lib/mutation-ops.mjs +63 -0
- package/tools/lib/parse-block.mjs +58 -0
- 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-build.mjs +15 -3
- package/tools/lint.mjs +19 -13
- package/tools/log-audit.mjs +76 -0
- package/tools/memory-distill.mjs +14 -1
- package/tools/modules-audit.mjs +11 -9
- package/tools/mutate.mjs +120 -74
- package/tools/prefix-drift.mjs +1 -1
- package/tools/registry-check.mjs +16 -10
- package/tools/release.mjs +54 -106
- package/tools/smells.mjs +97 -139
- package/src/core/staleCleanup.mjs +0 -224
- package/src/dsh/workspace.mjs +0 -5
- package/tools/library-index.mjs +0 -91
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 抽样汇总与分片派发(2026-11-27,3.5.4 从 tools/mutate.mjs 抽出)。
|
|
3
|
+
*
|
|
4
|
+
* `self` 与 `repo` 走参数:搬进 lib/ 之后 `import.meta.url` 指向 lib 自己,
|
|
5
|
+
* 而子进程必须重启**入口脚本**,不能指向这里。
|
|
6
|
+
*/
|
|
7
|
+
import { spawn } from "node:child_process"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 起 `jobs` 个分片子进程,合并结果。
|
|
11
|
+
*
|
|
12
|
+
* 为什么不是 worker_threads:被测的是子进程(`node --test`),线程里再 spawn 进程
|
|
13
|
+
* 没有任何收益,反而多一层共享状态。
|
|
14
|
+
*/
|
|
15
|
+
export async function surveyParallel({ jobs, self, repo }) {
|
|
16
|
+
const runs = Array.from({ length: jobs }, (_, i) => {
|
|
17
|
+
return new Promise((resolve) => {
|
|
18
|
+
const cp = spawn('node', [self, '--survey', '--shard', `${i}/${jobs}`], {
|
|
19
|
+
cwd: repo,
|
|
20
|
+
env: process.env,
|
|
21
|
+
})
|
|
22
|
+
let out = ''
|
|
23
|
+
cp.stdout.on('data', (d) => {
|
|
24
|
+
out += d
|
|
25
|
+
})
|
|
26
|
+
cp.stderr.on('data', (d) => process.stderr.write(d))
|
|
27
|
+
cp.on('close', () => {
|
|
28
|
+
const m = out.match(/__ROWS__([\s\S]*)$/)
|
|
29
|
+
if (!m) {
|
|
30
|
+
process.stderr.write(`[survey] 分片 ${i} 无结果\n`)
|
|
31
|
+
resolve([])
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
resolve(JSON.parse(m[1]))
|
|
36
|
+
} catch {
|
|
37
|
+
resolve([])
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
const rows = (await Promise.all(runs)).flat()
|
|
43
|
+
reportSurvey(rows)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 表格按得分升序:最薄的排最前,那才是要动手的地方。 */
|
|
47
|
+
export function reportSurvey(rows) {
|
|
48
|
+
const scored = rows.filter((r) => typeof r.score === 'number').sort((a, b) => a.score - b.score)
|
|
49
|
+
const skipped = rows.filter((r) => r.skipped)
|
|
50
|
+
console.log('\n模块 变异体 杀掉 得分')
|
|
51
|
+
for (const r of scored) {
|
|
52
|
+
console.log(`${r.rel.padEnd(38)} ${String(r.total).padStart(5)} ${String(r.killed).padStart(5)} ${String(r.score).padStart(6)}%`)
|
|
53
|
+
}
|
|
54
|
+
const avg = scored.length ? scored.reduce((a, r) => a + r.score, 0) / scored.length : 0
|
|
55
|
+
console.log(`\n可测模块 ${scored.length} 个,平均得分 ${avg.toFixed(1)}%;跳过 ${skipped.length} 个`)
|
|
56
|
+
for (const r of skipped) console.log(` 跳过 ${r.rel}:${r.skipped.split(',')[0].slice(0, 50)}`)
|
|
57
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 报点定位(tools/lib/where.mjs,2026-11-30)——工具链共用件,零依赖。
|
|
3
|
+
*
|
|
4
|
+
* 为什么单独抽出来:闸门报「哪个文件第几行」时,全仓有 20 处 finding 直接把 `line: 1` 写死。
|
|
5
|
+
* 那不是一个位置,是「文件第一行」——看着像定位,实际把人送到错误的地方。
|
|
6
|
+
* Python 的 traceback 之所以可信,是因为它给的每一行都是真的、可跳转的;
|
|
7
|
+
* **报点一旦有假,人就会学会不看报点**,闸门也就只剩下一个 exit code。
|
|
8
|
+
*
|
|
9
|
+
* 三条纪律:
|
|
10
|
+
* ① 找不到真行号就返回 0,**不拿 1 顶替**;
|
|
11
|
+
* ② 渲染时 line<=0 只给文件名(`path/file.mjs`),不给 `:0`,也不给 `:1`;
|
|
12
|
+
* ③ 行号一律 1-based,与编辑器、编译器、`grep -n` 一致。
|
|
13
|
+
*
|
|
14
|
+
* `line: 0` 有且只有一个意思:**病灶是整份文件**(超长、格式坏了、文件还没建),
|
|
15
|
+
* 不是"某个想不起来的行"。所以它合法地存在,只是不冒充一个具体位置。
|
|
16
|
+
*
|
|
17
|
+
* 反向约束:本模块只做「文件 + 行号 + 那一行原文」,**不做语义判断**。
|
|
18
|
+
* 判据各自知道自己要找什么(模块 id、路径字面量、`<!-- key -->`),
|
|
19
|
+
* 定位是纯机械的事,混进来就会变成第二个判据。
|
|
20
|
+
*/
|
|
21
|
+
import { readFileSync } from 'node:fs'
|
|
22
|
+
import { isAbsolute, join } from 'node:path'
|
|
23
|
+
|
|
24
|
+
/** 原文预览的截断长度:够看清写的是什么,又不至于把终端撑爆。 */
|
|
25
|
+
const SRC_MAX = 120
|
|
26
|
+
|
|
27
|
+
/** 字节偏移 → 1-based 行号。 */
|
|
28
|
+
export function lineAt(text, offset) {
|
|
29
|
+
if (!(offset >= 0)) return 0
|
|
30
|
+
let line = 1
|
|
31
|
+
for (let i = 0; i < offset && i < text.length; i += 1) if (text[i] === '\n') line += 1
|
|
32
|
+
return line
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 第一次出现的位置所在的**行号**;`needle` 可为字符串或正则。
|
|
37
|
+
* 找不到返回 0——这是本模块与旧写法最根本的区别:宁可没有,也不给假值。
|
|
38
|
+
*/
|
|
39
|
+
export function lineOf(text, needle) {
|
|
40
|
+
if (typeof text !== 'string' || !text) return 0
|
|
41
|
+
if (needle instanceof RegExp) {
|
|
42
|
+
const re = needle.global ? needle : new RegExp(needle.source, `${needle.flags}g`)
|
|
43
|
+
const m = re.exec(text)
|
|
44
|
+
return m ? lineAt(text, m.index) : 0
|
|
45
|
+
}
|
|
46
|
+
const k = text.indexOf(needle)
|
|
47
|
+
return k < 0 ? 0 : lineAt(text, k)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 所有出现处的行号(去重、保序)。
|
|
52
|
+
*
|
|
53
|
+
* 为什么需要"第 n 次":同一个路径字面量被两个模块登记时,重叠判据要报**后来那张**,
|
|
54
|
+
* 报第一张等于指回没错的那一行。
|
|
55
|
+
*/
|
|
56
|
+
export function linesOf(text, needle) {
|
|
57
|
+
if (typeof text !== 'string' || !text) return []
|
|
58
|
+
const seen = new Set()
|
|
59
|
+
const out = []
|
|
60
|
+
if (needle instanceof RegExp) {
|
|
61
|
+
const re = needle.global ? needle : new RegExp(needle.source, `${needle.flags}g`)
|
|
62
|
+
let m
|
|
63
|
+
while ((m = re.exec(text))) {
|
|
64
|
+
const line = lineAt(text, m.index)
|
|
65
|
+
if (!seen.has(line)) out.push(line)
|
|
66
|
+
seen.add(line)
|
|
67
|
+
if (!m[0]) re.lastIndex += 1 // 空匹配会原地打转
|
|
68
|
+
}
|
|
69
|
+
return out
|
|
70
|
+
}
|
|
71
|
+
let from = 0
|
|
72
|
+
for (;;) {
|
|
73
|
+
const k = text.indexOf(needle, from)
|
|
74
|
+
if (k < 0) break
|
|
75
|
+
const line = lineAt(text, k)
|
|
76
|
+
if (!seen.has(line)) out.push(line)
|
|
77
|
+
seen.add(line)
|
|
78
|
+
from = k + Math.max(1, needle.length)
|
|
79
|
+
}
|
|
80
|
+
return out
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** 取第 n 行的原文(去首尾空白、超长截断);越界或空行返回 ''。 */
|
|
84
|
+
export function sourceLine(text, line) {
|
|
85
|
+
if (typeof text !== 'string' || !(line > 0)) return ''
|
|
86
|
+
const row = text.split('\n')[line - 1]
|
|
87
|
+
if (row == null) return ''
|
|
88
|
+
const t = row.trim()
|
|
89
|
+
return t.length > SRC_MAX ? `${t.slice(0, SRC_MAX)}…` : t
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** `file:line`;行号未知时只给文件名。**永不输出 `:0` 或替身 `:1`。** */
|
|
93
|
+
export function where(file, line) {
|
|
94
|
+
const f = String(file || '').replace(/\\/g, '/')
|
|
95
|
+
return line > 0 ? `${f}:${line}` : f
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 正则转义:版本号、模块 id 里的 `.` `(` 不转义会匹配任意字符,定位就漂到别处去了。
|
|
100
|
+
*
|
|
101
|
+
* 抽出来是因为已经有第二处手写同一串 `replace(/[.*+?^${}()|[\]\\]/g, '\\$&')`——
|
|
102
|
+
* 那种式子抄第二遍时最常漏掉 `]` 或 `\\`,而漏掉之后不报错,只是偶尔定位失灵。
|
|
103
|
+
*/
|
|
104
|
+
export function escapeRe(s) {
|
|
105
|
+
return String(s).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 三段一起取,省得调用方自己 split 一遍。 */
|
|
109
|
+
export function locate(file, text, needle) {
|
|
110
|
+
const line = lineOf(text, needle)
|
|
111
|
+
return { file, line, src: sourceLine(text, line) }
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 读文件并取出第 line 行的原文——供 `--where` 显示"出错的那一行"。
|
|
116
|
+
* 读不到就返回 '':报点失败不该让闸门自己崩掉(那是二级故障,不该盖住一级问题)。
|
|
117
|
+
*/
|
|
118
|
+
export function sourceOf(file, line, root) {
|
|
119
|
+
if (!file || !(line > 0)) return ''
|
|
120
|
+
try {
|
|
121
|
+
const abs = isAbsolute(file) ? file : join(root || '.', file)
|
|
122
|
+
return sourceLine(readFileSync(abs, 'utf8'), line)
|
|
123
|
+
} catch {
|
|
124
|
+
return ''
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* 单条 finding 的渲染,全仓闸门统一走这里。
|
|
130
|
+
*
|
|
131
|
+
* 默认一行:`file:line\t[rule] detail`——沿用既有格式,改动只在"行号变真"。
|
|
132
|
+
* `withSource` 时追加一行缩进原文,像 traceback 那样把上下文摆在眼前。
|
|
133
|
+
*/
|
|
134
|
+
export function renderFinding(f, { root, withSource = false } = {}) {
|
|
135
|
+
const head = `${where(f.file, f.line)}\t[${f.rule || f.kind}] ${f.detail}`
|
|
136
|
+
if (!withSource) return head
|
|
137
|
+
const src = sourceOf(f.file, f.line, root)
|
|
138
|
+
return src ? `${head}\n │ ${src}` : head
|
|
139
|
+
}
|
package/tools/library-build.mjs
CHANGED
|
@@ -162,6 +162,11 @@ function main() {
|
|
|
162
162
|
const rows = []; // {id,file,title,sha,src,status}
|
|
163
163
|
const today = new Date().toISOString();
|
|
164
164
|
const normSep = (s) => String(s).replace(/\\/g, "/"); // 幂等配对与平台无关(分隔符归一)
|
|
165
|
+
// 按内容指纹建索引一次(2026-11-28):原实现每块都在 `index.rows.find(...)` 里线性扫一遍,
|
|
166
|
+
// 块数 × 存量行数是 O(n²)。同 sha 取首条——与 `find` 语义一致,且 `index.rows` 在本次
|
|
167
|
+
// 构建过程中不变(新块进的是 `rows`),所以一次性建表与原逐块查找完全等价。
|
|
168
|
+
const bySha = new Map();
|
|
169
|
+
for (const v of index.rows) if (!bySha.has(v.sha)) bySha.set(v.sha, v);
|
|
165
170
|
for (const f of sources) {
|
|
166
171
|
let text;
|
|
167
172
|
try { text = readFileSync(f, "utf8"); } catch { continue; }
|
|
@@ -171,7 +176,7 @@ function main() {
|
|
|
171
176
|
for (const b of pieces) {
|
|
172
177
|
const fp = sha(b.title + "\n" + b.body);
|
|
173
178
|
// 幂等:同内容指纹的积木已存在 → 复用旧 id 与文件(重建不换号;按内容配对,与源路径写法无关)
|
|
174
|
-
const dup =
|
|
179
|
+
const dup = bySha.get(fp);
|
|
175
180
|
if (dup) {
|
|
176
181
|
kept++;
|
|
177
182
|
rows.push({ id: dup.id, file: dup.file, title: b.title, sha: fp, src: rel, status: "保留" });
|
|
@@ -220,10 +225,17 @@ function buildTable(rows) {
|
|
|
220
225
|
"|---|---|---|---|---|---|",
|
|
221
226
|
].join("\n");
|
|
222
227
|
const groups = [];
|
|
228
|
+
// 组名 → 组(2026-11-28):原实现每行 `groups.find(...)` 线性找组,O(行×组)。
|
|
229
|
+
// Map 保留插入序,与原「首次出现即定序」一致;随后的 sort 仍按 themeOrder 定终序。
|
|
230
|
+
const byName = new Map();
|
|
223
231
|
for (const r of rows) {
|
|
224
232
|
const name = themeOf(r.title, r.src);
|
|
225
|
-
let g =
|
|
226
|
-
if (!g)
|
|
233
|
+
let g = byName.get(name);
|
|
234
|
+
if (!g) {
|
|
235
|
+
g = { name, rows: [] };
|
|
236
|
+
byName.set(name, g);
|
|
237
|
+
groups.push(g);
|
|
238
|
+
}
|
|
227
239
|
g.rows.push(r);
|
|
228
240
|
}
|
|
229
241
|
const themeOrder = [...SRC_THEMES.map(([n]) => n), ...THEMES.map(([n]) => n), UNGROUPED];
|
package/tools/lint.mjs
CHANGED
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
* 用法:
|
|
29
29
|
* node packages/core/tools/lint.mjs # 明细
|
|
30
30
|
* node packages/core/tools/lint.mjs --brief # 一行(供 npm run check)
|
|
31
|
+
* node packages/core/tools/lint.mjs --where # 每条问题附上出错那一行原文
|
|
31
32
|
* node packages/core/tools/lint.mjs --json
|
|
32
33
|
* node packages/core/tools/lint.mjs --strict # 超债务上限 exit 1
|
|
33
34
|
* node packages/core/tools/lint.mjs <目录...> # 只扫指定目录
|
|
@@ -42,25 +43,22 @@
|
|
|
42
43
|
*/
|
|
43
44
|
|
|
44
45
|
import { readFileSync } from 'node:fs'
|
|
45
|
-
import {
|
|
46
|
-
import { fileURLToPath } from 'node:url'
|
|
46
|
+
import { relative, resolve } from 'node:path'
|
|
47
47
|
// 去字面量与注释:与 smells.mjs 共用同一实现(2026-10-27 抽到 tools/lib/)。
|
|
48
48
|
// 各写一份必然分叉——smells 那份就漏了正则字面量,把 398 行的函数抹成 0。
|
|
49
49
|
import { stripLiterals } from './lib/source-text.mjs'
|
|
50
50
|
// 目录遍历复用 lib 里那一份:带 SKIP_DIRS 的版本曾在 lint / smells 各抄一遍(2026-11-17 收成参数)。
|
|
51
51
|
import { walk } from './lib/import-graph.mjs'
|
|
52
52
|
import { scanIdioms, IDIOMS } from './lib/idioms.mjs'
|
|
53
|
+
import { renderFinding } from './lib/where.mjs'
|
|
54
|
+
import { has, positionals, repoRoot } from './lib/cli.mjs'
|
|
53
55
|
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
const BRIEF = argv.includes('--brief')
|
|
61
|
-
const AS_JSON = argv.includes('--json')
|
|
62
|
-
const STRICT = argv.includes('--strict')
|
|
63
|
-
const dirArgs = argv.filter((a) => !a.startsWith('--'))
|
|
56
|
+
const REPO = repoRoot(import.meta.url)
|
|
57
|
+
const BRIEF = has('--brief')
|
|
58
|
+
const AS_JSON = has('--json')
|
|
59
|
+
const WHERE = has('--where')
|
|
60
|
+
const STRICT = has('--strict')
|
|
61
|
+
const dirArgs = positionals()
|
|
64
62
|
|
|
65
63
|
const DEFAULT_ROOTS = ['packages/core/src', 'packages/core/tools', 'packages/core/tests', 'packages/core/hooks', 'packages/extended/src']
|
|
66
64
|
const roots = (dirArgs.length ? dirArgs : DEFAULT_ROOTS).map((d) => resolve(REPO, d))
|
|
@@ -72,6 +70,9 @@ const roots = (dirArgs.length ? dirArgs : DEFAULT_ROOTS).map((d) => resolve(REPO
|
|
|
72
70
|
const BUDGET = 0
|
|
73
71
|
const GATED = /^packages\/(core\/(src|tools|tests)|extended\/src)\//
|
|
74
72
|
|
|
73
|
+
/** brief 超限时逐条点名的上限。 */
|
|
74
|
+
const BRIEF_SHOW = 8
|
|
75
|
+
|
|
75
76
|
const SKIP_DIRS = new Set(['node_modules', '.git', '.stryker-tmp', '.mutate-tmp', 'reports', '.ai'])
|
|
76
77
|
|
|
77
78
|
/**
|
|
@@ -255,11 +256,16 @@ if (AS_JSON) {
|
|
|
255
256
|
.map(([r, n]) => `${r} ${n}`)
|
|
256
257
|
const gate = gated.length > BUDGET ? `活跃层 ${gated.length} 条 > 上限 ${BUDGET} ❌` : `活跃层 ${gated.length}/${BUDGET} ✅`
|
|
257
258
|
console.log(`房规检查:${files.length} 文件,问题 ${findings.length} 条${parts.length ? `(${parts.join(' / ')})` : ''};${gate}${frozen ? `,封存层 ${frozen} 条不计` : ''}`)
|
|
259
|
+
// 超限就把活跃层 offenders 就地列出来:CI 上看到"1 条 > 上限 0"却不知道是哪一行,等于又跑一趟。
|
|
260
|
+
if (gated.length > BUDGET) {
|
|
261
|
+
for (const f of gated.slice(0, BRIEF_SHOW)) console.log(` ${renderFinding(f, { root: REPO, withSource: WHERE })}`)
|
|
262
|
+
if (gated.length > BRIEF_SHOW) console.log(` …另 ${gated.length - BRIEF_SHOW} 条,跑 npm run lint 看全量`)
|
|
263
|
+
}
|
|
258
264
|
} else {
|
|
259
265
|
console.log(`房规检查:${files.length} 文件,问题 ${findings.length} 条\n`)
|
|
260
266
|
if (!findings.length) console.log('无问题。')
|
|
261
267
|
for (const f of findings.sort((a, b) => a.file.localeCompare(b.file) || a.line - b.line)) {
|
|
262
|
-
console.log(
|
|
268
|
+
console.log(renderFinding(f, { root: REPO, withSource: WHERE }))
|
|
263
269
|
}
|
|
264
270
|
console.log('\n不报的三类(各有理由):curly 房规 / `== null` 惯用法 / 空 catch 三级兜底。')
|
|
265
271
|
console.log(`闸门口径:活跃层 ${gated.length} 条 / 上限 ${BUDGET};封存层 ${frozen} 条(hooks/)只报不计。`)
|
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-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" +
|
package/tools/modules-audit.mjs
CHANGED
|
@@ -13,14 +13,14 @@
|
|
|
13
13
|
* node packages/core/tools/modules-audit.mjs --json # 机器读
|
|
14
14
|
* node packages/core/tools/modules-audit.mjs --sync # 写 docs/MODULES.md
|
|
15
15
|
* node packages/core/tools/modules-audit.mjs --check-doc # 台账是否落后
|
|
16
|
+
* node packages/core/tools/modules-audit.mjs --where # 报点旁附上出错那一行原文
|
|
16
17
|
* node packages/core/tools/modules-audit.mjs --strict # 清单有缺陷时 exit 1
|
|
17
18
|
*
|
|
18
19
|
* 退出码:默认只在 `--strict` 下因清单缺陷(路径缺失/重叠/未归属/id 重复)非零;
|
|
19
20
|
* 「久未动」本身**永不**导致非零——那是信息,不是错误。
|
|
20
21
|
*/
|
|
21
22
|
import { writeFileSync } from 'node:fs'
|
|
22
|
-
import {
|
|
23
|
-
import { fileURLToPath } from 'node:url'
|
|
23
|
+
import { join } from 'node:path'
|
|
24
24
|
import {
|
|
25
25
|
MODULES,
|
|
26
26
|
MODULES_DOC,
|
|
@@ -33,17 +33,17 @@ import {
|
|
|
33
33
|
checkModuleCoverage,
|
|
34
34
|
checkModulesDoc,
|
|
35
35
|
} from './lib/modules.mjs'
|
|
36
|
+
import { renderFinding } from './lib/where.mjs'
|
|
37
|
+
import { has, repoRoot } from './lib/cli.mjs'
|
|
36
38
|
|
|
37
|
-
const
|
|
38
|
-
const
|
|
39
|
-
const argv = process.argv.slice(2)
|
|
40
|
-
const has = (k) => argv.includes(k)
|
|
39
|
+
const ROOT = repoRoot(import.meta.url)
|
|
40
|
+
const WHERE = has('--where')
|
|
41
41
|
|
|
42
42
|
const data = collectModules(ROOT, MODULES)
|
|
43
43
|
const coverage = collectCoverage(ROOT, MODULES)
|
|
44
44
|
const problems = [
|
|
45
45
|
...checkModulePaths(ROOT, MODULES),
|
|
46
|
-
...checkModuleIds(MODULES),
|
|
46
|
+
...checkModuleIds(ROOT, MODULES),
|
|
47
47
|
...checkModuleOverlap(ROOT, MODULES),
|
|
48
48
|
...checkModuleCoverage(ROOT, MODULES),
|
|
49
49
|
]
|
|
@@ -64,7 +64,8 @@ const dirty = data.rows.filter((r) => r.dirty)
|
|
|
64
64
|
|
|
65
65
|
if (has('--check-doc')) {
|
|
66
66
|
const docFindings = checkModulesDoc(ROOT)
|
|
67
|
-
|
|
67
|
+
// 带上位置:这条闸门红的时候,人要去改的是 `docs/MODULES.md` 的指纹块,不是整个文件。
|
|
68
|
+
process.stdout.write(docFindings.length ? `[modules] ${renderFinding(docFindings[0], { root: ROOT, withSource: WHERE })}\n` : '[modules] 台账与现算一致\n')
|
|
68
69
|
process.exit(docFindings.length ? 1 : 0)
|
|
69
70
|
}
|
|
70
71
|
|
|
@@ -104,5 +105,6 @@ if (has('--table')) {
|
|
|
104
105
|
)
|
|
105
106
|
}
|
|
106
107
|
|
|
107
|
-
|
|
108
|
+
// 报点带 `file:line`(此前只有 rule + detail,一条命令跳到文件开头等于没定位)。
|
|
109
|
+
for (const p of problems) process.stdout.write(` ✗ ${renderFinding(p, { root: ROOT, withSource: WHERE })}\n`)
|
|
108
110
|
process.exit(has('--strict') && problems.length ? 1 : 0)
|