dsh-echo-guard 0.2.1 → 0.3.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.en.md CHANGED
@@ -94,9 +94,47 @@ Three boundaries: only the **most recent pure-reasoning step**; never a tool-cal
94
94
  Hence the threshold of 5, aimed at the "one per sentence" drift.
95
95
 
96
96
 
97
+ ## Authorship: replacements are recorded as "the user said it" (fixable since 0.3.0)
98
+
99
+ Stock DSH only lets a plugin write replacement messages as `user`. So a reasoning block with characters stripped turns into a **user turn** in the history — on the next request the model reads it as something *you* said, not as its own thinking.
100
+
101
+ This can't be worked around inside the plugin: both checks live in the host, and writing `assistant` is rejected outright (`assistant/message … lacks an identified message`, `does not match an open turn and step`).
102
+
103
+ ### With a host patch
104
+
105
+ Loosen those two checks and the plugin can write replacements as `assistant`: the characters are gone, the speaker is still the same. This repo ships `tools/patch-host.mjs`, which does a **same-length** binary edit — only the `if` conditions change, so the file size and every offset stay identical.
106
+
107
+ ```bash
108
+ # List only. Expect 11 sites.
109
+ node tools/patch-host.mjs "<install>\resources\app.asar"
110
+
111
+ # Write, once the count matches. It backs up to app.asar.orig-<timestamp> first.
112
+ node tools/patch-host.mjs "<install>\resources\app.asar" --write
113
+ ```
114
+
115
+ Two checks, 11 conditions in total:
116
+
117
+ | Check | Sites |
118
+ | --- | --- |
119
+ | `assistant/message embeds its source stream and cannot carry sourceEventSeqs` | 5 |
120
+ | `does not match an open turn and step` | 6 |
121
+
122
+ - If the count doesn't match, **don't write**: the DSH version changed and the `if` shapes may have too.
123
+ - Restart DSH, then set `replaceRole: "assistant"` in the config.
124
+ - **A DSH update restores the original app.asar** and the patch is gone. The symptom is an error when the plugin writes, with the history untouched — the session is not damaged, stripping just stops. Re-apply after upgrading.
125
+ - To roll back, copy `app.asar.orig-<timestamp>` back.
126
+
127
+ ```yaml
128
+ replaceRole: "assistant" # default "user"; only with the host patch
129
+ ```
130
+
131
+ ### Without the patch
132
+
133
+ The default is `replaceRole: "user"`: the plugin works anywhere, with the authorship caveat above. If that's a dealbreaker, don't install it — or use `/strip-last` manually and read the result yourself.
134
+
97
135
  ## Version and testing
98
136
 
99
- **Version 0.2.1.** (0.1.0 → 0.2.0: per-word thresholds; manual `/strip-last` and request-file entry. 0.2.0 → 0.2.1: `OK` and `let me go` added to the default filler list.)
137
+ **Version 0.3.0.** (0.1.0 → 0.2.0: per-word thresholds; manual `/strip-last` and request-file entry. 0.2.0 → 0.2.1: `OK` and `let me go` added to the default filler list. 0.2.1 → 0.3.0: configurable `replaceRole`; with the host patch, replacements can be written back as `assistant`, so authorship is preserved. Ships `tools/patch-host.mjs`.)
100
138
 
101
139
  Verified on 2026-12-05 with:
102
140
 
package/README.md CHANGED
@@ -112,9 +112,47 @@ manual:
112
112
  所以阈值定在 5,专抓「一句一个」的分散型;整段复读交给折叠和急停阀。
113
113
 
114
114
 
