pi-think-anchor 1.0.9 → 1.0.11

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.
Files changed (3) hide show
  1. package/README.en.md +56 -30
  2. package/README.md +51 -26
  3. package/package.json +1 -1
package/README.en.md CHANGED
@@ -1,29 +1,16 @@
1
- # pi-think-anchor — thinking focus monitor (pi extension)
1
+ # pi-think-anchor — chain-of-thought drift monitor (pi extension)
2
2
 
3
- Measures the "focus fingerprint" of thinking and nudges the model back when it drifts.
3
+ > Requires pi >= 0.74 · Node >= 20 · MIT · no runtime dependencies
4
4
 
5
- ## Features
6
-
7
- - Computes a focus/drift word-frequency ratio per thinking block, aggregated over a sliding window.
8
- - Bands: focused / mixed / reactive; L1 gentle nudge on first drift, L2 stronger intervention on sustained drift.
9
- - Nudges are injected in a tail message with neutral wording; silent while healthy.
10
- - `/think-anchor` shows current state.
11
- - Log: `~/.pi/think-anchor.jsonl`.
5
+ Monitors a "focus fingerprint" in the thinking chain and pulls back in graded steps when the band drifts, avoiding long unproductive digressions.
12
6
 
13
- ## Defaults
14
-
15
- | Item | Default | Note |
16
- | --- | --- | --- |
17
- | Enabled | on | `THINK_ANCHOR=0` or `enabled: false` |
18
- | Bands | 1.2 / 0.5 | focused ≥1.2; reactive <0.5 |
19
- | Window | 5 turns | sliding |
20
- | L1 cooldown | 10 min | first drift |
21
- | L2 cooldown | 15 min | sustained drift, max 2 per session |
22
- | Word lists | Chinese | 28 focus / 18 drift terms |
23
-
24
- ## Credits
7
+ ## Features
25
8
 
