focus-guard 3.5.2 → 3.5.3
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 +13 -3
- package/assets/NOTICE.md +2 -0
- package/docs/ARCH-MAP.md +33 -20
- package/docs/BENCH.md +21 -12
- package/docs/RELEASE-POLICY.md +60 -16
- package/docs/RULES.md +75 -55
- package/docs/THREAT-MODEL.md +1 -1
- package/docs/WRITING-STYLE.md +1 -1
- 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 +43 -23
- package/src/adapters/dsh/session.mjs +22 -0
- package/src/core/approvals.mjs +10 -4
- package/src/core/audit-writer.mjs +5 -2
- package/src/core/checkEligibility.mjs +190 -129
- package/src/core/constants.mjs +1 -1
- package/src/core/decisionEngine.mjs +16 -13
- package/src/core/env.mjs +18 -0
- package/src/core/patterns.mjs +3 -1
- package/src/core/prefixStability.mjs +27 -3
- package/src/core/session.mjs +23 -0
- package/src/dsh/audit.mjs +21 -7
- 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/guard/authz.mjs +2 -2
- package/src/dsh/guard/retryGuard.mjs +96 -0
- package/src/dsh/guard/risk.mjs +61 -16
- package/src/dsh/guard/runtime.mjs +30 -5
- package/src/dsh/guard/tooling.mjs +5 -9
- package/src/dsh/guard/writing-gate.mjs +117 -0
- package/src/dsh/index.js +4 -7
- package/src/dsh/pipeline.mjs +66 -5
- package/src/dsh/preGuard.mjs +11 -2
- package/src/dsh/repeatGuard.mjs +5 -4
- package/src/dsh/seams/session-start.mjs +19 -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 +4 -4
- package/src/memory/workspace.mjs +41 -19
- 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 +165 -0
- package/tools/docs-lint.mjs +24 -0
- package/tools/gate-audit.mjs +3 -24
- package/tools/handoff.mjs +3 -1
- package/tools/install-check.mjs +107 -9
- package/tools/layering-audit.mjs +7 -1
- package/tools/legacy-audit.mjs +83 -0
- package/tools/lib/case-tool.mjs +8 -2
- package/tools/lib/docs-ack.mjs +95 -0
- package/tools/lib/docs-core.mjs +38 -22
- package/tools/lib/docs-files.mjs +73 -0
- package/tools/lib/docs-status.mjs +143 -0
- package/tools/lib/idioms.mjs +149 -0
- package/tools/lib/import-graph.mjs +8 -3
- package/tools/lib/legacy-core.mjs +178 -0
- package/tools/lib/markdown.mjs +202 -0
- package/tools/lib/modules-doc.mjs +96 -0
- package/tools/lib/modules-list.mjs +382 -0
- package/tools/lib/modules.mjs +312 -0
- package/tools/lib/prefix-cost.mjs +5 -3
- package/tools/library-book.mjs +7 -92
- package/tools/lint.mjs +5 -0
- package/tools/memory-audit.mjs +32 -6
- package/tools/memory-recall-eval.mjs +408 -0
- package/tools/module-audit.mjs +2 -17
- package/tools/modules-audit.mjs +108 -0
- package/tools/panel-baseline.mjs +172 -0
- package/tools/prefix-audit.mjs +10 -2
- package/tools/prefix-drift.mjs +9 -4
- package/tools/registry-check.mjs +194 -0
- package/tools/release.mjs +374 -0
- 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/dsh/guard/superpowers.mjs +0 -70
package/tools/gate-audit.mjs
CHANGED
|
@@ -15,8 +15,8 @@
|
|
|
15
15
|
* 用法:node packages/core/tools/gate-audit.mjs [--limit 20] [--json]
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
-
import { decodeSession as decode } from './lib/sessionDump.mjs'
|
|
19
|
-
import {
|
|
18
|
+
import { decodeSession as decode, walkSessionFiles } from './lib/sessionDump.mjs'
|
|
19
|
+
import { readFileSync } from 'node:fs'
|
|
20
20
|
import { join, dirname } from 'node:path'
|
|
21
21
|
import { fileURLToPath } from 'node:url'
|
|
22
22
|
|
|
@@ -28,32 +28,11 @@ const argv = process.argv.slice(2)
|
|
|
28
28
|
const LIMIT = Number(argv.includes('--limit') ? argv[argv.indexOf('--limit') + 1] : 20) || 20
|
|
29
29
|
const AS_JSON = argv.includes('--json')
|
|
30
30
|
|
|
31
|
-
function walk(dir, out = []) {
|
|
32
|
-
let entries = []
|
|
33
|
-
try {
|
|
34
|
-
entries = readdirSync(dir)
|
|
35
|
-
} catch {
|
|
36
|
-
return out
|
|
37
|
-
}
|
|
38
|
-
for (const e of entries) {
|
|
39
|
-
const p = join(dir, e)
|
|
40
|
-
let st
|
|
41
|
-
try {
|
|
42
|
-
st = statSync(p)
|
|
43
|
-
} catch {
|
|
44
|
-
continue
|
|
45
|
-
}
|
|
46
|
-
if (st.isDirectory()) walk(p, out)
|
|
47
|
-
else if (e === 'session.v4.jsonl.zstd') out.push({ file: p, mtime: st.mtimeMs })
|
|
48
|
-
}
|
|
49
|
-
return out
|
|
50
|
-
}
|
|
51
|
-
|
|
52
31
|
/** 多帧 zstd:按魔数 0x28B52FFD 分帧解压(单帧 API 只解第一帧)。 */
|
|
53
32
|
|
|
54
33
|
/** 从会话里取真实工具表:{ 工具名: Set(参数键) }。 */
|
|
55
34
|
function realToolSchema(limit) {
|
|
56
|
-
const files =
|
|
35
|
+
const files = walkSessionFiles(SESSIONS).slice(0, limit).map((x) => ({ file: x.f, mtime: x.m }))
|
|
57
36
|
const schema = new Map()
|
|
58
37
|
let usedSessions = 0
|
|
59
38
|
for (const f of files) {
|
package/tools/handoff.mjs
CHANGED
|
@@ -48,7 +48,9 @@ const block = (label, items) => {
|
|
|
48
48
|
}
|
|
49
49
|
if (items.length > 5) console.log(`- …(共 ${items.length} 条,其余见 workspace.json)`)
|
|
50
50
|
}
|
|
51
|
-
|
|
51
|
+
// 决策块已下架(2026-11-26):它打印的是人类指令原文,不是决策。见
|
|
52
|
+
// `src/memory/workspace.mjs` 文件头「下架声明」。新旧会话都不再展示,
|
|
53
|
+
// 避免"简报里看着像结论、其实是问句"。
|
|
52
54
|
block('约束(被人否决过的命令)', record.constraints)
|
|
53
55
|
block('下一步', record.next_action)
|
|
54
56
|
|
package/tools/install-check.mjs
CHANGED
|
@@ -23,16 +23,22 @@
|
|
|
23
23
|
* postinstall / prepare / prepublish)——**这条就是根因的回归防线**
|
|
24
24
|
* B `patch-exists` `dsh.bundle.patch` 指向的文件必须在包内
|
|
25
25
|
* C `entry-shape` 入口必须导出 `name` 与 `apply`(cordis 插件形态)
|
|
26
|
+
* D `engines-dsh` 兼容声明必须写在 `engines.dsh`(dsh 只认这个位置,见下)
|
|
26
27
|
*
|
|
27
28
|
* 安装(`--install`,需要 dsh):
|
|
28
|
-
*
|
|
29
|
+
* E `bundles-registered` 把**本地 tarball**装进一次性 profile,断言
|
|
29
30
|
* `dsh.profile.bundles` 里出现本包,然后拆掉现场
|
|
30
31
|
*
|
|
32
|
+
* D 的由来(2026-11-19):dsh 0.2 的 `unmetRequirements` 是「有 `engines.dsh` 就只看它,
|
|
33
|
+
* `peerDependencies` 全部忽略;没有才退回去扫 `@deepseek-ai/dsh*` 的 peer」。我们一直只写了
|
|
34
|
+
* peer(3.0.6 起),判定其实一直在生效——但**外部验证平台只认 `engines.dsh`**,于是页面显示
|
|
35
|
+
* 「作者未声明 dsh 版本要求」。约束存在而不可见,等于半个没有。
|
|
36
|
+
*
|
|
31
37
|
* 缺 `dsh` 时 D 记 **skip 并明说**,不静默通过——「没查」与「查过没问题」
|
|
32
38
|
* 必须能分开(本仓为此栽过:判据缺失静默返回空)。
|
|
33
39
|
*
|
|
34
40
|
* 用法:
|
|
35
|
-
* node tools/install-check.mjs #
|
|
41
|
+
* node tools/install-check.mjs # 静态四项
|
|
36
42
|
* node tools/install-check.mjs --install # 再加一次真装
|
|
37
43
|
* node tools/install-check.mjs --strict # 有任何 finding 即 exit 1
|
|
38
44
|
*/
|
|
@@ -45,6 +51,60 @@ import { fileURLToPath } from 'node:url'
|
|
|
45
51
|
const ROOT = fileURLToPath(new URL('..', import.meta.url))
|
|
46
52
|
const STRICT = process.argv.includes('--strict')
|
|
47
53
|
const WITH_INSTALL = process.argv.includes('--install')
|
|
54
|
+
if (process.env.FG_TRACE) {
|
|
55
|
+
process.stderr.write(
|
|
56
|
+
`[trace] pid=${process.pid} ppid=${process.ppid} lifecycle=${process.env.npm_lifecycle_event || '-'} argv=${process.argv.slice(1).join(' ')}\n`,
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* 防重入(2026-11-19):跑 `--install` 时会**再起一个本脚本**(pid 不同、profile 名不同),
|
|
61
|
+
* 两个进程各建一套现场;内层那套装成功了却没被拆掉,于是每次自检在 `~/.dsh/profiles`
|
|
62
|
+
* 里留一个 `fg-selfcheck-<pid>`。
|
|
63
|
+
*
|
|
64
|
+
* 先试过环境变量,**不生效**——内层进程看不到外层设的 `FG_INSTALL_CHECK`(那条链中间
|
|
65
|
+
* 有人重置了环境)。改成系统临时目录里的锁文件:不依赖环境继承,只看「有没有人在跑」。
|
|
66
|
+
* 持有者 pid 已死则接管,避免崩溃留下死锁。
|
|
67
|
+
*/
|
|
68
|
+
const LOCK = join(tmpdir(), 'fg-install-check.lock')
|
|
69
|
+
|
|
70
|
+
function acquireLock() {
|
|
71
|
+
const me = JSON.stringify({ pid: process.pid, ppid: process.ppid, at: Date.now(), argv: process.argv.slice(1) })
|
|
72
|
+
try {
|
|
73
|
+
writeFileSync(LOCK, me, { flag: 'wx' })
|
|
74
|
+
return true
|
|
75
|
+
} catch {
|
|
76
|
+
let holder = null
|
|
77
|
+
try {
|
|
78
|
+
holder = JSON.parse(readFileSync(LOCK, 'utf8'))
|
|
79
|
+
} catch {
|
|
80
|
+
holder = null
|
|
81
|
+
}
|
|
82
|
+
if (holder && typeof holder.pid === 'number') {
|
|
83
|
+
try {
|
|
84
|
+
process.kill(holder.pid, 0)
|
|
85
|
+
// 持有者还活着:把双方身份都记下来,冲突才有据可查。
|
|
86
|
+
notes.push(`锁被 pid ${holder.pid}(父 ${holder.ppid})持有,本人 pid ${process.pid}(父 ${process.ppid})`)
|
|
87
|
+
return false
|
|
88
|
+
} catch {
|
|
89
|
+
/* 持有者已死,接管 */
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
try {
|
|
93
|
+
writeFileSync(LOCK, me)
|
|
94
|
+
return true
|
|
95
|
+
} catch {
|
|
96
|
+
return false
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function releaseLock() {
|
|
102
|
+
try {
|
|
103
|
+
rmSync(LOCK, { force: true })
|
|
104
|
+
} catch {
|
|
105
|
+
/* 释放失败不影响判定 */
|
|
106
|
+
}
|
|
107
|
+
}
|
|
48
108
|
/** Windows 上 `dsh` / `npm` 是 .cmd 垫片,不经 shell 调用就得补后缀(走 shell 会被静态扫描判为注入面)。 */
|
|
49
109
|
const BIN = (n) => (process.platform === 'win32' ? `${n}.cmd` : n)
|
|
50
110
|
|
|
@@ -104,6 +164,25 @@ function checkEntry(pkg) {
|
|
|
104
164
|
}
|
|
105
165
|
}
|
|
106
166
|
|
|
167
|
+
/** D:兼容范围得写在 `engines.dsh`——那是 dsh 唯一「说了算」的位置。 */
|
|
168
|
+
function checkEnginesDsh(pkg) {
|
|
169
|
+
const range = pkg.engines && pkg.engines.dsh
|
|
170
|
+
if (typeof range !== 'string' || !range.trim()) {
|
|
171
|
+
findings.push({
|
|
172
|
+
rule: 'engines-dsh',
|
|
173
|
+
detail: 'package.json 没有 engines.dsh——版本约束只写在 peerDependencies 里时,dsh 仍会校验(退路),但外部平台与读者都看不见,等于半个声明',
|
|
174
|
+
})
|
|
175
|
+
return
|
|
176
|
+
}
|
|
177
|
+
// dsh 的 `workspace:` 特例只对 peer 生效;写进 engines 会被当成普通范围,永不满足。
|
|
178
|
+
if (range.includes('workspace:')) {
|
|
179
|
+
findings.push({
|
|
180
|
+
rule: 'engines-dsh',
|
|
181
|
+
detail: `engines.dsh 写成了 ${range}——workspace: 只在 peerDependencies 里被解析,放进 engines 等于永不满足`,
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
107
186
|
/** profile 目录可能落在 DSH_HOME(指向 .dsh 本身或它的父目录)或默认 ~/.dsh。 */
|
|
108
187
|
function findProfile(name) {
|
|
109
188
|
const homes = [process.env.DSH_HOME, join(homedir(), '.dsh')].filter(Boolean)
|
|
@@ -122,7 +201,7 @@ function checkInstall(pkg) {
|
|
|
122
201
|
const WIN = process.platform === 'win32'
|
|
123
202
|
const dsh = spawnSync(BIN('dsh'), ['--version'], { encoding: 'utf8', shell: WIN })
|
|
124
203
|
if (dsh.status !== 0) {
|
|
125
|
-
notes.push('skip 安装档:本机没有可用的 dsh
|
|
204
|
+
notes.push('skip 安装档:本机没有可用的 dsh 命令(静态四项已覆盖根因)')
|
|
126
205
|
return
|
|
127
206
|
}
|
|
128
207
|
// 现场放系统临时目录,不放包内 node_modules:pnpm 对「另一个包 node_modules 下的 tarball」
|
|
@@ -130,6 +209,8 @@ function checkInstall(pkg) {
|
|
|
130
209
|
const tmp = join(tmpdir(), `fg-install-check-${process.pid}`)
|
|
131
210
|
const name = `fg-selfcheck-${process.pid}`
|
|
132
211
|
let packed = ''
|
|
212
|
+
/** 现场目录在 finally 里拆——**任何提前 return 都不能留下 profile**。 */
|
|
213
|
+
let profileDir = ''
|
|
133
214
|
try {
|
|
134
215
|
rmSync(tmp, { recursive: true, force: true })
|
|
135
216
|
mkdirSync(tmp, { recursive: true })
|
|
@@ -156,15 +237,15 @@ function checkInstall(pkg) {
|
|
|
156
237
|
encoding: 'utf8',
|
|
157
238
|
shell: WIN,
|
|
158
239
|
})
|
|
159
|
-
|
|
160
|
-
if (!
|
|
240
|
+
profileDir = findProfile(name) || ''
|
|
241
|
+
if (!profileDir) {
|
|
161
242
|
findings.push({
|
|
162
243
|
rule: 'install-profile',
|
|
163
244
|
detail: `装完找不到 profile 目录(dsh exit ${add.status}):${(add.stdout || add.stderr || '').trim().slice(0, 200)}`,
|
|
164
245
|
})
|
|
165
246
|
return
|
|
166
247
|
}
|
|
167
|
-
const installed = JSON.parse(readFileSync(join(
|
|
248
|
+
const installed = JSON.parse(readFileSync(join(profileDir, 'package.json'), 'utf8'))
|
|
168
249
|
const bundles = (installed.dsh && installed.dsh.profile && installed.dsh.profile.bundles) || []
|
|
169
250
|
if (!bundles.includes(pkg.name)) {
|
|
170
251
|
findings.push({
|
|
@@ -172,8 +253,15 @@ function checkInstall(pkg) {
|
|
|
172
253
|
detail: `装完 dsh.profile.bundles 里没有 ${pkg.name}(现有:${bundles.join(' / ') || '空'})——装上了但不会加载`,
|
|
173
254
|
})
|
|
174
255
|
}
|
|
175
|
-
rmSync(dir, { recursive: true, force: true })
|
|
176
256
|
} finally {
|
|
257
|
+
// 清理失败不改变判定,但也不能吞得无声无息:留一行 note,下次才知道地上有垃圾。
|
|
258
|
+
if (profileDir) {
|
|
259
|
+
try {
|
|
260
|
+
rmSync(profileDir, { recursive: true, force: true })
|
|
261
|
+
} catch (e) {
|
|
262
|
+
notes.push(`profile 现场未拆净(${profileDir}):${e.code || e.message}`)
|
|
263
|
+
}
|
|
264
|
+
}
|
|
177
265
|
rmSync(tmp, { recursive: true, force: true })
|
|
178
266
|
if (packed && existsSync(join(ROOT, packed))) rmSync(join(ROOT, packed), { force: true })
|
|
179
267
|
}
|
|
@@ -183,9 +271,19 @@ const pkg = readPkg()
|
|
|
183
271
|
checkLifecycle(pkg)
|
|
184
272
|
checkPatch(pkg)
|
|
185
273
|
checkEntry(pkg)
|
|
186
|
-
|
|
274
|
+
checkEnginesDsh(pkg)
|
|
275
|
+
if (WITH_INSTALL) {
|
|
276
|
+
// 拿不到锁时 acquireLock 已把冲突双方写进 notes,这里不重复记。
|
|
277
|
+
if (acquireLock()) {
|
|
278
|
+
try {
|
|
279
|
+
checkInstall(pkg)
|
|
280
|
+
} finally {
|
|
281
|
+
releaseLock()
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
187
285
|
|
|
188
|
-
const head = `安装自检:${WITH_INSTALL ? '静态
|
|
286
|
+
const head = `安装自检:${WITH_INSTALL ? '静态 4 项 + 安装档' : '静态 4 项'}`
|
|
189
287
|
if (!findings.length) {
|
|
190
288
|
process.stdout.write(`[install-check] ${head},问题 0 条${notes.length ? `;${notes.join(';')}` : ''}\n`)
|
|
191
289
|
} else {
|
package/tools/layering-audit.mjs
CHANGED
|
@@ -35,7 +35,13 @@ const HOST_MARKS = {
|
|
|
35
35
|
const TOOL_NAMES = /\b(?:Bash|Write|Edit|MultiEdit|Read|Grep|Glob|run_code)(?![\w-])/
|
|
36
36
|
// 白名单:规则文本(SESSION_RULES)里提到 Grep/head_limit 属面向模型的表述。
|
|
37
37
|
const ALLOW = {
|
|
38
|
-
toolNames: [
|
|
38
|
+
toolNames: [
|
|
39
|
+
'src/core/constants.mjs',
|
|
40
|
+
// 2026-11-20:`PS_CMDLET` 的动词表里含 `Write`(`Write-Content` 这类 cmdlet 的动词),
|
|
41
|
+
// 命中 TOOL_NAMES 属误报——那里检测的是**命令文本方言**,与宿主工具名无关。
|
|
42
|
+
// 判据本身没错(PowerShell 动词确实不该出现在 bash 里),只是它长得像工具名。
|
|
43
|
+
'src/core/env.mjs',
|
|
44
|
+
],
|
|
39
45
|
}
|
|
40
46
|
|
|
41
47
|
function walk(dir, out = []) {
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* legacy:audit —— 旧写法扫描器(3.5.3)
|
|
4
|
+
*
|
|
5
|
+
* 回答一个问题:**这段文字描述的对象还在不在?**
|
|
6
|
+
*
|
|
7
|
+
* 两类判据:
|
|
8
|
+
* 1. retired-name 退役台账(packages/core/docs/RETIRED.json)里记过的旧名字,除 allow 外不得出现
|
|
9
|
+
* 2. stale-anchor 文档里的 `file.mjs:123` 锚点,行号不得超过文件长度(可证明的失效)
|
|
10
|
+
*
|
|
11
|
+
* `missing-file`(仓内找不到目标)只报不拦:它可能是外部包路径,也可能是删掉的文件,
|
|
12
|
+
* 机器分不清。判不了的不要假装判得了。
|
|
13
|
+
*
|
|
14
|
+
* 用法:
|
|
15
|
+
* node packages/core/tools/legacy-audit.mjs # 人读清单
|
|
16
|
+
* node packages/core/tools/legacy-audit.mjs --brief # 一行(供 npm run check)
|
|
17
|
+
* node packages/core/tools/legacy-audit.mjs --json # 机器读
|
|
18
|
+
* node packages/core/tools/legacy-audit.mjs --strict # 有硬伤即 exit 1
|
|
19
|
+
*
|
|
20
|
+
* 为什么要有这个工具:改名是本仓最高频的操作,也是最高频的漏改。3.5.3 一轮里同一个
|
|
21
|
+
* 错误出现三次(标识符漏改 4 处 / description 副本漏 1 份 / dsh-native 只改 2 个文件)。
|
|
22
|
+
* 根因不是不小心——是没有任何地方记着「旧名是什么」。台账补的就是这一块。
|
|
23
|
+
*/
|
|
24
|
+
import { resolve, dirname } from 'node:path'
|
|
25
|
+
import { fileURLToPath } from 'node:url'
|
|
26
|
+
import { walkScope, loadLedger, scanRetired, scanAnchors } from './lib/legacy-core.mjs'
|
|
27
|
+
|
|
28
|
+
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..', '..', '..')
|
|
29
|
+
const argv = process.argv.slice(2)
|
|
30
|
+
const AS_JSON = argv.includes('--json')
|
|
31
|
+
const BRIEF = argv.includes('--brief')
|
|
32
|
+
const STRICT = argv.includes('--strict')
|
|
33
|
+
|
|
34
|
+
const files = walkScope(ROOT)
|
|
35
|
+
const ledger = loadLedger(ROOT)
|
|
36
|
+
const retired = scanRetired(ROOT, files, ledger.entries)
|
|
37
|
+
const anchors = scanAnchors(ROOT, files, ledger.anchorAllow)
|
|
38
|
+
const overflow = anchors.filter((a) => a.kind === 'line-overflow')
|
|
39
|
+
const missing = anchors.filter((a) => a.kind === 'missing-file')
|
|
40
|
+
|
|
41
|
+
/** 硬伤 = 台账命中的旧名 + 可证明越界的锚点。missing-file 不算。 */
|
|
42
|
+
const hard = retired.length + overflow.length
|
|
43
|
+
|
|
44
|
+
if (AS_JSON) {
|
|
45
|
+
console.log(JSON.stringify({ files: files.length, retired, overflow, missing, hard }, null, 2))
|
|
46
|
+
process.exit(STRICT && hard > 0 ? 1 : 0)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (BRIEF) {
|
|
50
|
+
const parts = [`旧写法 ${hard} 处(旧名 ${retired.length} / 锚点越界 ${overflow.length})`]
|
|
51
|
+
if (missing.length) parts.push(`待人工判 ${missing.length}(仓内无此文件)`)
|
|
52
|
+
console.log(parts.join(','))
|
|
53
|
+
process.exit(STRICT && hard > 0 ? 1 : 0)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
console.log(`扫描 ${files.length} 个文件,台账 ${ledger.entries.length} 条退役记录\n`)
|
|
57
|
+
|
|
58
|
+
if (retired.length) {
|
|
59
|
+
console.log(`旧名字残留 ${retired.length} 处:`)
|
|
60
|
+
for (const r of retired) {
|
|
61
|
+
console.log(` ${r.file}:${r.line} 「${r.pattern}」(${r.id})`)
|
|
62
|
+
console.log(` ${r.text}`)
|
|
63
|
+
}
|
|
64
|
+
console.log('')
|
|
65
|
+
} else {
|
|
66
|
+
console.log('旧名字残留:0')
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (overflow.length) {
|
|
70
|
+
console.log(`\n行号锚点越界 ${overflow.length} 处(可证明失效):`)
|
|
71
|
+
for (const a of overflow) console.log(` ${a.file} → ${a.anchor}(${a.detail})`)
|
|
72
|
+
} else {
|
|
73
|
+
console.log('行号锚点越界:0')
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (missing.length) {
|
|
77
|
+
console.log(`\n待人工判 ${missing.length} 处(仓内找不到目标,可能是外部包路径):`)
|
|
78
|
+
for (const a of missing.slice(0, 20)) console.log(` ${a.file} → ${a.anchor}`)
|
|
79
|
+
if (missing.length > 20) console.log(` …另有 ${missing.length - 20} 处`)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
console.log(`\n合计硬伤 ${hard} 处。${STRICT && hard > 0 ? '闸门:红' : ''}`)
|
|
83
|
+
process.exit(STRICT && hard > 0 ? 1 : 0)
|
package/tools/lib/case-tool.mjs
CHANGED
|
@@ -124,11 +124,17 @@ function saveCase(record, file) {
|
|
|
124
124
|
renameSync(tmp, file)
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
-
/**
|
|
127
|
+
/** 展平成带序号的行:指令残片 / 约束 / 下一步 各自编号,用前缀区分。
|
|
128
|
+
*
|
|
129
|
+
* `decisions` 的显示名 2026-11-26 改为「指令残片」:自动落账已下架
|
|
130
|
+
* (缺陷见 `src/memory/workspace.mjs` 文件头),字段里剩下的只是历史 prompt 片段。
|
|
131
|
+
* **字段名仍叫 decisions**——改 schema 要给存量文件做迁移,而它已经不再产生新数据,
|
|
132
|
+
* 不值得。序号前缀继续用 `decisions:N`,旧命令照旧可用。
|
|
133
|
+
*/
|
|
128
134
|
export function entriesOf(record) {
|
|
129
135
|
const out = []
|
|
130
136
|
for (const [field, label] of [
|
|
131
|
-
['decisions', '
|
|
137
|
+
['decisions', '指令残片'],
|
|
132
138
|
['constraints', '约束'],
|
|
133
139
|
['next_action', '下一步'],
|
|
134
140
|
]) {
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 状态头「最后核对」的时效判据(2026-11-26):**不得早于该文件的最后一次提交日期**。
|
|
3
|
+
*
|
|
4
|
+
* ## 起因
|
|
5
|
+
*
|
|
6
|
+
* 2026-11-26 接手盘点:35 份文档的状态头日期早于它们各自的最后提交,权威文档里
|
|
7
|
+
* `README.md` 写 11-19(实际 11-26 改过)、`ARCH-MAP.md` 写 11-18 且版本停在 3.5.2。
|
|
8
|
+
* 这不是笔误,是**结构性的**:`docs-status.mjs` 只负责把状态头拼成总表,
|
|
9
|
+
* `docs-core.mjs` 只查「权威文档有没有状态头」,**没有一条规则问过『这行日期还诚实吗』**。
|
|
10
|
+
* 于是总表越整齐,越像真的。
|
|
11
|
+
*
|
|
12
|
+
* ## 判据
|
|
13
|
+
*
|
|
14
|
+
* 只查 `权威`。`参考` 的定义就是「有价值但可能滞后」,`台账`/`归档` 是过程记录与冻结历史,
|
|
15
|
+
* 逼它们改日期等于让闸门逼文档说谎——与 `docs-status.mjs` 拒绝「适用版本 == 当前版本」同一个理由。
|
|
16
|
+
*
|
|
17
|
+
* ## 为什么用 git 日期而不是 mtime
|
|
18
|
+
*
|
|
19
|
+
* mtime 随 clone / checkout 全变,且不区分「改了正文」与「只是格式化」。git 的提交日期是
|
|
20
|
+
* **这条内容进入仓库的时间**,正是「最后核对」想声明的那个时刻。代价是浅克隆下不可信:
|
|
21
|
+
* `actions/checkout` 默认 `fetch-depth: 1` 时每个文件都显示成 tip 那次提交,
|
|
22
|
+
* 于是全部判红。这种情况**跳过并说明**,不假红——永久红的闸门等于没有闸门。
|
|
23
|
+
*/
|
|
24
|
+
import { execFileSync } from 'node:child_process'
|
|
25
|
+
import { readFileSync } from 'node:fs'
|
|
26
|
+
import { relative } from 'node:path'
|
|
27
|
+
import { listMarkdownFiles } from './docs-core.mjs'
|
|
28
|
+
import { parseStatusHeader } from './docs-status.mjs'
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 每个文件最后一次被提交的日期(`YYYY-MM-DD`)。
|
|
32
|
+
*
|
|
33
|
+
* 一次 `git log` 拿全部,而不是每文件一次(45 份文档 × 30ms 会让 `gate:fast` 多出 1.4s)。
|
|
34
|
+
* 无法判断时返回 `null`:非 git 目录、浅克隆、git 不可用——三种都不该让闸门红。
|
|
35
|
+
*/
|
|
36
|
+
export function lastCommitDates(root) {
|
|
37
|
+
try {
|
|
38
|
+
if (execFileSync('git', ['rev-parse', '--is-shallow-repository'], { cwd: root, encoding: 'utf8' }).trim() === 'true') {
|
|
39
|
+
return null
|
|
40
|
+
}
|
|
41
|
+
// 分隔符不能用 NUL:`argv` 里传不了 NUL,git 会把 `\u0000` 当字面量收下,
|
|
42
|
+
// 于是切不开、一条都判不到——「闸门在跑但空转」比红更难发现(2026-11-26 实测踩到)。
|
|
43
|
+
const out = execFileSync('git', ['log', '--date=short', '--pretty=format:@@%ad', '--name-only'], {
|
|
44
|
+
cwd: root,
|
|
45
|
+
encoding: 'utf8',
|
|
46
|
+
maxBuffer: 256 * 1024 * 1024,
|
|
47
|
+
})
|
|
48
|
+
const dates = new Map()
|
|
49
|
+
for (const chunk of out.split('@@')) {
|
|
50
|
+
const lines = chunk.split('\n')
|
|
51
|
+
const date = (lines.shift() || '').trim()
|
|
52
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(date)) continue
|
|
53
|
+
for (const raw of lines) {
|
|
54
|
+
const p = raw.trim()
|
|
55
|
+
if (!p || dates.has(p)) continue // log 是倒序:先见到的就是最后一次
|
|
56
|
+
dates.set(p, date)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return dates
|
|
60
|
+
} catch {
|
|
61
|
+
return null
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** 判据:权威文档的「最后核对」不得早于最后提交日期。 */
|
|
66
|
+
export function checkAckFreshness(root, files = listMarkdownFiles(root)) {
|
|
67
|
+
const dates = lastCommitDates(root)
|
|
68
|
+
if (!dates) return []
|
|
69
|
+
const findings = []
|
|
70
|
+
for (const abs of files) {
|
|
71
|
+
const rel = relative(root, abs).split('\\').join('/')
|
|
72
|
+
let head = null
|
|
73
|
+
try {
|
|
74
|
+
head = parseStatusHeader(readFileSync(abs, 'utf8'))
|
|
75
|
+
} catch {
|
|
76
|
+
continue
|
|
77
|
+
}
|
|
78
|
+
if (!head || head.status !== '权威') continue
|
|
79
|
+
const commit = dates.get(rel)
|
|
80
|
+
if (!commit) continue // 未入库(新文件还没提交):无从比较,不假红
|
|
81
|
+
if (!head.date) {
|
|
82
|
+
findings.push({ rule: 'missing-ack', file: rel, line: 1, detail: `权威文档缺「最后核对」日期(最后提交 ${commit})` })
|
|
83
|
+
continue
|
|
84
|
+
}
|
|
85
|
+
if (head.date < commit) {
|
|
86
|
+
findings.push({
|
|
87
|
+
rule: 'stale-ack',
|
|
88
|
+
file: rel,
|
|
89
|
+
line: 1,
|
|
90
|
+
detail: `最后核对 ${head.date} 早于最后提交 ${commit}——正文改过而状态头没跟上`,
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return findings
|
|
95
|
+
}
|
package/tools/lib/docs-core.mjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* 文档口径内核(3.3 D5 / D2):版本面 + 相对链接 + 状态头 + INDEX 覆盖。纯函数,零依赖。
|
|
3
3
|
*
|
|
4
4
|
* 机械判定的四件事:
|
|
5
|
-
* 1)
|
|
5
|
+
* 1) **版本面**:八处清单 + `ENGINE_VERSION` 必须等于 CHANGELOG 最新版本段;
|
|
6
6
|
* 2) **相对链接**:仓库内 `.md` 的 `[text](path)` 目标文件必须存在(http/mailto/锚点跳过);
|
|
7
7
|
* 3) **状态头**(D2):权威文档正文必须带 `> **状态**:权威 · 适用版本 … · 最后核对 …`;
|
|
8
8
|
* 4) **INDEX 覆盖**(D2):权威文档必须能从 `docs/INDEX.md` 的链接到达。
|
|
@@ -11,10 +11,12 @@
|
|
|
11
11
|
*/
|
|
12
12
|
import { existsSync, readdirSync, readFileSync } from 'node:fs'
|
|
13
13
|
import { dirname, join, relative, resolve } from 'node:path'
|
|
14
|
+
import { listMarkdownFiles } from './docs-files.mjs'
|
|
14
15
|
|
|
15
|
-
/**
|
|
16
|
+
/** 八处清单(与 RELEASE-POLICY 第三节逐字对应;2026-11-22 补 package-lock.json)。 */
|
|
16
17
|
export const VERSION_FILES = [
|
|
17
18
|
'package.json',
|
|
19
|
+
'package-lock.json',
|
|
18
20
|
'marketplace.json',
|
|
19
21
|
'.zcode-plugin/plugin.json',
|
|
20
22
|
'.claude-plugin/plugin.json',
|
|
@@ -39,28 +41,12 @@ export const AUTHORITATIVE_DOCS = [
|
|
|
39
41
|
'packages/core/docs/WRITING-STYLE.md',
|
|
40
42
|
'packages/core/docs/BENCH.md',
|
|
41
43
|
'packages/core/docs/PROFILE-TUNING.md',
|
|
44
|
+
'packages/core/docs/THREAT-MODEL.md',
|
|
42
45
|
]
|
|
43
46
|
|
|
44
|
-
//
|
|
45
|
-
//
|
|
46
|
-
|
|
47
|
-
// 2026-10-31(3.4 D3):`docs/archive/` 是历史证据,链接按旧路径原样保留才不失真,
|
|
48
|
-
// 因此归档文档整体不进 lint(不查链接/状态头);INDEX 仍负责列出它们。
|
|
49
|
-
|
|
50
|
-
/** 遍历仓库内 Markdown(排除运行产物与归档)。 */
|
|
51
|
-
export function listMarkdownFiles(root) {
|
|
52
|
-
const out = []
|
|
53
|
-
const walk = (dir) => {
|
|
54
|
-
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
55
|
-
const full = join(dir, entry.name)
|
|
56
|
-
if (SKIP_SEGMENTS.has(entry.name)) continue
|
|
57
|
-
if (entry.isDirectory()) walk(full)
|
|
58
|
-
else if (entry.name.toLowerCase().endsWith('.md')) out.push(full)
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
walk(root)
|
|
62
|
-
return out
|
|
63
|
-
}
|
|
47
|
+
// 文档清单的真值只有一个(`docs-files.mjs`):`docs-lint` / `docs-status` / `docs-version` /
|
|
48
|
+
// `docs-ack` / `docs-book` 全从这里拿文件。输入少几个文件,判据再对也等于没看。
|
|
49
|
+
export { listMarkdownFiles } from './docs-files.mjs'
|
|
64
50
|
|
|
65
51
|
/** CHANGELOG 最新版本段(`## x.y.z`)。 */
|
|
66
52
|
export function changelogLatest(root) {
|
|
@@ -325,6 +311,36 @@ export function checkIndexCoverage(root, docs = AUTHORITATIVE_DOCS) {
|
|
|
325
311
|
return findings
|
|
326
312
|
}
|
|
327
313
|
|
|
314
|
+
/**
|
|
315
|
+
* 判据:行内代码里的 `npm run <name>` 必须是真实脚本。
|
|
316
|
+
*
|
|
317
|
+
* 为什么值得单独一条:文档最常给的**就是命令**,而命令写错只有读者踩到才知道。
|
|
318
|
+
* 命令名存在与否是硬事实,不需要判断力,属于"机械该做的事"。
|
|
319
|
+
* 只认 `npm run x` 这一种写法(`npm test` / `npx` / 裸 `node` 不查——它们的目标不是本仓 scripts)。
|
|
320
|
+
*/
|
|
321
|
+
export function checkScriptRefs(root, files = listMarkdownFiles(root)) {
|
|
322
|
+
const names = new Set()
|
|
323
|
+
for (const rel of ['package.json', 'packages/core/package.json', 'packages/extended/package.json']) {
|
|
324
|
+
try {
|
|
325
|
+
const j = JSON.parse(readFileSync(join(root, rel), 'utf8'))
|
|
326
|
+
for (const k of Object.keys(j.scripts || {})) names.add(k)
|
|
327
|
+
for (const k of Object.keys(j.bin || {})) names.add(k)
|
|
328
|
+
} catch {
|
|
329
|
+
/* 缺某份清单不影响其他份 */
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
const findings = []
|
|
333
|
+
for (const file of files) {
|
|
334
|
+
const rel = relative(root, file).split('\\').join('/')
|
|
335
|
+
readFileSync(file, 'utf8').split(/\r?\n/).forEach((line, i) => {
|
|
336
|
+
for (const m of line.matchAll(/`npm run ([\w:.-]+)`/g)) {
|
|
337
|
+
if (!names.has(m[1])) findings.push({ rule: 'dead-script-ref', file: rel, line: i + 1, detail: `npm run ${m[1]} 不存在于任何 package.json` })
|
|
338
|
+
}
|
|
339
|
+
})
|
|
340
|
+
}
|
|
341
|
+
return findings
|
|
342
|
+
}
|
|
343
|
+
|
|
328
344
|
/** D1:README 硬指标(行数 / 标题数 / 表格行数 / 三位数只留版本号)。 */
|
|
329
345
|
export const README_LIMITS = { lines: 160, headings: 10, tableRows: 30 }
|
|
330
346
|
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「哪些 Markdown 算文档」——单一出处(2026-11-26 从 `docs-core.mjs` 拆出)。
|
|
3
|
+
*
|
|
4
|
+
* 拆出来的直接原因:`docs-core.mjs` 已 418 行,越过 `smells` 的 400 行阈值。
|
|
5
|
+
* 按本仓的既有判断(见 `docs-version.mjs` / `docs-status.mjs` 的头注释),
|
|
6
|
+
* 越过阈值时**不许抬阈值**,只许拆——抬阈值等于把尺子锯短。
|
|
7
|
+
*
|
|
8
|
+
* 更实质的原因是:这个函数是**所有文档闸门的输入口**(`docs-lint` / `docs-status` /
|
|
9
|
+
* `docs-version` / `docs-ack` / `docs-book` 都从它拿文件清单)。输入少几个文件,
|
|
10
|
+
* 判据再对也等于没看。`gate-inputs.mjs` 记过同一类事故:
|
|
11
|
+
* 「同一根目录、同一个 listMarkdownFiles,差 6 个文件;两次的问题条数都是 5 条,闸门一路绿。」
|
|
12
|
+
*/
|
|
13
|
+
import { execFileSync } from 'node:child_process'
|
|
14
|
+
import { existsSync, readdirSync } from 'node:fs'
|
|
15
|
+
import { join } from 'node:path'
|
|
16
|
+
|
|
17
|
+
// `HANDOFF.md` 是本机私有备忘(在 .git/info/exclude 里),不属于公开文档面,
|
|
18
|
+
// 混进来会让"文档口径"这个数字同时反映私有文件——口径不纯。
|
|
19
|
+
// 2026-11-26:`.workbuddy/` 同理——它是会话记忆目录(未跟踪),里面的 `memory/*.md`
|
|
20
|
+
// 一度被扫进 `docs/STATUS.md`(46 份里多出 1 份本机私货),换台机器就少一份。
|
|
21
|
+
const SKIP_SEGMENTS = new Set([
|
|
22
|
+
'node_modules',
|
|
23
|
+
'.git',
|
|
24
|
+
'.focus-guard',
|
|
25
|
+
'.ai',
|
|
26
|
+
'.workbuddy',
|
|
27
|
+
'self-improvement',
|
|
28
|
+
'archived',
|
|
29
|
+
'archive',
|
|
30
|
+
'backup',
|
|
31
|
+
'HANDOFF.md',
|
|
32
|
+
])
|
|
33
|
+
// 2026-10-31(3.4 D3):`docs/archive/` 是历史证据,链接按旧路径原样保留才不失真,
|
|
34
|
+
// 因此归档文档整体不进 lint(不查链接/状态头);INDEX 仍负责列出它们。
|
|
35
|
+
|
|
36
|
+
/** 遍历仓库内 Markdown(排除运行产物与归档)。 */
|
|
37
|
+
export function listMarkdownFiles(root) {
|
|
38
|
+
// **枚举入库文件,而不是枚举磁盘上的文件**(2026-11-26)。
|
|
39
|
+
//
|
|
40
|
+
// `docs/STATUS.md` 是**入库的生成物**:它由本函数的输出渲染而成,又随代码一起提交。
|
|
41
|
+
// 若输入是「磁盘上有什么」,本机多出的私有文档就会进表,而 CI 上的树没有它们——
|
|
42
|
+
// 同一份 `STATUS.md` 在两台机器上算出两个指纹,`status-table-stale` 于 CI 必红、本地全绿。
|
|
43
|
+
// 3.5.3 的发布闸正是这么红的:本地 46 份 / CI 39 份,差的 7 份全是未入库的 `docs/vault-notes/vendors/*`。
|
|
44
|
+
// 判据:**生成物的输入必须可复现**——换了机器还能重算出同一张表,否则「本地绿」不保证「发得出去」。
|
|
45
|
+
const tracked = trackedMarkdown(root)
|
|
46
|
+
if (tracked) return tracked
|
|
47
|
+
const out = []
|
|
48
|
+
const walk = (dir) => {
|
|
49
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
50
|
+
const full = join(dir, entry.name)
|
|
51
|
+
if (SKIP_SEGMENTS.has(entry.name)) continue
|
|
52
|
+
if (entry.isDirectory()) walk(full)
|
|
53
|
+
else if (entry.name.toLowerCase().endsWith('.md')) out.push(full)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
walk(root)
|
|
57
|
+
return out
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** 入库的 Markdown;非 git 目录(`mutate` 副本、打包后的树)返回 null,交由磁盘遍历兜底。 */
|
|
61
|
+
function trackedMarkdown(root) {
|
|
62
|
+
try {
|
|
63
|
+
const raw = execFileSync('git', ['ls-files', '-z', '*.md'], { cwd: root, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 })
|
|
64
|
+
return raw
|
|
65
|
+
.split('\u0000')
|
|
66
|
+
.filter(Boolean)
|
|
67
|
+
.filter((rel) => !rel.split('/').some((seg) => SKIP_SEGMENTS.has(seg)))
|
|
68
|
+
.map((rel) => join(root, rel))
|
|
69
|
+
.filter((abs) => existsSync(abs))
|
|
70
|
+
} catch {
|
|
71
|
+
return null
|
|
72
|
+
}
|
|
73
|
+
}
|