focus-guard 3.4.0 → 3.5.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/README.md CHANGED
@@ -1,15 +1,19 @@
1
- # FocusGuard
1
+ # Keepward
2
2
 
3
- > **状态**:参考 · 适用版本 3.3.0 · 最后核对 2026-10-28
3
+ > **状态**:参考 · 适用版本 3.5.0 · 最后核对 2026-11-17
4
+
5
+ > Formerly **FocusGuard**. Renamed in 3.5.0 — the package name `focus-guard`, the plugin id,
6
+ > the `.focus-guard/` state directory and the `FG_*` environment variables are **unchanged**.
4
7
 
5
8
  [Chinese](README.zh-CN.md) · English
6
9
 
7
- Guardrails for AI coding agents, enforced at the hook layer. Four behaviours that make agents dangerous stop being prompt advice and become mechanically blocked events: **editing without reading first**, **conclusions without `[file:line]` evidence**, **whole-file reads that burn context**, and **asserting findings that were never verified**. A hit escalates through L1 push-back → L2 forced evidence → L3 fuse → L4 record → L5 downgrade → L6 report.
10
+ Guardrails for AI coding agents, enforced at the hook layer. Three kinds of runaway stop being prompt advice and become mechanically blocked events:
11
+
12
+ - **Thinking** — a conclusion with no `[file:line]` evidence is pushed back before it ships
13
+ - **Resources** — whole-file reads and repeated no-op reads burn context; both are gated
14
+ - **Permissions** — `rm -rf` / `git push -f` / `npm publish` need a grant *before* they run, not an apology after
8
15
 
9
- - **Zero dependencies** — the core package ships no runtime libraries (`dependencies` is empty)
10
- - **Two enforcement surfaces** — a native DSH plugin (host hooks) plus the ZCode hook shell
11
- - **Full audit trail** — every block is appended to `.focus-guard/AUDIT.log` (JSONL, rotated and archived)
12
- - **Auditable rules** — `docs/RULES.md` marks which clauses are machine-enforced and which are not yet implemented
16
+ Every block is appended to `.focus-guard/AUDIT.log` (JSONL) with the rule that actually fired. Nothing here asks the model to behave; it changes what the model is allowed to do.
13
17
 
14
18
  ## Install
15
19
 
@@ -21,42 +25,35 @@ dsh plugin --profile <your-profile> add github:irisblackwood/focus-guard#path:/p
21
25
 
22
26
  Restart the session to activate. The bundle patch is declared by this package's `package.json` (`dsh.bundle.patch` → `src/dsh/cordis.patch.yml`).
23
27
 
24
- > **On npm.** `npm install focus-guard` installs **3.3.3** — published 2026-11-07 through GitHub OIDC trusted publishing with a signed provenance statement. The GitHub install above tracks the repo directly.
28
+ > **Mutually exclusive** with the official bridge (`@deepseek-ai/dsh-hooks-claude-code`). Both mount the same seams; mounting both double-runs every hook — pick one.
29
+
30
+ ### npm
25
31
 
26
- 注意 Mutually exclusive with the official bridge (`@deepseek-ai/dsh-hooks-claude-code`). Mounting both double-runs the same hooks — pick one.
32
+ ```bash
33
+ npm install focus-guard
34
+ ```
27
35
 
28
36
  ### ZCode
29
37
 
30
- `.zcode-plugin/plugin.json` is ready; import this repository through ZCode's plugin flow.
38
+ `.zcode-plugin/plugin.json` is ready; import this repository through ZCode's plugin flow. The execution shell is sealed — it carries its own version and does not follow the main release.
31
39
 
32
40
  ## What it blocks
33
41
 
34
42
  | Situation | Mechanism |
35
43
  |---|---|
36
- | Editing files or running mutating commands without investigating first | Layer 4 precondition: the target must have been read in this session, otherwise one refusal (new files exempt) |
37
- | High-risk commands (recursive delete, format, force-push, publish, global install, system-path writes) | Layer 3 eligibility: **apply before execute**, denied without a grant; `fg_apply` is the request channel |
38
- | Absolute red lines (`rm -rf /`, `DROP DATABASE`, `git push -f`) | Layer 1 zero-latency short-circuit denial, **no approval prompt** |
39
- | Dangerous command strings that are merely **quoted** (test data, docs, eval fixtures) | Context exemption on three criteria (quoted / data marker / read-only command) → downgraded to semantic judgement instead of a hard denial |
44
+ | Editing files or running mutating commands without investigating first | L4 precondition: the target must have been read in this session, otherwise one refusal (new files exempt) |
45
+ | High-risk commands (recursive delete, format, force-push, publish, global install, system-path writes) | L3 eligibility: **apply before execute**, denied without a grant; `fg_apply` is the request channel |
46
+ | Absolute red lines (`rm -rf /`, `DROP DATABASE`, `git push -f`) | L2 zero-latency short-circuit denial, **no approval prompt** |
47
+ | Dangerous strings that are merely **quoted** (test data, docs, eval fixtures) | Context exemption on three criteria (quoted / data marker / read-only command) → downgraded to semantic judgement, never waved through |
40
48
  | Wrongly blocked, no appeal path | `fg_appeal`: submit counter-evidence anchors, one human ruling, fully recorded |
49
+ | Whole-file read of a large target | Above `FG_WHOLE_FILE_MAX_LINES` (default 300) without `offset`/`limit` → denied, pointed at XERJ |
50
+ | Vague development request, straight to editing | One refusal before the first write, requiring `brainstorming` or `writing-plans` |
41
51
  | One-size-fits-all across models | Model profiles (`src/profiles/`): review intensity follows the model |
42
52
  | Writing `grep`/`find` when the machine wants `rg`/`fd` | Environment fingerprint + hard command check, one-line substitution hint |
43
53
 
44
- ## Runtime additions (in-tree, 2026-10-18)
54
+ ## How it decides
45
55
 
