@restregular/dsh-memory 0.9.5 → 0.9.6

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.
@@ -5,6 +5,11 @@
5
5
  * 注意力;系统提示是每步都会看见的位置,且文本稳定时可以跟着前缀缓存。
6
6
  * 只放"不可机械校验"的规则 —— 能写成检查的规则应该进钩子(代码强制)。
7
7
  *
8
+ * 0.9.6 起:
9
+ * - 已被新规则 supersede 的旧规则不进入常驻段,保留历史用于 memory_get/search;
10
+ * - mandatory/pinned 标签的规则排在最前,超出预算时给出明确警告,不静默丢弃;
11
+ * - 任何因条数/预算未展示的规则都会在段尾报告数量,并写入审计。
12
+ *
8
13
  * 增量提醒(0.9.0 起):系统提示段每步都会按内存快照重渲染,但"规则是别的会话里
9
14
  * 新加/改过的"这件事只改文本、不产生任何对话消息;模型在旧会话里继续聊时未必会
10
15
  * 重新读一遍系统提示,而且规则段有 token 预算,新规则可能被旧规则挤出常驻段。
@@ -14,6 +19,7 @@
14
19
 
15
20
  import { estimateTokens } from '../provider/rerank.js'
16
21
 
22
+ import { supersededIds } from './digest.js'
17
23
 
18
24
  export const RULES_HEADER =
19
25
  '以下是必须遵守的硬性规则(来自 kind=rule 的长期记忆;除非用户明确要求破例,否则按它做):'
@@ -21,6 +27,16 @@ export const RULES_HEADER =
21
27
  export const RULES_DELTA_HEADER =
22
28
  '以下是新增或变更的硬性规则(来自 kind=rule 的长期记忆;从现在起按它做):'
23
29
 
30
+ /** mandatory/pinned 是显式的标签约定,不把全部 kind=rule 自动升级。 */
31
+ const MANDATORY_TAGS = new Set(['mandatory', 'pinned', '必含', '强制'])
32
+
33
+ export function isMandatoryRule(record ) {
34
+ return (record.tags ?? []).some((tag) => {
35
+ const value = String(tag).trim().toLowerCase()
36
+ return MANDATORY_TAGS.has(value) || value.startsWith('mandatory:') || value.startsWith('pinned:')
37
+ })
38
+ }
39
+
24
40
  /**
25
41
  * 一条规则的渲染行。常驻段与增量块共用同一个格式 —— 增量靠"逐行比对"判断
26
42
  * 会话是否已经见过某条规则,两边只要有一个字符不同就会重复提醒。
@@ -29,41 +45,150 @@ export function ruleLine(record ) {
29
45
  return `- ${record.content.replace(/\s+/g, ' ').slice(0, 240)}`
30
46
  }
31
47
 
32
- /** 规则排序:重要度优先,其次新的在前,最后按内容稳定排序(两个实例结果一致)。 */
48
+ /** 规则排序:mandatory 优先,其次重要度、新的在前,最后按内容稳定排序。 */
33
49
  export function orderRules(records ) {
34
50
  return [...records].sort(
35
51
  (a, b) =>
52
+ Number(isMandatoryRule(b)) - Number(isMandatoryRule(a)) ||
36
53
  b.importance - a.importance ||
37
54
  b.updatedAt - a.updatedAt ||
38
55
  a.content.localeCompare(b.content),
39
56
  )
40
57
  }
41
58
 
