dsh-memoir 0.4.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/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 dsh-memoir contributors
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,242 @@
1
+ # dsh-memoir
2
+
3
+ [![npm version](https://img.shields.io/npm/v/dsh-memoir.svg)](https://www.npmjs.com/package/dsh-memoir)
4
+
5
+ **dsh-memoir 是 DeepSeek Harness 的本地项目记忆层:把 Agent 的工作结论、经验教训和后续行动持久化,并通过有界 Hot Memory 自动继承、按需排序召回和 Web GUI 管理,实现跨会话项目记忆。**
6
+
7
+ > Cache-aware local project memory for DeepSeek Harness.
8
+
9
+ - **Local-only**:全部数据留在本机(`~/.dsh/dsh-memoir.json` + 项目内 `PROJECT_MEMORY.md`)
10
+ - **Zero external memory service**:无向量数据库、无 embedding API、无云端记忆服务
11
+ - **Bounded hot-memory injection**:token 预算内的 Hot Memory 自动注入 system prompt(默认 900/1200)
12
+ - **Ranked local recall**:倒排索引 + BM25 本地排序召回,`memoir_read` 按需检索长尾历史
13
+ - **Web GUI**:侧边栏「记忆」面板——项目/全局浏览、相关排序搜索、Hot Memory Inspector、Retrieval Diagnostics
14
+
15
+ ## Quick Start
16
+
17
+ ```bash
18
+ # 从 npm 安装到 web profile(推荐)
19
+ dsh plugin --profile web add dsh-memoir
20
+
21
+ # 或从 GitHub 安装最新源码
22
+ dsh plugin --profile web add github:Qinling-Melon-Farmers/dsh-memoir
23
+
24
+ # 或本地开发(克隆后)
25
+ dsh plugin --profile web add link:/绝对路径/dsh-memoir
26
+ ```
27
+
28
+ 安装后重启 DSH 生效(`dsh web`)。正常使用即可:
29
+
30
+ ```text
31
+ 正常使用 Agent
32
+ ↓
33
+ 有实际工作的回合结束自动提醒归纳
34
+ ↓
35
+ memoir_record 沉淀工作 / 教训 / 下一步
36
+ ↓
37
+ 未来 session 自动继承 Hot Memory(有界、排序、会话内冻结)
38
+ ↓
39
+ 需要长尾历史时 memoir_read(本地相关性排序召回)
40
+ ```
41
+
42
+ ## Architecture
43
+
44
+ ```text
45
+ ~/.dsh/dsh-memoir.json
46
+ │
47
+ │ SSOT(单一事实源)
48
+ ▼
49
+ MemoirStore
50
+ ┌─────────────┴─────────────┐
51
+ │ │
52
+ ▼ ▼
53
+ PROJECT_MEMORY.md Retrieval Index
54
+ human-readable ranked recall
55
+ (git 可提交) │
56
+ │ ▼
57
+ │ memoir_read
58
+ │ GUI /search
59
+ │
60
+ ▼
61
+ Hot Memory Selector
62
+ (token 预算)
63
+ │
64
+ ▼
65
+ Session Snapshot
66
+ (每会话冻结)
67
+ │
68
+ ▼
69
+ System Prompt
70
+ ```
71
+
72
+ ## Memory Model:Full Memory vs Hot Memory
73
+
74
+ **Full Memory(完整历史)**——结构化 JSON SSOT + 自动重新生成的 `PROJECT_MEMORY.md` 投影。用途:完整历史、GUI 浏览、git 提交、人工检查、排序召回的数据源。
75
+
76
+ **Hot Memory(有界注入)**——selector 在 token 预算内选出的高价值记忆,注入 system prompt。特点:**bounded / ranked / compact / session-frozen**。
77
+
78
+ > v0.4+ 不再把完整 PROJECT_MEMORY.md 注入模型:Hot Memory 进 prompt,长尾历史走排序召回。
79
+
80
+ **Session Snapshot 冻结语义**:同一 session 的注入文本只构建一次并冻结(prompt 前缀稳定,最大化 prompt-prefix cache 命中);当前 session 不重新消费自己刚写的记忆,新 session 重建并看到最新记忆。v0.4.2 起,没有唯一会话身份(session.id / agent.id)时**不做冻结**——宁可 cache miss,不可跨 session 错复用旧快照。
81
+
82
+ ## Tools
83
+
84
+ | 工具 | 作用 |
85
+ | --- | --- |
86
+ | `memoir_record` | 写入 work(工作记录)/ lessons(经验教训)/ actions(行动指南)/ note(备注) |
87
+ | `memoir_read` | project(默认)/ global / all 的本地相关性检索,limit + compact/full 输出形态 |
88
+
89
+ `memoir_read` 的 query 描述与真实行为一致:**本地相关性检索标题与正文,支持中文短语、英文关键词、代码标识符与路径,并按相关性排序**。
90
+
91
+ ## Retrieval
92
+
93
+ - 无 embedding、无向量库、无外部记忆服务
94
+ - 中文 2/3-gram + 英文单词 + 代码/路径标识符分词
95
+ - BM25(文档侧保留真实 term frequency;query 侧去重)
96
+ - 标题 2.5× 加权、精确短语加权、分类权重、时间衰减
97
+ - 标题与正文各自独立的长度归一化(v0.4.2)
98
+ - epoch 感知 + 1 小时 time-bucket 的 LRU 查询缓存:limit/detail 不参与缓存键,所有输出形态共享同一份排序结果(v0.4.2)
99
+ - Query cache 指标(hits/misses/evictions/hit rate)与 Last Query(latency/candidates/returned)可观测(v0.4.2)
100
+ - 全局 recall 的 limit 是真正全局 Top-K,输出截断保留高分头部(v0.4.2)
101
+
102
+ curated 查询 Top-5 命中率 100%(质量门禁 ≥90%,见 `test/recall-quality.test.ts`)。
103
+
104
+ ## GUI
105
+
106
+ 保留 v0.4 的 Project / Global / Search / Add / Delete / Diagnostics 架构,v0.4.2 起:
107
+
108
+ - **搜索统一走 RetrievalEngine**:query 非空时面板调用 `GET /api/dsh-memoir/search`,与 agent 的 `memoir_read` 共用同一套 BM25 排序,结果按相关性排列并显示分数
109
+ - **Hot Memory Inspector**:展开查看当前工作区实际会被注入的 Hot Memory(Actions / Lessons / Recent state),即「下一会话到底自动继承什么」
110
+ - **Retrieval Diagnostics**:Retrieval Index(docs/terms/epoch)、Query Cache(hits/misses/evictions/hit rate/size/capacity)、Last Query(latency/returned)、Session Snapshot(hash/createdAt/storeRevision)
111
+
112
+ ## 界面预览
113
+
114
+ **1. 插件生效与整体 UI**:侧边栏出现「记忆」入口(与 SSH / 任务看板同列、互斥展开),点击后在中心列打开记忆面板。
115
+
116
+ ![插件生效与整体 UI](picture/插件生效和UI效果1.png)
117
+
118
+ **2. 项目记忆**:当前项目会话的持久记忆按 工作记录 / 经验教训 / 行动指南 / 备注 分组展示,每条带时间、分类标签、标题、正文与会话来源,支持检索、刷新与逐条删除。
119
+
120
+ ![项目记忆](picture/项目记忆2.png)
121
+
122
+ **3. 手动添加记忆**:表单选择分类、填写一句话标题与正文,与 agent 的 `memoir_record` 写入同一份数据,提交后 `PROJECT_MEMORY.md` 自动重新生成。
123
+
124
+ ![手动添加记忆](picture/手动添加记忆3.png)
125
+
126
+ **4. 全局记忆管理**:所有项目的记忆桶(项目名、路径、更新时间、条数),跨项目检索与逐条维护。
127
+
128
+ ![全局记忆管理](picture/全局记忆管理4.png)
129
+
130
+ > v0.4.2 新增的带 query 的 ranked results 与 Hot Memory Inspector / Retrieval Diagnostics 截图将随版本发布补充。
131
+
132
+ ## Storage & Privacy
133
+
134
+ ```text
135
+ ~/.dsh/dsh-memoir.json ← 结构化 JSON(唯一事实源 / SSOT)
136
+ <工作区>/PROJECT_MEMORY.md ← 由 JSON 重新生成的人类可读投影(git 友好)
137
+
138
+ No cloud memory DB · No embedding API · No vector DB
139
+ ```
140
+
141
+ JSON 是 source of truth,Markdown 是 generated projection:面板、工具、agent 三条路径写同一份数据。v0.4.2 起,面板写 API 还受工作区授权保护——浏览器提交的绝对路径不等于授权,仅当前活动 cwd 或已有 store 项目可写。
142
+
143
+ ## Configuration
144
+
145
+ 在 `cordis.patch.yml` 的行上可加 `config`(全部可省略,默认值如下):
146
+
147
+ ```yaml
148
+ - insert:
149
+ - id: memoir
150
+ name: dsh-memoir
151
+ config:
152
+ enabled: true # 总开关(工具、路由、注入段)
153
+ announceToAgent: true # system prompt 公告段
154
+ autoDistill: true # 每轮有实际工作的回合结束自动提醒归纳
155
+ hotMemoryTokens: 900 # Hot Memory 目标 token 数
156
+ hotMemoryMaxTokens: 1200 # Hot Memory 硬上限(永不超过)
157
+ readDefaultLimit: 8 # memoir_read 默认返回条数
158
+ readMaxLimit: 30 # memoir_read 最大返回条数
159
+ sessionSnapshotMax: 128 # 每会话快照的 LRU 上限
160
+ queryCacheSize: 128 # 排序查询的 LRU 缓存大小
161
+ ```
162
+
163
+ ## Design Trade-offs
164
+
165
+ - **有界注入 vs 全量注入**:v0.3 把完整历史注入 prompt,越用越膨胀;v0.4+ 只注入预算内的 Hot Memory,长尾历史按需召回。token 基准见下方 Benchmark。
166
+ - **冻结 vs 新鲜**:session 内冻结注入文本换取 prompt-prefix cache 命中;没有唯一会话身份时不冻结(v0.4.2),保证新 session 一定看到新记忆。
167
+ - **Hot Memory 配额**:Recent state(最新 work,1~3 条)保底、actions/lessons 排名填充,work 只进 Recent state 不重复注入(v0.4.2)。
168
+ - **多进程安全**:store 的 record/remove 走 `~/.dsh/dsh-memoir.lock` 跨进程临界区(O_EXCL 独占创建 + 超时),临界区内强制从磁盘重读再改,两个 DSH 进程交错写入不丢更新(v0.4.2)。
169
+ - **Windows 路径**:canonical key 全小写(`C:\A` / `c:\a\` / `C:/A` 一个桶),display path 保留原始大小写(v0.4.2)。
170
+ - **GUI 与 Agent 同源**:面板搜索与 `memoir_read` 共用 RetrievalEngine,不再各写一套过滤逻辑(v0.4.2)。
171
+
172
+ ## Use Cases
173
+
174
+ | 场景 | 怎么用 |
175
+ | --- | --- |
176
+ | 反复出现的环境坑(乱码 / 转义 / 路径 / 权限) | 解决后记一条 `lessons`,附可复制的修复命令 |
177
+ | 项目红线与约定(禁 emoji、发布前跑测试、分支规范) | 记入 `actions`,自动注入给接手者 |
178
+ | 难查 bug 的根因与结论 | 记入 `lessons` / `work`,避免重复排查 |
179
+ | 部署 / 上线的固定步骤清单 | 记入 `actions`,新会话照单执行 |
180
+ | 跨项目复用经验 | 面板全局 tab 或 `memoir_read(scope: 'global', query: ...)` |
181
+
182
+ 典型例子:第一次解决「控制台中文乱码」后把诊断结论与修复步骤记成一条 lessons(如 `先 chcp 65001 …写文件一律 UTF-8 无 BOM`),此后本项目每个新会话都自动继承这条经验,不再重复排查;跨项目用全局检索也能命中。记忆插件做的是把「根因 + 修复命令」沉淀为项目知识,不负责根治终端本身的编码缺陷。
183
+
184
+ ## Comparison
185
+
186
+ | 项目 | 主要定位 |
187
+ | --- | --- |
188
+ | dsh-memory | citation / 来源可追溯的引用式记忆 |
189
+ | dsh-mnemon | 更重的长期记忆体系 |
190
+ | distill | 会话蒸馏成 skill |
191
+ | **dsh-memoir** | **轻量的项目工作流记忆:本地、有界注入、排序召回** |
192
+
193
+ 各插件定位不同,按需选择,不做谁强谁弱的比较。
194
+
195
+ ## Development / Benchmark / Tests
196
+
197
+ ```bash
198
+ pnpm install # 安装 devDeps(typescript、esbuild、@deepseek-ai/* 类型包)
199
+ pnpm run build # tsc 构建 host + esbuild 构建 client bundle
200
+ pnpm run typecheck # 全量类型检查(src + test)
201
+ pnpm test # 131 项测试:store(含多进程锁) / snapshot / selector / retrieval / tools / routes / 自动收尾 / 集成 / client 纯逻辑 / bundle 协议与纯净性
202
+ npm run bench # benchmark(100/1k/10k/100k 条目),结果写入 bench/report.md
203
+ ```
204
+
205
+ 质量门禁:**Top-5 recall ≥ 90% · Hot Memory ≤ 配置 hardMax · 同会话 prompt 前缀稳定 · 全局召回 ≤ limit · 多进程写入零丢失**。
206
+
207
+ v0.4.2 benchmark 摘要(node v22.23.2,budget 900/1200 tokens;完整报告见 `bench/report.md`。方法已修正:uncached 查询直测 `search()`、cached 查询先预热同一 query 再计时):
208
+
209
+ | 条目数 | 冷加载 | 热读取 | Hot Memory 构建 | 索引构建 | 未缓存查询 | 缓存查询 | 缓存命中率 | 全量 markdown tokens | 注入 tokens | 降幅 |
210
+ |---|---|---|---|---|---|---|---|---|---|---|
211
+ | 100 | 1.3 ms | 2.22 µs | 0.54 ms | 2.9 ms | 0.224 ms | 2.87 µs | 50.0% | 3870 | 902 | 76.7% |
212
+ | 1,000 | 1.6 ms | 0.40 µs | 0.70 ms | 15.0 ms | 1.419 ms | 1.45 µs | 50.0% | 38182 | 916 | 97.6% |
213
+ | 10,000 | 25.3 ms | 0.42 µs | 2.60 ms | 142.9 ms | 11.889 ms | 1.14 µs | 50.0% | 385807 | 902 | 99.8% |
214
+ | 100,000 | 158.2 ms | 0.42 µs | 31.97 ms | 2238.7 ms | 153.551 ms | 1.15 µs | 50.0% | 3907057 | 917 | 100.0% |
215
+
216
+ ## 实现说明
217
+
218
+ - **TypeScript 全栈**:`src/host/*.ts`(store / tools / retrieval / selector / snapshot / routes / autodistill / index,tsc 构建出 `lib/*.js`)+ `src/client/*.ts(x)`(esbuild 打出 `lib/client.js` 闭包工厂 bundle)。
219
+ - **双面插件**:host 半注册 agent 工具、`/api/dsh-memoir` 路由、`agent/turn-stopping` 自动收尾监听与按项目求值的 system prompt 注入段;client 半提供面板。运行时仅依赖官方 NPM SDK。
220
+ - 通过 `dsh.bundle.patch` manifest(`cordis.patch.yml` 的 `insert` 行)挂载,不改 DSH 源码。
221
+ - 自动收尾安全边界:仅顶级会话(跳过 subagent / 嵌套委托)、仅「有工具调用且未记录过」的回合、已中止回合不打扰、每回合至多一次。
222
+
223
+ ## Release
224
+
225
+ 版本发布由 `.github/workflows/publish.yml` 在 `v*` tag 推送后自动执行:安装依赖、校验 tag 与 `package.json` 版本一致、运行 typecheck/test,再发布到 npm。仓库需配置以下任一认证方式:
226
+
227
+ - npm Trusted Publishing:GitHub 仓库 `Qinling-Melon-Farmers/dsh-memoir`,workflow `publish.yml`
228
+ - GitHub Actions secret `NPM_TOKEN`:使用具有发布权限且允许绕过发布 2FA 的 granular token
229
+
230
+ 发布 patch 版本:
231
+
232
+ ```bash
233
+ npm version patch
234
+ git push
235
+ git push origin vX.Y.Z # 使用 npm version 输出的实际版本号
236
+ ```
237
+
238
+ `npm version patch` 会修改 `package.json`、创建版本提交并创建对应 tag;无需再次执行 `git tag` 或在本机执行 `npm publish`。
239
+
240
+ ## 许可
241
+
242
+ Apache-2.0
@@ -0,0 +1,21 @@
1
+ # dsh-memoir bundle patch: inserts the host-half plugin row into the web
2
+ # profile roster. Applied as a profile bundle layer (the `dsh.bundle.patch`
3
+ # manifest field) over dsh-base; activate with
4
+ # dsh plugin --profile web add github:<owner>/dsh-memoir
5
+ # or, for local development,
6
+ # dsh plugin --profile web add file:<abs path to this directory>
7
+ #
8
+ # The row is a bare plugin by package name: the node half (exports ".")
9
+ # registers the memoir_record / memoir_read tools, the /api/dsh-memoir panel
10
+ # routes, the turn-end auto-distill listener, and the per-project
11
+ # system-prompt announcement; the browser half (exports "./client") renders
12
+ # the sidebar entry + memory panel.
13
+ #
14
+ # Optional config (all default true):
15
+ # config:
16
+ # enabled: true # master switch (tools, routes, prompt section)
17
+ # announceToAgent: true # system-prompt announcement
18
+ # autoDistill: true # steer a distill prompt after each worked turn
19
+ - insert:
20
+ - id: memoir
21
+ name: dsh-memoir
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Automatic turn-end distillation: when the plugin is enabled, each turn of a
3
+ * top-level agent that did real work (made tool calls) and did not already
4
+ * record memory is followed by one steering step asking the agent to distill
5
+ * the turn into memoir_record entries. Turns without tool activity are left
6
+ * alone (no extra model cost), subagent sessions are never steered, and each
7
+ * turn is steered at most once — the steering step runs inside the same turn,
8
+ * so the per-turn gate is what lets the turn close afterwards.
9
+ *
10
+ * Pure decision helpers are exported for unit tests.
11
+ */
12
+ import type { UserMessage } from '@deepseek-ai/dsh-llm';
13
+ /** The steering prompt injected at the end of an active turn. */
14
+ export declare const DISTILL_PROMPT: string;
15
+ /** Plugin identity stamped on the steering message source. */
16
+ export declare const AUTO_DISTILL_PLUGIN = "dsh-memoir";
17
+ /** A minimal event view for the turn-activity scan (data is narrowed inside). */
18
+ export interface TurnEventLike {
19
+ type: string;
20
+ data?: unknown;
21
+ }
22
+ /** Scan the tail of a session log for one turn's tool activity. */
23
+ export declare function turnActivity(events: readonly TurnEventLike[], turn: number): {
24
+ worked: boolean;
25
+ recorded: boolean;
26
+ };
27
+ /** The agent surface the turn-stopping listener needs. */
28
+ export interface AutoDistillAgentLike {
29
+ id: string;
30
+ session: {
31
+ header: {
32
+ origin?: string;
33
+ delegationDepth?: number;
34
+ };
35
+ events: readonly TurnEventLike[];
36
+ };
37
+ steer(message: UserMessage): void;
38
+ }
39
+ /** Subagent sessions (and any nested delegation) never get distilled. */
40
+ export declare function isSubagentSession(agent: AutoDistillAgentLike): boolean;
41
+ /** Per-agent memory of turns already steered (with pruning). */
42
+ export declare class AutoDistillGate {
43
+ private steered;
44
+ /** Claim a turn for steering; false when already claimed (or pruned-out). */
45
+ consume(agentId: string, turn: number): boolean;
46
+ /** Drop all state for one agent (disposal hygiene). */
47
+ forget(agentId: string): void;
48
+ }
49
+ export interface TurnStoppingPayload {
50
+ agent: AutoDistillAgentLike;
51
+ turn: number;
52
+ signal: AbortSignal;
53
+ }
54
+ /** The event-wire surface the installer needs (satisfied by ctx.on). */
55
+ export interface AutoDistillWire {
56
+ on(name: 'agent/turn-stopping', listener: (payload: TurnStoppingPayload) => void): () => void;
57
+ }
58
+ /**
59
+ * Install the turn-end listener. The returned disposer removes the listener.
60
+ * @param wire - the event wire (the cordis context).
61
+ * @param options.enabled - live read of the autoDistill switch.
62
+ */
63
+ export declare function installAutoDistill(wire: AutoDistillWire, options: {
64
+ enabled: () => boolean;
65
+ }): () => void;
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Automatic turn-end distillation: when the plugin is enabled, each turn of a
3
+ * top-level agent that did real work (made tool calls) and did not already
4
+ * record memory is followed by one steering step asking the agent to distill
5
+ * the turn into memoir_record entries. Turns without tool activity are left
6
+ * alone (no extra model cost), subagent sessions are never steered, and each
7
+ * turn is steered at most once — the steering step runs inside the same turn,
8
+ * so the per-turn gate is what lets the turn close afterwards.
9
+ *
10
+ * Pure decision helpers are exported for unit tests.
11
+ */
12
+ import { createUserMessage } from '@deepseek-ai/dsh-llm';
13
+ /** The steering prompt injected at the end of an active turn. */
14
+ export const DISTILL_PROMPT = '(dsh-memoir 自动收尾)本轮工作已结束,请把本轮归纳沉淀进项目记忆:\n' +
15
+ '1. 若本轮有实质产出、踩坑结论或下一步安排,调用 memoir_record 分条记录(section 取 work 工作记录 / lessons 经验教训 / actions 行动指南,可用 title 一句话概括);\n' +
16
+ '2. 若本轮已经记录过、或没有值得沉淀的内容,直接回复「本轮无需沉淀」,不要调用任何工具。\n' +
17
+ '最终回复保持一句话以内,不要展开。';
18
+ /** Plugin identity stamped on the steering message source. */
19
+ export const AUTO_DISTILL_PLUGIN = 'dsh-memoir';
20
+ /** Scan the tail of a session log for one turn's tool activity. */
21
+ export function turnActivity(events, turn) {
22
+ let worked = false;
23
+ let recorded = false;
24
+ for (let i = events.length - 1; i >= 0; i--) {
25
+ const event = events[i];
26
+ const data = event.data;
27
+ if (data === undefined || typeof data.turn !== 'number')
28
+ continue;
29
+ if (data.turn < turn)
30
+ break;
31
+ if (data.turn !== turn)
32
+ continue;
33
+ if (event.type === 'tool/call') {
34
+ worked = true;
35
+ if (data.name === 'memoir_record')
36
+ recorded = true;
37
+ }
38
+ }
39
+ return { worked, recorded };
40
+ }
41
+ /** Subagent sessions (and any nested delegation) never get distilled. */
42
+ export function isSubagentSession(agent) {
43
+ return agent.session.header.origin === 'subagent' || (agent.session.header.delegationDepth ?? 0) > 0;
44
+ }
45
+ /** Per-agent memory of turns already steered (with pruning). */
46
+ export class AutoDistillGate {
47
+ steered = new Map();
48
+ /** Claim a turn for steering; false when already claimed (or pruned-out). */
49
+ consume(agentId, turn) {
50
+ let set = this.steered.get(agentId);
51
+ if (set === undefined) {
52
+ set = new Set();
53
+ this.steered.set(agentId, set);
54
+ }
55
+ if (set.has(turn))
56
+ return false;
57
+ set.add(turn);
58
+ for (const t of [...set]) {
59
+ if (t < turn - 100)
60
+ set.delete(t);
61
+ }
62
+ return true;
63
+ }
64
+ /** Drop all state for one agent (disposal hygiene). */
65
+ forget(agentId) {
66
+ this.steered.delete(agentId);
67
+ }
68
+ }
69
+ /**
70
+ * Install the turn-end listener. The returned disposer removes the listener.
71
+ * @param wire - the event wire (the cordis context).
72
+ * @param options.enabled - live read of the autoDistill switch.
73
+ */
74
+ export function installAutoDistill(wire, options) {
75
+ const gate = new AutoDistillGate();
76
+ return wire.on('agent/turn-stopping', (payload) => {
77
+ if (!options.enabled())
78
+ return;
79
+ const { agent, turn, signal } = payload;
80
+ if (isSubagentSession(agent))
81
+ return;
82
+ if (signal.aborted)
83
+ return;
84
+ const { worked, recorded } = turnActivity(agent.session.events, turn);
85
+ if (!worked || recorded)
86
+ return;
87
+ if (!gate.consume(agent.id, turn))
88
+ return;
89
+ agent.steer(createUserMessage({
90
+ content: [{ type: 'text', text: DISTILL_PROMPT }],
91
+ source: { kind: 'plugin', plugin: AUTO_DISTILL_PLUGIN },
92
+ }));
93
+ });
94
+ }