dsh-memu-memory 1.2.8 → 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 (78) hide show
  1. package/CHANGELOG.md +472 -0
  2. package/README.md +1 -1
  3. package/README.zh.md +1 -1
  4. package/lib/backup.js +154 -25
  5. package/lib/binding-diagnostics.js +1 -2
  6. package/lib/budget.js +0 -9
  7. package/lib/client.js +221 -128
  8. package/lib/commitment.js +3 -3
  9. package/lib/consolidation-ledger.js +5 -2
  10. package/lib/curator-policy.js +23 -0
  11. package/lib/cycle.js +8 -4
  12. package/lib/embedding-download.js +168 -28
  13. package/lib/embedding-runtime.js +76 -17
  14. package/lib/engine.js +0 -6
  15. package/lib/freshness.js +2 -2
  16. package/lib/graph.js +139 -11
  17. package/lib/index.js +71 -20
  18. package/lib/inject.js +50 -7
  19. package/lib/lexicon.js +61 -16
  20. package/lib/local-vector-service.js +21 -1
  21. package/lib/media.js +49 -12
  22. package/lib/memory.js +662 -116
  23. package/lib/paths.js +0 -7
  24. package/lib/preset-scope.js +6 -1
  25. package/lib/private.js +45 -7
  26. package/lib/prompts.js +0 -7
  27. package/lib/rpc.js +95 -17
  28. package/lib/snippet.js +18 -2
  29. package/lib/state.js +109 -30
  30. package/lib/stats.js +2 -2
  31. package/lib/temporal.js +19 -0
  32. package/lib/text-retrieval.js +120 -44
  33. package/lib/text-similarity.js +205 -24
  34. package/lib/tokenizer-v2.js +0 -24
  35. package/lib/vector-migration.js +15 -0
  36. package/lib/vector-profile.js +81 -8
  37. package/lib/visibility.js +0 -4
  38. package/lib/webui/standalone.css +17 -22
  39. package/lib/webui/standalone.js +27 -9
  40. package/lib/webui.js +4 -4
  41. package/package.json +3 -2
  42. package/scripts/acceptance-local.mjs +2 -2
  43. package/scripts/check-all.mjs +3 -1
  44. package/scripts/check-i18n.mjs +35 -1
  45. package/scripts/migrate-visibility.mjs +3 -3
  46. package/scripts/prune-sessions.mjs +34 -0
  47. package/scripts/prune-vectors.mjs +24 -0
  48. package/tests/appendix-b.test.mjs +5 -4
  49. package/tests/audit-batch3.test.mjs +247 -0
  50. package/tests/audit-regression.test.mjs +2 -2
  51. package/tests/backup.test.mjs +83 -83
  52. package/tests/browser.test.mjs +6 -1
  53. package/tests/client-render.test.mjs +2 -2
  54. package/tests/curator-clusters.test.mjs +18 -0
  55. package/tests/curator-pairing.test.mjs +59 -16
  56. package/tests/dynamic-rrf.test.mjs +22 -19
  57. package/tests/dynamic-threshold.test.mjs +52 -0
  58. package/tests/fact-version.test.mjs +18 -0
  59. package/tests/fixtures/tokenizer-cases.mjs +27 -0
  60. package/tests/graph-cascade.test.mjs +29 -0
  61. package/tests/graph.test.mjs +45 -0
  62. package/tests/inject-conflict.test.mjs +18 -0
  63. package/tests/lexical-bm25.test.mjs +171 -0
  64. package/tests/lexicon-frequency.test.mjs +16 -0
  65. package/tests/lexicon-inject.test.mjs +21 -0
  66. package/tests/lexicon-perf.test.mjs +37 -9
  67. package/tests/lexicon-scope.test.mjs +2 -2
  68. package/tests/lexicon-ui.test.mjs +1 -1
  69. package/tests/managed-memory.test.mjs +19 -0
  70. package/tests/ops-hardening.test.mjs +209 -0
  71. package/tests/package-shipping.test.mjs +4 -0
  72. package/tests/recall-intent.test.mjs +23 -0
  73. package/tests/slot-similarity.test.mjs +153 -0
  74. package/tests/state-gc.test.mjs +34 -0
  75. package/tests/state.test.mjs +2 -2
  76. package/tests/tokenizer-v2.test.mjs +1 -1
  77. package/tests/vector-gc.test.mjs +37 -0
  78. package/tests/webui.test.mjs +27 -0