26
- Distilled from [dsh-anchored-monitor](https://dsh-plugin.org/zh/plugins/aik358/dsh-anchored-monitor) 0.3.1 (DSH ecosystem): the mechanism idea only, no code port — per-turn evaluation instead of live streaming, no separate process. Thresholds come from two replays (all turns: react 11.3% → 3.4%; scorable turns with ≥3 hits: 18.3% → 9.7%; 6151 logged rows).
9
+ - Computes a focus/distraction word-ratio per thinking block and classifies the band over a sliding window (focused / mixed / reactive).
10
+ - L1 gentle nudge when drifting into reactive; L2 stronger intervention if it persists; nothing at all when healthy.
11
+ - Interventions are injected as trailing messages (neutral wording), never written into the system prompt.
12
+ - Command `/think-anchor` for status, `/think-anchor stats` for version / bands / actions / effectiveness / log size.
13
+ - Log at `~/.pi/think-anchor.jsonl` (tail-preserving truncation at 1 MiB).
27
14
 
28
15
  ## Install
29
16
 
@@ -32,20 +19,59 @@ pi install npm:pi-think-anchor
32
19
  pi install ./pi-think-anchor
33
20
  ```
34
21
 
35
- ## Config
22
+ ## Configuration
23
+
24
+ ### File `~/.pi/think-anchor.json`
36
25
 
37
- `~/.pi/think-anchor.json`, e.g.:
26
+ Every key is optional; defaults below apply when omitted, and invalid JSON falls back to defaults.
27
+
28
+ | Key | Type | Default | Description |
29
+ | --- | --- | --- | --- |
30
+ | `enabled` | boolean | `true` | Master switch |
31
+ | `score.minHits` | number | `3` | Evidence gate: a turn with fewer hits is neither scored nor intervened |
32
+ | `score.spec` | number | `1.2` | Focused threshold (alpha-smoothed ratio >= this means spec) |
33
+ | `score.react` | number | `0.5` | Reactive threshold (ratio < this means react) |
34
+ | `score.alpha` | number | `1` | Alpha smoothing (pulls low-hit ratios toward 1) |
35
+ | `windowSize` | number | `5` | Sliding window size in messages |
36
+ | `weights.beta` | number | `0.4` | Weight of neutral words in the numerator |
37
+ | `weights.eps` | number | `0.5` | Denominator floor (avoids division by zero) |
38
+ | `intervention.l1CooldownMin` | number | `10` | L1 cooldown in minutes |
39
+ | `intervention.l2CooldownMin` | number | `15` | L2 cooldown in minutes |
40
+ | `intervention.maxL2` | number | `2` | Max L2 interventions per session |
41
+ | `lexicon.positive` / `negative` / `neutral` | string[] | built-in Chinese lexicon | Override word lists (28 focus / 18 drift by default) |
42
+ | `bands` | object | — | Legacy compatibility: used when `score` is absent |
43
+
44
+ ### Environment
45
+
46
+ | Variable | Value | Description |
47
+ | --- | --- | --- |
48
+ | `THINK_ANCHOR` | `0` | Hard off; overrides the config file |
49
+
50
+ ### Example
38
51
 
39
52
  ```json
40
- { "enabled": true }
53
+ {
54
+ "enabled": true,
55
+ "score": { "minHits": 3, "spec": 1.2, "react": 0.5, "alpha": 1 },
56
+ "windowSize": 5,
57
+ "intervention": { "l1CooldownMin": 10, "l2CooldownMin": 15, "maxL2": 2 }
58
+ }
41
59
  ```
42
60
 
43
61
  ## How it works
44
62
 
45
- - `turn_end` evaluates each assistant message's thinking, then injects on the next turn when a condition fires.
46
- - Word lists and thresholds can be calibrated from the log.
63
+ - `turn_end` evaluates each assistant thinking block, then a conditional trailing injection on the next turn.
64
+ - v2.3 criteria: per-turn evidence gate plus an alpha-smoothed ratio; low-evidence turns are not scored.
65
+ - No escalation when ineffective: if the previous intervention did not improve the ratio, L2 degrades to L1.
66
+ - Effectiveness loop: `eff` is settled three turns after an intervention; the window survives 30 minutes across sessions.
67
+ - Adaptive silence: with >= 8 samples and < 50% improvement it records without injecting.
68
+
69
+ ## Limitations
70
+
71
+ - Turn-level evaluation, not in-stream real time.
72
+ - The default lexicon is Chinese-oriented; English sessions depend on wording.
73
+ - Interventions are hints, not enforcement.
47
74
 
48
- ## Limits
75
+ ## License
49
76
 
50
- - Per-turn evaluation, not streaming.
51
- - Word lists are tuned for Chinese; English sessions depend on vocabulary.
77
+ MIT
package/README.md CHANGED
@@ -1,30 +1,16 @@
1
- # pi-think-anchor — 思维链锚定监测(pi 扩展)
1
+ # pi-think-anchor —— 思维链锚定监测(pi 扩展)
2
2
 
3
- 监测 thinking 的「专注指纹」,波段漂移时分级拉回,避免长时间发散。
4
-
5
- ## 来源与借鉴
3
+ > 适用 pi ≥ 0.74 · Node ≥ 20 · MIT · 无运行时依赖
6
4
 
7
- 蒸馏自 [dsh-anchored-monitor](https://dsh-plugin.org/zh/plugins/aik358/dsh-anchored-monitor) 0.3.1(DSH 生态):机制思想来源,非代码移植(回合级而非流内实时、无独立进程)。判据经两轮回放校准(全量轮次 react 11.3% → 3.4%;命中 ≥3 的可评分回合 18.3% → 9.7%;日志共 6151 行):**v2.3 = 单轮证据门槛 + α 平滑专注/发散比**(α=1;react <0.5 / mixed <1.2 / spec ≥1.2)。命中不足的轮次不评分(避免把「长而散」判成反复试探)。
5
+ 监测 thinking 的「专注指纹」,波段漂移时分级拉回,避免长时间发散。
8
6
 
9
7
  ## 功能
10
8
 
11
- - 对每段 thinking 计算专注/发散词频比率,滑动窗口聚合判定波段。
12
- - 波段:高专注 / 过渡 / 发散;跌入发散时 L1 温和引导,持续发散时 L2 强干预。
13
- - 干预走对话尾部注入,中性措辞;健康时零注入(不打扰)。
14
- - 命令 `/think-anchor` 查看当前状态。
15
- - 日志 `~/.pi/think-anchor.jsonl`。
16
-
17
- ## 默认配置
18
-
19
- | 项 | 默认 | 说明 |
20
- | --- | --- | --- |
21
- | 启用 | 开 | `THINK_ANCHOR=0` 或配置文件 `enabled: false` 关闭 |
22
- | 波段阈值 | 1.2 / 0.5 | α 平滑比率:高专注 ≥1.2;发散 <0.5 |
23
- | 证据门槛 | 3 命中/轮 | 本轮词频命中不足 3 个 → 不评分、不干预 |
24
- | 聚合窗口 | 5 条消息 | 滑动窗口(长工具链中约 1 轮) |
25
- | L1 冷却 | 10 分钟 | 首次跌入发散 |
26
- | L2 冷却 | 15 分钟 | 持续发散,会话上限 2 次 |
27
- | 词表 | 中文 | 专注 28 词 / 发散 18 词 |
9
+ - 每段 thinking 计算专注 / 发散词频比率,滑动窗口聚合后判定波段(高专注 / 过渡 / 发散)。
10
+ - 跌入发散时 L1 温和引导;持续发散时 L2 强干预;健康时零注入。
11
+ - 干预走对话尾部注入(中性措辞),不写 system。
12
+ - 命令 `/think-anchor` 查看状态,`/think-anchor stats` 查看版本 / 波段 / 动作 / 有效率 / 日志体积。
13
+ - 日志 `~/.pi/think-anchor.jsonl`(超 1 MiB 保留尾部整行)。
28
14
 
29
15
  ## 安装
30
16
 
@@ -35,18 +21,57 @@ pi install ./pi-think-anchor
35
21
 
36
22
  ## 配置
37
23
 
38
- `~/.pi/think-anchor.json`,例如:
24
+ ### 配置文件 `~/.pi/think-anchor.json`
25
+
26
+ 所有键可省略,缺省用下表默认值;非法 JSON 自动回退默认。
27
+
28
+ | 键 | 类型 | 默认 | 说明 |
29
+ | --- | --- | --- | --- |
30
+ | `enabled` | boolean | `true` | 总开关 |
31
+ | `score.minHits` | number | `3` | 证据门槛:单轮词频命中少于该值则不评分、不干预 |
32
+ | `score.spec` | number | `1.2` | 高专注阈值(α 平滑比率 ≥ 该值为 spec) |
33
+ | `score.react` | number | `0.5` | 发散阈值(比率 < 该值为 react) |
34
+ | `score.alpha` | number | `1` | α 平滑(让低命中比率趋向 1,避免「长而散」误判) |
35
+ | `windowSize` | number | `5` | 滑动窗口消息数(长工具链中约 1 轮) |
36
+ | `weights.beta` | number | `0.4` | 中性词计入分子的权重 |
37
+ | `weights.eps` | number | `0.5` | 分母下限(防除零) |
38
+ | `intervention.l1CooldownMin` | number | `10` | L1 冷却(分钟) |
39
+ | `intervention.l2CooldownMin` | number | `15` | L2 冷却(分钟) |
40
+ | `intervention.maxL2` | number | `2` | 每会话 L2 次数上限 |
41
+ | `lexicon.positive` / `negative` / `neutral` | string[] | 内置中文词表 | 词表覆盖(专注 28 词 / 发散 18 词) |
42
+ | `bands` | object | — | 旧版兼容:无 `score` 时由 `bands.spec/react` 映射 |
43
+
44
+ ### 环境变量
45
+
46
+ | 变量 | 取值 | 说明 |
47
+ | --- | --- | --- |
48
+ | `THINK_ANCHOR` | `0` | 硬关闭,优先级高于配置文件 |
49
+
50
+ ### 示例
39
51
 
40
52
  ```json
41
- { "enabled": true }
53
+ {
54
+ "enabled": true,
55
+ "score": { "minHits": 3, "spec": 1.2, "react": 0.5, "alpha": 1 },
56
+ "windowSize": 5,
57
+ "intervention": { "l1CooldownMin": 10, "l2CooldownMin": 15, "maxL2": 2 }
58
+ }
42
59
  ```
43
60
 
44
61
  ## 机制
45
62
 
46
- - `turn_end` 评估每条 assistant 消息的 thinking → 条件触发 → 下一轮注入。
47
- - 词表与阈值可按日志校准。
63
+ - `turn_end` 逐条评估 assistant 消息的 thinking → 条件触发 → 下一轮注入。
64
+ - v2.3 判据:单轮证据门槛 + α 平滑比率;命中不足的轮次不评分。
65
+ - 无效不升级:上次干预实测无改善则本次 L2 自动降为 L1。
66
+ - 效果闭环:干预后第 3 轮结算 `eff`;跨会话窗口保留 30 分钟。
67
+ - 自适应静默:样本 ≥ 8 且下降率 < 50% 时只记录不注入。
48
68
 
49
69
  ## 限制
50
70
 
51
71
  - 按回合评估,非流内实时。
52
72
  - 词表默认偏中文;英文会话效果取决于用词。
73
+ - 干预是提示,不是强制;模型可能不遵守。
74
+
75
+ ## License
76
+
77
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-think-anchor",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "description": "Monitors thinking focus drift and nudges the model back on track.",
5
5
  "type": "module",
6
6
  "license": "MIT",