115
+ ## 署名:替换消息写成了“用户说的”(0.3.0 起可改)
116
+
117
+ 原版 DSH 只允许插件把替换消息写成 `user`。于是删过字的思考,在历史里变成一条**用户发言**——模型下一轮读上下文,会把这句当成你说的,而不是它自己说的。
118
+
119
+ 这不是插件能绕过去的:两道校验在宿主里,写 `assistant` 会被直接拒(`assistant/message … lacks an identified message`、`does not match an open turn and step`)。
120
+
121
+ ### 打了补丁之后
122
+
123
+ 宿主松开那两处校验,插件就能把替换消息写成 `assistant`:字删了,说话的人还是原来那个。仓库里带 `tools/patch-host.mjs`,做的是**同长度**二进制改写——只动 `if` 条件,文件大小和所有偏移都不变。
124
+
125
+ ```bash
126
+ # 先只列,别写。正常应该找到 11 处。
127
+ node tools/patch-host.mjs "C:\Program Files\DeepSeek Harness\resources\app.asar"
128
+
129
+ # 确认数目对得上再写。写之前自动备份成 app.asar.orig-<时间戳>。
130
+ node tools/patch-host.mjs "C:\Program Files\DeepSeek Harness\resources\app.asar" --write
131
+ ```
132
+
133
+ 要放开的就两句话,一共 11 个条件:
134
+
135
+ | 校验 | 处数 |
136
+ | --- | --- |
137
+ | `assistant/message embeds its source stream and cannot carry sourceEventSeqs` | 5 |
138
+ | `does not match an open turn and step` | 6 |
139
+
140
+ - 找到的数目对不上就**别写**:说明 DSH 版本变了,`if` 的形状可能也变了,照错的位置改会把宿主改坏。
141
+ - 改完重启 DSH,再把配置里的 `replaceRole` 改成 `"assistant"`。
142
+ - **升级 DSH 会把 app.asar 换回原版**,补丁就没了。表现是插件写 assistant 时报错、历史一个字不动——不会把会话弄坏,只是删字停摆。升级完重打一次。
143
+ - 要退:把 `app.asar.orig-<时间戳>` 复制回去。
144
+
145
+ ```yaml
146
+ replaceRole: "assistant" # 默认 "user";打了宿主补丁才用这个
147
+ ```
148
+
149
+ ### 不打补丁的时候
150
+
151
+ 默认 `replaceRole: "user"`,在别的机器上照常能用,代价就是上面说的署名问题。接受不了就别装;或者只用 `/strip-last` 手动清,清完自己看一眼。
152
+
115
153
  ## 版本与验证
116
154
 
117
- **版本 0.2.1。**(0.1.0 → 0.2.0:每个词各算各的门槛;新增手动清上一轮 `/strip-last` 和请求文件入口。0.2.0 → 0.2.1:默认词表加上 `OK`、`let me go`。)
155
+ **版本 0.3.0。**(0.1.0 → 0.2.0:每个词各算各的门槛;新增手动清上一轮 `/strip-last` 和请求文件入口。0.2.0 → 0.2.1:默认词表加上 `OK`、`let me go`。0.2.1 → 0.3.0:`replaceRole` 可配,配合宿主补丁能把替换消息写回 `assistant`,署名不再变;附 `tools/patch-host.mjs`。)
118
156
 
119
157
  在以下环境实测通过(2026-12-05):
120
158
 
package/cordis.patch.yml CHANGED
@@ -40,6 +40,11 @@
40
40
  # 先观察:true 时只写日志、不改历史。建议先跑一天。
41
41
  dryRun: true
42
42
 
43
+ # 替换消息写成谁说的(0.3.0):
44
+ # "user" 默认。原版 DSH 只收这个;副作用是模型会把删过的思考当成用户说的。
45
+ # "assistant" 要宿主补丁(tools/patch-host.mjs);字删了,署名还是本人。
46
+ replaceRole: "user"
47
+
43
48
  # 手动「清上一轮」(0.2.0):斜杠命令 /strip-last 一直挂着;
44
49
  # 要给别的进程(比如手机上的门)用,就填一个请求文件的绝对路径。
45
50
  manual:
package/lib/index.mjs CHANGED
@@ -58,6 +58,44 @@ function hasToolCall(message) {
58
58
  return Array.isArray(message?.content) && message.content.some((b) => b?.type === 'tool-call')
59
59
  }
60
60
 
61
+ /**
62
+ * 写一条替换消息。
63
+ *
64
+ * 默认写 user(原版 DSH 只收这个)。宿主打了补丁、配置 replaceRole: "assistant"
65
+ * 之后写 assistant——字删了,说话的人还是原来那个,模型不会把删过的话当成用户说的。
66
+ * 写 assistant 要带齐 turn/step/stream/usage 和 message.id,宿主只做形状校验。
67
+ */
68
+ function appendReplacement(session, { id, body, seq, event, role }) {
69
+ if (role === 'assistant' && event?.type === 'assistant/message') {
70
+ const data = event.data ?? {}
71
+ session.append('assistant/message', {
72
+ turn: Number.isSafeInteger(data.turn) ? data.turn : 0,
73
+ step: Number.isSafeInteger(data.step) ? data.step : 1,
74
+ message: {
75
+ id,
76
+ role: 'assistant',
77
+ source: { kind: 'model', provider: 'unknown', model: 'unknown' },
78
+ content: [{ type: 'text', text: body }]
79
+ },
80
+ usage: { inputTokens: 0, cacheReadTokens: 0, outputTokens: 0 },
81
+ stream: []
82
+ }, {
83
+ surfaceOp: { op: 'replace', startSeq: seq, endSeq: seq },
84
+ sourceEventSeqs: [seq]
85
+ })
86
+ return
87
+ }
88
+ session.append('user/message', {
89
+ id,
90
+ role: 'user',
91
+ source: { kind: 'plugin:echo-guard' },
92
+ content: [{ type: 'text', text: body }]
93
+ }, {
94
+ surfaceOp: { op: 'replace', startSeq: seq, endSeq: seq },
95
+ sourceEventSeqs: [seq]
96
+ })
97
+ }
98
+
61
99
  /** 会话 id -> 最近的 agent 句柄。手动入口要用它:命令和门都只能拿到 id。 */
