@restregular/dsh-memory 0.20.0 → 0.21.0

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.
@@ -43,8 +43,26 @@ import { canonicalWorkspacePath, normalizeScopeInput, resolveScopeRef } from '..
43
43
  /** 一次范围外审批的结局:允许一次 / 用户拒绝 / 超时未答 / 当前版本不支持审批。 */
44
44
 
45
45
 
46
- /** 被可见域挡住、没有返回给模型的原因(absolute 模式不会出现:一律视为不存在)。 */
47
-
46
+ /**
47
+ * 一次范围外审批的结论(0.21.0)。
48
+ *
49
+ * `allowed-once` 默认 = 本次请求**全部**允许;用户在审批卡里逐条勾选时,可带
50
+ * `allowedIds` 只允许其中一部分。老 provider 可能仍直接返回字符串,工具层兼容。
51
+ */
52
+
53
+
54
+                        
55
+
56
+
57
+
58
+ /** provider 的 requestOutOfScope 可以返回裸字符串(旧口径)或带 allowedIds 的对象。 */
59
+
60
+
61
+ /**
62
+ * 被可见域挡住、没有返回给模型的原因(absolute 模式不会出现:一律视为不存在)。
63
+ * `partial` = 用户只允许了其中几条,其余按拒绝处理。
64
+ */
65
+
48
66
 
49
67
 
50
68
 
@@ -76,9 +94,25 @@ import { canonicalWorkspacePath, normalizeScopeInput, resolveScopeRef } from '..
76
94
 
77
95
 
78
96
 
97
+ /**
98
+ * 一个会话的上下文去重视图(0.21.0)。由入口从注入半边提供的
99
+ * `memoryContextDedup` 服务接进来;没有时工具保持老行为(每次都带摘要)。
100
+ *
101
+ * 为什么要它:memory_search / memory_list 的摘要在同一会话里重复命中时
102
+ * 会把同样的内容反复塞回上下文。工具层只问“这条是否已经以同版本出现过”,
103
+ * 具体状态(摘要/正文/版本)由注入半边的 InjectionState 统一维护。
104
+ */
105
+
106
+                     
107
+
108
+       
109
+
110
+       
111
+
112
+
113
+
79
114
  /** 只有这一层需要的能力子集(provider 在宿主里提供,测试里可以 mock)。 */
80
-
81
-
115
+
82
116
 
83
117
 
84
118
 
@@ -103,8 +137,9 @@ import { canonicalWorkspacePath, normalizeScopeInput, resolveScopeRef } from '..
103
137
 
104
138
                              
105
139
                                  
140
+             
106
141
 
107
-
142
+
108
143
                            
109
144
 
110
145
                 
@@ -123,6 +158,12 @@ import { canonicalWorkspacePath, normalizeScopeInput, resolveScopeRef } from '..
123
158
 
124
159
              
125
160
 
161
+
162
+                   
163
+                    
164
+                        
165
+
166
+
126
167
 
127
168
 
128
169
  const SCOPE_DESCRIPTION =
