@restregular/dsh-memory 0.17.0 → 0.17.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 CHANGED
@@ -2,6 +2,17 @@
2
2
 
3
3
  本项目的版本记录。安装 id 与 npm 版本一一对应。
4
4
 
5
+ ## 0.17.1 — memory_save 成功即把这条记忆记为「正文已注入」
6
+
7
+ - **现象**:在同一个会话里 `memory_save` 成功后,下一轮(或同一轮后续步骤)的召回会把刚保存的那条记忆再注入一遍,然后再被会话级去重标记。实况样本:某会话 turn 1 末尾保存一条发布记录,turn 2 第一步的召回又把它作为「相关记忆」塞回上下文,之后同一条再也不出现。
8
+ - **根因**:0.16.1 只把 `memory_get` 取正文记成 `content_injected`;`memory_save` 的正文其实也进了上下文 —— 写入语句就是模型产出的 `tool/call` 参数 —— 但保存路径没有写会话注入状态,于是下一次召回把它当成「还没注入过的新记忆」。
9
+ - **修复**:`session/event` 里跟踪 `memory_save` 的 `callId`;`tool/result` 成功、且能从结果里解析出记录 id 时(`getByIds` 再校验存在性),把该记录按 `updatedAt` 记为 `content_injected`。
10
+ - 只按 UUID 形态解析记录 id,不依赖结果文案,本地化 / 措辞改动不会让去重失效。
11
+ - 保存失败 / 被拒绝不标记;记录之后被更新(`updatedAt` 变化)仍会重新评估并重新注入。
12
+ - 去重标记独立于 `autoRecall` 开关,和 `memory_get` 的口径一致;compaction 时随会话状态一起清空。
13
+ - **审计**:保存成功时写一条 `kind=lifecycle / stage=content_pinned / reasonCode=saved_in_context` 事件(带 `operationId` = 这次 save 的 callId、`selected.contentIds`),面板按记忆 id 能查到「它为什么后面被去重跳过」。
14
+ - 回归测试:结果解析(保存 / 去重命中 / 非中文文案 / 无 id)、成功后下一次召回被去重且审计可见、保存失败不标记、版本变化重新注入、去重命中同样标记。
15
+
5
16
  ## 0.17.0 — 会话去重状态持久化到工作区 `.dsh/dsh-memory`
6
17
 
7
18
  - **状态现在可以跨插件重载 / 宿主重启恢复**:`InjectionState` 可按会话挂一个持久化 store,首次接触会话时读一次盘,之后写入由 store 合并。
package/README.md CHANGED
@@ -239,6 +239,7 @@ memory_forget({ "id": "3f2a9c1d" }) // 按内容删:memory_forget({ q
239
239
 
240
240
  - 召回按 **sessionId + 记忆 id / 版本** 去重;只给过标签的记录,任务命中后可补正文;内容更新会重新评估。
241
241
  - `memory_get` 取到正文后,这条记忆会记成「正文已注入」;同一会话后续的普通召回 / 摘要 / 自动召回都不会再把同一段正文塞一遍。只取 `summary` / `tags` / 元数据不算正文,仍可被召回。
242
+ - `memory_save` **保存成功**同样算「正文已在上下文里」:写入语句(`content`)就是模型自己产出的 tool/call 参数,因此这条记录会立刻记成「正文已注入」,本会话后续的召回 / 摘要不会把刚保存的记忆原样再注入一次(去重命中既有记录时也一样)。保存失败不标记;记录之后被更新(`updatedAt` 变化)仍会重新评估。审计里会留一条 `content_pinned / saved_in_context` 的 lifecycle 事件,说明后面的「去重跳过」从哪来。
242
243
  - **去重状态持久化**:状态写到会话工作区的插件目录
243
244
  `<cwd>/.dsh/dsh-memory/sessions/<session>.jsonl`,插件重载 / 宿主重启后自动恢复;只有首次接触该会话时读一次盘。
244
245
  - 只存记录 id、状态、版本(`updatedAt`)和时间戳,**不存正文,也不存查询**。
