@restregular/dsh-memory 0.13.2 → 0.14.1
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 +14 -0
- package/README.md +14 -0
- package/lib/client.js +823 -18
- package/lib/inject/apply.js +74 -6
- package/lib/inject/audit-types.js +6 -0
- package/lib/inject/signals.js +8 -0
- package/lib/panel.js +62 -2
- package/package.json +1 -1
package/lib/inject/apply.js
CHANGED
|
@@ -276,7 +276,11 @@ export function apply(ctx , config = {}) {
|
|
|
276
276
|
return limited
|
|
277
277
|
}
|
|
278
278
|
|
|
279
|
-
const observeSignal = (
|
|
279
|
+
const observeSignal = (
|
|
280
|
+
sessionId ,
|
|
281
|
+
signal ,
|
|
282
|
+
context = {},
|
|
283
|
+
) => {
|
|
280
284
|
if (!signal || !signals.note(sessionId, signal)) return
|
|
281
285
|
const query = sanitizeAuditText(signal.query)
|
|
282
286
|
recordAudit({
|
|
@@ -288,6 +292,9 @@ export function apply(ctx , config = {}) {
|
|
|
288
292
|
sessionId,
|
|
289
293
|
trigger: signal.reason,
|
|
290
294
|
phase: signal.phase,
|
|
295
|
+
...(context.turn !== undefined ? { turn: context.turn } : {}),
|
|
296
|
+
...(context.step !== undefined ? { step: context.step } : {}),
|
|
297
|
+
...(signal.callId !== undefined ? { operationId: signal.callId } : {}),
|
|
291
298
|
...(query !== undefined ? { query } : {}),
|
|
292
299
|
signal: {
|
|
293
300
|
key: signal.key,
|
|
@@ -296,10 +303,37 @@ export function apply(ctx , config = {}) {
|
|
|
296
303
|
...(signal.tool ? { tool: signal.tool } : {}),
|
|
297
304
|
...(signal.tag ? { tag: signal.tag } : {}),
|
|
298
305
|
...(signal.source ? { source: signal.source } : {}),
|
|
306
|
+
...(signal.callId !== undefined ? { callId: signal.callId } : {}),
|
|
299
307
|
},
|
|
300
308
|
})
|
|
301
309
|
}
|
|
302
310
|
|
|
311
|
+
/** 记忆工具自身的调用:不属于注入评估,但它是审计里最有用的轨迹锚点。 */
|
|
312
|
+
const MEMORY_TOOL_NAMES = new Set(['memory_save', 'memory_search', 'memory_forget', 'memory_list', 'memory_get'])
|
|
313
|
+
const rememberMemoryToolCall = (
|
|
314
|
+
sessionId ,
|
|
315
|
+
data ,
|
|
316
|
+
) => {
|
|
317
|
+
const callId = typeof data.callId === 'string' ? data.callId : undefined
|
|
318
|
+
const tool = typeof data.name === 'string' ? data.name : undefined
|
|
319
|
+
if (callId === undefined || tool === undefined || !MEMORY_TOOL_NAMES.has(tool)) return
|
|
320
|
+
const turn = typeof data.turn === 'number' ? data.turn : undefined
|
|
321
|
+
const step = typeof data.step === 'number' ? data.step : undefined
|
|
322
|
+
recordAudit({
|
|
323
|
+
kind: 'signal',
|
|
324
|
+
channel: 'signal',
|
|
325
|
+
stage: 'tool_call',
|
|
326
|
+
outcome: 'success',
|
|
327
|
+
reasonCode: 'memory_tool',
|
|
328
|
+
trigger: 'memory_tool',
|
|
329
|
+
sessionId,
|
|
330
|
+
operationId: callId,
|
|
331
|
+
...(turn !== undefined ? { turn } : {}),
|
|
332
|
+
...(step !== undefined ? { step } : {}),
|
|
333
|
+
signal: { key: callId, reason: 'memory_tool', tool, source: 'tool', callId },
|
|
334
|
+
})
|
|
335
|
+
}
|
|
336
|
+
|
|
303
337
|
const injector = new MemoryInjector(
|
|
304
338
|
async (query, signal, scopes) => {
|
|
305
339
|
const memory = ctx.get('memory')
|
|
@@ -343,6 +377,10 @@ export function apply(ctx , config = {}) {
|
|
|
343
377
|
const memory = ctx.get('memory')
|
|
344
378
|
if (memory) attachAudit(memory)
|
|
345
379
|
|
|
380
|
+
// 记忆工具调用先落一条锚点审计:即使自动召回关闭、或该工具没有命中
|
|
381
|
+
// 任何触发标签,用户也能从审计面板/记忆视图精确跳回轨迹里的那一次调用。
|
|
382
|
+
if (type === 'tool/call') rememberMemoryToolCall(sessionId, data)
|
|
383
|
+
|
|
346
384
|
if (type === 'compaction/start' || type === 'compaction/end') {
|
|
347
385
|
// 上下文重建:旧去重状态不再可信,允许摘要/正文重新进入。
|
|
348
386
|
// 审计也要留下分界,否则压缩前后的记录会被误读成同一段上下文。
|
|
@@ -371,17 +409,24 @@ export function apply(ctx , config = {}) {
|
|
|
371
409
|
|
|
372
410
|
if (type === 'tool/call') {
|
|
373
411
|
const tool = typeof data.name === 'string' ? data.name : 'tool'
|
|
412
|
+
const callId = typeof data.callId === 'string' ? data.callId : undefined
|
|
374
413
|
const args = typeof data.arguments === 'string'
|
|
375
414
|
? data.arguments
|
|
376
415
|
: (() => { try { return JSON.stringify(data.arguments ?? '') } catch { return '' } })()
|
|
377
416
|
const description = auto.useToolDescription ? toolCallDescription(data.arguments) : undefined
|
|
378
417
|
rememberCallMeta(data.callId, tool, description)
|
|
418
|
+
const turn = typeof data.turn === 'number' ? data.turn : undefined
|
|
419
|
+
const step = typeof data.step === 'number' ? data.step : undefined
|
|
379
420
|
observeSignal(sessionId, highRiskSignal(tool, args, {
|
|
380
421
|
tags,
|
|
381
422
|
...(description !== undefined ? { description } : {}),
|
|
423
|
+
...(callId !== undefined ? { callId } : {}),
|
|
382
424
|
useToolDescription: auto.useToolDescription,
|
|
383
425
|
maxQueryChars: auto.maxQueryChars,
|
|
384
|
-
})
|
|
426
|
+
}), {
|
|
427
|
+
...(turn !== undefined ? { turn } : {}),
|
|
428
|
+
...(step !== undefined ? { step } : {}),
|
|
429
|
+
})
|
|
385
430
|
return
|
|
386
431
|
}
|
|
387
432
|
if (type === 'tool/result') {
|
|
@@ -391,10 +436,16 @@ export function apply(ctx , config = {}) {
|
|
|
391
436
|
const callId = message?.source?.callId
|
|
392
437
|
const meta = typeof callId === 'string' ? callMeta.get(callId) : undefined
|
|
393
438
|
const description = auto.useToolDescription ? meta?.description : undefined
|
|
439
|
+
const turn = typeof data.turn === 'number' ? data.turn : undefined
|
|
440
|
+
const step = typeof data.step === 'number' ? data.step : undefined
|
|
394
441
|
observeSignal(sessionId, failureSignal(meta?.name, failure.code, failure.message, {
|
|
395
442
|
...(description !== undefined ? { description } : {}),
|
|
443
|
+
...(typeof callId === 'string' ? { callId } : {}),
|
|
396
444
|
maxQueryChars: auto.maxQueryChars,
|
|
397
|
-
})
|
|
445
|
+
}), {
|
|
446
|
+
...(turn !== undefined ? { turn } : {}),
|
|
447
|
+
...(step !== undefined ? { step } : {}),
|
|
448
|
+
})
|
|
398
449
|
return
|
|
399
450
|
}
|
|
400
451
|
// 可见 assistant 输出 / 任务清单 / 命令 / goal:命中记忆标签就排队一次召回。
|
|
@@ -528,14 +579,30 @@ export function apply(ctx , config = {}) {
|
|
|
528
579
|
recordAudit(auditBase('signal', {
|
|
529
580
|
kind: 'signal', stage: 'signal_dropped', outcome: 'skipped', reasonCode: 'auto_scope_unavailable',
|
|
530
581
|
trigger: signal.reason, phase: signal.phase,
|
|
531
|
-
|
|
582
|
+
...(signal.callId !== undefined ? { operationId: signal.callId } : {}),
|
|
583
|
+
signal: {
|
|
584
|
+
key: signal.key,
|
|
585
|
+
reason: signal.reason,
|
|
586
|
+
...(signal.tag ? { tag: signal.tag } : {}),
|
|
587
|
+
...(signal.tool ? { tool: signal.tool } : {}),
|
|
588
|
+
...(signal.callId !== undefined ? { callId: signal.callId } : {}),
|
|
589
|
+
},
|
|
532
590
|
}))
|
|
533
591
|
}
|
|
534
592
|
if (usableSignal !== undefined) {
|
|
535
593
|
recordAudit(auditBase('signal', {
|
|
536
594
|
kind: 'signal', stage: 'signal_consumed', outcome: 'success', reasonCode: 'consumed',
|
|
537
595
|
trigger: usableSignal.reason, phase: usableSignal.phase,
|
|
538
|
-
|
|
596
|
+
...(usableSignal.callId !== undefined ? { operationId: usableSignal.callId } : {}),
|
|
597
|
+
signal: {
|
|
598
|
+
key: usableSignal.key,
|
|
599
|
+
reason: usableSignal.reason,
|
|
600
|
+
...(usableSignal.tag ? { tag: usableSignal.tag } : {}),
|
|
601
|
+
...(usableSignal.tool ? { tool: usableSignal.tool } : {}),
|
|
602
|
+
...(usableSignal.errorClass ? { errorClass: usableSignal.errorClass } : {}),
|
|
603
|
+
...(usableSignal.source ? { source: usableSignal.source } : {}),
|
|
604
|
+
...(usableSignal.callId !== undefined ? { callId: usableSignal.callId } : {}),
|
|
605
|
+
},
|
|
539
606
|
}))
|
|
540
607
|
}
|
|
541
608
|
|
|
@@ -755,6 +822,7 @@ export function apply(ctx , config = {}) {
|
|
|
755
822
|
kind: 'evaluation', stage: 'searched', outcome, reasonCode,
|
|
756
823
|
trigger: usableSignal?.reason ?? 'turn_start',
|
|
757
824
|
...(usableSignal !== undefined ? { phase: usableSignal.phase } : {}),
|
|
825
|
+
...(usableSignal?.callId !== undefined ? { operationId: usableSignal.callId } : {}),
|
|
758
826
|
...(queryField !== undefined ? { query: queryField } : {}),
|
|
759
827
|
candidates: recall.candidates,
|
|
760
828
|
filter: {
|
|
@@ -785,7 +853,7 @@ export function apply(ctx , config = {}) {
|
|
|
785
853
|
...(recallScopes !== undefined ? { scopes: [...recallScopes] } : {}),
|
|
786
854
|
limits: { topK: settings.recall.topK, maxQueryChars: settings.autoRecall.maxQueryChars },
|
|
787
855
|
...(usableSignal !== undefined
|
|
788
|
-
? { signal: { key: usableSignal.key, reason: usableSignal.reason, ...(usableSignal.errorClass ? { errorClass: usableSignal.errorClass } : {}), ...(usableSignal.tool ? { tool: usableSignal.tool } : {}), ...(usableSignal.tag ? { tag: usableSignal.tag } : {}), ...(usableSignal.source ? { source: usableSignal.source } : {}) } }
|
|
856
|
+
? { signal: { key: usableSignal.key, reason: usableSignal.reason, ...(usableSignal.errorClass ? { errorClass: usableSignal.errorClass } : {}), ...(usableSignal.tool ? { tool: usableSignal.tool } : {}), ...(usableSignal.tag ? { tag: usableSignal.tag } : {}), ...(usableSignal.source ? { source: usableSignal.source } : {}), ...(usableSignal.callId ? { callId: usableSignal.callId } : {}) } }
|
|
789
857
|
: {}),
|
|
790
858
|
}))
|
|
791
859
|
}
|
package/lib/inject/signals.js
CHANGED
|
@@ -32,6 +32,8 @@
|
|
|
32
32
|
|
|
33
33
|
|
|
34
34
|
|
|
35
|
+
|
|
36
|
+
|
|
35
37
|
|
|
36
38
|
|
|
37
39
|
|
|
@@ -40,11 +42,15 @@
|
|
|
40
42
|
|
|
41
43
|
|
|
42
44
|
|
|
45
|
+
|
|
46
|
+
|
|
43
47
|
|
|
44
48
|
|
|
45
49
|
|
|
46
50
|
|
|
47
51
|
|
|
52
|
+
|
|
53
|
+
|
|
48
54
|
|
|
49
55
|
|
|
50
56
|
const DEFAULT_MAX_QUERY = 240
|
|
@@ -196,6 +202,7 @@ export function highRiskSignal(tool , argumentsText , options
|
|
|
196
202
|
tool,
|
|
197
203
|
tag,
|
|
198
204
|
source: 'tool',
|
|
205
|
+
...(options.callId !== undefined ? { callId: options.callId } : {}),
|
|
199
206
|
}
|
|
200
207
|
}
|
|
201
208
|
return undefined
|
|
@@ -334,6 +341,7 @@ export function failureSignal(
|
|
|
334
341
|
...(tool !== undefined ? { tool } : {}),
|
|
335
342
|
errorClass,
|
|
336
343
|
source: 'failure',
|
|
344
|
+
...(options.callId !== undefined ? { callId: options.callId } : {}),
|
|
337
345
|
}
|
|
338
346
|
}
|
|
339
347
|
|
package/lib/panel.js
CHANGED
|
@@ -355,8 +355,52 @@ function legacyAuditPage(memory, query) {
|
|
|
355
355
|
}
|
|
356
356
|
}
|
|
357
357
|
|
|
358
|
-
|
|
359
|
-
|
|
358
|
+
/**
|
|
359
|
+
* 把一个审计条目的 turn/step 锚点解析成轨迹可直接聚焦的 tool call id。
|
|
360
|
+
*
|
|
361
|
+
* Trajectory 的深链只接受 callId(它的 `toolCallFocus` 就是同一性映射),所以
|
|
362
|
+
* 没有 operationId 的审计事件(摘要/规则等按步注入)需要回到会话事件里找该步
|
|
363
|
+
* 的第一个 `tool/call`。找不到工具调用时不伪造:返回 null,由客户端退化成
|
|
364
|
+
* “只切到轨迹页”。
|
|
365
|
+
*
|
|
366
|
+
* @param ctx - Host 插件上下文。
|
|
367
|
+
* @param query - `{ sessionId, turn?, step?, callId? }`。
|
|
368
|
+
* @returns 锚点结果(永不抛错)。
|
|
369
|
+
*/
|
|
370
|
+
export function findTrajectoryAnchor(ctx, query) {
|
|
371
|
+
const sessionId = typeof query?.sessionId === 'string' ? query.sessionId.trim() : ''
|
|
372
|
+
if (sessionId === '') return { callId: null, reason: 'session_required' }
|
|
373
|
+
let session
|
|
374
|
+
try { session = ctx?.get?.('sessions')?.get?.(sessionId) } catch { session = undefined }
|
|
375
|
+
if (session === undefined || typeof session?.ownEvents !== 'function') {
|
|
376
|
+
return { callId: null, reason: 'session_unavailable' }
|
|
377
|
+
}
|
|
378
|
+
const turn = typeof query.turn === 'number' ? query.turn : undefined
|
|
379
|
+
const step = typeof query.step === 'number' ? query.step : undefined
|
|
380
|
+
// 已有 callId 时校验它确实属于这个会话,避免把跨会话/伪造 id 送进轨迹。
|
|
381
|
+
const wanted = typeof query.callId === 'string' && query.callId !== '' ? query.callId : undefined
|
|
382
|
+
try {
|
|
383
|
+
for (const event of session.ownEvents()) {
|
|
384
|
+
if (event?.type !== 'tool/call') continue
|
|
385
|
+
const data = event.data ?? {}
|
|
386
|
+
if (turn !== undefined && data.turn !== turn) continue
|
|
387
|
+
if (step !== undefined && data.step !== step) continue
|
|
388
|
+
const callId = typeof data.callId === 'string' ? data.callId : undefined
|
|
389
|
+
if (callId === undefined) continue
|
|
390
|
+
if (wanted !== undefined && callId !== wanted) continue
|
|
391
|
+
return {
|
|
392
|
+
callId,
|
|
393
|
+
tool: typeof data.name === 'string' ? data.name : undefined,
|
|
394
|
+
turn: typeof data.turn === 'number' ? data.turn : undefined,
|
|
395
|
+
step: typeof data.step === 'number' ? data.step : undefined,
|
|
396
|
+
reason: wanted !== undefined ? 'operation' : 'step_tool',
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
} catch { /* 事件窗口读取失败:按找不到处理 */ }
|
|
400
|
+
return { callId: null, reason: wanted !== undefined ? 'operation_not_found' : 'no_tool_in_step' }
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function auditPage(memory, query) { if (memory === undefined || memory === null) {
|
|
360
404
|
throw new ApiError(503, 'AUDIT_UNAVAILABLE', 'memory provider 未加载')
|
|
361
405
|
}
|
|
362
406
|
try {
|
|
@@ -446,6 +490,22 @@ async function handle(ctx, routePath, req, res) {
|
|
|
446
490
|
writeJson(res, { ok: true, value: auditStats(memory) })
|
|
447
491
|
return
|
|
448
492
|
}
|
|
493
|
+
if (method === 'GET' && path === '/anchor') {
|
|
494
|
+
const numeric = (name) => {
|
|
495
|
+
const raw = url.searchParams.get(name)
|
|
496
|
+
if (raw === null || raw.trim() === '') return undefined
|
|
497
|
+
if (!/^-?\d+$/.test(raw.trim())) throw new ApiError(400, 'ANCHOR_QUERY_INVALID', `${name} 必须是整数`)
|
|
498
|
+
return Number(raw)
|
|
499
|
+
}
|
|
500
|
+
const query = {
|
|
501
|
+
sessionId: url.searchParams.get('session') ?? '',
|
|
502
|
+
turn: numeric('turn'),
|
|
503
|
+
step: numeric('step'),
|
|
504
|
+
callId: url.searchParams.get('callId') ?? undefined,
|
|
505
|
+
}
|
|
506
|
+
writeJson(res, { ok: true, value: findTrajectoryAnchor(ctx, query) })
|
|
507
|
+
return
|
|
508
|
+
}
|
|
449
509
|
if (method === 'GET' && path === '/audit/export') {
|
|
450
510
|
const query = parseAuditQuery(url)
|
|
451
511
|
const format = url.searchParams.get('format') === 'jsonl' ? 'jsonl' : 'json'
|