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.
- package/CHANGELOG.md +472 -0
- package/README.md +1 -1
- package/README.zh.md +1 -1
- package/lib/backup.js +154 -25
- package/lib/binding-diagnostics.js +1 -2
- package/lib/budget.js +0 -9
- package/lib/client.js +221 -128
- package/lib/commitment.js +3 -3
- package/lib/consolidation-ledger.js +5 -2
- package/lib/curator-policy.js +23 -0
- package/lib/cycle.js +8 -4
- package/lib/embedding-download.js +168 -28
- package/lib/embedding-runtime.js +76 -17
- package/lib/engine.js +0 -6
- package/lib/freshness.js +2 -2
- package/lib/graph.js +139 -11
- package/lib/index.js +71 -20
- package/lib/inject.js +50 -7
- package/lib/lexicon.js +61 -16
- package/lib/local-vector-service.js +21 -1
- package/lib/media.js +49 -12
- package/lib/memory.js +662 -116
- package/lib/paths.js +0 -7
- package/lib/preset-scope.js +6 -1
- package/lib/private.js +45 -7
- package/lib/prompts.js +0 -7
- package/lib/rpc.js +95 -17
- package/lib/snippet.js +18 -2
- package/lib/state.js +109 -30
- package/lib/stats.js +2 -2
- package/lib/temporal.js +19 -0
- package/lib/text-retrieval.js +120 -44
- package/lib/text-similarity.js +205 -24
- package/lib/tokenizer-v2.js +0 -24
- package/lib/vector-migration.js +15 -0
- package/lib/vector-profile.js +81 -8
- package/lib/visibility.js +0 -4
- package/lib/webui/standalone.css +17 -22
- package/lib/webui/standalone.js +27 -9
- package/lib/webui.js +4 -4
- package/package.json +3 -2
- package/scripts/acceptance-local.mjs +2 -2
- package/scripts/check-all.mjs +3 -1
- package/scripts/check-i18n.mjs +35 -1
- package/scripts/migrate-visibility.mjs +3 -3
- package/scripts/prune-sessions.mjs +34 -0
- package/scripts/prune-vectors.mjs +24 -0
- package/tests/appendix-b.test.mjs +5 -4
- package/tests/audit-batch3.test.mjs +247 -0
- package/tests/audit-regression.test.mjs +2 -2
- package/tests/backup.test.mjs +83 -83
- package/tests/browser.test.mjs +6 -1
- package/tests/client-render.test.mjs +2 -2
- package/tests/curator-clusters.test.mjs +18 -0
- package/tests/curator-pairing.test.mjs +59 -16
- package/tests/dynamic-rrf.test.mjs +22 -19
- package/tests/dynamic-threshold.test.mjs +52 -0
- package/tests/fact-version.test.mjs +18 -0
- package/tests/fixtures/tokenizer-cases.mjs +27 -0
- package/tests/graph-cascade.test.mjs +29 -0
- package/tests/graph.test.mjs +45 -0
- package/tests/inject-conflict.test.mjs +18 -0
- package/tests/lexical-bm25.test.mjs +171 -0
- package/tests/lexicon-frequency.test.mjs +16 -0
- package/tests/lexicon-inject.test.mjs +21 -0
- package/tests/lexicon-perf.test.mjs +37 -9
- package/tests/lexicon-scope.test.mjs +2 -2
- package/tests/lexicon-ui.test.mjs +1 -1
- package/tests/managed-memory.test.mjs +19 -0
- package/tests/ops-hardening.test.mjs +209 -0
- package/tests/package-shipping.test.mjs +4 -0
- package/tests/recall-intent.test.mjs +23 -0
- package/tests/slot-similarity.test.mjs +153 -0
- package/tests/state-gc.test.mjs +34 -0
- package/tests/state.test.mjs +2 -2
- package/tests/tokenizer-v2.test.mjs +1 -1
- package/tests/vector-gc.test.mjs +37 -0
- 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:
|
|
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
|
-
|
|
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
|
-
|
|
322
|
-
|
|
323
|
-
|
|
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:
|
|
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', [
|
|
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
|
-
|
|
434
|
-
|
|
435
|
-
|
|
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')
|