62
100
  const LIVE_AGENTS = new Map()
63
101
 
@@ -119,14 +157,12 @@ export function stripLastTurn(agent, cfg) {
119
157
  let body = cfg.receipt
120
158
  if (stripped.text !== '') body = body ? `${body}\n\n${stripped.text}` : stripped.text
121
159
  if (speech !== '') body = `${body}\n\n此处说出口的话(原文照录):\n${speech}`
122
- session.append('user/message', {
160
+ appendReplacement(session, {
123
161
  id: `echo-guard-manual-${seq}-${Date.now()}`,
124
- role: 'user',
125
- source: { kind: 'plugin:echo-guard' },
126
- content: [{ type: 'text', text: body }]
127
- }, {
128
- surfaceOp: { op: 'replace', startSeq: seq, endSeq: seq },
129
- sourceEventSeqs: [seq]
162
+ body,
163
+ seq,
164
+ event,
165
+ role: cfg.replaceRole
130
166
  })
131
167
  return { ok: true, seq, deleted: stripped.removed, counted: stripped.counted }
132
168
  }
@@ -157,6 +193,9 @@ export function apply(ctx, config = {}) {
157
193
  onlySessions: Array.isArray(config.onlySessions) ? config.onlySessions.map(String) : [],
158
194
  includeSubagents: config.includeSubagents !== false,
159
195
  dryRun: config.dryRun === true,
196
+ // 替换消息写成谁说的。默认 user(原版 DSH 只收这个,副作用是模型会把
197
+ // 删过的思考当成用户说的);宿主打过补丁后设 "assistant",就留在本人名下。
198
+ replaceRole: config.replaceRole === 'assistant' ? 'assistant' : 'user',
160
199
  // 手动「清上一轮」(0.2.0 起):命令 + 请求文件。
161
200
  // requestFile 留空 = 不监听请求文件,只留斜杠命令。
162
201
  manual: {
@@ -244,14 +283,12 @@ export function apply(ctx, config = {}) {
244
283
  }
245
284
 
246
285
  try {
247
- session.append('user/message', {
286
+ appendReplacement(session, {
248
287
  id: `echo-guard-${seq}`,
249
- role: 'user',
250
- source: { kind: 'plugin:echo-guard' },
251
- content: [{ type: 'text', text: body }]
252
- }, {
253
- surfaceOp: { op: 'replace', startSeq: seq, endSeq: seq },
254
- sourceEventSeqs: [seq]
288
+ body,
289
+ seq,
290
+ event,
291
+ role: cfg.replaceRole
255
292
  })
256
293
  } catch (error) {
257
294
  // 落不下去就下次再说:不记 seen,避免把这一步永久漏掉。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-echo-guard",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Strips filler-word bursts out of the model-visible reasoning history — only standalone occurrences, everything else verbatim. DSH plugin.",
5
5
  "type": "module",
6
6
  "main": "lib/index.mjs",
@@ -11,6 +11,7 @@
11
11
  },
12
12
  "files": [
13
13
  "lib",
14
+ "tools",
14
15
  "cordis.patch.yml",
15
16
  "README.md",
16
17
  "README.en.md",
@@ -0,0 +1,112 @@
1
+ // 让 dsh-echo-guard 的替换消息能保持 assistant 署名。
2
+ // 做法:同长度改写宿主 app.asar 里两处 if 条件,改成 false/*…*/。
3
+ // 用法:
4
+ // node tools/patch-host.mjs "C:\...\resources\app.asar" 只列,不写
5
+ // node tools/patch-host.mjs "C:\...\resources\app.asar" --write 备份后改写
6
+ //
7
+ // 注意:这是二进制的“同长度”改写,文件和包里所有偏移都不变。
8
+ // 升级 DSH 会把 app.asar 换回原版,补丁就没了,要重打。
9
+
10
+ import fs from 'node:fs'
11
+ import path from 'node:path'
12
+
13
+ const NEEDLES = [
14
+ 'assistant/message embeds its source stream and cannot carry sourceEventSeqs',
15
+ 'does not match an open turn and step',
16
+ ]
17
+
18
+ const file = process.argv[2]
19
+ const write = process.argv.includes('--write')
20
+ if (!file) {
21
+ console.error('用法:node tools/patch-host.mjs <app.asar> [--write]')
22
+ process.exit(1)
23
+ }
24
+ if (!fs.existsSync(file)) {
25
+ console.error(`找不到文件:${file}`)
26
+ process.exit(1)
27
+ }
28
+
29
+ const buf = fs.readFileSync(file)
30
+ const originalLen = buf.length
31
+
32
+ // 从 open 处的 '(' 找到配对的 ')',跳过字符串里的括号
33
+ function matchParen(src, open) {
34
+ let depth = 0
35
+ let quote = null
36
+ for (let i = open; i < src.length; i++) {
37
+ const c = src[i]
38
+ if (quote) {
39
+ if (c === 0x5c) { i++; continue } // 反斜杠转义
40
+ if (c === quote) quote = null
41
+ continue
42
+ }
43
+ if (c === 0x22 || c === 0x27 || c === 0x60) { quote = c; continue }
44
+ if (c === 0x28) depth++
45
+ else if (c === 0x29) {
46
+ depth--
47
+ if (depth === 0) return i
48
+ }
49
+ }
50
+ return -1
51
+ }
52
+
53
+ const edits = []
54
+ for (const needle of NEEDLES) {
55
+ const nb = Buffer.from(needle, 'ascii')
56
+ let at = 0
57
+ while (true) {
58
+ const idx = buf.indexOf(nb, at)
59
+ if (idx < 0) break
60
+ at = idx + nb.length
61
+ // 往回找最近的 'if ('
62
+ let ifIdx = -1
63
+ for (const pat of ['if (', 'if(']) {
64
+ const p = buf.lastIndexOf(Buffer.from(pat, 'ascii'), idx)
65
+ if (p > ifIdx) ifIdx = p
66
+ }
67
+ if (ifIdx < 0) { console.log(`跳过:${needle.slice(0, 40)}… 附近没有 if(偏移 ${idx})`); continue }
68
+ const open = buf.indexOf(0x28, ifIdx)
69
+ const close = matchParen(buf, open)
70
+ if (close < 0 || close > idx) { console.log(`跳过:偏移 ${idx} 的 if 括号没配对上`); continue }
71
+ const cond = buf.subarray(open + 1, close).toString('ascii')
72
+ if (cond.startsWith('false/*')) { console.log(`已打过:偏移 ${idx}`); continue }
73
+ const pad = cond.length - 9
74
+ if (pad < 1) { console.log(`跳过:偏移 ${idx} 条件太短(${cond.length} 字节)`); continue }
75
+ const repl = `false/*${' '.repeat(pad)}*/`
76
+ edits.push({ offset: open + 1, length: cond.length, repl, cond, idx, needle })
77
+ }
78
+ }
79
+
80
+ console.log(`文件 ${file}`)
81
+ console.log(`大小 ${originalLen} 字节;找到 ${edits.length} 处待改`)
82
+ for (const e of edits) {
83
+ console.log(` @${e.offset} ${e.cond.slice(0, 70)}${e.cond.length > 70 ? '…' : ''}`)
84
+ }
85
+ if (!edits.length) { console.log('没有要改的——原版可能已经不是这个样子了,方法得重看。'); process.exit(0) }
86
+ if (!write) { console.log('\n只列不改。确认没问题再加 --write。'); process.exit(0) }
87
+
88
+ const bak = `${file}.orig-${Date.now()}`
89
+ fs.copyFileSync(file, bak)
90
+ for (const e of edits) {
91
+ const b = Buffer.from(e.repl, 'ascii')
92
+ if (b.length !== e.length) throw new Error('长度不一致,放弃')
93
+ b.copy(buf, e.offset)
94
+ }
95
+ fs.writeFileSync(file, buf)
96
+
97
+ // 自检:文件大小没变;两句话再也搜不到(都被藏在改过的 if 里不需要了?不,字符串还在,搜的是条件是否已 false)
98
+ const check = fs.readFileSync(file)
99
+ if (check.length !== originalLen) { console.error('自检失败:文件大小变了'); process.exit(2) }
100
+ let left = 0
101
+ for (const needle of NEEDLES) {
102
+ let at2 = 0
103
+ const nb = Buffer.from(needle, 'ascii')
104
+ while (true) {
105
+ const idx = check.indexOf(nb, at2)
106
+ if (idx < 0) break
107
+ at2 = idx + nb.length
108
+ left++
109
+ }
110
+ }
111
+ console.log(`已写回,大小不变(${check.length} 字节)。原文里这句话还有 ${left} 处字符串(正常,字符串没删,改的是条件)。`)
112
+ console.log(`备份:${path.relative(process.cwd(), bak)}`)