dsh-echo-guard 0.0.0-stage → 0.1.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 +21 -0
- package/README.en.md +100 -0
- package/README.md +109 -2
- package/cordis.patch.yml +34 -0
- package/lib/core.mjs +106 -0
- package/lib/index.mjs +176 -0
- package/package.json +47 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 dsh-echo-guard contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.en.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# dsh-echo-guard
|
|
2
|
+
|
|
3
|
+
A DeepSeek Harness (DSH) plugin that removes filler-word flooding from the **model-visible reasoning history** — and leaves everything else verbatim.
|
|
4
|
+
|
|
5
|
+
## What it treats
|
|
6
|
+
|
|
7
|
+
In long sessions, reasoning degrades into stamping: one closing word after every sentence — «好。» «好。» «好。» … until the reasoning holds nothing else and the actual reply never gets written.
|
|
8
|
+
|
|
9
|
+
That is not the model getting worse. It is **over-confirmation under pressure**: the model revises a sentence, confirms the revision, the confirmation spawns another action, and so on. And the history is full of those samples, so the next turn copies them.
|
|
10
|
+
|
|
11
|
+
Worse, DSH sends historical `reasoning` blocks back upstream, so the loop is not confined to that one step: it stays in context and replays every step, every turn.
|
|
12
|
+
|
|
13
|
+
## What it does
|
|
14
|
+
|
|
15
|
+
When filler words appear ≥ N times (default 5) in one reasoning block:
|
|
16
|
+
|
|
17
|
+
1. **Only standalone occurrences are removed** — «好。», «好,»; occurrences inside words («好像», «刚好») stay;
|
|
18
|
+
2. **everything else is kept verbatim**;
|
|
19
|
+
3. a **one-line receipt** is left in the visible history (configurable — including empty or zero-width).
|
|
20
|
+
|
|
21
|
+
The event log is never modified, so "what the model remembers" and "what the log says" can both be true.
|
|
22
|
+
|
|
23
|
+
## What it does not do
|
|
24
|
+
|
|
25
|
+
- **It does not fold** whole repeated blocks. That is thinking-guard's job in [dsh-degeneration-kit](https://github.com/walksls/dsh-degeneration-kit). The two can run together: this plugin only touches **pure-reasoning steps with no tool calls**, leaving steps with tool calls to the other one, so they never rewrite the same node.
|
|
26
|
+
- **It does not save money.** Removing a handful of characters barely changes token count (90–97% remains). Its value is stopping the history from teaching the model to keep looping.
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pnpm add link:/path/to/dsh-echo-guard
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Add a `link:` dependency to the profile's `package.json` and include it in `dsh.profile.bundles`.
|
|
35
|
+
|
|
36
|
+
Configure via `cordis.patch.yml`. **Start with `dryRun: true`** for a day.
|
|
37
|
+
|
|
38
|
+
## Fillers
|
|
39
|
+
|
|
40
|
+
```yaml
|
|
41
|
+
fillers: ["好", "嗯"] # Chinese
|
|
42
|
+
fillers: ["OK", "let me go"] # English, case-insensitive
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
- **Counting** includes occurrences inside words (`好` inside `好像`, `OK` inside `Okay`).
|
|
46
|
+
- **Deletion** only removes standalone ones: preceded by a non-word char, followed by punctuation/space/EOL.
|
|
47
|
+
- CJK uses character boundaries, Latin uses alphanumeric boundaries; multi-word phrases are removed whole.
|
|
48
|
+
|
|
49
|
+
## Receipt
|
|
50
|
+
|
|
51
|
+
- `receipt: "⟪已整理⟫"` — visible (default);
|
|
52
|
+
- `receipt: ""` — nothing. **Not recommended**: an invisible edit is a forged history; the reader eventually assumes the text was always there;
|
|
53
|
+
- `receipt: "\u200B\u200B\u200B"` — invisible but greppable by code point.
|
|
54
|
+
|
|
55
|
+
## Measured on 1000+ real reasoning blocks
|
|
56
|
+
|
|
57
|
+
- occurrences per block: median 1, P90 = 4, P99 = 12, ≥5 in only 7%;
|
|
58
|
+
- position: first 10% holds 2%, **last 20% holds 40%** — a closing move, not an opening habit;
|
|
59
|
+
- the worst collapses were all above ~200k tokens; compaction removed them instantly. **Context amplifies, it does not cause.**
|
|
60
|
+
|
|
61
|
+
Hence the threshold of 5, aimed at the "one per sentence" drift.
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
## Version and testing
|
|
65
|
+
|
|
66
|
+
**Version 0.1.0.**
|
|
67
|
+
|
|
68
|
+
Verified on 2026-12-05 with:
|
|
69
|
+
|
|
70
|
+
- DSH 0.2.2-rc.5 (Windows)
|
|
71
|
+
- Node 24.22.1
|
|
72
|
+
- Method: `dryRun: true` for one pass → 5 reasoning blocks matched in the log; then `dryRun: false` → all 5 were actually rewritten.
|
|
73
|
+
- Checked item by item:
|
|
74
|
+
- replacement messages carry id `echo-guard-<seq>` and source `plugin:echo-guard`, starting with the receipt;
|
|
75
|
+
- the original `assistant/message` events remain in the event stream, untouched;
|
|
76
|
+
- all 5 matches were "reasoning + text" steps with zero tool calls — steps with tool calls are skipped by design;
|
|
77
|
+
- the other session's file was never written (`onlySessions` respected).
|
|
78
|
+
- Unit tests: `node --test test/core.test.mjs` — 10 cases pass.
|
|
79
|
+
|
|
80
|
+
**Not verified**: the case where both this plugin and thinking-guard match the same step. They do not overlap by design (steps with tool calls are skipped here), but that scenario was not exercised.
|
|
81
|
+
|
|
82
|
+
## Limitations
|
|
83
|
+
|
|
84
|
+
- DSH only; built on `session.append` + `surfaceOp`.
|
|
85
|
+
- Steps with tool calls are skipped by design.
|
|
86
|
+
- Tokenizers may swallow invisible characters — measure before using a zero-width receipt.
|
|
87
|
+
|
|
88
|
+
## Test
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
npm test
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Credits
|
|
95
|
+
|
|
96
|
+
Host API usage was learned from [dsh-degeneration-kit](https://github.com/walksls/dsh-degeneration-kit) by walksls (MIT) — same problem, same API. The code here is rewritten and keeps only the strip behaviour.
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
|
|
100
|
+
MIT.
|
package/README.md
CHANGED
|
@@ -1,3 +1,110 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-echo-guard
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
一个 DeepSeek Harness(DSH)插件:把「收束词灌水」从**模型可见的思考历史**里删掉,其余一个字不动。
|
|
4
|
+
|
|
5
|
+
## 它治什么
|
|
6
|
+
|
|
7
|
+
长对话跑久了,模型的思考里会出现一种退化:每句话后面盖个章——「好。」「好。」「好。」……
|
|
8
|
+
|
|
9
|
+
整段思考最后只剩那个字,正文反而没发出来。
|
|
10
|
+
|
|
11
|
+
这不是模型变笨,是**高压下的过度确认**:它每改一版就自我确认一次,确认动作又生出新动作,越确认越多。而历史里全是这种样本,下一轮它照着学。
|
|
12
|
+
|
|
13
|
+
更麻烦的是:DSH 会把历史里的 `reasoning` 块回传给上游,所以这段复读不只是"这一步废了"——它留在上下文里,之后每一步、每一轮都带着它重放。
|
|
14
|
+
|
|
15
|
+
## 它做什么
|
|
16
|
+
|
|
17
|
+
一段思考里,收束词累计 ≥ N 次(默认 5)时:
|
|
18
|
+
|
|
19
|
+
1. **只删单独成句的那几个**——「好。」「好,」;词里的(「好像」「刚好」「挺好」)留着;
|
|
20
|
+
2. **其余原文照录**,一个字不改;
|
|
21
|
+
3. 在可见历史里留**一行回执**(可配,也可以设成零宽字符或留空)。
|
|
22
|
+
|
|
23
|
+
事件流一个字不动。所以「模型记得什么」和「日志里写的是什么」可以同时对。
|
|
24
|
+
|
|
25
|
+
## 它不做什么
|
|
26
|
+
|
|
27
|
+
- **不折叠**整段复读。那是 [dsh-degeneration-kit](https://github.com/walksls/dsh-degeneration-kit) 里 thinking-guard 的活,两个插件可以同时装:本插件只处理**没有工具调用的纯思考步**,带工具调用的整步留给它,互不抢同一段改写。
|
|
28
|
+
- **不省钱**。删几个字,token 几乎不变(实测剩下 90%~97%)。它的价值是"不再拿这些当示范教模型",不是省 token。
|
|
29
|
+
|
|
30
|
+
## 装法
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pnpm add link:/path/to/dsh-echo-guard
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
在 profile 的 `package.json` 里加一条 `link:` 依赖,并把它加进 `dsh.profile.bundles`。
|
|
37
|
+
|
|
38
|
+
配置见 `cordis.patch.yml`。
|
|
39
|
+
|
|
40
|
+
**第一次建议 `dryRun: true`**,跑一天看日志里命中多少、删掉多少,再决定阈值。
|
|
41
|
+
|
|
42
|
+
## 词表
|
|
43
|
+
|
|
44
|
+
```yaml
|
|
45
|
+
fillers: ["好", "嗯"] # 中文
|
|
46
|
+
fillers: ["OK", "let me go"] # 英文,大小写不敏感
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
规则:
|
|
50
|
+
|
|
51
|
+
- **计数**算全部:`好` 会数进「好像」,`OK` 会数进 `Okay`。
|
|
52
|
+
- **删除**只删单独成句的:前面不是词、后面是标点/空白/行尾。
|
|
53
|
+
- 中文按汉字边界判,英文按字母数字边界判;多词短语(`let me go`)整句删。
|
|
54
|
+
|
|
55
|
+
## 回执
|
|
56
|
+
|
|
57
|
+
删完会在那条历史里留一行。三种配法:
|
|
58
|
+
|
|
59
|
+
- `receipt: "⟪已整理⟫"` —— 默认,看得见;
|
|
60
|
+
- `receipt: ""` —— 完全不留。**不建议**:看不见的改动等于篡改历史,使用者某天翻回去会以为字本来就在;
|
|
61
|
+
- `receipt: "\u200B\u200B\u200B"` —— 看不见,但按码点查得到。
|
|
62
|
+
|
|
63
|
+
## 实测数据(1000+ 条真实思考)
|
|
64
|
+
|
|
65
|
+
- 那个词的出现次数:中位数 1,P90 = 4,P99 = 12,≥5 次的只占 7%。
|
|
66
|
+
- 位置:开头 10% 只占 2%,**最后 20% 占 40%**——是收束动作,不是起笔习惯。
|
|
67
|
+
- 崩得最狠的那几条都在 20 万 token 以上;压缩之后立刻消失。**上下文是放大器,不是原因。**
|
|
68
|
+
|
|
69
|
+
所以阈值定在 5,专抓「一句一个」的分散型;整段复读交给折叠和急停阀。
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
## 版本与验证
|
|
73
|
+
|
|
74
|
+
**版本 0.1.0。**
|
|
75
|
+
|
|
76
|
+
在以下环境实测通过(2026-12-05):
|
|
77
|
+
|
|
78
|
+
- DSH 0.2.2-rc.5(Windows)
|
|
79
|
+
- Node 24.22.1
|
|
80
|
+
- 验证方式:先 `dryRun: true` 跑一轮,日志里命中 5 段思考;再关掉 dryRun 重跑,5 段全部真改写。
|
|
81
|
+
- 逐条核对过:
|
|
82
|
+
- 替换消息的 id 形如 `echo-guard-<seq>`,来源 `plugin:echo-guard`,正文以回执开头;
|
|
83
|
+
- 原始 `assistant/message` 事件仍在事件流里,一个字没动;
|
|
84
|
+
- 命中的 5 段全是「纯思考 + 正文」、零工具调用——带工具调用的步按设计跳过;
|
|
85
|
+
- 另一个会话的文件没有被写过(`onlySessions` 生效)。
|
|
86
|
+
- 单测:`node --test test/core.test.mjs`,10 个用例全过。
|
|
87
|
+
|
|
88
|
+
**未验证**:两个插件同时命中同一步的交叉场景。设计上不重叠(带工具调用的步本插件直接跳过),但确实没实测到。
|
|
89
|
+
|
|
90
|
+
## 限制
|
|
91
|
+
|
|
92
|
+
- 只对 DSH 有效,用的宿主接口是 `session.append` + `surfaceOp`。
|
|
93
|
+
- 只处理没有工具调用的步(带工具调用的整步替换会跟 thinking-guard 打架)。
|
|
94
|
+
- 分词器可能吞掉不可见字符,用零宽回执前先实测。
|
|
95
|
+
|
|
96
|
+
## 测试
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
npm test
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
`lib/core.mjs` 是纯函数、不依赖宿主,单测覆盖中英文、词内/独立、标点清理。
|
|
103
|
+
|
|
104
|
+
## 来源
|
|
105
|
+
|
|
106
|
+
宿主接口的用法参考过 [dsh-degeneration-kit](https://github.com/walksls/dsh-degeneration-kit)(作者 walksls,MIT)——同一个问题、同一套 API。代码是重写的,只保留了「删字」这一件事。
|
|
107
|
+
|
|
108
|
+
## 许可
|
|
109
|
+
|
|
110
|
+
MIT。
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
- insert:
|
|
2
|
+
- id: echo-guard
|
|
3
|
+
name: dsh-echo-guard
|
|
4
|
+
# 要读 agent.session,所以等 agents 服务就绪
|
|
5
|
+
inject: [ agents ]
|
|
6
|
+
config:
|
|
7
|
+
enabled: true
|
|
8
|
+
|
|
9
|
+
# 哪些词算「收束词灌水」。中文常见这两个;英文可以写 ["OK", "let me go", "alright"]。
|
|
10
|
+
# 计数算全部(「好像」里的「好」也算),删除只删单独成句的那一个。
|
|
11
|
+
fillers:
|
|
12
|
+
- "好"
|
|
13
|
+
- "嗯"
|
|
14
|
+
|
|
15
|
+
# 一段思考里这些词合计 ≥ 这个数就动手
|
|
16
|
+
burstMinCount: 5
|
|
17
|
+
|
|
18
|
+
# 删完在历史里留的那一行,可配:
|
|
19
|
+
# "⟪已整理⟫" 看得见(默认)
|
|
20
|
+
# "" 完全不留(不建议)
|
|
21
|
+
# "\u200B\u200B\u200B" 看不见,但按码点查得到
|
|
22
|
+
receipt: "⟪已整理⟫"
|
|
23
|
+
|
|
24
|
+
# 删完剩下的字数不到原来的这个比例就放弃(防止整段都是收束词时被删空)
|
|
25
|
+
stripMinKeepRatio: 0.2
|
|
26
|
+
|
|
27
|
+
# 只对这些会话生效(空 = 全部)。填会话 id 的片段。
|
|
28
|
+
onlySessions: []
|
|
29
|
+
|
|
30
|
+
# 先观察:true 时只写日志、不改历史。建议先跑一天。
|
|
31
|
+
dryRun: true
|
|
32
|
+
|
|
33
|
+
# 日志(会话标签 + 每次命中一条)
|
|
34
|
+
# debugFile: "./echo-guard.log"
|
package/lib/core.mjs
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// 收束词/复读词检测与清理的核心,跟宿主无关,纯函数,方便单测和复用。
|
|
2
|
+
//
|
|
3
|
+
// 为什么单独一个文件:原来这套逻辑写死了一个中文字(中文里最常见的收束词),
|
|
4
|
+
// 换英文、换别的模型就得改代码。这里把它抽成「词表 + 边界规则」,词表从配置来。
|
|
5
|
+
//
|
|
6
|
+
// 两个概念要分清:
|
|
7
|
+
// - **计数**算全部:连「好像」「刚好」里夹着的那个字也算,因为门槛是"出现得太多"。
|
|
8
|
+
// - **删除**只删单独成句的那一个:前后都不是词的一部分才删。
|
|
9
|
+
// 这条区分是整套逻辑的关键,别简化。
|
|
10
|
+
|
|
11
|
+
export const DEFAULT_FILLERS = ['好', '嗯']
|
|
12
|
+
|
|
13
|
+
// 跟着被删词一起带走的标点(中文、中文全角、英文常见)
|
|
14
|
+
const TRAILING_PUNCT = '[,,、。.!!??;;::.…")”’]'
|
|
15
|
+
const PUNCT_AFTER = '[,,、。.!!??;;::.…\\s")”’]|$'
|
|
16
|
+
|
|
17
|
+
const CJK = /[\u3400-\u4dbf\u4e00-\u9fff]/
|
|
18
|
+
|
|
19
|
+
/** 判断一个词是不是纯中文(用汉字边界),否则按拉丁字母词边界处理。 */
|
|
20
|
+
export function isCJKWord(word) {
|
|
21
|
+
return Array.from(word).every((ch) => CJK.test(ch))
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function escapeRe(s) {
|
|
25
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 造一个「单独出现」的正则。
|
|
30
|
+
* @param {string} word
|
|
31
|
+
* @param {{caseInsensitive?:boolean}} [opts]
|
|
32
|
+
*/
|
|
33
|
+
export function standalonePattern(word, opts = {}) {
|
|
34
|
+
const w = escapeRe(word)
|
|
35
|
+
if (isCJKWord(word)) {
|
|
36
|
+
// 前面不是汉字;后面是标点/空白/行尾,或紧跟标点时一起带走
|
|
37
|
+
return new RegExp(`(?<![\\u3400-\\u4dbf\\u4e00-\\u9fff])${w}(?=${PUNCT_AFTER})${TRAILING_PUNCT}?`, 'g')
|
|
38
|
+
}
|
|
39
|
+
const flags = opts.caseInsensitive === false ? 'g' : 'gi'
|
|
40
|
+
// 前后都不能是字母/数字,避免误杀 okra、book 这类
|
|
41
|
+
return new RegExp(`(?<![A-Za-z0-9])${w}(?![A-Za-z0-9])(?=${PUNCT_AFTER})${TRAILING_PUNCT}?`, flags)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 计数:整段里这些词一共出现多少次(含词内部的,比如「好像」里的「好」)。 */
|
|
45
|
+
export function countFillers(text, fillers = DEFAULT_FILLERS) {
|
|
46
|
+
if (typeof text !== 'string' || text === '') return 0
|
|
47
|
+
let n = 0
|
|
48
|
+
for (const word of fillers) {
|
|
49
|
+
if (!word) continue
|
|
50
|
+
const re = new RegExp(escapeRe(word), isCJKWord(word) ? 'g' : 'gi')
|
|
51
|
+
n += (text.match(re) || []).length
|
|
52
|
+
}
|
|
53
|
+
return n
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 只删单独成句的那些,其余原样。
|
|
58
|
+
* @returns {{text:string, removed:number, counted:number}}
|
|
59
|
+
*/
|
|
60
|
+
export function stripFillers(text, fillers = DEFAULT_FILLERS) {
|
|
61
|
+
if (typeof text !== 'string' || text === '') return { text: '', removed: 0, counted: 0 }
|
|
62
|
+
const counted = countFillers(text, fillers)
|
|
63
|
+
if (counted === 0) return { text, removed: 0, counted: 0 }
|
|
64
|
+
|
|
65
|
+
let out = text
|
|
66
|
+
let removed = 0
|
|
67
|
+
for (const word of fillers) {
|
|
68
|
+
if (!word) continue
|
|
69
|
+
const re = standalonePattern(word)
|
|
70
|
+
out = out.replace(re, () => {
|
|
71
|
+
removed += 1
|
|
72
|
+
return ''
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// 删完可能留下的残渣
|
|
77
|
+
out = out.replace(/「」/g, '')
|
|
78
|
+
out = out.replace(/([。!?!?])(?:\s*[。!?!?])+/g, '$1')
|
|
79
|
+
out = out.replace(/([,,、])(?:\s*[,,、])+/g, '$1')
|
|
80
|
+
out = out.replace(/^\s*[,,、。!?!?]+\s*/gm, '')
|
|
81
|
+
out = out.replace(/[,,、]\s*$/gm, '')
|
|
82
|
+
out = out.replace(/\n{3,}/g, '\n\n').replace(/[ \t]+\n/g, '\n').trim()
|
|
83
|
+
|
|
84
|
+
return { text: out, removed, counted }
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 在一段文字里找「这些词扎堆出现」的位置。
|
|
89
|
+
* @param {string} text
|
|
90
|
+
* @param {string[]} fillers
|
|
91
|
+
* @param {{minCount?:number}} [cfg]
|
|
92
|
+
* @returns {null | {firstAt:number, count:number}}
|
|
93
|
+
*/
|
|
94
|
+
export function findBurst(text, fillers = DEFAULT_FILLERS, cfg = {}) {
|
|
95
|
+
if (typeof text !== 'string' || text === '') return null
|
|
96
|
+
const minCount = Number.isSafeInteger(cfg.minCount) ? cfg.minCount : 5
|
|
97
|
+
const count = countFillers(text, fillers)
|
|
98
|
+
if (count < minCount) return null
|
|
99
|
+
let firstAt = -1
|
|
100
|
+
for (const word of fillers) {
|
|
101
|
+
if (!word) continue
|
|
102
|
+
const i = text.indexOf(word)
|
|
103
|
+
if (i >= 0 && (firstAt < 0 || i < firstAt)) firstAt = i
|
|
104
|
+
}
|
|
105
|
+
return { firstAt, count }
|
|
106
|
+
}
|
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-echo-guard —— 把「收束词灌水」从模型可见历史里删掉。
|
|
3
|
+
*
|
|
4
|
+
* 跟 dsh-degeneration-kit(作者 walksls,MIT)里的 thinking-guard 的区别,一句话:
|
|
5
|
+
* - 那个是**折叠**:整条步骤从可见历史里换掉,抓「同一句重复」;
|
|
6
|
+
* - 这个是**删字**:只删单独成句的收束词,其余原文照录,抓「一句一个」。
|
|
7
|
+
*
|
|
8
|
+
* 两个可以同时装:这个只处理「没有工具调用的纯思考步」,带工具调用的整步折叠留给
|
|
9
|
+
* thinking-guard,互不抢同一段改写。
|
|
10
|
+
*
|
|
11
|
+
* 一条硬约束:**只改 surface(模型读到的那层),事件流一个字不动。**
|
|
12
|
+
* 所以「模型记得什么」和「日志里写的是什么」可以同时对。
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import fs from 'node:fs'
|
|
16
|
+
import { stripFillers, countFillers, DEFAULT_FILLERS } from './core.mjs'
|
|
17
|
+
|
|
18
|
+
export const name = 'echo-guard'
|
|
19
|
+
|
|
20
|
+
export const DEFAULT_RECEIPT = '⟪已整理⟫'
|
|
21
|
+
|
|
22
|
+
let DEBUG_FILE = ''
|
|
23
|
+
|
|
24
|
+
function dbg(line) {
|
|
25
|
+
if (!DEBUG_FILE) return
|
|
26
|
+
try {
|
|
27
|
+
// 保持只追加;单行写入,避免并发时交错。
|
|
28
|
+
fs.appendFileSync(DEBUG_FILE, `${new Date().toISOString()} [echo-guard] ${line}\n`, 'utf8')
|
|
29
|
+
} catch {
|
|
30
|
+
/* 日志写不进去不影响主流程 */
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isSubagent(agent) {
|
|
35
|
+
return Boolean(agent?.parentSessionId || agent?.parent)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** 该消息里第一段 reasoning 的全文(宿主把它映射成上游的 thinking)。 */
|
|
39
|
+
function reasoningOf(message) {
|
|
40
|
+
if (!Array.isArray(message?.content)) return null
|
|
41
|
+
for (const block of message.content) {
|
|
42
|
+
if (block && block.type === 'reasoning' && typeof block.text === 'string') return block
|
|
43
|
+
}
|
|
44
|
+
return null
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** 该消息里说出口的话(text 块),原样拼起来。 */
|
|
48
|
+
function speechOf(message) {
|
|
49
|
+
if (!Array.isArray(message?.content)) return ''
|
|
50
|
+
return message.content
|
|
51
|
+
.filter((b) => b && b.type === 'text' && typeof b.text === 'string')
|
|
52
|
+
.map((b) => b.text)
|
|
53
|
+
.join('')
|
|
54
|
+
.trim()
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function hasToolCall(message) {
|
|
58
|
+
return Array.isArray(message?.content) && message.content.some((b) => b?.type === 'tool-call')
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function apply(ctx, config = {}) {
|
|
62
|
+
if (typeof config.debugFile === 'string' && config.debugFile.length > 0) DEBUG_FILE = config.debugFile
|
|
63
|
+
if (config.enabled === false) return
|
|
64
|
+
|
|
65
|
+
const cfg = {
|
|
66
|
+
fillers: Array.isArray(config.fillers) && config.fillers.length ? config.fillers.map(String) : DEFAULT_FILLERS,
|
|
67
|
+
burstMinCount: Number.isSafeInteger(config.burstMinCount) ? config.burstMinCount : 5,
|
|
68
|
+
receipt: typeof config.receipt === 'string' ? config.receipt : DEFAULT_RECEIPT,
|
|
69
|
+
// 删完剩不到两成就放弃:整段都是收束词时删光只会剩个空块,不如留着。
|
|
70
|
+
stripMinKeepRatio: typeof config.stripMinKeepRatio === 'number' ? config.stripMinKeepRatio : 0.2,
|
|
71
|
+
// 只对这些会话生效(空 = 全部)。填会话 id 片段。
|
|
72
|
+
onlySessions: Array.isArray(config.onlySessions) ? config.onlySessions.map(String) : [],
|
|
73
|
+
includeSubagents: config.includeSubagents !== false,
|
|
74
|
+
dryRun: config.dryRun === true
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const state = new WeakMap() // session -> { seen: Set<seq> }
|
|
78
|
+
|
|
79
|
+
ctx.on('agent/pre-step', async ({ agent, signal }, next) => {
|
|
80
|
+
try {
|
|
81
|
+
const session = agent?.session
|
|
82
|
+
if (!session || signal?.aborted) return next()
|
|
83
|
+
if (!cfg.includeSubagents && isSubagent(agent)) return next()
|
|
84
|
+
|
|
85
|
+
const rawId = String(session.id ?? '')
|
|
86
|
+
const inList = cfg.onlySessions.length === 0 || cfg.onlySessions.some((p) => rawId.includes(p))
|
|
87
|
+
if (!inList) return next()
|
|
88
|
+
|
|
89
|
+
let s = state.get(session)
|
|
90
|
+
if (s === undefined) {
|
|
91
|
+
s = { seen: new Set() }
|
|
92
|
+
state.set(session, s)
|
|
93
|
+
dbg(`首次扫描:surface ${session.surface.nodes.length} 节点`)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
for (const seq of [...session.surface.nodes]) {
|
|
97
|
+
if (s.seen.has(seq)) continue
|
|
98
|
+
const event = session.eventAt(seq)
|
|
99
|
+
if (event === undefined || event.type !== 'assistant/message') {
|
|
100
|
+
s.seen.add(seq)
|
|
101
|
+
continue
|
|
102
|
+
}
|
|
103
|
+
if (event.surfaceOp !== 'append') {
|
|
104
|
+
s.seen.add(seq)
|
|
105
|
+
continue
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const message = session.deriveEventMessage(event)
|
|
109
|
+
if (message === null || !Array.isArray(message.content)) {
|
|
110
|
+
s.seen.add(seq)
|
|
111
|
+
continue
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// 带工具调用的步不碰:整步替换是 thinking-guard 的活,两个插件抢同一段会打架。
|
|
115
|
+
if (hasToolCall(message)) {
|
|
116
|
+
s.seen.add(seq)
|
|
117
|
+
continue
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const block = reasoningOf(message)
|
|
121
|
+
if (block === null) {
|
|
122
|
+
s.seen.add(seq)
|
|
123
|
+
continue
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const counted = countFillers(block.text, cfg.fillers)
|
|
127
|
+
if (counted < cfg.burstMinCount) {
|
|
128
|
+
s.seen.add(seq)
|
|
129
|
+
continue
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const stripped = stripFillers(block.text, cfg.fillers)
|
|
133
|
+
if (stripped.text !== '' && stripped.text.length < block.text.length * cfg.stripMinKeepRatio) {
|
|
134
|
+
s.seen.add(seq)
|
|
135
|
+
dbg(`跳过 seq ${seq}:删完只剩 ${stripped.text.length}/${block.text.length}`)
|
|
136
|
+
continue
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const speech = speechOf(message)
|
|
140
|
+
let body = cfg.receipt
|
|
141
|
+
if (stripped.text !== '') body = body ? `${body}\n\n${stripped.text}` : stripped.text
|
|
142
|
+
if (speech !== '') body = `${body}\n\n此处说出口的话(原文照录):\n${speech}`
|
|
143
|
+
|
|
144
|
+
if (cfg.dryRun) {
|
|
145
|
+
s.seen.add(seq)
|
|
146
|
+
dbg(`DRY-RUN 命中 seq ${seq}:${counted} 次,删 ${stripped.removed} 个,${block.text.length}→${stripped.text.length} 字`)
|
|
147
|
+
continue
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
try {
|
|
151
|
+
session.append('user/message', {
|
|
152
|
+
id: `echo-guard-${seq}`,
|
|
153
|
+
role: 'user',
|
|
154
|
+
source: { kind: 'plugin:echo-guard' },
|
|
155
|
+
content: [{ type: 'text', text: body }]
|
|
156
|
+
}, {
|
|
157
|
+
surfaceOp: { op: 'replace', startSeq: seq, endSeq: seq },
|
|
158
|
+
sourceEventSeqs: [seq]
|
|
159
|
+
})
|
|
160
|
+
} catch (error) {
|
|
161
|
+
// 落不下去就下次再说:不记 seen,避免把这一步永久漏掉。
|
|
162
|
+
dbg(`写入失败 seq ${seq}:${String(error)}`)
|
|
163
|
+
ctx.logger?.warn?.(`[${name}] 改写失败 seq ${seq}:${String(error)}`)
|
|
164
|
+
continue
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
s.seen.add(seq)
|
|
168
|
+
dbg(`已删字 seq ${seq}:${counted} 次,删 ${stripped.removed} 个,${block.text.length}→${stripped.text.length} 字`)
|
|
169
|
+
}
|
|
170
|
+
} catch (error) {
|
|
171
|
+
dbg(`扫描失败:${String(error)}`)
|
|
172
|
+
ctx.logger?.warn?.(`[${name}] 扫描失败(已忽略,不影响本步):${String(error)}`)
|
|
173
|
+
}
|
|
174
|
+
return next()
|
|
175
|
+
})
|
|
176
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-echo-guard",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Strips filler-word bursts out of the model-visible reasoning history — only standalone occurrences, everything else verbatim. DSH plugin.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/index.mjs",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./lib/index.mjs",
|
|
9
|
+
"./core": "./lib/core.mjs",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"lib",
|
|
14
|
+
"cordis.patch.yml",
|
|
15
|
+
"README.md",
|
|
16
|
+
"README.en.md",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "node --test test/core.test.mjs"
|
|
21
|
+
},
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=22.19.0"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"deepseek",
|
|
27
|
+
"deepseek-harness",
|
|
28
|
+
"dsh",
|
|
29
|
+
"dsh-plugin",
|
|
30
|
+
"reasoning",
|
|
31
|
+
"loop",
|
|
32
|
+
"degeneration",
|
|
33
|
+
"long-context"
|
|
34
|
+
],
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"dsh": {
|
|
37
|
+
"bundle": {
|
|
38
|
+
"patch": "./cordis.patch.yml"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/efef3/dsh-echo-guard.git"
|
|
44
|
+
},
|
|
45
|
+
"homepage": "https://github.com/efef3/dsh-echo-guard#readme",
|
|
46
|
+
"bugs": {
|
|
47
|
+
"url": "https://github.com/efef3/dsh-echo-guard/issues"
|
|
48
|
+
}
|
|
49
|
+
}
|