Skip to content

Memory: use the existing embeddings for dedup and contradiction detection #310

Closed

Description

@currentbits

Code audit finding: entries get embedded (all-MiniLM-L6-v2, entry_vectors table) but dedup on write is lexical Jaccard over the 500 newest entries (src/memory.js:315-331), and the janitor's contradiction scan is shared-entity-count + Jaccard (src/janitor.js:21-90). Neither uses the vectors that already exist.

Use cosine over entry_vectors for near-duplicate detection (catches paraphrases Jaccard misses) and to rank contradiction candidates before enqueueing review pairs. While there: the vector leg is brute-force cosine with BLOB decode per row on every query — fine at hundreds of entries, but if the store grows, add sqlite-vec for ANN rather than hand-rolling.

Activity

  1. added
    enhancement
    plan:todo
    plan:doing
    and removed
    plan:todo
    on Aug 17, 2026
  2. currentbits commented on Aug 17, 2026

    @currentbits
    OwnerAuthor

    Approach settled after reading the flow. Shared contract landed first, two grok workers building on it in parallel:

    Contract (done, 4289207) — semanticNeighbors(db, vec, opts) in memory-server/src/review.js, whose docstring already declared it "shared by store gate + janitor scan". Cosine over entry_vectors, same-project + live only, fail-soft. Plus SEMANTIC_DUP = 0.88 / SEMANTIC_RELATED = 0.6. Covered by test/semantic-neighbors.test.js.

    Worker A — write path (memory.js): semantic near-dup check runs inside embedEntry, right after the vector is UPSERTed, and enqueues a near_dup pair at cosine >= 0.88.

    Worker B — janitor (janitor.js): contradiction candidates ranked by cosine, capped at 3 pairs per entry, with a third gate (shared entity + cosine >= 0.6) catching the paraphrased contradictions Jaccard scores under 0.4. Falls back to today's exact behaviour when an entry has no vector.

    Two deliberate non-goals:

    • store() stays synchronous, so the hard block gate stays lexical. Embedding is async; making the block gate semantic forces store() async and await onto ~50 test call sites and 2 callers. A paraphrase now gets stored and immediately queued for review rather than refused — the review queue is the designed mechanism for that, and memory_maintenance/memory_resolve already surface it.
    • No sqlite-vec. Brute-force cosine with a per-row BLOB decode is fine at this store size; the ceiling and the ANN upgrade path are noted in a ponytail: comment at the helper.
  3. currentbits commented on Aug 17, 2026

    @currentbits
    OwnerAuthor

    Correction to my previous comment: this issue was already implemented on main before I started.

    My worktree was cut from 32917ec. Commit 7de6476 (merge of coder/memory-use-the-existing-embeddings-for-d-d60c60) landed on main after that, from a parallel thread on this same issue, and already ships both halves: checkSemanticDup() + SEMANTIC_DUP = 0.9 in memory.js, and CONTRA_SIM/CONTRA_TOP cosine ranking with a per-pass vector cache in janitor.js. That is the issue as filed; nothing further is needed for it.

    What survives from my thread is a refactor, not a feature: main had two hand-rolled brute-force cosine scans (vectorSearch and checkSemanticDup). The shared semanticNeighbors() helper collapses the second into a 5-line call and deletes checkSemanticDup entirely: net -62 lines, suite green 157/157.

    Two deltas, deliberately handled:

    • Threshold drifted 0.9 to 0.88 in the worker's commit. Restored to 0.9 so the rewire is behaviour-preserving.
    • Scope: checkSemanticDup used projectScopeSql, so a global entry compared against every project's entries while a project entry compared only within its own. The helper uses exact project IS ? both ways, matching the lexical gate findNearDup. Small but real behaviour change; worth a look rather than a rubber stamp.

    I did not rebuild the janitor half. main's version caches decoded vectors across candidates within a pass; routing it through semanticNeighbors would re-decode per candidate, which is strictly worse.

  4. added
    plan:done
    and removed
    plan:doing
    on Aug 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementplan:done

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions