dsh-memu-memory 1.2.7 → 1.2.9

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.
Files changed (106) hide show
  1. package/AGENTS.md +1 -1
  2. package/CHANGELOG.md +472 -0
  3. package/README.md +10 -15
  4. package/README.zh.md +10 -15
  5. package/docs/memory-semantics.md +23 -3
  6. package/lib/backup.js +155 -26
  7. package/lib/binding-diagnostics.js +1 -2
  8. package/lib/budget.js +31 -9
  9. package/lib/client.js +221 -128
  10. package/lib/commitment.js +138 -0
  11. package/lib/consolidation-ledger.js +5 -2
  12. package/lib/consolidation-receipt.js +2 -0
  13. package/lib/consolidation-worker.js +3 -0
  14. package/lib/curator-policy.js +23 -0
  15. package/lib/cycle.js +8 -4
  16. package/lib/embedding-download.js +168 -28
  17. package/lib/embedding-runtime.js +76 -17
  18. package/lib/engine.js +0 -6
  19. package/lib/freshness.js +24 -0
  20. package/lib/graph.js +139 -11
  21. package/lib/index.js +71 -20
  22. package/lib/inject.js +167 -17
  23. package/lib/lexicon.js +62 -17
  24. package/lib/local-vector-service.js +25 -2
  25. package/lib/media.js +49 -12
  26. package/lib/memory.js +1202 -123
  27. package/lib/memu.js +4 -0
  28. package/lib/paths.js +0 -7
  29. package/lib/preset-scope.js +6 -1
  30. package/lib/private.js +50 -10
  31. package/lib/prompts/private-consolidation.mixed.md +2 -2
  32. package/lib/prompts/private-consolidation.rp.md +6 -1
  33. package/lib/prompts/private-policy.rp.md +4 -0
  34. package/lib/prompts/system-guide.rp.md +3 -0
  35. package/lib/prompts.js +0 -7
  36. package/lib/recall-cache.js +10 -2
  37. package/lib/rpc.js +104 -19
  38. package/lib/snippet.js +41 -0
  39. package/lib/state.js +109 -30
  40. package/lib/stats.js +2 -2
  41. package/lib/temporal.js +348 -0
  42. package/lib/text-retrieval.js +191 -44
  43. package/lib/text-similarity.js +205 -24
  44. package/lib/tokenizer-v2.js +0 -24
  45. package/lib/tools.js +81 -4
  46. package/lib/vector-migration.js +15 -0
  47. package/lib/vector-profile.js +81 -8
  48. package/lib/visibility.js +0 -4
  49. package/lib/webui/standalone.css +17 -22
  50. package/lib/webui/standalone.js +27 -9
  51. package/lib/webui.js +4 -4
  52. package/package.json +3 -2
  53. package/scripts/acceptance-local.mjs +2 -2
  54. package/scripts/bench-recall.mjs +2 -2
  55. package/scripts/check-all.mjs +3 -1
  56. package/scripts/check-i18n.mjs +35 -1
  57. package/scripts/check-seed-compat.mjs +1 -1
  58. package/scripts/migrate-visibility.mjs +3 -3
  59. package/scripts/prune-sessions.mjs +34 -0
  60. package/scripts/prune-vectors.mjs +24 -0
  61. package/tests/appendix-b.test.mjs +5 -4
  62. package/tests/audit-batch3.test.mjs +247 -0
  63. package/tests/audit-regression.test.mjs +2 -2
  64. package/tests/backup.test.mjs +85 -85
  65. package/tests/browser.test.mjs +6 -1
  66. package/tests/client-render.test.mjs +2 -2
  67. package/tests/commitment.test.mjs +144 -0
  68. package/tests/consolidation-fork.test.mjs +3 -3
  69. package/tests/curator-clusters.test.mjs +18 -0
  70. package/tests/curator-pairing.test.mjs +59 -16
  71. package/tests/disclosure.test.mjs +34 -17
  72. package/tests/dynamic-rrf.test.mjs +22 -19
  73. package/tests/dynamic-threshold.test.mjs +52 -0
  74. package/tests/fact-version.test.mjs +146 -0
  75. package/tests/fixtures/tokenizer-cases.mjs +27 -0
  76. package/tests/graph-cascade.test.mjs +29 -0
  77. package/tests/graph.test.mjs +45 -0
  78. package/tests/help-page.test.mjs +1 -1
  79. package/tests/inject-conflict.test.mjs +112 -0
  80. package/tests/inject-guide.test.mjs +5 -3
  81. package/tests/inject.test.mjs +2 -2
  82. package/tests/injection-intent-budget.test.mjs +92 -0
  83. package/tests/lexical-bm25.test.mjs +171 -0
  84. package/tests/lexicon-frequency.test.mjs +17 -1
  85. package/tests/lexicon-inject.test.mjs +21 -0
  86. package/tests/lexicon-perf.test.mjs +37 -9
  87. package/tests/lexicon-scope.test.mjs +2 -2
  88. package/tests/lexicon-ui.test.mjs +1 -1
  89. package/tests/managed-memory.test.mjs +19 -0
  90. package/tests/memory-chain.test.mjs +140 -0
  91. package/tests/memory-fields.test.mjs +134 -0
  92. package/tests/memu-resident.test.mjs +33 -0
  93. package/tests/ops-hardening.test.mjs +209 -0
  94. package/tests/package-shipping.test.mjs +4 -0
  95. package/tests/prompt-semantics.test.mjs +51 -0
  96. package/tests/query-decompose.test.mjs +62 -0
  97. package/tests/recall-cache.test.mjs +4 -2
  98. package/tests/recall-intent.test.mjs +123 -0
  99. package/tests/slot-similarity.test.mjs +153 -0
  100. package/tests/speaker-provenance.test.mjs +120 -0
  101. package/tests/state-gc.test.mjs +34 -0
  102. package/tests/state.test.mjs +2 -2
  103. package/tests/temporal.test.mjs +124 -0
  104. package/tests/tokenizer-v2.test.mjs +1 -1
  105. package/tests/vector-gc.test.mjs +37 -0
  106. package/tests/webui.test.mjs +27 -0