package/lib/media.js CHANGED
@@ -18,6 +18,25 @@ const MIME_EXT = { 'image/png': 'png', 'image/jpeg': 'jpg', 'image/webp': 'webp'
18
18
  const SHA = /^[a-f0-9]{64}$/
19
19
  const captures = new Map()
20
20
  let activeCaptions = 0
21
+ // A v2 bundle is always written with a SHA-256 of memories.json. Legacy v1
22
+ // archives predate that field, so they stay importable without it; a v2
23
+ // manifest with the field removed (or malformed) must be rejected instead of
24
+ // silently trusting an edited body. This is an integrity check against
25
+ // truncation/accidental edits, NOT a signature: a caller able to rewrite the
26
+ // whole manifest can also downgrade `format` to a legacy name, which is an
27
+ // explicit compatibility opt-in rather than a checksum bypass.
28
+ const MEDIA_FORMAT = 'dsh-memu-memory-media@2'
29
+ const LEGACY_MEDIA_FORMATS = ['dsh-memu-memory-media@1', 'dsh-memu-media@1']
30
+ function verifyBundleChecksum(manifest, memories) {
31
+ const digest = manifest.memoriesSha256
32
+ if (!digest) {
33
+ if (!LEGACY_MEDIA_FORMATS.includes(manifest.format)) throw new Error('memories.json checksum missing')
34
+ return
35
+ }
36
+ if (typeof digest !== 'string' || !SHA.test(digest)) throw new Error('memories.json checksum is malformed')
37
+ const actual = crypto.createHash('sha256').update(memories).digest('hex')
38
+ if (actual !== digest) throw new Error('memories.json checksum mismatch')
39
+ }
21
40
  function poolFile(libraryId) {
22
41
  if (!isSafeId(libraryId)) throw new Error('invalid library id')
23
42
  return path.join(indexDir(), `${libraryId}.media.json`)
@@ -291,7 +310,7 @@ export async function captionImage(libraryId, sha256) {
291
310
  export function exportMediaBundle(libraryId, payload) {
292
311
  const pool = loadPool(libraryId)
293
312
  const files = {}
294
- const manifest = { format: 'dsh-memu-memory-media@1', blobs: {} }
313
+ const manifest = { format: MEDIA_FORMAT, blobs: {} }
295
314
  const wanted = new Set(payload.memories.flatMap(entry => (entry.attachments || []).map(item => item.sha256)))
296
315
  for (const sha of wanted) {
297
316
  const blob = pool.blobs[sha]
@@ -304,7 +323,10 @@ export function exportMediaBundle(libraryId, payload) {
304
323
  files[name] = data
305
324
  manifest.blobs[sha] = { ...blob, file: name }
306
325
  }
307
- files['memories.json'] = Buffer.from(JSON.stringify(payload))
326
+ const memoriesBuffer = Buffer.from(JSON.stringify(payload))
327
+ manifest.bundleId = payload?.bundleId || null
328
+ manifest.memoriesSha256 = crypto.createHash('sha256').update(memoriesBuffer).digest('hex')
329
+ files['memories.json'] = memoriesBuffer
308
330
  files['manifest.json'] = Buffer.from(JSON.stringify(manifest))
309
331
  return Buffer.from(zipSync(files, { level: 0 }))
310
332
  }
@@ -318,9 +340,15 @@ export function importMediaBundle(libraryId, archive) {
318
340
  if (file.name !== 'memories.json' && file.name !== 'manifest.json' && !/^attachments\/[a-f0-9]{64}\.(png|jpg|gif|webp)$/.test(file.name)) throw new Error('archive contains an unsupported path')
319
341
  return true
320
342
  } })
321
- const manifest = JSON.parse(Buffer.from(files['manifest.json'] || []).toString('utf8'))
322
- const payload = JSON.parse(Buffer.from(files['memories.json'] || []).toString('utf8'))
323
- if (!['dsh-memu-memory-media@1', 'dsh-memu-media@1'].includes(manifest.format) || !Array.isArray(payload.memories)) throw new Error('invalid memory archive')
343
+ let manifest, payload
344
+ try {
345
+ manifest = JSON.parse(Buffer.from(files['manifest.json'] || []).toString('utf8'))
346
+ payload = JSON.parse(Buffer.from(files['memories.json'] || []).toString('utf8'))
347
+ } catch {
348
+ throw new Error('invalid memory archive')
349
+ }
350
+ if (![MEDIA_FORMAT, ...LEGACY_MEDIA_FORMATS].includes(manifest.format) || !Array.isArray(payload.memories)) throw new Error('invalid memory archive')
351
+ verifyBundleChecksum(manifest, files['memories.json'])
324
352
  let missing = 0
325
353
  withFileLock(poolFile(libraryId), () => {
326
354
  const pool = loadPool(libraryId)
@@ -341,7 +369,7 @@ export function importMediaBundle(libraryId, archive) {
341
369
  }
342
370
  savePool(libraryId, pool)
343
371
  })
344
- return { memories: payload.memories, graph: payload.graph || null, missing }
372
+ return { memories: payload.memories, graph: payload.graph || null, missing, bundleId: payload.bundleId || manifest.bundleId || null }
345
373
  }
346
374
 
347
375
  /** Stream the original images; never materialize a 500 MB library as base64. */
@@ -353,7 +381,10 @@ export async function streamMediaExport(libraryId, payload, response) {
353
381
  if (chunk.length) response.write(chunk)
354
382
  if (final) response.end()
355
383
  })
