|
| 1 | +package memory |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestRecall_BoostTags(t *testing.T) { |
| 9 | + s := newTestStore(t) |
| 10 | + ctx := context.Background() |
| 11 | + |
| 12 | + // Store two entries with different tags at nearly equal distance from query |
| 13 | + // angle 0.3 from query(0.3): auth at 0 → dist=0.045, db at 0.6 → dist=0.045 |
| 14 | + _, _ = s.Store(ctx, StoreRequest{ |
| 15 | + Entries: []StoreEntry{ |
| 16 | + {Text: "Auth uses JWT", Embedding: makeEmbedding(0, 8), Tags: []string{"auth"}}, |
| 17 | + {Text: "DB uses Postgres", Embedding: makeEmbedding(0.6, 8), Tags: []string{"database"}}, |
| 18 | + }, |
| 19 | + }) |
| 20 | + |
| 21 | + // Query equidistant from both; boost on "database" should tip the ranking |
| 22 | + recall, err := s.Recall(ctx, RecallRequest{ |
| 23 | + Query: "infrastructure", |
| 24 | + QueryEmbedding: makeEmbedding(0.3, 8), // equidistant from 0 and 0.6 |
| 25 | + MaxResults: 10, |
| 26 | + BoostTags: []string{"database"}, |
| 27 | + }) |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("Recall: %v", err) |
| 30 | + } |
| 31 | + if len(recall.Memories) != 2 { |
| 32 | + t.Fatalf("expected 2 memories, got %d", len(recall.Memories)) |
| 33 | + } |
| 34 | + // With the boost, database entry should be ranked first |
| 35 | + if recall.Memories[0].Tags[0] != "database" { |
| 36 | + t.Errorf("expected database entry first (boosted), got tags=%v", recall.Memories[0].Tags) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestRecall_MinRelevance(t *testing.T) { |
| 41 | + s := newTestStore(t) |
| 42 | + ctx := context.Background() |
| 43 | + |
| 44 | + _, _ = s.Store(ctx, StoreRequest{ |
| 45 | + Entries: []StoreEntry{ |
| 46 | + {Text: "Highly relevant", Embedding: makeEmbedding(0, 8)}, |
| 47 | + {Text: "Somewhat relevant", Embedding: makeEmbedding(0.6, 8)}, |
| 48 | + {Text: "Not relevant", Embedding: makeEmbedding(2.0, 8)}, |
| 49 | + }, |
| 50 | + }) |
| 51 | + |
| 52 | + // Query with high min relevance — should filter out low-scoring entries |
| 53 | + recall, err := s.Recall(ctx, RecallRequest{ |
| 54 | + Query: "test", |
| 55 | + QueryEmbedding: makeEmbedding(0, 8), |
| 56 | + MaxResults: 10, |
| 57 | + MinRelevance: 0.8, |
| 58 | + }) |
| 59 | + if err != nil { |
| 60 | + t.Fatalf("Recall: %v", err) |
| 61 | + } |
| 62 | + // Only the highly relevant entry (cosine similarity ~1.0) should pass |
| 63 | + if len(recall.Memories) == 0 { |
| 64 | + t.Fatal("expected at least 1 memory above min relevance") |
| 65 | + } |
| 66 | + for _, m := range recall.Memories { |
| 67 | + if m.Relevance < 0.8 { |
| 68 | + t.Errorf("memory %s has relevance %.3f, below min 0.8", m.ID, m.Relevance) |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestRecall_MinRelevance_Zero_NoFilter(t *testing.T) { |
| 74 | + s := newTestStore(t) |
| 75 | + ctx := context.Background() |
| 76 | + |
| 77 | + _, _ = s.Store(ctx, StoreRequest{ |
| 78 | + Entries: []StoreEntry{ |
| 79 | + {Text: "Entry A", Embedding: makeEmbedding(0, 8)}, |
| 80 | + {Text: "Entry B", Embedding: makeEmbedding(2.0, 8)}, |
| 81 | + }, |
| 82 | + }) |
| 83 | + |
| 84 | + // MinRelevance=0 should return all entries |
| 85 | + recall, _ := s.Recall(ctx, RecallRequest{ |
| 86 | + Query: "test", |
| 87 | + QueryEmbedding: makeEmbedding(0, 8), |
| 88 | + MaxResults: 10, |
| 89 | + MinRelevance: 0, |
| 90 | + }) |
| 91 | + if len(recall.Memories) != 2 { |
| 92 | + t.Errorf("expected 2 memories with no min filter, got %d", len(recall.Memories)) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestRecall_TaskContext_SourceBoost(t *testing.T) { |
| 97 | + s := newTestStore(t) |
| 98 | + ctx := context.Background() |
| 99 | + |
| 100 | + // Use angles far enough apart to avoid dedup (>0.555 rad) |
| 101 | + _, _ = s.Store(ctx, StoreRequest{ |
| 102 | + Entries: []StoreEntry{ |
| 103 | + {Text: "JWT validation logic", Embedding: makeEmbedding(0, 8), Source: "code_review"}, |
| 104 | + {Text: "JWT token format", Embedding: makeEmbedding(0.6, 8), Source: "docs"}, |
| 105 | + }, |
| 106 | + }) |
| 107 | + |
| 108 | + // Query equidistant; task context mentions "code_review" — should boost that source |
| 109 | + recall, err := s.Recall(ctx, RecallRequest{ |
| 110 | + Query: "JWT", |
| 111 | + QueryEmbedding: makeEmbedding(0.3, 8), // equidistant from 0 and 0.6 |
| 112 | + MaxResults: 10, |
| 113 | + TaskContext: "reviewing code_review findings", |
| 114 | + }) |
| 115 | + if err != nil { |
| 116 | + t.Fatalf("Recall: %v", err) |
| 117 | + } |
| 118 | + if len(recall.Memories) < 2 { |
| 119 | + t.Fatalf("expected 2 memories, got %d", len(recall.Memories)) |
| 120 | + } |
| 121 | + if recall.Memories[0].Source != "code_review" { |
| 122 | + t.Errorf("expected code_review source first (boosted), got %s", recall.Memories[0].Source) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func TestRecall_RelevanceClamped(t *testing.T) { |
| 127 | + s := newTestStore(t) |
| 128 | + ctx := context.Background() |
| 129 | + |
| 130 | + _, _ = s.Store(ctx, StoreRequest{ |
| 131 | + Entries: []StoreEntry{ |
| 132 | + {Text: "Perfect match", Embedding: makeEmbedding(0, 8), Tags: []string{"auth"}}, |
| 133 | + }, |
| 134 | + }) |
| 135 | + |
| 136 | + // Exact embedding match + boost tag + task context = would exceed 1.0 |
| 137 | + recall, _ := s.Recall(ctx, RecallRequest{ |
| 138 | + Query: "auth", |
| 139 | + QueryEmbedding: makeEmbedding(0, 8), |
| 140 | + MaxResults: 10, |
| 141 | + BoostTags: []string{"auth"}, |
| 142 | + TaskContext: "auth", |
| 143 | + }) |
| 144 | + if len(recall.Memories) != 1 { |
| 145 | + t.Fatalf("expected 1 memory, got %d", len(recall.Memories)) |
| 146 | + } |
| 147 | + if recall.Memories[0].Relevance > 1.0 { |
| 148 | + t.Errorf("relevance should be clamped to 1.0, got %.3f", recall.Memories[0].Relevance) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func TestRecall_BoostTags_Empty_NoEffect(t *testing.T) { |
| 153 | + s := newTestStore(t) |
| 154 | + ctx := context.Background() |
| 155 | + |
| 156 | + _, _ = s.Store(ctx, StoreRequest{ |
| 157 | + Entries: []StoreEntry{ |
| 158 | + {Text: "Entry A", Embedding: makeEmbedding(0, 8), Tags: []string{"a"}}, |
| 159 | + {Text: "Entry B", Embedding: makeEmbedding(0.6, 8), Tags: []string{"b"}}, |
| 160 | + }, |
| 161 | + }) |
| 162 | + |
| 163 | + // No boost tags — ranking should be purely by similarity |
| 164 | + recall, _ := s.Recall(ctx, RecallRequest{ |
| 165 | + Query: "test", |
| 166 | + QueryEmbedding: makeEmbedding(0, 8), |
| 167 | + MaxResults: 10, |
| 168 | + }) |
| 169 | + if len(recall.Memories) != 2 { |
| 170 | + t.Fatalf("expected 2 memories, got %d", len(recall.Memories)) |
| 171 | + } |
| 172 | + // Entry A is closer to query (angle 0 vs 0.6) |
| 173 | + if recall.Memories[0].Text != "Entry A" { |
| 174 | + t.Errorf("expected Entry A first (closer), got %s", recall.Memories[0].Text) |
| 175 | + } |
| 176 | +} |
0 commit comments