46
- Shipped in the code, not yet reflected in the version number (still `3.0.6`):
47
-
48
- - **Heartbeat and liveness** — `src/dsh/heartbeat.mjs:85`, `src/dsh/status.mjs:73`: every 10s the plugin atomically rewrites `.focus-guard/status.json`; `node src/dsh/status.mjs` reports `ALIVE` / `STALE` and exits `0` all-green, `2` module missing, `3` stale, `4` no file.
49
- - **Module self-check** — `heartbeat.mjs:45`: the ten enforcement seams plus the heartbeat each report at startup; anything missing lands in `degraded`, is warned to the host log, and is injected to the model as `focus-guard-degraded`.
50
- - **L4 cross-session record** — `src/dsh/workspace.mjs:38` (hash) / `:125` (turn record) / `src/dsh/index.js:71` (injection): state is keyed by `sha256(realpath(cwd))`; a hash mismatch injects nothing, so a renamed folder cannot inherit old state.
51
- - **Whole-file read gate** — `src/dsh/guard/wholeFile.mjs:26` / `:70`: a target above 300 lines read without `offset`/`limit` is denied and pointed at XERJ (`xerj search` / `xerj def`). Configurable via `FG_WHOLE_FILE_MAX_LINES`.
52
- - **Superpowers writing gate** — `src/dsh/guard/superpowers.mjs:34`: a vague development request (dev verb + vague marker + no acceptance criteria) gets one denial before the first edit/write/destructive shell, requiring `brainstorming` or `writing-plans`; a retry or a loaded skill passes.
53
- - **Graded destructive heuristic** — `src/dsh/guard/risk.mjs:37`: `Remove-Item` / `fs.rmSync` weight by target — system path or recursive `0.9` (blocked), TEMP `0.2`, other `0.4`; redirection `0.15`.
54
- - **Compaction watch** — `src/dsh/compactionWatch.mjs:239` / `:364`: observes `compaction/start|end|prune` and request-error collisions, records them, and never writes surface or cancels a turn.
55
- - **Denial reasons in the audit** — `src/dsh/audit.mjs:120`: `auditDeny(exec, cmd, {trigger, level})` records the real rule (`redline:<name>`, `env-substitute`, `gate:fg_apply`, `evidence-gate`, `whole-file:lines`, `risk:<category>`) instead of a constant `high-risk-rm`.
56
- - **Executor detection** — `src/core/env.mjs:18` + `src/dsh/index.js:31`: the master layer honours an explicit `FG_SHELL`; the DSH adapter declares `powershell`, ending the three-way mismatch between record, fingerprint and actual executor.
57
- - **Audit isolation guard** — `tests/audit-isolation.mjs:1`: snapshots the real `AUDIT.log` before the suites run and fails the run if it grew (verified both ways: clean Δ0 PASS, deliberate pollution Δ1 FAIL).
58
-
59
- ## Six-layer eligibility
56
+ Six layers, in order. A profile may skip layers; it may never disable L2 or L3.
60
57
 
61
58
  | Layer | Question |
62
59
  |---|---|
@@ -68,34 +65,57 @@ Shipped in the code, not yet reflected in the version number (still `3.0.6`):
68
65
  | L5 | Semantic signals — the model probe's `mismatch` / high-risk score |
69
66
  | L6 | Grant and record |
70
67
 
71
- ## Three commands
68
+ ## What else is running
72
69
 
73
- ```bash
74
- npm test # full suite (all test files; acceptance is only one of them)
75
- npm run test:eligibility # eligibility + red-line exemption + appeal
76
- npm run eval # threat evaluation (high-risk detection rate + benign false-positive rate)
77
- ```
70
+ All shipped. Module paths carry no line numbers on purpose — those rot silently.
71
+
72
+ - **Heartbeat and liveness** — `src/dsh/heartbeat.mjs`, `src/dsh/status.mjs`: every 10s the plugin atomically rewrites `.focus-guard/status.json`; `node src/dsh/status.mjs` reports `ALIVE` / `STALE` and exits `0` all-green, `2` module missing, `3` stale, `4` no file.
73
+ - **Module self-check** — `src/dsh/heartbeat.mjs`: the ten enforcement seams plus the heartbeat each report at startup; anything missing lands in `degraded`, is warned to the host log, and is injected to the model as `focus-guard-degraded`.
74
+ - **Cross-session record** — `src/memory/workspace.mjs`: state is keyed by `sha256(realpath(cwd))`; a hash mismatch injects nothing, so a renamed folder cannot inherit old state.
75
+ - **Compaction watch** — `src/dsh/compactionWatch.mjs`: observes `compaction/start|end|prune` and request-error collisions, and measures how many anchors (paths, line refs, error codes, constants) a summary kept versus the original. Read-only: never writes a surface, never cancels a turn.
76
+ - **Prefix stability** — `src/core/prefixStability.mjs`: fingerprints the assembled prompt prefix each turn and records shifts, so a cache-breaking change shows up as an audit event instead of on an invoice.
77
+ - **Graded destructive heuristic** — `src/dsh/guard/risk.mjs`: `Remove-Item` / `fs.rmSync` weight by target — system path or recursive `0.9` (blocked), TEMP `0.2`, other `0.4`; redirection `0.15`.
78
+ - **Denial reasons in the audit** — `src/dsh/audit.mjs`: `auditDeny(exec, cmd, {trigger, level})` records the real rule (`redline:<name>`, `env-substitute`, `gate:fg_apply`, `evidence-gate`, `whole-file:lines`, `risk:<category>`) instead of a constant `high-risk-rm`.
79
+ - **Executor detection** — `src/core/env.mjs`, `src/dsh/index.js`: the master layer honours an explicit `FG_SHELL`; the DSH adapter declares `powershell`, ending the three-way mismatch between record, fingerprint and actual executor.
80
+ - **Audit isolation guard** — `tests/audit-isolation.mjs`: snapshots the real `AUDIT.log` before the suites run and fails the run if it grew.
78
81
 
79
82
  ## Architecture
80
83
 
81
84
  ```
82
- src/core/ master layer: zero-dependency, host-agnostic judging logic
83
- src/adapters/dsh/ adapter layer: wires the master into DSH (fg_apply / fg_appeal + eligibility gate)
84
- src/dsh/pipeline.mjs host hooks: pre-execute / system-prompt / post-execute (orchestration)
85
- src/dsh/guard/ per-layer gate implementations (10 modules, split 2026-10-27)
86
- hooks/guard.mjs ZCode execution shell (sealed: independent version, does not follow the main release)
85
+ src/core/ master layer: zero-dependency, host-agnostic judging logic (26 modules)
86
+ src/adapters/dsh/ adapter layer: wires the master into DSH (fg_apply / fg_appeal + eligibility gate)
87
+ src/dsh/ host seams: pipeline.mjs orchestrates 8 events; guard/ holds the per-layer gates
88
+ src/memory/ cross-session record, tags, workspace hashing
89
+ src/profiles/ five pure-parameter model profiles
90
+ hooks/guard.mjs ZCode execution shell (sealed: independent version)
87
91
  ```