@@ -67,6 +67,7 @@ import {
67
67
  highRiskSignal,
68
68
  memoryGetRequestsContent,
69
69
  memoryGetResultIds,
70
+ memorySaveResultIds,
70
71
  textTagSignal,
71
72
  todoText,
72
73
  toolCallDescription,
@@ -149,6 +150,8 @@ export function apply(ctx , config = {}) {
149
150
  const callMeta = new Map ()
150
151
  /** memory_get 调用 → 这次是否取正文;tool/result 到达后用来写 content_injected。 */
151
152
  const memoryGetCalls = new Map ()
153
+ /** memory_save 调用中还没等到结果的 callId;成功后把被保存的记录记为 content_injected。 */
154
+ const memorySaveCalls = new Set ()
152
155
  const sessionScopes = new Map ()
153
156
  const sessionAutoScopes = new Map ()
154
157
  /** 本轮已经发生过的 signal 驱动召回:每轮最多一次,同 tag/query 也跳过。 */
@@ -290,6 +293,54 @@ export function apply(ctx , config = {}) {
290
293
  } catch { /* 去重状态是尽力而为,不能影响工具结果 */ }
291
294
  }
292
295
 
296
+ /** 记住这次 memory_save 的 callId;结果成功到达后才知道记录 id。 */
297
+ const rememberMemorySaveCall = (callId ) => {
298
+ if (typeof callId !== 'string' || callId === '') return
299
+ memorySaveCalls.add(callId)
300
+ if (memorySaveCalls.size > 500) {
301
+ const oldest = memorySaveCalls.values().next()
302
+ if (!oldest.done) memorySaveCalls.delete(oldest.value)
303
+ }
304
+ }
305
+
306
+ /**
307
+ * memory_save 成功 → 这条记忆的正文**已经由模型自己写进上下文**(写入语句就在
308
+ * tool/call 的参数里),和 memory_get 取正文同一口径记为 content_injected:
309
+ * 本会话后续的摘要 / 召回不再把刚写进去的内容原样塞回来(0.17.0 之前的实况是
310
+ * 下一轮 recall 会把保存的记忆再注入一遍,然后才被去重)。
311
+ *
312
+ * 只在保存成功时标记:失败 / 被拒绝的保存没有可信的记录 id,也没有"正文已在
313
+ * 上下文里"这一事实。与 memory_get 一样,去重标记独立于 autoRecall 开关。
314
+ */
315
+ const markMemorySaveContent = (
316
+ sessionId ,
317
+ memory ,
318
+ event ,
319
+ context = {},
320
+ ) => {
321
+ try {
322
+ const ids = memorySaveResultIds(event)
323
+ if (ids.length === 0 || typeof memory.getByIds !== 'function') return
324
+ const found = memory.getByIds(ids)
325
+ if (found.length === 0) return
326
+ injectionState.markContent(sessionId, found.map((record) => ({ id: record.id, updatedAt: record.updatedAt })))
327
+ // 这条状态变化要能从审计里查到,否则后续 recall 的 dedup 列表会显得没来由。
328
+ recordAudit({
329
+ kind: 'lifecycle',
330
+ channel: 'lifecycle',
331
+ stage: 'content_pinned',
332
+ outcome: 'success',
333
+ reasonCode: 'saved_in_context',
334
+ trigger: 'memory_save',
335
+ sessionId,
336
+ ...(context.callId !== undefined ? { operationId: context.callId } : {}),
337
+ ...(context.turn !== undefined ? { turn: context.turn } : {}),
338
+ ...(context.step !== undefined ? { step: context.step } : {}),
339
+ selected: { contentIds: found.map((record) => record.id), hintIds: [], count: found.length },
340
+ })
341
+ } catch { /* 去重状态是尽力而为,不能影响工具结果 */ }
342
+ }
343
+
293
344
  /** 记录本会话最近用过的工具名,给流程索引做触发词匹配。 */
294
345
  const rememberProcedureTool = (sessionId , tool ) => {
295
346
  const recent = procedureTools.get(sessionId) ?? []
@@ -511,6 +562,7 @@ export function apply(ctx , config = {}) {
511
562
  for (const key of tagCache.keys()) if (key.startsWith(`${sessionId}|`)) tagCache.delete(key)
512
563
  callMeta.clear()
513
564
  memoryGetCalls.clear()
565
+ memorySaveCalls.clear()
514
566
  return
515
567
  }
516
568
 
@@ -518,13 +570,24 @@ export function apply(ctx , config = {}) {
518
570
  // 普通每轮召回 / 摘要仍会注入同一段正文。这里在 autoRecall 闸门前记录。
519
571
  if (type === 'tool/call' && data.name === 'memory_get') {
520
572
  rememberMemoryGetCall(data.callId, memoryGetRequestsContent(data.arguments))
573
+ } else if (type === 'tool/call' && data.name === 'memory_save') {
574
+ rememberMemorySaveCall(data.callId)
521
575
  } else if (type === 'tool/result') {
522
576
  const callId = (data.message )?.source?.callId
523
577
  if (typeof callId === 'string') {
524
578
  const takesContent = memoryGetCalls.get(callId)
525
579
  memoryGetCalls.delete(callId)
526
- if (takesContent === true && memory !== undefined && !toolResultFailure(event).isError) {
580
+ const isError = toolResultFailure(event).isError
581
+ if (takesContent === true && memory !== undefined && !isError) {
527
582
  markMemoryGetContent(sessionId, memory, event)
583
+ } else if (memorySaveCalls.delete(callId) && memory !== undefined && !isError) {
584
+ const turn = typeof data.turn === 'number' ? data.turn : undefined
585
+ const step = typeof data.step === 'number' ? data.step : undefined
586
+ markMemorySaveContent(sessionId, memory, event, {
587
+ callId,
588
+ ...(turn !== undefined ? { turn } : {}),
589
+ ...(step !== undefined ? { step } : {}),
590
+ })
528
591
  }
529
592
  }
530
593
  }
@@ -420,6 +420,36 @@ export function memoryGetResultIds(event ) {
420
420
  return ids.slice(0, 50)
421
421
  }
422
422
 
423
+ /** 记录 id 的形态:`newId()` 生成 randomUUID,这里只认完整 UUID。 */
424
+ const RECORD_ID_PATTERN = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi
425
+
426
+ /**
427
+ * 从 `memory_save` 的 tool/result 里取这次写入 / 去重命中的记录 id。
428
+ *
429
+ * 工具结果的渲染只有两种形态:「已保存记忆 <id> @ <scope>」与
430
+ * 「已更新既有记忆(内容相同,去重):<id> @ <scope>」,id 是 randomUUID。
431
+ * 这里**不按文案匹配**(本地化/措辞改动不该让去重失效),只按 UUID 形态取候选;
432
+ * 调用方还会用 `getByIds` 校验一次存在性,所以 scope 名字里的偶然片段不会误伤。
433
+ */
434
+ export function memorySaveResultIds(event ) {
435
+ const data = (event )?.data
436
+ const message = data?.message
437
+ const text = Array.isArray(message?.content)
438
+ ? message.content.filter((block) => block?.type === 'text' && typeof block.text === 'string').map((block) => block.text ).join('\n')
439
+ : ''
440
+ if (text === '') return []
441
+ const seen = new Set ()
442
+ const ids = []
443
+ for (const match of text.matchAll(RECORD_ID_PATTERN)) {
444
+ const id = match[0]
445
+ if (seen.has(id)) continue
446
+ seen.add(id)
447
+ ids.push(id)
448
+ if (ids.length >= 10) break
449
+ }
450
+ return ids
451
+ }
452
+
423
453
  /**
424
454
  * 每个会话只保留**最新一条未消费**的信号;相同 key 不重复排队。
425
455
  * `take()` 消费后保留 lastKey,同一失败不会因为重复事件反复搜索。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@restregular/dsh-memory",
3
- "version": "0.17.0",
3
+ "version": "0.17.1",
4
4
  "description": "DeepSeek Harness 记忆系统(单包单入口):JSONL 存储 + 本地向量检索(BGE-M3)+ 五个模型工具 + 自动召回注入 + 管理面板。",
5
5
  "icon": "./icon.svg",
6
6
  "type": "module",