@restregular/dsh-memory 0.16.1 → 0.17.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 +20 -0
- package/README.md +7 -0
- package/lib/inject/apply.js +73 -0
- package/lib/inject/inject.js +11 -0
- package/lib/inject/session-state-store.js +332 -0
- package/lib/inject/state.js +134 -7
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
本项目的版本记录。安装 id 与 npm 版本一一对应。
|
|
4
4
|
|
|
5
|
+
## 0.17.0 — 会话去重状态持久化到工作区 `.dsh/dsh-memory`
|
|
6
|
+
|
|
7
|
+
- **状态现在可以跨插件重载 / 宿主重启恢复**:`InjectionState` 可按会话挂一个持久化 store,首次接触会话时读一次盘,之后写入由 store 合并。
|
|
8
|
+
- **位置**:`<会话工作区>/.dsh/dsh-memory/sessions/<session>.jsonl`。只存记录 id、状态、版本(`updatedAt`)和时间戳;**不存正文,也不存查询**(避免把凭据写进状态文件)。
|
|
9
|
+
- **运行效率**:JSONL 增量日志 + 默认 250ms 合并窗口,一次召回/摘要的多次标记只 append 一行;写延迟与状态大小无关。轮末与插件卸载会强制 flush。
|
|
10
|
+
- **存储效率**:行数 / 字节超过阈值时 compact 成一行快照,用临时文件 + rename 原子替换;每个工作区默认保留 30 天、最多 1000 个会话文件,首次接触该工作区时清理一次。
|
|
11
|
+
- **容错**:目录不可写、文件损坏、写盘失败都只退化为内存去重并告警一次,绝不阻断注入链路;`compaction` 清空状态时同步删掉对应文件。
|
|
12
|
+
- **接线**:`session/event` 与 `agent/pre-step` 都会在第一次拿到 `header.cwd` 时挂载 store;拿不到工作区时保持纯内存。配置项 `sessionState.{enabled,dir,debounceMs,maxAgeDays,maxFiles}` 可覆盖,默认开启并写到工作区。
|
|
13
|
+
- 回归测试:JSONL 往返、合并写一行、阈值 compact、evict 同步、remove/坏行容错、30 天清理、异常 sessionId 安全文件名;以及 `apply` 层“重载后仍去重、记录更新后重新注入”的端到端用例。
|
|
14
|
+
|
|
15
|
+
## 0.16.2 — memory_get 去重的审计与端到端覆盖
|
|
16
|
+
|
|
17
|
+
- **补审计缺口**:所有候选都被会话级去重时,`renderRecallBlock` 返回 `null`,`dedupIds` 没有写进结果,审计只能看到 `dedupSkipped` 数量、看不到具体跳过了哪些 id。现在 `dedupIds` 在“全部去重”和“预算省略”路径上也会返回。
|
|
18
|
+
- **端到端测试**:新增真实 `session/event` 链路测试(`tool/call` → `tool/result` → `injectionState` → 下一次 pre-step):
|
|
19
|
+
- `memory_get` 取正文后,下一次普通召回不再追加同一条消息,审计能列出被去重的 id;
|
|
20
|
+
- 记录 `updatedAt` 变化后,下一次召回重新注入新版本;
|
|
21
|
+
- 只取 `summary` 时不算正文,仍可通过召回补全文;
|
|
22
|
+
- 自动召回关闭时去重标记仍然生效。
|
|
23
|
+
- 语义保持不变:`memory_get` 默认和显式 `fields: ["content"]` 都显示完整正文;只有正文进入上下文才记为 `content_injected`。
|
|
24
|
+
|
|
5
25
|
## 0.16.1 — memory_get 取正文也参与会话级去重
|
|
6
26
|
|
|
7
27
|
- **补上去重缺口**:0.16.0 把正文入口从 `memory_search` 移到 `memory_get` 后,工具读到的正文没有记进会话注入状态,后续普通召回 / 摘要 / 自动召回仍可能把同一条记忆再塞一遍。
|
package/README.md
CHANGED
|
@@ -239,6 +239,13 @@ memory_forget({ "id": "3f2a9c1d" }) // 按内容删:memory_forget({ q
|
|
|
239
239
|
|
|
240
240
|
- 召回按 **sessionId + 记忆 id / 版本** 去重;只给过标签的记录,任务命中后可补正文;内容更新会重新评估。
|
|
241
241
|
- `memory_get` 取到正文后,这条记忆会记成「正文已注入」;同一会话后续的普通召回 / 摘要 / 自动召回都不会再把同一段正文塞一遍。只取 `summary` / `tags` / 元数据不算正文,仍可被召回。
|
|
242
|
+
- **去重状态持久化**:状态写到会话工作区的插件目录
|
|
243
|
+
`<cwd>/.dsh/dsh-memory/sessions/<session>.jsonl`,插件重载 / 宿主重启后自动恢复;只有首次接触该会话时读一次盘。
|
|
244
|
+
- 只存记录 id、状态、版本(`updatedAt`)和时间戳,**不存正文,也不存查询**。
|
|
245
|
+
- JSONL 增量日志:默认 250ms 合并写一行,写延迟不随状态大小增长;行数 / 字节超过阈值时 compact 成一行快照(临时文件 + rename 原子替换)。
|
|
246
|
+
- 每个工作区默认保留 30 天、最多 1000 个会话文件;目录不可写或文件损坏时自动退化为内存去重,不阻断注入。
|
|
247
|
+
- 插件配置 `inject.sessionState.{enabled,dir,debounceMs,maxAgeDays,maxFiles}` 可覆盖默认值;默认开启。
|
|
248
|
+
- 工作区里的 `.dsh/` 已加入本仓库 `.gitignore`;其他项目按需自行忽略。
|
|
242
249
|
- 被 `supersedes` 指向的旧记录不再进入 `memory_search`、`memory_list`、摘要、规则段和自动召回;`memory_get(id)` 与面板仍可查历史。需要历史检索时 `memory_search({ includeSuperseded: true })`。
|
|
243
250
|
- 摘要排除 `kind=rule`;规则段实际进入系统提示的 id 会写进会话注入状态,recall 不再重复注入同一条规则。
|
|
244
251
|
- 同一轮最多发生一次 signal 驱动召回;同 tag / 同 query 的第二个信号只记一条 `signal_dropped` 审计。
|
package/lib/inject/apply.js
CHANGED
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
|
|
26
26
|
import { createUserMessage } from '@deepseek-ai/dsh-llm'
|
|
27
27
|
|
|
28
|
+
import { join } from 'node:path'
|
|
28
29
|
import {
|
|
29
30
|
MemoryDigester,
|
|
30
31
|
MemoryInjector,
|
|
@@ -45,6 +46,7 @@ import { renderRules, } from './rules.js'
|
|
|
45
46
|
import { RulesDeltaInjector } from './rules-delta.js'
|
|
46
47
|
import { POLICY_TEXT } from './policy.js'
|
|
47
48
|
import { InjectionState } from './state.js'
|
|
49
|
+
import { createSessionStateStore } from './session-state-store.js'
|
|
48
50
|
import {
|
|
49
51
|
InjectAuditStore,
|
|
50
52
|
auditBudget,
|
|
@@ -72,6 +74,7 @@ import {
|
|
|
72
74
|
|
|
73
75
|
} from './signals.js'
|
|
74
76
|
import { normalizeInjectSettings, } from '../provider/inject-settings.js'
|
|
77
|
+
import { canonicalWorkspacePath } from '../provider/scope.js'
|
|
75
78
|
|
|
76
79
|
|
|
77
80
|
|
|
@@ -80,6 +83,21 @@ import { normalizeInjectSettings, } from '../provider/inject
|
|
|
80
83
|
|
|
81
84
|
|
|
82
85
|
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
83
101
|
|
|
84
102
|
|
|
85
103
|
/** provider 在注入侧能提供的那部分能力(多余的方法都用可选判断)。 */
|
|
@@ -145,6 +163,47 @@ export function apply(ctx , config = {}) {
|
|
|
145
163
|
let systemPromptReady = false
|
|
146
164
|
let attachedMemory = null
|
|
147
165
|
|
|
166
|
+
// ---- 去重状态持久化:<workspace>/.dsh/dsh-memory/sessions/<session>.jsonl ----
|
|
167
|
+
const stateConfig = config.sessionState ?? {}
|
|
168
|
+
const stateEnabled = stateConfig.enabled !== false
|
|
169
|
+
let stateWarned = false
|
|
170
|
+
/** attach 之后同步给摘要器补「已做过」标记(digester 在更后面创建)。 */
|
|
171
|
+
let onStateAttached
|
|
172
|
+
|
|
173
|
+
/** pre-step 的 agent / session/event 的 session 都能取到 header.cwd。 */
|
|
174
|
+
const workspaceOf = (value ) => {
|
|
175
|
+
const nested = (value )?.session?.header?.cwd
|
|
176
|
+
if (typeof nested === 'string' && nested.trim() !== '') return nested
|
|
177
|
+
const direct = (value )?.header?.cwd
|
|
178
|
+
return typeof direct === 'string' && direct.trim() !== '' ? direct : undefined
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** 第一次接触某个会话时挂上它的持久化 store;拿不到 cwd 就只走内存。 */
|
|
182
|
+
const attachSessionState = (sessionId , workspace ) => {
|
|
183
|
+
if (sessionId === undefined || !stateEnabled || injectionState.hasStore(sessionId)) return
|
|
184
|
+
let root = ''
|
|
185
|
+
try {
|
|
186
|
+
root = stateConfig.dir !== undefined && stateConfig.dir !== ''
|
|
187
|
+
? stateConfig.dir
|
|
188
|
+
: workspace !== undefined ? join(canonicalWorkspacePath(workspace), '.dsh', 'dsh-memory') : ''
|
|
189
|
+
} catch { root = '' }
|
|
190
|
+
if (root === '') return
|
|
191
|
+
try {
|
|
192
|
+
injectionState.attach(sessionId, createSessionStateStore({
|
|
193
|
+
root,
|
|
194
|
+
sessionId,
|
|
195
|
+
...(stateConfig.debounceMs !== undefined ? { debounceMs: stateConfig.debounceMs } : {}),
|
|
196
|
+
...(stateConfig.maxAgeDays !== undefined ? { maxAgeDays: stateConfig.maxAgeDays } : {}),
|
|
197
|
+
...(stateConfig.maxFiles !== undefined ? { maxFiles: stateConfig.maxFiles } : {}),
|
|
198
|
+
}))
|
|
199
|
+
onStateAttached?.(sessionId)
|
|
200
|
+
} catch (error) {
|
|
201
|
+
if (stateWarned) return
|
|
202
|
+
stateWarned = true
|
|
203
|
+
console.warn(`[dsh-memory] 初始化会话去重状态失败: ${error instanceof Error ? error.message : String(error)};本次退化为内存去重`)
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
148
207
|
const attachAudit = (memory ) => {
|
|
149
208
|
const target = memory
|
|
150
209
|
if (!target || target === attachedMemory || typeof target.attachInjectAudit !== 'function') return
|
|
@@ -165,6 +224,8 @@ export function apply(ctx , config = {}) {
|
|
|
165
224
|
} catch { /* 解绑失败不影响卸载 */ }
|
|
166
225
|
attachedMemory = null
|
|
167
226
|
audit.dispose()
|
|
227
|
+
// 卸载前把会话去重状态的待写增量刷掉;失败只告警,不影响卸载。
|
|
228
|
+
injectionState.dispose()
|
|
168
229
|
}, 'dsh-memory: audit sink')
|
|
169
230
|
} catch { /* 老宿主没有 ctx.effect */ }
|
|
170
231
|
|
|
@@ -402,6 +463,10 @@ export function apply(ctx , config = {}) {
|
|
|
402
463
|
limit: 200,
|
|
403
464
|
})
|
|
404
465
|
})
|
|
466
|
+
// 持久化状态恢复后,把「摘要已做过」同步给摘要器,避免插件重载后重复注入。
|
|
467
|
+
onStateAttached = (sessionId) => {
|
|
468
|
+
if (injectionState.digestDoneOf(sessionId)) digester.seedDone(sessionId)
|
|
469
|
+
}
|
|
405
470
|
|
|
406
471
|
// 规则增量:一个激活一个实例,跨轮记住每条规则上一轮的样子(编辑后才补提醒)。
|
|
407
472
|
const rulesDelta = new RulesDeltaInjector()
|
|
@@ -419,6 +484,8 @@ export function apply(ctx , config = {}) {
|
|
|
419
484
|
const data = (event )?.data ?? {}
|
|
420
485
|
const memory = ctx.get('memory')
|
|
421
486
|
if (memory) attachAudit(memory)
|
|
487
|
+
// 先挂持久化 store:compaction 的 clear 要能删到对应文件。
|
|
488
|
+
attachSessionState(sessionId, workspaceOf(session))
|
|
422
489
|
|
|
423
490
|
// 记忆工具调用先落一条锚点审计:即使自动召回关闭、或该工具没有命中
|
|
424
491
|
// 任何触发标签,用户也能从审计面板/记忆视图精确跳回轨迹里的那一次调用。
|
|
@@ -605,6 +672,8 @@ export function apply(ctx , config = {}) {
|
|
|
605
672
|
let maxStep = 0
|
|
606
673
|
let pendingNudge = false
|
|
607
674
|
ctx.on('agent/turn-stopping', () => {
|
|
675
|
+
// 轮末把待写的去重增量刷掉:即使下一轮开始前进程退出,状态也只丢一个窗口。
|
|
676
|
+
injectionState.flushAll()
|
|
608
677
|
if (!enabled || !settingsOf().turnEnd.enabled) return
|
|
609
678
|
if (maxStep >= 2) pendingNudge = true
|
|
610
679
|
})
|
|
@@ -628,6 +697,8 @@ export function apply(ctx , config = {}) {
|
|
|
628
697
|
maxStep = Math.max(maxStep, step)
|
|
629
698
|
|
|
630
699
|
const sessionId = sessionIdOf(agent)
|
|
700
|
+
// 第一步接触会话时恢复持久化去重状态(读盘只发生一次)。
|
|
701
|
+
attachSessionState(sessionId, workspaceOf(agent))
|
|
631
702
|
if (sessionId !== undefined) rulesStepStart.set(sessionId, stepStartedAt)
|
|
632
703
|
const scopes = readScopesFor(memory, agent)
|
|
633
704
|
if (sessionId !== undefined && scopes !== undefined) sessionScopes.set(sessionId, [...scopes])
|
|
@@ -771,6 +842,8 @@ export function apply(ctx , config = {}) {
|
|
|
771
842
|
digestStats = rendered?.stats
|
|
772
843
|
return rendered
|
|
773
844
|
})
|
|
845
|
+
// 失败也记 done(与内存行为一致:摘要不逐轮重试);落盘后重载不再重复发。
|
|
846
|
+
if (sessionId !== undefined && outcome.status !== 'skipped') injectionState.markDigestDone(sessionId)
|
|
774
847
|
if (outcome.status === 'failed') {
|
|
775
848
|
stage(auditBase('digest', {
|
|
776
849
|
kind: 'evaluation', stage: 'source_load', outcome: 'failed', reasonCode: outcome.errorCode,
|
package/lib/inject/inject.js
CHANGED
|
@@ -389,6 +389,8 @@ export class MemoryInjector {
|
|
|
389
389
|
const block = renderRecallBlock(freshHits, cfg, optionsForRender)
|
|
390
390
|
const base = {
|
|
391
391
|
ids: [], candidates: candidates.length, dedupSkipped: deduplicated.length, updated, omitted: [], query,
|
|
392
|
+
// 全部候选都被去重时 block 为 null;把 id 留在结果里,审计才能解释"跳过了哪条"。
|
|
393
|
+
dedupIds: deduplicated,
|
|
392
394
|
tokens: 0, rawHits: hits.length, tagFiltered, supersededExcluded, expiredExcluded,
|
|
393
395
|
retrieval: { ok: true, count: hits.length },
|
|
394
396
|
}
|
|
@@ -525,6 +527,15 @@ export class MemoryDigester {
|
|
|
525
527
|
}
|
|
526
528
|
}
|
|
527
529
|
|
|
530
|
+
/** 从持久化状态恢复「这个会话已经做过摘要」,避免插件重载后重复注入。 */
|
|
531
|
+
seedDone(sessionId ) {
|
|
532
|
+
if (sessionId !== undefined) this.markDone(sessionId)
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
isDone(sessionId ) {
|
|
536
|
+
return sessionId !== undefined && this.done.has(sessionId)
|
|
537
|
+
}
|
|
538
|
+
|
|
528
539
|
/** 上下文被压缩/重建后重新允许摘要评估(旧摘要可能已不在上下文里)。 */
|
|
529
540
|
reset(sessionId ) {
|
|
530
541
|
if (sessionId !== undefined) this.done.delete(sessionId)
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 会话级注入去重状态的持久化。
|
|
3
|
+
*
|
|
4
|
+
* 位置:<workspace>/.dsh/dsh-memory/sessions/<session>.jsonl
|
|
5
|
+
*
|
|
6
|
+
* 为什么不是"每次改都重写 JSON":
|
|
7
|
+
* - 一次召回/摘要会标记多条;全量重写会把 I/O 放大到 O(状态大小)。
|
|
8
|
+
* - 这里用 JSONL 增量日志:合并窗口(默认 250ms)内多次修改只 append 一行,
|
|
9
|
+
* 写延迟与状态大小无关。
|
|
10
|
+
* - 行数 / 字节超过阈值时 compact 成一行快照(临时文件 + rename,原子替换),
|
|
11
|
+
* 文件不会无限增长。
|
|
12
|
+
*
|
|
13
|
+
* 行格式(短键,减小体积):
|
|
14
|
+
* 快照 {"v":1,"d":1,"e":[["id",2,version,at],...]}
|
|
15
|
+
* 增量 {"d":1,"e":[...]}
|
|
16
|
+
* status: 1=hint_injected, 2=content_injected; d=1 表示本会话摘要已做过
|
|
17
|
+
*
|
|
18
|
+
* 只存记录 id / 状态 / 版本 / 时间戳与摘要标记,不落正文,也不落 lastQuery
|
|
19
|
+
* (查询可能含凭据)。
|
|
20
|
+
*
|
|
21
|
+
* 容错:读/写/清理任何一步失败都只退化为内存状态并告警一次,绝不把异常抛回
|
|
22
|
+
* 注入链路。目录不可写时同样退化。
|
|
23
|
+
*/
|
|
24
|
+
import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs'
|
|
25
|
+
import { join } from 'node:path'
|
|
26
|
+
import { createHash } from 'node:crypto'
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
const STATUS_CODE = {
|
|
30
|
+
hint_injected: 1,
|
|
31
|
+
content_injected: 2,
|
|
32
|
+
}
|
|
33
|
+
const STATUS_BY_CODE = {
|
|
34
|
+
1: 'hint_injected',
|
|
35
|
+
2: 'content_injected',
|
|
36
|
+
}
|
|
37
|
+
const SNAPSHOT_VERSION = 1
|
|
38
|
+
const DEFAULT_DEBOUNCE_MS = 250
|
|
39
|
+
const DEFAULT_MAX_AGE_DAYS = 30
|
|
40
|
+
const DEFAULT_MAX_FILES = 1000
|
|
41
|
+
const DEFAULT_COMPACT_MIN_LINES = 200
|
|
42
|
+
const DEFAULT_COMPACT_MAX_BYTES = 512 * 1024
|
|
43
|
+
const DAY_MS = 86_400_000
|
|
44
|
+
|
|
45
|
+
/** 每个工作区只清理一次;清理是 best-effort,失败不影响读写。 */
|
|
46
|
+
const prunedRoots = new Set ()
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
export function createSessionStateStore(options ) {
|
|
67
|
+
return new JsonlSessionStateStore(options)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
function encodeEntry(entry ) {
|
|
79
|
+
return [entry.id, STATUS_CODE[entry.status], entry.version, entry.at]
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function decodeEntry(raw ) {
|
|
83
|
+
if (!Array.isArray(raw) || raw.length < 4) return undefined
|
|
84
|
+
const [id, code, version, at] = raw
|
|
85
|
+
if (typeof id !== 'string' || id === '') return undefined
|
|
86
|
+
const status = typeof code === 'number' ? STATUS_BY_CODE[code] : undefined
|
|
87
|
+
if (status === undefined || typeof version !== 'number' || typeof at !== 'number') return undefined
|
|
88
|
+
return { id, status, version, at }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** 会话 id 通常已是 UUID;异常字符换成 `_`,超长/被改写的再加 8 位 hash 防碰撞。 */
|
|
92
|
+
function safeName(sessionId ) {
|
|
93
|
+
const cleaned = sessionId.replace(/[^A-Za-z0-9._-]+/g, '_').slice(0, 80)
|
|
94
|
+
if (cleaned === sessionId && cleaned !== '') return cleaned
|
|
95
|
+
const base = cleaned === '' ? 'session' : cleaned.slice(0, 64)
|
|
96
|
+
return `${base}-${createHash('sha1').update(sessionId).digest('hex').slice(0, 8)}`
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
class JsonlSessionStateStore {
|
|
100
|
+
dir
|
|
101
|
+
file
|
|
102
|
+
debounceMs
|
|
103
|
+
maxAgeDays
|
|
104
|
+
maxFiles
|
|
105
|
+
compactMinLines
|
|
106
|
+
compactMaxBytes
|
|
107
|
+
now
|
|
108
|
+
warn
|
|
109
|
+
/** 权威内存副本:load 出来 + append 合并进来的全部条目。 */
|
|
110
|
+
entries = new Map ()
|
|
111
|
+
/** 待写增量(按 id 合并);flush 后清空。 */
|
|
112
|
+
pending = new Map ()
|
|
113
|
+
digestDone = false
|
|
114
|
+
pendingDigestDone = false
|
|
115
|
+
lines = 0
|
|
116
|
+
bytes = 0
|
|
117
|
+
timer
|
|
118
|
+
loaded = false
|
|
119
|
+
disabled = false
|
|
120
|
+
warned = false
|
|
121
|
+
|
|
122
|
+
constructor(options ) {
|
|
123
|
+
this.dir = join(options.root, 'sessions')
|
|
124
|
+
this.file = join(this.dir, `${safeName(options.sessionId)}.jsonl`)
|
|
125
|
+
this.debounceMs = Math.max(0, options.debounceMs ?? DEFAULT_DEBOUNCE_MS)
|
|
126
|
+
this.maxAgeDays = Math.max(1, options.maxAgeDays ?? DEFAULT_MAX_AGE_DAYS)
|
|
127
|
+
this.maxFiles = Math.max(1, options.maxFiles ?? DEFAULT_MAX_FILES)
|
|
128
|
+
this.compactMinLines = Math.max(10, options.compactMinLines ?? DEFAULT_COMPACT_MIN_LINES)
|
|
129
|
+
this.compactMaxBytes = Math.max(4096, options.compactMaxBytes ?? DEFAULT_COMPACT_MAX_BYTES)
|
|
130
|
+
this.now = options.now ?? Date.now
|
|
131
|
+
this.warn = options.warn ?? ((message) => console.warn(message))
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
load() {
|
|
135
|
+
if (this.loaded) return this.snapshot()
|
|
136
|
+
this.loaded = true
|
|
137
|
+
this.prune()
|
|
138
|
+
try {
|
|
139
|
+
if (!existsSync(this.file)) return undefined
|
|
140
|
+
const text = readFileSync(this.file, 'utf8')
|
|
141
|
+
if (text.trim() === '') return undefined
|
|
142
|
+
const entries = new Map ()
|
|
143
|
+
let lines = 0
|
|
144
|
+
for (const line of text.split('\n')) {
|
|
145
|
+
const trimmed = line.trim()
|
|
146
|
+
if (trimmed === '') continue
|
|
147
|
+
lines += 1
|
|
148
|
+
let wire
|
|
149
|
+
try { wire = JSON.parse(trimmed) } catch { continue }
|
|
150
|
+
if (wire.v === SNAPSHOT_VERSION) {
|
|
151
|
+
entries.clear()
|
|
152
|
+
this.digestDone = wire.d === 1
|
|
153
|
+
} else if (wire.d === 1) {
|
|
154
|
+
this.digestDone = true
|
|
155
|
+
}
|
|
156
|
+
if (Array.isArray(wire.e)) {
|
|
157
|
+
for (const raw of wire.e) {
|
|
158
|
+
const entry = decodeEntry(raw)
|
|
159
|
+
if (entry !== undefined) entries.set(entry.id, entry)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
for (const [id, entry] of entries) this.entries.set(id, entry)
|
|
164
|
+
this.lines = lines
|
|
165
|
+
this.bytes = Buffer.byteLength(text)
|
|
166
|
+
return this.snapshot()
|
|
167
|
+
} catch (error) {
|
|
168
|
+
this.disable('读取会话去重状态失败', error)
|
|
169
|
+
return undefined
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
append(entries ) {
|
|
174
|
+
if (this.disabled) return
|
|
175
|
+
this.load()
|
|
176
|
+
for (const entry of entries) {
|
|
177
|
+
this.entries.set(entry.id, entry)
|
|
178
|
+
this.pending.set(entry.id, entry)
|
|
179
|
+
}
|
|
180
|
+
if (this.pending.size === 0) return
|
|
181
|
+
this.schedule()
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
markDigestDone() {
|
|
185
|
+
if (this.disabled) return
|
|
186
|
+
this.load()
|
|
187
|
+
if (this.digestDone && !this.pendingDigestDone) return
|
|
188
|
+
this.digestDone = true
|
|
189
|
+
this.pendingDigestDone = true
|
|
190
|
+
this.schedule()
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
evict(ids ) {
|
|
194
|
+
for (const id of ids) {
|
|
195
|
+
this.entries.delete(id)
|
|
196
|
+
this.pending.delete(id)
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
flush() {
|
|
201
|
+
this.stopTimer()
|
|
202
|
+
if (this.disabled) return
|
|
203
|
+
if (this.pending.size === 0 && !this.pendingDigestDone) return
|
|
204
|
+
try {
|
|
205
|
+
this.ensureDir()
|
|
206
|
+
// 第一次写(或文件被清空)直接写一行快照,保证 load 时一定有基线。
|
|
207
|
+
if (!existsSync(this.file) || this.lines === 0) {
|
|
208
|
+
this.compact()
|
|
209
|
+
return
|
|
210
|
+
}
|
|
211
|
+
const line = {
|
|
212
|
+
...(this.pendingDigestDone ? { d: 1 } : {}),
|
|
213
|
+
...(this.pending.size > 0 ? { e: [...this.pending.values()].map(encodeEntry) } : {}),
|
|
214
|
+
}
|
|
215
|
+
const text = `${JSON.stringify(line)}\n`
|
|
216
|
+
appendFileSync(this.file, text, 'utf8')
|
|
217
|
+
this.lines += 1
|
|
218
|
+
this.bytes += Buffer.byteLength(text)
|
|
219
|
+
this.clearPending()
|
|
220
|
+
if (this.needCompact()) this.compact()
|
|
221
|
+
} catch (error) {
|
|
222
|
+
this.disable('写入会话去重状态失败', error)
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
remove() {
|
|
227
|
+
this.stopTimer()
|
|
228
|
+
this.pending.clear()
|
|
229
|
+
this.pendingDigestDone = false
|
|
230
|
+
this.digestDone = false
|
|
231
|
+
this.entries.clear()
|
|
232
|
+
this.lines = 0
|
|
233
|
+
this.bytes = 0
|
|
234
|
+
this.loaded = false
|
|
235
|
+
try {
|
|
236
|
+
rmSync(this.file, { force: true })
|
|
237
|
+
rmSync(`${this.file}.tmp`, { force: true })
|
|
238
|
+
} catch { /* best effort */ }
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
dispose() {
|
|
242
|
+
this.flush()
|
|
243
|
+
this.stopTimer()
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
snapshot() {
|
|
247
|
+
return { entries: [...this.entries.values()], digestDone: this.digestDone }
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
schedule() {
|
|
251
|
+
if (this.timer !== undefined) return
|
|
252
|
+
this.timer = setTimeout(() => {
|
|
253
|
+
this.timer = undefined
|
|
254
|
+
this.flush()
|
|
255
|
+
}, this.debounceMs)
|
|
256
|
+
// 不要让一个待写状态把进程/测试挂住。
|
|
257
|
+
;(this.timer ).unref?.()
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
stopTimer() {
|
|
261
|
+
if (this.timer === undefined) return
|
|
262
|
+
clearTimeout(this.timer)
|
|
263
|
+
this.timer = undefined
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
clearPending() {
|
|
267
|
+
this.pending.clear()
|
|
268
|
+
this.pendingDigestDone = false
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
needCompact() {
|
|
272
|
+
const lineLimit = Math.max(this.compactMinLines, this.entries.size * 2)
|
|
273
|
+
return this.lines >= lineLimit || this.bytes >= this.compactMaxBytes
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
compact() {
|
|
277
|
+
this.ensureDir()
|
|
278
|
+
const line = {
|
|
279
|
+
v: SNAPSHOT_VERSION,
|
|
280
|
+
...(this.digestDone ? { d: 1 } : {}),
|
|
281
|
+
e: [...this.entries.values()].map(encodeEntry),
|
|
282
|
+
}
|
|
283
|
+
const text = `${JSON.stringify(line)}\n`
|
|
284
|
+
const tmp = `${this.file}.tmp`
|
|
285
|
+
writeFileSync(tmp, text, 'utf8')
|
|
286
|
+
renameSync(tmp, this.file)
|
|
287
|
+
this.clearPending()
|
|
288
|
+
this.lines = 1
|
|
289
|
+
this.bytes = Buffer.byteLength(text)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
ensureDir() {
|
|
293
|
+
if (!existsSync(this.dir)) mkdirSync(this.dir, { recursive: true })
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
prune() {
|
|
297
|
+
if (prunedRoots.has(this.dir)) return
|
|
298
|
+
prunedRoots.add(this.dir)
|
|
299
|
+
if (!existsSync(this.dir)) return
|
|
300
|
+
try {
|
|
301
|
+
const cutoff = this.now() - this.maxAgeDays * DAY_MS
|
|
302
|
+
const current = this.file.slice(this.dir.length + 1)
|
|
303
|
+
const kept = []
|
|
304
|
+
for (const name of readdirSync(this.dir)) {
|
|
305
|
+
if (!name.endsWith('.jsonl') || name === current) continue
|
|
306
|
+
const path = join(this.dir, name)
|
|
307
|
+
let mtime = 0
|
|
308
|
+
try { mtime = statSync(path).mtimeMs } catch { continue }
|
|
309
|
+
if (mtime < cutoff) rmSync(path, { force: true })
|
|
310
|
+
else kept.push({ path, mtime })
|
|
311
|
+
}
|
|
312
|
+
// 当前会话也占一个名额;超出上限时删最旧的。
|
|
313
|
+
const over = kept.length - (this.maxFiles - 1)
|
|
314
|
+
if (over > 0) {
|
|
315
|
+
kept.sort((a, b) => a.mtime - b.mtime)
|
|
316
|
+
for (const item of kept.slice(0, over)) rmSync(item.path, { force: true })
|
|
317
|
+
}
|
|
318
|
+
} catch (error) {
|
|
319
|
+
this.disable('清理旧会话去重状态失败', error)
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
disable(what , error ) {
|
|
324
|
+
this.disabled = true
|
|
325
|
+
if (this.warned) return
|
|
326
|
+
this.warned = true
|
|
327
|
+
this.warn(`[dsh-memory] ${what}: ${error instanceof Error ? error.message : String(error)};本次退化为内存去重`)
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
//# sourceURL=source/memory-inject/src/session-state-store.ts
|
package/lib/inject/state.js
CHANGED
|
@@ -9,6 +9,10 @@
|
|
|
9
9
|
*
|
|
10
10
|
* 版本 = record.updatedAt。内容更新后允许重新注入;上下文重建(压缩/新会话)
|
|
11
11
|
* 由调用方 clear(),绝不用"历史上出现过"代替"当前仍在上下文里"。
|
|
12
|
+
*
|
|
13
|
+
* 0.17.0 起可挂一个按会话的持久化 store(见 session-state-store.ts):状态在
|
|
14
|
+
* 插件重载 / 宿主重启后仍能恢复;读盘只在第一次接触该会话时发生,写入由 store
|
|
15
|
+
* 自己做合并与阈值 compact。
|
|
12
16
|
*/
|
|
13
17
|
|
|
14
18
|
|
|
@@ -29,6 +33,34 @@
|
|
|
29
33
|
|
|
30
34
|
|
|
31
35
|
|
|
36
|
+
/** 一个会话的可持久化状态:只存去重条目与会话摘要是否做过(不落正文、不落查询)。 */
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 会话状态存储的最小接口。实现必须容错:读失败/写失败只能退化为内存状态,
|
|
45
|
+
* 绝不能把异常抛回注入链路。
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
32
64
|
|
|
33
65
|
|
|
34
66
|
|
|
@@ -46,6 +78,53 @@ const MAX_ENTRIES = 4000
|
|
|
46
78
|
|
|
47
79
|
export class InjectionState {
|
|
48
80
|
sessions = new Map ()
|
|
81
|
+
stores = new Map ()
|
|
82
|
+
/** 摘要每个会话只注入一次;这个标记也持久化,插件重载后不重复发。 */
|
|
83
|
+
digestDone = new Set ()
|
|
84
|
+
|
|
85
|
+
/** 挂上按会话的持久化 store;重复 attach 是 no-op。 */
|
|
86
|
+
attach(sessionId , store ) {
|
|
87
|
+
if (sessionId === undefined || this.stores.has(sessionId)) return
|
|
88
|
+
const state = this.stateOf(sessionId)
|
|
89
|
+
this.stores.set(sessionId, store)
|
|
90
|
+
let snapshot
|
|
91
|
+
try {
|
|
92
|
+
snapshot = store.load()
|
|
93
|
+
} catch { /* store 自己应容错;这里再兜一层 */ }
|
|
94
|
+
if (snapshot !== undefined) {
|
|
95
|
+
for (const entry of snapshot.entries) {
|
|
96
|
+
const previous = state.entries.get(entry.id)
|
|
97
|
+
if (previous === undefined || entry.at >= previous.at) {
|
|
98
|
+
state.entries.set(entry.id, { status: entry.status, version: entry.version, at: entry.at })
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
this.trim(state, sessionId)
|
|
102
|
+
}
|
|
103
|
+
if (snapshot?.digestDone === true) this.markDigest(sessionId)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
hasStore(sessionId ) {
|
|
107
|
+
return sessionId !== undefined && this.stores.has(sessionId)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** 摘要是否已经做过(含持久化恢复的标记)。 */
|
|
111
|
+
digestDoneOf(sessionId ) {
|
|
112
|
+
return sessionId !== undefined && this.digestDone.has(sessionId)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** 摘要实际做过一次后调用:标记并尽快落盘。 */
|
|
116
|
+
markDigestDone(sessionId ) {
|
|
117
|
+
if (sessionId === undefined || this.digestDone.has(sessionId)) return
|
|
118
|
+
this.markDigest(sessionId)
|
|
119
|
+
this.storeOf(sessionId)?.markDigestDone()
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
markDigest(sessionId ) {
|
|
123
|
+
this.digestDone.add(sessionId)
|
|
124
|
+
if (this.digestDone.size <= MAX_SESSIONS) return
|
|
125
|
+
const oldest = this.digestDone.values().next()
|
|
126
|
+
if (!oldest.done) this.digestDone.delete(oldest.value)
|
|
127
|
+
}
|
|
49
128
|
|
|
50
129
|
stateOf(sessionId ) {
|
|
51
130
|
const key = sessionId ?? '__unknown__'
|
|
@@ -55,32 +134,53 @@ export class InjectionState {
|
|
|
55
134
|
this.sessions.set(key, state)
|
|
56
135
|
if (this.sessions.size > MAX_SESSIONS) {
|
|
57
136
|
const oldest = this.sessions.keys().next()
|
|
58
|
-
if (!oldest.done)
|
|
137
|
+
if (!oldest.done) {
|
|
138
|
+
// 状态被容量淘汰时,先把该会话的待写增量刷掉,再释放 store;文件保留给下次加载。
|
|
139
|
+
const store = this.stores.get(oldest.value)
|
|
140
|
+
if (store !== undefined) {
|
|
141
|
+
try { store.dispose() } catch { /* best effort */ }
|
|
142
|
+
this.stores.delete(oldest.value)
|
|
143
|
+
}
|
|
144
|
+
this.digestDone.delete(oldest.value)
|
|
145
|
+
this.sessions.delete(oldest.value)
|
|
146
|
+
}
|
|
59
147
|
}
|
|
60
148
|
}
|
|
61
149
|
return state
|
|
62
150
|
}
|
|
63
151
|
|
|
152
|
+
storeOf(sessionId ) {
|
|
153
|
+
return sessionId === undefined ? undefined : this.stores.get(sessionId)
|
|
154
|
+
}
|
|
155
|
+
|
|
64
156
|
/** 记录"只注入了标签"——正文仍未加载。 */
|
|
65
157
|
markHint(sessionId , records ) {
|
|
66
158
|
const state = this.stateOf(sessionId)
|
|
67
159
|
const now = Date.now()
|
|
160
|
+
const changed = []
|
|
68
161
|
for (const record of records) {
|
|
69
162
|
const previous = state.entries.get(record.id)
|
|
70
163
|
if (previous?.status === 'content_injected') continue
|
|
71
|
-
|
|
164
|
+
const entry = { status: 'hint_injected' , version: record.updatedAt, at: now }
|
|
165
|
+
state.entries.set(record.id, entry)
|
|
166
|
+
changed.push({ id: record.id, ...entry })
|
|
72
167
|
}
|
|
73
|
-
this.
|
|
168
|
+
if (changed.length > 0) this.storeOf(sessionId)?.append(changed)
|
|
169
|
+
this.trim(state, sessionId)
|
|
74
170
|
}
|
|
75
171
|
|
|
76
172
|
/** 记录"正文已注入"(hint 状态被正文覆盖)。 */
|
|
77
173
|
markContent(sessionId , records ) {
|
|
78
174
|
const state = this.stateOf(sessionId)
|
|
79
175
|
const now = Date.now()
|
|
176
|
+
const changed = []
|
|
80
177
|
for (const record of records) {
|
|
81
|
-
|
|
178
|
+
const entry = { status: 'content_injected' , version: record.updatedAt, at: now }
|
|
179
|
+
state.entries.set(record.id, entry)
|
|
180
|
+
changed.push({ id: record.id, ...entry })
|
|
82
181
|
}
|
|
83
|
-
this.
|
|
182
|
+
if (changed.length > 0) this.storeOf(sessionId)?.append(changed)
|
|
183
|
+
this.trim(state, sessionId)
|
|
84
184
|
}
|
|
85
185
|
|
|
86
186
|
/**
|
|
@@ -121,12 +221,36 @@ export class InjectionState {
|
|
|
121
221
|
}
|
|
122
222
|
|
|
123
223
|
setLastQuery(sessionId , query ) {
|
|
224
|
+
// lastQuery 只用于同一进程内的重复检索保护;不落盘(查询可能含凭据)。
|
|
124
225
|
this.stateOf(sessionId).lastQuery = query
|
|
125
226
|
}
|
|
126
227
|
|
|
127
228
|
/** 上下文重建(压缩/重置/新会话)时清空,不能沿用错误的去重状态。 */
|
|
128
229
|
clear(sessionId ) {
|
|
129
|
-
|
|
230
|
+
const key = sessionId ?? '__unknown__'
|
|
231
|
+
const store = this.stores.get(key)
|
|
232
|
+
if (store !== undefined) {
|
|
233
|
+
try { store.remove() } catch { /* 删文件失败不影响内存清空 */ }
|
|
234
|
+
this.stores.delete(key)
|
|
235
|
+
}
|
|
236
|
+
this.sessions.delete(key)
|
|
237
|
+
this.digestDone.delete(key)
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** 插件卸载 / 轮末:把每个会话的待写增量都刷掉。 */
|
|
241
|
+
flushAll() {
|
|
242
|
+
for (const store of this.stores.values()) {
|
|
243
|
+
try { store.flush() } catch { /* 持久化失败只记在 store 里 */ }
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
dispose() {
|
|
248
|
+
for (const store of this.stores.values()) {
|
|
249
|
+
try { store.dispose() } catch { /* 持久化失败不影响卸载 */ }
|
|
250
|
+
}
|
|
251
|
+
this.stores.clear()
|
|
252
|
+
this.sessions.clear()
|
|
253
|
+
this.digestDone.clear()
|
|
130
254
|
}
|
|
131
255
|
|
|
132
256
|
statusOf(sessionId , id ) {
|
|
@@ -149,13 +273,16 @@ export class InjectionState {
|
|
|
149
273
|
}
|
|
150
274
|
}
|
|
151
275
|
|
|
152
|
-
trim(state ) {
|
|
276
|
+
trim(state , sessionId ) {
|
|
153
277
|
if (state.entries.size <= MAX_ENTRIES) return
|
|
154
278
|
// Map 保留插入顺序;淘汰最旧的一批即可(状态只用于去重,丢了最多多注入一次)。
|
|
279
|
+
const evicted = []
|
|
155
280
|
for (const key of state.entries.keys()) {
|
|
156
281
|
state.entries.delete(key)
|
|
282
|
+
evicted.push(key)
|
|
157
283
|
if (state.entries.size <= MAX_ENTRIES) break
|
|
158
284
|
}
|
|
285
|
+
if (evicted.length > 0) this.storeOf(sessionId)?.evict(evicted)
|
|
159
286
|
}
|
|
160
287
|
}
|
|
161
288
|
|