@restregular/dsh-memory 0.9.4 → 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.
- package/README.md +37 -5
- package/lib/client.js +909 -2
- package/lib/inject/apply.js +211 -14
- package/lib/inject/audit.js +87 -0
- package/lib/inject/digest.js +252 -15
- package/lib/inject/inject.js +419 -294
- package/lib/inject/rules.js +134 -9
- package/lib/inject/signals.js +176 -0
- package/lib/inject/state.js +163 -0
- package/lib/panel.js +6 -0
- package/lib/provider/index.js +52 -6
- package/lib/provider/inject-settings.js +7 -1
- package/package.json +1 -1
package/lib/inject/apply.js
CHANGED
|
@@ -3,12 +3,17 @@
|
|
|
3
3
|
*
|
|
4
4
|
* 注入分四层,全部可以在面板「记忆 → 设置」里单独开关:
|
|
5
5
|
* 1. recall —— 每轮按查询召回(可有可无;内容没变就不重复注入)
|
|
6
|
-
* 2. digest —— 会话第一步给一次"已有记忆"的摘要
|
|
6
|
+
* 2. digest —— 会话第一步给一次"已有记忆"的摘要 + 截断标签索引
|
|
7
7
|
* 3. policy —— 系统提示里的「记忆使用策略」常驻段
|
|
8
8
|
* 4. rules —— 系统提示里的「硬性规则」段(kind='rule' 的记忆)
|
|
9
9
|
* 另有 rulesDelta —— 旧会话在下一轮补一条新增/变更的规则(0.9.0;随 rules 开关)
|
|
10
10
|
* 另有 turnEnd —— 轮末提醒(下一轮开头提示"上一轮若有结论就记一条"),默认关。
|
|
11
11
|
*
|
|
12
|
+
* 0.9.6 起多了两条可验证的路径:
|
|
13
|
+
* - digest 的正文/标签/省略统计会写进 `InjectAuditStore`;
|
|
14
|
+
* - `session/event` 的 tool/call、tool/result、compaction 事件会生成行为信号,
|
|
15
|
+
* 在下一个 pre-step 做一次增量自动召回(已注入正文按会话 + 记忆 id/版本去重)。
|
|
16
|
+
*
|
|
12
17
|
* Message construction MUST go through createUserMessage: the loop reads
|
|
13
18
|
* `decision.kind` and downstream consumers read `message.source.kind`, so a
|
|
14
19
|
* hand-rolled `{ role, content }` object aborts the turn with
|
|
@@ -25,6 +30,7 @@ import {
|
|
|
25
30
|
MemoryInjector,
|
|
26
31
|
hasRecallMessage,
|
|
27
32
|
normalizeExtraScopes,
|
|
33
|
+
pickQueryText,
|
|
28
34
|
resolveInjectScopes,
|
|
29
35
|
sessionIdOf,
|
|
30
36
|
|
|
@@ -32,10 +38,20 @@ import {
|
|
|
32
38
|
|
|
33
39
|
|
|
34
40
|
} from './inject.js'
|
|
35
|
-
import {
|
|
36
|
-
import { renderRulesBlock } from './rules.js'
|
|
41
|
+
import { renderDigest, } from './digest.js'
|
|
42
|
+
import { renderRules, renderRulesBlock } from './rules.js'
|
|
37
43
|
import { RulesDeltaInjector } from './rules-delta.js'
|
|
38
44
|
import { POLICY_TEXT } from './policy.js'
|
|
45
|
+
import { InjectionState } from './state.js'
|
|
46
|
+
import { InjectAuditStore, auditBudget, auditQuery, } from './audit.js'
|
|
47
|
+
import {
|
|
48
|
+
BehaviorSignalStore,
|
|
49
|
+
detectPhase,
|
|
50
|
+
failureSignal,
|
|
51
|
+
highRiskSignal,
|
|
52
|
+
toolResultFailure,
|
|
53
|
+
|
|
54
|
+
} from './signals.js'
|
|
39
55
|
import { normalizeInjectSettings, } from '../provider/inject-settings.js'
|
|
40
56
|
|
|
41
57
|
|
|
@@ -49,8 +65,9 @@ import { normalizeInjectSettings, } from '../provider/inject
|
|
|
49
65
|
|
|
50
66
|
/** provider 在注入侧能提供的那部分能力(多余的方法都用可选判断)。 */
|
|
51
67
|
|
|
52
|
-
|
|
68
|
+
|
|
53
69
|
|
|
70
|
+
|
|
54
71
|
|
|
55
72
|
|
|
56
73
|
/** 轮末提醒的文案:短、祈使句,不占预算。 */
|
|
@@ -87,6 +104,43 @@ export function apply(ctx , config = {}) {
|
|
|
87
104
|
const readScopesFor = (memory , agent ) =>
|
|
88
105
|
resolveInjectScopes(memory, agent, extraScopes, ctx.get?.('sessions') )
|
|
89
106
|
|
|
107
|
+
/** 会话级注入状态(正文/标签/版本)与审计。 */
|
|
108
|
+
const injectionState = new InjectionState()
|
|
109
|
+
const audit = new InjectAuditStore()
|
|
110
|
+
const signals = new BehaviorSignalStore()
|
|
111
|
+
const callNames = new Map ()
|
|
112
|
+
let attachedMemory = null
|
|
113
|
+
|
|
114
|
+
const attachAudit = (memory ) => {
|
|
115
|
+
const target = memory
|
|
116
|
+
if (!target || target === attachedMemory || typeof target.attachInjectAudit !== 'function') return
|
|
117
|
+
try {
|
|
118
|
+
target.attachInjectAudit(audit)
|
|
119
|
+
attachedMemory = target
|
|
120
|
+
} catch { /* 审计是可选增强 */ }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const recordAudit = (entry ) => {
|
|
124
|
+
audit.record({ at: Date.now(), ...entry })
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const rememberCallName = (callId , name ) => {
|
|
128
|
+
if (typeof callId !== 'string' || callId === '') return
|
|
129
|
+
callNames.set(callId, name)
|
|
130
|
+
if (callNames.size > 500) {
|
|
131
|
+
const oldest = callNames.keys().next()
|
|
132
|
+
if (!oldest.done) callNames.delete(oldest.value)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const excludedIds = (memory , scopes ) => {
|
|
137
|
+
try {
|
|
138
|
+
return new Set(memory.supersededIds?.(scopes === undefined ? undefined : [...scopes]) ?? [])
|
|
139
|
+
} catch {
|
|
140
|
+
return new Set()
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
90
144
|
// One injector per activation: its repeat-suppression state must live across
|
|
91
145
|
// the steps of a turn (a per-step instance would re-inject every step).
|
|
92
146
|
const injector = new MemoryInjector(
|
|
@@ -102,6 +156,7 @@ export function apply(ctx , config = {}) {
|
|
|
102
156
|
},
|
|
103
157
|
recallCfg,
|
|
104
158
|
impressionCfg,
|
|
159
|
+
injectionState,
|
|
105
160
|
)
|
|
106
161
|
|
|
107
162
|
// 摘要:provider 没有 list(旧版/半边加载失败)时自动退化为"不发摘要"。
|
|
@@ -117,6 +172,63 @@ export function apply(ctx , config = {}) {
|
|
|
117
172
|
// 规则增量:一个激活一个实例,跨轮记住每条规则上一轮的样子(编辑后才补提醒)。
|
|
118
173
|
const rulesDelta = new RulesDeltaInjector()
|
|
119
174
|
|
|
175
|
+
/**
|
|
176
|
+
* 可观测行为信号 → 下一次 pre-step 的定向召回。
|
|
177
|
+
* 只消费宿主公开的 session/event;不读取隐藏思维链,不记录工具原始参数。
|
|
178
|
+
*/
|
|
179
|
+
try {
|
|
180
|
+
ctx.on('session/event', (session , event ) => {
|
|
181
|
+
try {
|
|
182
|
+
const sessionId = (session )?.id
|
|
183
|
+
if (typeof sessionId !== 'string' || sessionId === '') return
|
|
184
|
+
const type = (event )?.type
|
|
185
|
+
const data = (event )?.data ?? {}
|
|
186
|
+
const memory = ctx.get('memory')
|
|
187
|
+
if (memory) attachAudit(memory)
|
|
188
|
+
|
|
189
|
+
if (type === 'compaction/start' || type === 'compaction/end') {
|
|
190
|
+
// 上下文重建:旧去重状态不再可信,允许摘要/正文重新进入。
|
|
191
|
+
injectionState.clear(sessionId)
|
|
192
|
+
signals.clear(sessionId)
|
|
193
|
+
digester.reset(sessionId)
|
|
194
|
+
callNames.clear()
|
|
195
|
+
return
|
|
196
|
+
}
|
|
197
|
+
if (type === 'tool/call') {
|
|
198
|
+
const tool = typeof data.name === 'string' ? data.name : 'tool'
|
|
199
|
+
const args = typeof data.arguments === 'string' ? data.arguments : ''
|
|
200
|
+
rememberCallName(data.callId, tool)
|
|
201
|
+
const signal = highRiskSignal(tool, args)
|
|
202
|
+
if (signal && signals.note(sessionId, signal)) {
|
|
203
|
+
recordAudit({
|
|
204
|
+
sessionId, trigger: signal.reason, phase: signal.phase, query: signal.query,
|
|
205
|
+
candidates: 0, contentIds: [], hintIds: [], omitted: [], dedupSkipped: 0, budgets: {},
|
|
206
|
+
retrieval: { ok: true, count: 0 }, signal: { reason: signal.reason, tool: signal.tool },
|
|
207
|
+
})
|
|
208
|
+
}
|
|
209
|
+
return
|
|
210
|
+
}
|
|
211
|
+
if (type === 'tool/result') {
|
|
212
|
+
const failure = toolResultFailure(event)
|
|
213
|
+
if (!failure.isError) return
|
|
214
|
+
const message = data.message
|
|
215
|
+
const callId = message?.source?.callId
|
|
216
|
+
const tool = typeof callId === 'string' ? callNames.get(callId) : undefined
|
|
217
|
+
const signal = failureSignal(tool, failure.code, failure.message)
|
|
218
|
+
if (signals.note(sessionId, signal)) {
|
|
219
|
+
recordAudit({
|
|
220
|
+
sessionId, trigger: signal.reason, phase: signal.phase, query: signal.query,
|
|
221
|
+
candidates: 0, contentIds: [], hintIds: [], omitted: [], dedupSkipped: 0, budgets: {},
|
|
222
|
+
retrieval: { ok: true, count: 0 },
|
|
223
|
+
signal: { reason: signal.reason, ...(signal.errorClass ? { errorClass: signal.errorClass } : {}), ...(signal.tool ? { tool: signal.tool } : {}) },
|
|
224
|
+
})
|
|
225
|
+
}
|
|
226
|
+
return
|
|
227
|
+
}
|
|
228
|
+
} catch { /* 观察者失败绝不影响会话事件发布 */ }
|
|
229
|
+
})
|
|
230
|
+
} catch { /* 老宿主没有 session/event:行为召回自动缺席 */ }
|
|
231
|
+
|
|
120
232
|
// 系统提示:记忆使用策略(常驻) + 硬性规则(kind='rule')。
|
|
121
233
|
// 两者都用函数形式,面板开关/规则改动下一步就生效;文本稳定时可随前缀缓存。
|
|
122
234
|
ctx.inject(['systemPrompt'], (scoped ) => {
|
|
@@ -167,15 +279,18 @@ export function apply(ctx , config = {}) {
|
|
|
167
279
|
// optional capability: absence (or partial load) → pass through untouched
|
|
168
280
|
const memory = ctx.get('memory')
|
|
169
281
|
if (!memory) return decision
|
|
282
|
+
attachAudit(memory)
|
|
170
283
|
|
|
171
284
|
const agent = (input ).agent
|
|
172
285
|
const step = (input ).step ?? 1
|
|
173
286
|
if (step === 1) maxStep = 0
|
|
174
287
|
maxStep = Math.max(maxStep, step)
|
|
175
288
|
|
|
176
|
-
|
|
177
|
-
// 默认合并集合用同一套解析(provider 的 resolveReadScopes)。
|
|
289
|
+
const sessionId = sessionIdOf(agent)
|
|
178
290
|
const scopes = readScopesFor(memory, agent)
|
|
291
|
+
const exclude = excludedIds(memory, scopes)
|
|
292
|
+
const signal = sessionId === undefined ? undefined : signals.take(sessionId)
|
|
293
|
+
const userQuery = pickQueryText(input.messages )
|
|
179
294
|
|
|
180
295
|
// 摘要(每个会话一次)、召回(每轮有新内容时)、轮末提醒(下一轮首步)合成
|
|
181
296
|
// 同一条消息:内容没变就什么都不加,避免一步两张卡片。
|
|
@@ -194,15 +309,96 @@ export function apply(ctx , config = {}) {
|
|
|
194
309
|
}
|
|
195
310
|
if (settings) {
|
|
196
311
|
if (settings.digest.enabled) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
312
|
+
let digestStats
|
|
313
|
+
const digest = await digester.maybeDigestDetailed(sessionId, scopes, (records) => {
|
|
314
|
+
const rendered = renderDigest(records, settings.digest)
|
|
315
|
+
digestStats = rendered?.stats
|
|
316
|
+
return rendered
|
|
317
|
+
})
|
|
318
|
+
if (digest) {
|
|
319
|
+
if (sessionId !== undefined && digestStats !== undefined) {
|
|
320
|
+
injectionState.markContent(sessionId, digestStats.bodyEntries)
|
|
321
|
+
injectionState.markHint(sessionId, digestStats.hintEntries)
|
|
322
|
+
}
|
|
323
|
+
parts.push(digest.text)
|
|
324
|
+
if (digestStats !== undefined) {
|
|
325
|
+
recordAudit({
|
|
326
|
+
...(sessionId !== undefined ? { sessionId } : {}),
|
|
327
|
+
trigger: 'turn_start', phase: 'unknown',
|
|
328
|
+
candidates: digestStats.candidates,
|
|
329
|
+
contentIds: digestStats.bodyIds,
|
|
330
|
+
hintIds: digestStats.hintIds,
|
|
331
|
+
omitted: digestStats.omitted.map((entry) => ({ id: entry.id, reason: entry.reason })),
|
|
332
|
+
dedupSkipped: 0,
|
|
333
|
+
budgets: {
|
|
334
|
+
digest: auditBudget(settings.digest.tokenBudget, digest.tokens),
|
|
335
|
+
hints: auditBudget(settings.digest.hintBudget, digestStats.hint.tokens),
|
|
336
|
+
},
|
|
337
|
+
retrieval: { ok: true, count: digestStats.candidates },
|
|
338
|
+
})
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// 规则段每步都会在系统提示里重渲染;只在第一步记录一次审计,避免 200 条缓冲被刷爆。
|
|
344
|
+
if (step === 1 && settings.rules.enabled) {
|
|
345
|
+
try {
|
|
346
|
+
const rendered = renderRules(memory.listSync?.({ scopes, kind: 'rule', limit: 100 }) ?? [], settings.rules)
|
|
347
|
+
if (rendered) {
|
|
348
|
+
recordAudit({
|
|
349
|
+
...(sessionId !== undefined ? { sessionId } : {}),
|
|
350
|
+
trigger: 'rules', phase: 'unknown',
|
|
351
|
+
candidates: rendered.stats.candidates,
|
|
352
|
+
contentIds: [], hintIds: [], omitted: [], dedupSkipped: 0,
|
|
353
|
+
budgets: { rules: auditBudget(settings.rules.tokenBudget, rendered.tokens) },
|
|
354
|
+
retrieval: { ok: true, count: rendered.stats.candidates },
|
|
355
|
+
rules: {
|
|
356
|
+
injected: rendered.stats.injected,
|
|
357
|
+
omittedTopK: rendered.stats.omittedTopK,
|
|
358
|
+
omittedBudget: rendered.stats.omittedBudget,
|
|
359
|
+
omittedSuperseded: rendered.stats.omittedSuperseded,
|
|
360
|
+
mandatory: rendered.stats.mandatory,
|
|
361
|
+
mandatoryOmitted: rendered.stats.mandatoryOmitted,
|
|
362
|
+
},
|
|
363
|
+
})
|
|
364
|
+
}
|
|
365
|
+
} catch { /* 审计不是注入的前置条件 */ }
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// 行为信号优先:发布/推送/失败事件触发定向查询;没有信号才走普通用户消息召回。
|
|
369
|
+
// 同一轮普通 query 已处理过时跳过重复检索(去重状态仍会兜底)。
|
|
370
|
+
const skipNormalRecall = signal === undefined && step > 1 && sessionId !== undefined
|
|
371
|
+
&& userQuery !== '' && injectionState.lastQueryOf(sessionId) === userQuery
|
|
372
|
+
if (!skipNormalRecall) {
|
|
373
|
+
const recall = await injector.maybeInjectDetailed(
|
|
374
|
+
{ messages: input.messages , signal: input.signal, ...(scopes !== undefined ? { scopes } : {}), ...(sessionId !== undefined ? { sessionId } : {}) },
|
|
375
|
+
{ messages: decision.messages },
|
|
376
|
+
{
|
|
377
|
+
...(sessionId !== undefined ? { sessionId } : {}),
|
|
378
|
+
...(signal !== undefined ? { query: signal.query, trigger: signal.reason, force: true } : { trigger: 'turn_start' }),
|
|
379
|
+
excludeIds: exclude,
|
|
380
|
+
},
|
|
381
|
+
)
|
|
382
|
+
if (recall?.text) parts.push(recall.text)
|
|
383
|
+
if (recall) {
|
|
384
|
+
recordAudit({
|
|
385
|
+
...(sessionId !== undefined ? { sessionId } : {}),
|
|
386
|
+
trigger: signal?.reason ?? 'turn_start',
|
|
387
|
+
phase: signal?.phase ?? detectPhase(userQuery),
|
|
388
|
+
...(auditQuery(recall.query) !== undefined ? { query: auditQuery(recall.query) } : {}),
|
|
389
|
+
candidates: recall.candidates,
|
|
390
|
+
contentIds: recall.ids,
|
|
391
|
+
hintIds: [],
|
|
392
|
+
omitted: recall.omitted.map((entry) => ({ id: entry.id, reason: entry.reason })),
|
|
393
|
+
dedupSkipped: recall.dedupSkipped,
|
|
394
|
+
budgets: { recall: auditBudget(settings.recall.tokenBudget, recall.tokens) },
|
|
395
|
+
retrieval: recall.retrieval,
|
|
396
|
+
...(signal !== undefined
|
|
397
|
+
? { signal: { reason: signal.reason, ...(signal.errorClass ? { errorClass: signal.errorClass } : {}), ...(signal.tool ? { tool: signal.tool } : {}) } }
|
|
398
|
+
: {}),
|
|
399
|
+
})
|
|
400
|
+
}
|
|
200
401
|
}
|
|
201
|
-
const recall = await injector.maybeInject(
|
|
202
|
-
{ messages: input.messages , signal: input.signal, ...(scopes !== undefined ? { scopes } : {}) },
|
|
203
|
-
{ messages: decision.messages },
|
|
204
|
-
)
|
|
205
|
-
if (recall) parts.push(recall)
|
|
206
402
|
}
|
|
207
403
|
|
|
208
404
|
const text = parts.join('\n\n')
|
|
@@ -233,4 +429,5 @@ function isWellFormedMessage(message ) {
|
|
|
233
429
|
return typeof source?.kind === 'string'
|
|
234
430
|
}
|
|
235
431
|
|
|
432
|
+
|
|
236
433
|
//# sourceURL=source/memory-inject/src/apply.ts
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 注入审计:每次摘要/召回评估都留一条结构化记录。
|
|
3
|
+
*
|
|
4
|
+
* 第一版不要求复杂 UI:记录放在内存环形缓冲里,provider 通过
|
|
5
|
+
* `attachInjectAudit()` 挂上,面板 `GET /memory/api/audit?session=` 读取。
|
|
6
|
+
* 不记录任何凭据/令牌;检索查询会先过凭据脱敏,再最多截到 200 字。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { redactSecrets } from '../provider/secrets.js'
|
|
10
|
+
|
|
11
|
+
|
|
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
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
const MAX_ENTRIES = 200
|
|
54
|
+
|
|
55
|
+
export class InjectAuditStore {
|
|
56
|
+
entries = []
|
|
57
|
+
|
|
58
|
+
record(entry ) {
|
|
59
|
+
this.entries.push(entry)
|
|
60
|
+
if (this.entries.length > MAX_ENTRIES) this.entries.splice(0, this.entries.length - MAX_ENTRIES)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** 最新在前;可按会话过滤。 */
|
|
64
|
+
list(sessionId ) {
|
|
65
|
+
const list = sessionId === undefined
|
|
66
|
+
? this.entries
|
|
67
|
+
: this.entries.filter((entry) => entry.sessionId === sessionId)
|
|
68
|
+
return [...list].reverse()
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
clear() {
|
|
72
|
+
this.entries = []
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 查询文本里可能带用户内容:截断并折叠空白,不写凭据。 */
|
|
77
|
+
export function auditQuery(text ) {
|
|
78
|
+
if (typeof text !== 'string' || text.trim() === '') return undefined
|
|
79
|
+
return redactSecrets(text).replace(/\s+/g, ' ').trim().slice(0, 200)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function auditBudget(budget , used ) {
|
|
83
|
+
return { budget, used, remaining: Math.max(0, budget - used) }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
//# sourceURL=source/memory-inject/src/audit.ts
|