Skip to content

feat: cosine-based auto-linking + dedup detection on remember() #401

Closed

Description

@ajianaz

Motivation

Issue #346 (pattern-based auto-edges) is closed and ships with [[ref]], @tag, ^id patterns. This works for structured content but misses implicit similarity — memories that are semantically related without explicit references.

Currently, two memories about the same topic written by different agents have zero graph connection unless someone manually adds [[ref]] patterns.

Real-world example (from Hermes, 77 memories)

Memory A [default]: "TrapFall repo BELUM dibuat. Target: codecoradev/..."
Memory B [hermes-knowledge]: "TrapFall repo BELUM dibuat. Target: codecoradev/..."
Cosine: 0.9145 — nearly identical, but no edge exists

With 77 memories, 2 pairs exceed cosine 0.90 and 11 pairs exceed 0.85. All currently invisible to graph traversal and recall scoring.

Proposal

1. Cosine-based Auto-Link on remember()

After embedding a new memory, compute cosine similarity against existing memories and auto-create SIMILAR_TO edges.

fn auto_link_cosine(&self, new_id: &str, embedding: &[f32]) -> Result<()> {
    let candidates = self.vector_index.search(embedding, top_k=20)?;
    
    for (mem_id, score) in candidates {
        if score >= config.auto_link_threshold {
            self.insert_graph_edge(
                source = new_id,
                target = mem_id,
                relation = "similar_to",
                weight = score as f64,
            );
        }
        if score >= config.dedup_threshold {
            self.insert_graph_edge(
                source = new_id,
                target = mem_id,
                relation = "possible_duplicate",
                weight = score as f64,
            );
        }
    }
}

2. Dedup Detection + Auto-Merge

When cosine >= dedup_threshold (0.95), flag as duplicate. If auto-merge enabled, merge:

Merge logic:

  1. Pick "best" memory (longest content, highest access_count)
  2. Migrate tags + metadata from loser → winner
  3. Create edge: loser supersedes_to winner (bidirectional)
  4. Mark loser as deprecated

Config:

[graph]
auto_link_enabled = true
auto_link_threshold = 0.90    # cosine >= 0.90 → SIMILAR_TO edge
dedup_threshold = 0.95        # cosine >= 0.95 → possible_duplicate
auto_merge = false           # flag only by default (safe)

3. New CLI Commands

uteke graph dedup              # list possible_duplicate edges
uteke graph dedup --merge      # auto-merge all flagged pairs
uteke graph dedup --dry-run    # preview merge without executing

Validation (Real Data)

Tested on 77 memories (2,926 pairs) from a production Hermes instance:

Threshold Pairs Examples
>= 0.95 0 No duplicates (clean data)
>= 0.90 2 True cross-namespace duplicates
>= 0.85 11 Related but distinct memories
>= 0.80 14 Evolution of same topic (v0.3 → v0.5)
>= 0.75 23 Starts noisy (join messages)

Conclusion: Threshold 0.90 produces clean, valid edges. 0.95 is safe for dedup (no false positives in current data).

Acceptance Criteria

  1. remember() auto-creates SIMILAR_TO edges when cosine >= threshold
  2. remember() auto-creates possible_duplicate edges when cosine >= dedup threshold
  3. graph dedup lists all possible_duplicate edges
  4. graph dedup --merge merges flagged pairs (content, tags, metadata)
  5. Config [graph] section controls all thresholds and toggles
  6. Zero regression: existing [[ref]] pattern edges (feat: typed auto-edges — self-wiring knowledge graph on every remember() #346) still work
  7. Unit tests: threshold boundary, empty DB, identical memories, cross-namespace
  8. Benchmark: remember() latency increase < 50ms for 1K memories

Out of Scope

Dependencies

Open Questions

  1. Should auto-link be opt-in or default-on? (Proposal: default-on, configurable)
  2. Should possible_duplicate edges be bidirectional? (Proposal: yes)
  3. Performance: cosine against ALL memories vs top-K candidates? (Proposal: top-20 from usearch, then filter by threshold)

Activity

  1. added
    enhancement
    scope:core
    type:feature
    priority:high
    epic:memory-engine
    on Jun 19, 2026
  2. added this to the v0.3.0 — Graph RAG milestone on Jun 21, 2026
  3. added a commit that references this issue on Jun 21, 2026
  4. ajianaz commented on Jun 21, 2026

    @ajianaz
    CollaboratorAuthor

    Implemented in #421. auto_link_cosine() creates similar_to (>=0.80) and possible_duplicate (>=0.92) edges on every remember().

  5. added a commit that references this issue on Jun 21, 2026
  6. added 2 commits that reference this issue on Aug 13, 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

    enhancementepic:memory-enginepriority:highscope:coretype:feature

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions