@restregular/dsh-memory 0.9.5 → 0.9.7

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.
@@ -0,0 +1,163 @@
1
+ /**
2
+ * 注入状态:每条记忆在当前**会话上下文**里的语义状态。
3
+ *
4
+ * 0.9.6 之前只有一个全局 `lastText`:换会话会错误去重,而且无法区分
5
+ * "正文已注入"与"只给了标签"。这里按 sessionId 保存:
6
+ * - not_seen → 没有任何记录(默认)
7
+ * - hint_injected → 只进过截断标签索引,正文仍未加载
8
+ * - content_injected→ 正文已进入当前上下文
9
+ *
10
+ * 版本 = record.updatedAt。内容更新后允许重新注入;上下文重建(压缩/新会话)
11
+ * 由调用方 clear(),绝不用"历史上出现过"代替"当前仍在上下文里"。
12
+ */
13
+
14
+
15
+
16
+
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
+ const MAX_SESSIONS = 500
45
+ const MAX_ENTRIES = 4000
46
+
47
+ export class InjectionState {
48
+ sessions = new Map ()
49
+
50
+ stateOf(sessionId ) {
51
+ const key = sessionId ?? '__unknown__'
52
+ let state = this.sessions.get(key)
53
+ if (state === undefined) {
54
+ state = { entries: new Map(), lastText: '', lastQuery: '' }
55
+ this.sessions.set(key, state)
56
+ if (this.sessions.size > MAX_SESSIONS) {
57
+ const oldest = this.sessions.keys().next()
58
+ if (!oldest.done) this.sessions.delete(oldest.value)
59
+ }
60
+ }
61
+ return state
62
+ }
63
+
64
+ /** 记录"只注入了标签"——正文仍未加载。 */
65
+ markHint(sessionId , records ) {
66
+ const state = this.stateOf(sessionId)
67
+ const now = Date.now()
68
+ for (const record of records) {
69
+ const previous = state.entries.get(record.id)
70
+ if (previous?.status === 'content_injected') continue
71
+ state.entries.set(record.id, { status: 'hint_injected', version: record.updatedAt, at: now })
72
+ }
73
+ this.trim(state)
74
+ }
75
+
76
+ /** 记录"正文已注入"(hint 状态被正文覆盖)。 */
77
+ markContent(sessionId , records ) {
78
+ const state = this.stateOf(sessionId)
79
+ const now = Date.now()
80
+ for (const record of records) {
81
+ state.entries.set(record.id, { status: 'content_injected', version: record.updatedAt, at: now })
82
+ }
83
+ this.trim(state)
84
+ }
85
+
86
+ /**
87
+ * 从检索结果里挑出**还没注入正文**的记录。
88
+ * 已注入且版本没变 → deduplicated;版本变了 → 视为需要重新评估。
89
+ */
90
+ filterNewContent (
91
+ sessionId ,
92
+ records ,
93
+ ) {
94
+ const state = this.stateOf(sessionId)
95
+ const fresh = []
96
+ const deduplicated = []
97
+ const updated = []
98
+ for (const record of records) {
99
+ const entry = state.entries.get(record.id)
100
+ if (entry?.status === 'content_injected' && entry.version === record.updatedAt) {
101
+ deduplicated.push(record.id)
102
+ continue
103
+ }
104
+ if (entry?.status === 'content_injected' && entry.version !== record.updatedAt) updated.push(record.id)
105
+ fresh.push(record)
106
+ }
107
+ return { fresh, deduplicated, updated }
108
+ }
109
+
110
+ /** 同一上下文里是否已经注入过同一段文本(最后一道按内容幂等)。 */
111
+ lastTextOf(sessionId ) {
112
+ return this.stateOf(sessionId).lastText
113
+ }
114
+
115
+ setLastText(sessionId , text ) {
116
+ this.stateOf(sessionId).lastText = text
117
+ }
118
+
119
+ lastQueryOf(sessionId ) {
120
+ return this.stateOf(sessionId).lastQuery
121
+ }
122
+
123
+ setLastQuery(sessionId , query ) {
124
+ this.stateOf(sessionId).lastQuery = query
125
+ }
126
+
127
+ /** 上下文重建(压缩/重置/新会话)时清空,不能沿用错误的去重状态。 */
128
+ clear(sessionId ) {
129
+ this.sessions.delete(sessionId ?? '__unknown__')
130
+ }
131
+
132
+ statusOf(sessionId , id ) {
133
+ return this.stateOf(sessionId).entries.get(id)?.status ?? 'not_seen'
134
+ }
135
+
136
+ snapshot(sessionId ) {
137
+ const state = this.sessions.get(sessionId)
138
+ if (state === undefined) {
139
+ return { sessionId, hintCount: 0, contentCount: 0, lastText: '', lastQuery: '', entries: [] }
140
+ }
141
+ const entries = [...state.entries.entries()].map(([id, entry]) => ({ id, ...entry }))
142
+ return {
143
+ sessionId,
144
+ hintCount: entries.filter((entry) => entry.status === 'hint_injected').length,
145
+ contentCount: entries.filter((entry) => entry.status === 'content_injected').length,
146
+ lastText: state.lastText,
147
+ lastQuery: state.lastQuery,
148
+ entries,
149
+ }
150
+ }
151
+
152
+ trim(state ) {
153
+ if (state.entries.size <= MAX_ENTRIES) return
154
+ // Map 保留插入顺序;淘汰最旧的一批即可(状态只用于去重,丢了最多多注入一次)。
155
+ for (const key of state.entries.keys()) {
156
+ state.entries.delete(key)
157
+ if (state.entries.size <= MAX_ENTRIES) break
158
+ }
159
+ }
160
+ }
161
+
162
+
163
+ //# sourceURL=source/memory-inject/src/state.ts
package/lib/panel.js CHANGED
@@ -240,6 +240,12 @@ async function handle(ctx, routePath, req, res) {
240
240
  // memories from earlier sessions included) — best-effort discovery, cached.
241
241
  discoverScopes(memory, store)
242
242
 
243
+ if (method === 'GET' && path === '/audit') {
244
+ if (typeof memory.injectAudit !== 'function') throw new ApiError(501, 'NOT_SUPPORTED', 'provider 未提供注入审计接口(请升级 @restregular/dsh-memory)')
245
+ const session = (url.searchParams.get('session') ?? '').trim()
246
+ writeJson(res, { ok: true, value: { entries: memory.injectAudit(session === '' ? undefined : session) } })
247
+ return
248
+ }
243
249
  if (method === 'GET' && path === '/status') {
244
250
  const stats = typeof memory.scopeStats === 'function'
245
251
  ? memory.scopeStats()
@@ -494,6 +494,8 @@ export class LocalMemoryService {
494
494
  names
495
495
  /** 注入设置(面板「设置」标签页可改;落盘在 <storeDir>/settings.json)。 */
496
496
  inject
497
+ /** 注入半边的审计收集器(可选;没有注入半边时保持 null)。 */
498
+ injectAuditSink = null
497
499
 
498
500
  constructor(config = {}) {
499
501
  this.cfg = resolveConfig(config)
@@ -536,6 +538,16 @@ export class LocalMemoryService {
536
538
  return normalizeInjectSettings(this.inject, this.inject)
537
539
  }
538
540
 
541
+ /** 注入半边注册审计收集器。重复注册时后一个覆盖前一个(激活重载)。 */
542
+ attachInjectAudit(sink ) {
543
+ this.injectAuditSink = sink
544
+ }
545
+
546
+ /** 面板 /memory/api/audit 用:返回注入评估记录(最新在前);没有注入半边时为空。 */
547
+ injectAudit(sessionId ) {
548
+ try { return this.injectAuditSink?.list(sessionId) ?? [] } catch { return [] }
549
+ }
550
+
539
551
  /** 面板改设置:合并补丁 → 落盘 → 返回新值。写盘失败只告警,不影响运行中的设置。 */
540
552
  updateInjectSettings(patch ) {
541
553
  this.inject = mergeInjectSettings(this.inject, patch)
@@ -553,7 +565,16 @@ export class LocalMemoryService {
553
565
  const base = normalizeInjectSettings(fromConfig)
554
566
  try {
555
567
  if (existsSync(this.settingsPath)) {
556
- return mergeInjectSettings(base, JSON.parse(readFileSync(this.settingsPath, 'utf8')))
568
+ const parsed = JSON.parse(readFileSync(this.settingsPath, 'utf8'))
569
+ const digest = (parsed )?.digest
570
+ // 0.9.5 及以前的默认 digest 预算是 400,且没有 hintBudget 字段。
571
+ // 升级到 0.9.6 时把"旧默认值"迁到新默认 800;显式写过 hintBudget 的配置不动。
572
+ const oldDigestDefault = digest !== undefined && digest !== null
573
+ && typeof digest === 'object' && digest.hintBudget === undefined
574
+ && Number(digest.tokenBudget) === 400
575
+ const merged = mergeInjectSettings(base, parsed)
576
+ if (oldDigestDefault) merged.digest.tokenBudget = base.digest.tokenBudget
577
+ return merged
557
578
  }
558
579
  } catch (error) {
559
580
  console.warn(`[dsh-memory-local] 忽略损坏的注入设置 ${this.settingsPath}: ${error instanceof Error ? error.message : String(error)}`)
@@ -561,19 +582,43 @@ export class LocalMemoryService {
561
582
  return base
562
583
  }
563
584
 
585
+ /**
586
+ * 被其他记录 supersedes 指向的记录 id(=已退役)。scope 缺省时扫描全部已加载 scope。
587
+ * 只读、不发现、不创建;注入半边/规则段/自动召回共用这一份口径。
588
+ */
589
+ supersededIds(scopes ) {
590
+ const ids = new Set ()
591
+ const stores = scopes === undefined
592
+ ? this.store.allScopes()
593
+ : scopes.map((scope) => this.store.peek(scope)).filter((s) => s !== undefined)
594
+ for (const store of stores) {
595
+ for (const record of store.records.values()) {
596
+ if (typeof record.supersedes === 'string' && record.supersedes.length > 0) ids.add(record.supersedes)
597
+ }
598
+ }
599
+ return [...ids]
600
+ }
601
+
564
602
  /**
565
603
  * 同步列记录(只读、不触发磁盘发现)。系统提示里的「硬性规则」段每步都要
566
604
  * 渲染,必须是同步的;这里直接读已加载 scope 的内存快照。
605
+ *
606
+ * 默认排除已被 supersede 的旧记录;需要历史/审计时显式 includeSuperseded。
567
607
  */
568
- listSync(input = {}) {
608
+ listSync(input = {}) {
569
609
  const seen = new Set ()
570
- const out = []
610
+ const stores = []
571
611
  for (const scope of input.scopes ?? []) {
572
612
  if (seen.has(scope)) continue
573
613
  seen.add(scope)
574
614
  const s = this.store.peek(scope)
575
- if (!s) continue
615
+ if (s) stores.push(s)
616
+ }
617
+ const retired = input.includeSuperseded ? new Set () : new Set(this.supersededIds(input.scopes))
618
+ const out = []
619
+ for (const s of stores) {
576
620
  for (const rec of s.records.values()) {
621
+ if (retired.has(rec.id)) continue
577
622
  if (input.kind !== undefined && rec.kind !== input.kind) continue
578
623
  out.push(rec)
579
624
  }
@@ -1370,7 +1415,11 @@ export class LocalMemoryService {
1370
1415
  const candidates = new Map ()
1371
1416
  for (const { store, isGlobal } of stores) {
1372
1417
  const all = [...store.records.values()]
1373
- const tokenSets = all.map((r) => new Set(tokenize(r.content)))
1418
+ // 词法通道把标签也当可检索文本:自动召回用标签做触发词时,记忆正文里
1419
+ // 不一定出现这个标签(例如 tags=['release'] 但正文是中文),不并进来就搜不到。
1420
+ const searchableText = (record ) =>
1421
+ `${record.content} ${(record.tags ?? []).join(' ')}`
1422
+ const tokenSets = all.map((r) => new Set(tokenize(searchableText(r))))
1374
1423
  const stats = buildStats(tokenSets)
1375
1424
  for (let i = 0; i < all.length; i++) {
1376
1425
  const rec = all[i]
@@ -1665,13 +1714,14 @@ export class LocalMemoryService {
1665
1714
  return { imported, skipped, rejected, errors }
1666
1715
  }
1667
1716
 
1668
- /** 规则条数(面板设置页显示;只数已加载/已发现的作用域)。 */
1717
+ /** 规则条数(面板设置页显示;只数已加载/已发现的作用域,排除退役规则)。 */
1669
1718
  ruleStats() {
1670
1719
  this.discoverScopes()
1720
+ const retired = new Set(this.supersededIds())
1671
1721
  let total = 0
1672
1722
  let scopes = 0
1673
1723
  for (const s of this.store.allScopes()) {
1674
- const n = [...s.records.values()].filter((r) => r.kind === 'rule').length
1724
+ const n = [...s.records.values()].filter((r) => r.kind === 'rule' && !retired.has(r.id)).length
1675
1725
  if (n > 0) { total += n; scopes += 1 }
1676
1726
  }
1677
1727
  return { total, scopes }
@@ -16,6 +16,10 @@
16
16
          
17
17
 
18
18
 
19
+                        
20
+
21
+           
22
+
19
23
 
20
24
 
21
25
 
@@ -42,6 +46,37 @@
42
46
 
43
47
 
44
48
 
49
+ /**
50
+ * 自动召回的“模式”就是一组**标签**。工具调用的 name / description / 参数
51
+ * 命中任一标签时触发一次定向召回;失败调用则始终用 description 触发。
52
+ * 标签来自用户的记忆数据(如 release / database / credentials),可在设置里增删,
53
+ * 不需要写正则,也不只服务发布部署。
54
+ */
55
+
56
+
57
+                    
58
+
59
+                       
60
+
61
+                          
62
+
63
+                    
64
+
65
+
66
+
67
+ /** 默认触发标签:覆盖发布、部署、数据库、凭据、破坏性操作、依赖、远程等域。 */
68
+ export const DEFAULT_AUTO_RECALL_TAGS = [
69
+ 'release', 'publish', 'deploy', 'deployment', 'rollback', 'restore', 'backup',
70
+ 'database', 'migration', 'migrate', 'schema', 'credentials', 'credential', 'auth', 'token',
71
+ 'secret', 'login', 'permission', 'security', 'destructive', 'delete', 'drop', 'truncate',
72
+ 'install', 'upgrade', 'network', 'ssh', 'server', 'git', 'push', 'github', 'npm',
73
+ 'devcred', 'docker', 'kubectl', 'terraform', 'production', 'prod',
74
+ '发布', '推送', '部署', '回滚', '恢复', '备份', '数据库', '迁移', '凭据', '认证',
75
+ '权限', '安全', '删除', '清空', '安装', '升级', '网络', '服务器', '生产',
76
+ ]
77
+
78
+
79
+
45
80
 
46
81
                                    
47
82
 
@@ -58,16 +93,24 @@
58
93
 
59
94
 
60
95
 
96
+
61
97
 
62
98
 
63
99
 
64
100
 
65
101
  export const defaultInjectSettings = {
66
102
  recall: { enabled: true, topK: 5, tokenBudget: 500 },
67
- digest: { enabled: true, topK: 8, tokenBudget: 400 },
103
+ digest: { enabled: true, topK: 8, tokenBudget: 800, hintBudget: 160, hintMaxItems: 6 },
68
104
  impression: { enabled: true, chars: 120 },
69
105
  policy: { enabled: true },
70
106
  rules: { enabled: true, maxItems: 10, tokenBudget: 300, delta: true },
107
+ autoRecall: {
108
+ enabled: true,
109
+ useToolDescription: true,
110
+ maxQueryChars: 240,
111
+ tags: DEFAULT_AUTO_RECALL_TAGS,
112
+ useMemoryTags: true,
113
+ },
71
114
  turnEnd: { enabled: false },
72
115
  sidebar: { enabled: false },
73
116
  }
@@ -106,8 +149,21 @@ export function normalizeInjectSettings(raw , base = def
106
149
  const impression = section('impression')
107
150
  const policy = section('policy')
108
151
  const rules = section('rules')
152
+ const autoRecall = section('autoRecall')
109
153
  const turnEnd = section('turnEnd')
110
154
  const sidebar = section('sidebar')
155
+ const rawTags = Array.isArray(autoRecall.tags) ? autoRecall.tags : base.autoRecall.tags
156
+ const normalizedTags = []
157
+ const seenTags = new Set ()
158
+ for (const raw of rawTags.slice(0, 60)) {
159
+ if (typeof raw !== 'string') continue
160
+ const tag = raw.trim()
161
+ if (tag === '') continue
162
+ const key = tag.toLowerCase()
163
+ if (seenTags.has(key)) continue
164
+ seenTags.add(key)
165
+ normalizedTags.push(tag)
166
+ }
111
167
  return {
112
168
  recall: {
113
169
  enabled: bool(recall.enabled, base.recall.enabled),
@@ -118,6 +174,8 @@ export function normalizeInjectSettings(raw , base = def
118
174
  enabled: bool(digest.enabled, base.digest.enabled),
119
175
  topK: clampInt(digest.topK, 1, 30, base.digest.topK),
120
176
  tokenBudget: clampInt(digest.tokenBudget, 50, 4000, base.digest.tokenBudget),
177
+ hintBudget: clampInt(digest.hintBudget, 30, 1000, base.digest.hintBudget),
178
+ hintMaxItems: clampInt(digest.hintMaxItems, 1, 30, base.digest.hintMaxItems),
121
179
  },
122
180
  impression: {
123
181
  enabled: bool(impression.enabled, base.impression.enabled),
@@ -130,6 +188,13 @@ export function normalizeInjectSettings(raw , base = def
130
188
  tokenBudget: clampInt(rules.tokenBudget, 50, 4000, base.rules.tokenBudget),
131
189
  delta: bool(rules.delta, base.rules.delta),
132
190
  },
191
+ autoRecall: {
192
+ enabled: bool(autoRecall.enabled, base.autoRecall.enabled),
193
+ useToolDescription: bool(autoRecall.useToolDescription, base.autoRecall.useToolDescription),
194
+ maxQueryChars: clampInt(autoRecall.maxQueryChars, 40, 500, base.autoRecall.maxQueryChars),
195
+ tags: normalizedTags,
196
+ useMemoryTags: bool(autoRecall.useMemoryTags, base.autoRecall.useMemoryTags),
197
+ },
133
198
  turnEnd: { enabled: bool(turnEnd.enabled, base.turnEnd.enabled) },
134
199
  sidebar: { enabled: bool(sidebar.enabled, base.sidebar.enabled) },
135
200
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@restregular/dsh-memory",
3
- "version": "0.9.5",
3
+ "version": "0.9.7",
4
4
  "description": "DeepSeek Harness 本地长期记忆(单包单入口):JSONL 存储 + 本地向量检索(BGE-M3)+ 五个模型工具 + 自动召回注入 + 管理面板。",
5
5
  "icon": "./icon.svg",
6
6
  "type": "module",