memorix icon indicating copy to clipboard operation
memorix copied to clipboard

Embedding cache worker postMessage can overwrite a 319MB cache with an empty map

Open RaviTharuma opened this issue 3 months ago • 2 comments

Description

parseCacheJsonInWorker posted ~27,320 × 1024-d float vectors back to the parent via postMessage (structured clone). That clone failed / OOM'd. The empty catch {} treated the cache as empty, set diskCacheLoaded = true, then saveDiskCacheNow() overwrote a 319MB on-disk cache with ~5 entries (68KB).

This is silent data loss. Lexical search still works; hybrid/vector recall is gone until a full re-embed.

Steps to Reproduce

  1. Have a JSON-array embedding cache ≥ ~100MB at ~/.memorix/data/.embedding-api-cache.json (27k+ vectors).
  2. Start HTTP or CLI in a way that loads the cache through the worker parse path.
  3. Watch the cache file size after the first successful "load" + save.

Expected Behavior

A failed off-thread parse must not overwrite a larger on-disk cache. Vectors should never be postMessage'd as a giant structured-clone payload.

Actual Behavior

On 2026-08-17 the 319,521,596-byte cache was replaced by a 68KB file. Restored from .jina-omni-reembed.jsonl (27,320 lines).

Environment

  • Memorix 1.6.0
  • macOS, live store ~/.memorix/data/
  • Jina Omni 1024d, 27,320 cached vectors

Additional Context

#214 writes JSONL, converts the array cache on disk (worker posts {ok, count} only), and refuses to save when in-memory size is < 50% of on-disk line count. Owner review on #214 also asked for atomic JSON→JSONL rename and a bounded-memory load (do not materialize an unbounded number of 4096d vectors).

Related: #25, #214.

RaviTharuma avatar Aug 17 '26 13:08 RaviTharuma

Fix is in PR https://github.com/AVIDS2/memorix/pull/214 — not on main yet, so leaving this issue open until that PR merges.

Relevant work:

  • 35358a1a4c — stream the embedding cache from JSONL; refuse to overwrite a larger on-disk cache with a smaller in-memory map (including an empty worker postMessage)
  • 382e367d9d — type-narrow cache rows, bound in-memory vectors, atomic JSON→JSONL migrate
  • Files: src/embedding/api-provider.ts, tests/embedding/cache-worker-parse.test.ts, tests/embedding/cache-jsonl-and-guard.test.ts

RaviTharuma avatar Aug 17 '26 16:08 RaviTharuma

Fixed by #214 and released in v1.6.2. Cache persistence now fails closed instead of allowing an empty/incomplete worker result to replace a larger on-disk cache. Closing as shipped.

AVIDS2 avatar Aug 18 '26 10:08 AVIDS2