dsh-instruction-memory 1.6.1 → 1.6.3

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/CHANGELOG.md CHANGED
@@ -7,6 +7,165 @@
7
7
  > 1.0.13 起由 CI 经 OIDC Trusted Publishing 发布(首个带 provenance 的版本)。
8
8
  > 这些历史 tag **不应回填**——npm 上的版本号已被占用。
9
9
 
10
+ ## [1.6.3] — 2026-11-20
11
+
12
+ 第三轮审计(技术方案评审团:架构 / 应用安全 / 验收三条线)的修复批次。**一条阻断级缺陷**:
13
+ v1.6.0 的头号功能「触发词 → 本轮强制」在真实宿主上是死代码,桌面端与网页端同时失效;
14
+ `npm test` 全绿(1065 PASS)与机制不可用并存,因为整个套件里没有任何一条用例用过宿主真实
15
+ 发送的消息形状。
16
+
17
+ ### 修复(阻断)
18
+
19
+ - **触发词取不到文本,功能从未触发**。`lib/index.js` 的 `user/message` 处理器读
20
+ `event.data.text`,而宿主交给插件的是**消息对象本身**——`{ id, role, content:
21
+ ContentBlock[], source }`(`dsh-llm` 的 `MessageBase`;`dsh-session` 里
22
+ `event.type === 'user/message' ? data : data.message`)。`text` 因此在真机上恒为空,
23
+ 处理器在 `text === ''` 处直接 return,只有手工构造 `{ text }` 的夹具能触发。现在按
24
+ `content` 的 text 块拼接(保留两种字符串回退供夹具与旧宿主使用)。
25
+ - **提升赶不上本轮第一个请求**。宿主 `preStep()` 先 `systemPrompt.assemble()`、
26
+ `step()` 里才 `append('user/message')`,而 section 文本在 assemble 时就已求值。所以即使
27
+ 上面那条修好,无工具调用的单步回合也拿不到提升。**文案已按事实更正**:标注与注释统一为
28
+ 「从命中之后的下一次注入开始生效,不追溯已发出的请求,到本回合结束为止」,不再宣称「当轮
29
+ 立刻生效」。
30
+
31
+ ### 修复(安全)
32
+
33
+ - **无 `connection` 服务的 profile 默认停用写操作(fail-closed)**。v1.6.2 的写门判据是
34
+ 「请求像不像浏览器」(`Origin` 或 `Sec-Fetch-Site: same-origin`),而这两个头都由调用方
35
+ 自带——注释自己写着"只加一行 `Sec-Fetch-Site: same-origin` 即从 403 变 200"。这条路径能
36
+ 改写每轮注入的指令,等于把持久化提示注入放在一个自证头之后。现在没有宿主 `connection`
37
+ 服务时,所有修改方法一律 503(读操作照常,设置页可打开查看);单用户设备可显式
38
+ `DSH_INSTRUCTION_MEMORY_ALLOW_UNGUARDED_WRITES=1` 恢复旧的宽松判据(重启生效)。
39
+ 设置页会读取快照里的 `security.writeGate`(`fence` / `opt-in` / `refused`)**在用户动手
40
+ 之前**就把这件事说出来。
41
+ - **`reload` 真正只读**。它列在只读集合里,但 `readFromDisk()` 的 `.bak` 提升分支会
42
+ `writeToDisk()`——一个被归为"读"的方法能落盘。现在 `reload` 传 `write: false`:备份被载入
43
+ 内存、注入照常,但写回留给下一次真实保存,且 `saved` 恒为 `false`(字段含义保持"本次操作
44
+ 是否持久化了存储")。
45
+ - **导入 / 整体恢复 / 迁移旧文件的确认从客户端移到宿主**。v1.6.1 只加了面板对话框,任何能
46
+ 通过写门的调用方都可以不经确认直接 POST 同一份 payload,而导入的条目会保留 `triggers`
47
+ 与 `when`——配合 1.6.0 的语义,一行文本就能变成"每轮必执行"。现在
48
+ `import-data` / `restore-data` / `adopt-legacy` 要求 `args.confirm === true`,面板在用户
49
+ 同意后发送该字段;确认文案也补上了"会保留触发词"这一句。
50
+ - **`本轮强制` 纳入防伪造白名单**。`FORGED_ENTRY_PREFIX` 只列了「始终 / 条件必做 / 按需」,
51
+ 而 1.6.0 开始输出「本轮强制」——条目正文里手写的同名行可以原样通过,读起来与本插件自己的
52
+ 结构行完全一样。标签列表改为 `ENTRY_TAGS` 一处定义,"打印的标签 ⊆ 中立化的标签"这条不变式
53
+ 由测试逐项枚举。
54
+ - **临时文件改为独占创建(`wx`)**。`.bak` 提升或重试路径会用 `open(tmpPath, 'w')` 在原路径
55
+ 重建临时文件;同一路径若被放置符号链接,会写到链接目标。`ensureSchemaFile` 同型。
56
+ - **请求体并发上限**。单个请求体有上限,但同时在途的数量没有,同用户进程可以并行打开多个大包
57
+ 压宿主堆。现在最多 8 个在途(`MAX_BODY_IN_FLIGHT`),按"仍在累积字节"计时,body 读完即
58
+ 释放,不会让慢写饿死面板自己的 `state` 轮询。
59
+
60
+ ### 修复(正确性 / 可观测)
61
+
62
+ - **`FORCED_IDS` 不再是模块级全局**。它被同进程内所有会话共享,A 会话命中触发词会把该条在
63
+ B 会话里也提升为「本轮强制」(实测:同进程两个实例,驱动 A 的监听器,A 与 B 同时变红);
64
+ 且全文没有任何回合结束清理,提升会一直残留。现在按会话键存 `Map`,`turn/end` /
65
+ `turn/abort` 清理;section 渲染时从装配上下文取会话键,取不到才回退到"最近一次",因此单
66
+ 会话宿主行为不变。上下文明确给出会话时,无记录即"该会话没有提升",绝不借用别人的。
67
+ - **提升只作用于激活集合**。匹配与键都改用 `setId\0id`(库内其它地方一致的去重键),排序前先
68
+ 过 `activeScope`/`inScope`:非激活集合里的触发词不再能改写激活集合的同 id 条目(F13)。
69
+ - **`refreshSection` 不再 dispose + 重注册**。section 文本本来就是活 thunk,每次装配都会被
70
+ 重新求值,重注册既多余又危险:它发生在 `session/event` 处理器里(与当轮装配并发),且
71
+ dispose 抛异常时句柄已被置空,`syncSection()` 会再注册一份而旧的仍然活着——同一 prompt 里
72
+ 两份记忆块。现在只做 `syncSection()`(处理呈现/移除边沿)与快照推送。
73
+ - **`injection.forced`**:快照发布当前被提升的条目 id,面板不再"标签变了但界面什么都不说"。
74
+ - **`maxTriggers` / `maxTriggerLength` 进入 `limits`**:面板此前硬编码 `slice(0, 8)` 并复用
75
+ 标签上限,8/24 与标签上限相等只是数值巧合。schema 与代码的两侧漂移也进了 `verify.mjs` 的
76
+ 比对清单。
77
+ - **客户端能区分桌面壳**(`location.protocol === 'dsh-app:'`)。桌面导出此前没有分支,全部落
78
+ 通用 `blob:` 下载,而宿主的下载处理只认自己标记的链接——现在桌面导出会同时给出"完整 JSON"
79
+ 这条保底通道,不再声称文件一定落地。
80
+
81
+ ### 测试
82
+
83
+ - `contract-test.mjs`:新增真实 `user/message` 形状用例组(真实形状提升、另一会话不继承、
84
+ `turn/end` 回落、非人类来源不提升、扁平 `{ text }` 兼容、同一条目只出现一次提升标签);
85
+ 新增 `desktop + fence` 形状(无 `Origin`、无 `Sec-Fetch-Site`、注入 cookie、门放行 → 写
86
+ 必须 200)——此前只测了"桌面 + 无 connection"这种出厂 profile 不存在的组合;
87
+ 新增 fence-less 默认 fail-closed 的六条断言(写 503、伪造单头无效、读仍 200);
88
+ `READ_ONLY_METHODS` 的源码钉改为 `METHOD_KINDS` 表 + 与 dispatch switch 的**双向**差集断言。
89
+ - `smoke-test.mjs`:四种标签(含「本轮强制」)逐项枚举中立化;`planInjection` 的 forced 参数
90
+ 与跨集合键的用例。
91
+ - `section-order-test.mjs`:候选集加入**未打包安装**(本机 `app.asar` 已改名,真正在跑的是
92
+ `resources/app/dsh` 的 0.2.2-rc.5);无宿主时打印 `SKIPPED` 而不是 `ALL PASS`,CI 不再把
93
+ "什么都没断言"计成绿。
94
+ - `verify.mjs`:`triggers.maxItems` / `trigger.maxLength` 进入 schema ↔ 代码漂移比对。
95
+
96
+ ### 已知边界
97
+
98
+ - 「本轮强制」的生效时机是**下一次注入**(原:当轮)。若要做到真正的同回合,需要把提升改由
99
+ runtime-context provider 承载,并在 `agent/pre-step` waterfall 里用本轮消息计算——那是宿主
100
+ 侧装配顺序的改动,需要单独验证,本版只把语义写诚实。
101
+ - 插件仍不注册任何面向模型的工具,模型没有"被注入后自己调用工具持久化"的写入面。
102
+ - `reload`、`state`、`read-source` 仍会物化一份**本插件自己的默认空 store**(首次运行、设置页
103
+ 打开时),但物化内容从不是调用方给的。
104
+
105
+ ## [1.6.2] — 2026-11-19
106
+
107
+ 第二轮增量审计(桌面端 runtime 已升到 0.2.2-rc.5)的修复批次,主体是**修 1.6.1 自己引入的一个
108
+ 回归**,以及让测试真正覆盖"桌面壳转发"这种请求形状。
109
+
110
+ ### 修复(1.6.1 的回归)
111
+
112
+ - **`reload` 放回 `READ_ONLY_METHODS`**。1.6.1 把 `export-to-dir` / `send-feedback` 移出只读集合时
113
+ 连 `reload` 一起删掉了,而同一提交的注释、CHANGELOG、README、提交信息**四处**都写着"reload 有意
114
+ 保留"。触发条件是「桌面壳转发(必然剥掉 `Origin` 与 `Sec-Fetch-Site`)+ profile 没有
115
+ `connection` 服务」,此时面板「重新载入」会永久 403。本机 3080 有 `connection` 门,所以线上看不
116
+ 见——这正是它需要一条**不带 origin** 的用例才抓得住的原因。
117
+
118
+ ### 修复(说明与事实不符)
119
+
120
+ - **写门的注释与拒绝文案**。原文称 `Sec-Fetch-Site` "script cannot set it",并告诉用户
121
+ "本机脚本 / curl 的写入会被拒绝"。两者都错:`Origin` 与 `Sec-Fetch-Site` 是普通请求头,本机任何
122
+ 进程都能自带一个(实测加一行 `Sec-Fetch-Site: same-origin` 即把 403 变 200)。现在注释与文案都
123
+ 按"只挡浏览器跨站,不是身份校验"表述,并指向 `REQUIRE_AUTH_FENCE=1` 或宿主的 `connection`。
124
+ - **只读集合的注释**。"它们只读内存、不写任何东西"对 `state` / `read-source` 不准确——两者在首次
125
+ 运行时会物化一份默认空 store。改为"物化的内容是本插件自己的默认值,从不是调用方给的"。
126
+
127
+ ### 修复(测试)
128
+
129
+ - **`section-order-test.mjs` 的 asar 分支此前是死代码**。asar 目录里文件的 `offset` 是**字符串**,
130
+ 而该测试要求 `typeof offset === 'number'`,于是每次读 `app.asar` 都抛错并静默回退到 profile 树。
131
+ 这台机器上 `profiles/node_modules/@deepseek-ai/dsh-system-prompt` 是 TRAE 打包的
132
+ **0.2.2-alpha.2** junction,而真正在跑的是 asar 里的 **0.2.2-rc.5**,于是测试打印了七条绿行、
133
+ 断言的是没人使用的那份宿主。现在:`offset` 两种写法都接受;桌面安装优先;**每个存在的副本都
134
+ 单独断言**,并在每行标出种类与版本(如 `[asar v0.2.2-rc.5]`、`[profile v0.2.2-alpha.2]`)。
135
+ - **`contract-test.mjs` 增加「desktop-forward」形状用例组**:不带 `Origin`、不带
136
+ `Sec-Fetch-Site` 的请求,`reload` / `state` / `history-list` 必须 200,`save-entry` /
137
+ `send-feedback` / `export-to-dir` 必须 403;并把 `READ_ONLY_METHODS` 的成员从源码钉住
138
+ (多一个或少一个都要红)。此前全部用例的默认头都带 `origin`,这个形状从未被测过。
139
+ - **`ui-test.mjs` 覆盖导入确认的两条分支**:取消 → 一个 rev 都不变;无对话框 → 拒绝合并并给出
140
+ 可读提示。1.6.1 加了这个确认却没有任何测试。
141
+ - **`section-order-test.mjs` 兼容 CRLF 检出**。CI 在 Windows 上用 `core.autocrlf` 检出,同一份
142
+ `lib/index.js` 到那边是 CRLF;按 `\n` 锚定的正则本地全绿、只在 Windows runner 上失败。读取后
143
+ 先归一化行尾。
144
+
145
+ ### 修复(存储)
146
+
147
+ - **临时文件在 rename 前消失时重写一次**。CI(Node 22)上出现过一次
148
+ `保存失败:ENOENT … rename 'memory.json.tmp-…' -> 'memory.json'`:临时文件在 `close()` 与
149
+ `rename()` 之间被本进程之外的清理者删掉了(安全扫描器、索引器、runner 清理 `/tmp`),
150
+ 于是用户看到"保存失败",而存储其实完全健康。现在遇到该 ENOENT 会重写一次临时文件再 rename;
151
+ 目标目录不存在(ENOENT 的另一种含义)仍立即失败并保留原错误信息。
152
+
153
+ ### 兼容性
154
+
155
+ - 存储格式不变(`version: 2`)。宿主契约复核:rc.3 → 0.2.2-rc.5 的 10 个依赖点零漂移
156
+ (`SECTION_ORDERS` 900/1000 未动、950 仍空;`forwardWebRequest` 仍删 4 个头并注入 cookie;
157
+ `will-download` 仍只认 `#dsh-download=`)。
158
+ - 装配:`npm test` 现为 8 个脚本(新增 `section-order-test.mjs` 已在 1.6.1 加入);本版在
159
+ Node 24 与 Node 22 上各自全绿。
160
+
161
+ ### 未做
162
+
163
+ - 真实宿主点击级验证(面板渲染、SSE 重连、导出取消分支)仍未执行:GUI 需 launch token,本轮无
164
+ 凭据。`host-smoke` 作业仍是人工项。
165
+ - 跨进程并发写依旧是"锁超时后继续写"的活性优先策略(未改)。
166
+ - `resources\app.reapply-failed-20261119\` 是桌面壳 reapply 失败留下的整份 rc.5 副本,属环境残留,
167
+ 未清理(会让后续取证读错树,建议 owner 删除)。
168
+
10
169
  ## [1.6.1] — 2026-11-18
11
170
 
12
171
  一次针对"桌面端 / 网页端两个宿主"的缺陷审计(架构、授权安全、验收边界三方独立复查 + 代码级复核)
package/README.md CHANGED
@@ -74,7 +74,13 @@ profile 的解析顺序是 **`$DSH_PROFILE_DIR` → `--profile <名字>` → `$D
74
74
  ## 生效方式
75
75
 
76
76
  - **始终** 条目:无条件遵守。
77
- - **按需** 条目:由模型按相关性自行判断是否适用,不相关时忽略;可填「适用场景」辅助判断。
77
+ - **条件必做** 条目(旧称「按需」):在「适用情形」出现时必须执行,跳过须在回答中说明原因。
78
+ 它不再是"由模型自行决定是否相关"——1.6.0 起这个退出语义已经取消。
79
+ - **本轮强制**:条件必做条目可以填**触发词**(最多 8 个、单个不超过 24 字符)。本回合用户消息里
80
+ 出现任一触发词时,该条目在**下一次注入**中带上 `[本轮强制]` 前缀,效力等同条件必做、优先级
81
+ 更高;之后一轮若没有再命中就退回条件必做,回合结束也会清除。
82
+ **生效时机是"下一次注入",不是"当轮"**:宿主先装配系统提示词、再追加用户消息,所以命中发生在
83
+ 本轮提示词定稿之后,已发出的请求不会被追溯改写(无工具调用的单步回合因此要到下一次请求才看到)。
78
84
  - **优先级** 高 > 普通 > 低,决定注入顺序。
79
85
  - **注入字数上限**:超出时按优先级保留,被略过的条目数会在注入文本末尾注明。
80
86
  上限单位是**字符**而非 token——按 DeepSeek 官方分词比例约 1 个中文字符 ≈ 0.6 token、
@@ -87,8 +93,9 @@ profile 的解析顺序是 **`$DSH_PROFILE_DIR` → `--profile <名字>` → `$D
87
93
  存储格式不变,旧宿主/旧客户端对新字段一律忽略,互不影响。
88
94
  - **标签、筛选与排序**:每条指令可加若干**标签**(回车或逗号添加,最多 8 个、单个不超过 24 字符)。
89
95
  标签只用于设置页分组与筛选,**不会进入注入文本**;标签缺失的旧记忆照常加载(等同无标签)。
90
- 列表可按标签、生效方式(始终/按需)、启用状态组合筛选,并在「按优先级 / 按更新时间 / 按标题」
91
- 之间切换排序;列表排序纯为查看方便,不改变宿主侧的注入顺序。
96
+ 列表可按标签、生效方式(始终/条件必做)、启用状态组合筛选,并在「按优先级 / 按更新时间 / 按标题」
97
+ 之间切换排序;列表排序纯为查看方便,不改变宿主侧的注入顺序。被触发词提升的条目,列表会依据快照里
98
+ 的 `injection.forced` 标出「本轮强制」。
92
99
  - **批量操作**:点「批量操作」进入多选,可对勾选条目一次性启用、停用、设置优先级、加标签或删除;
93
100
  多选与上方的筛选/搜索叠加(「全选」只作用于当前可见条目)。整批修改在宿主侧是**单次写入**
94
101
  (只产生一次落盘、一次修订号递增与一次同步推送),全部条目已是目标状态时不写盘;批量删除需确认。
@@ -414,6 +421,11 @@ DSH 就会得到第二份空记忆,看起来像"记忆消失"。相对的 `$DS
414
421
  选择本插件导出的文件,确认后**整体替换**当前记忆。同样是"可反悔"的——替换前的内容已经在
415
422
  `.bak` 与版本历史里。
416
423
 
424
+ > **改版后回退到旧版本之前,先「导出」一份 JSON。** 1.6.0 给条目加了 `triggers` 字段,
425
+ > 而 1.5.7 及更早的 `sanitizeEntry` 不认识它——白名单重建会把该字段**丢掉**(读取本身不报错,
426
+ > 所以丢得没有声音)。存储格式版本没有变,因此文件能打开,只是触发词在回退后就没了。导出的
427
+ > JSON 可原样再导入回来。
428
+
417
429
  ## 手改与 JSON Schema(F15)
418
430
 
419
431
  随包发布一份 `memory.schema.json`(JSON Schema draft-07),描述版本 1/2 两种文件形状与全部
@@ -477,19 +489,21 @@ DSH 就会得到第二份空记忆,看起来像"记忆消失"。相对的 `$DS
477
489
  **不要同步存储目录本身**(`memory.json`)——两台机器同时写入会互相覆盖,插件那套
478
490
  rev 冲突检测与写锁都只在同一台机器内有效。换机器时用「导出 → 另一台导入/恢复」。
479
491
 
480
- ## 按需指令的本地预演(F17)
492
+ ## 条件必做条目的本地预演(F17)
481
493
 
482
- 「按需」条目到底会不会被采纳,本质上是模型的判断,插件看不到也猜不准。设置页提供一个
483
- **本地预演**:输入一句当前任务描述,前端**纯字面关键词**匹配各条按需指令的
494
+ 「条件必做」条目(旧称「按需」)到底会不会被采纳,本质上是模型的判断,插件看不到也猜不准。
495
+ 设置页提供一个**本地预演**:输入一句当前任务描述,前端**纯字面关键词**匹配各条条目的
484
496
  「适用场景 / 标题 / 内容」(场景权重最高、正文最低),列出字面重合度较高的条目并给出
485
497
  高/中/低分档,同时在列表里给这些条目打上「预演·高/中/低」标记。
486
498
 
487
- 它的用途只有一个:**帮你把「适用场景」写清楚、预判取舍**(例如"没写场景的条目只能靠正文
499
+ 它的用途只有一个:**帮你把「适用情形」写清楚、预判取舍**(例如"没写场景的条目只能靠正文
488
500
  碰运气"、"这条优先级低、一旦超出字数上限会先被丢下")。因此:
489
501
 
490
502
  - 措辞是克制的:界面上明确写着**「本地预演 ≠ 模型的真实判断」**,它不参与注入,也不代表模型
491
503
  实际会不会采纳;`verify.mjs` 断言了这句免责声明在两种语言里都存在。
492
- - 只有「按需」条目参与;「始终」条目无条件生效,与预演无关。
504
+ - 预演只做启发式打分,与**触发词提升是两条独立机制**:触发词是确定性的字面命中(由宿主判定),
505
+ 预演只是给你看的参考。1.6.0 起"不相关就可以忽略"的退出语义已取消,未命中触发词的条件必做
506
+ 条目仍然是"适用情形出现时必须执行"。
493
507
  - 匹配是启发式的:中文按 2/3 字滑窗切词(无分词依赖),英文/数字按词切分,术语上限 40 个,
494
508
  纯前端计算、不发任何请求。
495
509
 
@@ -729,21 +743,39 @@ DNS 重绑定恰好推翻了它**:`evil.com` 解析到 `127.0.0.1` 之后,
729
743
  即使某些技巧(如 `no-cors` fetch)不带 `Origin`,`evil.com` 发来的请求仍会被标记为
730
744
  `cross-site`——命中即 **403**。在回环绑定(受保护)时 **`cross-site` 与 `same-site` 都拒绝**:
731
745
  "同站"判定不看端口,而回环上的任何其他端口都是别人跑的本地服务,放行 `same-site` 等于让任意
732
- 本地页面改写你的长期指令。`none`(用户直接导航)放行;curl、Node 与 2020 年前的老浏览器根本
733
- 不发这个头,也不受影响(它们由 Host 与 content-type 校验兜底)。
746
+ 本地页面改写你的长期指令。`none`(用户直接导航)放行。
747
+
748
+ curl、Node 与 2020 年前的老浏览器根本不发这个头——在**有** `connection` 门时它们由宿主与
749
+ content-type 校验兜底;在**没有**门的 profile 里,1.6.1 起「写方法必须带来源标记」这条规则会把
750
+ 它们的写请求拒成 **403**(见下一节)。
734
751
 
735
752
  注意这一整套 Host / `Origin` / `Sec-Fetch-Site` 判定**只在没有 `connection` 服务时执行**。
736
753
  有门时它由宿主做(宿主接受回环、`trustedHosts` 与监听地址,并校验 `Origin` 与 `Host` 同源、
737
754
  `Sec-Fetch-Site` 不是 `cross-site`),本插件不再重复判——重复判会把反代域名全部 403;
738
755
  没有门时非回环绑定直接 503,根本走不到这里。
739
756
 
740
- **没有 `connection` 时,哪些方法还算"只读"(1.6.1 更正)**:写门把方法分成两拨——只读集合
741
- (`state`、`history-list`、`read-source`、`export-data`)直接放行,其余一律要求浏览器来源标记
742
- (`Origin` 或 `Sec-Fetch-Site: same-origin`),否则 403。`export-to-dir` 与 `send-feedback`
743
- **不在**只读集合里:前者往备份目录写文件,后者用**你自己的 SMTP 账号**发信,两者都是需要来源
744
- 确认的写操作。1.6.0 及以前它们被错放进只读集合,等于让本机任意进程(Android 上则是任何装了
745
- App 的程序)绕过写门发信。`reload` 仍留在只读集合内,这是有意的:它只重读磁盘、首次落盘幂等,
746
- 而桌面壳转发时会剥掉 `Origin`,移出去会让"重新读取"在无门 profile 上直接 403。
757
+ **没有 `connection` 时,哪些方法还算"只读"(1.6.3 口径)**:写门依据**一张表**(`METHOD_KINDS`)
758
+ 分拨——只读方法(`state`、`history-list`、`read-source`、`export-data`、`reload`)直接放行,
759
+ 其余在 1.6.3 起**默认一律 503**(见下一段)。`export-to-dir` 与 `send-feedback` **不在**只读
760
+ 集合里:前者往备份目录写文件,后者用**你自己的 SMTP 账号**发信,两者都是需要来源确认的写操作。
761
+ 1.6.0 及以前它们被错放进只读集合,等于让本机任意进程(Android 上则是任何装了 App 的程序)
762
+ 绕过写门发信;1.6.1 修复时又把 `reload` 一并误删,1.6.2 已放回。1.6.3 起 `reload` 是**真只读**:
763
+ `readFromDisk` 的 `.bak` 提升分支(会 `writeToDisk`)在 `reload` 路径上被跳过,写回留给下一次
764
+ 真实保存,`saved` 恒为 `false`。
765
+
766
+ **1.6.3:无门 profile 的写操作默认停用(fail-closed)**。之前那套"浏览器来源标记"判据对同机
767
+ 进程不设防——`Origin` 与 `Sec-Fetch-Site` 都是普通请求头,本机任何进程都能自带一个(实测:加
768
+ 一行 `Sec-Fetch-Site: same-origin` 就把 403 变成 200),而这条路径能改写此后每一轮注入的指令,
769
+ 等于把持久化提示注入放在一个自证头之后。所以现在:
770
+
771
+ - 没有 `connection` 服务时,**所有修改方法返回 503**,拒绝文案说明原因;读操作照常,设置页能
772
+ 打开查看。设置页读取快照里的 `security.writeGate`(`fence` / `opt-in` / `refused`),在用户
773
+ 动手之前就把这件事显示出来。
774
+ - 单用户设备(Android / DSH Go / 沙箱)若接受该暴露面,设
775
+ `DSH_INSTRUCTION_MEMORY_ALLOW_UNGUARDED_WRITES=1` 后重启,恢复原来的来源标记判据(仍只挡
776
+ 浏览器跨站,不是身份校验)。
777
+ - 真正的边界仍是宿主的 `connection` 服务;或设 `DSH_INSTRUCTION_MEMORY_REQUIRE_AUTH_FENCE=1`
778
+ 让整条路由 fail-closed(连只读方法都是 503)。
747
779
 
748
780
  ### 宿主的鉴权门(connection):非回环绑定没有它就直接拒绝
749
781
 
@@ -761,8 +793,23 @@ App 的程序)绕过写门发信。`reload` 仍留在只读集合内,这是
761
793
  早先没有这一条,把服务器绑到局域网地址(`--host <具体 IP>`)的机器等于把"改写此后每一轮
762
794
  注入内容"的写入口敞开给整个局域网。设置页在这种状态下会直接显示这条 503 的原因,而不是装作存储是空的。
763
795
 
764
- ### 可信主机名(`DSH_INSTRUCTION_MEMORY_TRUSTED_HOSTS`)
796
+ ### 无鉴权写的显式开关(`DSH_INSTRUCTION_MEMORY_ALLOW_UNGUARDED_WRITES`)
797
+
798
+ 1.6.3 起,**没有 `connection` 服务的 profile 默认拒绝所有修改方法**(503)。这是默认行为,
799
+ 不需要任何环境变量。只有当你在单用户设备上明确接受"同机任意进程也能写"的暴露面、并希望设置页
800
+ 恢复可写时,才设:
765
801
 
802
+ ```bash
803
+ # 单用户设备 / 沙箱 / Android,接受该风险时才设
804
+ DSH_INSTRUCTION_MEMORY_ALLOW_UNGUARDED_WRITES=1
805
+ ```
806
+
807
+ 设了之后恢复的是 v1.6.2 的判据:修改请求必须带浏览器来源标记(`Origin` 或
808
+ `Sec-Fetch-Site: same-origin`)。**它仍然不是身份校验**——两个头本机进程也能自带。要真正的
809
+ 边界,请让宿主提供 `connection` 服务,或设 `DSH_INSTRUCTION_MEMORY_REQUIRE_AUTH_FENCE=1`
810
+ 让整条路由 fail-closed。
811
+
812
+ ### 可信主机名(`DSH_INSTRUCTION_MEMORY_TRUSTED_HOSTS`)
766
813
  > ⚠️ 这一项**只放宽主机名校验,不提供任何身份认证**。在没有宿主 `connection` 服务的 profile
767
814
  > 上,如果一个反代把请求转成"合法主机名",这条路由就是无登录可读写的。面板在该组合
768
815
  > (`security.hostGuarded=false`、`security.authFence=false`、`trustedHostCount>0`)下会显示
package/lib/client.js CHANGED
@@ -273,7 +273,7 @@ window.__ModuleLoader__.load({
273
273
  'err.httpDetail': '宿主返回 HTTP {status}:{detail}',
274
274
  'err.timeout': '请求宿主超时(15 秒无响应)',
275
275
  'export.failed': '导出失败:宿主返回了无法识别的数据',
276
- 'import.confirm': '导入会把文件里的配置集、条目与用户形象并入当前记忆:现有内容不会被删除,但你当前的排列会被整体改写。确定继续?',
276
+ 'import.confirm': '导入会把文件里的配置集、条目与用户形象并入当前记忆:现有内容不会被删除,但你当前的排列会被整体改写。注意导入的条目会保留其「触发词」与「适用情形」——一旦命中,该条目会在下一次注入中变成「本轮强制」。确定继续?',
277
277
  'import.notJson': '导入文件不是有效的 JSON({detail})',
278
278
  'import.tooLarge': '文件约 {size} MB,远超本插件可导入的规模(约 {limit} MB 以内)。请确认选择的是「导出」生成的 JSON 文件。',
279
279
  'editor.new': '新增指令',
@@ -319,6 +319,7 @@ window.__ModuleLoader__.load({
319
319
  'tags.label': '标签(可选,仅用于在本页分组与筛选,不会注入提示词)',
320
320
  'tags.placeholder': '输入标签后回车,逗号分隔多个,例如:代码规范',
321
321
  'tags.hint': '每条最多 {max} 个标签,单个不超过 {len} 字符',
322
+ 'triggers.hint': '每条最多 {max} 个触发词,单个不超过 {len} 字符;用户消息里出现任一触发词时,该条目在下一次注入中临时提升为「本轮强制」',
322
323
  'tags.remove': '移除标签 {tag}',
323
324
  'tags.duplicate': '已有同名标签',
324
325
  'tags.full': '每条最多 {max} 个标签',
@@ -370,6 +371,7 @@ window.__ModuleLoader__.load({
370
371
  'export.saved': '已请求保存。若保存框被取消、或下载被浏览器/系统拦下,文件不会出现——请重试,或改用下面的「完整 JSON」',
371
372
  'export.savedToApp': '已交给 DSH Go 保存:在系统分享面板里选「保存到文件」即可',
372
373
  'export.fallbackHint': '当前环境无法触发文件下载,完整 JSON 已放在下面:全选复制后自行保存',
374
+ 'export.desktopHint': '桌面端:已尝试用浏览器下载通道保存,但宿主的下载处理只接管自身标记的链接,插件无法确认文件是否落地。下面的「完整 JSON」是保底通道——若下载目录里没有文件,请全选复制后自行保存。',
373
375
  'export.fallbackClose': '关闭',
374
376
  'import.webviewHint': 'Android / WebView 上若文件选择器不弹出,请改用「源码模式」粘贴 JSON',
375
377
  'sse.down': '实时更新已断开(连续重试仍失败),数据可能是旧的:请点「重新载入」',
@@ -436,6 +438,7 @@ window.__ModuleLoader__.load({
436
438
  'diag.copied': '诊断信息已复制到剪贴板',
437
439
  'diag.copyManual': '自动复制不可用;请手动全选复制下方文本。',
438
440
  'diag.hostWarn': '安全提示:设置路由当前绑定在非回环地址({host}),同一网络内的设备可以无鉴权访问此页面,请勿在不可信网络暴露;如需对外发布,请阅读 README 的「经反向代理对外发布」。',
441
+ 'diag.writeRefused': '写操作已停用:当前 profile 没有宿主的 connection 会话鉴权门。没有该门时,插件无法区分「设置页发出的请求」与「本机任意进程伪造的请求」,因此保存、导入、删除等修改一律返回 503(读取不受影响)。请让宿主提供 connection 服务;单用户设备可用环境变量 DSH_INSTRUCTION_MEMORY_ALLOW_UNGUARDED_WRITES=1 显式恢复(重启后生效)。',
439
442
  'diag.hostWarnFence': '安全提示:设置路由当前绑定在非回环地址({host}),但宿主鉴权门已挂载——访问仍需登录 DSH 后的浏览器会话,未登录请求会被 401/403 拒绝;仍建议只在可信网络暴露。',
440
443
  'diag.hostWarnTrusted': '安全提示:设置路由绑定在非回环地址({host}),且宿主没有发布鉴权服务;当前唯一放行条件是你配置的 {count} 个可信主机名——那只放宽主机名校验,不提供任何身份认证。请仅在可信网络暴露,或改用隧道方案。',
441
444
  'diag.groupStorage': '存储',
@@ -576,7 +579,7 @@ window.__ModuleLoader__.load({
576
579
  'err.httpDetail': 'Host returned HTTP {status}: {detail}',
577
580
  'err.timeout': 'Host request timed out (no response for 15 s)',
578
581
  'export.failed': 'Export failed: the host returned unrecognized data',
579
- 'import.confirm': 'Import merges the file’s sets, entries and personas into the current memory: nothing is deleted, but your current arrangement is rewritten. Continue?',
582
+ 'import.confirm': 'Import merges the file’s sets, entries and personas into the current memory: nothing is deleted, but your current arrangement is rewritten. Imported entries keep their trigger words and conditions — once one matches, that entry becomes 本轮强制 on the next injection. Continue?',
580
583
  'import.notJson': 'The import file is not valid JSON ({detail})',
581
584
  'import.tooLarge': 'This file is about {size} MB, far beyond what this plugin can import (keep it within about {limit} MB). Make sure you selected a JSON file produced by Export.',
582
585
  'editor.new': 'New instruction',
@@ -622,6 +625,7 @@ window.__ModuleLoader__.load({
622
625
  'tags.label': 'Tags (optional; used only for grouping/filtering on this page, never injected)',
623
626
  'tags.placeholder': 'Type a tag and press Enter; separate several with commas, e.g. code-style',
624
627
  'tags.hint': 'Up to {max} tags per entry, each at most {len} characters',
628
+ 'triggers.hint': 'Up to {max} trigger words per entry, each at most {len} characters; when a user message contains one, the entry is promoted to 本轮强制 for the next injection',
625
629
  'tags.remove': 'Remove tag {tag}',
626
630
  'tags.duplicate': 'This tag already exists',
627
631
  'tags.full': 'Up to {max} tags per entry',
@@ -673,6 +677,7 @@ window.__ModuleLoader__.load({
673
677
  'export.saved': 'Save requested. If you cancelled the dialog or the download was blocked, no file is written — retry, or use the full JSON below',
674
678
  'export.savedToApp': 'Handed to DSH Go — pick “Save to Files” in the system share sheet',
675
679
  'export.fallbackHint': 'This environment cannot start a file download; the full JSON is below — select all and copy it',
680
+ 'export.desktopHint': 'Desktop: the browser download path was attempted, but the host only claims its own marked links, so the plugin cannot confirm a file landed. The full JSON below is the guaranteed route — if nothing appeared in your downloads, copy it from there.',
676
681
  'export.fallbackClose': 'Close',
677
682
  'import.webviewHint': 'On Android / WebView, if the file picker does not open, paste the JSON into “Source mode” instead',
678
683
  'sse.down': 'Live updates stopped (retries kept failing) and the data may be stale — click “Reload”',
@@ -739,6 +744,7 @@ window.__ModuleLoader__.load({
739
744
  'diag.copied': 'Diagnostics copied to the clipboard',
740
745
  'diag.copyManual': 'Automatic copy is unavailable; select and copy the text below manually.',
741
746
  'diag.hostWarn': 'Security notice: the settings route is bound to a non-loopback address ({host}); other devices on the same network can reach this page without authentication. Do not expose it on an untrusted network — for public deployments read “Publishing behind a reverse proxy” in the README.',
747
+ 'diag.writeRefused': 'Writes are disabled: this profile has no host `connection` authentication fence. Without it the plugin cannot tell "the settings page sent this" from "any local process forged this", so saves, imports and deletes all answer 503 (reads still work). Let the host provide the connection service, or — on a single-user device — set DSH_INSTRUCTION_MEMORY_ALLOW_UNGUARDED_WRITES=1 and restart.',
742
748
  'diag.hostWarnFence': 'Security notice: the settings route is bound to a non-loopback address ({host}), but the host authentication fence is attached — access still requires a signed-in DSH browser session, and unauthenticated requests are refused with 401/403. Keep it on a trusted network anyway.',
743
749
  'diag.hostWarnTrusted': 'Security notice: the settings route is bound to a non-loopback address ({host}) and the host publishes no authentication service; the only thing letting requests in is your {count} trusted hostname(s) — that relaxes a hostname check, it does not authenticate anyone. Keep it on a trusted network or use a tunnel.',
744
750
  'diag.groupStorage': 'Storage',
@@ -995,6 +1001,16 @@ window.__ModuleLoader__.load({
995
1001
  // keep the editor usable against an older Host that predates them.
996
1002
  const MAX_TAGS = 8
997
1003
  const MAX_TAG_LEN = 24
1004
+ /**
1005
+ * v1.6.0's trigger list had its own ceilings in the host
1006
+ * (`MAX_TRIGGERS`/`MAX_TRIGGER_LENGTH`). They equal the tag ceilings today,
1007
+ * but that was a numerical coincidence, not a contract: the editor used to
1008
+ * slice at a hard-coded 8 and reuse the tag caps, so changing either host
1009
+ * limit would leave the panel silently disagreeing with the loader. These
1010
+ * two defaults only stand in for a host that predates the published limits.
1011
+ */
1012
+ const MAX_TRIGGERS = 8
1013
+ const MAX_TRIGGER_LEN = 24
998
1014
  const DEFAULT_MAX_TITLE = 120
999
1015
  const DEFAULT_MAX_CONTENT = 6000
1000
1016
  const DEFAULT_MAX_WHEN = 200
@@ -1019,9 +1035,22 @@ window.__ModuleLoader__.load({
1019
1035
  maxWhen: pick('maxWhen', DEFAULT_MAX_WHEN),
1020
1036
  maxTags: pick('maxTagsPerEntry', MAX_TAGS),
1021
1037
  maxTagLength: pick('maxTagLength', MAX_TAG_LEN),
1038
+ maxTriggers: pick('maxTriggers', MAX_TRIGGERS),
1039
+ maxTriggerLength: pick('maxTriggerLength', MAX_TRIGGER_LEN),
1022
1040
  }
1023
1041
  }
1024
1042
 
1043
+ /**
1044
+ * The same ceilings, selected for the box being rendered: tags and trigger
1045
+ * words share the parsing rules but not the limits.
1046
+ */
1047
+ function capsFor(limits, kind) {
1048
+ const caps = fieldLimitsOf(limits)
1049
+ return kind === 'triggers'
1050
+ ? { ...caps, maxTags: caps.maxTriggers, maxTagLength: caps.maxTriggerLength }
1051
+ : caps
1052
+ }
1053
+
1025
1054
  /**
1026
1055
  * Parse free text typed into the tag box into normalised tag candidates.
1027
1056
  * Tags are split on commas (ASCII/full-width), semicolons and newlines —
@@ -1030,9 +1059,9 @@ window.__ModuleLoader__.load({
1030
1059
  * the count cap. Both caps come from the snapshot when supplied. Pinned as
1031
1060
  * a pure export.
1032
1061
  */
1033
- function parseTagInput(text, limits) {
1062
+ function parseTagInput(text, limits, kind) {
1034
1063
  if (typeof text !== 'string') return []
1035
- const caps = fieldLimitsOf(limits)
1064
+ const caps = capsFor(limits, kind)
1036
1065
  const out = []
1037
1066
  const seen = new Set()
1038
1067
  for (const piece of text.split(/[,,;;\n\r]+/)) {
@@ -1091,7 +1120,7 @@ window.__ModuleLoader__.load({
1091
1120
  content: entry.content,
1092
1121
  mode: entry.mode,
1093
1122
  when: entry.when,
1094
- triggers: Array.isArray(entry.triggers) ? entry.triggers.slice(0, 8) : [],
1123
+ triggers: Array.isArray(entry.triggers) ? entry.triggers.slice(0, MAX_TRIGGERS) : [],
1095
1124
  priority: entry.priority,
1096
1125
  enabled: entry.enabled,
1097
1126
  tags: Array.isArray(entry.tags) ? entry.tags.slice() : [],
@@ -1822,6 +1851,7 @@ window.__ModuleLoader__.load({
1822
1851
  inputId: 'im-entry-triggers',
1823
1852
  tags: Array.isArray(draft.triggers) ? draft.triggers : [],
1824
1853
  limits: props.limits,
1854
+ kind: 'triggers',
1825
1855
  disabled: props.busy,
1826
1856
  onChange: (next) => set({ triggers: next }),
1827
1857
  })))
@@ -1853,7 +1883,8 @@ window.__ModuleLoader__.load({
1853
1883
  function TagInput(props) {
1854
1884
  const [text, setText] = React.useState('')
1855
1885
  const tags = Array.isArray(props.tags) ? props.tags : []
1856
- const caps = fieldLimitsOf(props.limits)
1886
+ const kind = props.kind === 'triggers' ? 'triggers' : 'tags'
1887
+ const caps = capsFor(props.limits, kind)
1857
1888
  const merge = (pieces) => {
1858
1889
  const merged = []
1859
1890
  for (const tag of [...tags, ...pieces]) {
@@ -1862,7 +1893,7 @@ window.__ModuleLoader__.load({
1862
1893
  props.onChange(merged.slice(0, caps.maxTags))
1863
1894
  }
1864
1895
  const commit = () => {
1865
- const parsed = parseTagInput(text, props.limits)
1896
+ const parsed = parseTagInput(text, props.limits, kind)
1866
1897
  if (parsed.length > 0) merge(parsed)
1867
1898
  setText('')
1868
1899
  }
@@ -1897,7 +1928,7 @@ window.__ModuleLoader__.load({
1897
1928
  // v is the input's full new value (it already contains what was
1898
1929
  // typed before the separator) — do NOT prepend `text`, that
1899
1930
  // duplicated it ("b" + "b," -> "bb").
1900
- const parsed = parseTagInput(v, props.limits)
1931
+ const parsed = parseTagInput(v, props.limits, kind)
1901
1932
  if (parsed.length > 0) merge(parsed)
1902
1933
  setText('')
1903
1934
  } else {
@@ -1914,7 +1945,10 @@ window.__ModuleLoader__.load({
1914
1945
  },
1915
1946
  onBlur: commit,
1916
1947
  }),
1917
- h('div', { className: 'im-meta' }, t('tags.hint', { max: caps.maxTags, len: caps.maxTagLength })))
1948
+ h('div', { className: 'im-meta' },
1949
+ kind === 'triggers'
1950
+ ? t('triggers.hint', { max: caps.maxTags, len: caps.maxTagLength })
1951
+ : t('tags.hint', { max: caps.maxTags, len: caps.maxTagLength })))
1918
1952
  }
1919
1953
 
1920
1954
  /**
@@ -2680,7 +2714,7 @@ window.__ModuleLoader__.load({
2680
2714
 
2681
2715
  function adoptLegacy() {
2682
2716
  if (busy) return
2683
- void send('adopt-legacy', { baseRev: revOf() })
2717
+ void send('adopt-legacy', { baseRev: revOf(), confirm: true })
2684
2718
  }
2685
2719
 
2686
2720
  function dismissLegacy() {
@@ -2712,7 +2746,7 @@ window.__ModuleLoader__.load({
2712
2746
  setError(t('import.notJson', { detail: String(err && err.message ? err.message : err) }))
2713
2747
  return
2714
2748
  }
2715
- await send('restore-data', { payload, baseRev: revOf() })
2749
+ await send('restore-data', { payload, baseRev: revOf(), confirm: true })
2716
2750
  } catch (err) {
2717
2751
  setError(String(err && err.message ? err.message : err))
2718
2752
  }
@@ -2791,6 +2825,26 @@ window.__ModuleLoader__.load({
2791
2825
  }
2792
2826
  }
2793
2827
 
2828
+ /**
2829
+ * The Electron desktop shell.
2830
+ *
2831
+ * `dsh-app://` is the protocol the renderer is loaded from, so this is a
2832
+ * direct observation rather than a user-agent guess — until 1.6.3 the
2833
+ * panel had NO desktop branch at all: every export fell through to the
2834
+ * generic `blob:` download, while the host's `will-download` handler only
2835
+ * recognises `#dsh-download=` URLs and leaves the rest to Chromium's
2836
+ * default policy, which the page cannot observe. That is why the desktop
2837
+ * export now also offers the in-panel copy instead of claiming a file.
2838
+ */
2839
+ function isDesktopShell() {
2840
+ try {
2841
+ return typeof location !== 'undefined' && typeof location.protocol === 'string'
2842
+ && location.protocol === 'dsh-app:'
2843
+ } catch {
2844
+ return false
2845
+ }
2846
+ }
2847
+
2794
2848
  /**
2795
2849
  * Hand `text` to the DSH Go island bridge, in chunks that fit its 16 KB
2796
2850
  * message cap (`IslandMessageLimits.maxBytes`). The app reassembles them
@@ -2861,7 +2915,15 @@ window.__ModuleLoader__.load({
2861
2915
  link.click()
2862
2916
  if (link.parentNode !== null) link.parentNode.removeChild(link)
2863
2917
  setTimeout(() => { URL.revokeObjectURL(url) }, 1000)
2864
- setNotice(t('export.saved'))
2918
+ if (isDesktopShell()) {
2919
+ // The desktop host cannot confirm this landed: its download handler
2920
+ // only claims `#dsh-download=` URLs, and a blob click is Chromium's
2921
+ // business. Offer the guaranteed route and say what was attempted.
2922
+ setExportFallback(text)
2923
+ setNotice(t('export.desktopHint'))
2924
+ } else {
2925
+ setNotice(t('export.saved'))
2926
+ }
2865
2927
  } catch (err) {
2866
2928
  setError(String(err && err.message ? err.message : err))
2867
2929
  }
@@ -2902,7 +2964,7 @@ window.__ModuleLoader__.load({
2902
2964
  if (askImport === CONFIRM_UNAVAILABLE) setError(t('confirm.unavailable'))
2903
2965
  return
2904
2966
  }
2905
- await send('import-data', { payload, baseRev: revOf() })
2967
+ await send('import-data', { payload, baseRev: revOf(), confirm: true })
2906
2968
  } catch (err) {
2907
2969
  setError(String(err && err.message ? err.message : err))
2908
2970
  }
@@ -3092,7 +3154,7 @@ window.__ModuleLoader__.load({
3092
3154
  // known problem" — an absent field must not manufacture a warning.
3093
3155
  const security = view.security !== null && typeof view.security === 'object'
3094
3156
  ? view.security
3095
- : { hostGuarded: true, boundHost: '' }
3157
+ : { hostGuarded: true, boundHost: '', writeGate: 'fence' }
3096
3158
  // List order is display-only and never changes what the Host injects
3097
3159
  // (the injection order is decided host-side). Sorting, filtering and the
3098
3160
  // keyword scan all happen once, inside the memoised deriveListState.
@@ -3334,6 +3396,11 @@ window.__ModuleLoader__.load({
3334
3396
  ? t('diag.hostWarnTrusted', { host: security.boundHost !== '' ? security.boundHost : '?', count: security.trustedHostCount })
3335
3397
  : t('diag.hostWarn', { host: security.boundHost !== '' ? security.boundHost : '?' }))
3336
3398
  : null,
3399
+ // 1.6.3: a fence-less profile refuses every mutating method by default,
3400
+ // so a save button would fail only AFTER the user typed. Say it before.
3401
+ security.writeGate === 'refused'
3402
+ ? h('div', { className: 'im-hostwarn', key: 'writewarn' }, t('diag.writeRefused'))
3403
+ : null,
3337
3404
  h('div', { className: 'im-row' },
3338
3405
  h('div', null,
3339
3406
  h('h3', { className: 'im-h' }, t('panel.title')),