focus-guard 3.0.7 → 3.4.0
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/LICENSE +683 -21
- package/NOTICE +25 -0
- package/README.md +14 -9
- package/README.zh-CN.md +12 -9
- package/docs/AB-PROMPTS.md +123 -0
- package/docs/AB-SESSION.md +161 -0
- package/docs/ARCH-MAP.md +148 -0
- package/docs/BENCH-BASELINE.json +95 -0
- package/docs/BENCH.md +323 -0
- package/docs/GATE-INPUTS.json +10 -0
- package/docs/HANDOFF-2026-10-27.md +736 -0
- package/docs/HOOK-ORDERING.md +139 -0
- package/docs/LEGISLATION-LAW.md +4 -2
- package/docs/MCP-4.0-PLAN.md +184 -0
- package/docs/MEMORY-4.5-PLAN.md +415 -0
- package/docs/PROFILE-TUNING.md +147 -0
- package/docs/RATCHET.json +48 -0
- package/docs/RELEASE-POLICY.md +163 -0
- package/docs/RULES.md +95 -14
- package/docs/THREAT-MODEL.md +68 -0
- package/docs/TOOL-SCHEMA.json +6 -0
- package/docs/VERSION-PLAN.md +532 -0
- package/docs/WRITING-STYLE.md +109 -0
- package/docs/architecture-map.mermaid +186 -0
- package/docs/deepseek-pricing-audit.md +2 -0
- package/hooks/guard.mjs +92 -56
- package/package.json +34 -5
- package/skills/focus-thinking/SKILL.md +9 -7
- package/src/adapters/dsh/eligibility-gate.mjs +446 -23
- package/src/adapters/dsh/tool-kinds.mjs +81 -0
- package/src/core/approval.mjs +2 -8
- package/src/core/approvals.mjs +208 -0
- package/src/core/audit-writer.mjs +297 -0
- package/src/core/audit.mjs +46 -16
- package/src/core/backup.mjs +37 -11
- package/src/core/checkEligibility.mjs +7 -4
- package/src/core/constants.mjs +67 -22
- package/src/core/decisionEngine.mjs +12 -0
- package/src/core/env.mjs +147 -19
- package/src/core/grants.mjs +54 -13
- package/src/core/kpi.mjs +16 -0
- package/src/core/outputGate.mjs +77 -0
- package/src/core/patterns.mjs +129 -0
- package/src/core/prefixStability.mjs +143 -0
- package/src/core/profileLoader.mjs +40 -5
- package/src/core/progressPolicy.mjs +65 -0
- package/src/core/redlines.mjs +49 -5
- package/src/core/risk.mjs +94 -15
- package/src/core/schemaDrift.mjs +125 -0
- package/src/core/sessionScratch.mjs +147 -0
- package/src/core/stageGraph.mjs +104 -0
- package/src/core/staleCleanup.mjs +153 -0
- package/src/core/state.mjs +215 -81
- package/src/core/stopPolicy.mjs +97 -0
- package/src/core/tempPath.mjs +23 -0
- package/src/dsh/audit.mjs +168 -118
- package/src/dsh/compactionWatch/announce.mjs +117 -0
- package/src/dsh/compactionWatch.mjs +68 -153
- package/src/dsh/guard/authz.mjs +97 -0
- package/src/dsh/guard/evidence.mjs +308 -0
- package/src/dsh/guard/hardCheck.mjs +85 -0
- package/src/dsh/guard/redline.mjs +62 -0
- package/src/dsh/guard/risk.mjs +196 -0
- package/src/dsh/guard/runtime.mjs +162 -0
- package/src/dsh/guard/superpowers.mjs +70 -0
- package/src/dsh/guard/tooling.mjs +177 -0
- package/src/dsh/guard/volume.mjs +181 -0
- package/src/dsh/guard/wholeFile.mjs +109 -0
- package/src/dsh/heartbeat.mjs +76 -13
- package/src/dsh/hookOrder.mjs +101 -0
- package/src/dsh/index.js +247 -15
- package/src/dsh/pipeline.mjs +77 -854
- package/src/dsh/policy-audit.mjs +84 -0
- package/src/dsh/postProgress.mjs +400 -274
- package/src/dsh/preGuard.mjs +249 -228
- package/src/dsh/repeatGuard.mjs +98 -0
- package/src/dsh/seams/pre-step.mjs +186 -0
- package/src/dsh/seams/prompt-rules.mjs +59 -0
- package/src/dsh/seams/session-start.mjs +297 -0
- package/src/dsh/seams/shared.mjs +109 -0
- package/src/dsh/seams.mjs +95 -408
- package/src/dsh/search-hints.mjs +136 -0
- package/src/dsh/sessionScope.mjs +28 -0
- package/src/dsh/status.mjs +7 -0
- package/src/dsh/stopGuard.mjs +225 -164
- package/src/dsh/testRuntime.mjs +62 -0
- package/src/dsh/toolKindAudit.mjs +66 -0
- package/src/dsh/tools.mjs +81 -5
- package/src/dsh/traceMeta.mjs +125 -0
- package/src/dsh/value-gate.mjs +93 -0
- package/src/dsh/workspace.mjs +3 -143
- package/src/memory/index.mjs +15 -0
- package/src/memory/tags.mjs +30 -0
- package/src/memory/workspace.mjs +283 -0
- package/src/peak-cost.mjs +1 -1
- package/src/profiles/deepseek-flash.mjs +7 -1
- package/src/profiles/deepseek-pro.mjs +6 -1
- package/src/profiles/default.mjs +6 -1
- package/src/profiles/glm.mjs +6 -1
- package/src/profiles/gpt-astra.mjs +6 -1
- package/tools/bench-ab.mjs +240 -0
- package/tools/bench-cost.mjs +176 -0
- package/tools/brain-cosmic.mjs +43 -0
- package/tools/brain-view.html +853 -0
- package/tools/brain.mjs +317 -0
- package/tools/capture-fixtures.mjs +182 -0
- package/tools/case-audit.mjs +114 -0
- package/tools/collect-panel.mjs +218 -0
- package/tools/dead-export.mjs +60 -0
- package/tools/docs-lint.mjs +82 -0
- package/tools/env-fingerprint.mjs +126 -33
- package/tools/first-run-sweep.mjs +123 -0
- package/tools/focusguard.mjs +155 -0
- package/tools/gate-audit.mjs +140 -0
- package/tools/gate-inputs.mjs +115 -0
- package/tools/handoff.mjs +56 -0
- package/tools/health.mjs +182 -0
- package/tools/knowledge.mjs +118 -0
- package/tools/layering-audit.mjs +105 -0
- package/tools/lib/book-style.mjs +136 -0
- package/tools/lib/brain-core.mjs +342 -0
- package/tools/lib/brain-docs.mjs +293 -0
- package/tools/lib/brain-regions.mjs +169 -0
- package/tools/lib/brain-relate.mjs +235 -0
- package/tools/lib/brain-text.mjs +121 -0
- package/tools/lib/case-tool.mjs +186 -0
- package/tools/lib/docs-core.mjs +392 -0
- package/tools/lib/docs-version.mjs +66 -0
- package/tools/lib/envMap.mjs +31 -0
- package/tools/lib/health-core.mjs +94 -0
- package/tools/lib/import-graph.mjs +118 -0
- package/tools/lib/index-io.mjs +70 -0
- package/tools/lib/open.mjs +32 -0
- package/tools/lib/prefix-cost.mjs +143 -0
- package/tools/lib/ratchet.mjs +174 -0
- package/tools/lib/reach.mjs +28 -0
- package/tools/lib/sessionDump.mjs +80 -0
- package/tools/lib/source-text.mjs +112 -0
- package/tools/library-book.mjs +313 -0
- package/tools/library-build.mjs +50 -12
- package/tools/library-index.mjs +91 -0
- package/tools/lint.mjs +282 -0
- package/tools/log-audit.mjs +108 -0
- package/tools/manifest-sync.mjs +111 -0
- package/tools/memory-audit.mjs +67 -0
- package/tools/module-audit.mjs +56 -0
- package/tools/mutate.mjs +316 -0
- package/tools/prefix-audit.mjs +122 -0
- package/tools/prefix-drift.mjs +325 -0
- package/tools/privacy-guard.mjs +352 -0
- package/tools/quality-audit.mjs +166 -0
- package/tools/schema-fingerprint.mjs +191 -0
- package/{src/dsh → tools}/selftest.mjs +75 -22
- package/tools/smells.mjs +398 -0
- package/tools/verify-case-paths.mjs +93 -0
- package/tools/version.mjs +191 -0
- package/docs/MASTER-PLAN-2.0.0.md +0 -133
- package/docs/MASTER-PLAN-3.0.0.md +0 -76
- package/src/adapters/dsh/fg-appeal-tool.mjs +0 -47
- package/src/adapters/dsh/fg-apply-tool.mjs +0 -68
- /package/{src/adapters/dsh → hooks}/protocol.mjs +0 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 首次使用自清洁(2026-11-02)
|
|
4
|
+
*
|
|
5
|
+
* ## 为什么需要
|
|
6
|
+
*
|
|
7
|
+
* 发布物里**不该有**运行期私有产物:审计日志、脑索引、卷宗、私有面目录。
|
|
8
|
+
* 现在的事实是「没有」——`npm pack` 出来 153 个文件,一个都不含(见 CHANGELOG)。
|
|
9
|
+
* 但「现在没有」靠的是 `files` 字段写对了,**而 `files` 是可以被改错的**。
|
|
10
|
+
* 这条是第二道闸:万一哪天打包配置被改坏、把开发机的数据带出去了,
|
|
11
|
+
* 下载的人第一次装完就会自动清掉,**从 0 开始,只剩随包分发的法条**。
|
|
12
|
+
*
|
|
13
|
+
* ## 判据:靠 `.git` 分辨"谁的副本"
|
|
14
|
+
*
|
|
15
|
+
* 开发副本(作者自己的仓库)里有 `.git`,那里面的私有产物是**正常数据**,
|
|
16
|
+
* 动了就是删用户的记忆。安装副本(node_modules 里的、解压的 tarball)没有 `.git`,
|
|
17
|
+
* 那里面的私有产物只可能是**误打包**。
|
|
18
|
+
* 所以:**有 `.git` 一律不动,没有才清**。这个判据比"看文件时间"、"看目录名"都硬。
|
|
19
|
+
*
|
|
20
|
+
* 再加两道保险,宁可不动:
|
|
21
|
+
* ① 只有 `package.json` 的 `name` 确实是本包才动手;
|
|
22
|
+
* ② 每个待删路径 realpath 之后必须仍在包根之内(防符号链接把删除引到别处)。
|
|
23
|
+
*
|
|
24
|
+
* ## 名字为什么可以写在这里
|
|
25
|
+
*
|
|
26
|
+
* `privacy-guard` 坚持代码里不提私有面目录名,因为它那份清单本身就是秘密。
|
|
27
|
+
* 这里不同:下面这几个名字在 README 与 `brain.mjs` 的用法说明里**已经公开**
|
|
28
|
+
* (`.focus-guard/AUDIT.log`、`.ai/CASE_FILE.md` 就是验证安装是否生效的那两行)。
|
|
29
|
+
* 写在这里不新增任何暴露面,反而少一次"名字藏在配置文件里、哪天配置文件丢了就失效"。
|
|
30
|
+
*
|
|
31
|
+
* 用法:
|
|
32
|
+
* node packages/core/tools/first-run-sweep.mjs # 检查并清理
|
|
33
|
+
* node packages/core/tools/first-run-sweep.mjs --dry # 只说会删什么
|
|
34
|
+
* 退出码:恒 0(挂在 postinstall 上,清洁失败不该让安装失败)
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
import { existsSync, readFileSync, realpathSync, rmSync } from 'node:fs'
|
|
38
|
+
import { dirname, join, relative, resolve } from 'node:path'
|
|
39
|
+
import { fileURLToPath } from 'node:url'
|
|
40
|
+
|
|
41
|
+
/** 运行期产物:安装副本里出现即误打包。目录名在 README 中已公开。 */
|
|
42
|
+
const DIRS = ['.focus-guard', '.ai', 'self-improvement']
|
|
43
|
+
/** 单文件产物:与上面同性质。 */
|
|
44
|
+
const FILES = ['HANDOFF.md']
|
|
45
|
+
|
|
46
|
+
const DRY = process.argv.includes('--dry')
|
|
47
|
+
|
|
48
|
+
/** 从本文件上溯到包根(含 package.json 的那一层)。 */
|
|
49
|
+
function findRoot() {
|
|
50
|
+
let d = dirname(fileURLToPath(import.meta.url))
|
|
51
|
+
for (let i = 0; i < 6; i++) {
|
|
52
|
+
if (existsSync(join(d, 'package.json'))) return d
|
|
53
|
+
const up = resolve(d, '..')
|
|
54
|
+
if (up === d) break
|
|
55
|
+
d = up
|
|
56
|
+
}
|
|
57
|
+
return ''
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 三道保险:是本包、是安装副本、路径没逃出包根。
|
|
62
|
+
* 任何一条不满足就整件事不做——**删数据的代码,不确定就等于不做**。
|
|
63
|
+
*/
|
|
64
|
+
function guarded(root, target) {
|
|
65
|
+
if (!root || !existsSync(target)) return false
|
|
66
|
+
try {
|
|
67
|
+
const rel = relative(realpathSync(root), realpathSync(target))
|
|
68
|
+
return rel !== '' && !rel.startsWith('..')
|
|
69
|
+
} catch {
|
|
70
|
+
return false
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 往上找 `.git`——**不能只看包根**。
|
|
76
|
+
* monorepo 里包根是 `packages/core`,`.git` 在仓库根;
|
|
77
|
+
* 只看包根会把开发副本误判成安装副本,然后删掉作者自己的 `.ai/CASE_FILE.md`。
|
|
78
|
+
* 这条是实测踩出来的:`--dry` 报"会清理 1 项:.ai",而那一项是真实数据。
|
|
79
|
+
*/
|
|
80
|
+
function inWorkTree(root) {
|
|
81
|
+
let d = root
|
|
82
|
+
for (let i = 0; i < 8; i++) {
|
|
83
|
+
if (existsSync(join(d, '.git'))) return true
|
|
84
|
+
const up = resolve(d, '..')
|
|
85
|
+
if (up === d) break
|
|
86
|
+
d = up
|
|
87
|
+
}
|
|
88
|
+
return false
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function isOurPackage(root) {
|
|
92
|
+
try {
|
|
93
|
+
return JSON.parse(readFileSync(join(root, 'package.json'), 'utf8')).name === 'focus-guard'
|
|
94
|
+
} catch {
|
|
95
|
+
return false
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function main() {
|
|
100
|
+
const root = findRoot()
|
|
101
|
+
if (!root || !isOurPackage(root)) return
|
|
102
|
+
/* 开发副本:作者自己的仓库。这里的私有产物是数据,不是垃圾。 */
|
|
103
|
+
if (inWorkTree(root)) {
|
|
104
|
+
if (!DRY) return
|
|
105
|
+
process.stdout.write('[sweep] 开发副本(祖先目录有 .git),不动\n')
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
const hits = [...DIRS, ...FILES].map((n) => join(root, n)).filter((p) => guarded(root, p))
|
|
109
|
+
if (!hits.length) return
|
|
110
|
+
const names = hits.map((p) => relative(root, p).replace(/\\/g, '/'))
|
|
111
|
+
if (!DRY) for (const p of hits) rmSync(p, { recursive: true, force: true })
|
|
112
|
+
process.stdout.write(
|
|
113
|
+
`[sweep] ${DRY ? '会清理' : '已清理'} ${names.length} 项误打包的运行期产物:${names.join(' / ')}\n` +
|
|
114
|
+
'[sweep] 原因:安装副本里不该有开发机的数据。脑会从 0 开始长,法条随文档重新读入。\n',
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
try {
|
|
119
|
+
main()
|
|
120
|
+
} catch (e) {
|
|
121
|
+
/* 挂在 postinstall 上:清洁失败不该让用户装不上包。只报,不抛。 */
|
|
122
|
+
process.stdout.write(`[sweep] 跳过(${e.message})\n`)
|
|
123
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* focusguard CLI(2026-10-18,B 方案审批闸)
|
|
4
|
+
*
|
|
5
|
+
* node tools/focusguard.mjs pending 查看当前待批请求
|
|
6
|
+
* node tools/focusguard.mjs approve <id> 批准该请求(人类在终端敲)
|
|
7
|
+
* node tools/focusguard.mjs list 列出已批准未消费的请求
|
|
8
|
+
* node tools/focusguard.mjs status 审计/引擎状态摘要
|
|
9
|
+
*
|
|
10
|
+
* 退出码:0 成功 / 1 失败(id 不匹配等)/ 2 用法错误
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { dirname, join } from 'node:path'
|
|
14
|
+
import { fileURLToPath } from 'node:url'
|
|
15
|
+
|
|
16
|
+
const { readApprovalRequest, approveRequest, listApprovals, approvalTtlMs } = await import('../src/core/approvals.mjs')
|
|
17
|
+
|
|
18
|
+
const here = dirname(fileURLToPath(import.meta.url))
|
|
19
|
+
const args = process.argv.slice(2)
|
|
20
|
+
const cmd = args[0] || 'pending'
|
|
21
|
+
|
|
22
|
+
function fmtRequest(r) {
|
|
23
|
+
if (!r) return '(无待批请求)'
|
|
24
|
+
return [
|
|
25
|
+
`id : ${r.id}`,
|
|
26
|
+
`时间 : ${r.ts}`,
|
|
27
|
+
`会话 : ${r.session}`,
|
|
28
|
+
`工具 : ${r.tool}`,
|
|
29
|
+
`命令 : ${r.command}`,
|
|
30
|
+
`门槛 : ${r.gateId}`,
|
|
31
|
+
`状态 : ${r.state}`,
|
|
32
|
+
`理由 : ${r.reason}`,
|
|
33
|
+
].join('\n')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (cmd === 'pending') {
|
|
37
|
+
console.log(fmtRequest(readApprovalRequest()))
|
|
38
|
+
process.exit(0)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (cmd === 'approve') {
|
|
42
|
+
const id = args[1]
|
|
43
|
+
if (!id) {
|
|
44
|
+
console.error('用法:focusguard approve <id>(用 focusguard pending 查看 id)')
|
|
45
|
+
process.exit(2)
|
|
46
|
+
}
|
|
47
|
+
const r = approveRequest(id)
|
|
48
|
+
if (!r.ok) {
|
|
49
|
+
console.error(`批准失败:${r.reason}`)
|
|
50
|
+
process.exit(1)
|
|
51
|
+
}
|
|
52
|
+
console.log(`已批准 ${r.id}:${r.record.tool} — ${r.record.command}`)
|
|
53
|
+
// 批准不是永久通行证:只对**这个会话的这一次意图**有效(一次性 + 时效)。
|
|
54
|
+
const mins = Math.round(approvalTtlMs() / 60000)
|
|
55
|
+
console.log(`有效窗口 ${mins} 分钟,且仅限会话 ${r.record.session};消费一次即失效(FG_APPROVAL_TTL_MS 可调)。`)
|
|
56
|
+
process.exit(0)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (cmd === 'list') {
|
|
60
|
+
const ids = listApprovals()
|
|
61
|
+
console.log(ids.length ? ids.join('\n') : '(无已批准未消费的请求)')
|
|
62
|
+
process.exit(0)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (cmd === 'status') {
|
|
66
|
+
const { readFileSync } = await import('node:fs')
|
|
67
|
+
const audit = process.env.FG_AUDIT_FILE || join(here, '..', '..', '..', '.focus-guard', 'AUDIT.log')
|
|
68
|
+
let lines = 0
|
|
69
|
+
try {
|
|
70
|
+
lines = readFileSync(audit, 'utf8').split('\n').filter(Boolean).length
|
|
71
|
+
} catch {
|
|
72
|
+
lines = 0
|
|
73
|
+
}
|
|
74
|
+
const pending = readApprovalRequest()
|
|
75
|
+
console.log(`审计文件 : ${audit}`)
|
|
76
|
+
console.log(`审计行数 : ${lines}`)
|
|
77
|
+
console.log(`待批请求 : ${pending ? `${pending.id}(${pending.tool})` : '无'}`)
|
|
78
|
+
console.log(`已批准 : ${listApprovals().length}`)
|
|
79
|
+
process.exit(0)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (cmd === 'case') {
|
|
83
|
+
// 卷宗人工维护(2026-10-30):只读打分 + 人工删改,**不做自动淘汰**。
|
|
84
|
+
const { loadCase, entriesOf, scoreEntry, dropEntry, tagEntry, parseTag, stripTag, CASE_TAGS, auditCase } = await import('./lib/case-tool.mjs')
|
|
85
|
+
const { record, file, error } = loadCase()
|
|
86
|
+
if (error) {
|
|
87
|
+
console.error(`卷宗读不了:${error}`)
|
|
88
|
+
process.exit(1)
|
|
89
|
+
}
|
|
90
|
+
if (!record) {
|
|
91
|
+
console.log(`本目录没有卷宗(${file})。卷宗在首次回合结束时自动生成。`)
|
|
92
|
+
process.exit(0)
|
|
93
|
+
}
|
|
94
|
+
const rest = args.slice(1)
|
|
95
|
+
const val = (flag) => (rest.includes(flag) ? rest[rest.indexOf(flag) + 1] : null)
|
|
96
|
+
|
|
97
|
+
if (rest.includes('--drop')) {
|
|
98
|
+
const r = dropEntry(record, val('--drop'))
|
|
99
|
+
if (!r.ok) {
|
|
100
|
+
console.error(`删除失败:${r.reason}`)
|
|
101
|
+
process.exit(1)
|
|
102
|
+
}
|
|
103
|
+
console.log(`已删除:${r.removed}`)
|
|
104
|
+
console.log(`原卷宗已备份:${r.backup}`)
|
|
105
|
+
process.exit(0)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (rest.includes('--tag')) {
|
|
109
|
+
const loc = val('--tag')
|
|
110
|
+
const tag = rest[rest.indexOf('--tag') + 2] || null
|
|
111
|
+
const r = tagEntry(record, loc, tag)
|
|
112
|
+
if (!r.ok) {
|
|
113
|
+
console.error(`打标签失败:${r.reason}(可用标签:${CASE_TAGS.join(' / ')},不传则清除)`)
|
|
114
|
+
process.exit(1)
|
|
115
|
+
}
|
|
116
|
+
console.log(`已改:${r.before}\n → ${r.after || '(无标签)'}`)
|
|
117
|
+
process.exit(0)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const withScore = rest.includes('--score')
|
|
121
|
+
const all = entriesOf(record)
|
|
122
|
+
if (!all.length) {
|
|
123
|
+
console.log('卷宗是空的。')
|
|
124
|
+
process.exit(0)
|
|
125
|
+
}
|
|
126
|
+
console.log(`卷宗:${file}`)
|
|
127
|
+
console.log(`工作区:${record.workspace_path || '-'} · 更新 ${record.updated_at || '-'}\n`)
|
|
128
|
+
let lastField = null
|
|
129
|
+
for (let i = 0; i < all.length; i += 1) {
|
|
130
|
+
const e = all[i]
|
|
131
|
+
if (e.field !== lastField) {
|
|
132
|
+
console.log(`【${e.label}】`)
|
|
133
|
+
lastField = e.field
|
|
134
|
+
}
|
|
135
|
+
const tag = parseTag(e.text)
|
|
136
|
+
const body = stripTag(e.text)
|
|
137
|
+
const hidden = tag === 'done' || tag === 'note'
|
|
138
|
+
const mark = tag ? `[${tag}]`.padEnd(8) : ' '
|
|
139
|
+
const line = `${String(i + 1).padStart(3)} ${mark}${body.length > 46 ? `${body.slice(0, 46)}…` : body}`
|
|
140
|
+
if (withScore) {
|
|
141
|
+
const { score, reasons } = scoreEntry(body)
|
|
142
|
+
console.log(`${line}${hidden ? ' (不注入)' : ''}`)
|
|
143
|
+
console.log(` ${String(score).padStart(3)} 分 · ${reasons.join(' / ') || '无信号'}`)
|
|
144
|
+
} else {
|
|
145
|
+
console.log(`${line}${hidden ? ' (不注入)' : ''}`)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
console.log(`\n共 ${all.length} 条。--score 看打分(仅参考,不作判决);--drop <序号> 删除;--tag <序号> <goal|done|note> 打标签。`)
|
|
149
|
+
// 只读浏览也算"这个功能有没有人用"的证据:留一条 case-list,内容只记条数与是否打分。
|
|
150
|
+
auditCase('case-list', `entries=${all.length} score=${withScore}`)
|
|
151
|
+
process.exit(0)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
console.error(`未知子命令:${cmd}(可用:pending / approve <id> / list / status / case)`)
|
|
155
|
+
process.exit(2)
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 死闸审计(2026-10-22):把"判定条件里引用的参数键 / 工具名"与**真实会话里的工具 schema** 对账。
|
|
4
|
+
*
|
|
5
|
+
* 病根(两次实测):
|
|
6
|
+
* ① `grep` 体积闸要求 `args.output_mode === 'content' && !args.head_limit`,
|
|
7
|
+
* 而真实 DSH grep 只有 `{pattern,path,include}` → 条件永不成立;
|
|
8
|
+
* ② 整文件闸/取证闸拿相对路径直接 statSync(按宿主 cwd 解析)→ ENOENT → 静默放行。
|
|
9
|
+
* 共同特征:**判定写在合成参数上**,测试用合成数据全绿,线上百发百中漏。
|
|
10
|
+
*
|
|
11
|
+
* 本工具只读会话记录,不做判定;输出两类清单:
|
|
12
|
+
* A. 源码引用但真实 schema 里不存在的参数键(可能是死闸)
|
|
13
|
+
* B. 真实目录里既不算只读、也不算改动、也不算 shell 的工具(分类盲区)
|
|
14
|
+
*
|
|
15
|
+
* 用法:node packages/core/tools/gate-audit.mjs [--limit 20] [--json]
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { decodeSession as decode } from './lib/sessionDump.mjs'
|
|
19
|
+
import { readdirSync, readFileSync, statSync } from 'node:fs'
|
|
20
|
+
import { join, dirname } from 'node:path'
|
|
21
|
+
import { fileURLToPath } from 'node:url'
|
|
22
|
+
|
|
23
|
+
const HERE = dirname(fileURLToPath(import.meta.url))
|
|
24
|
+
const CORE = join(HERE, '..')
|
|
25
|
+
const SESSIONS = process.env.FG_SESSIONS_DIR || 'E:/DSH/.dsh/sessions'
|
|
26
|
+
|
|
27
|
+
const argv = process.argv.slice(2)
|
|
28
|
+
const LIMIT = Number(argv.includes('--limit') ? argv[argv.indexOf('--limit') + 1] : 20) || 20
|
|
29
|
+
const AS_JSON = argv.includes('--json')
|
|
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
|
+
/** 多帧 zstd:按魔数 0x28B52FFD 分帧解压(单帧 API 只解第一帧)。 */
|
|
53
|
+
|
|
54
|
+
/** 从会话里取真实工具表:{ 工具名: Set(参数键) }。 */
|
|
55
|
+
function realToolSchema(limit) {
|
|
56
|
+
const files = walk(SESSIONS).sort((a, b) => b.mtime - a.mtime).slice(0, limit)
|
|
57
|
+
const schema = new Map()
|
|
58
|
+
let usedSessions = 0
|
|
59
|
+
for (const f of files) {
|
|
60
|
+
const lines = decode(f.file)
|
|
61
|
+
const header = lines.find((l) => l.type === 'request/header') || null
|
|
62
|
+
// 真实结构:{data:{header:{config,adapterDefaults,tools:[{name,description,parameters}]}, reason}}
|
|
63
|
+
const h = header && header.data ? header.data.header || header.data : null
|
|
64
|
+
const tools = (h && (h.tools || h.toolList)) || (header && header.data && header.data.tools) || null
|
|
65
|
+
if (!Array.isArray(tools)) continue
|
|
66
|
+
usedSessions += 1
|
|
67
|
+
for (const t of tools) {
|
|
68
|
+
const name = String(t && t.name)
|
|
69
|
+
if (!name) continue
|
|
70
|
+
const keys = schema.get(name) || new Set()
|
|
71
|
+
const props = (t.parameters && t.parameters.properties) || {}
|
|
72
|
+
for (const k of Object.keys(props)) keys.add(k)
|
|
73
|
+
schema.set(name, keys)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return { schema, usedSessions }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** 源码里引用过的参数键:args.xxx / arguments.xxx / ti.xxx(只取 dsh 侧判定文件),带行号。 */
|
|
80
|
+
function referencedKeys() {
|
|
81
|
+
const files = ['src/dsh/pipeline.mjs', 'src/dsh/preGuard.mjs', 'src/adapters/dsh/eligibility-gate.mjs']
|
|
82
|
+
const keys = new Map()
|
|
83
|
+
for (const rel of files) {
|
|
84
|
+
let src = ''
|
|
85
|
+
try {
|
|
86
|
+
src = readFileSync(join(CORE, rel), 'utf8')
|
|
87
|
+
} catch {
|
|
88
|
+
continue
|
|
89
|
+
}
|
|
90
|
+
const lines = src.split('\n')
|
|
91
|
+
lines.forEach((line, idx) => {
|
|
92
|
+
for (const m of line.matchAll(/\b(?:args|arguments|ti)\.([A-Za-z_$][\w$]*)/g)) {
|
|
93
|
+
const k = m[1]
|
|
94
|
+
if (!keys.has(k)) keys.set(k, [])
|
|
95
|
+
keys.get(k).push(`${rel}:${idx + 1}`)
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
return keys
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const READ = /^(?:Read|Grep|Glob|read_image|NotebookRead|read|grep|glob|read_image|web_search|web_fetch|list_\w+|get_\w+|search|query_\w+)$/i
|
|
103
|
+
const MUT = /^(?:write|edit|multiedit|patch|applypatch|notebookedit|plugin_manager|move_run|import_claude|claude_export)$/i
|
|
104
|
+
const MUT_MCP =
|
|
105
|
+
/^mcp__[\s\S]+?__(?:write|create|update|delete|remove|move|copy|patch|apply|edit|save|install|uninstall|build|package|scaffold|deploy|upload|publish)_/i
|
|
106
|
+
const SHELL = /^(?:bash|shell|sh|pwsh|powershell|cmd|terminal|runcommand)$/i
|
|
107
|
+
|
|
108
|
+
const { schema, usedSessions } = realToolSchema(LIMIT)
|
|
109
|
+
const union = new Set()
|
|
110
|
+
for (const keys of schema.values()) for (const k of keys) union.add(k)
|
|
111
|
+
const refs = referencedKeys()
|
|
112
|
+
|
|
113
|
+
const ghost = []
|
|
114
|
+
for (const [k, where] of refs) {
|
|
115
|
+
if (!union.has(k)) ghost.push({ key: k, sites: where.slice(0, 6), count: where.length })
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const blind = []
|
|
119
|
+
for (const name of schema.keys()) {
|
|
120
|
+
if (READ.test(name) || MUT.test(name) || MUT_MCP.test(name) || SHELL.test(name)) continue
|
|
121
|
+
blind.push(name)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const report = {
|
|
125
|
+
sessions: usedSessions,
|
|
126
|
+
tools: schema.size,
|
|
127
|
+
ghostKeys: ghost.sort((a, b) => a.key.localeCompare(b.key)),
|
|
128
|
+
unclassifiedTools: blind.sort(),
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (AS_JSON) {
|
|
132
|
+
console.log(JSON.stringify(report, null, 2))
|
|
133
|
+
} else {
|
|
134
|
+
console.log(`扫描会话 ${report.sessions} 个,工具 ${report.tools} 个`)
|
|
135
|
+
console.log(`\nA. 源码引用但真实 schema 无此键(${ghost.length})——可能是死闸:`)
|
|
136
|
+
for (const g of report.ghostKeys) console.log(` ${g.key} ← ${g.sites.join(', ')}${g.count > g.sites.length ? ` …(+${g.count - g.sites.length})` : ''}`)
|
|
137
|
+
console.log(`\nB. 既不算只读/改动/shell 的工具(${blind.length})——分类盲区:`)
|
|
138
|
+
for (const t of report.unclassifiedTools) console.log(` ${t}`)
|
|
139
|
+
}
|
|
140
|
+
process.exit(0)
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 闸门输入量对账(2026-10-31)
|
|
4
|
+
*
|
|
5
|
+
* ## 为什么需要
|
|
6
|
+
*
|
|
7
|
+
* 判据再对,**看的样本少了就等于没看**。
|
|
8
|
+
*
|
|
9
|
+
* 实测(2026-10-31):`npm run check` 报「文档口径 42 个 md」,直接跑 docs:lint 报 36。
|
|
10
|
+
* 同一根目录、同一个 `listMarkdownFiles`,差 6 个文件;两次的问题条数都是 5 条,闸门一路绿。
|
|
11
|
+
* **没有任何地方会发现"它少看了 6 个文件"**——因为闸门只报自己看了多少个,不报这个数变没变。
|
|
12
|
+
*
|
|
13
|
+
* ## 范式不是新发明的
|
|
14
|
+
*
|
|
15
|
+
* `schema:verify` 早就在做这件事:基线记 254 个工具,这次只有 86,于是报
|
|
16
|
+
* 「疑似插件未加载,不是 schema 漂移」。它做对的地方不是"独立核算 86 对不对",
|
|
17
|
+
* 而是**跨次比较自报的量**。本工具把同一范式推广到其余三条闸门。
|
|
18
|
+
*
|
|
19
|
+
* 为什么不去独立数一遍文件:口径差异(跳过空文件、扩展名过滤)会让两边永远对不上,
|
|
20
|
+
* 制造噪音。**要防的是"闸门看的样本在无声变化",这个信息只有闸门自己知道。**
|
|
21
|
+
*
|
|
22
|
+
* ## 判定(按原因分档,不按结果对错分档)
|
|
23
|
+
*
|
|
24
|
+
* lost 样本变少 —— 缺陷,退 1。文件被删了没更新基线,或者口径被静默缩小
|
|
25
|
+
* grown 样本变多 —— 正常,只提示。新写了文件,`--update` 更新基线即可
|
|
26
|
+
*
|
|
27
|
+
* **不把"多了"也报红**:会让人开始忽略红。这正是 2026-10-29 那次的教训——
|
|
28
|
+
* 197/200 全红 = 红得没信息量。
|
|
29
|
+
*
|
|
30
|
+
* ## 为什么不进 check 链
|
|
31
|
+
*
|
|
32
|
+
* 新闸门本身也要被检查,进链就是第二条 `schema:verify`(空转 8 天那条)。
|
|
33
|
+
* 先当独立命令跑一段时间,看得见真实用途再谈入链。
|
|
34
|
+
*/
|
|
35
|
+
import { execFileSync } from 'node:child_process'
|
|
36
|
+
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
|
|
37
|
+
import { join } from 'node:path'
|
|
38
|
+
import { fileURLToPath } from 'node:url'
|
|
39
|
+
|
|
40
|
+
const REPO = fileURLToPath(new URL('../../../', import.meta.url))
|
|
41
|
+
const BASE = join(REPO, 'packages/core/docs/GATE-INPUTS.json')
|
|
42
|
+
|
|
43
|
+
/** 三条闸门:各自都支持 `--json`,且都报了 `files`(本次实际看了多少个样本)。 */
|
|
44
|
+
const GATES = [
|
|
45
|
+
{ key: 'smells', file: 'packages/core/tools/smells.mjs', label: '结构异味' },
|
|
46
|
+
{ key: 'lint', file: 'packages/core/tools/lint.mjs', label: '房规' },
|
|
47
|
+
{ key: 'docs', file: 'packages/core/tools/docs-lint.mjs', label: '文档口径' },
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
/** 取一次样本数。闸门自己跑挂了就返回 null——那是闸门的问题,这里只如实说取不到。 */
|
|
51
|
+
function sample(gate) {
|
|
52
|
+
try {
|
|
53
|
+
const out = execFileSync(process.execPath, [join(REPO, gate.file), '--json'], {
|
|
54
|
+
cwd: REPO,
|
|
55
|
+
encoding: 'utf8',
|
|
56
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
57
|
+
})
|
|
58
|
+
const n = JSON.parse(out).files
|
|
59
|
+
return Number.isFinite(n) ? n : null
|
|
60
|
+
} catch {
|
|
61
|
+
return null
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const now = {}
|
|
66
|
+
const lost = []
|
|
67
|
+
const grown = []
|
|
68
|
+
for (const g of GATES) {
|
|
69
|
+
const n = sample(g)
|
|
70
|
+
if (n === null) {
|
|
71
|
+
console.error(`[gate-inputs] ${g.label}:取不到样本数(闸门自己跑挂了)`)
|
|
72
|
+
continue
|
|
73
|
+
}
|
|
74
|
+
now[g.key] = n
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (process.argv.includes('--update')) {
|
|
78
|
+
const doc = {
|
|
79
|
+
_说明: '闸门输入量基线。每条闸门"上次看到多少个样本"。样本变少 = 闸门看的文件少了,绿是假的;变多 = 正常。',
|
|
80
|
+
_维护: '写文件后跑 `npm run gate:inputs -- --update`;发现变少先查清是删了还是口径改了。',
|
|
81
|
+
updated: new Date().toISOString().slice(0, 10),
|
|
82
|
+
inputs: now,
|
|
83
|
+
}
|
|
84
|
+
writeFileSync(BASE, `${JSON.stringify(doc, null, 2)}\n`)
|
|
85
|
+
console.log(`[gate-inputs] 基线已更新:${JSON.stringify(now)}`)
|
|
86
|
+
process.exit(0)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const base = existsSync(BASE) ? JSON.parse(readFileSync(BASE, 'utf8')) : { inputs: {} }
|
|
90
|
+
for (const g of GATES) {
|
|
91
|
+
const before = base.inputs?.[g.key]
|
|
92
|
+
const after = now[g.key]
|
|
93
|
+
if (typeof before !== 'number' || typeof after !== 'number') continue
|
|
94
|
+
if (after < before) lost.push(`${g.label}:${before} → ${after}(少 ${before - after})`)
|
|
95
|
+
else if (after > before) grown.push(`${g.label}:${before} → ${after}(多 ${after - before})`)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const head = GATES.filter((g) => typeof now[g.key] === 'number')
|
|
99
|
+
.map((g) => `${g.label} ${now[g.key]}`)
|
|
100
|
+
.join(' / ')
|
|
101
|
+
console.log(`[gate-inputs] ${head}`)
|
|
102
|
+
|
|
103
|
+
if (lost.length) {
|
|
104
|
+
console.error(`[gate-inputs] ❌ 样本变少 ${lost.length} 项——闸门看的文件少了,绿是假的:`)
|
|
105
|
+
for (const l of lost) console.error(` ${l}`)
|
|
106
|
+
console.error(' 先查清:是真删了(那就 --update),还是扫描口径被改了(那是缺陷)。')
|
|
107
|
+
process.exit(1)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (grown.length) {
|
|
111
|
+
console.log(`[gate-inputs] 样本变多 ${grown.length} 项(正常;确认后 --update 更新基线)`)
|
|
112
|
+
for (const g of grown) console.log(` ${g}`)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
console.log('[gate-inputs] PASS:样本量未缩水')
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 跨会话快速交接(主图 L4,2026-10-22)
|
|
4
|
+
*
|
|
5
|
+
* 打印当前工作区的交接简报:上次会话快照 + 决策 + 约束 + 下一步。
|
|
6
|
+
* 只读、不推断:所有数字都来自 FG 自己数出来的状态与卷宗,不猜"做到哪了"。
|
|
7
|
+
*
|
|
8
|
+
* 用法:
|
|
9
|
+
* node packages/core/tools/handoff.mjs # 当前目录
|
|
10
|
+
* node packages/core/tools/handoff.mjs --cwd <path> # 指定工作区
|
|
11
|
+
* node packages/core/tools/handoff.mjs --json # 机器读
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { readWorkspace, renderWorkspaceSection, workspaceHash } from '../src/dsh/workspace.mjs'
|
|
15
|
+
|
|
16
|
+
const argv = process.argv.slice(2)
|
|
17
|
+
const cwdArg = argv.includes('--cwd') ? argv[argv.indexOf('--cwd') + 1] : process.cwd()
|
|
18
|
+
const asJson = argv.includes('--json')
|
|
19
|
+
|
|
20
|
+
const record = readWorkspace(cwdArg)
|
|
21
|
+
if (!record) {
|
|
22
|
+
console.log(`无卷宗:${cwdArg}(hash=${workspaceHash(cwdArg)})`)
|
|
23
|
+
process.exit(0)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (asJson) {
|
|
27
|
+
console.log(JSON.stringify(record, null, 2))
|
|
28
|
+
process.exit(0)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const s = record.snapshot
|
|
32
|
+
console.log(`# 交接简报 · ${record.workspace_path || cwdArg}`)
|
|
33
|
+
console.log(`hash=${record.workspace_hash} · 更新 ${record.updated_at || '-'}`)
|
|
34
|
+
if (s) {
|
|
35
|
+
console.log(
|
|
36
|
+
`\n上次会话:${s.sid || '-'}(${s.at || '-'})\n` +
|
|
37
|
+
`- 工具调用 ${s.toolCalls ?? '-'} · 被拦 ${s.denies ?? '-'}\n` +
|
|
38
|
+
`- KPI ${s.kpi ?? '-'} · 委托池剩余 ${s.delegateBudget ?? '-'}`,
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
const block = (label, items) => {
|
|
42
|
+
if (!items || !items.length) return
|
|
43
|
+
console.log(`\n${label}:`)
|
|
44
|
+
// 简报要"快速":只取最近 5 条,每条压到 90 字(原文仍在 workspace.json,不丢数据)。
|
|
45
|
+
for (const x of items.slice(-5)) {
|
|
46
|
+
const one = String(x).replace(/\s+/g, ' ').trim()
|
|
47
|
+
console.log(`- ${one.length > 90 ? one.slice(0, 90) + '…' : one}`)
|
|
48
|
+
}
|
|
49
|
+
if (items.length > 5) console.log(`- …(共 ${items.length} 条,其余见 workspace.json)`)
|
|
50
|
+
}
|
|
51
|
+
block('决策(人类指令原文)', record.decisions)
|
|
52
|
+
block('约束(被人否决过的命令)', record.constraints)
|
|
53
|
+
block('下一步', record.next_action)
|
|
54
|
+
|
|
55
|
+
const section = renderWorkspaceSection(record)
|
|
56
|
+
if (section) console.log(`\n---\n(新会话自动注入的 section,${section.length} 字)\n${section}`)
|