42
- function renderLines(records , header , cfg , budget ) {
59
+
60
+
61
+
62
+
63
+
64
+
65
+
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+ function renderLines(
80
+ records ,
81
+ header ,
82
+ cfg ,
83
+ budget ,
84
+ ) {
85
+ const retired = supersededIds(records)
86
+ const active = []
87
+ let omittedSuperseded = 0
88
+ for (const record of records) {
89
+ if (retired.has(record.id)) { omittedSuperseded += 1; continue }
90
+ active.push(record)
91
+ }
92
+
43
93
  let tokens = estimateTokens(header) + 4
44
94
  const lines = []
45
- for (const record of orderRules(records).slice(0, cfg.maxItems)) {
95
+ let omittedTopK = 0
96
+ let omittedBudget = 0
97
+ let budgetExhausted = false
98
+ let mandatoryInjected = 0
99
+ let mandatoryOmitted = 0
100
+ const mandatoryTotal = active.filter(isMandatoryRule).length
101
+
102
+ for (const [index, record] of orderRules(active).entries()) {
103
+ const mandatory = isMandatoryRule(record)
104
+ if (index >= cfg.maxItems) {
105
+ omittedTopK += 1
106
+ if (mandatory) mandatoryOmitted += 1
107
+ continue
108
+ }
109
+ if (budgetExhausted) {
110
+ omittedBudget += 1
111
+ if (mandatory) mandatoryOmitted += 1
112
+ continue
113
+ }
46
114
  const line = ruleLine(record)
47
115
  const cost = estimateTokens(line) + 1
48
- if (tokens + cost > budget) break
116
+ if (tokens + cost > budget) {
117
+ budgetExhausted = true
118
+ omittedBudget += 1
119
+ if (mandatory) mandatoryOmitted += 1
120
+ continue
121
+ }
49
122
  lines.push(line)
50
123
  tokens += cost
124
+ if (mandatory) mandatoryInjected += 1
125
+ }
126
+
127
+ if (lines.length === 0 && active.length === 0) {
128
+ return {
129
+ text: '', tokens: 0, count: 0,
130
+ stats: {
131
+ candidates: records.length, active: 0, injected: 0,
132
+ omittedTopK, omittedBudget, omittedSuperseded,
133
+ mandatory: 0, mandatoryInjected: 0, mandatoryOmitted: 0, truncated: omittedSuperseded > 0,
134
+ },
135
+ }
136
+ }
137
+ // 一条都装不下且不是 mandatory 溢出的情况:文本为空,但统计仍保留给审计。
138
+ // 系统提示的调用方(renderRulesBlock)会把空文本转成 null,保持旧行为。
139
+
140
+ const notes = []
141
+ const noBody = lines.length === 0 && mandatoryOmitted === 0
142
+ const omitted = omittedTopK + omittedBudget
143
+ if (!noBody && omitted > 0) {
144
+ const parts = []
145
+ if (omittedTopK > 0) parts.push(`条数上限 ${omittedTopK}`)
146
+ if (omittedBudget > 0) parts.push(`预算 ${omittedBudget}`)
147
+ notes.push(`(另有 ${omitted} 条规则未展示:${parts.join(' / ')};需要时用 memory_search 查 kind=rule)`)
148
+ }
149
+ if (!noBody && mandatoryOmitted > 0) {
150
+ notes.push(`(警告:${mandatoryOmitted} 条 mandatory / pinned 规则未展示,当前规则不完整)`)
151
+ }
152
+ const text = [lines.length > 0 ? `${header}\n${lines.join('\n')}` : '', ...notes].filter(Boolean).join('\n')
153
+ const extraTokens = notes.reduce((sum, note) => sum + estimateTokens(note) + 1, 0)
154
+
155
+ return {
156
+ text,
157
+ tokens: tokens + extraTokens,
158
+ count: lines.length,
159
+ stats: {
160
+ candidates: records.length,
161
+ active: active.length,
162
+ injected: lines.length,
163
+ omittedTopK,
164
+ omittedBudget,
165
+ omittedSuperseded,
166
+ mandatory: mandatoryTotal,
167
+ mandatoryInjected,
168
+ mandatoryOmitted,
169
+ truncated: omitted > 0,
170
+ },
51
171
  }
52
- if (lines.length === 0) return null
53
- return `${header}\n${lines.join('\n')}`
54
172
  }
55
173
 
56
- /** 渲染规则段(系统提示常驻)。重要性优先,新的在前;每条截 240 字,超出预算截断。 */
57
- export function renderRulesBlock(records , cfg ) {
174
+ /** 渲染规则段(系统提示常驻)及其审计统计。没有可展示的规则时 text 为空串。 */
175
+ export function renderRules(records , cfg ) {
58
176
  return renderLines(records, RULES_HEADER, cfg, cfg.tokenBudget)
59
177
  }
60
178
 
179
+ /** 渲染规则段(系统提示常驻),只要文本;空文本返回 null。 */
180
+ export function renderRulesBlock(records , cfg ) {
181
+ const text = renderRules(records, cfg).text
182
+ return text === '' ? null : text
183
+ }
184
+
61
185
  /**
62
186
  * 渲染"新增/变更的规则"块。预算比常驻段留宽一点:增量块本来就是常驻段装不下
63
187
  * 时才需要的补充,用同一个 300 token 的上限容易把最该提醒的一条又截掉。
64
188
  */
65
189
  export function renderRulesDeltaBlock(records , cfg ) {
66
- return renderLines(records, RULES_DELTA_HEADER, cfg, Math.max(cfg.tokenBudget, 600))
190
+ const text = renderLines(records, RULES_DELTA_HEADER, cfg, Math.max(cfg.tokenBudget, 600)).text
191
+ return text === '' ? null : text
67
192
  }
68
193
 
69
194
  /** 模型可见历史里的一小部分形状(只读;不依赖宿主的 Message 类型)。 */
@@ -0,0 +1,176 @@
1
+ /**
2
+ * 行为信号 → 自动召回查询。
3
+ *
4
+ * 只消费宿主**公开**的可观测信号:
5
+ * - session/event 的 `tool/call`(name + arguments)
6
+ * - session/event 的 `tool/result`(error / message.isError)
7
+ * - compaction 事件(上下文重建)
8
+ *
9
+ * 不读取隐藏思维链,也不把原始工具参数写进审计或日志。这里只做关键词分类,
10
+ * 生成短的检索查询和稳定 key;是否真的召回由注入半边在下一个 pre-step 决定。
11
+ */
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+ const HIGH_RISK_PATTERNS = [
32
+ {
33
+ key: 'npm-publish',
34
+ re: /\b(?:npm|pnpm|yarn)\s+(?:publish|stage\s+publish|release)\b/i,
35
+ query: 'npm publish release credentials devcred validating',
36
+ phase: 'release',
37
+ },
38
+ {
39
+ key: 'git-push',
40
+ re: /\bgit\s+push\b/i,
41
+ query: 'git push github rest api credentials network',
42
+ phase: 'release',
43
+ },
44
+ {
45
+ key: 'container-deploy',
46
+ re: /\b(?:docker|podman)\s+push\b|\bkubectl\s+(?:apply|rollout|delete)\b|\bhelm\s+(?:upgrade|install)\b/i,
47
+ query: 'deploy container kubernetes release rollback',
48
+ phase: 'deploy',
49
+ },
50
+ {
51
+ key: 'terraform-apply',
52
+ re: /\bterraform\s+(?:apply|destroy)\b/i,
53
+ query: 'deploy terraform apply rollback approval',
54
+ phase: 'deploy',
55
+ },
56
+ {
57
+ key: 'generic-release',
58
+ re: /\b(?:release|deploy|ship)\s+(?:the\s+)?(?:package|app|service|build|version)\b/i,
59
+ query: 'release deploy publish checklist credentials rollback',
60
+ phase: 'release',
61
+ },
62
+ ]
63
+
64
+ /** 从用户可见文本推断任务阶段;不确定就返回 unknown,不伪造。 */
65
+ export function detectPhase(text ) {
66
+ const value = String(text ?? '').toLowerCase()
67
+ if (/(?:npm|pnpm|yarn)\s+(?:publish|stage)|git\s+push|\brelease\b|\bpublish\b|发布|推送/.test(value)) return 'release'
68
+ if (/(?:deploy|rollout|kubectl|terraform\s+apply|部署)/.test(value)) return 'deploy'
69
+ if (/(?:implement|refactor|fix|add)\b|开发|实现|修复|新增|重构|添加/.test(value)) return 'development'
70
+ return 'unknown'
71
+ }
72
+
73
+ /** 工具名 + 参数里的可见操作目标是否命中高风险操作。只返回匹配类别,不保留参数。 */
74
+ export function highRiskSignal(tool , argumentsText ) {
75
+ const haystack = `${tool} ${argumentsText}`
76
+ for (const pattern of HIGH_RISK_PATTERNS) {
77
+ if (pattern.re.test(haystack)) {
78
+ return {
79
+ key: `high-risk:${pattern.key}`,
80
+ reason: 'before_high_risk_tool',
81
+ query: pattern.query,
82
+ phase: pattern.phase,
83
+ tool,
84
+ }
85
+ }
86
+ }
87
+ return undefined
88
+ }
89
+
90
+ /** 工具失败分类。只根据 code/消息里的**类别词**判断,不保存原始消息。 */
91
+ export function failureSignal(tool , code , message ) {
92
+ const haystack = `${code ?? ''} ${message ?? ''}`.toLowerCase()
93
+ let errorClass = 'TOOL_FAILED'
94
+ let query = `failure diagnostics ${tool ?? 'tool'}`
95
+ let phase = 'unknown'
96
+ if (/credential|missing.*token|no.*token|unauthori[sz]ed|e401|401|auth(?:entication)?\s+required/.test(haystack)) {
97
+ errorClass = 'MISSING_CREDENTIAL'
98
+ query = 'credentials npm publish devcred token missing'
99
+ phase = 'release'
100
+ } else if (/permission|denied|forbidden|e403|403|eacces|eperm/.test(haystack)) {
101
+ errorClass = 'PERMISSION_DENIED'
102
+ query = 'permission denied sandbox approval credentials'
103
+ } else if (/timeout|timed\s*out|etimedout/.test(haystack)) {
104
+ errorClass = 'TIMEOUT'
105
+ query = 'timeout retry long running command'
106
+ } else if (/abort|cancel|interrupt/.test(haystack)) {
107
+ errorClass = 'ABORTED_UNKNOWN'
108
+ query = 'aborted command diagnosis retry'
109
+ }
110
+ return {
111
+ key: `tool-failure:${tool ?? 'unknown'}:${errorClass}`,
112
+ reason: 'tool_failure',
113
+ query,
114
+ phase,
115
+ ...(tool !== undefined ? { tool } : {}),
116
+ errorClass,
117
+ }
118
+ }
119
+
120
+ /** 从一个 tool/result 事件里取错误信息(已做防御性读取)。 */
121
+ export function toolResultFailure(event ) {
122
+ const data = (event )?.data
123
+ const error = data?.error
124
+ const message = data?.message
125
+ const text = Array.isArray(message?.content)
126
+ ? message.content.filter((block) => block?.type === 'text' && typeof block.text === 'string').map((block) => block.text ).join(' ')
127
+ : ''
128
+ const code = typeof error?.code === 'string'
129
+ ? error.code
130
+ : typeof error?.name === 'string' ? error.name : undefined
131
+ return {
132
+ ...(code !== undefined ? { code } : {}),
133
+ ...(text !== '' ? { message: text.slice(0, 240) } : {}),
134
+ isError: message?.isError === true || error !== undefined,
135
+ }
136
+ }
137
+
138
+ /**
139
+ * 每个会话只保留**最新一条未消费**的信号;相同 key 不重复排队。
140
+ * `take()` 消费后保留 lastKey,同一失败不会因为重复事件反复搜索。
141
+ */
142
+ export class BehaviorSignalStore {
143
+ bySession = new Map ()
144
+
145
+ note(sessionId , signal ) {
146
+ if (sessionId === undefined) return false
147
+ const key = sessionId
148
+ const state = this.bySession.get(key) ?? { lastKey: '' }
149
+ if (signal.key === state.lastKey) return false
150
+ state.pending = signal
151
+ state.lastKey = signal.key
152
+ this.bySession.set(key, state)
153
+ if (this.bySession.size > 500) {
154
+ const oldest = this.bySession.keys().next()
155
+ if (!oldest.done) this.bySession.delete(oldest.value)
156
+ }
157
+ return true
158
+ }
159
+
160
+ take(sessionId ) {
161
+ if (sessionId === undefined) return undefined
162
+ const state = this.bySession.get(sessionId)
163
+ if (state?.pending === undefined) return undefined
164
+ const signal = state.pending
165
+ state.pending = undefined
166
+ return signal
167
+ }
168
+
169
+ clear(sessionId ) {
170
+ if (sessionId === undefined) return
171
+ this.bySession.delete(sessionId)
172
+ }
173
+ }
174
+
175
+
176
+ //# sourceURL=source/memory-inject/src/signals.ts
@@ -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
  }
@@ -1665,13 +1710,14 @@ export class LocalMemoryService {
1665
1710
  return { imported, skipped, rejected, errors }
1666
1711
  }
1667
1712
 
1668
- /** 规则条数(面板设置页显示;只数已加载/已发现的作用域)。 */
1713
+ /** 规则条数(面板设置页显示;只数已加载/已发现的作用域,排除退役规则)。 */
1669
1714
  ruleStats() {
1670
1715
  this.discoverScopes()
1716
+ const retired = new Set(this.supersededIds())
1671
1717
  let total = 0
1672
1718
  let scopes = 0
1673
1719
  for (const s of this.store.allScopes()) {
1674
- const n = [...s.records.values()].filter((r) => r.kind === 'rule').length
1720
+ const n = [...s.records.values()].filter((r) => r.kind === 'rule' && !retired.has(r.id)).length
1675
1721
  if (n > 0) { total += n; scopes += 1 }
1676
1722
  }
1677
1723
  return { total, scopes }
@@ -16,6 +16,10 @@
16
16
          
17
17
 
18
18
 
19
+                        
20
+
21
+           
22
+
19
23
 
20
24
 
21
25
 
@@ -64,7 +68,7 @@
64
68
 
65
69
  export const defaultInjectSettings = {
66
70
  recall: { enabled: true, topK: 5, tokenBudget: 500 },
67
- digest: { enabled: true, topK: 8, tokenBudget: 400 },
71
+ digest: { enabled: true, topK: 8, tokenBudget: 800, hintBudget: 160, hintMaxItems: 6 },
68
72
  impression: { enabled: true, chars: 120 },
69
73
  policy: { enabled: true },
70
74
  rules: { enabled: true, maxItems: 10, tokenBudget: 300, delta: true },
@@ -118,6 +122,8 @@ export function normalizeInjectSettings(raw , base = def
118
122
  enabled: bool(digest.enabled, base.digest.enabled),
119
123
  topK: clampInt(digest.topK, 1, 30, base.digest.topK),
120
124
  tokenBudget: clampInt(digest.tokenBudget, 50, 4000, base.digest.tokenBudget),
125
+ hintBudget: clampInt(digest.hintBudget, 30, 1000, base.digest.hintBudget),
126
+ hintMaxItems: clampInt(digest.hintMaxItems, 1, 30, base.digest.hintMaxItems),
121
127
  },
122
128
  impression: {
123
129
  enabled: bool(impression.enabled, base.impression.enabled),