@restregular/dsh-memory 0.2.0 → 0.3.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/README.md CHANGED
@@ -80,6 +80,21 @@ patch 里给 `dsh-memory-local` 传配置即可(默认值通常够用):
80
80
  - 若 `autoInstall.dir`(或 `storeDir` 上一级)里已经存在 `llamacpp/` + `models/`,插件会**直接采用**,面板只显示状态,不需要重新下载。
81
81
  - 面板的开关只在"进程确实由本应用托管"时出现;未配置给下载按钮,外部服务只显示连通性,配置里关掉了就只解释原因——不会出现一个永远灰着的开关。
82
82
 
83
+ ### 维护与卸载
84
+
85
+ 面板底部有一个「维护」区,不可撤销的操作都走**两步确认**(第一次点击只是把按钮换成"确认…"):
86
+
87
+ | 动作 | 做什么 | 安全边界 |
88
+ | --- | --- | --- |
89
+ | **卸载向量服务** | 删掉插件下载的 llama.cpp 运行时 + 模型 + 日志(约 480 MB) | 只删 `retrieval.vector.autoInstall.dir`(默认 storeDir 上一级)里的东西;**绝不碰记忆数据目录**;若 `spawn.exe` 指向该根目录之外(也就是你自己装的 llama.cpp),直接拒绝并说明原因 |
90
+ | **清空记忆数据** | 删除所有作用域的全部记录(走 store,JSONL 与 `.vec` 边车保持一致,不会删出半个库) | 两步确认,且接口要求显式 `confirm: "delete"`,缺了不执行 |
91
+
92
+ **卸载插件本身**:在 DSH 的「插件 / 插件市场」里移除本包和 `@restregular/dsh-memory-panel`。
93
+
94
+ > ⚠️ DSH 卸载插件只是在 profile 里 `pnpm remove` 并去掉 bundles 条目:它**不会**给插件任何提问机会,也**不会**自动删除向量服务文件或记忆数据。所以"要不要留向量服务""要不要留记忆数据"这两件事要在卸载**之前**到面板「维护」区处理——插件卸载后那个面板也就没了。
95
+ >
96
+ > 手动清理路径:向量服务在 `retrieval.vector.autoInstall.dir`(默认 `~/.dsh/memory-vector`;显式配了 `storeDir` 时是它的上一级目录),记忆数据在 `storeDir`。
97
+
83
98
  ## 说明
84
99
 
85
100
  - 存储为每作用域一个 JSONL + `.vec` 向量边车(含模型/维度身份头,不匹配自动重建)。
@@ -92,6 +92,16 @@ export class CordisMemoryService extends Service {
92
92
  this.impl.cancelVectorInstall()
93
93
  }
94
94
 
95
+ /** Panel: remove the runtime + model we installed (never the memory store). */
96
+ uninstallVectorBackend() {
97
+ return this.impl.uninstallVectorBackend()
98
+ }
99
+
100
+ /** Panel: delete every record in every scope. */
101
+ wipeAllRecords() {
102
+ return this.impl.wipeAllRecords()
103
+ }
104
+
95
105
  search(query ) {
96
106
  return this.impl.search(query)
97
107
  }
@@ -22,9 +22,29 @@ import { RerankCache } from './rerank-cache.js'
22
22
  import { scanSecrets, redactSecrets } from './secrets.js'
23
23
  import { EmbedServerManager, } from './embed-server.js'
24
24
  import { VectorInstaller, detectInstalled, installPaths, } from './embed-install.js'
25
- import { dirname, join, resolve } from 'node:path'
25
+ import { existsSync, readdirSync, rmSync, statSync } from 'node:fs'
26
+ import { dirname, isAbsolute, join, relative, resolve } from 'node:path'
26
27
  import { homedir } from 'node:os'
27
28
 
29
+ /** Recursive byte count; 0 for anything unreadable. */
30
+ function pathSize(path ) {
31
+ try {
32
+ const st = statSync(path)
33
+ if (!st.isDirectory()) return st.size
34
+ let total = 0
35
+ for (const entry of readdirSync(path)) total += pathSize(join(path, entry))
36
+ return total
37
+ } catch {
38
+ return 0
39
+ }
40
+ }
41
+
42
+ /** True when `child` is `parent` or lives under it (both resolved). */
43
+ function isInside(parent , child ) {
44
+ const rel = relative(resolve(parent), resolve(child))
45
+ return rel === '' || (!rel.startsWith('..') && !isAbsolute(rel))
46
+ }
47
+
28
48
 
29
49
 
30
50
 
