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,110 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 模块修订审计(tools/modules-audit.mjs,2026-11-19)
|
|
4
|
+
*
|
|
5
|
+
* 回答一个问题:**哪块地方很久没人看了?**
|
|
6
|
+
*
|
|
7
|
+
* 数据全部现算自 git,清单在 `lib/modules-list.mjs`,算法在 `lib/modules.mjs`。
|
|
8
|
+
* 为什么判据用「其后全仓提交」、为什么看中位文件而不是最新文件——见那两个文件的文件头。
|
|
9
|
+
*
|
|
10
|
+
* 用法:
|
|
11
|
+
* node packages/core/tools/modules-audit.mjs # 一行摘要(供 check 链)
|
|
12
|
+
* node packages/core/tools/modules-audit.mjs --table # 完整表
|
|
13
|
+
* node packages/core/tools/modules-audit.mjs --json # 机器读
|
|
14
|
+
* node packages/core/tools/modules-audit.mjs --sync # 写 docs/MODULES.md
|
|
15
|
+
* node packages/core/tools/modules-audit.mjs --check-doc # 台账是否落后
|
|
16
|
+
* node packages/core/tools/modules-audit.mjs --where # 报点旁附上出错那一行原文
|
|
17
|
+
* node packages/core/tools/modules-audit.mjs --strict # 清单有缺陷时 exit 1
|
|
18
|
+
*
|
|
19
|
+
* 退出码:默认只在 `--strict` 下因清单缺陷(路径缺失/重叠/未归属/id 重复)非零;
|
|
20
|
+
* 「久未动」本身**永不**导致非零——那是信息,不是错误。
|
|
21
|
+
*/
|
|
22
|
+
import { writeFileSync } from 'node:fs'
|
|
23
|
+
import { join } from 'node:path'
|
|
24
|
+
import {
|
|
25
|
+
MODULES,
|
|
26
|
+
MODULES_DOC,
|
|
27
|
+
collectModules,
|
|
28
|
+
collectCoverage,
|
|
29
|
+
renderModules,
|
|
30
|
+
checkModulePaths,
|
|
31
|
+
checkModuleIds,
|
|
32
|
+
checkModuleOverlap,
|
|
33
|
+
checkModuleCoverage,
|
|
34
|
+
checkModulesDoc,
|
|
35
|
+
} from './lib/modules.mjs'
|
|
36
|
+
import { renderFinding } from './lib/where.mjs'
|
|
37
|
+
import { has, repoRoot } from './lib/cli.mjs'
|
|
38
|
+
|
|
39
|
+
const ROOT = repoRoot(import.meta.url)
|
|
40
|
+
const WHERE = has('--where')
|
|
41
|
+
|
|
42
|
+
const data = collectModules(ROOT, MODULES)
|
|
43
|
+
const coverage = collectCoverage(ROOT, MODULES)
|
|
44
|
+
const problems = [
|
|
45
|
+
...checkModulePaths(ROOT, MODULES),
|
|
46
|
+
...checkModuleIds(ROOT, MODULES),
|
|
47
|
+
...checkModuleOverlap(ROOT, MODULES),
|
|
48
|
+
...checkModuleCoverage(ROOT, MODULES),
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
if (has('--sync')) {
|
|
52
|
+
writeFileSync(join(ROOT, MODULES_DOC), renderModules(data, coverage))
|
|
53
|
+
process.stdout.write(`[modules] 已生成 ${MODULES_DOC}:${data.rows.length} 个模块\n`)
|
|
54
|
+
process.exit(0)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (has('--json')) {
|
|
58
|
+
process.stdout.write(`${JSON.stringify({ head: data.head, age: data.age, total: data.total, rows: data.rows, coverage, problems }, null, 2)}\n`)
|
|
59
|
+
process.exit(has('--strict') && problems.length ? 1 : 0)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const byLevel = (lv) => data.rows.filter((r) => r.level === lv)
|
|
63
|
+
const dirty = data.rows.filter((r) => r.dirty)
|
|
64
|
+
|
|
65
|
+
if (has('--check-doc')) {
|
|
66
|
+
const docFindings = checkModulesDoc(ROOT)
|
|
67
|
+
// 带上位置:这条闸门红的时候,人要去改的是 `docs/MODULES.md` 的指纹块,不是整个文件。
|
|
68
|
+
process.stdout.write(docFindings.length ? `[modules] ${renderFinding(docFindings[0], { root: ROOT, withSource: WHERE })}\n` : '[modules] 台账与现算一致\n')
|
|
69
|
+
process.exit(docFindings.length ? 1 : 0)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (has('--table')) {
|
|
73
|
+
process.stdout.write(`模块修订(HEAD ${data.head ? data.head.slice(0, 7) : '—'} · 仓龄 ${data.age} 天 · ${data.total} 次提交)\n\n`)
|
|
74
|
+
process.stdout.write(' 距今:最新 = 最勤快的文件;中位 = 一半文件比它旧(判定看它);最久 = 最沉的文件\n\n')
|
|
75
|
+
const icon = { 久未动: '✗', 临期: '!', 偏冷: '·', 活跃: '·', 无法定位: '?' }
|
|
76
|
+
for (const r of data.rows) {
|
|
77
|
+
const flags = []
|
|
78
|
+
if (r.touches < 3) flags.push('历史浅')
|
|
79
|
+
if (r.dirty) flags.push('有未提交改动')
|
|
80
|
+
const tail = flags.length ? ` · ${flags.join(' · ')}` : ''
|
|
81
|
+
const span = `${r.latestDays}/${r.medianDays}/${r.oldestDays}`.padStart(11)
|
|
82
|
+
process.stdout.write(
|
|
83
|
+
` ${icon[r.level]} ${r.name.padEnd(20, ' ')} 距今 ${span} 天 · 中位其后 ${String(r.median ?? '—').padStart(3)} 次 · ${String(r.files).padStart(2)} 文件 · ${r.level}${tail}\n`,
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
process.stdout.write(
|
|
87
|
+
'\n图例:✗ 久未动 ! 临期 · 偏冷/活跃 ? 无法定位\n' +
|
|
88
|
+
'判定看「中位文件其后全仓提交数」:该文件最后修订之后,仓库又走了多少步。≥30 偏冷 / ≥80 临期 / ≥150 久未动。\n' +
|
|
89
|
+
`覆盖:受管 ${coverage.total} 个文件,仓根元数据 ${coverage.meta} 个,未归属功能文件 ${coverage.unowned} 个\n`,
|
|
90
|
+
)
|
|
91
|
+
} else {
|
|
92
|
+
// 摘要行:进 check 链,每次跑都提一句。**不红**——久未动不等于坏。
|
|
93
|
+
// 不逐条列 id:三十几个模块列出来没人读,只报计数与前三个最陈旧的。
|
|
94
|
+
const n = (lv) => byLevel(lv).length
|
|
95
|
+
const worst = data.rows
|
|
96
|
+
.filter((r) => r.level === '久未动')
|
|
97
|
+
.slice(0, 3)
|
|
98
|
+
.map((r) => `${r.id} ${r.median} 次`)
|
|
99
|
+
const rest = []
|
|
100
|
+
if (n('无法定位')) rest.push(`无法定位 ${n('无法定位')}`)
|
|
101
|
+
if (dirty.length) rest.push(`有未提交改动 ${dirty.length}`)
|
|
102
|
+
process.stdout.write(
|
|
103
|
+
`模块修订:${data.rows.length} 个模块,久未动 ${n('久未动')} / 临期 ${n('临期')} / 偏冷 ${n('偏冷')} / 活跃 ${n('活跃')}` +
|
|
104
|
+
`${worst.length ? `;最陈旧 ${worst.join(' · ')}` : ''}${rest.length ? `;${rest.join(';')}` : ''}\n`,
|
|
105
|
+
)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// 报点带 `file:line`(此前只有 rule + detail,一条命令跳到文件开头等于没定位)。
|
|
109
|
+
for (const p of problems) process.stdout.write(` ✗ ${renderFinding(p, { root: ROOT, withSource: WHERE })}\n`)
|
|
110
|
+
process.exit(has('--strict') && problems.length ? 1 : 0)
|
package/tools/mutate.mjs
CHANGED
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
* 而这个仓的源码是安全闸门本体,脏了比跑不完严重得多。
|
|
35
35
|
*
|
|
36
36
|
* 用法:
|
|
37
|
-
* node packages/core/tools/mutate.mjs dsh/toolName.mjs
|
|
37
|
+
* node packages/core/tools/mutate.mjs adapters/dsh/toolName.mjs # 变异一个模块
|
|
38
38
|
* node packages/core/tools/mutate.mjs core/approval.mjs --list # 只列变异体,不跑
|
|
39
39
|
* node packages/core/tools/mutate.mjs dsh/postProgress.mjs --max 30
|
|
40
40
|
* node packages/core/tools/mutate.mjs dsh/postProgress.mjs --json
|
|
@@ -43,12 +43,16 @@
|
|
|
43
43
|
*/
|
|
44
44
|
|
|
45
45
|
import { spawnSync } from 'node:child_process'
|
|
46
|
-
import { readFileSync, writeFileSync,
|
|
47
|
-
import { join, dirname, resolve, basename } from 'node:path'
|
|
46
|
+
import { readFileSync, writeFileSync, rmSync, mkdtempSync } from 'node:fs'
|
|
47
|
+
import { join, dirname, resolve, basename, relative, sep } from 'node:path'
|
|
48
48
|
import { tmpdir } from 'node:os'
|
|
49
49
|
import { fileURLToPath } from 'node:url'
|
|
50
|
-
import { stripLiterals } from './lib/source-text.mjs'
|
|
51
50
|
import { buildImportGraph } from './lib/import-graph.mjs'
|
|
51
|
+
import { generateMutants, applyMutant } from './lib/mutation-ops.mjs'
|
|
52
|
+
import { copyRepo } from './lib/repo-copy.mjs'
|
|
53
|
+
import { surveyParallel, reportSurvey } from './lib/survey-report.mjs'
|
|
54
|
+
// 副本与抽样汇总搬去 lib/,仍从这里再导出:测试与外部只认 tools/mutate.mjs 这个入口。
|
|
55
|
+
export { copyRepo }
|
|
52
56
|
|
|
53
57
|
const HERE = dirname(fileURLToPath(import.meta.url))
|
|
54
58
|
// 仓库根可覆盖:端到端测试要跑**受控夹具**,不能跑在真实仓库的测试上——
|
|
@@ -60,79 +64,54 @@ const argv = process.argv.slice(2)
|
|
|
60
64
|
const AS_JSON = argv.includes('--json')
|
|
61
65
|
const LIST_ONLY = argv.includes('--list')
|
|
62
66
|
const STRICT = argv.includes('--strict')
|
|
67
|
+
/** 全模块抽样(3.5.4)。与单模块模式互斥:走 survey 时不看 targets。 */
|
|
68
|
+
const SURVEY = argv.includes('--survey')
|
|
63
69
|
const numArg = (flag, dflt) => {
|
|
64
70
|
const i = argv.indexOf(flag)
|
|
65
71
|
return i >= 0 && argv[i + 1] ? Number(argv[i + 1]) : dflt
|
|
66
72
|
}
|
|
67
|
-
const MAX = numArg('--max', Infinity)
|
|
68
|
-
const MAX_SURVIVE = numArg('--max-survive', 40)
|
|
69
|
-
const PER_MUTANT_TIMEOUT = numArg('--timeout', 60000)
|
|
70
|
-
|
|
71
73
|
/**
|
|
72
|
-
*
|
|
74
|
+
* 并行度与分片(2026-11-27)。
|
|
75
|
+
*
|
|
76
|
+
* 串行实测 **约 25 秒/模块**(78 个模块 ≈ 30 分钟)——卡在 `node --test` 的启动与
|
|
77
|
+
* 慢文件(`acceptance.test.mjs` 单文件 29 秒),与变异算法无关。计划里的判据是
|
|
78
|
+
* 「一条命令 ≤5 分钟」,串行永远达不到,所以必须并行。
|
|
73
79
|
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
* 所以 `plain` 里的下标可以直接映射回原文件(下面有长度断言兜底)。
|
|
80
|
+
* 分片而不是线程:每个分片是一个子进程,各建各的临时副本。模块之间只读共享源码、
|
|
81
|
+
* 只写自己那一个文件,进程隔离后连「测试互相污染状态」都不用担心。
|
|
77
82
|
*/
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
export function generateMutants(src) {
|
|
92
|
-
const plain = stripLiterals(src)
|
|
93
|
-
if (plain.length !== src.length) {
|
|
94
|
-
throw new Error(`stripLiterals 改变了长度(${src.length} → ${plain.length}),下标无法映射,拒绝变异`)
|
|
95
|
-
}
|
|
96
|
-
const lineStarts = [0]
|
|
97
|
-
for (let i = 0; i < src.length; i += 1) if (src[i] === '\n') lineStarts.push(i + 1)
|
|
98
|
-
const lineOf = (idx) => {
|
|
99
|
-
let lo = 0
|
|
100
|
-
let hi = lineStarts.length - 1
|
|
101
|
-
while (lo < hi) {
|
|
102
|
-
const mid = (lo + hi + 1) >> 1
|
|
103
|
-
if (lineStarts[mid] <= idx) lo = mid
|
|
104
|
-
else hi = mid - 1
|
|
105
|
-
}
|
|
106
|
-
return lo + 1
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const out = []
|
|
110
|
-
for (const op of OPERATORS) {
|
|
111
|
-
for (const m of plain.matchAll(op.re)) {
|
|
112
|
-
const token = m[1]
|
|
113
|
-
const replacement = op.flip[token]
|
|
114
|
-
if (replacement === undefined) continue
|
|
115
|
-
out.push({ op: op.id, index: m.index, token, replacement, line: lineOf(m.index) })
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
return out.sort((a, b) => a.index - b.index)
|
|
119
|
-
}
|
|
83
|
+
const JOBS = numArg('--jobs', 4)
|
|
84
|
+
const SHARD = (() => {
|
|
85
|
+
const i = argv.indexOf('--shard')
|
|
86
|
+
if (i < 0) return null
|
|
87
|
+
const [a, b] = String(argv[i + 1] || '').split('/').map(Number)
|
|
88
|
+
return a >= 0 && b > 0 ? { index: a, of: b } : null
|
|
89
|
+
})()
|
|
90
|
+
// survey 默认每模块抽 3 个:不设上限就是「全量跑全模块」,一轮几小时,没人会跑第二次。
|
|
91
|
+
const MAX = numArg('--max', SURVEY ? 3 : Infinity)
|
|
92
|
+
const MAX_SURVIVE = numArg('--max-survive', 40)
|
|
93
|
+
// survey 的单次超时给到 180 秒:10 路并行时机器被占满,`core/` 那批含 acceptance 的
|
|
94
|
+
// 测试集平时 26 秒,负载下会顶到 60 秒以上。超时被算成「基线未通过」会把人指去查断言。
|
|
95
|
+
const PER_MUTANT_TIMEOUT = numArg('--timeout', SURVEY ? 180000 : 60000)
|
|
120
96
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
97
|
+
// 算子与生成/应用搬去 lib/mutation-ops.mjs(3.5.4):纯函数,与编排无关。
|
|
98
|
+
// 仍从这里再导出——测试与外部只认 tools/mutate.mjs 这个入口。
|
|
99
|
+
export { generateMutants, applyMutant }
|
|
125
100
|
|
|
126
101
|
function runTests(cwd, testFiles) {
|
|
127
102
|
const env = {
|
|
128
103
|
...process.env,
|
|
129
104
|
// 隔离:子进程不许继承宿主的工作区根,否则变异跑出来的审计会写脏真实 AUDIT.log
|
|
130
|
-
// (`npm run eval` 上踩过:单跑一次写脏 62
|
|
105
|
+
// (`npm run eval` 上踩过:单跑一次写脏 62 行)。**清空这三个就够了**——
|
|
106
|
+
// 剩下没清空的路径都由 `process.cwd()` 推出来,而 cwd 是临时副本,写脏也只在副本里。
|
|
131
107
|
ZCODE_PROJECT_DIR: '',
|
|
132
108
|
CLAUDE_PROJECT_DIR: '',
|
|
133
109
|
FG_PROJECT_DIR: '',
|
|
134
110
|
FG_SHELL: '',
|
|
135
|
-
|
|
111
|
+
// 2026-11-27(3.5.4):**不要设 `FG_AUDIT_FILE`**。原先设成副本里的
|
|
112
|
+
// `.focus-guard/mutate-audit.log`,本意是「别写真实流水」,实际把 26 条用例弄红了——
|
|
113
|
+
// 那些用例断言的是默认审计路径上的内容,改了文件名就等于让它们断言一个空文件。
|
|
114
|
+
// 实测:只设这一个变量 → 435 过 / 26 红;只清空三个 PROJECT_DIR → 461 全过。
|
|
136
115
|
}
|
|
137
116
|
// **必须删掉 NODE_TEST_CONTEXT(2026-10-27 实测)。**
|
|
138
117
|
// 这个变量在 `node --test` 里会被设成 `child-v8`,子进程继承后 `node --test` 会**恒 exit 0**,
|
|
@@ -161,9 +140,8 @@ function runTests(cwd, testFiles) {
|
|
|
161
140
|
// 直接撞上阈值 80,把结构维从 28 条推到 29 条。棘轮的规矩是只许降不许升,
|
|
162
141
|
// 所以拆代码,不是调上限。
|
|
163
142
|
|
|
164
|
-
/**
|
|
165
|
-
function planTargets(targets) {
|
|
166
|
-
const g = buildImportGraph(REPO)
|
|
143
|
+
/** 解析目标模块,算变异计划。`g` 可复用:survey 一次建图跑全模块,不重复建。 */
|
|
144
|
+
function planTargets(targets, g = buildImportGraph(REPO)) {
|
|
167
145
|
const plans = targets.map((t) => {
|
|
168
146
|
const abs = resolve(g.srcDir, t)
|
|
169
147
|
try {
|
|
@@ -174,8 +152,16 @@ function planTargets(targets) {
|
|
|
174
152
|
}
|
|
175
153
|
const src = readFileSync(abs, 'utf8')
|
|
176
154
|
const all = generateMutants(src)
|
|
177
|
-
|
|
178
|
-
|
|
155
|
+
// 抽样必须**等距**,不能取前 N 个(2026-11-27 实测)。
|
|
156
|
+
// 取前 3 个拿到的是文件最开头那几个变异体——那里通常是 import 与常量表,
|
|
157
|
+
// 改了对行为没影响(等价变异体)或压根没断言。结果:一批模块齐刷刷 0 分,
|
|
158
|
+
// 与单模块全量跑出来的 50%~100% 完全对不上。等距至少覆盖文件首中尾。
|
|
159
|
+
const selected =
|
|
160
|
+
Number.isFinite(MAX) && all.length > MAX
|
|
161
|
+
? Array.from({ length: MAX }, (_, k) => all[Math.floor((k * all.length) / MAX)])
|
|
162
|
+
: all
|
|
163
|
+
// `rel` 与 `name` 都要:单模块模式看 `name` 够,survey 里两个 `index.mjs` 只能靠路径分开。
|
|
164
|
+
return { abs, name: basename(abs), rel: t, src, all, mutants: selected, tests: g.reachingTests(abs) }
|
|
179
165
|
})
|
|
180
166
|
return { g, plans }
|
|
181
167
|
}
|
|
@@ -201,7 +187,7 @@ function listPlans(plans) {
|
|
|
201
187
|
function runModule(work, g, p) {
|
|
202
188
|
const targetInCopy = join(work, 'packages/core/src', p.abs.slice(g.srcDir.length + 1))
|
|
203
189
|
const testsInCopy = p.tests.map((t) => join(work, t.slice(REPO.length + 1)))
|
|
204
|
-
const base = { name: p.name, total: p.mutants.length, generated: p.all.length, tests: p.tests.length }
|
|
190
|
+
const base = { name: p.name, rel: p.rel, total: p.mutants.length, generated: p.all.length, tests: p.tests.length }
|
|
205
191
|
|
|
206
192
|
if (!p.mutants.length) return { ...base, skipped: '该模块没有可变异算子(纯常量表或纯声明)' }
|
|
207
193
|
if (!p.tests.length) {
|
|
@@ -217,9 +203,17 @@ function runModule(work, g, p) {
|
|
|
217
203
|
const b2 = b1.ok ? b1 : runTests(work, testsInCopy)
|
|
218
204
|
if (!b1.ok || !b2.ok) {
|
|
219
205
|
const flaky = b1.ok !== b2.ok
|
|
206
|
+
// 超时与「测试真的红了」必须分开报(2026-11-27)。并行抽样时机器被占满,
|
|
207
|
+
// 一个平时 26 秒的测试集可能顶到 60 秒超时;报成「基线未通过」会让人去查断言,
|
|
208
|
+
// 而实际要调的是 `--timeout`。**报错指错方向,比不报还费时间。**
|
|
209
|
+
const why = b1.timedOut || b2.timedOut
|
|
210
|
+
? '基线超时(单次测试跑不完,多半是并行负载下超时,调大 --timeout)'
|
|
211
|
+
: flaky
|
|
212
|
+
? '基线不稳定(一次红一次绿)'
|
|
213
|
+
: '基线测试未通过'
|
|
220
214
|
return {
|
|
221
215
|
...base,
|
|
222
|
-
skipped: `${
|
|
216
|
+
skipped: `${why},拒绝变异。偶发失败会被算成"杀死",变异得分不可信:${b2.out.slice(-300)}`,
|
|
223
217
|
}
|
|
224
218
|
}
|
|
225
219
|
|
|
@@ -278,13 +272,70 @@ function report(results) {
|
|
|
278
272
|
return STRICT && ran > 0 && (alive / ran) * 100 > MAX_SURVIVE ? 1 : 0
|
|
279
273
|
}
|
|
280
274
|
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* `--survey`:全模块抽样出分(2026-11-27,3.5.4)。
|
|
279
|
+
*
|
|
280
|
+
* 为什么要它:单模块模式回答「这个模块的断言密不密」,但排期需要的是
|
|
281
|
+
* 「**75 个模块里哪些最薄**」——没有全表,就只能凭印象挑,挑完再跑一轮,一轮几十分钟。
|
|
282
|
+
*
|
|
283
|
+
* 抽样而不是全量:`--max` 默认 3。全量跑全模块是一轮几小时的事,没人会跑第二次,
|
|
284
|
+
* 而**没人会跑第二次的尺子等于没有**。3 个变异体足以区分「有断言」与「没断言」,
|
|
285
|
+
* 分不出 92% 与 95%——那也不需要在这里分。
|
|
286
|
+
*
|
|
287
|
+
* 基线红的模块会自己标「跳过」并说明原因,不混进平均分:把「测不了」算成「0 分」
|
|
288
|
+
* 会让平均分变成「副本能不能跑」的函数,而不是「断言密不密」的函数。
|
|
289
|
+
*/
|
|
290
|
+
function runSurvey() {
|
|
291
|
+
const g = buildImportGraph(REPO)
|
|
292
|
+
let targets = g.srcFiles.map((f) => relative(g.srcDir, f).split(sep).join('/'))
|
|
293
|
+
// 分片按取模切:源码顺序稳定,同输入同分片,结果可复现。
|
|
294
|
+
if (SHARD) targets = targets.filter((_, k) => k % SHARD.of === SHARD.index)
|
|
295
|
+
const { plans } = planTargets(targets, g)
|
|
296
|
+
const work = mkdtempSync(join(tmpdir(), 'fg-mutate-'))
|
|
297
|
+
copyRepo(REPO, work)
|
|
298
|
+
const rows = []
|
|
299
|
+
try {
|
|
300
|
+
for (const [i, p] of plans.entries()) {
|
|
301
|
+
// 模块之间必须清运行时状态(2026-11-27 实测)。
|
|
302
|
+
// 串行跑同一副本时,前一个模块的测试会把 `.focus-guard/` 写脏;后一个模块的基线
|
|
303
|
+
// 读到脏状态就红,于是被标成「基线测试未通过」。实测 `core/risk.mjs` 单跑 100%,
|
|
304
|
+
// 在 survey 里却报基线红——**同一份源码两种结论,只能是状态污染**。
|
|
305
|
+
rmSync(join(work, '.focus-guard'), { recursive: true, force: true })
|
|
306
|
+
process.stderr.write(`[${i + 1}/${plans.length}] ${p.rel} … `)
|
|
307
|
+
const r = runModule(work, g, p)
|
|
308
|
+
rows.push(r)
|
|
309
|
+
process.stderr.write(r.skipped ? '跳过\n' : `${r.score}%\n`)
|
|
310
|
+
}
|
|
311
|
+
} finally {
|
|
312
|
+
rmSync(work, { recursive: true, force: true })
|
|
313
|
+
}
|
|
314
|
+
if (SHARD) {
|
|
315
|
+
// 子进程只交数据,排版交给父进程——两边各排一份必然分叉。
|
|
316
|
+
console.log(`__ROWS__${JSON.stringify(rows)}`)
|
|
317
|
+
return 0
|
|
318
|
+
}
|
|
319
|
+
reportSurvey(rows)
|
|
320
|
+
return 0
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
281
325
|
function main() {
|
|
326
|
+
if (SURVEY) {
|
|
327
|
+
// 分片进程与单进程都走 runSurvey;只有「父进程 + jobs>1」才并行派发。
|
|
328
|
+
if (SHARD || JOBS <= 1) process.exit(runSurvey())
|
|
329
|
+
surveyParallel({ jobs: JOBS, self: fileURLToPath(import.meta.url), repo: REPO }).then(() => process.exit(0))
|
|
330
|
+
return
|
|
331
|
+
}
|
|
282
332
|
const targets = argv.filter(
|
|
283
333
|
(a, i) => !a.startsWith('--') && !/^\d+$/.test(a) && !['--max', '--max-survive', '--timeout'].includes(argv[i - 1]),
|
|
284
334
|
)
|
|
285
335
|
if (!targets.length) {
|
|
286
336
|
console.error('用法:node packages/core/tools/mutate.mjs <src 相对路径...> [--list] [--max n] [--json] [--strict]')
|
|
287
|
-
console.error('
|
|
337
|
+
console.error(' node packages/core/tools/mutate.mjs --survey [--max n] # 全模块抽样出分')
|
|
338
|
+
console.error(' 例:node packages/core/tools/mutate.mjs adapters/dsh/toolName.mjs')
|
|
288
339
|
process.exit(2)
|
|
289
340
|
}
|
|
290
341
|
|
|
@@ -296,12 +347,7 @@ function main() {
|
|
|
296
347
|
|
|
297
348
|
// 复制到临时目录:真实源码永不被改
|
|
298
349
|
const work = mkdtempSync(join(tmpdir(), 'fg-mutate-'))
|
|
299
|
-
|
|
300
|
-
try {
|
|
301
|
-
cpSync(join(REPO, 'package.json'), join(work, 'package.json'))
|
|
302
|
-
} catch {
|
|
303
|
-
/* package.json 不是必需 */
|
|
304
|
-
}
|
|
350
|
+
copyRepo(REPO, work)
|
|
305
351
|
|
|
306
352
|
const results = []
|
|
307
353
|
try {
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* panel:baseline(2026-11-24)—— 补 ARCH-MAP 缺口 #5「面板基线」
|
|
4
|
+
*
|
|
5
|
+
* 为什么不依赖 dsh-context:FG 的基线必须离线可复算。本工具只读宿主的会话日志,
|
|
6
|
+
* 按**每次请求**取 `usage.{inputTokens,outputTokens,cacheReadTokens}`,
|
|
7
|
+
* 用 `peak-cost.mjs::calcCost` 计价(与成本台账同一函数)。
|
|
8
|
+
*
|
|
9
|
+
* 口径(写进输出,别人能复算):
|
|
10
|
+
* inputTokens = 未命中缓存的输入(宿主字段语义,2026-11-24 对账)
|
|
11
|
+
* promptTotal = inputTokens + cacheReadTokens
|
|
12
|
+
* hitRate = cacheReadTokens / promptTotal
|
|
13
|
+
* usd = calcCost(model, {input:inputTokens,output,cacheRead}, inPeak)
|
|
14
|
+
*
|
|
15
|
+
* 用法:
|
|
16
|
+
* node packages/core/tools/panel-baseline.mjs # 最近 10 个会话
|
|
17
|
+
* node packages/core/tools/panel-baseline.mjs --last 30
|
|
18
|
+
* node packages/core/tools/panel-baseline.mjs --session c00d # 前缀匹配
|
|
19
|
+
* node packages/core/tools/panel-baseline.mjs --json
|
|
20
|
+
* node packages/core/tools/panel-baseline.mjs --write # 写 docs/PANEL-BASELINE.json
|
|
21
|
+
*/
|
|
22
|
+
import { writeFileSync } from 'node:fs'
|
|
23
|
+
import { dirname, join, resolve } from 'node:path'
|
|
24
|
+
import { fileURLToPath } from 'node:url'
|
|
25
|
+
import { calcCost, isPeakAt } from '../src/peak-cost.mjs'
|
|
26
|
+
import { decodeSession, walkSessionFiles } from './lib/sessionDump.mjs'
|
|
27
|
+
|
|
28
|
+
const HERE = dirname(fileURLToPath(import.meta.url))
|
|
29
|
+
const ROOT = resolve(HERE, '..', '..', '..')
|
|
30
|
+
const HOME = process.env.DSH_HOME || join(ROOT, '..')
|
|
31
|
+
const SESSIONS = HOME.endsWith('.dsh') ? join(HOME, 'sessions') : join(HOME, '.dsh', 'sessions')
|
|
32
|
+
|
|
33
|
+
const args = process.argv.slice(2)
|
|
34
|
+
const pick = (flag) => {
|
|
35
|
+
const i = args.indexOf(flag)
|
|
36
|
+
return i >= 0 ? args[i + 1] : null
|
|
37
|
+
}
|
|
38
|
+
const last = Number(pick('--last') || 10)
|
|
39
|
+
const only = pick('--session')
|
|
40
|
+
const asJson = args.includes('--json')
|
|
41
|
+
const shouldWrite = args.includes('--write')
|
|
42
|
+
const byTool = args.includes('--by-tool')
|
|
43
|
+
const bySchema = args.includes('--schema')
|
|
44
|
+
const inPeak = isPeakAt(new Date()).inPeak
|
|
45
|
+
|
|
46
|
+
function sidOf(file) {
|
|
47
|
+
const parts = file.replace(/\\/g, '/').split('/')
|
|
48
|
+
return parts[parts.length - 2] + '/' + (parts[parts.length - 3] || '')
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const rows = []
|
|
52
|
+
const toolUse = new Map()
|
|
53
|
+
const schemaUse = new Map()
|
|
54
|
+
// `--session` 只看**最新**那一个匹配(walkSessionFiles 已按 mtime 倒序):
|
|
55
|
+
// 同名会话在多个项目目录下可能有历史副本,遍历全部会让旧的工具面覆盖当前值。
|
|
56
|
+
const scanFiles = only ? walkSessionFiles(SESSIONS).filter((x) => x.f.includes(only)).slice(0, 1) : walkSessionFiles(SESSIONS)
|
|
57
|
+
for (const { f, m } of scanFiles) {
|
|
58
|
+
let lines
|
|
59
|
+
try {
|
|
60
|
+
lines = decodeSession(f)
|
|
61
|
+
} catch {
|
|
62
|
+
continue
|
|
63
|
+
}
|
|
64
|
+
let calls = 0
|
|
65
|
+
let input = 0
|
|
66
|
+
let output = 0
|
|
67
|
+
let cacheRead = 0
|
|
68
|
+
let tools = 0
|
|
69
|
+
let model = ''
|
|
70
|
+
for (const j of lines) {
|
|
71
|
+
const header = j.data && (j.data.header || j.data)
|
|
72
|
+
if (header && Array.isArray(header.tools)) {
|
|
73
|
+
tools = header.tools.length
|
|
74
|
+
model = model || header.model || ''
|
|
75
|
+
if (bySchema) {
|
|
76
|
+
// 每个 header 重建一份:工具面以**最后一个装配**为准,历史里出现过的工具不保留。
|
|
77
|
+
const next = new Map()
|
|
78
|
+
for (const t of header.tools) {
|
|
79
|
+
const name = String(t && t.name ? t.name : '?')
|
|
80
|
+
next.set(name, { bytes: Buffer.byteLength(JSON.stringify(t), 'utf8') })
|
|
81
|
+
}
|
|
82
|
+
schemaUse.clear()
|
|
83
|
+
for (const [k, v] of next) schemaUse.set(k, v)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const u = (j.usage && j.usage.inputTokens !== undefined ? j.usage : null) || (j.data && j.data.usage) || null
|
|
87
|
+
if (!u || u.inputTokens === undefined) continue
|
|
88
|
+
calls += 1
|
|
89
|
+
input += Number(u.inputTokens) || 0
|
|
90
|
+
output += Number(u.outputTokens) || 0
|
|
91
|
+
cacheRead += Number(u.cacheReadTokens) || 0
|
|
92
|
+
}
|
|
93
|
+
if (!calls) continue
|
|
94
|
+
if (byTool) {
|
|
95
|
+
for (const j of lines) {
|
|
96
|
+
if (j.type !== 'tool/call') continue
|
|
97
|
+
const d = j.data || {}
|
|
98
|
+
const name = String(d.name || '?')
|
|
99
|
+
const a = typeof d.arguments === 'string' ? d.arguments : JSON.stringify(d.arguments || {})
|
|
100
|
+
const cur = toolUse.get(name) || { calls: 0, argBytes: 0 }
|
|
101
|
+
cur.calls += 1
|
|
102
|
+
cur.argBytes += Buffer.byteLength(a, 'utf8')
|
|
103
|
+
toolUse.set(name, cur)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const promptTotal = input + cacheRead
|
|
107
|
+
const usage = { input, output, cacheRead }
|
|
108
|
+
rows.push({
|
|
109
|
+
session: sidOf(f),
|
|
110
|
+
date: new Date(m).toISOString().slice(0, 16),
|
|
111
|
+
calls,
|
|
112
|
+
tools,
|
|
113
|
+
input,
|
|
114
|
+
cacheRead,
|
|
115
|
+
promptTotal,
|
|
116
|
+
output,
|
|
117
|
+
hitRate: Number((cacheRead / Math.max(1, promptTotal)).toFixed(4)),
|
|
118
|
+
usd: calcCost(model || 'deepseek-flash', usage, inPeak),
|
|
119
|
+
formula: 'usd=calcCost(model,{input:uncached,output,cacheRead},inPeak); promptTotal=uncached+cacheRead',
|
|
120
|
+
})
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const picked = rows.sort((a, b) => (a.date < b.date ? 1 : -1)).slice(0, only ? 999 : last)
|
|
124
|
+
const report = {
|
|
125
|
+
generatedAt: new Date().toISOString(),
|
|
126
|
+
sessionsDir: SESSIONS,
|
|
127
|
+
peak: inPeak,
|
|
128
|
+
rows: picked,
|
|
129
|
+
totals: picked.reduce(
|
|
130
|
+
(a, r) => ({
|
|
131
|
+
calls: a.calls + r.calls,
|
|
132
|
+
promptTotal: a.promptTotal + r.promptTotal,
|
|
133
|
+
cacheRead: a.cacheRead + r.cacheRead,
|
|
134
|
+
uncached: a.uncached + r.input,
|
|
135
|
+
output: a.output + r.output,
|
|
136
|
+
usd: Number(((a.usd || 0) + (r.usd || 0)).toFixed(6)),
|
|
137
|
+
}),
|
|
138
|
+
{ calls: 0, promptTotal: 0, cacheRead: 0, uncached: 0, output: 0, usd: 0 },
|
|
139
|
+
),
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (shouldWrite) {
|
|
143
|
+
const p = join(ROOT, 'docs', 'PANEL-BASELINE.json')
|
|
144
|
+
writeFileSync(p, JSON.stringify(report, null, 2) + '\n')
|
|
145
|
+
console.log(`[panel:baseline] 写入 ${p}`)
|
|
146
|
+
}
|
|
147
|
+
if (asJson) {
|
|
148
|
+
console.log(JSON.stringify(report, null, 2))
|
|
149
|
+
} else {
|
|
150
|
+
console.log(`[panel:baseline] ${picked.length} 个会话 · 峰谷=${inPeak ? '峰' : '谷'}`)
|
|
151
|
+
for (const r of picked) {
|
|
152
|
+
console.log(
|
|
153
|
+
` ${r.date} ${r.session.slice(0, 26)} calls=${String(r.calls).padStart(4)} ` +
|
|
154
|
+
`tools=${String(r.tools).padStart(3)} prompt=${r.promptTotal} cache=${r.cacheRead} ` +
|
|
155
|
+
`uncached=${r.input} out=${r.output} hit=${r.hitRate} usd=${r.usd}`,
|
|
156
|
+
)
|
|
157
|
+
}
|
|
158
|
+
if (byTool && toolUse.size) {
|
|
159
|
+
console.log(' 工具调用(按次数):')
|
|
160
|
+
for (const [name, v] of [...toolUse].sort((a, b) => b[1].calls - a[1].calls).slice(0, 12)) {
|
|
161
|
+
console.log(` ${name.padEnd(18)} calls=${String(v.calls).padStart(5)} args≈tok=${Math.round(v.argBytes / 3)}`)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (bySchema && schemaUse.size) {
|
|
165
|
+
console.log(' 工具 schema(每步都要付,按字节):')
|
|
166
|
+
for (const [name, v] of [...schemaUse].sort((a, b) => b[1].bytes - a[1].bytes).slice(0, 12)) {
|
|
167
|
+
console.log(` ${name.padEnd(18)} schema=${String(v.bytes).padStart(5)}B ≈tok=${Math.round(v.bytes / 3)}`)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const t = report.totals
|
|
171
|
+
console.log(` 合计:calls=${t.calls} prompt=${t.promptTotal} cache=${t.cacheRead} uncached=${t.uncached} out=${t.output} usd=${t.usd}`)
|
|
172
|
+
}
|
package/tools/prefix-audit.mjs
CHANGED
|
@@ -78,8 +78,16 @@ const skillsChars = blockLen(firstUserText, '<available_skills>', '</available_s
|
|
|
78
78
|
// 锚点用两版注入块的公共前缀 `<focus-guard `:2026-10-24 前的完整版开头是
|
|
79
79
|
// `focus-guard AI履职执法模型v3.0`,之后的精简版是 `focus-guard v3.0`。旧锚点只认前者,
|
|
80
80
|
// 精简版上线后 indexOf 恒 -1 → 本行恒报 0(读起来像"没成本")。历史会话仍要能测,故取公共前缀。
|
|
81
|
-
|
|
82
|
-
|
|
81
|
+
//
|
|
82
|
+
// 2026-11-20 修第二层:FG 注入已从**首个 user 消息**搬到 **system/message**。
|
|
83
|
+
// 实测 6/6 会话 `[FG-WORKSPACE]` 与 `<focus-guard ` 全部命中 system、user 命中 0 条,
|
|
84
|
+
// 而旧口径只查 firstUserText → 两行双双恒报 0。现两处都查:
|
|
85
|
+
// 历史会话(注入在 user)仍能测,当前会话(注入在 system)不再漏。
|
|
86
|
+
// 注意:FG 注入是 system 段的**子集**,只作展示,不参与 totalFixed 合计(避免重复计数)。
|
|
87
|
+
const sysCorpus = sysMsgs.map((l) => JSON.stringify((l.data && l.data.content) || l.data || '')).join('\n')
|
|
88
|
+
const fgCorpus = `${firstUserText}\n${sysCorpus}`
|
|
89
|
+
const fgRulesChars = blockLen(fgCorpus, '<focus-guard ', 'docs/RULES.md')
|
|
90
|
+
const fgWsChars = blockLen(fgCorpus, '[FG-WORKSPACE]', '决策:') || 0
|
|
83
91
|
const costChars = blockLen(firstUserText, '【成本】', '【成本】') || 20
|
|
84
92
|
|
|
85
93
|
const steps = lines.filter((l) => l.type === 'step/start').length
|
package/tools/prefix-drift.mjs
CHANGED
|
@@ -3,8 +3,12 @@
|
|
|
3
3
|
* 前缀抖动探针(2026-10-29)
|
|
4
4
|
*
|
|
5
5
|
* 目的:回答"每步的前缀到底是不是同一段字节"。缓存命中要求**前缀逐字节相同**,
|
|
6
|
-
* 而 FG
|
|
7
|
-
*
|
|
6
|
+
* 而 FG 自己往系统提示里注入卷宗段——若它每回合变一次,前缀就不再稳定。
|
|
7
|
+
*
|
|
8
|
+
* 2026-11-26 注记:卷宗段已于 11-21 改 pull(不再进 system),decisions 自动落账同月下架。
|
|
9
|
+
* 因此**当前生产路径已无每回合增长的注入源**;本工具的 `--ab` 仍保留,用途有两个:
|
|
10
|
+
* 一是对照历史(论证 pull 与下架为什么对),二是压测唯一还会增长的 constraints
|
|
11
|
+
* (人类连续否决命令)会不会传到前缀。别把它的旧曲线当成现状。
|
|
8
12
|
*
|
|
9
13
|
* 本探针只读会话落盘(`session.v4.jsonl.zstd`),按 step 切段,逐段比哈希:
|
|
10
14
|
* · 工具表 / system 段 / developer 段 / 首个 user 段,各自一个指纹
|
|
@@ -124,7 +128,7 @@ async function runAB() {
|
|
|
124
128
|
const savedAudit = process.env.FG_AUDIT_FILE
|
|
125
129
|
process.env.FG_WORKSPACE_FILE = join(TMP, 'workspace.json')
|
|
126
130
|
process.env.FG_AUDIT_FILE = join(TMP, 'AUDIT.log')
|
|
127
|
-
const url = new URL('../src/
|
|
131
|
+
const url = new URL('../src/memory/index.mjs', import.meta.url).href
|
|
128
132
|
const ws = await import(url)
|
|
129
133
|
const WORK = join(TMP, 'work')
|
|
130
134
|
const SID_A = 'sess-A'
|
|
@@ -132,7 +136,6 @@ async function runAB() {
|
|
|
132
136
|
const TURNS = 30
|
|
133
137
|
// 预置上一场留下的卷宗(真实场景里会话开始前文件就在了)
|
|
134
138
|
ws.mergeWorkspace(WORK, {
|
|
135
|
-
decisions: ['上一场留下的决策'],
|
|
136
139
|
constraints: ['npm publish'],
|
|
137
140
|
snapshot: { at: '2026-10-29T09:00:00.000Z', sid: 'sess-prev', toolCalls: 40, denies: 2, kpi: -9, delegateBudget: 12 },
|
|
138
141
|
})
|
|
@@ -144,7 +147,9 @@ async function runAB() {
|
|
|
144
147
|
seqA.push(String(ws.renderWorkspaceSection(rec, { sid: SID_A }) || ''))
|
|
145
148
|
seqB.push(String(ws.renderWorkspaceSectionOnce(rec, { sid: SID_A }) || ''))
|
|
146
149
|
ws.mergeWorkspace(WORK, {
|
|
147
|
-
decisions
|
|
150
|
+
// 2026-11-26:decisions 已下架,改用 constraints 当增长源——它是现在唯一还会
|
|
151
|
+
// 随回合变长的卷宗字段(人类连续否决命令),也是真实场景里最可能压到前缀的一个。
|
|
152
|
+
constraints: [`第 ${t} 回合被否决的命令`],
|
|
148
153
|
snapshot: { at: `2026-10-29T10:${String(t).padStart(2, '0')}:00.000Z`, sid: SID_A, toolCalls: t, denies: 0, kpi: 0, delegateBudget: 20 },
|
|
149
154
|
})
|
|
150
155
|
if (t % 3 === 0) {
|