@restregular/dsh-memory 0.16.0 → 0.16.2

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,24 @@
2
2
 
3
3
  本项目的版本记录。安装 id 与 npm 版本一一对应。
4
4
 
5
+ ## 0.16.2 — memory_get 去重的审计与端到端覆盖
6
+
7
+ - **补审计缺口**:所有候选都被会话级去重时,`renderRecallBlock` 返回 `null`,`dedupIds` 没有写进结果,审计只能看到 `dedupSkipped` 数量、看不到具体跳过了哪些 id。现在 `dedupIds` 在“全部去重”和“预算省略”路径上也会返回。
8
+ - **端到端测试**:新增真实 `session/event` 链路测试(`tool/call` → `tool/result` → `injectionState` → 下一次 pre-step):
9
+ - `memory_get` 取正文后,下一次普通召回不再追加同一条消息,审计能列出被去重的 id;
10
+ - 记录 `updatedAt` 变化后,下一次召回重新注入新版本;
11
+ - 只取 `summary` 时不算正文,仍可通过召回补全文;
12
+ - 自动召回关闭时去重标记仍然生效。
13
+ - 语义保持不变:`memory_get` 默认和显式 `fields: ["content"]` 都显示完整正文;只有正文进入上下文才记为 `content_injected`。
14
+
15
+ ## 0.16.1 — memory_get 取正文也参与会话级去重
16
+
17
+ - **补上去重缺口**:0.16.0 把正文入口从 `memory_search` 移到 `memory_get` 后,工具读到的正文没有记进会话注入状态,后续普通召回 / 摘要 / 自动召回仍可能把同一条记忆再塞一遍。
18
+ - 现在 `memory_get` 的 `tool/result` 会被解析:`fields` 省略或包含 `content` 时,把结果里的记录 id 记为 `content_injected`(按 `updatedAt` 版本去重)。只取 `summary` / `tags` / 元数据时不标记,正文仍可被召回。
19
+ - 去重标记独立于「自动召回」开关:即使行为信号召回关闭,普通每轮召回也会跳过已取过正文的记录。
20
+ - 解析同时兼容渲染文本(`[id]` 行首)和结构化 JSON(`records[].id`);正文里普通的方括号文本不会被误认成 id,最终还会用 `getByIds` 校验一次。
21
+ - 回归测试:`fields` 是否含 content 的判定、文本/JSON 两种结果解析、写入 `content_injected` 后同会话召回被去重、版本变化允许重注入、另一会话不受影响。
22
+
5
23
  ## 0.16.0 — memory_search 只回印象,memory_get 按字段取
6
24
 
7
25
  - **`memory_search` 不再返回正文**:每条结果只给 8 位 id、作用域、类型、标签和一段短摘要(默认 160 字),避免一次检索把整篇记忆塞进上下文。摘要长度可用 `summaryChars` 调整(20~2000)。返回结构从 `content` 改为 `summary`,正文统一走 `memory_get`。
package/README.md CHANGED
@@ -238,6 +238,7 @@ memory_forget({ "id": "3f2a9c1d" }) // 按内容删:memory_forget({ q
238
238
  ### 去重与修正
239
239
 
240
240
  - 召回按 **sessionId + 记忆 id / 版本** 去重;只给过标签的记录,任务命中后可补正文;内容更新会重新评估。
241
+ - `memory_get` 取到正文后,这条记忆会记成「正文已注入」;同一会话后续的普通召回 / 摘要 / 自动召回都不会再把同一段正文塞一遍。只取 `summary` / `tags` / 元数据不算正文,仍可被召回。
241
242
  - 被 `supersedes` 指向的旧记录不再进入 `memory_search`、`memory_list`、摘要、规则段和自动召回;`memory_get(id)` 与面板仍可查历史。需要历史检索时 `memory_search({ includeSuperseded: true })`。
242
243
  - 摘要排除 `kind=rule`;规则段实际进入系统提示的 id 会写进会话注入状态,recall 不再重复注入同一条规则。
243
244
  - 同一轮最多发生一次 signal 驱动召回;同 tag / 同 query 的第二个信号只记一条 `signal_dropped` 审计。