356
- const manifest = { format: 'dsh-memu-memory-media@1', blobs: {} }
384
+ const manifest = { format: MEDIA_FORMAT, blobs: {} }
385
+ const memoriesBuffer = Buffer.from(JSON.stringify(payload))
386
+ manifest.bundleId = payload.bundleId || null
387
+ manifest.memoriesSha256 = crypto.createHash('sha256').update(memoriesBuffer).digest('hex')
357
388
  const push = async (name, source) => {
358
389
  const entry = new ZipPassThrough(name)
359
390
  zip.add(entry)
@@ -381,7 +412,7 @@ export async function streamMediaExport(libraryId, payload, response) {
381
412
  await push(name, fs.createReadStream(file, { highWaterMark: 64 * 1024 }))
382
413
  manifest.blobs[sha] = { ...blob, file: name }
383
414
  }
384
- await push('memories.json', [Buffer.from(JSON.stringify(payload))])
415
+ await push('memories.json', [memoriesBuffer])
385
416
  await push('manifest.json', [Buffer.from(JSON.stringify(manifest))])
386
417
  zip.end()
387
418
  } catch (error) {
@@ -430,9 +461,15 @@ export async function importMediaStream(libraryId, request) {
430
461
  unzip.push(new Uint8Array(), true)
431
462
  if (failure) throw failure
432
463
  if ([...files.values()].some(item => !item.complete) || !files.has('manifest.json') || !files.has('memories.json')) throw new Error('incomplete memory archive')
433
- const payload = JSON.parse(fs.readFileSync(files.get('memories.json').file, 'utf8'))
434
- const manifest = JSON.parse(fs.readFileSync(files.get('manifest.json').file, 'utf8'))
435
- if (!['dsh-memu-memory-media@1', 'dsh-memu-media@1'].includes(manifest.format) || !Array.isArray(payload.memories)) throw new Error('invalid memory archive')
464
+ let payload, manifest
465
+ try {
466
+ payload = JSON.parse(fs.readFileSync(files.get('memories.json').file, 'utf8'))
467
+ manifest = JSON.parse(fs.readFileSync(files.get('manifest.json').file, 'utf8'))
468
+ } catch {
469
+ throw new Error('invalid memory archive')
470
+ }
471
+ if (![MEDIA_FORMAT, ...LEGACY_MEDIA_FORMATS].includes(manifest.format) || !Array.isArray(payload.memories)) throw new Error('invalid memory archive')
472
+ verifyBundleChecksum(manifest, fs.readFileSync(files.get('memories.json').file))
436
473
  let missing = 0
437
474
  withFileLock(poolFile(libraryId), () => {
438
475
  const pool = loadPool(libraryId)
@@ -460,7 +497,7 @@ export async function importMediaStream(libraryId, request) {
460
497
  throw error
461
498
  }
462
499
  })
463
- return { memories: payload.memories, graph: payload.graph, missing }
500
+ return { memories: payload.memories, graph: payload.graph, missing, bundleId: payload.bundleId || manifest.bundleId || null }
464
501
  } finally {
465
502
  for (const fd of descriptors) { try { fs.closeSync(fd) } catch {} }
466
503
  if (!path.resolve(staging).startsWith(path.resolve(dataDir(), 'tmp') + path.sep)) throw new Error('invalid archive staging directory')