88
92
 
89
- **Layering rule**: the master layer must not depend back on the adapter layer (locked by an acceptance test).
93
+ **Layering rule**: the master layer must not depend back on the adapter layer (locked by an acceptance test). Dependency direction is `core → adapters → seams`, one way only.
94
+
95
+ ## Commands
96
+
97
+ ```bash
98
+ npm test # full suite, run inside an audit sandbox
99
+ npm run test:eligibility # eligibility + red-line exemption + appeal
100
+ npm run eval # threat evaluation (detection rate + benign false-positive rate)
101
+ npm run check # everything the release gate runs
102
+ ```
90
103
 
91
104
  ## Version
92
105
 
93
- Currently `3.4.0`. Version surfaces are asserted mutually consistent, and the front-page version must equal the published maximum.
106
+ Three numbers, three meanings — do not mix them:
107
+
108
+ | Surface | Value | Meaning |
109
+ |---|---|---|
110
+ | npm registry | `3.3.3` | last `npm publish` (2026-11-07, OIDC trusted publishing + provenance) |
111
+ | GitHub `main` | `3.4.1` | `PUBLISHED_MAX` — what "released" means for this project |
112
+ | This tree | `3.5.0` | in flight; ahead of released is not the same as released |
94
113
 
95
114
  ## Docs
96
115
 
97
- - Capabilities and troubleshooting: repository `README.md`, `INSTALL.md`
98
- - Rule text: `docs/RULES.md`
116
+ - Capabilities, code anchors, gaps: `docs/ARCH-MAP.md`
117
+ - Rule text, and which clauses are machine-enforced: `docs/RULES.md`
118
+ - Mechanism walkthrough: repository `docs/ARCHITECTURE.md`
99
119
  - Changelog: repository `CHANGELOG.md`
100
120
 
101
121
  ## License
package/README.zh-CN.md CHANGED
@@ -1,13 +1,19 @@
1
- # FocusGuard 聚焦护栏
1
+ # 司会 Keepward
2
2
 
3
- > **状态**:参考 · 适用版本 3.3.0 · 最后核对 2026-10-28
3
+ > **状态**:参考 · 适用版本 3.5.0 · 最后核对 2026-11-17
4
4
 
5
- 给 AI 编码智能体装上履职纪律:**未取证就改、结论无锚点、整读大文件烧上下文、查无实据硬凑**这四类失控,被变成可机械拦截的行为事件——命中即按 L1 打回 → L2 强制取证 → L3 熔断 → L4 记档 → L5 降权 → L6 上报逐级处置。
5
+ > 旧名 **FocusGuard 聚焦护栏**(3.5.0 起更名)。包名 `focus-guard`、插件 id、
6
+ > 状态目录 `.focus-guard/`、环境变量 `FG_*` 一律不变——更名只动展示层,不动技术契约。
6
7
 
7
- - **零依赖**:核心包不依赖任何运行时库(`dependencies` 为空)
8
- - **DSH 优先执行面**:原生插件(宿主缝)为首选;ZCode 钩子并存,执行壳已封存
9
- - **全程留痕**:拦截写入 `.focus-guard/AUDIT.log`(JSONL,带体积轮转与归档)
10
- - **法条可审计**:条文正本 `docs/RULES.md` 明确标注哪些条款已机械化、哪些尚未落地
8
+ 中文 · [English](README.md)
9
+
10
+ 给 AI 编码智能体装上履职纪律。三类失控不再是提示词里的劝告,而是被机械拦截的行为事件:
11
+
12
+ - **思考失控**——结论没有 `[文件:行号]` 锚点,在交付前被打回
13
+ - **资源失控**——整读大文件、反复读同一个没变的文件,两道闸都拦
14
+ - **权限失控**——`rm -rf` / `git push -f` / `npm publish` 要在**执行前**拿到授权,而不是事后道歉
15
+
16
+ 每次拦截都写进 `.focus-guard/AUDIT.log`(JSONL),并带上**真实生效的规则名**。这里不做"劝模型自觉"的事,改的是模型**被允许做什么**。
11
17
 
12
18
  ## 安装
13
19
 
@@ -17,44 +23,37 @@
17
23
  dsh plugin --profile <你的 profile> add github:irisblackwood/focus-guard#path:/packages/core