@@ -63,6 +63,8 @@ import {
63
63
  failureSignal,
64
64
  goalText,
65
65
  highRiskSignal,
66
+ memoryGetRequestsContent,
67
+ memoryGetResultIds,
66
68
  textTagSignal,
67
69
  todoText,
68
70
  toolCallDescription,
@@ -127,6 +129,8 @@ export function apply(ctx , config = {}) {
127
129
  const audit = new InjectAuditStore()
128
130
  const signals = new BehaviorSignalStore()
129
131
  const callMeta = new Map ()
132
+ /** memory_get 调用 → 这次是否取正文;tool/result 到达后用来写 content_injected。 */
133
+ const memoryGetCalls = new Map ()
130
134
  const sessionScopes = new Map ()
131
135
  const sessionAutoScopes = new Map ()
132
136
  /** 本轮已经发生过的 signal 驱动召回:每轮最多一次,同 tag/query 也跳过。 */
@@ -201,6 +205,30 @@ export function apply(ctx , config = {}) {
201
205
  }
202
206
  }
203
207
 
208
+ /** 记住这次 memory_get 是否取正文;结果到达后无论 autoRecall 开关都会用到。 */
209
+ const rememberMemoryGetCall = (callId , content ) => {
210
+ if (typeof callId !== 'string' || callId === '') return
211
+ memoryGetCalls.set(callId, content)
212
+ if (memoryGetCalls.size > 500) {
213
+ const oldest = memoryGetCalls.keys().next()
214
+ if (!oldest.done) memoryGetCalls.delete(oldest.value)
215
+ }
216
+ }
217
+
218
+ /**
219
+ * memory_get 已把正文放进当前上下文 → 记为 content_injected,后续 recall/digest
220
+ * 的同一条记忆会被会话级去重跳过。只取 summary / tags / 元数据时不标记。
221
+ */
222
+ const markMemoryGetContent = (sessionId , memory , event ) => {
223
+ try {
224
+ const ids = memoryGetResultIds(event)
225
+ if (ids.length === 0 || typeof memory.getByIds !== 'function') return
226
+ const found = memory.getByIds(ids)
227
+ if (found.length === 0) return
228
+ injectionState.markContent(sessionId, found.map((record) => ({ id: record.id, updatedAt: record.updatedAt })))
229
+ } catch { /* 去重状态是尽力而为,不能影响工具结果 */ }
230
+ }
231
+
204
232
  /** 记录本会话最近用过的工具名,给流程索引做触发词匹配。 */
205
233
  const rememberProcedureTool = (sessionId , tool ) => {
206
234
  const recent = procedureTools.get(sessionId) ?? []
@@ -415,8 +443,24 @@ export function apply(ctx , config = {}) {
415
443
  procedureTools.delete(sessionId)
416
444
  for (const key of tagCache.keys()) if (key.startsWith(`${sessionId}|`)) tagCache.delete(key)
417
445
  callMeta.clear()
446
+ memoryGetCalls.clear()
418
447
  return
419
448
  }
449
+
450
+ // memory_get 的正文去重必须独立于 autoRecall 开关:哪怕行为信号召回关着,
451
+ // 普通每轮召回 / 摘要仍会注入同一段正文。这里在 autoRecall 闸门前记录。
452
+ if (type === 'tool/call' && data.name === 'memory_get') {
453
+ rememberMemoryGetCall(data.callId, memoryGetRequestsContent(data.arguments))
454
+ } else if (type === 'tool/result') {
455
+ const callId = (data.message )?.source?.callId
456
+ if (typeof callId === 'string') {
457
+ const takesContent = memoryGetCalls.get(callId)
458
+ memoryGetCalls.delete(callId)
459
+ if (takesContent === true && memory !== undefined && !toolResultFailure(event).isError) {
460
+ markMemoryGetContent(sessionId, memory, event)
461
+ }
462
+ }
463
+ }
420
464
  const settings = enabled ? settingsOf() : undefined
421
465
  const auto = settings?.autoRecall
422
466
  if (!auto?.enabled) return
@@ -37,6 +37,8 @@ export const defaultInjectConfig = {
37
37
 
38
38
                             
39
39
 
40
+                           
41
+
40
42
 
41
43
 
42
44
  /** 宿主里"按会话 id 找 live session"的那一小部分能力(拿不到就不做父会话召回)。 */
@@ -387,6 +389,8 @@ export class MemoryInjector {
387
389
  const block = renderRecallBlock(freshHits, cfg, optionsForRender)
388
390
  const base = {
389
391
  ids: [], candidates: candidates.length, dedupSkipped: deduplicated.length, updated, omitted: [], query,
392
+ // 全部候选都被去重时 block 为 null;把 id 留在结果里,审计才能解释"跳过了哪条"。
393
+ dedupIds: deduplicated,
390
394
  tokens: 0, rawHits: hits.length, tagFiltered, supersededExcluded, expiredExcluded,
391
395
  retrieval: { ok: true, count: hits.length },
392
396
  }
@@ -363,6 +363,63 @@ export function toolResultFailure(event )
363
363
  }
364
364
  }
365
365
 
366
+ /**
367
+ * `memory_get` 的这次调用是否会把正文放进上下文。
368
+ *
369
+ * 与工具默认值同一口径:`fields` 省略 = 默认含 `content`;显式给了 fields 时,
370
+ * 只有包含 `content` 才算。只取 summary / tags / 元数据不是正文,不参与正文去重。
371
+ */
372
+ export function memoryGetRequestsContent(args ) {
373
+ let parsed = args
374
+ if (typeof args === 'string') {
375
+ try { parsed = JSON.parse(args) } catch { return false }
376
+ }
377
+ if (parsed === null || typeof parsed !== 'object') return false
378
+ const fields = (parsed ).fields
379
+ if (fields === undefined || fields === null) return true
380
+ const items = Array.isArray(fields)
381
+ ? fields
382
+ : typeof fields === 'string' ? fields.split(/[,\s]+/) : []
383
+ return items.some((item) => String(item).trim().toLowerCase() === 'content')
384
+ }
385
+
386
+ /**
387
+ * 从 `memory_get` 的 tool/result 里取记录 id。
388
+ *
389
+ * 正常路径是渲染后的文本,每条以 `[<id>]` 开头;这里只认十六进制/横线组成的
390
+ * id,避免把正文里普通的方括号文本(如 `[提示]`)误当成记录。若宿主把结构化
391
+ * 结果直接放进文本块(JSON),也兼容 `records[].id`。真正的存在性还会由调用方
392
+ * 用 `getByIds` 再校验一次,前缀也能解析。
393
+ */
394
+ export function memoryGetResultIds(event ) {
395
+ const data = (event )?.data
396
+ const message = data?.message
397
+ const text = Array.isArray(message?.content)
398
+ ? message.content.filter((block) => block?.type === 'text' && typeof block.text === 'string').map((block) => block.text ).join('\n')
399
+ : ''
400
+ if (text === '') return []
401
+ const seen = new Set ()
402
+ const ids = []
403
+ const push = (value ) => {
404
+ if (typeof value !== 'string' || value === '' || seen.has(value)) return
405
+ seen.add(value)
406
+ ids.push(value)
407
+ }
408
+ // 兼容结构化输出:{"records":[{"id":"..."}], ...}
409
+ try {
410
+ const parsed = JSON.parse(text)
411
+ if (Array.isArray(parsed?.records)) {
412
+ for (const record of parsed.records) push((record )?.id)
413
+ if (ids.length > 0) return ids.slice(0, 50)
414
+ }
415
+ } catch { /* 不是 JSON,按渲染文本解析 */ }
416
+ for (const line of text.split(/\r?\n/)) {
417
+ const match = /^\[([0-9a-f][0-9a-f-]{7,})\]/i.exec(line)
418
+ if (match !== null) push(match[1] )
419
+ }
420
+ return ids.slice(0, 50)
421
+ }
422
+
366
423
  /**
367
424
  * 每个会话只保留**最新一条未消费**的信号;相同 key 不重复排队。
368
425
  * `take()` 消费后保留 lastKey,同一失败不会因为重复事件反复搜索。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@restregular/dsh-memory",
3
- "version": "0.16.0",
3
+ "version": "0.16.2",
4
4
  "description": "DeepSeek Harness 记忆系统(单包单入口):JSONL 存储 + 本地向量检索(BGE-M3)+ 五个模型工具 + 自动召回注入 + 管理面板。",
5
5
  "icon": "./icon.svg",
6
6
  "type": "module",