@restregular/dsh-memory 0.5.0 → 0.5.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
@@ -97,9 +97,9 @@ memory_save({
97
97
  | `<installRoot>/models/` | 下载的 GGUF 模型 |
98
98
  | `<installRoot>/selected-model.json` | 面板里选过哪一档(比配置优先级低) |
99
99
 
100
- `storeDir` 默认 `.dsh-memory`(相对启动目录),**建议显式配一个绝对路径**。文件是**首次写入才创建**(读一个不存在的 scope 只会得到空结果,不会造出空文件);`projects/` 目录在插件启动时就建好。
100
+ `storeDir` 默认 `.dsh-memory`(相对启动目录),**建议显式配一个绝对路径**。文件是**首次写入才创建**(读一个不存在的 scope 只会得到空结果,不会造出空文件);`projects/` 目录在插件启动时就建好。**作用域被删空后,它的 `.jsonl` 与 `.vec` 会在下一次落盘时一起移除**(0.5.1 起),不会再留下 0 字节文件;下次写同一个名字会重新创建。唯一例外是 `global.jsonl`:保留作用域删空后只清内容、保留文件。
101
101
 
102
- scope 键的三种形态:`global`、`project:<cwd>`、`session:<会话 id>`,其余都是自定义名(小写折叠、内部空白折成一个空格、最长 64 字符,禁止 `:`、路径分隔符、控制字符、`..` 与保留名 `global/project/session/all`)。**0.5.0 没有任何数据迁移**:旧的 `project:<cwd>` 文件继续原样读写。
102
+ scope 键的三种形态:`global`、`project:<cwd>`、`session:<会话 id>`,其余都是自定义名(小写折叠、内部空白折成一个空格、最长 64 字符,禁止 `:`、路径分隔符、控制字符、`..` 与保留名 `global/project/session/all`)。**没有任何数据迁移**:旧的 `project:<cwd>` 文件继续原样读写。
103
103
 
104
104
  `installRoot` 默认 `~/.dsh/memory-vector`;显式配了 `storeDir` 时取**它的父目录**(所以手写的 `<root>/llamacpp` + `<root>/models` 布局会被直接认出来)。
105
105
 
@@ -221,10 +221,30 @@ npm run verify # 构建到 .verify/,不落 packages/(CI 或改动后自检)
221
221
  可选的真实加载验证(证明产物在没有 TS 支持的普通 node 里能跑):
222
222
 
223
223
  ```bash
224
- node tools/build-single-package.mjs --scope @restregular --version 0.4.0 \
224
+ node tools/build-single-package.mjs --scope @restregular --version 0.5.1 \
225
225
  --host-modules <一个 @deepseek-ai 目录> # 或设 DSH_HOST_MODULES
226
226
  ```
227
227
 
228
+ ### 发布 / Publish
229
+
230
+ ```bash
231
+ npm run build # 产物落到 packages/dsh-memory(版本号在 package.json 的 build/verify 里)
232
+ node tools/publish-npm-packages.mjs --scope @restregular --version 0.5.1
233
+ ```
234
+
235
+ `publish-npm-packages.mjs` 发的是**单包** `packages/dsh-memory`,顺序是
236
+ `npm test` → `npm run verify` → `npm whoami` 预检 → `npm pack --dry-run` 校验产物清单(缺
237
+ `lib/client.js`/`cordis.patch.yml` 等必填项、或混进 `.ts`/测试文件都会中止)→ `npm publish`。
238
+ 任何一步失败都停在"什么都没发出去"的状态;`--dry-run` 只走到校验,`--skip-checks` 跳过前两步。
239
+
240
+ npm token **不经过命令行**:默认从 DevCred 读取(凭据名默认 `npm-agent-use`,`--credential` 可换),
241
+ 由 `devcred inject` 把 `NPM_TOKEN` 交给一个只写文件、不回显的子进程,写进临时目录里的私有
242
+ `.npmrc`,发布后连临时目录一起删除;也可以 `--token-file <npmrc>` 指一份自己的凭据文件。
243
+
244
+ > npm 的**暂存发布(staged publishing)** 会让重复版本号撞上 `E409 Cannot publish over previously
245
+ > staged version`:说明这个版本已经在 npm 侧暂存、等待在 npmjs.com 的包页面批准,批准前
246
+ > `latest` 不会指向它。这时不需要再发一次,去网页上确认即可。
247
+
228
248
  ## 仓库结构 / Layout
229
249
 
230
250
  ```
@@ -260,6 +280,7 @@ tools/build-single-package.mjs 构建(产物契约检查 + 可选加载验证)
260
280
 
261
281
  ## 版本
262
282
 
283
+ - **0.5.1** —— 清理空作用域文件:作用域被删空后(删掉最后一条、或 `POST /data/wipe`),它的 `.jsonl` 与 `.vec` 会在下一次落盘时一起移除,不再留 0 字节文件与只剩身份头的边车;下次写同一个名字会重新创建。`global.jsonl` 是唯一例外(保留作用域只清内容、留文件)。同版本的旧文件行为不变,无数据迁移。
263
284
  - **0.5.0** —— 作用域重构:
264
285
  - **默认作用域 = 当前会话**:`memory_save` 省略 `scope` 时写进 `session:<会话 id>`(子代理共享父会话;拿不到会话信息或 `scopes.session: false` 时回退当前项目,不报错)。
265
286
  - **读写默认一致**:0.4.x 的写路径默认是 `project:<cwd>`、而 provider 的读路径默认是 `global` —— 写进去的记忆默认检索不到。现在两边都是「会话 + 项目 + global」合并去重(同分时 session > project > 自定义 > global)。
@@ -3,7 +3,7 @@
3
3
  * atomic compaction (tmp + rename). Vectors live in memory for M1; the .vec
4
4
  * sidecar format arrives in M1.5.
5
5
  */
6
- import { appendFileSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, statSync, writeFileSync } from 'node:fs'
6
+ import { appendFileSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs'
7
7
  import { createHash, randomUUID } from 'node:crypto'
8
8
  import { Buffer } from 'node:buffer'
9
9
  import { dirname, join } from 'node:path'
@@ -85,6 +85,8 @@ export class ScopeStore {
85
85
  dirty = false
86
86
  /** true once vectors were touched since the last .vec flush. */
87
87
  vectorsDirty = false
88
+ /** 数据文件路径(写、以及删空后移除都走它)。 */
89
+ file
88
90
  vecFile
89
91
  events
90
92
  /** Embedding identity for sidecar validation; set via setVecIdentity(). */
@@ -92,6 +94,7 @@ export class ScopeStore {
92
94
 
93
95
  constructor(scope , root , events = {}) {
94
96
  this.scope = scope
97
+ this.file = join(root, scopeFileName(scope))
95
98
  this.vecFile = join(root, scopeFileName(scope).replace(/\.jsonl$/, '.vec'))
96
99
  this.events = events
97
100
  }
@@ -126,6 +129,27 @@ export class ScopeStore {
126
129
  return had
127
130
  }
128
131
 
132
+ /**
133
+ * Delete this scope's data files(.jsonl 与 .vec 边车)。
134
+ *
135
+ * 只在 flush 时、且该 scope **内存里已经没有记录**的情况下调用 —— 也就是"作用域被删空了"。
136
+ * 留一个 0 字节的 .jsonl + 只有一个身份头的 .vec 在磁盘上没有任何价值:数据已经空,
137
+ * 而作用域本身是懒加载的(下次写同一个名字会重新创建)。global 永不适用(调用方 guard)。
138
+ */
139
+ removeFiles() {
140
+ let removed = false
141
+ for (const file of [this.file, this.vecFile]) {
142
+ try {
143
+ rmSync(file, { force: true })
144
+ removed = true
145
+ } catch (err) {
146
+ // 删不掉不影响正确性(空文件无数据);下一次 flush 还会再试一次
147
+ this.events.onWarn?.(`store: cannot remove ${file}: ${(err ).message}`)
148
+ }
149
+ }
150
+ return removed
151
+ }
152
+
129
153
  setVector(id , vec ) {
130
154
  this.vectors.set(id, vec)
131
155
  this.vectorsDirty = true
@@ -403,6 +427,15 @@ export class MemoryStore {
403
427
  return join(this.root, this.fileNames.get(scope) ?? scopeFileName(scope))
404
428
  }
405
429
 
430
+ /**
431
+ * 这个 scope 删空后能不能把文件删掉。
432
+ * 只有 `global` 例外:它是唯一一个"永远存在"的保留作用域(所有人共享),文件不该被
433
+ * 任何一次清空动作从磁盘上抹掉 —— 即使将来某条路径把它删空了,也只写一个空文件。
434
+ */
435
+ isRemovable(scope ) {
436
+ return scope !== 'global'
437
+ }
438
+
406
439
  anyDirty() {
407
440
  for (const s of this.scopes.values()) if (s.dirty) return true
408
441
  return false
@@ -418,10 +451,22 @@ export class MemoryStore {
418
451
  this.flushTimer.unref?.()
419
452
  }
420
453
 
421
- /** Write dirty scopes atomically (tmp + rename). */
454
+ /**
455
+ * Write dirty scopes atomically (tmp + rename).
456
+ *
457
+ * 一个被删空的 scope(内存里 0 条记录)**不再落一个 0 字节文件**,而是把它的 .jsonl 与
458
+ * .vec 一起删掉:空文件没有任何信息,反而在面板里留下一条 `(0)` 的作用域、在磁盘上长期堆积。
459
+ * 作用域是懒加载的,下次往同一个名字写入时会重新创建。`global` 永不自动删除(见 isRemovable)。
460
+ */
422
461
  flush() {
423
462
  for (const s of this.scopes.values()) {
424
463
  if (!s.dirty) continue
464
+ if (s.records.size === 0 && this.isRemovable(s.scope)) {
465
+ s.removeFiles()
466
+ s.dirty = false
467
+ s.vectorsDirty = false
468
+ continue
469
+ }
425
470
  const file = this.fileFor(s.scope)
426
471
  mkdirSync(dirname(file), { recursive: true })
427
472
  const tmp = `${file}.tmp`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@restregular/dsh-memory",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "DeepSeek Harness 本地长期记忆(单包单入口):JSONL 存储 + 本地向量检索(BGE-M3)+ 四个模型工具 + 自动召回注入 + 管理面板。",
5
5
  "icon": "./icon.svg",
6
6
  "type": "module",