18
24
  ```
19
25
 
20
- 装完 **重启会话** 生效。bundle 补丁由本包的 `package.json` 的 `dsh.bundle.patch` 指向 `src/dsh/cordis.patch.yml`。
26
+ 装完 **重启会话** 生效。bundle 补丁由本包 `package.json` 的 `dsh.bundle.patch` 指向 `src/dsh/cordis.patch.yml`。
27
+
28
+ > 与官方桥 `@deepseek-ai/dsh-hooks-claude-code` **互斥**:两条路挂的是同一批缝,同时挂载会双跑(同一条缝双监听),**二选一**。
21
29
 
22
- > **npm 已发布**:`npm install focus-guard` 装到 **3.3.3**(2026-11-07,GitHub OIDC 受信发布 + 签名 provenance)。上面那条 GitHub 路径跟的是仓库最新代码。
30
+ ### npm
23
31
 
24
- 注意 与官方桥 `@deepseek-ai/dsh-hooks-claude-code` **互斥**:两条路同时挂载会双跑(同一条缝双监听),**二选一**。
32
+ ```bash
33
+ npm install focus-guard
34
+ ```
25
35
 
26
36
  ### ZCode(并存,非首选)
27
37
 
28
- `.zcode-plugin/plugin.json` 已就绪,按 ZCode 的插件安装流程导入本仓库即可。执行壳已封存,版本号独立。
38
+ `.zcode-plugin/plugin.json` 已就绪,按 ZCode 的插件安装流程导入本仓库即可。执行壳已封存——版本号独立,不随主版本演进。
29
39
 
30
40
  ## 它拦什么
31
41
 
32
42
  | 场景 | 机制 |
33
43
  |---|---|
34
- | 未调查取证就改文件 / 执行变更命令 | 第 4 层前置条件:目标须本会话已读,否则拒一次(新建文件除外) |
35
- | 高危命令(递归强删 / 格式化 / 强推 / 发布 / 全局安装 / 系统路径写入) | 第 3 层资格审核:**先申请后执行**,无授权即拒;`fg_apply` 是申请通道 |
36
- | 绝对红线(`rm -rf /`、`DROP DATABASE`、`git push -f`) | 第 1 层零延迟短路拒绝,**不弹审批** |
37
- | 被**引用的**危险命令字符串(测试数据、文档引用、评测集) | 上下文豁免三判据(引号内 / 数据标记 / 只读命令)→ 降级到语义预判,不再直接拒 |
44
+ | 未调查取证就改文件 / 执行变更命令 | L4 前置条件:目标须本会话已读,否则拒一次(新建文件除外) |
45
+ | 高危命令(递归强删 / 格式化 / 强推 / 发布 / 全局安装 / 系统路径写入) | L3 资格审核:**先申请后执行**,无授权即拒;`fg_apply` 是申请通道 |
46
+ | 绝对红线(`rm -rf /`、`DROP DATABASE`、`git push -f`) | L2 零延迟短路拒绝,**不弹审批** |
47
+ | 被**引用的**危险命令字符串(测试数据、文档引用、评测集) | 上下文豁免三判据(引号内 / 数据标记 / 只读命令)→ 降级到语义预判,绝不直接放行 |
38
48
  | 拦错了没有程序可走 | `fg_appeal` 误伤申辩:附反例锚点提交,人类一次性裁决,全程留痕 |
49
+ | 整读大文件 | 超 `FG_WHOLE_FILE_MAX_LINES`(默认 300 行)且未带 `offset`/`limit` 即拒,指向 XERJ |
50
+ | 模糊需求上来就改代码 | 首次动笔前拒一次,要求先加载 `brainstorming` 或 `writing-plans` |
39
51
  | 大小模型一刀切 | 模型画像(`src/profiles/`):按模型调整审核强度 |
40
52
  | 本机该用 `rg`/`fd` 却写了 `grep`/`find` | 环境指纹 + 命令硬校验,给一行替代提示 |
41
53
 
42
- ## 运行面增量(in-tree,2026-10-18)
43
-
44
- 已在代码里跑、但版本号仍记 `3.0.6` 的部分:
45
-
46
- - **心跳与存活确认** — `src/dsh/heartbeat.mjs:85`、`src/dsh/status.mjs:73`:每 10s 原子覆写 `.focus-guard/status.json`;`node src/dsh/status.mjs` 判活并退出 `0` 全绿 / `2` 缺模块 / `3` STALE / `4` 文件缺失。
47
- - **模块启动自检** — `heartbeat.mjs:45`:10 条执法缝 + 心跳各自报到;缺谁写 `degraded`、告警宿主日志、并以 `focus-guard-degraded` 段注入模型。
48
- - **L4 跨会话卷宗** — `src/dsh/workspace.mjs:38`(哈希)/ `:125`(回合落账)/ `src/dsh/index.js:71`(注入):状态按 `sha256(realpath(cwd))` 绑定;hash 不符零注入,改文件夹名无法继承旧状态。
49
- - **整文件读取闸** — `src/dsh/guard/wholeFile.mjs:26` / `:70`:目标 > 300 行且未带 `offset`/`limit` 即拒,指向 XERJ(`xerj search` / `xerj def`);阈值 `FG_WHOLE_FILE_MAX_LINES` 可调。
50
- - **Superpowers 动笔闸** — `src/dsh/guard/superpowers.mjs:34`:模糊开发任务(开发动词 + 模糊标记 + 无验收词)首次动笔拒一次,要求先加载 `brainstorming` 或 `writing-plans`;同条重试或已加载即放行。
51
- - **破坏性 API 分级** — `src/dsh/guard/risk.mjs:37`:`Remove-Item` / `fs.rmSync` 按目标给权——系统路径或递归 `0.9`(拦截)、TEMP `0.2`、其他 `0.4`;重定向 `0.15`。
52
- - **压缩监察** — `src/dsh/compactionWatch.mjs:239` / `:364`:观察 `compaction/start|end|prune` 与请求错误撞窗并留痕,不写 surface、不 cancel 回合。
53
- - **审计记录拦截理由** — `src/dsh/audit.mjs:120`:`auditDeny(exec, cmd, {trigger, level})` 记录真实规则名,不再是常量 `high-risk-rm`。
54
- - **执行体判定** — `src/core/env.mjs:18` + `src/dsh/index.js:31`:母版认显式 `FG_SHELL`;DSH 适配层声明 `powershell`,消除"卷宗【一】/指纹/实际执行"三方矛盾。
55
- - **审计隔离守卫** — `tests/audit-isolation.mjs:1`:跑测前后比对真实 `AUDIT.log`,有增长即 fail(双向验证过)。
54
+ ## 它怎么判
56
55
 
57
- ## 六层资格审核
56
+ 六层,按序。画像可以跳层,**永不能关掉 L2 与 L3**。
58
57
 
59
58
  | 层 | 判什么 |
60
59
  |---|---|
@@ -66,34 +65,57 @@ dsh plugin --profile <你的 profile> add github:irisblackwood/focus-guard#path:
66
65
  | L5 | 语义信号——模型探针的 `mismatch` / 高风险分 |
67
66
  | L6 | 授权并留痕 |
68
67
 
69
- ## 三条命令
68
+ ## 还在跑什么
70
69
 
71
- ```bash
72
- npm test # 全量测试(所有测试文件;acceptance 只是其中之一)
73
- npm run test:eligibility # 资格审核 + 红线豁免 + 申辩
74
- npm run eval # 威胁评测(高危检出率 + 良性误报率)
75
- ```
70
+ 全部已随版本发布。下列模块路径**故意不带行号**——行号会静默腐烂。
71
+
72
+ - **心跳与存活确认** — `src/dsh/heartbeat.mjs`、`src/dsh/status.mjs`:每 10s 原子覆写 `.focus-guard/status.json`;`node src/dsh/status.mjs` 判活并退出 `0` 全绿 / `2` 缺模块 / `3` STALE / `4` 文件缺失。
73
+ - **模块启动自检** — `src/dsh/heartbeat.mjs`:10 条执法缝 + 心跳各自报到;缺谁写 `degraded`、告警宿主日志、并以 `focus-guard-degraded` 段注入模型。
74
+ - **跨会话卷宗** — `src/memory/workspace.mjs`:状态按 `sha256(realpath(cwd))` 绑定;hash 不符零注入,改文件夹名无法继承旧状态。
75
+ - **压缩监察** — `src/dsh/compactionWatch.mjs`:观察 `compaction/start|end|prune` 与请求错误撞窗,并度量摘要相对原文保住了多少锚点(路径 / 行号引用 / 错误码 / 常量)。只读:不写 surface、不 cancel 回合。
76
+ - **前缀稳定** — `src/core/prefixStability.mjs`:每回合给装配后的提示前缀取指纹并记录位移,让"破缓存"的改动变成一条审计事件,而不是一张账单。
77
+ - **破坏性 API 分级** — `src/dsh/guard/risk.mjs`:`Remove-Item` / `fs.rmSync` 按目标给权——系统路径或递归 `0.9`(拦截)、TEMP `0.2`、其他 `0.4`;重定向 `0.15`。
78
+ - **审计记录拦截理由** — `src/dsh/audit.mjs`:`auditDeny(exec, cmd, {trigger, level})` 记录真实规则名(`redline:<name>` / `env-substitute` / `gate:fg_apply` / `evidence-gate` / `whole-file:lines` / `risk:<category>`),不再是常量 `high-risk-rm`。
79
+ - **执行体判定** — `src/core/env.mjs`、`src/dsh/index.js`:母版认显式 `FG_SHELL`;DSH 适配层声明 `powershell`,消除"卷宗 / 指纹 / 实际执行"三方矛盾。
80
+ - **审计隔离守卫** — `tests/audit-isolation.mjs`:跑测前后比对真实 `AUDIT.log`,有增长即 fail。
76
81
 
77
82
  ## 架构
78
83
 
79
84
  ```