@@ -250,7 +291,16 @@ function recordsToApprovalItems(
250
291
  * 结局必须可区分:允许一次 / 用户拒绝 / 超时 / 旧 provider 不支持审批。
251
292
  * `agent` 省略 = 不审批(工具层在拿不到会话时退回 provider 的护栏)。
252
293
  * 允许写/删时要带 `approvedItems` 之类的合成条目,浮层才显示得出来。
294
+ *
295
+ * 0.21.0:允许的条目可以是"全部"(allowedIds 省略)或用户在审批卡里逐条勾选的
296
+ * 一部分;后者通过 `allowedIds` 返回,调用方只放行这些 id。
253
297
  */
298
+
299
+
300
+             
301
+
302
+
303
+
254
304
  async function askOutOfScope(
255
305
  service ,
256
306
  agent ,
@@ -261,24 +311,39 @@ async function askOutOfScope(
261
311
  action ,
262
312
  signal ,
263
313
  ) {
264
- if (records.length === 0) return 'allowed-once'
265
- if (typeof service.requestOutOfScope !== 'function') return 'unavailable'
266
- if (signal?.aborted) return 'rejected'
314
+ if (records.length === 0) return { outcome: 'allowed-once' }
315
+ if (typeof service.requestOutOfScope !== 'function') return { outcome: 'unavailable' }
316
+ if (signal?.aborted) return { outcome: 'rejected' }
267
317
  try {
268
- const outcome = await service.requestOutOfScope(agent, {
318
+ const answer = await service.requestOutOfScope(agent, {
269
319
  tool,
270
320
  action,
271
321
  query,
272
322
  items: recordsToApprovalItems(policy, records),
273
323
  ...(signal !== undefined ? { signal } : {}),
274
324
  })
275
- if (outcome === 'allowed-once' || outcome === 'timeout' || outcome === 'unavailable') return outcome
276
- return 'rejected'
325
+ // 老 provider 直接返回字符串;新 provider 返回 { outcome, allowedIds }。
326
+ const decision = typeof answer === 'string' ? { outcome: answer } : answer ?? { outcome: 'unavailable' }
327
+ const outcome = decision.outcome === 'allowed-once' || decision.outcome === 'timeout' || decision.outcome === 'unavailable'
328
+ ? decision.outcome
329
+ : 'rejected'
330
+ const ids = outcome === 'allowed-once' && Array.isArray(decision.allowedIds)
331
+ ? new Set(decision.allowedIds.map(String))
332
+ : undefined
333
+ // 逐条勾选但一条都没选中 = 用户拒绝了这次访问。
334
+ if (ids !== undefined && ids.size === 0) return { outcome: 'rejected' }
335
+ return { outcome, ...(ids !== undefined ? { allowedIds: ids } : {}) }
277
336
  } catch {
278
- return 'unavailable'
337
+ return { outcome: 'unavailable' }
279
338
  }
280
339
  }
281
340
 
341
+ /** 这次审批是否允许这一条(没有 allowedIds = 全部允许)。 */
342
+ function verdictAllows(verdict , id ) {
343
+ if (verdict.outcome !== 'allowed-once') return false
344
+ return verdict.allowedIds === undefined || verdict.allowedIds.has(id)
345
+ }
346
+
282
347
  /** 审批没通过时给模型的真实原因(写/删路径直接抛这个错误)。 */
283
348
  function visibilityError(outcome , action , scope ) {
284
349
  const verb = action === 'write' ? '写入' : '删除'
@@ -480,7 +545,7 @@ export function memorySaveTool(opts )
480
545
  importance: typeof args.importance === 'number' ? args.importance : 0,
481
546
  updatedAt: Date.now(),
482
547
  }
483
- const outcome = await askOutOfScope(
548
+ const verdict = await askOutOfScope(
484
549
  service,
485
550
  agent,
486
551
  policy,
@@ -490,7 +555,9 @@ export function memorySaveTool(opts )
490
555
  'write',
491
556
  (exec )?.signal,
492
557
  )
493
- if (outcome !== 'allowed-once') throw visibilityError(outcome, 'write', target)
558
+ if (!verdictAllows(verdict, draft.id)) {
559
+ throw visibilityError(verdict.outcome === 'allowed-once' ? 'rejected' : verdict.outcome, 'write', target)
560
+ }
494
561
  approved = true
495
562
  }
496
563
 
@@ -534,6 +601,30 @@ export function summarize(content , chars = DEFAULT_SUMMARY_CHARS
534
601
  return flat.length <= chars ? flat : `${flat.slice(0, chars)}…`
535
602
  }
536
603
 
604
+ /**
605
+ * memory_search 的上下文去重(0.21.0):
606
+ * - 同一条记忆、同一个 updatedAt 版本已经出现在上下文里 → 摘要置空并标 deduped;
607
+ * - 没出现过的(含内容更新后的新版本)→ 照常带摘要,并记为“摘要已出现”。
608
+ * 注入半边不在时 `contextDedup` 返回 undefined,保持老行为。
609
+ */
610
+ function applySummaryDedupe(opts , exec , records ) {
611
+ if (records.length === 0) return
612
+ const dedup = opts.contextDedup?.(exec)
613
+ if (dedup === undefined) return
614
+ const seen = dedup.seen(records)
615
+ const newlyShown = []
616
+ for (const record of records) {
617
+ if (seen.has(record.id)) {
618
+ record.summary = ''
619
+ record.deduped = true
620
+ } else {
621
+ newlyShown.push(record)
622
+ }
623
+ }
624
+ // 只把这次真的带摘要返回的条目记为已出现;被省略的本来就是已出现。
625
+ if (newlyShown.length > 0) dedup.markSummary(newlyShown)
626
+ }
627
+
537
628
 
538
629
 
539
630
 
@@ -553,6 +644,11 @@ export function summarize(content , chars = DEFAULT_SUMMARY_CHARS
553
644
 
554
645
                     
555
646
 
647
+
648
+                           
649
+               
650
+
651
+
556
652
 
557
653
 
558
654
 
@@ -617,9 +713,9 @@ export function memorySearchTool(opts )
617
713
  const inside = hits.filter((hit) => recordAllowed(policy, hit.record.scope, hit.record.kind))
618
714
  const outside = hits.filter((hit) => !recordAllowed(policy, hit.record.scope, hit.record.kind))
619
715
  // undefined = 没有范围外命中 / 绝对限制(不审批);只有普通限制会真的问一次。
620
- let outcome
716
+ let verdict
621
717
  if (outside.length > 0 && level === 'normal') {
622
- outcome = await askOutOfScope(
718
+ verdict = await askOutOfScope(
623
719
  service,
624
720
  agent,
625
721
  policy,
@@ -630,8 +726,17 @@ export function memorySearchTool(opts )
630
726
  (exec )?.signal,
631
727
  )
632
728
  }
633
- const allowed = outside.length === 0 || outcome === 'allowed-once'
634
- const selected = allowed ? hits : inside
729
+ // verdict 为空 = 没有发起审批(绝对限制 / 没有范围外命中):范围外一律不放行。
730
+ const approvedOutside = verdict === undefined
731
+ ? []
732
+ : verdict.outcome !== 'allowed-once'
733
+ ? []
734
+ : verdict.allowedIds === undefined
735
+ ? outside
736
+ : outside.filter((hit) => verdict .allowedIds .has(hit.record.id))
737
+ const insideIds = new Set(inside.map((hit) => hit.record.id))
738
+ const approvedOutsideIds = new Set(approvedOutside.map((hit) => hit.record.id))
739
+ const selected = hits.filter((hit) => insideIds.has(hit.record.id) || approvedOutsideIds.has(hit.record.id))
635
740
  const result = {
636
741
  records: selected.map((h) => ({
637
742
  id: h.record.id,
@@ -644,10 +749,13 @@ export function memorySearchTool(opts )
644
749
  })),
645
750
  }
646
751
  // absolute:范围外视为不存在,连"被挡住"都不告诉模型。
647
- if (!allowed && outcome !== undefined) {
648
- result.withheld = { count: outside.length, reason: outcome }
752
+ if (verdict !== undefined) {
753
+ const denied = outside.length - approvedOutside.length
754
+ if (denied > 0) result.withheld = { count: denied, reason: verdict.outcome === 'allowed-once' ? 'partial' : verdict.outcome }
649
755
  }
650
756
  if (wantedTopK !== undefined) result.records = result.records.slice(0, wantedTopK)
757
+ // 上下文去重:同一条记忆同版本已经出现过 → 只回 id,摘要让 memory_get 按需取。
758
+ applySummaryDedupe(opts, exec, result.records)
651
759
  return result
652
760
  } catch (error) {
653
761
  throw toolError(error)
@@ -696,7 +804,7 @@ export function memoryForgetTool(opts )
696
804
  throw new MemoryToolError('RECORD_NOT_FOUND', `id ${args.id} not found`)
697
805
  }
698
806
  if (level === 'normal') {
699
- const outcome = await askOutOfScope(
807
+ const verdict = await askOutOfScope(
700
808
  service,
701
809
  agent,
702
810
  policy,
@@ -706,7 +814,9 @@ export function memoryForgetTool(opts )
706
814
  'delete',
707
815
  signal,
708
816
  )
709
- if (outcome !== 'allowed-once') throw visibilityError(outcome, 'delete', blocked[0] .scope)
817
+ if (!verdictAllows(verdict, args.id)) {
818
+ throw visibilityError(verdict.outcome === 'allowed-once' ? 'rejected' : verdict.outcome, 'delete', blocked[0] .scope)
819
+ }
710
820
  approved = true
711
821
  }
712
822
  }
@@ -732,7 +842,7 @@ export function memoryForgetTool(opts )
732
842
  // 绝对限制:这个作用域"不存在",按一条都没匹配处理(不删、也不泄露原因)。
733
843
  return { removed: 0, ids: [] }
734
844
  }
735
- const outcome = await askOutOfScope(
845
+ const verdict = await askOutOfScope(
736
846
  service,
737
847
  agent,
738
848
  policy,
@@ -750,7 +860,9 @@ export function memoryForgetTool(opts )
750
860
  'delete',
751
861
  signal,
752
862
  )
753
- if (outcome !== 'allowed-once') throw visibilityError(outcome, 'delete', scope)
863
+ if (!verdictAllows(verdict, `query:${scope}`)) {
864
+ throw visibilityError(verdict.outcome === 'allowed-once' ? 'rejected' : verdict.outcome, 'delete', scope)
865
+ }
754
866
  approved = true
755
867
  }
756
868
  try {
@@ -774,7 +886,7 @@ export function memoryForgetTool(opts )
774
886
 
775
887
 
776
888
 
777
-
889
+
778
890
                                 
779
891
 
780
892
 
@@ -827,9 +939,9 @@ export function memoryListTool(opts )
827
939
  const inside = raw.filter((record) => recordAllowed(policy, record.scope, record.kind))
828
940
  const outside = raw.filter((record) => !recordAllowed(policy, record.scope, record.kind))
829
941
  // undefined = 没有范围外命中 / 绝对限制(不审批);只有普通限制会真的问一次。
830
- let outcome
942
+ let verdict
831
943
  if (outside.length > 0 && level === 'normal') {
832
- outcome = await askOutOfScope(
944
+ verdict = await askOutOfScope(
833
945
  service,
834
946
  agent,
835
947
  policy,
@@ -840,8 +952,18 @@ export function memoryListTool(opts )
840
952
  (exec )?.signal,
841
953
  )
842
954
  }
843
- const allowed = outside.length === 0 || outcome === 'allowed-once'
844
- const selected = (allowed ? raw : inside).slice(0, limit)
955
+ const approvedOutside = verdict === undefined
956
+ ? []
957
+ : verdict.outcome !== 'allowed-once'
958
+ ? []
959
+ : verdict.allowedIds === undefined
960
+ ? outside
961
+ : outside.filter((record) => verdict .allowedIds .has(record.id))
962
+ const insideIds = new Set(inside.map((record) => record.id))
963
+ const approvedOutsideIds = new Set(approvedOutside.map((record) => record.id))
964
+ const selected = raw
965
+ .filter((record) => insideIds.has(record.id) || approvedOutsideIds.has(record.id))
966
+ .slice(0, limit)
845
967
  records = selected.map((r) => ({
846
968
  id: r.id,
847
969
  scope: r.scope,
@@ -849,8 +971,24 @@ export function memoryListTool(opts )
849
971
  updatedAt: r.updatedAt,
850
972
  content: r.content,
851
973
  }))
852
- if (!allowed && outcome !== undefined) {
853
- withheld = { count: outside.length, reason: outcome }
974
+ if (verdict !== undefined) {
975
+ const denied = outside.length - approvedOutside.length
976
+ if (denied > 0) withheld = { count: denied, reason: verdict.outcome === 'allowed-once' ? 'partial' : verdict.outcome }
977
+ }
978
+ // 上下文去重:同版本已经出现过 → 只回 id,正文让 memory_get 按需取。
979
+ const dedup = opts.contextDedup?.(exec)
980
+ if (dedup !== undefined && records.length > 0) {
981
+ const seen = dedup.seen(records)
982
+ const newlyShown = []
983
+ records = records.map((record) => {
984
+ if (!seen.has(record.id)) {
985
+ newlyShown.push(record)
986
+ return record
987
+ }
988
+ return { ...record, content: '', deduped: true }
989
+ })
990
+ // 这次真的把正文放进上下文的条目,后续检索/召回都按"已出现"处理。
991
+ if (newlyShown.length > 0) dedup.markContent(newlyShown)
854
992
  }
855
993
  } catch (error) {
856
994
  throw toolError(error)
@@ -997,9 +1135,9 @@ export function memoryGetTool(opts )
997
1135
  const inside = all.filter((record) => recordAllowed(policy, record.scope, record.kind))
998
1136
  const outside = all.filter((record) => !recordAllowed(policy, record.scope, record.kind))
999
1137
  // undefined = 没有范围外命中 / 绝对限制(不审批);只有普通限制会真的问一次。
1000
- let outcome
1138
+ let verdict
1001
1139
  if (outside.length > 0 && level === 'normal') {
1002
- outcome = await askOutOfScope(
1140
+ verdict = await askOutOfScope(
1003
1141
  service,
1004
1142
  agent,
1005
1143
  policy,
@@ -1010,14 +1148,24 @@ export function memoryGetTool(opts )
1010
1148
  (exec )?.signal,
1011
1149
  )
1012
1150
  }
1013
- const allowed = outside.length === 0 || outcome === 'allowed-once'
1014
- const found = allowed ? all : inside
1151
+ // verdict 为空 = 没有发起审批(绝对限制):范围外一律按不存在处理。
1152
+ const approvedOutside = verdict === undefined
1153
+ ? []
1154
+ : verdict.outcome !== 'allowed-once'
1155
+ ? []
1156
+ : verdict.allowedIds === undefined
1157
+ ? outside
1158
+ : outside.filter((record) => verdict .allowedIds .has(record.id))
1159
+ const insideIds = new Set(inside.map((record) => record.id))
1160
+ const approvedIds = new Set(approvedOutside.map((record) => record.id))
1161
+ const found = all.filter((record) => insideIds.has(record.id) || approvedIds.has(record.id))
1162
+ const denied = outside.length - approvedOutside.length
1015
1163
  return {
1016
1164
  records: found.map((r) => pickGetFields(r, fields, chars)),
1017
1165
  fields,
1018
1166
  missing: ids.filter((token) => !found.some((r) => r.id === token || r.id.startsWith(token))),
1019
- ...(!allowed && outcome !== undefined
1020
- ? { withheld: { count: outside.length, reason: outcome } }
1167
+ ...(verdict !== undefined && denied > 0
1168
+ ? { withheld: { count: denied, reason: verdict.outcome === 'allowed-once' ? 'partial' : verdict.outcome } }
1021
1169
  : {}),
1022
1170
  }
1023
1171
  } catch (error) {
@@ -9,7 +9,7 @@
9
9
  * 写进渲染文本。绝对限制下工具根本不会带 withheld(视为不存在),所以这里
10
10
  * 不需要也不应该提"被挡住"。
11
11
  */
12
-
12
+
13
13
 
14
14
  /** 把 scope 键渲染成展示文案(宿主能查到会话标题时用它,否则用短 id)。 */
15
15
 
@@ -17,7 +17,13 @@
17
17
  /** 有内容没返回时的原因(absolute 模式不会走到这里)。 */
18
18
 
19
19
 
20
- const shortId = (id ) => id.slice(0, 8)
20
+ /**
21
+ * 上下文去重的提示语(0.21.0)。memory_search / memory_list 里已经出现过的条目
22
+ * 只回 id、不再回摘要/正文;这条提示告诉模型为什么少了东西、以及怎么取全文。
23
+ */
24
+ export const DEDUPE_NOTE = '本次请求部分记忆摘要因上下文去重机制被省略,若要获取完整内容请使用 memory_get'
25
+
26
+ export const shortId = (id ) => id.slice(0, 8)
21
27
 
22
28
  const day = (value ) =>
23
29
  typeof value === 'number' && Number.isFinite(value) ? new Date(value).toISOString().slice(0, 10) : ''
@@ -39,6 +45,8 @@ export function renderWithheldNote(withheld ) {
39
45
  return `另有 ${count} 条记忆在本会话的可见域外,审批在 5 分钟内没有得到答复,已按拒绝处理,未返回。如确实需要,请让用户调整「记忆范围」后重试。`
40
46
  case 'unavailable':
41
47
  return `另有 ${count} 条记忆在本会话的可见域外,当前环境无法发起审批,未返回。`
48
+ case 'partial':
49
+ return `另有 ${count} 条记忆在本会话的可见域外,用户在本次审批中只允许了其余条目(限制等级:普通限制),未返回。如确实需要,请让用户调整「记忆范围」后重试。`
42
50
  default:
43
51
  return ''
44
52
  }
@@ -51,12 +59,39 @@ export function renderSearchResult(value , label )
51
59
  if (note === '') return '没有找到相关记忆。'
52
60
  return `没有找到可见范围内的相关记忆。\n${note}`
53
61
  }
62
+ // 已出现过的条目(上下文去重)只回 id:不带作用域/类型/标签/摘要。
63
+ let omitted = 0
54
64
  const body = value.records.map((hit) => {
65
+ if (hit.deduped === true) {
66
+ omitted += 1
67
+ return shortId(hit.id)
68
+ }
55
69
  const tags = joinList(hit.tags)
56
70
  const meta = `${label(hit.scope)}/${hit.kind}${tags === '' ? '' : ` · ${tags}`}`
57
71
  return `[${hit.score.toFixed(3)}] ${shortId(hit.id)} (${meta}) ${hit.summary}`
58
72
  }).join('\n')
59
- return note === '' ? body : `${body}\n\n${note}`
73
+ const tail = [omitted > 0 ? DEDUPE_NOTE : '', note].filter((part) => part !== '').join('\n\n')
74
+ return tail === '' ? body : `${body}\n\n${tail}`
75
+ }
76
+
77
+ /**
78
+ * memory_list 的渲染:每条「(作用域/类型) 正文」;上下文去重过的条目只给 id。
79
+ * `scopeStats` 的尾部摘要由 apply.ts 追加(那里能查会话标题)。
80
+ */
81
+ export function renderListResult(value , label ) {
82
+ let omitted = 0
83
+ const body = value.records.length === 0
84
+ ? '该作用域还没有记忆。'
85
+ : value.records.map((record) => {
86
+ if (record.deduped === true) {
87
+ omitted += 1
88
+ return shortId(record.id)
89
+ }
90
+ return `(${label(record.scope)}/${record.kind}) ${record.content}`
91
+ }).join('\n')
92
+ const note = renderWithheldNote(value.withheld)
93
+ const tail = [omitted > 0 ? DEDUPE_NOTE : '', note].filter((part) => part !== '').join('\n\n')
94
+ return tail === '' ? body : `${body}\n\n${tail}`
60
95
  }
61
96
 
62
97
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@restregular/dsh-memory",
3
- "version": "0.20.0",
3
+ "version": "0.21.0",
4
4
  "description": "DeepSeek Harness 记忆系统(单包单入口):JSONL 存储 + 本地向量检索(BGE-M3)+ 五个模型工具 + 自动召回注入 + 管理面板。",
5
5
  "icon": "./icon.svg",
6
6
  "type": "module",