@@ -131,7 +151,13 @@ export function resolveConfig(config = {}) {
131
151
  ?? (config.storeDir !== undefined ? dirname(resolve(storeDir)) : join(homedir(), '.dsh', 'memory-vector'))
132
152
  const installed = detectInstalled(installRoot)
133
153
  const endpointConfigured = v.endpoint !== undefined
134
- const spawnConfigured = v.spawn?.enabled === true || (v.spawn?.exe ?? '') !== '' || installed !== null
154
+ // A configured spawn path that no longer exists (plugin uninstalled the
155
+ // backend, or the user moved it) must not count as configured: that would
156
+ // leave the panel showing a managed server that can never start, instead of
157
+ // offering the download button again.
158
+ const spawnExe = v.spawn?.exe
159
+ const spawnExeUsable = spawnExe === undefined || spawnExe === '' || existsSync(spawnExe)
160
+ const spawnConfigured = ((v.spawn?.enabled === true || (spawnExe ?? '') !== '') && spawnExeUsable) || installed !== null
135
161
  const configured = endpointConfigured || spawnConfigured
136
162
  const vectorEnabled = v.enabled ?? configured
137
163
  return {
@@ -214,6 +240,12 @@ export class LocalMemoryService {
214
240
  this.cfg = resolveConfig(config)
215
241
  this.rerankBudget = new DailyBudget(this.cfg.llm.dailyBudgetTokens)
216
242
  this.store = new MemoryStore(this.cfg.storeDir, { onWarn: (m) => console.warn(`[dsh-memory-local] ${m}`) })
243
+ // Identity BEFORE the eager load (issue #1): ScopeStore skips its sidecar
244
+ // validation entirely while the identity is unset (`if (this.vecIdentity && …)`),
245
+ // so loading first would accept vectors from a previous embedding model
246
+ // whenever the dimensions matched — and the next flush would then rewrite the
247
+ // header as the new model, making the mismatch permanent.
248
+ this.store.setVecIdentity(this.cfg.vector.model, this.cfg.vector.dims)
217
249
  this.store.loadAll(this.cfg.projectEnabled && this.cfg.globalEnabled ? [this.defaultScope, 'global'] : [this.defaultScope])
218
250
  this.embed = new EmbedClient({
219
251
  endpoint: this.cfg.vector.endpoint,
@@ -221,8 +253,6 @@ export class LocalMemoryService {
221
253
  dims: this.cfg.vector.dims,
222
254
  timeoutMs: this.cfg.vector.timeoutMs,
223
255
  })
224
- // sidecar validation: model/dims change discards old vectors on load
225
- this.store.setVecIdentity(this.cfg.vector.model, this.cfg.vector.dims)
226
256
  this.startMaintenance()
227
257
  this.startEmbedServer()
228
258
  }
@@ -299,11 +329,14 @@ export class LocalMemoryService {
299
329
 
300
330
 
301
331
 
332
+
333
+
302
334
  {
303
335
  const mgr = this.embedServer
304
336
  const cfg = this.cfg.vector
305
337
  const pendingEmbeds = this.pendingEmbedCount
306
338
  const mode = !cfg.configured ? 'off' : cfg.spawnConfigured ? 'managed' : 'external'
339
+ const usage = this.vectorInstallUsage()
307
340
  const base = {
308
341
  enabled: cfg.enabled,
309
342
  configured: cfg.configured,
@@ -313,6 +346,14 @@ export class LocalMemoryService {
313
346
  spawnConfigured: cfg.spawnConfigured,
314
347
  pendingEmbeds,
315
348
  install: this.installSnapshot(),
349
+ maintenance: {
350
+ usageBytes: usage.bytes,
351
+ paths: usage.paths,
352
+ // NOTE: `cfg` is the VECTOR config here, not the whole config — reading
353
+ // cfg.storeDir would silently yield undefined (and JSON drops it).
354
+ storeDir: this.cfg.storeDir,
355
+ records: this.store.allScopes().reduce((n, s) => n + s.records.size, 0),
356
+ },
316
357
  }
317
358
  if (!mgr) return { ...base, owned: false, pid: null, baseUrl: '', reachable: false }
318
359
  const reachable = await mgr.healthCheck()
@@ -361,6 +402,90 @@ export class LocalMemoryService {
361
402
  this.installer?.cancel()
362
403
  }
363
404
 
405
+ /**
406
+ * What the installer put on disk, so the panel can say "释放 xxx MB" before
407
+ * offering to remove it. Counts only our own layout (runtime dir, models dir,
408
+ * log + pid), never the memory store.
409
+ */
410
+ vectorInstallUsage() {
411
+ const paths = installPaths(this.cfg.vector.installRoot)
412
+ const roots = [paths.dir, join(this.cfg.vector.installRoot, 'models')]
413
+ const files = [paths.logFile, `${paths.logFile}.pid`, `${paths.logFile}.err`]
414
+ let bytes = 0
415
+ const seen = []
416
+ for (const p of [...roots, ...files]) {
417
+ if (!existsSync(p)) continue
418
+ seen.push(p)
419
+ bytes += pathSize(p)
420
+ }
421
+ return { bytes, paths: seen }
422
+ }
423
+
424
+ /**
425
+ * Remove the vector backend we installed (runtime + model + logs).
426
+ *
427
+ * Refuses when the configured backend lives outside installRoot: that is a
428
+ * server the user installed and manages, and deleting it would be destroying
429
+ * something we never created. The memory store is never touched — it lives
430
+ * beside the runtime, not inside it.
431
+ */
432
+ uninstallVectorBackend()
433
+
434
+
435
+
436
+
437
+ {
438
+ const root = this.cfg.vector.installRoot
439
+ const configuredExe = this.cfg.vector.spawn.exe
440
+ if (configuredExe && !isInside(root, configuredExe)) {
441
+ return { removed: false, reason: 'EXTERNAL_BACKEND', freedBytes: 0, paths: [] }
442
+ }
443
+ const usage = this.vectorInstallUsage()
444
+ if (usage.paths.length === 0) {
445
+ return { removed: false, reason: 'NOT_INSTALLED', freedBytes: 0, paths: [] }
446
+ }
447
+ // Stop our child first: on Windows the executable and its DLLs are locked
448
+ // while it runs, so removal would fail with EBUSY/EPERM.
449
+ try { this.embedServer?.stop() } catch { /* best effort */ }
450
+ const removedPaths = []
451
+ for (const p of usage.paths) {
452
+ // Belt and braces: never step outside the install root, and never delete
453
+ // the memory store even if someone pointed storeDir inside it.
454
+ if (!isInside(root, p)) continue
455
+ if (isInside(p, this.cfg.storeDir) || resolve(p) === resolve(this.cfg.storeDir)) continue
456
+ try {
457
+ rmSync(p, { recursive: true, force: true })
458
+ removedPaths.push(p)
459
+ } catch { /* leave it; the panel reports what went */ }
460
+ }
461
+ this.cfg.vector.configured = false
462
+ this.cfg.vector.spawnConfigured = false
463
+ this.cfg.vector.enabled = false
464
+ this.embedServer = null
465
+ return { removed: true, reason: 'REMOVED', freedBytes: usage.bytes, paths: removedPaths }
466
+ }
467
+
468
+ /**
469
+ * Delete every memory record in every scope (the panel's "delete all data"
470
+ * action, used when someone wants the plugin gone without leaving notes
471
+ * behind). Goes through the store so the JSONL + vector sidecars stay
472
+ * consistent instead of ripping files out from under a live store.
473
+ */
474
+ wipeAllRecords() {
475
+ let removed = 0
476
+ const scopes = []
477
+ for (const scope of this.store.allScopes()) {
478
+ const ids = [...scope.records.keys()]
479
+ if (ids.length === 0) continue
480
+ let local = 0
481
+ for (const id of ids) if (scope.remove(id)) local += 1
482
+ removed += local
483
+ scopes.push(scope.scope)
484
+ }
485
+ try { this.store.flushSync() } catch { /* scheduled flush covers it */ }
486
+ return { removed, scopes }
487
+ }
488
+
364
489
  /**
365
490
  * Switch the provider over to a backend that is now on disk: fill the spawn
366
491
  * config from the installed layout and start it. Idempotent.
@@ -198,6 +198,12 @@ export class ScopeStore {
198
198
  discard(`identity mismatch (${trimmed.slice(1)} != ${this.vecIdentity})`)
199
199
  return
200
200
  }
201
+ // Loading before the identity is declared means NO validation happens
202
+ // (the guards below are all identity-gated). That ordering bug shipped
203
+ // once — see RestRegular/dsh-memory#1 — so make any recurrence visible.
204
+ if (!this.vecIdentity) {
205
+ this.events.onWarn?.(`${this.scope}: loading a vector sidecar before setVecIdentity(); its ${trimmed.slice(1)} vectors are accepted unvalidated`)
206
+ }
201
207
  sawHeader = true
202
208
  continue
203
209
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@restregular/dsh-memory",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "DeepSeek Harness 本地长期记忆:JSONL 存储 + 本地向量检索(BGE-M3)+ 四个模型工具 + 自动召回注入。",
5
5
  "type": "module",
6
6
  "exports": {