dsh-rosemary-memory 0.3.0 → 0.3.1
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 +12 -0
- package/README.md +6 -0
- package/client.js +8 -2
- package/index.js +32 -11
- package/lib/digest.js +301 -268
- package/lib/store.js +26 -1
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# 更新日志
|
|
2
2
|
|
|
3
|
+
## 0.3.1
|
|
4
|
+
|
|
5
|
+
- 修复:**压缩检查点被当成用户提问**。DSH 压缩后会追加一条 `source.kind = 'compact-checkpoint'` 的合成 `user/message`;压缩发生在没有打开回合的空档期时,它会被写成一条 user 为英文 preamble 的假 run。已加入 `PSEUDO_USER_KINDS` 白名单
|
|
6
|
+
- 修复:**标题式行被存成结论**。`**③ 两个必须改的点**`、`必须重写的只有三块:` 这类能命中 `DECISION_RE` 的标题/引导句不再入库(新增 `isStatement()` 过滤:过短、以冒号逗号结尾、纯加粗包裹、无谓语且 <26 字)
|
|
7
|
+
- 改进:note 的 `title` 改为取首句(第一个逗号前,≥8 字才用),`body` 保留整句,不再出现 `title == body`
|
|
8
|
+
- 改进:剥离 `结论先说` / `先说结论` / `一句话` 前缀和 `#` 标题符
|
|
9
|
+
- 修复:**同一 bundle 被执行两次会抛 duplicate factory registration**。改名后陈旧模块图会按旧 id 重新请求旧 rev,拿回的不是 JS;`client.js` 加一次性守卫,第二次执行变成 no-op
|
|
10
|
+
- 修复:**并发归纳**。`/digest` 加单飞锁,正在跑时返回 409,避免两个请求同时读写同一批 JSONL
|
|
11
|
+
- 改进:**归纳期间让出事件循环**。每处理完一个会话 `setImmediate` 一次,桌面版宿主被守护进程监督,长时间阻塞看起来像挂死(实测 36 会话仍约 3 秒)
|
|
12
|
+
- 补 `engines.dsh` 版本约束
|
|
13
|
+
- 说明:DSH 压缩只遮蔽(`shadowedSeqs`)原始记录、不删档,所以 digest 仍能读到被压缩掉的完整回合;本次只影响合成检查点那条
|
|
14
|
+
|
|
3
15
|
## 0.3.0
|
|
4
16
|
|
|
5
17
|
- **改名:`dsh-memory` → `dsh-rosemary-memory`**(rosemary 是迷迭香:记忆的香料)。包名、bundle 名、客户端模块 id、导出名全部跟着改;`/dsh-memory/*` 路由前缀保留为别名,旧书签和旧客户端 bundle 不会 404
|
package/README.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# dsh-rosemary-memory
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/dsh-rosemary-memory)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
[](package.json)
|
|
6
|
+
|
|
3
7
|
**可审计的跨会话记忆** —— DeepSeek Harness(DSH)插件。
|
|
4
8
|
|
|
5
9
|
> 名字里的 rosemary 是「迷迭香」:记忆的香料。记住该记的,也敢忘记该忘的。
|
|
@@ -87,6 +91,8 @@ npm install dsh-rosemary-memory # 或 pnpm add dsh-rosemary-memory
|
|
|
87
91
|
|
|
88
92
|
重载 DSH(或 `plugin_manager set_bundle enabled=false` → `true`)后,左侧栏出现「🧠 记忆」。
|
|
89
93
|
|
|
94
|
+
> ⚠️ 从 npm 安装时,默认数据目录落在 `node_modules/dsh-rosemary-memory/data`,重装或升级依赖会把它连同记忆一起换掉。想长期保留,请用 `DSH_ROSEMARY_MEMORY_DIR` 或 bundle config 的 `storeDir` 指到 profile 之外(见「数据」)。
|
|
95
|
+
|
|
90
96
|
### 本地目录(改代码时用这个)
|
|
91
97
|
|
|
92
98
|
```
|
package/client.js
CHANGED
|
@@ -9,7 +9,12 @@
|
|
|
9
9
|
//
|
|
10
10
|
// Everything is a same-origin fetch to /dsh-rosemary-memory/*, so it keeps working when
|
|
11
11
|
// the UI is opened from a phone through the remote tunnel.
|
|
12
|
-
|
|
12
|
+
// A stale module graph can make the shell execute this same bundle URL twice
|
|
13
|
+
// (it logs "duplicate factory registration" and keeps the first module). The
|
|
14
|
+
// guard makes a second execution a no-op instead of a console error.
|
|
15
|
+
if (window.__dshRosemaryMemoryLoaded !== true) {
|
|
16
|
+
window.__dshRosemaryMemoryLoaded = true;
|
|
17
|
+
window.__ModuleLoader__.load({
|
|
13
18
|
id: 'dsh-rosemary-memory',
|
|
14
19
|
factory: (require) => {
|
|
15
20
|
const React = require('react');
|
|
@@ -394,4 +399,5 @@ window.__ModuleLoader__.load({
|
|
|
394
399
|
},
|
|
395
400
|
};
|
|
396
401
|
},
|
|
397
|
-
});
|
|
402
|
+
});
|
|
403
|
+
}
|
package/index.js
CHANGED
|
@@ -15,7 +15,7 @@ import path from 'node:path';
|
|
|
15
15
|
import { fileURLToPath } from 'node:url';
|
|
16
16
|
import { listSessions, readRecords } from './lib/transcript.js';
|
|
17
17
|
import { digestRecords } from './lib/digest.js';
|
|
18
|
-
import { readAll, mergeRuns, mergeNotes, loadState, saveState } from './lib/store.js';
|
|
18
|
+
import { readAll, mergeRuns, mergeNotes, loadState, saveState, noteKey } from './lib/store.js';
|
|
19
19
|
import { recall, brief } from './lib/recall.js';
|
|
20
20
|
import { buildUnits, normalizeConfig, DEFAULT_CONFIG } from './lib/units.js';
|
|
21
21
|
|
|
@@ -81,10 +81,14 @@ function digestSessions({ sessionId, project, all, limit }) {
|
|
|
81
81
|
return { home, sessions: listSessions(scope) };
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
export function planDigest({ sessionId, project, all, limit } = {}) {
|
|
84
|
+
export async function planDigest({ sessionId, project, all, limit } = {}) {
|
|
85
85
|
const { home, sessions } = digestSessions({ sessionId, project, all, limit });
|
|
86
86
|
const plans = [];
|
|
87
87
|
for (const session of sessions) {
|
|
88
|
+
// A digest of every session is a few seconds of transcript decoding. Yield
|
|
89
|
+
// between sessions so the host keeps answering IPC while it runs: desktop
|
|
90
|
+
// hosts are supervised, and a multi-second block looks like a hang.
|
|
91
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
88
92
|
let records;
|
|
89
93
|
try {
|
|
90
94
|
records = readRecords(session.file);
|
|
@@ -104,8 +108,8 @@ export function planDigest({ sessionId, project, all, limit } = {}) {
|
|
|
104
108
|
return { home, plans };
|
|
105
109
|
}
|
|
106
110
|
|
|
107
|
-
export function applyDigest({ sessionId, project, all, limit } = {}) {
|
|
108
|
-
const { plans } = planDigest({ sessionId, project, all, limit });
|
|
111
|
+
export async function applyDigest({ sessionId, project, all, limit } = {}) {
|
|
112
|
+
const { plans } = await planDigest({ sessionId, project, all, limit });
|
|
109
113
|
const root = storeRoot();
|
|
110
114
|
const summary = { sessions: 0, runsAdded: 0, notesAdded: 0, skippedSessions: [] };
|
|
111
115
|
for (const plan of plans) {
|
|
@@ -126,15 +130,19 @@ export function applyDigest({ sessionId, project, all, limit } = {}) {
|
|
|
126
130
|
return { ...summary, storeDir: root, lastDigestAt: state.lastDigestAt };
|
|
127
131
|
}
|
|
128
132
|
|
|
129
|
-
function previewDigest({ sessionId, project, all, limit } = {}) {
|
|
130
|
-
const { plans } = planDigest({ sessionId, project, all, limit });
|
|
133
|
+
async function previewDigest({ sessionId, project, all, limit } = {}) {
|
|
134
|
+
const { plans } = await planDigest({ sessionId, project, all, limit });
|
|
131
135
|
const root = storeRoot();
|
|
132
136
|
const existingRuns = new Set(readAll(root, 'runs').map((record) => record.id));
|
|
133
|
-
const
|
|
137
|
+
const storedNotes = readAll(root, 'notes');
|
|
138
|
+
const existingNotes = new Set(storedNotes.map((record) => record.id));
|
|
139
|
+
// A note is the fact it states, not the exact wording: without this, a digest
|
|
140
|
+
// that only strips a "结论:" label would re-add every existing note.
|
|
141
|
+
const existingNoteKeys = new Set(storedNotes.map((record) => noteKey(record)));
|
|
134
142
|
const preview = plans.map((plan) => {
|
|
135
143
|
if (plan.error) return { sessionId: plan.sessionId, error: plan.error };
|
|
136
144
|
const newRuns = plan.runs.filter((run) => !existingRuns.has(run.id));
|
|
137
|
-
const newNotes = plan.notes.filter((note) => !existingNotes.has(note.id));
|
|
145
|
+
const newNotes = plan.notes.filter((note) => !existingNotes.has(note.id) && !existingNoteKeys.has(noteKey(note)));
|
|
138
146
|
const dropped = plan.runs.filter((run) => newRuns.includes(run) && !(run.conclusions?.length || run.errors?.length || run.commands?.length));
|
|
139
147
|
return {
|
|
140
148
|
sessionId: plan.sessionId,
|
|
@@ -342,6 +350,11 @@ function readBody(req) {
|
|
|
342
350
|
});
|
|
343
351
|
}
|
|
344
352
|
|
|
353
|
+
// A second digest while one is still running would read and rewrite the same
|
|
354
|
+
// JSONL files concurrently. The panel disables the button while it waits, but a
|
|
355
|
+
// second window (or a remote client) could still fire one.
|
|
356
|
+
let digestInFlight = false;
|
|
357
|
+
|
|
345
358
|
function makeRouteHandler() {
|
|
346
359
|
return async (req, res) => {
|
|
347
360
|
const url = new URL(req.url || '/', 'http://127.0.0.1');
|
|
@@ -373,8 +386,16 @@ function makeRouteHandler() {
|
|
|
373
386
|
const newest = listSessions({ home: homeDir(), all: true, limit: 1 })[0];
|
|
374
387
|
if (newest) request.sessionId = newest.id;
|
|
375
388
|
}
|
|
376
|
-
|
|
377
|
-
|
|
389
|
+
if (digestInFlight) {
|
|
390
|
+
return jsonResponse(res, 409, { ok: false, error: 'a digest is already running; wait for it to finish' });
|
|
391
|
+
}
|
|
392
|
+
digestInFlight = true;
|
|
393
|
+
try {
|
|
394
|
+
const payload = dryRun ? await previewDigest(request) : await applyDigest(request);
|
|
395
|
+
return jsonResponse(res, 200, { ok: true, ...payload });
|
|
396
|
+
} finally {
|
|
397
|
+
digestInFlight = false;
|
|
398
|
+
}
|
|
378
399
|
}
|
|
379
400
|
if (route === '/remember' && req.method === 'POST') {
|
|
380
401
|
const body = await readBody(req);
|
|
@@ -444,7 +465,7 @@ export function apply(ctx, config = {}) {
|
|
|
444
465
|
// scope to every session under a big project would be a surprise. Explicit
|
|
445
466
|
// sessionId/project/all win; otherwise the newest session is previewed.
|
|
446
467
|
const request = { ...args, project: args.project || '' };
|
|
447
|
-
return args.dryRun === false ? applyDigest(request) : previewDigest(request);
|
|
468
|
+
return args.dryRun === false ? await applyDigest(request) : await previewDigest(request);
|
|
448
469
|
},
|
|
449
470
|
output: textOutput(),
|
|
450
471
|
},
|
package/lib/digest.js
CHANGED
|
@@ -1,268 +1,301 @@
|
|
|
1
|
-
// Deterministic turn digester.
|
|
2
|
-
//
|
|
3
|
-
// Reads already-parsed transcript records and produces two kinds of records:
|
|
4
|
-
// runs — one per turn: what the user asked, which tools ran, what failed,
|
|
5
|
-
// which files were touched, what conclusion the assistant reached
|
|
6
|
-
// notes — durable one-liners worth recalling later (decisions, error->fix)
|
|
7
|
-
//
|
|
8
|
-
// No model call happens here on purpose: pressing the button is cheap, offline
|
|
9
|
-
// and repeatable, and the same transcript always yields the same records.
|
|
10
|
-
import { contentText } from './transcript.js';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
//
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
//
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
current
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
}
|
|
1
|
+
// Deterministic turn digester.
|
|
2
|
+
//
|
|
3
|
+
// Reads already-parsed transcript records and produces two kinds of records:
|
|
4
|
+
// runs — one per turn: what the user asked, which tools ran, what failed,
|
|
5
|
+
// which files were touched, what conclusion the assistant reached
|
|
6
|
+
// notes — durable one-liners worth recalling later (decisions, error->fix)
|
|
7
|
+
//
|
|
8
|
+
// No model call happens here on purpose: pressing the button is cheap, offline
|
|
9
|
+
// and repeatable, and the same transcript always yields the same records.
|
|
10
|
+
import { contentText } from './transcript.js';
|
|
11
|
+
|
|
12
|
+
// compact-checkpoint: after a compaction DSH appends a synthetic user message
|
|
13
|
+
// carrying the English checkpoint preamble. It is not something the user said,
|
|
14
|
+
// so it must never become a run's "user" text.
|
|
15
|
+
const PSEUDO_USER_KINDS = new Set(['time-context', 'system-reminder', 'attachment', 'job-notification', 'compact-checkpoint']);
|
|
16
|
+
const ERROR_RE = /(?:^|\n)\s*(?:error|fatal|exception|failed|failure|cannot|can't|unable|not found|not recognized|denied|traceback|exit code [1-9]|ENOENT|EACCES|EPERM|TypeError|SyntaxError|ReferenceError)/i;
|
|
17
|
+
const DECISION_RE = /(?:结论|根因|原因|决定|改用|选择|改成|必须|不要|注意[::]|修复了|验证通过|原因是|chose|decided|because|root cause|instead of|must not)/i;
|
|
18
|
+
const PATH_KEYS = ['file_path', 'path', 'target', 'deckPath', 'pptxPath', 'filePath', 'outputDir', 'cwd', 'workdir'];
|
|
19
|
+
const COMMAND_KEYS = ['command', 'cmd', 'script', 'pattern', 'query', 'url'];
|
|
20
|
+
|
|
21
|
+
function hash32(input) {
|
|
22
|
+
let h = 0x811c9dc5;
|
|
23
|
+
for (let i = 0; i < input.length; i += 1) {
|
|
24
|
+
h ^= input.charCodeAt(i);
|
|
25
|
+
h = Math.imul(h, 0x01000193) >>> 0;
|
|
26
|
+
}
|
|
27
|
+
return h.toString(36);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function clip(text, max) {
|
|
31
|
+
const value = String(text ?? '').replace(/\s+/g, ' ').trim();
|
|
32
|
+
return value.length > max ? `${value.slice(0, max - 1)}…` : value;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function parseArgs(raw) {
|
|
36
|
+
if (!raw) return {};
|
|
37
|
+
if (typeof raw === 'object') return raw;
|
|
38
|
+
try {
|
|
39
|
+
const parsed = JSON.parse(String(raw));
|
|
40
|
+
return parsed && typeof parsed === 'object' ? parsed : {};
|
|
41
|
+
} catch {
|
|
42
|
+
return {};
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function describeCall(call) {
|
|
47
|
+
const args = parseArgs(call.args);
|
|
48
|
+
const detail = COMMAND_KEYS.map((key) => args[key]).find((value) => typeof value === 'string') || '';
|
|
49
|
+
const file = PATH_KEYS.map((key) => args[key]).find((value) => typeof value === 'string') || '';
|
|
50
|
+
return {
|
|
51
|
+
name: String(call.name || 'unknown'),
|
|
52
|
+
detail: clip(detail, 200),
|
|
53
|
+
file: file ? clip(file, 160) : '',
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function resultError(text) {
|
|
58
|
+
if (!text) return '';
|
|
59
|
+
const lines = String(text).split('\n');
|
|
60
|
+
for (const line of lines) {
|
|
61
|
+
if (ERROR_RE.test(line)) return clip(line, 180);
|
|
62
|
+
}
|
|
63
|
+
return '';
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Titles keep the words, not the markup: "**结论**" reads worse in a list than
|
|
67
|
+
// "结论".
|
|
68
|
+
function stripMd(text) {
|
|
69
|
+
return String(text || '').replace(/\*\*|__|`/g, '').trim();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// A heading is a label, not a finding. "必须重写的只有三块:" and "**③ 两个必须改的点**"
|
|
73
|
+
// both match DECISION_RE, and both say nothing on their own — they were being
|
|
74
|
+
// stored as notes whose body was identical to their title.
|
|
75
|
+
function isStatement(part) {
|
|
76
|
+
const bare = stripMd(part).replace(/^#+\s*/, '').replace(/\s+/g, ' ').trim();
|
|
77
|
+
if (bare.length < 14) return false; // too short to carry a claim
|
|
78
|
+
if (/[::,,、;;]\s*$/.test(bare)) return false; // lead-in cut off mid-thought
|
|
79
|
+
if (/^[*_]{1,3}[^*_]{1,40}[*_]{1,3}$/.test(String(part).trim())) return false; // pure bold label
|
|
80
|
+
if (!/[。!?…!?]/.test(bare) && bare.length < 26) return false; // bare noun phrase, no predicate
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// The list row shows the head clause; the body keeps the whole sentence, so a
|
|
85
|
+
// note no longer reads "title === body".
|
|
86
|
+
function noteTitle(sentence) {
|
|
87
|
+
const bare = stripMd(sentence).replace(/\s+/g, ' ').trim();
|
|
88
|
+
const head = bare.split(/[,,。;;::]/)[0].trim();
|
|
89
|
+
return clip(head.length >= 8 ? head : bare, 42);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function sentences(text) {
|
|
93
|
+
return String(text || '')
|
|
94
|
+
// Split on CJK terminators anywhere, and on ASCII ". ! ?" only when a space
|
|
95
|
+
// follows — otherwise "0.216?" would break into "…升到 0." + "216?".
|
|
96
|
+
.split(/(?<=[。!?;;])\s*|\n+|(?<=[.!?])\s+/)
|
|
97
|
+
.map((part) => part.replace(/^[-–—•·*]\s+/, '').replace(/\s+/g, ' ').trim())
|
|
98
|
+
.filter((part) => part.length >= 12 && part.length <= 240)
|
|
99
|
+
// Markdown furniture is not a finding: table rows, tree glyphs and headings
|
|
100
|
+
// would otherwise dominate the note list.
|
|
101
|
+
.filter((part) => !/^[|├└│┌┐┘─━=+#>`]/.test(part))
|
|
102
|
+
.filter((part) => (part.match(/\|/g) || []).length < 2)
|
|
103
|
+
// A question is an open loop, not a decision: it would pollute recall with
|
|
104
|
+
// "要不要我也顺便…" lines that never became true.
|
|
105
|
+
.filter((part) => !/[??]["'”’)\]]*\s*$/.test(part))
|
|
106
|
+
// Talking about talking is not a finding either: these lead-ins are the
|
|
107
|
+
// narrator clearing their throat before the actual statement.
|
|
108
|
+
.filter((part) => !/^(我先说|我先讲|我直接说|先说结论|简单说|另外|补充一?点)/.test(part))
|
|
109
|
+
// "结论是:X" keeps X — the label carries no information once the note is
|
|
110
|
+
// already a decision. Strip markup first: "**结论**:X" only reveals its
|
|
111
|
+
// label after the asterisks are gone.
|
|
112
|
+
.map((part) =>
|
|
113
|
+
stripMd(part)
|
|
114
|
+
.replace(/^#+\s*/, '')
|
|
115
|
+
.replace(/^(结论先说|先说结论|结论是|结论|判断|要点|一句话(?:总结|说|讲)?)[::]\s*/, '')
|
|
116
|
+
.trim(),
|
|
117
|
+
)
|
|
118
|
+
// Label-shaped lines (headings, "只有三块:" lead-ins) pass DECISION_RE but
|
|
119
|
+
// carry no claim; keeping them made the note list look like a table of
|
|
120
|
+
// contents instead of a memory.
|
|
121
|
+
.filter(isStatement);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function pickConclusions(assistantTexts) {
|
|
125
|
+
const seen = new Set();
|
|
126
|
+
const out = [];
|
|
127
|
+
for (const text of assistantTexts) {
|
|
128
|
+
for (const sentence of sentences(text)) {
|
|
129
|
+
if (!DECISION_RE.test(sentence)) continue;
|
|
130
|
+
const key = sentence.slice(0, 60);
|
|
131
|
+
if (seen.has(key)) continue;
|
|
132
|
+
seen.add(key);
|
|
133
|
+
out.push(sentence);
|
|
134
|
+
if (out.length >= 3) return out;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return out;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function tokenish(text) {
|
|
141
|
+
return Math.max(1, Math.round(String(text || '').length / 3.5));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// records -> { runs, notes, stats }
|
|
145
|
+
export function digestRecords(records, meta) {
|
|
146
|
+
const project = meta.project || '';
|
|
147
|
+
const sessionId = meta.sessionId;
|
|
148
|
+
const turns = [];
|
|
149
|
+
let current = null;
|
|
150
|
+
|
|
151
|
+
const flush = () => {
|
|
152
|
+
if (current && (current.userText || current.calls.length)) turns.push(current);
|
|
153
|
+
current = null;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
for (const record of records) {
|
|
157
|
+
const type = record?.type;
|
|
158
|
+
const data = record?.data || {};
|
|
159
|
+
if (type === 'turn/start') {
|
|
160
|
+
flush();
|
|
161
|
+
current = {
|
|
162
|
+
turn: data.turn ?? turns.length + 1,
|
|
163
|
+
at: record.time || Date.now(),
|
|
164
|
+
userText: '',
|
|
165
|
+
assistantTexts: [],
|
|
166
|
+
calls: [],
|
|
167
|
+
errors: [],
|
|
168
|
+
files: new Set(),
|
|
169
|
+
};
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
if (!current) {
|
|
173
|
+
if (type !== 'user/message') continue;
|
|
174
|
+
current = { turn: 1, at: record.time || Date.now(), userText: '', assistantTexts: [], calls: [], errors: [], files: new Set() };
|
|
175
|
+
}
|
|
176
|
+
if (type === 'user/message') {
|
|
177
|
+
const kind = data.source?.kind;
|
|
178
|
+
if (kind && PSEUDO_USER_KINDS.has(kind)) continue;
|
|
179
|
+
const text = contentText(data.content);
|
|
180
|
+
if (text && !current.userText) current.userText = clip(text, 400);
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
if (type === 'assistant/message') {
|
|
184
|
+
const content = data.message?.content ?? data.content;
|
|
185
|
+
const text = Array.isArray(content)
|
|
186
|
+
? content.filter((part) => part?.type === 'text' && typeof part.text === 'string').map((part) => part.text).join('\n')
|
|
187
|
+
: '';
|
|
188
|
+
if (text.trim()) current.assistantTexts.push(text.trim());
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
if (type === 'tool/call') {
|
|
192
|
+
const described = describeCall({ name: data.name, args: data.arguments });
|
|
193
|
+
if (described.file) current.files.add(described.file);
|
|
194
|
+
current.calls.push({ ...described, step: data.step, callId: data.callId });
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
if (type === 'tool/result') {
|
|
198
|
+
const call = current.calls.find((entry) => entry.callId === data.toolCallId);
|
|
199
|
+
const text = contentText(data.message?.content ?? data.content);
|
|
200
|
+
const error = data.isError ? clip(text, 180) || 'tool reported an error' : resultError(text);
|
|
201
|
+
if (call) call.ok = !error;
|
|
202
|
+
if (error) {
|
|
203
|
+
current.errors.push({ tool: call?.name || 'tool', signature: error });
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
flush();
|
|
208
|
+
|
|
209
|
+
const runs = [];
|
|
210
|
+
const notes = [];
|
|
211
|
+
for (const turn of turns) {
|
|
212
|
+
const conclusions = pickConclusions(turn.assistantTexts);
|
|
213
|
+
const commands = turn.calls.filter((call) => call.detail).slice(0, 12).map((call) => `${call.name}: ${call.detail}`);
|
|
214
|
+
const files = [...turn.files].slice(0, 12);
|
|
215
|
+
const errors = turn.errors.slice(0, 6);
|
|
216
|
+
const runId = `run_${hash32(`${sessionId}:${turn.turn}`)}`;
|
|
217
|
+
const summary = conclusions.length
|
|
218
|
+
? conclusions
|
|
219
|
+
: turn.assistantTexts.map((text) => clip(text, 200)).filter(Boolean).slice(-1);
|
|
220
|
+
runs.push({
|
|
221
|
+
kind: 'run',
|
|
222
|
+
id: runId,
|
|
223
|
+
sessionId,
|
|
224
|
+
project,
|
|
225
|
+
turn: turn.turn,
|
|
226
|
+
at: turn.at,
|
|
227
|
+
user: turn.userText,
|
|
228
|
+
actions: turn.calls.slice(0, 20).map((call) => ({ name: call.name, detail: call.detail, file: call.file, ok: call.ok !== false })),
|
|
229
|
+
commands,
|
|
230
|
+
files,
|
|
231
|
+
errors,
|
|
232
|
+
conclusions,
|
|
233
|
+
summary,
|
|
234
|
+
tokens: tokenish([turn.userText, ...summary, ...commands].join(' ')),
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
for (const conclusion of conclusions.slice(0, 2)) {
|
|
238
|
+
notes.push({
|
|
239
|
+
kind: 'note',
|
|
240
|
+
noteKind: 'decision',
|
|
241
|
+
id: `note_${hash32(`decision:${conclusion.slice(0, 80)}`)}`,
|
|
242
|
+
sessionId,
|
|
243
|
+
project,
|
|
244
|
+
title: noteTitle(conclusion),
|
|
245
|
+
body: stripMd(conclusion),
|
|
246
|
+
tags: ['decision', ...files.slice(0, 3)],
|
|
247
|
+
at: turn.at,
|
|
248
|
+
origin: 'digest',
|
|
249
|
+
weight: 0.6,
|
|
250
|
+
sessionsSeen: 1,
|
|
251
|
+
sessionsSeenList: [sessionId],
|
|
252
|
+
timesSeen: 1,
|
|
253
|
+
active: true,
|
|
254
|
+
staleReason: null,
|
|
255
|
+
conflicts: [],
|
|
256
|
+
runId,
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// error -> later successful use of the same tool in this turn
|
|
261
|
+
for (const failure of errors) {
|
|
262
|
+
const later = turn.calls.find((call) => call.name === failure.tool && call.ok === true && call.detail);
|
|
263
|
+
if (!later) continue;
|
|
264
|
+
const title = `${failure.tool} 报错后改用:${clip(later.detail, 60)}`;
|
|
265
|
+
notes.push({
|
|
266
|
+
kind: 'note',
|
|
267
|
+
noteKind: 'fix',
|
|
268
|
+
id: `note_${hash32(`fix:${failure.signature.slice(0, 80)}:${later.detail.slice(0, 60)}`)}`,
|
|
269
|
+
sessionId,
|
|
270
|
+
project,
|
|
271
|
+
title: clip(stripMd(title), 100),
|
|
272
|
+
body: `失败:${failure.signature}\n可行:${later.detail}`,
|
|
273
|
+
tags: ['fix', failure.tool, ...files.slice(0, 2)],
|
|
274
|
+
at: turn.at,
|
|
275
|
+
origin: 'digest',
|
|
276
|
+
weight: 0.7,
|
|
277
|
+
sessionsSeen: 1,
|
|
278
|
+
sessionsSeenList: [sessionId],
|
|
279
|
+
timesSeen: 1,
|
|
280
|
+
active: true,
|
|
281
|
+
staleReason: null,
|
|
282
|
+
conflicts: [],
|
|
283
|
+
runId,
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return {
|
|
289
|
+
runs,
|
|
290
|
+
notes,
|
|
291
|
+
stats: {
|
|
292
|
+
sessionId,
|
|
293
|
+
project,
|
|
294
|
+
turns: turns.length,
|
|
295
|
+
runs: runs.length,
|
|
296
|
+
notes: notes.length,
|
|
297
|
+
errors: runs.reduce((sum, run) => sum + run.errors.length, 0),
|
|
298
|
+
tokens: runs.reduce((sum, run) => sum + run.tokens, 0),
|
|
299
|
+
},
|
|
300
|
+
};
|
|
301
|
+
}
|
package/lib/store.js
CHANGED
|
@@ -77,17 +77,42 @@ export function mergeRuns(root, runs) {
|
|
|
77
77
|
return { added: fresh.length, total: existing.size };
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
+
// Content identity for a note: what it says, not how it was spelled. Digest
|
|
81
|
+
// formatting changes (stripping a "结论:" label, dropping bold markers) must not
|
|
82
|
+
// turn an existing fact into a "new" note, so the store also looks notes up by
|
|
83
|
+
// this key.
|
|
84
|
+
export function noteKey(note) {
|
|
85
|
+
const body = String(note?.body || note?.title || '')
|
|
86
|
+
.replace(/\*\*|__|`/g, '')
|
|
87
|
+
.replace(/^#+\s*/, '')
|
|
88
|
+
.replace(/^(结论先说|先说结论|结论是|结论|判断|要点|一句话(?:总结|说|讲)?)[::]\s*/, '')
|
|
89
|
+
.replace(/\s+/g, ' ')
|
|
90
|
+
.trim();
|
|
91
|
+
return `${note?.noteKind || 'note'}|${body}`;
|
|
92
|
+
}
|
|
93
|
+
|
|
80
94
|
export function mergeNotes(root, notes) {
|
|
81
95
|
const existing = new Map(readAll(root, 'notes').map((n) => [n.id, n]));
|
|
96
|
+
const byKey = new Map();
|
|
97
|
+
for (const note of existing.values()) {
|
|
98
|
+
const key = noteKey(note);
|
|
99
|
+
if (!byKey.has(key)) byKey.set(key, note);
|
|
100
|
+
}
|
|
82
101
|
const fresh = [];
|
|
83
102
|
const touched = [];
|
|
84
103
|
for (const note of notes) {
|
|
85
|
-
const
|
|
104
|
+
const key = noteKey(note);
|
|
105
|
+
const prior = existing.get(note.id) || byKey.get(key);
|
|
86
106
|
if (!prior) {
|
|
87
107
|
existing.set(note.id, note);
|
|
108
|
+
byKey.set(key, note);
|
|
88
109
|
fresh.push(note);
|
|
89
110
|
continue;
|
|
90
111
|
}
|
|
112
|
+
// Keep the stored id (other records may point at it) but adopt the newer
|
|
113
|
+
// wording, which is what the list and the search index read.
|
|
114
|
+
if (prior.id !== note.id && note.title) prior.title = note.title;
|
|
115
|
+
if (prior.id !== note.id && note.body) prior.body = note.body;
|
|
91
116
|
if (!prior.sessionsSeenList?.includes(note.sessionId)) {
|
|
92
117
|
prior.sessionsSeenList = [...(prior.sessionsSeenList || []), note.sessionId];
|
|
93
118
|
prior.sessionsSeen = (prior.sessionsSeen || 1) + 1;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-rosemary-memory",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Auditable cross-session memory for DeepSeek Harness: digest finished turns into runs and notes deterministically, compress them by explainable weights, recall them by exact match in any session. No server, no model calls, no vectors.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -59,6 +59,7 @@
|
|
|
59
59
|
"access": "public"
|
|
60
60
|
},
|
|
61
61
|
"engines": {
|
|
62
|
-
"node": ">=22.15.0"
|
|
62
|
+
"node": ">=22.15.0",
|
|
63
|
+
"dsh": ">=0.2.1-rc.2 <0.3.0-0"
|
|
63
64
|
}
|
|
64
65
|
}
|