Skip to main content

check_duplicate

Function check_duplicate 

Source
pub fn check_duplicate(
    conn: &Connection,
    query_embedding: &[f32],
    namespace: Option<&str>,
    threshold: f32,
) -> Result<DuplicateCheck>
Expand description

Find the nearest-neighbor live memory by cosine similarity (Pillar 2 / Stream D — memory_check_duplicate).

Linear scan over memories.embedding rows that pass the live-row (non-expired) gate and the optional namespace filter. The chosen candidate is the highest-cosine match across the pool; the caller-supplied threshold is used purely to set is_duplicate on the response — the nearest neighbor is always returned (when the pool is non-empty) so callers can show “closest existing memory was X at similarity Y” even on a not-quite-duplicate.

Threshold is clamped at DUPLICATE_THRESHOLD_MIN so that wildly permissive thresholds can’t be used to dress unrelated content as a merge suggestion.

Returns (check, scanned) where scanned is the count of embedded candidates compared (useful for diagnostics).