80
- src/core/ 母版层:零依赖、宿主无关的判定逻辑(六层审核 / 画像 / 红线与豁免 / 申辩)
81
- src/adapters/dsh/ 适配层:把母版接进 DSH(fg_apply / fg_appeal 工具注册 + 资格闸)
82
- src/dsh/pipeline.mjs 宿主缝:pre-execute / system-prompt / post-execute(编排)
83
- src/dsh/guard/ 各层判定实现(10 个模块,2026-10-27 拆)
84
- hooks/guard.mjs ZCode 执行壳(已封存:版本号独立,不随主版本更新)
85
+ src/core/ 母版层:零依赖、宿主无关的判定逻辑(26 个模块)
86
+ src/adapters/dsh/ 适配层:把母版接进 DSH(fg_apply / fg_appeal 注册 + 资格闸)
87
+ src/dsh/ 宿主缝:pipeline.mjs 编排 8 个事件;各层判定在 guard/
88
+ src/memory/ 跨会话卷宗、标签、工作区哈希
89
+ src/profiles/ 五份纯参数模型画像
90
+ hooks/guard.mjs ZCode 执行壳(已封存:版本号独立)
85
91
  ```
86
92
 
87
- **分层铁律**:母版层**不得**反向依赖适配层(有验收用例锁定)。
93
+ **分层铁律**:母版层**不得**反向依赖适配层(有验收用例锁定)。依赖方向只有一条:`core → adapters → seams`。
94
+
95
+ ## 命令
96
+
97
+ ```bash
98
+ npm test # 全量测试,跑在审计沙箱里
99
+ npm run test:eligibility # 资格审核 + 红线豁免 + 申辩
100
+ npm run eval # 威胁评测(高危检出率 + 良性误报率)
101
+ npm run check # 发版闸门跑的全部
102
+ ```
88
103
 
89
104
  ## 版本
90
105
 
91
- 当前 `3.4.0`。版本面由验收断言锁定:清单彼此相等,门面版本等于已发布版本。
106
+ 三个号,三个含义,别混:
107
+
108
+ | 面 | 值 | 含义 |
109
+ |---|---|---|
110
+ | npm registry | `3.3.3` | 最后一次 `npm publish`(2026-11-07,OIDC 受信发布 + provenance) |
111
+ | GitHub `main` | `3.4.1` | `PUBLISHED_MAX`——本项目说"已发布"时的那个号 |
112
+ | 本树 | `3.5.0` | 在飞;领先已发布不等于已发布 |
92
113
 
93
114
  ## 文档
94
115
 
95
- - 能力与排错:仓库 `README.md`、`INSTALL.md`
96
- - 条文正本:`docs/RULES.md`
116
+ - 能力状态、代码锚点、缺口:`docs/ARCH-MAP.md`
117
+ - 条文正本,以及哪些条款已机械化:`docs/RULES.md`
118
+ - 机制怎么运作:仓库 `docs/ARCHITECTURE.md`
97
119
  - 变更记录:仓库 `CHANGELOG.md`
98
120
 
99
121
  ## License
@@ -0,0 +1,47 @@
1
+ # 第三方数据说明(`packages/core/assets/`)
2
+
3
+ ## `cortex.bin`
4
+
5
+ | 项 | 值 |
6
+ |---|---|
7
+ | 内容 | 人脑 pial 表面(软膜面)点云,27,638 点,每点 `x,y,z`(uint16 量化)+ 沟深代理(uint8) |
8
+ | 来源 | FreeSurfer 导出的 `lh.pial.ply` / `rh.pial.ply`(ASCII PLY,289,561 顶点)降采样 |
9
+ | 生成 | `node packages/core/tools/make-cortex-asset.mjs <pial_Full_ply 目录>` |
10
+ | 许可 | **CC BY-SA 3.0**(源数据来自 memory-brain,Anderson Winkler) |
11
+ | 与主许可的关系 | 本仓代码为 **AGPL-3.0-only**。CC BY-SA 3.0 与 AGPL 不兼容,因此本目录**整体按 CC BY-SA 3.0 授权**,作为独立作品与代码并列分发(mere aggregation),不并入程序源码。 |
12
+
13
+ ### 与 npm 包的关系
14
+
15
+ 自 3.5.0 起 `assets/` **已列入 `packages/core/package.json` 的 `files` 白名单**,
16
+ npm 包与 GitHub clone 拿到的皮层资产完全一致,脑壳在两条路上都是真皮层网格。
17
+
18
+ 两种互不兼容的 copyleft 靠**并列分发**处理,不靠把条款缝在一起:
19
+
20
+ - 代码(`src/` `hooks/` `tools/` …)按 **AGPL-3.0-only** 授权。
21
+ - 本目录(`assets/`)整体按 **CC BY-SA 3.0** 授权,**是独立作品**,不是程序的组成部分。
22
+ - 两者在同一 tarball 内属于 **mere aggregation**:聚合不产生衍生作品,各自条款只约束各自部分。
23
+ - 因此 `package.json` 的 `license` 字段仍只声明代码的 `AGPL-3.0-only`;本文件即为数据部分的
24
+ 完整许可声明与署名,二者缺一不可。
25
+
26
+ 再分发时请保留本文件。若只取用代码、不需要皮层形状,删掉 `assets/` 即可——
27
+ 脑会自动退回程序化沟回(解析椭球 + 噪声刻槽),输出明写
28
+ 「解析椭球(未找到 assets/cortex.bin,形状是算的)」,不会静默降级。
29
+
30
+ ### 再生成
31
+
32
+ 源 PLY 不在版本库里(19 MB ASCII,且同属 CC BY-SA)。要重新生成,需自备
33
+ FreeSurfer 的 pial 表面(`lh.pial.ply` / `rh.pial.ply`),然后:
34
+
35
+ ```bash
36
+ node packages/core/tools/make-cortex-asset.mjs <目录> --target=29000
37
+ ```
38
+
39
+ `--target` 控制总点数(默认 29,000,实际产出约 27,600)。点数越多轮廓越细,
40
+ 体积按每点 7 字节线性增长。
41
+
42
+ ### 署名
43
+
44
+ > Cortical surface data derived from FreeSurfer `pial` surfaces distributed via
45
+ > memory-brain (Anderson Winkler), licensed CC BY-SA 3.0.
46
+ > Modifications: subsampled to 27,638 vertices, quantized to uint16, normals
47
+ > reduced to a single per-vertex depth proxy.
Binary file
package/docs/ARCH-MAP.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # 架构主图(长期规划基线)
2
2
 
3
- > **状态**:权威 · 适用版本 3.3.0 · 最后核对 2026-10-28
3
+ > **状态**:权威 · 适用版本 3.5.0 · 最后核对 2026-11-16
4
4
 
5
5
  > 来源:人类提供的 `deepseek_mermaid_20261018_f03162.mermaid`(2026-10-18)。
6
6
  > 本文件把它固化为**可核对的项目基线**:每一块都给出落地状态、代码锚点、缺口。
@@ -117,20 +117,22 @@ L6 处置结果与伪 RLHF 反馈闭环
117
117
 
118
118
  ---
119
119
 
120
- ## 版本路线(2026-10-31 重排;执行清单见 [`VERSION-PLAN.md`](VERSION-PLAN.md) §1.1)
121
-
122
- | 序 | 版本/批次 | 目标 | 前置 |
123
- |---|---|---|---|
124
- | 0 | **3.3.1**(已标记,待推) | 文档收口 D3/D4/D6 + sessionScope 去重 + 回合出口 | 无 |
125
- | 1 | **3.3.2** | 前缀稳定接线(`prompt-prefix-shift`)+ 临时目录残留清理(§1.5①②) | 无 |
126
- | 2 | **4.0 P0 spike**(无版本号) | deny / `updatedInput` / turn-step 三件实测,失败也记录 | 无 |
127
- | 3 | **3.4.0**(2026-11-11 已发) | **收口批**:3.3.1 后累积 55 条(卷宗丢行 / 前缀口径 / 文档与清单 / fg_env)。N1 / N2 / N4 / P0 结论顺延到 3.4.x | 无(原依赖已解除) |
128
- | 4 | **3.5.0** | 文档完成态交付(内容已就绪,只验收与发版) | 3.4.0 |
129
- | 5 | **4.0.0** | 废除外挂 Hook,内核迁 **MCP 常驻进程**([`MCP-4.0-PLAN.md`](MCP-4.0-PLAN.md)) | spike + 3.4.0 |
130
- | 6 | **4.5** | 记忆独立 + 静态知识库索引 + 伪 RLHF;档 4 不排进 4.5 | 档 3 需先定「人格档存什么」 |
131
-
132
- > 3.3.0 已推 `cb44c57`;**3.4.0 按计数自然发出**(3.3.1 后累积 55 条,越过「满 10 条升次版本」阈值,
133
- > 不靠里程碑例外),N1/N2/N4 顺延到 3.4.x,见 VERSION-PLAN §1.1。
120
+ ## 版本路线(2026-11-16 改定;执行清单见 [`VERSION-PLAN.md`](VERSION-PLAN.md) §1.1)
121
+
122
+ | 序 | 版本/批次 | 目标 | 状态 | 前置 |
123
+ |---|---|---|---|---|
124
+ | 0 | **3.3.1**(10-31) | 文档重置 D1/D3/D4/D6 + `docs/INDEX.md` + sessionScope 去重 + 回合出口 | 已发 | 无 |
125
+ | 1 | **3.4.0**(11-11) | 收口批 55 条(卷宗转义 / 前缀口径 / 文档与清单 / fg_env) | 已发;N1 / N2 / N4 仍未做 | 无 |
126
+ | 2 | **3.4.1**(11-11) | 静态分析告警 + 结构债 + 文档 | 已发 | 序 1 |
127
+ | 3 | **4.0 P0 spike**(无版本号) | deny / `updatedInput` / turn-step 三件实测,失败也记录 | **逾期未做**(原定 3.4 起跑) | **无** |
128
+ | 4 | **3.5.0** | 文档收口 + 更名 / 脑壳资产 / 记忆沉淀 / 压缩损失度量 | 成形未推 | 序 2 |
129
+ | 5 | **4.0.0** | 废除外挂 Hook,内核迁 **MCP 常驻进程**([`MCP-4.0-PLAN.md`](MCP-4.0-PLAN.md)) | 未开始 | spike + 序 4 |
130
+ | 6 | **4.5** | 记忆独立 + 静态知识库索引 + 伪 RLHF;档 4 不排进 4.5 | 未开始 | 档 3 需先定「人格档存什么」 |
131
+
132
+ > **`PUBLISHED_MAX` = 3.4.1**;3.5.0 已成形未推。npm 最新仍 3.0.7(MIT,不可撤销)。
133
+ > **3.3.2 未独立发版**:原定两项,② 系统临时目录残留已于 11-01 落地(VERSION-PLAN §1.5②),
134
+ > ① 前缀稳定接线仍未做(等三点批示,见 §1.5①)。
135
+ > **P0 表里依赖一直是「无」**:它是实测,不改生产代码,可与任何收口并行;「逾期」是排期没跟上,不是被阻塞。
134
136
  > **C6 已撤出排期**(2026-11-11 实测:第 5 层语义探针在 DSH 真实路径上恒未注入,
135
137
  > `modelSignal` 保持 `null`,影子判定没有可补跑的对象),见 VERSION-PLAN §1.6;
136
138
  > **C9/C10**(相似干扰 / 巩固通道)登记在 4.5 · M3,见 §4.5.3;
@@ -0,0 +1,94 @@
1
+ # 脑可视化:渲染实现(BRAIN-VIZ.md)
2
+
3
+ > **状态**:参考 · 适用版本 3.5.0 · 最后核对 2026-11-11
4
+ >
5
+ > 本文只回答一件事:`brain.html` 里那些东西**是怎么画出来的**。
6
+ > 为什么这么设计、不这么做会怎么坏,见 [DESIGN-CONSTRAINTS.md](../../../docs/DESIGN-CONSTRAINTS.md);
7
+ > 数据层(实体、衰减、分区)见 [MEMORY-4.5-PLAN.md](MEMORY-4.5-PLAN.md)。
8
+
9
+ ## 一、一个入口,两档渲染
10
+
11
+ | 入口 | 脑壳来源 | 背景 | 产物 | clone 可用 |
12
+ |---|---|---|---|---|
13
+ | `npm run brain` / `npx focus-guard-brain` | 有 `assets/cortex.bin`:真皮层点云 27,638 点<br>没有:解析椭球 + 程序化沟回 | 纯黑 | 约 0.8 MB / 约 0.5 MB | **是** |
14
+ | `npm run brain:cosmic` | 强制皮层版(真 PLY 网格 + three.js 光照) | 星空 + 双黑洞 | 约 6.2 MB | **否**,资产在私有面 |
15
+
16
+ **数据只有一条链**:`brain.mjs --data-only` 更新索引与 `brain-data.json` → 渲染器读它出图。
17
+ 以前不是这样——皮层版读的 `brain-data.json` 没有任何脚本在写,是某次手工导出的快照,
18
+ 审计日志天天长而它的节点停在导出那天。看着最顺眼的那版,恰恰是唯一不接活数据的。
19
+ 现在两档永远是同一颗脑,区别只在网格与背景。
20
+
21
+ ### 脑壳的三档,以及为什么是三档
22
+
23
+ | 档 | 触发条件 | 判据 |
24
+ |---|---|---|
25
+ | 真 PLY 网格 | `npm run brain:cosmic` + 私有 PLY | 最像,但资产 19 MB 且在私有面,只有本机能跑 |
26
+ | 皮层点云 | 包里有 `assets/cortex.bin` | clone 与 `npm i` 都有,27,638 点,轮廓与沟回都是真的 |
27
+ | 解析椭球 | 上面两者都没有(`assets/` 被删) | 形状是算的,头部会明写 |
28
+
29
+ **为什么不是"要么真要么假"**:真 PLY 是 CC BY-SA 3.0,本仓代码是 AGPL-3.0-only,两者不兼容。
30
+ `assets/cortex.bin` 是它的降采样产物,放在 `assets/` 下**按 CC BY-SA 单独授权**,
31
+ 与代码**并列分发**(mere aggregation)——3.5.0 起已列入 npm `files`,两条安装路径拿到的资产一致。
32
+ 条款与署名见 [assets/NOTICE.md](../assets/NOTICE.md)。
33
+
34
+ 缺资产时**回退而不是报错**:用户要的是「看到脑」,不是「知道有个更高级的版本自己装不起」。
35
+
36
+ **第一次跑是空的,这是对的。** 新装没有足迹,脑的形状由网格与沟回撑住,不靠记忆点亮;
37
+ 跑几次让 guard 记下动作,足迹与连线才长出来。头部如实报「实读几份文档、缺几份」——
38
+ `self-improvement/` 与 `.ai/` 是私有面,别人 clone 下来本来就没有,缺是正常的。
39
+
40
+ ## 二、脑壳:为什么必须是真网格
41
+
42
+ 模板版早期用 `cortexAt()` 算脑形:三个数学函数拼一个椭球,加 0.022 的噪声起伏。
43
+ 它和真皮层版摆在一起,**一眼就是两个东西**——不是背景有没有星,是脑壳不是同一副。
44
+
45
+ 现在 `brain-view.html` 优先读 `assets/cortex.bin`(FreeSurfer pial 表面降采样)。
46
+ 两个关键细节:
47
+
48
+ - **沟深靠几何,不靠噪声**:每点带一个 `grv`(法线·径向的夹角,1 = 回顶、0.25 = 沟底),
49
+ `emit()` 用它压暗沟底。点云没有法线也没有光照,几何凹下去在稀疏点里读不出来,
50
+ 必须再给一层明暗——但**只动颜色不动透明度**,降 alpha 会变成"洞",那是缺数据的样子。
51
+ - **厚度带必须窄**:`emit()` 曾按 `0.80 + 0.20·rnd()` 向心内收,径向随机量 0.20 比沟深还大,
52
+ 刚刻出来的褶皱当场被抹平。网格版收到 3%,解析版 10%。
53
+ - **小脑与脑干的锚点由网格给**:`pial` 只覆盖大脑皮层,这两块始终是解析几何。
54
+ 它们的挂载位置取"后下方那一片的重心",不是我拍一个常数——真皮层下缘比椭球低,
55
+ 照旧常数挂会悬在半空、裂出一道黑缝。
56
+
57
+ ## 三、背景:程序化星空(仅皮层版)
58
+
59
+ 零外部资源,全部着色器生成:会闪的恒星 + 银河带 + 三团彩色星云 + 两台黑洞。
60
+
61
+ 黑洞按真东西画,不是画个黑圈加光晕:
62
+
63
+ - 盘**侧对**(离侧对 11°)——正视的盘看不出多普勒不对称,那才是最容易辨认的特征;
64
+ - 薄盘温度走 **Shakura–Sunyaev** 剖面(T ∝ r^(−3/4)),颜色由温度经**黑体**转 RGB,不是手调色;
65
+ - 亮度 ∝ **g⁴**(多普勒 × 引力红移的合成因子),两侧亮度差约 **80 倍**;
66
+ - 中间那块黑是「这个方向不撒星」撒出来的**真遮挡**,不是叠一层黑圆;
67
+ - 顶上那道弧是引力弯折后的**次级像**。
68
+
69
+ 几何取自 `sundyme/event-horizon`,但**积分器没抄**——它每像素几百步光线步进,
70
+ 我们是 30 ms 预算里的背景,用不起。
71
+
72
+ ## 四、draw call 拆分
73
+
74
+ 星空拆**两个** draw call:
75
+
76
+ | 层 | 内容 | 处理 |
77
+ |---|---|---|
78
+ | 锐点 | 恒星 | 走假 bloom |
79
+ | 雾点 | 银河 / 星云 / 盘面 / 外晕 | 只走一遍,不做 bloom |
80
+
81
+ 判据是**填充率**:对本来就软的大点再做 bloom,只是把同一片像素重复烧一次,视觉上没有增量。
82
+
83
+ ## 五、`?stars=0` 是什么
84
+
85
+ **性能 A/B 用的对照臂,不是给用户的功能开关。** 关掉星空后测同一段场景,
86
+ 实测 **31.2 ms vs 20.1 ms**(软件渲染口径)——这个差值是「背景值不值这个预算」的唯一证据。
87
+
88
+ 它出现在 URL 里而不是设置项里,就是为了让它**不像功能**:没人会在设置面板里
89
+ 找「关掉星星」,但做性能对照的人需要一个可复现的开关。
90
+
91
+ `?sulc=0` 同理:关掉程序化沟回,用于对照"解析椭球到底比真网格差多少"。
92
+
93
+ > 数字口径提醒:31.2 / 20.1 是**软件渲染**(无 GPU 加速)下的单帧耗时。
94
+ > 有 GPU 时两者都远低于此,这个比值不能直接搬到硬件渲染上。
package/docs/RATCHET.json CHANGED
@@ -43,6 +43,22 @@
43
43
  "smells": 8,
44
44
  "duplicates": 21,
45
45
  "note": "还债 2 条:src/dsh/index.js 的「只报不改」三条自检(策略可达性 / 钩子顺序 / 工具类别)各抄了一遍同样的 try/catch + next 透传,收成 apply() 内的 selfCheck 模板,重复块 23→21,apply() 顺带短了 11 行。结构异味 8 条不变:core/state.mjs 因卷宗七列改造涨到 424 行成为第 9 条,当场把 saveCaseRecords / saveLedger 的 tmp+rename 样板收成 rewriteCase,压回 398 行还掉,没有进账——与 3.3.2 / 3.3.3 同一处置:拆代码,不抬阈值。"
46
+ },
47
+ {
48
+ "version": "3.4.1",
49
+ "date": "2026-11-11",
50
+ "commit": "0136589",
51
+ "smells": 8,
52
+ "duplicates": 19,
53
+ "note": "还债 2 条:selftest.mjs 里「未命中绝对红线但被资格闸接管」的断言 `(deny || ask) && !/绝对红线/` 在 A1/A2 抄了三遍,收成 notRedline(),重复块 21→19。结构异味 8 条不变:本轮 state.mjs 删掉多余的 uncell(分列阶段已还原,再还原一次会把 `\\|` 吃成 `|`)反而降到 397 行,仍是 8 条;selftest.mjs 因新增 3 行辅助函数从 517 涨到 520,超长文件本就记在账上,未新增条目。"
54
+ },
55
+ {
56
+ "version": "3.5.0",
57
+ "date": "2026-11-11",
58
+ "commit": "2765c7c",
59
+ "smells": 8,
60
+ "duplicates": 17,
61
+ "note": "还债 2 条:selftest.mjs 里四段「run → 断言 deny」各抄一遍、只换命令字符串(rm -rf ~/ / git push -f / DROP DATABASE / TRUNCATE),收成表驱动的 for-of,重复块 19→17。结构异味 8 条不变:本轮新增两个文件(make-cortex-asset 189 行、memory-distill 约 250 行)都压在 400 行以下;memory-distill 的 distill() 首版 109 行超阈值,当场拆成 loadFiles / indexAuto / expirePass / pickCandidates / entryOf / flush 六件,降到 25 行;brain.mjs 的 main() 因插入沉淀步骤涨到 93 行,把「沉淀→读文档→遗忘」抽成 growMemory、元信息抽成 buildMeta、出图抽成 writeOutput,回到 25 行。两处都是拆代码,不是抬阈值。"
46
62
  }
47
63
  ]
48
64
  }
@@ -156,7 +156,9 @@ CHANGELOG.md 最新条目(须等于上述版
156
156
  ## 四、升版动作清单
157
157
 
158
158
  1. 改上述 7+2 处版本(`PIPELINE_VERSION` 例外:它记的是 `pipeline.mjs` 最后一次改动,本文件没动就不动)
159
- 2. `CHANGELOG.md` 把累积区改为 `## x.y.z · 日期`,条目按 `fix/feat/perf/docs/chore` 前缀
159
+ 2. `CHANGELOG.md` 把累积区改为 `## x.y.z · 日期`,条目按 `fix/feat/perf/docs/chore` 前缀。
160
+ **日期填推送当天**(对外发布日),不填动笔日——两者同日时看不出区别,不同日时前者才是
161
+ 读者要的那个数。所以本清单第 6 条「同日推送」不只是省事,它是日期口径成立的前提。
160
162
  3. 按第一节标**影响面**:整段未对外分发 → 段首写 `> **影响面:开发期**`,且正文不出现「修复」;已发布 → 写「修复」+ 影响面
161
163
  4. 面向使用者的改法同步 `README.md` / `README.zh-CN.md` / `INSTALL.md`
162
164
  5. `npm run check`(含 schema 指纹、真实夹具回归、等变矩阵、受众口径);全绿才允许提交