@@ -15,14 +15,14 @@ test.after(() => {
15
15
  try { fs.rmSync(home, { recursive: true, force: true }) } catch {}
16
16
  })
17
17
 
18
- test('a backup carries every store and a checksum manifest', () => {
18
+ test('a backup carries every store and a checksum manifest', async () => {
19
19
  const binding = state.ensureSession('backup-session', { cwd: path.join(home, 'proj') })
20
20
  state.updatePrivateIndex(binding.libraryId, (index) => {
21
21
  index.entries['mem-keep'] = { id: 'mem-keep', content: '别丢', layer: 'long_term', status: 'active' }
22
22
  })
23
23
  state.setWorkspacePolicy(path.join(home, 'proj'), { useLibrary: true })
24
24
 
25
- const dest = state.backupDatabases()
25
+ const dest = await state.backupDatabases()
26
26
  assert.ok(fs.existsSync(path.join(dest, 'state.json')))
27
27
  assert.ok(fs.existsSync(path.join(dest, 'index', `${binding.libraryId}.json`)))
28
28
 
@@ -32,7 +32,7 @@ test('a backup carries every store and a checksum manifest', () => {
32
32
  assert.ok(manifest.files[`index/${binding.libraryId}.json`].sha256)
33
33
  })
34
34
 
35
- test('restore puts the backed-up content back and keeps a safety copy', () => {
35
+ test('restore puts the backed-up content back and keeps a safety copy', async () => {
36
36
  const binding = state.getSessionBinding('backup-session')
37
37
  const backups = state.listBackups()
38
38
  assert.ok(backups.length >= 1)
@@ -45,14 +45,14 @@ test('restore puts the backed-up content back and keeps a safety copy', () => {
45
45
  })
46
46
  assert.ok(state.loadPrivateIndex(binding.libraryId).entries['mem-after'])
47
47
 
48
- const result = state.restoreBackup(target.name)
48
+ const result = await state.restoreBackup(target.name)
49
49
  assert.ok(result.safety, 'a restore backs the live data up first')
50
50
  const restored = state.loadPrivateIndex(binding.libraryId)
51
51
  assert.ok(restored.entries['mem-keep'], 'the backed-up entry is back')
52
52
  assert.equal(restored.entries['mem-after'], undefined, 'the later write is gone')
53
53
  })
54
54
 
55
- test('a backup with a broken checksum is refused', () => {
55
+ test('a backup with a broken checksum is refused', async () => {
56
56
  const backups = state.listBackups()
57
57
  const target = backups[0]
58
58
  const manifestPath = path.join(target.dir, 'manifest.json')
@@ -60,24 +60,24 @@ test('a backup with a broken checksum is refused', () => {
60
60
  manifest.files['state.json'].sha256 = '0'.repeat(64)
61
61
  fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2))
62
62
 
63
- assert.throws(() => state.restoreBackup(target.name), /corrupt|checksum/i)
63
+ await assert.rejects(async () => await state.restoreBackup(target.name), /corrupt|checksum/i)
64
64
  })
65
65
 
66
- test('an incomplete backup is refused', () => {
67
- const dest = state.backupDatabases()
66
+ test('an incomplete backup is refused', async () => {
67
+ const dest = await state.backupDatabases()
68
68
  const name = path.basename(dest)
69
69
  fs.rmSync(path.join(dest, 'state.json'))
70
- assert.throws(() => state.restoreBackup(name), /incomplete/i)
70
+ await assert.rejects(async () => await state.restoreBackup(name), /incomplete/i)
71
71
  })
72
72
 
73
- test('backup names that try to escape the backups directory are rejected', () => {
74
- assert.throws(() => state.restoreBackup('../../etc'), /invalid backup name/i)
73
+ test('backup names that try to escape the backups directory are rejected', async () => {
74
+ await assert.rejects(async () => await state.restoreBackup('../../etc'), /invalid backup name/i)
75
75
  assert.throws(() => state.deleteBackup('../../etc'), /invalid backup name/i)
76
76
  })
77
77
 
78
- test('a backup can be deleted, and only that one', () => {
79
- const keep = state.backupDatabases()
80
- const drop = state.backupDatabases()
78
+ test('a backup can be deleted, and only that one', async () => {
79
+ const keep = await state.backupDatabases()
80
+ const drop = await state.backupDatabases()
81
81
  const before = state.listBackups().length
82
82
  assert.equal(state.deleteBackup(path.basename(drop)).deleted, path.basename(drop))
83
83
  assert.equal(fs.existsSync(drop), false)
@@ -87,7 +87,7 @@ test('a backup can be deleted, and only that one', () => {
87
87
  })
88
88
 
89
89
  // ---------------------------------------------------------------------------
90
- // Retention, staging and recovery (本地向量施工文档 §14.9).
90
+ // Retention, staging and recovery.
91
91
  // Every test below runs in its own temporary data root: no real user backup is
92
92
  // created, pruned or restored by this file.
93
93
  // ---------------------------------------------------------------------------
@@ -128,7 +128,7 @@ function statusOf(name) {
128
128
  return entry ? entry.status : null
129
129
  }
130
130
 
131
- test('retention keeps only the newest three complete backups', () => {
131
+ test('retention keeps only the newest three complete backups', async () => {
132
132
  const root = tempRoot('keep3')
133
133
  try {
134
134
  seed(root, 'v0')
@@ -136,7 +136,7 @@ test('retention keeps only the newest three complete backups', () => {
136
136
  const made = []
137
137
  for (let round = 1; round <= 6; round += 1) {
138
138
  seed(root, 'v' + round)
139
- made.push(backup.createBackup().name)
139
+ made.push((await backup.createBackup()).name)
140
140
  const complete = completeNames()
141
141
  assert.ok(complete.length <= 3, 'at most three complete backups after ' + round + ' creations')
142
142
  assert.deepEqual(complete.slice().sort(), made.slice(-3).slice().sort(), 'the newest three are the ones kept')
@@ -147,12 +147,12 @@ test('retention keeps only the newest three complete backups', () => {
147
147
  } finally { releaseRoot(root) }
148
148
  })
149
149
 
150
- test('backups created in the same millisecond stay distinct and ordered', () => {
150
+ test('backups created in the same millisecond stay distinct and ordered', async () => {
151
151
  const root = tempRoot('samems')
152
152
  try {
153
153
  seed(root, 'same')
154
- const first = backup.createBackup().name
155
- const second = backup.createBackup().name
154
+ const first = (await backup.createBackup()).name
155
+ const second = (await backup.createBackup()).name
156
156
  assert.notEqual(first, second, 'the name is unique even within one millisecond')
157
157
  const entries = backup.scanBackups()
158
158
  assert.equal(entries.length, 2)
@@ -162,21 +162,21 @@ test('backups created in the same millisecond stay distinct and ordered', () =>
162
162
  } finally { releaseRoot(root) }
163
163
  })
164
164
 
165
- test('a failed creation publishes nothing and deletes no usable backup', () => {
165
+ test('a failed creation publishes nothing and deletes no usable backup', async () => {
166
166
  const root = tempRoot('failed-create')
167
167
  try {
168
168
  seed(root, 'good')
169
- const kept = backup.createBackup().name
169
+ const kept = (await backup.createBackup()).name
170
170
  // A directory where a file belongs: the copy must fail, not paper over it.
171
171
  fs.mkdirSync(path.join(root, 'prompts', 'manifest.json'), { recursive: true })
172
- assert.throws(() => backup.createBackup(), /non-file|symbolic/i)
172
+ await assert.rejects(async () => await backup.createBackup(), /non-file|symbolic/i)
173
173
  assert.deepEqual(completeNames(), [kept], 'the previous backup is intact')
174
174
  assert.equal(backup.scanBackups().length, 1, 'nothing was published')
175
175
  assert.deepEqual(fs.readdirSync(path.join(root, 'backups', '.staging')), [], 'the staging directory was discarded')
176
176
  } finally { releaseRoot(root) }
177
177
  })
178
178
 
179
- test('leftovers of a crashed run are reclaimed, never published', () => {
179
+ test('leftovers of a crashed run are reclaimed, never published', async () => {
180
180
  const root = tempRoot('crash')
181
181
  try {
182
182
  seed(root, 'crash')
@@ -188,7 +188,7 @@ test('leftovers of a crashed run are reclaimed, never published', () => {
188
188
  const unpublished = path.join(backups, '2020-01-01T00-00-00-000Z-powerloss')
189
189
  fs.mkdirSync(unpublished, { recursive: true })
190
190
  fs.writeFileSync(path.join(unpublished, 'state.json'), '{}')
191
- const created = backup.createBackup()
191
+ const created = await backup.createBackup()
192
192
  assert.equal(fs.existsSync(abandoned), false, 'staging of a dead process is reclaimed')
193
193
  assert.equal(fs.existsSync(unpublished), true, 'an unpublished directory is reported, never deleted')
194
194
  assert.equal(statusOf(path.basename(unpublished)), 'unknown')
@@ -200,23 +200,23 @@ test('leftovers of a crashed run are reclaimed, never published', () => {
200
200
  } finally { releaseRoot(root) }
201
201
  })
202
202
 
203
- test('a backup whose bytes drifted is not counted as valid, and is not silently deleted', () => {
203
+ test('a backup whose bytes drifted is not counted as valid, and is not silently deleted', async () => {
204
204
  const root = tempRoot('corrupt')
205
205
  try {
206
206
  seed(root, 'one')
207
- const first = backup.createBackup({ keep: 10 }).name
207
+ const first = (await backup.createBackup({ keep: 10 })).name
208
208
  seed(root, 'two')
209
- backup.createBackup({ keep: 10 })
209
+ await backup.createBackup({ keep: 10 })
210
210
  seed(root, 'three')
211
- backup.createBackup({ keep: 10 })
211
+ await backup.createBackup({ keep: 10 })
212
212
  seed(root, 'four')
213
- const newest = backup.createBackup({ keep: 10 }).name
213
+ const newest = (await backup.createBackup({ keep: 10 })).name
214
214
  const victim = path.join(root, 'backups', first, 'state.json')
215
215
  const original = fs.readFileSync(victim, 'utf8')
216
216
  const flipped = original.slice(0, -2) + (original.slice(-2) === '1}' ? '2}' : '1}')
217
217
  assert.equal(Buffer.byteLength(flipped), Buffer.byteLength(original), 'same size, different bytes')
218
218
  fs.writeFileSync(victim, flipped)
219
- assert.throws(() => backup.restoreFromBackup(first, { keep: 10 }), /corrupt|checksum/i)
219
+ await assert.rejects(async () => await backup.restoreFromBackup(first, { keep: 10 }), /corrupt|checksum/i)
220
220
  const pruned = backup.pruneBackups()
221
221
  assert.equal(fs.existsSync(path.join(root, 'backups', first)), true, 'a corrupt backup is reported, not quietly removed')
222
222
  assert.ok(pruned.skipped.some((item) => item.name === first && item.status === 'corrupt'), 'it is reported as corrupt')
@@ -225,11 +225,11 @@ test('a backup whose bytes drifted is not counted as valid, and is not silently
225
225
  } finally { releaseRoot(root) }
226
226
  })
227
227
 
228
- test('unknown and legacy directories survive retention untouched', () => {
228
+ test('unknown and legacy directories survive retention untouched', async () => {
229
229
  const root = tempRoot('foreign')
230
230
  try {
231
231
  seed(root, 'x')
232
- const good = backup.createBackup({ keep: 10 }).name
232
+ const good = (await backup.createBackup({ keep: 10 })).name
233
233
  const backups = path.join(root, 'backups')
234
234
  const handMade01 = path.join(backups, 'my-own-backup')
235
235
  fs.mkdirSync(handMade01, { recursive: true })
@@ -249,13 +249,13 @@ test('unknown and legacy directories survive retention untouched', () => {
249
249
  } finally { releaseRoot(root) }
250
250
  })
251
251
 
252
- test('a backup that cannot be deleted safely is reported and left in place', () => {
252
+ test('a backup that cannot be deleted safely is reported and left in place', async () => {
253
253
  const root = tempRoot('delete-fail')
254
254
  try {
255
255
  seed(root, 'a')
256
- const older = backup.createBackup({ keep: 10 }).name
256
+ const older = (await backup.createBackup({ keep: 10 })).name
257
257
  seed(root, 'b')
258
- const newer = backup.createBackup({ keep: 10 }).name
258
+ const newer = (await backup.createBackup({ keep: 10 })).name
259
259
  const nested = path.join(root, 'backups', older, 'attachments')
260
260
  fs.mkdirSync(nested, { recursive: true })
261
261
  const outside = path.join(root, 'outside')
@@ -273,17 +273,17 @@ test('a backup that cannot be deleted safely is reported and left in place', ()
273
273
  } finally { releaseRoot(root) }
274
274
  })
275
275
 
276
- test('one lock serializes create, restore, delete and prune', () => {
276
+ test('one lock serializes create, restore, delete and prune', async () => {
277
277
  const root = tempRoot('lock')
278
278
  try {
279
279
  seed(root, 'lock')
280
280
  // Reentrant: a restore makes its own safety backup while holding the lock.
281
- const inner = backup.withBackupLock(() => backup.createBackup({ keep: 10 }).name)
281
+ const inner = await backup.withBackupLock(async () => (await backup.createBackup({ keep: 10 })).name)
282
282
  assert.ok(inner)
283
283
  const lock = path.join(root, 'backups', '.backup.lock')
284
284
  fs.writeFileSync(lock, String(process.pid))
285
285
  try {
286
- assert.throws(() => backup.createBackup(), /busy/i)
286
+ await assert.rejects(async () => await backup.createBackup(), /busy/i)
287
287
  } finally { fs.rmSync(lock, { force: true }) }
288
288
  let deadPid = null
289
289
  for (let candidate = 999999; candidate < 1000100 && deadPid === null; candidate += 1) {
@@ -291,24 +291,24 @@ test('one lock serializes create, restore, delete and prune', () => {
291
291
  }
292
292
  if (deadPid !== null) {
293
293
  fs.writeFileSync(lock, String(deadPid))
294
- const recovered = backup.createBackup({ keep: 10 })
294
+ const recovered = await backup.createBackup({ keep: 10 })
295
295
  assert.ok(fs.existsSync(recovered.dir), 'a lock left by a dead process is reclaimed')
296
296
  assert.equal(fs.existsSync(lock), false, 'the lock is released')
297
297
  }
298
298
  } finally { releaseRoot(root) }
299
299
  })
300
300
 
301
- test('restoring the oldest of four keeps the target and its safety backup', () => {
301
+ test('restoring the oldest of four keeps the target and its safety backup', async () => {
302
302
  const root = tempRoot('restore-oldest')
303
303
  try {
304
304
  seed(root, 'oldest')
305
- const target = backup.createBackup({ keep: 10, label: 'manual' }).name
305
+ const target = (await backup.createBackup({ keep: 10, label: 'manual' })).name
306
306
  for (const text of ['second', 'third', 'fourth']) {
307
307
  seed(root, text)
308
- backup.createBackup({ keep: 10 })
308
+ await backup.createBackup({ keep: 10 })
309
309
  }
310
310
  assert.equal(completeNames().length, 4)
311
- const result = backup.restoreFromBackup(target, { keep: 3 })
311
+ const result = await backup.restoreFromBackup(target, { keep: 3 })
312
312
  assert.equal(result.restored, target)
313
313
  assert.ok(result.safety, 'a safety backup was made')
314
314
  assert.equal(fs.existsSync(path.join(root, 'backups', path.basename(result.safety))), true)
@@ -321,36 +321,36 @@ test('restoring the oldest of four keeps the target and its safety backup', () =
321
321
  } finally { releaseRoot(root) }
322
322
  })
323
323
 
324
- test('a restore validates the whole inventory before it writes anything', () => {
324
+ test('a restore validates the whole inventory before it writes anything', async () => {
325
325
  const root = tempRoot('restore-guard')
326
326
  try {
327
327
  seed(root, 'backup')
328
- const name = backup.createBackup({ keep: 10 }).name
328
+ const name = (await backup.createBackup({ keep: 10 })).name
329
329
  seed(root, 'live')
330
330
  const liveBefore = fs.readFileSync(path.join(root, 'state.json'), 'utf8')
331
331
  const liveIndex = fs.readFileSync(path.join(root, 'index', 'lib-a.json'), 'utf8')
332
332
  fs.rmSync(path.join(root, 'backups', name, 'index', 'lib-a.json'))
333
- assert.throws(() => backup.restoreFromBackup(name, { keep: 10 }), /incomplete/i)
333
+ await assert.rejects(async () => await backup.restoreFromBackup(name, { keep: 10 }), /incomplete/i)
334
334
  assert.equal(fs.readFileSync(path.join(root, 'state.json'), 'utf8'), liveBefore, 'the live root is untouched')
335
335
  assert.equal(fs.readFileSync(path.join(root, 'index', 'lib-a.json'), 'utf8'), liveIndex)
336
336
  assert.equal(fs.existsSync(path.join(root, 'backups', name, 'state.json')), true)
337
337
  } finally { releaseRoot(root) }
338
338
  })
339
339
 
340
- test('a restore that fails while writing stays recoverable through its safety backup', () => {
340
+ test('a restore that fails while writing stays recoverable through its safety backup', async () => {
341
341
  const root = tempRoot('restore-fail')
342
342
  try {
343
343
  seed(root, 'backup')
344
344
  fs.mkdirSync(path.join(root, 'attachments'), { recursive: true })
345
345
  fs.writeFileSync(path.join(root, 'attachments', 'a.txt'), 'attachment')
346
- const name = backup.createBackup({ keep: 10 }).name
346
+ const name = (await backup.createBackup({ keep: 10 })).name
347
347
  seed(root, 'live')
348
348
  fs.writeFileSync(path.join(root, 'attachments', 'a.txt'), 'attachment')
349
349
  // Make one restore target unwritable after the safety backup exists.
350
350
  fs.rmSync(path.join(root, 'attachments'), { recursive: true, force: true })
351
351
  fs.writeFileSync(path.join(root, 'attachments'), 'not a directory')
352
352
  let caught = null
353
- try { backup.restoreFromBackup(name, { keep: 10 }) } catch (error) { caught = error }
353
+ try { await backup.restoreFromBackup(name, { keep: 10 }) } catch (error) { caught = error }
354
354
  assert.ok(caught, 'the restore failed')
355
355
  assert.equal(caught.code, 'DSH_MEMU_BACKUP_RESTORE_FAILED')
356
356
  assert.ok(caught.safety, 'the failure names the safety backup it kept')
@@ -360,17 +360,17 @@ test('a restore that fails while writing stays recoverable through its safety ba
360
360
  // Recovery: repair the obstacle, then roll back with the safety backup.
361
361
  fs.rmSync(path.join(root, 'attachments'), { force: true })
362
362
  fs.mkdirSync(path.join(root, 'attachments'), { recursive: true })
363
- const rollback = backup.restoreFromBackup(caught.safety, { keep: 10 })
363
+ const rollback = await backup.restoreFromBackup(caught.safety, { keep: 10 })
364
364
  assert.equal(rollback.restored, caught.safety)
365
365
  assert.equal(JSON.parse(fs.readFileSync(path.join(root, 'state.json'), 'utf8')).marker, 'live')
366
366
  } finally { releaseRoot(root) }
367
367
  })
368
368
 
369
- test('a backup from before the vector journal retires the stale live pointer', () => {
369
+ test('a backup from before the vector journal retires the stale live pointer', async () => {
370
370
  const root = tempRoot('vector')
371
371
  try {
372
372
  seed(root, 'text-only')
373
- const name = backup.createBackup({ keep: 10 }).name
373
+ const name = (await backup.createBackup({ keep: 10 })).name
374
374
  const live = path.join(root, 'index', 'vector-active.json')
375
375
  fs.writeFileSync(live, JSON.stringify({
376
376
  version: 1,
@@ -383,23 +383,23 @@ test('a backup from before the vector journal retires the stale live pointer', (
383
383
  databases: { global: { path: 'libraries/global.abcdef01.g2.sqlite3' }, libraries: {} },
384
384
  },
385
385
  }))
386
- const result = backup.restoreFromBackup(name, { keep: 10 })
386
+ const result = await backup.restoreFromBackup(name, { keep: 10 })
387
387
  assert.equal(result.vectorActive, 'cleared')
388
388
  assert.equal(fs.existsSync(live), false, 'the stale pointer is gone')
389
389
  assert.ok(fs.readdirSync(path.join(root, 'index')).some((file) => file.indexOf('vector-active.json.stale-') === 0), 'it was moved aside, not lost')
390
390
  } finally { releaseRoot(root) }
391
391
  })
392
392
 
393
- test('retention and restore refuse paths that leave the data root', () => {
393
+ test('retention and restore refuse paths that leave the data root', async () => {
394
394
  const root = tempRoot('traversal')
395
395
  try {
396
396
  seed(root, 'tv')
397
- const name = backup.createBackup({ keep: 10 }).name
397
+ const name = (await backup.createBackup({ keep: 10 })).name
398
398
  const manifestPath = path.join(root, 'backups', name, 'manifest.json')
399
399
  const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'))
400
400
  manifest.files['../escaped.json'] = { bytes: 2, sha256: 'x' }
401
401
  fs.writeFileSync(manifestPath, JSON.stringify(manifest))
402
- assert.throws(() => backup.restoreFromBackup(name, { keep: 10 }), /invalid backup manifest path/)
402
+ await assert.rejects(async () => await backup.restoreFromBackup(name, { keep: 10 }), /invalid backup manifest path/)
403
403
  assert.throws(() => backup.deleteBackupEntry('..'), /invalid backup name/)
404
404
  assert.throws(() => backup.deleteBackupEntry('.staging'), /invalid backup name/)
405
405
  const outside = path.join(root, 'outside')
@@ -415,17 +415,17 @@ test('retention and restore refuse paths that leave the data root', () => {
415
415
  } finally { releaseRoot(root) }
416
416
  })
417
417
 
418
- test('the state API keeps its shape and reports the retention policy', () => {
418
+ test('the state API keeps its shape and reports the retention policy', async () => {
419
419
  const root = tempRoot('api')
420
420
  try {
421
421
  const binding = state.ensureSession('api-session', { cwd: path.join(root, 'proj') })
422
422
  state.updatePrivateIndex(binding.libraryId, (index) => {
423
423
  index.entries['mem-api'] = { id: 'mem-api', content: '接口', layer: 'long_term', status: 'active' }
424
424
  })
425
- const dir = state.backupDatabases()
425
+ const dir = await state.backupDatabases()
426
426
  assert.equal(typeof dir, 'string')
427
427
  assert.ok(fs.existsSync(path.join(dir, 'manifest.json')))
428
- for (let round = 0; round < 4; round += 1) state.backupDatabases()
428
+ for (let round = 0; round < 4; round += 1) await state.backupDatabases()
429
429
  const listed = state.listBackups()
430
430
  assert.ok(Array.isArray(listed))
431
431
  assert.ok(listed.length >= 1)
@@ -442,14 +442,14 @@ test('the state API keeps its shape and reports the retention policy', () => {
442
442
  assert.equal(report.action, 'create')
443
443
  assert.match(report.notice, /at most 3/)
444
444
  const oldest = listed[listed.length - 1]
445
- const restored = state.restoreBackup(oldest.name)
445
+ const restored = await state.restoreBackup(oldest.name)
446
446
  assert.equal(restored.restored, oldest.name)
447
447
  assert.ok(restored.safety)
448
448
  assert.ok(state.loadPrivateIndex(binding.libraryId).entries['mem-api'], 'the restored library is readable')
449
449
  } finally { releaseRoot(root) }
450
450
  })
451
451
 
452
- test('the backup manifest carries identity, never a credential', () => {
452
+ test('the backup manifest carries identity, never a credential', async () => {
453
453
  const root = tempRoot('secrets')
454
454
  try {
455
455
  seed(root, 'secret')
@@ -457,7 +457,7 @@ test('the backup manifest carries identity, never a credential', () => {
457
457
  version: 1,
458
458
  settings: { apiKey: 'sk-abcdef0123456789abcdef', endpoint: 'https://user:pw@example.invalid/v1' },
459
459
  }))
460
- const name = backup.createBackup({ keep: 10 }).name
460
+ const name = (await backup.createBackup({ keep: 10 })).name
461
461
  const manifestText = fs.readFileSync(path.join(root, 'backups', name, 'manifest.json'), 'utf8')
462
462
  assert.equal(manifestText.indexOf('sk-abcdef0123456789abcdef'), -1, 'no key in the manifest')
463
463
  assert.equal(manifestText.indexOf('user:pw'), -1, 'no credential in a URL')
@@ -468,7 +468,7 @@ test('the backup manifest carries identity, never a credential', () => {
468
468
  } finally { releaseRoot(root) }
469
469
  })
470
470
  // ---------------------------------------------------------------------------
471
- // Live SQLite snapshotting (本地向量施工文档 §14.9, rule 2).
471
+ // Live SQLite snapshotting (rule 2).
472
472
  //
473
473
  // A non-empty database may only be snapshotted by SQLite's own backup API, run
474
474
  // by the plugin's isolated engine/<version>/venv interpreter. These tests prove
@@ -532,7 +532,7 @@ function stagedFiles(root) {
532
532
  return fs.readdirSync(path.join(root, 'backups', '.staging'))
533
533
  }
534
534
 
535
- test('a non-empty database is snapshotted by the engine, never copied', () => {
535
+ test('a non-empty database is snapshotted by the engine, never copied', async () => {
536
536
  const root = tempRoot('engine-snapshot')
537
537
  try {
538
538
  seed(root, 'live')
@@ -540,7 +540,7 @@ test('a non-empty database is snapshotted by the engine, never copied', () => {
540
540
  seedDatabase(root, 'libraries/lib-a.sqlite3-wal', Buffer.alloc(64, 7))
541
541
  const engine = fakeEngine(root)
542
542
  const calls = []
543
- const created = backup.createBackup({ keep: 10, pythonExec: fakeInterpreter(engine.venv, { calls }) })
543
+ const created = await backup.createBackup({ keep: 10, pythonExec: fakeInterpreter(engine.venv, { calls }) })
544
544
 
545
545
  const copy = fs.readFileSync(path.join(created.dir, 'libraries', 'lib-a.sqlite3'))
546
546
  assert.equal(copy.toString('latin1').includes('ENGINE-SNAPSHOT'), true, 'the published file is the engine snapshot')
@@ -559,14 +559,14 @@ test('a non-empty database is snapshotted by the engine, never copied', () => {
559
559
  } finally { releaseRoot(root) }
560
560
  })
561
561
 
562
- test('without a verified engine the backup fails and publishes nothing', () => {
562
+ test('without a verified engine the backup fails and publishes nothing', async () => {
563
563
  const root = tempRoot('no-engine')
564
564
  try {
565
565
  seed(root, 'live')
566
566
  const live = seedDatabase(root, 'libraries/lib-a.sqlite3', sqliteFile('LIVE-BYTES'))
567
567
  const before = fs.readFileSync(live)
568
568
  let caught = null
569
- try { backup.createBackup({ keep: 10 }) } catch (error) { caught = error }
569
+ try { await backup.createBackup({ keep: 10 }) } catch (error) { caught = error }
570
570
  assert.ok(caught, 'the backup refused')
571
571
  assert.equal(caught.code, 'DSH_MEMU_BACKUP_ENGINE_REQUIRED')
572
572
  assert.match(caught.message, /engine/)
@@ -577,7 +577,7 @@ test('without a verified engine the backup fails and publishes nothing', () => {
577
577
  } finally { releaseRoot(root) }
578
578
  })
579
579
 
580
- test('a global interpreter is never borrowed, and a shim is caught by sys.prefix', () => {
580
+ test('a global interpreter is never borrowed, and a shim is caught by sys.prefix', async () => {
581
581
  const root = tempRoot('global-python')
582
582
  try {
583
583
  seed(root, 'live')
@@ -600,7 +600,7 @@ test('a global interpreter is never borrowed, and a shim is caught by sys.prefix
600
600
  return { status: 0, stdout: JSON.stringify({ ok: true, quickCheck: 'ok' }), stderr: '' }
601
601
  }
602
602
  let caught = null
603
- try { backup.createBackup({ keep: 10, pythonExec: shim }) } catch (error) { caught = error }
603
+ try { await backup.createBackup({ keep: 10, pythonExec: shim }) } catch (error) { caught = error }
604
604
  assert.ok(caught, 'the backup refused')
605
605
  assert.equal(caught.code, 'DSH_MEMU_BACKUP_ENGINE_REQUIRED')
606
606
  assert.match(caught.message, /sys\.prefix/)
@@ -611,7 +611,7 @@ test('a global interpreter is never borrowed, and a shim is caught by sys.prefix
611
611
  } finally { releaseRoot(root) }
612
612
  })
613
613
 
614
- test('an engine that cannot prove its snapshot fails the whole backup', () => {
614
+ test('an engine that cannot prove its snapshot fails the whole backup', async () => {
615
615
  for (const [snapshot, expected] of [
616
616
  ['io-error', /disk I\/O error/],
617
617
  ['bad-check', /quick_check/],
@@ -624,7 +624,7 @@ test('an engine that cannot prove its snapshot fails the whole backup', () => {
624
624
  seedDatabase(root, 'libraries/lib-a.sqlite3', sqliteFile('LIVE-BYTES'))
625
625
  const engine = fakeEngine(root)
626
626
  let caught = null
627
- try { backup.createBackup({ keep: 10, pythonExec: fakeInterpreter(engine.venv, { snapshot }) }) } catch (error) { caught = error }
627
+ try { await backup.createBackup({ keep: 10, pythonExec: fakeInterpreter(engine.venv, { snapshot }) }) } catch (error) { caught = error }
628
628
  assert.ok(caught, snapshot + ': the backup refused')
629
629
  assert.equal(caught.code, 'DSH_MEMU_BACKUP_SNAPSHOT_FAILED', snapshot + ': the refusal names the snapshot step')
630
630
  assert.match(caught.message, expected)
@@ -635,12 +635,12 @@ test('an engine that cannot prove its snapshot fails the whole backup', () => {
635
635
  }
636
636
  })
637
637
 
638
- test('a zero-byte placeholder is copied and needs no engine', () => {
638
+ test('a zero-byte placeholder is copied and needs no engine', async () => {
639
639
  const root = tempRoot('placeholder')
640
640
  try {
641
641
  seed(root, 'empty')
642
642
  seedDatabase(root, 'global.sqlite3', Buffer.alloc(0))
643
- const created = backup.createBackup({ keep: 10 })
643
+ const created = await backup.createBackup({ keep: 10 })
644
644
  const manifest = JSON.parse(fs.readFileSync(path.join(created.dir, 'manifest.json'), 'utf8'))
645
645
  const snapshot = manifest.snapshot.databases.find((item) => item.file === 'global.sqlite3')
646
646
  assert.equal(snapshot.mode, 'empty-placeholder')
@@ -649,13 +649,13 @@ test('a zero-byte placeholder is copied and needs no engine', () => {
649
649
  } finally { releaseRoot(root) }
650
650
  })
651
651
 
652
- test('a non-empty file that is not SQLite is refused instead of copied', () => {
652
+ test('a non-empty file that is not SQLite is refused instead of copied', async () => {
653
653
  const root = tempRoot('not-sqlite')
654
654
  try {
655
655
  seed(root, 'junk')
656
656
  seedDatabase(root, 'global.sqlite3', Buffer.alloc(4096, 65))
657
657
  let caught = null
658
- try { backup.createBackup({ keep: 10 }) } catch (error) { caught = error }
658
+ try { await backup.createBackup({ keep: 10 }) } catch (error) { caught = error }
659
659
  assert.ok(caught)
660
660
  assert.equal(caught.code, 'DSH_MEMU_BACKUP_NOT_SQLITE')
661
661
  assert.match(caught.message, /neither an empty placeholder nor a SQLite database/)
@@ -663,7 +663,7 @@ test('a non-empty file that is not SQLite is refused instead of copied', () => {
663
663
  } finally { releaseRoot(root) }
664
664
  })
665
665
 
666
- test('even with an installed engine, force-unavailable sqlite refuses rather than copies', () => {
666
+ test('even with an installed engine, force-unavailable sqlite refuses rather than copies', async () => {
667
667
  const root = tempRoot('no-python-switch')
668
668
  try {
669
669
  seed(root, 'live')
@@ -672,19 +672,19 @@ test('even with an installed engine, force-unavailable sqlite refuses rather tha
672
672
  process.env.DSH_MEMU_BACKUP_NO_PYTHON = '1'
673
673
  backup.resetBackupPythonProbe()
674
674
  let caught = null
675
- try { backup.createBackup({ keep: 10, pythonExec: fakeInterpreter(engine.venv) }) } catch (error) { caught = error }
675
+ try { await backup.createBackup({ keep: 10, pythonExec: fakeInterpreter(engine.venv) }) } catch (error) { caught = error }
676
676
  assert.ok(caught, 'the forced-unavailable engine refuses')
677
677
  assert.equal(caught.code, 'DSH_MEMU_BACKUP_ENGINE_REQUIRED')
678
678
  assert.equal(backup.scanBackups().length, 0)
679
679
  } finally { releaseRoot(root) }
680
680
  })
681
681
 
682
- test('a refused backup is visible through the state API, not swallowed', () => {
682
+ test('a refused backup is visible through the state API, not swallowed', async () => {
683
683
  const root = tempRoot('refusal-report')
684
684
  try {
685
685
  seed(root, 'live')
686
686
  seedDatabase(root, 'libraries/lib-a.sqlite3', sqliteFile('LIVE-BYTES'))
687
- assert.throws(() => state.backupDatabases(), /engine/i)
687
+ await assert.rejects(async () => await state.backupDatabases(), /engine/i)
688
688
  const report = state.lastBackupReportView()
689
689
  assert.equal(report.action, 'create')
690
690
  assert.equal(report.status, 'failed')
@@ -698,7 +698,7 @@ test('a refused backup is visible through the state API, not swallowed', () => {
698
698
  // It is skipped by default for the same reason every other real-engine case in
699
699
  // this repo is: it needs a base Python to create the venv with (the venv is
700
700
  // built exactly the way lib/engine.js builds it) and it takes a few seconds.
701
- test('a real venv interpreter folds a real live WAL into the backup', (t) => {
701
+ test('a real venv interpreter folds a real live WAL into the backup', async (t) => {
702
702
  if (process.env.DSH_MEMU_BACKUP_REAL_ENGINE !== '1') {
703
703
  t.skip('set DSH_MEMU_BACKUP_REAL_ENGINE=1 to run the real-engine case')
704
704
  return
@@ -739,7 +739,7 @@ test('a real venv interpreter folds a real live WAL into the backup', (t) => {
739
739
  assert.equal(made.status, 0, 'the live database was written: ' + String(made.stderr || ''))
740
740
  assert.ok(fs.statSync(db + '-wal').size > 0, 'the database really has a live WAL to fold in')
741
741
 
742
- const created = backup.createBackup({ keep: 10 })
742
+ const created = await backup.createBackup({ keep: 10 })
743
743
  const manifest = JSON.parse(fs.readFileSync(path.join(created.dir, 'manifest.json'), 'utf8'))
744
744
  const snapshot = manifest.snapshot.databases.find((item) => item.file === 'libraries/lib-real.sqlite3')
745
745
  assert.equal(snapshot.mode, 'sqlite-backup-api')
@@ -761,7 +761,7 @@ test('a real venv interpreter folds a real live WAL into the backup', (t) => {
761
761
  assert.equal(lines[1], '3', 'every committed row made it in, including the WAL frames')
762
762
  } finally { releaseRoot(root) }
763
763
  })
764
- test('an engine still installing (venv.tmp-*) can take the snapshot too', () => {
764
+ test('an engine still installing (venv.tmp-*) can take the snapshot too', async () => {
765
765
  // The repair path: lib/engine.js backs up with beforeSwitch while the new
766
766
  // venv is still `venv.tmp-*` and current.json still points at the broken one.
767
767
  const root = tempRoot('repair')
@@ -773,7 +773,7 @@ test('an engine still installing (venv.tmp-*) can take the snapshot too', () =>
773
773
  fs.mkdirSync(bin, { recursive: true })
774
774
  const python = path.join(bin, process.platform === 'win32' ? 'python.exe' : 'python3')
775
775
  fs.writeFileSync(python, '')
776
- const created = backup.createBackup({ keep: 10, pythonExec: fakeInterpreter(venv) })
776
+ const created = await backup.createBackup({ keep: 10, pythonExec: fakeInterpreter(venv) })
777
777
  const manifest = JSON.parse(fs.readFileSync(path.join(created.dir, 'manifest.json'), 'utf8'))
778
778
  const snapshot = manifest.snapshot.databases.find((item) => item.file === 'libraries/lib-a.sqlite3')
779
779
  assert.equal(snapshot.mode, 'sqlite-backup-api')
@@ -781,7 +781,7 @@ test('an engine still installing (venv.tmp-*) can take the snapshot too', () =>
781
781
  } finally { releaseRoot(root) }
782
782
  })
783
783
 
784
- test('lib/backup.js cannot reach a global interpreter or a copy fallback', () => {
784
+ test('lib/backup.js cannot reach a global interpreter or a copy fallback', async () => {
785
785
  const src = fs.readFileSync(path.join(process.cwd(), 'lib', 'backup.js'), 'utf8')
786
786
  assert.equal(src.includes('DSH_MEMU_PYTHON'), false, 'the env override is not consulted at all')
787
787
  assert.equal(src.includes('pythonCandidates'), false, 'the PATH/py-launcher candidate list is gone')
@@ -383,7 +383,12 @@ test('an archived memory can be deleted for good, and leaves the browser', async
383
383
  const before = await dispatch({}, 'browseLibrary', { libraryId: lib.id })
384
384
  assert.equal(before.files.length, 2)
385
385
 
386
- const out = await dispatch({}, 'deleteMemory', { libraryId: lib.id, id: 'mem-gone' })
386
+ await assert.rejects(
387
+ () => dispatch({}, 'deleteMemory', { libraryId: lib.id, id: 'mem-gone' }),
388
+ /explicit confirmation/,
389
+ 'erasing a memory for good needs consent, not just an archived status',
390
+ )
391
+ const out = await dispatch({}, 'deleteMemory', { libraryId: lib.id, id: 'mem-gone', confirmed: true })
387
392
  assert.equal(out.deleted, true)
388
393
  assert.equal(out.libraryId, lib.id)
389
394
 
@@ -422,7 +422,7 @@ test('the WebUI buttons open a new tab and never navigate this one', async () =>
422
422
  globalThis.window.open = (url) => { opened.push(url); return { focus() {}, opener: null } }
423
423
  buttons[0].props.onClick()
424
424
  assert.equal(opened.length, 1, 'a popup was requested')
425
- assert.ok(opened[0].endsWith('/dsh-memu-memory/ui#/settings/engine'), `unexpected url: ${opened[0]}`)
425
+ assert.ok(opened[0].includes('/dsh-memu-memory/ui') && /[?&]theme=(light|dark)/.test(opened[0]) && opened[0].endsWith('#/settings/engine'), `unexpected url: ${opened[0]}`)
426
426
  assert.deepEqual(navigated, [], 'no fallback navigation while the popup works')
427
427
 
428
428
  // Blocked popups return null: fall back to an in-app overlay. Navigating this
@@ -435,7 +435,7 @@ test('the WebUI buttons open a new tab and never navigate this one', async () =>
435
435
  assert.equal(overlay.attrs['data-dsh-memu-memory-webui'], '', 'the overlay is marked')
436
436
  const frame = overlay.children.find((child) => child.tag === 'iframe')
437
437
  assert.ok(frame, 'the overlay hosts an iframe')
438
- assert.ok(String(frame.src).endsWith('/dsh-memu-memory/ui#/settings/engine'), `unexpected url: ${frame.src}`)
438
+ assert.ok(String(frame.src).includes('/dsh-memu-memory/ui') && /[?&]theme=(light|dark)/.test(String(frame.src)) && String(frame.src).endsWith('#/settings/engine'), `unexpected url: ${frame.src}`)
439
439
  assert.deepEqual(navigated, [], 'the current tab is never navigated')
440
440
  // The close control lives inside the page; the overlay only waits for its message.
441
441
  assert.equal(overlay.children.length, 1, 'no floating chrome outside the panel')