@restregular/dsh-memory 0.11.0 → 0.12.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.
- package/CHANGELOG.md +44 -0
- package/README.md +47 -9
- package/lib/client.js +481 -1
- package/lib/inject/apply.js +406 -114
- package/lib/inject/audit-types.js +219 -0
- package/lib/inject/audit.js +298 -55
- package/lib/inject/inject.js +72 -10
- package/lib/inject/rules-delta.js +58 -33
- package/lib/inject/rules.js +43 -23
- package/lib/panel.js +206 -6
- package/lib/provider/apply.js +7 -0
- package/lib/provider/index.js +181 -8
- package/lib/provider/scope.js +61 -1
- package/lib/tools/index.js +4 -4
- package/package.json +1 -1
package/lib/inject/inject.js
CHANGED
|
@@ -241,7 +241,10 @@ export function renderRecallBlock(
|
|
|
241
241
|
ids.push(h.record.id)
|
|
242
242
|
tokens += cost
|
|
243
243
|
}
|
|
244
|
-
if (lines.length === 0)
|
|
244
|
+
if (lines.length === 0) {
|
|
245
|
+
// 所有候选都超预算时也要保留 omitted 原因:审计需要解释“为什么什么都没注入”。
|
|
246
|
+
return { text: '', tokens: 0, ids: [], omitted }
|
|
247
|
+
}
|
|
245
248
|
return { text: `${header}\n${lines.join('\n')}`, tokens, ids, omitted }
|
|
246
249
|
}
|
|
247
250
|
|
|
@@ -270,15 +273,24 @@ export function recallText(text ) {
|
|
|
270
273
|
|
|
271
274
|
|
|
272
275
|
|
|
273
|
-
|
|
276
|
+
|
|
274
277
|
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
275
282
|
|
|
276
283
|
|
|
277
284
|
|
|
278
285
|
|
|
279
286
|
|
|
280
287
|
|
|
281
|
-
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
282
294
|
|
|
283
295
|
|
|
284
296
|
/**
|
|
@@ -337,7 +349,8 @@ export class MemoryInjector {
|
|
|
337
349
|
// 检索失败不能打断 agent 循环;审计会区分"失败"与"成功但无结果"。
|
|
338
350
|
return {
|
|
339
351
|
ids: [], candidates: 0, dedupSkipped: 0, updated: [], omitted: [], query,
|
|
340
|
-
tokens: 0,
|
|
352
|
+
tokens: 0, rawHits: 0, tagFiltered: 0, supersededExcluded: 0,
|
|
353
|
+
retrieval: { ok: false, count: 0, error: 'retrieval_failed', errorCode: 'retrieval_failed' },
|
|
341
354
|
}
|
|
342
355
|
}
|
|
343
356
|
// 成功检索后记住查询(即使没有新正文):同一 query 的后续 step 不再重复全量检索。
|
|
@@ -352,8 +365,10 @@ export class MemoryInjector {
|
|
|
352
365
|
const taggedHits = requiredTag === ''
|
|
353
366
|
? hits
|
|
354
367
|
: hits.filter((hit) => (hit.record.tags ?? []).some((tag) => textHasTag(String(tag), requiredTag)))
|
|
368
|
+
const tagFiltered = hits.length - taggedHits.length
|
|
355
369
|
const seenIds = options.excludeIds
|
|
356
370
|
const candidates = seenIds === undefined ? taggedHits : taggedHits.filter((hit) => !seenIds.has(hit.record.id))
|
|
371
|
+
const supersededExcluded = taggedHits.length - candidates.length
|
|
357
372
|
const { fresh, deduplicated, updated } = this.state.filterNewContent(
|
|
358
373
|
sessionId,
|
|
359
374
|
candidates.map((hit) => hit.record),
|
|
@@ -364,11 +379,25 @@ export class MemoryInjector {
|
|
|
364
379
|
const block = renderRecallBlock(freshHits, cfg, optionsForRender)
|
|
365
380
|
const base = {
|
|
366
381
|
ids: [], candidates: candidates.length, dedupSkipped: deduplicated.length, updated, omitted: [], query,
|
|
367
|
-
tokens: 0,
|
|
382
|
+
tokens: 0, rawHits: hits.length, tagFiltered, supersededExcluded,
|
|
383
|
+
retrieval: { ok: true, count: hits.length },
|
|
368
384
|
}
|
|
369
385
|
if (!block) return base
|
|
386
|
+
if (block.text === '') {
|
|
387
|
+
// 有候选但全部被 top_k/预算省略:保留原因,但不标记已注入。
|
|
388
|
+
return { ...base, omitted: block.omitted }
|
|
389
|
+
}
|
|
370
390
|
if (block.text === this.state.lastTextOf(sessionId)) {
|
|
371
|
-
return {
|
|
391
|
+
return {
|
|
392
|
+
...base,
|
|
393
|
+
ids: block.ids,
|
|
394
|
+
newlyAddedIds: [],
|
|
395
|
+
dedupIds: [...deduplicated, ...block.ids],
|
|
396
|
+
omitted: block.omitted,
|
|
397
|
+
tokens: block.tokens,
|
|
398
|
+
dedupSkipped: deduplicated.length + block.ids.length,
|
|
399
|
+
lastTextSame: true,
|
|
400
|
+
}
|
|
372
401
|
}
|
|
373
402
|
|
|
374
403
|
this.state.markContent(sessionId, block.ids.map((id) => {
|
|
@@ -377,7 +406,16 @@ export class MemoryInjector {
|
|
|
377
406
|
}))
|
|
378
407
|
this.state.setLastText(sessionId, block.text)
|
|
379
408
|
this.state.setLastQuery(sessionId, query)
|
|
380
|
-
return {
|
|
409
|
+
return {
|
|
410
|
+
...base,
|
|
411
|
+
text: block.text,
|
|
412
|
+
ids: block.ids,
|
|
413
|
+
newlyAddedIds: block.ids,
|
|
414
|
+
dedupIds: deduplicated,
|
|
415
|
+
omitted: block.omitted,
|
|
416
|
+
tokens: block.tokens,
|
|
417
|
+
lastTextSame: false,
|
|
418
|
+
}
|
|
381
419
|
}
|
|
382
420
|
}
|
|
383
421
|
|
|
@@ -429,15 +467,39 @@ export class MemoryDigester {
|
|
|
429
467
|
scopes ,
|
|
430
468
|
render ,
|
|
431
469
|
) {
|
|
432
|
-
|
|
470
|
+
const outcome = await this.maybeDigestOutcome(sessionId, scopes, render)
|
|
471
|
+
return outcome.status === 'ok' ? outcome.value : undefined
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* 带状态的摘要评估:审计需要区分“没有会话 / 已经做过 / 加载失败 / 成功但为空”。
|
|
476
|
+
* 仍然保持旧行为:每个会话只尝试一次(加载失败也不重试),避免每步打网络。
|
|
477
|
+
*/
|
|
478
|
+
async maybeDigestOutcome (
|
|
479
|
+
sessionId ,
|
|
480
|
+
scopes ,
|
|
481
|
+
render ,
|
|
482
|
+
)
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
{
|
|
487
|
+
if (sessionId === undefined) return { status: 'skipped', reasonCode: 'no_session' }
|
|
488
|
+
if (this.done.has(sessionId)) return { status: 'skipped', reasonCode: 'already_done' }
|
|
433
489
|
this.markDone(sessionId)
|
|
434
490
|
let records
|
|
435
491
|
try {
|
|
436
492
|
records = await this.list(scopes)
|
|
437
493
|
} catch {
|
|
438
|
-
return
|
|
494
|
+
return { status: 'failed', errorCode: 'source_load_failed' }
|
|
495
|
+
}
|
|
496
|
+
const value = render(records)
|
|
497
|
+
return {
|
|
498
|
+
status: 'ok',
|
|
499
|
+
...(value !== null && value !== undefined ? { value } : {}),
|
|
500
|
+
empty: value === null || value === undefined,
|
|
501
|
+
candidates: records.length,
|
|
439
502
|
}
|
|
440
|
-
return render(records) ?? undefined
|
|
441
503
|
}
|
|
442
504
|
|
|
443
505
|
markDone(sessionId ) {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
|
|
21
21
|
import {
|
|
22
22
|
latestSystemText,
|
|
23
|
-
|
|
23
|
+
renderRulesDelta,
|
|
24
24
|
ruleLine,
|
|
25
25
|
seenRuleLines,
|
|
26
26
|
|
|
@@ -55,6 +55,13 @@ export function derivedHistoryOf(agent )
|
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
58
65
|
/**
|
|
59
66
|
* 规则增量注入器。一个激活一个实例:它要跨轮记住"每条规则上一轮开始时长什么样",
|
|
60
67
|
* 才能在规则被编辑后补一次、在没变时不重复。
|
|
@@ -63,41 +70,59 @@ export class RulesDeltaInjector {
|
|
|
63
70
|
/** sessionId -> (规则 id -> 上一轮开始时的渲染行) */
|
|
64
71
|
baselines = new Map ()
|
|
65
72
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
73
|
+
/**
|
|
74
|
+
* 本轮第一步的增量正文;没有新增/变更(或这是会话的第一步)时返回 undefined。
|
|
75
|
+
*
|
|
76
|
+
* 规则范围与系统提示「硬性规则」段一致(`resolveReadScopes`,拿不到就 global),
|
|
77
|
+
* 免得提醒了本来就不该进常驻段的规则。只在一轮的第一步调用:增量消息会持久化,
|
|
78
|
+
* 每一步都算会重复;新会话(还没有系统提示)直接跳过 —— 系统提示这次就带全。
|
|
79
|
+
*/
|
|
80
|
+
maybeInject(agent , memory , cfg ) {
|
|
81
|
+
return this.maybeInjectDetailed(agent, memory, cfg).text
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 带结构化结果:审计需要知道本次补了哪些规则、原因是新增还是编辑。
|
|
86
|
+
* 业务渲染仍由 `renderRulesDeltaBlock` 负责,`maybeInject` 保持旧行为。
|
|
87
|
+
*/
|
|
88
|
+
maybeInjectDetailed(agent , memory , cfg ) {
|
|
89
|
+
const history = derivedHistoryOf(agent)
|
|
90
|
+
if (history === undefined || latestSystemText(history) === undefined) return { records: [], changes: [] }
|
|
91
|
+
let scopes = ['global']
|
|
92
|
+
try {
|
|
93
|
+
const resolved = memory.resolveReadScopes?.(agent)
|
|
94
|
+
if (Array.isArray(resolved) && resolved.length > 0) scopes = resolved
|
|
95
|
+
} catch { scopes = ['global'] }
|
|
96
|
+
let records
|
|
97
|
+
try {
|
|
98
|
+
records = memory.listSync?.({ scopes, kind: 'rule', limit: 100 }) ?? []
|
|
99
|
+
} catch {
|
|
100
|
+
return { records: [], changes: [] }
|
|
101
|
+
}
|
|
87
102
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
103
|
+
const sessionId = sessionIdOf(agent)
|
|
104
|
+
const seen = seenRuleLines(history)
|
|
105
|
+
const baseline = (sessionId === undefined ? undefined : this.baselines.get(sessionId)) ?? seedBaseline(records, seen)
|
|
106
|
+
const changes = []
|
|
107
|
+
const unseen = records.filter((record) => {
|
|
108
|
+
const line = ruleLine(record)
|
|
109
|
+
const before = baseline.get(record.id)
|
|
110
|
+
// 编辑过(同一 id 的行变了)一定要提醒;新规则只在模型还没在历史里见过时才提醒。
|
|
111
|
+
const changed = before !== undefined ? before !== line : !seen.has(line)
|
|
112
|
+
if (changed) changes.push({ id: record.id, reason: before !== undefined ? 'edited' : 'new' })
|
|
113
|
+
return changed
|
|
114
|
+
})
|
|
97
115
|
|
|
98
|
-
|
|
99
|
-
|
|
116
|
+
if (sessionId !== undefined) this.remember(sessionId, records)
|
|
117
|
+
const rendered = renderRulesDelta(unseen, cfg)
|
|
118
|
+
const text = rendered.text === '' ? undefined : rendered.text
|
|
119
|
+
return {
|
|
120
|
+
...(text !== undefined ? { text } : {}),
|
|
121
|
+
records: unseen,
|
|
122
|
+
changes,
|
|
123
|
+
...(text !== undefined ? { tokens: rendered.tokens } : {}),
|
|
100
124
|
}
|
|
125
|
+
}
|
|
101
126
|
|
|
102
127
|
remember(sessionId , records ) {
|
|
103
128
|
const snapshot = new Map ()
|
package/lib/inject/rules.js
CHANGED
|
@@ -67,6 +67,11 @@ export function orderRules(records ) {
|
|
|
67
67
|
|
|
68
68
|
|
|
69
69
|
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
70
75
|
|
|
71
76
|
|
|
72
77
|
|
|
@@ -85,13 +90,19 @@ function renderLines(
|
|
|
85
90
|
const retired = supersededIds(records)
|
|
86
91
|
const active = []
|
|
87
92
|
let omittedSuperseded = 0
|
|
93
|
+
const omitted = []
|
|
88
94
|
for (const record of records) {
|
|
89
|
-
if (retired.has(record.id)) {
|
|
95
|
+
if (retired.has(record.id)) {
|
|
96
|
+
omittedSuperseded += 1
|
|
97
|
+
omitted.push({ id: record.id, reason: 'superseded' })
|
|
98
|
+
continue
|
|
99
|
+
}
|
|
90
100
|
active.push(record)
|
|
91
101
|
}
|
|
92
102
|
|
|
93
103
|
let tokens = estimateTokens(header) + 4
|
|
94
104
|
const lines = []
|
|
105
|
+
const injectedIds = []
|
|
95
106
|
let omittedTopK = 0
|
|
96
107
|
let omittedBudget = 0
|
|
97
108
|
let budgetExhausted = false
|
|
@@ -103,11 +114,13 @@ function renderLines(
|
|
|
103
114
|
const mandatory = isMandatoryRule(record)
|
|
104
115
|
if (index >= cfg.maxItems) {
|
|
105
116
|
omittedTopK += 1
|
|
117
|
+
omitted.push({ id: record.id, reason: 'top_k' })
|
|
106
118
|
if (mandatory) mandatoryOmitted += 1
|
|
107
119
|
continue
|
|
108
120
|
}
|
|
109
121
|
if (budgetExhausted) {
|
|
110
122
|
omittedBudget += 1
|
|
123
|
+
omitted.push({ id: record.id, reason: 'budget' })
|
|
111
124
|
if (mandatory) mandatoryOmitted += 1
|
|
112
125
|
continue
|
|
113
126
|
}
|
|
@@ -116,35 +129,46 @@ function renderLines(
|
|
|
116
129
|
if (tokens + cost > budget) {
|
|
117
130
|
budgetExhausted = true
|
|
118
131
|
omittedBudget += 1
|
|
132
|
+
omitted.push({ id: record.id, reason: 'budget' })
|
|
119
133
|
if (mandatory) mandatoryOmitted += 1
|
|
120
134
|
continue
|
|
121
135
|
}
|
|
122
136
|
lines.push(line)
|
|
137
|
+
injectedIds.push(record.id)
|
|
123
138
|
tokens += cost
|
|
124
139
|
if (mandatory) mandatoryInjected += 1
|
|
125
140
|
}
|
|
126
141
|
|
|
142
|
+
const statsBase = {
|
|
143
|
+
candidates: records.length,
|
|
144
|
+
active: active.length,
|
|
145
|
+
injected: lines.length,
|
|
146
|
+
omittedTopK,
|
|
147
|
+
omittedBudget,
|
|
148
|
+
omittedSuperseded,
|
|
149
|
+
mandatory: mandatoryTotal,
|
|
150
|
+
mandatoryInjected,
|
|
151
|
+
mandatoryOmitted,
|
|
152
|
+
truncated: omitted.length > 0,
|
|
153
|
+
injectedIds,
|
|
154
|
+
omitted,
|
|
155
|
+
omittedIds: omitted.map((entry) => entry.id),
|
|
156
|
+
}
|
|
157
|
+
|
|
127
158
|
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
|
-
}
|
|
159
|
+
return { text: '', tokens: 0, count: 0, stats: statsBase }
|
|
136
160
|
}
|
|
137
161
|
// 一条都装不下且不是 mandatory 溢出的情况:文本为空,但统计仍保留给审计。
|
|
138
162
|
// 系统提示的调用方(renderRulesBlock)会把空文本转成 null,保持旧行为。
|
|
139
163
|
|
|
140
164
|
const notes = []
|
|
141
165
|
const noBody = lines.length === 0 && mandatoryOmitted === 0
|
|
142
|
-
const
|
|
143
|
-
if (!noBody &&
|
|
166
|
+
const omittedCount = omittedTopK + omittedBudget
|
|
167
|
+
if (!noBody && omittedCount > 0) {
|
|
144
168
|
const parts = []
|
|
145
169
|
if (omittedTopK > 0) parts.push(`条数上限 ${omittedTopK}`)
|
|
146
170
|
if (omittedBudget > 0) parts.push(`预算 ${omittedBudget}`)
|
|
147
|
-
notes.push(`(另有 ${
|
|
171
|
+
notes.push(`(另有 ${omittedCount} 条规则未展示:${parts.join(' / ')};需要时用 memory_search 查 kind=rule)`)
|
|
148
172
|
}
|
|
149
173
|
if (!noBody && mandatoryOmitted > 0) {
|
|
150
174
|
notes.push(`(警告:${mandatoryOmitted} 条 mandatory / pinned 规则未展示,当前规则不完整)`)
|
|
@@ -157,16 +181,8 @@ function renderLines(
|
|
|
157
181
|
tokens: tokens + extraTokens,
|
|
158
182
|
count: lines.length,
|
|
159
183
|
stats: {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
injected: lines.length,
|
|
163
|
-
omittedTopK,
|
|
164
|
-
omittedBudget,
|
|
165
|
-
omittedSuperseded,
|
|
166
|
-
mandatory: mandatoryTotal,
|
|
167
|
-
mandatoryInjected,
|
|
168
|
-
mandatoryOmitted,
|
|
169
|
-
truncated: omitted > 0,
|
|
184
|
+
...statsBase,
|
|
185
|
+
truncated: omittedCount > 0,
|
|
170
186
|
},
|
|
171
187
|
}
|
|
172
188
|
}
|
|
@@ -186,8 +202,12 @@ export function renderRulesBlock(records , cfg
|
|
|
186
202
|
* 渲染"新增/变更的规则"块。预算比常驻段留宽一点:增量块本来就是常驻段装不下
|
|
187
203
|
* 时才需要的补充,用同一个 300 token 的上限容易把最该提醒的一条又截掉。
|
|
188
204
|
*/
|
|
205
|
+
export function renderRulesDelta(records , cfg ) {
|
|
206
|
+
return renderLines(records, RULES_DELTA_HEADER, cfg, Math.max(cfg.tokenBudget, 600))
|
|
207
|
+
}
|
|
208
|
+
|
|
189
209
|
export function renderRulesDeltaBlock(records , cfg ) {
|
|
190
|
-
const text =
|
|
210
|
+
const text = renderRulesDelta(records, cfg).text
|
|
191
211
|
return text === '' ? null : text
|
|
192
212
|
}
|
|
193
213
|
|
package/lib/panel.js
CHANGED
|
@@ -221,6 +221,178 @@ function collectScope(scopeStore) {
|
|
|
221
221
|
return out.sort((a, b) => b.updatedAt - a.updatedAt)
|
|
222
222
|
}
|
|
223
223
|
|
|
224
|
+
/** 审计时间参数:Unix ms 或可解析的 ISO 字符串。 */
|
|
225
|
+
function parseAuditTime(value, name) {
|
|
226
|
+
if (value === null || value === undefined || value === '') return undefined
|
|
227
|
+
const numeric = Number(value)
|
|
228
|
+
if (Number.isFinite(numeric)) return numeric
|
|
229
|
+
const parsed = Date.parse(value)
|
|
230
|
+
if (Number.isFinite(parsed)) return parsed
|
|
231
|
+
throw new ApiError(400, 'AUDIT_QUERY_INVALID', `${name} 必须是毫秒时间戳或 ISO 时间`)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** 面板查询参数 → audit sink 的 AuditListQuery。 */
|
|
235
|
+
const AUDIT_CHANNELS = new Set(['digest', 'recall', 'rules', 'rules_delta', 'policy', 'nudge', 'signal', 'lifecycle'])
|
|
236
|
+
const AUDIT_OUTCOMES = new Set(['success', 'failed', 'skipped', 'partial', 'unknown'])
|
|
237
|
+
function parseAuditQuery(url) {
|
|
238
|
+
const text = (name) => {
|
|
239
|
+
const value = url.searchParams.get(name)
|
|
240
|
+
const trimmed = value === null ? '' : value.trim()
|
|
241
|
+
return trimmed === '' ? undefined : trimmed
|
|
242
|
+
}
|
|
243
|
+
const limitRaw = text('limit')
|
|
244
|
+
let limit
|
|
245
|
+
if (limitRaw !== undefined) {
|
|
246
|
+
if (!/^\d+$/.test(limitRaw)) throw new ApiError(400, 'AUDIT_QUERY_INVALID', 'limit 必须是 1..200 的整数')
|
|
247
|
+
limit = Number(limitRaw)
|
|
248
|
+
if (limit < 1 || limit > 200) throw new ApiError(400, 'AUDIT_QUERY_INVALID', 'limit 必须在 1..200 之间')
|
|
249
|
+
}
|
|
250
|
+
const cursor = text('cursor')
|
|
251
|
+
if (cursor !== undefined && !/^\d+$/.test(cursor)) {
|
|
252
|
+
throw new ApiError(400, 'AUDIT_CURSOR_INVALID', 'cursor 必须是 seq 数字')
|
|
253
|
+
}
|
|
254
|
+
const channel = text('channel')
|
|
255
|
+
if (channel !== undefined && !AUDIT_CHANNELS.has(channel)) {
|
|
256
|
+
throw new ApiError(400, 'AUDIT_QUERY_INVALID', `未知 channel: ${channel}`)
|
|
257
|
+
}
|
|
258
|
+
const outcome = text('outcome')
|
|
259
|
+
if (outcome !== undefined && !AUDIT_OUTCOMES.has(outcome)) {
|
|
260
|
+
throw new ApiError(400, 'AUDIT_QUERY_INVALID', `未知 outcome: ${outcome}`)
|
|
261
|
+
}
|
|
262
|
+
const since = parseAuditTime(text('since'), 'since')
|
|
263
|
+
const until = parseAuditTime(text('until'), 'until')
|
|
264
|
+
if (since !== undefined && until !== undefined && since > until) {
|
|
265
|
+
throw new ApiError(400, 'AUDIT_QUERY_INVALID', 'since 不能晚于 until')
|
|
266
|
+
}
|
|
267
|
+
return {
|
|
268
|
+
...(text('session') !== undefined ? { sessionId: text('session') } : {}),
|
|
269
|
+
...(text('trace') !== undefined ? { traceId: text('trace') } : {}),
|
|
270
|
+
...(text('operation') !== undefined ? { operationId: text('operation') } : {}),
|
|
271
|
+
...(text('memory') !== undefined ? { memoryId: text('memory') } : {}),
|
|
272
|
+
...(channel !== undefined ? { channel } : {}),
|
|
273
|
+
...(text('trigger') !== undefined ? { trigger: text('trigger') } : {}),
|
|
274
|
+
...(outcome !== undefined ? { outcome } : {}),
|
|
275
|
+
...(url.searchParams.get('problem') === '1' ? { problem: true } : {}),
|
|
276
|
+
...(since !== undefined ? { since } : {}),
|
|
277
|
+
...(until !== undefined ? { until } : {}),
|
|
278
|
+
...(limit !== undefined ? { limit } : {}),
|
|
279
|
+
...(cursor !== undefined ? { cursor } : {}),
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function publicAuditQuery(query) {
|
|
284
|
+
return {
|
|
285
|
+
...(query.sessionId !== undefined ? { sessionId: query.sessionId } : {}),
|
|
286
|
+
...(query.traceId !== undefined ? { traceId: query.traceId } : {}),
|
|
287
|
+
...(query.operationId !== undefined ? { operationId: query.operationId } : {}),
|
|
288
|
+
...(query.memoryId !== undefined ? { memoryId: query.memoryId } : {}),
|
|
289
|
+
...(query.channel !== undefined ? { channel: query.channel } : {}),
|
|
290
|
+
...(query.trigger !== undefined ? { trigger: query.trigger } : {}),
|
|
291
|
+
...(query.outcome !== undefined ? { outcome: query.outcome } : {}),
|
|
292
|
+
...(query.problem === true ? { problem: true } : {}),
|
|
293
|
+
...(query.since !== undefined ? { since: query.since } : {}),
|
|
294
|
+
...(query.until !== undefined ? { until: query.until } : {}),
|
|
295
|
+
limit: query.limit ?? 50,
|
|
296
|
+
...(query.cursor !== undefined ? { cursor: query.cursor } : {}),
|
|
297
|
+
scope: query.sessionId !== undefined ? 'session' : 'all',
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function emptyAuditStats() {
|
|
302
|
+
return {
|
|
303
|
+
entries: 0, capacity: 0, perSessionCapacity: 0, dropped: 0,
|
|
304
|
+
writeErrors: 0, persistence: 'memory',
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function auditStats(memory) {
|
|
309
|
+
try {
|
|
310
|
+
const stats = typeof memory?.injectAuditStats === 'function' ? memory.injectAuditStats() : undefined
|
|
311
|
+
return stats && typeof stats === 'object' ? stats : emptyAuditStats()
|
|
312
|
+
} catch {
|
|
313
|
+
return { ...emptyAuditStats(), persistence: 'degraded' }
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** 旧 provider 只有 injectAudit(list) 时,在面板侧补最小过滤/分页。 */
|
|
318
|
+
function legacyAuditPage(memory, query) {
|
|
319
|
+
const all = (() => {
|
|
320
|
+
try { return typeof memory?.injectAudit === 'function' ? memory.injectAudit(query.sessionId) : [] } catch { return [] }
|
|
321
|
+
})()
|
|
322
|
+
const entries = Array.isArray(all) ? all : []
|
|
323
|
+
const filtered = entries.filter((entry) => {
|
|
324
|
+
if (!entry || typeof entry !== 'object') return false
|
|
325
|
+
if (query.channel !== undefined && entry.channel !== query.channel) return false
|
|
326
|
+
if (query.trigger !== undefined && entry.trigger !== query.trigger) return false
|
|
327
|
+
if (query.outcome !== undefined && entry.outcome !== query.outcome) return false
|
|
328
|
+
if (query.problem === true) {
|
|
329
|
+
const delivery = entry.delivery
|
|
330
|
+
const omitted = Array.isArray(entry.omitted) ? entry.omitted.length : 0
|
|
331
|
+
if (!(entry.outcome === 'failed' || entry.outcome === 'partial' || omitted > 0 || (delivery && delivery.status !== 'appended' && delivery.status !== 'unknown'))) return false
|
|
332
|
+
}
|
|
333
|
+
if (query.memoryId !== undefined) {
|
|
334
|
+
const ids = [
|
|
335
|
+
...(entry.contentIds ?? []), ...(entry.hintIds ?? []),
|
|
336
|
+
...((entry.selected && entry.selected.contentIds) ?? []),
|
|
337
|
+
...((entry.omitted ?? []).map((item) => item.id)),
|
|
338
|
+
]
|
|
339
|
+
if (!ids.includes(query.memoryId)) return false
|
|
340
|
+
}
|
|
341
|
+
if (query.since !== undefined && !(entry.at >= query.since)) return false
|
|
342
|
+
if (query.until !== undefined && !(entry.at <= query.until)) return false
|
|
343
|
+
return true
|
|
344
|
+
})
|
|
345
|
+
const limit = query.limit ?? 50
|
|
346
|
+
const start = query.cursor === undefined ? 0 : Number(query.cursor)
|
|
347
|
+
const page = filtered.slice(start, start + limit)
|
|
348
|
+
return {
|
|
349
|
+
entries: page,
|
|
350
|
+
total: filtered.length,
|
|
351
|
+
...(start + limit < filtered.length ? { nextCursor: String(start + limit) } : {}),
|
|
352
|
+
stats: emptyAuditStats(),
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function auditPage(memory, query) {
|
|
357
|
+
if (memory === undefined || memory === null) {
|
|
358
|
+
throw new ApiError(503, 'AUDIT_UNAVAILABLE', 'memory provider 未加载')
|
|
359
|
+
}
|
|
360
|
+
try {
|
|
361
|
+
if (typeof memory.injectAuditPage === 'function') {
|
|
362
|
+
const page = memory.injectAuditPage(query)
|
|
363
|
+
if (page && typeof page === 'object' && Array.isArray(page.entries)) {
|
|
364
|
+
return {
|
|
365
|
+
entries: page.entries,
|
|
366
|
+
total: typeof page.total === 'number' ? page.total : page.entries.length,
|
|
367
|
+
...(typeof page.nextCursor === 'string' ? { nextCursor: page.nextCursor } : {}),
|
|
368
|
+
stats: page.stats && typeof page.stats === 'object' ? page.stats : emptyAuditStats(),
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (typeof memory.injectAudit === 'function') return legacyAuditPage(memory, query)
|
|
373
|
+
throw new ApiError(503, 'AUDIT_UNAVAILABLE', 'provider 未提供注入审计接口(请升级 @restregular/dsh-memory)')
|
|
374
|
+
} catch (error) {
|
|
375
|
+
if (error instanceof ApiError) throw error
|
|
376
|
+
if (error && typeof error.code === 'string' && error.code.startsWith('AUDIT_')) {
|
|
377
|
+
throw new ApiError(400, error.code, error.message || '审计查询参数无效')
|
|
378
|
+
}
|
|
379
|
+
throw error
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** 导出:按当前筛选循环取页(上限 200/页,最多 50 页 = 10000 条)。 */
|
|
384
|
+
function collectAuditEntries(memory, query) {
|
|
385
|
+
const out = []
|
|
386
|
+
let cursor
|
|
387
|
+
for (let i = 0; i < 50; i++) {
|
|
388
|
+
const page = auditPage(memory, { ...query, ...(cursor !== undefined ? { cursor } : {}), limit: 200 })
|
|
389
|
+
out.push(...page.entries)
|
|
390
|
+
if (page.nextCursor === undefined) break
|
|
391
|
+
cursor = page.nextCursor
|
|
392
|
+
}
|
|
393
|
+
return out
|
|
394
|
+
}
|
|
395
|
+
|
|
224
396
|
async function handle(ctx, routePath, req, res) {
|
|
225
397
|
const url = new URL(req.url ?? '/', 'http://dsh.local')
|
|
226
398
|
const path = url.pathname.slice(routePath.length) || '/'
|
|
@@ -233,6 +405,40 @@ async function handle(ctx, routePath, req, res) {
|
|
|
233
405
|
}
|
|
234
406
|
try {
|
|
235
407
|
const memory = ctx.get('memory')
|
|
408
|
+
// 审计路由只依赖注入半边挂载的 sink,不依赖记忆 store;先于 store 检查。
|
|
409
|
+
if (method === 'GET' && path === '/audit') {
|
|
410
|
+
const query = parseAuditQuery(url)
|
|
411
|
+
const value = auditPage(memory, query)
|
|
412
|
+
writeJson(res, { ok: true, value: { ...value, query: publicAuditQuery(query) } })
|
|
413
|
+
return
|
|
414
|
+
}
|
|
415
|
+
if (method === 'GET' && path === '/audit/stats') {
|
|
416
|
+
writeJson(res, { ok: true, value: auditStats(memory) })
|
|
417
|
+
return
|
|
418
|
+
}
|
|
419
|
+
if (method === 'GET' && path === '/audit/export') {
|
|
420
|
+
const query = parseAuditQuery(url)
|
|
421
|
+
const format = url.searchParams.get('format') === 'jsonl' ? 'jsonl' : 'json'
|
|
422
|
+
const entries = collectAuditEntries(memory, query)
|
|
423
|
+
const body = format === 'jsonl'
|
|
424
|
+
? entries.map((entry) => JSON.stringify(entry)).join('\n') + (entries.length > 0 ? '\n' : '')
|
|
425
|
+
: JSON.stringify({
|
|
426
|
+
schemaVersion: 2,
|
|
427
|
+
exportedAt: Date.now(),
|
|
428
|
+
query: publicAuditQuery(query),
|
|
429
|
+
count: entries.length,
|
|
430
|
+
entries,
|
|
431
|
+
}, null, 2) + '\n'
|
|
432
|
+
try {
|
|
433
|
+
res.writeHead(200, {
|
|
434
|
+
'content-type': format === 'jsonl' ? 'application/x-ndjson; charset=utf-8' : 'application/json; charset=utf-8',
|
|
435
|
+
'content-disposition': `attachment; filename="dsh-memory-audit.${format === 'jsonl' ? 'jsonl' : 'json'}"`,
|
|
436
|
+
'cache-control': 'no-store',
|
|
437
|
+
})
|
|
438
|
+
res.end(body)
|
|
439
|
+
} catch { /* client disconnected */ }
|
|
440
|
+
return
|
|
441
|
+
}
|
|
236
442
|
if (!memory?.store) throw new ApiError(503, 'MEMORY_UNAVAILABLE', 'memory provider 未加载')
|
|
237
443
|
const store = memory.store
|
|
238
444
|
// Scopes are created lazily, so a fresh process only knows the ones it has
|
|
@@ -240,12 +446,6 @@ async function handle(ctx, routePath, req, res) {
|
|
|
240
446
|
// memories from earlier sessions included) — best-effort discovery, cached.
|
|
241
447
|
discoverScopes(memory, store)
|
|
242
448
|
|
|
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
|
-
}
|
|
249
449
|
if (method === 'GET' && path === '/status') {
|
|
250
450
|
const stats = typeof memory.scopeStats === 'function'
|
|
251
451
|
? memory.scopeStats()
|
package/lib/provider/apply.js
CHANGED
|
@@ -27,6 +27,13 @@ export class CordisMemoryService extends Service {
|
|
|
27
27
|
constructor(ctx , config = {}) {
|
|
28
28
|
super(ctx, 'memory')
|
|
29
29
|
this.impl = new LocalMemoryService(config)
|
|
30
|
+
// 0.11.1:同一个目录因为 `D:/x` 与 `D:\x` 写法不同而分叉成两个 scope 的历史数据,
|
|
31
|
+
// 在装配期合并一次(每进程一次;失败只影响合并,不影响读写)。
|
|
32
|
+
try {
|
|
33
|
+
this.impl.reconcileScopes()
|
|
34
|
+
} catch {
|
|
35
|
+
// 合并失败:两个 scope 继续并存(读写仍然正常),不阻断插件加载
|
|
36
|
+
}
|
|
30
37
|
ctx.effect(() => () => this.impl.flushSync())
|
|
31
38
|
this.attachRerank(ctx, config)
|
|
32
39
|
}
|