-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_test.go
More file actions
599 lines (480 loc) · 14.6 KB
/
memory_test.go
File metadata and controls
599 lines (480 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
package sdk
import (
"context"
"testing"
"time"
"github.com/xraph/ai-sdk/testhelpers"
)
func TestNewMemoryManager(t *testing.T) {
store := &MockStateStore{}
vector := &MockVectorStore{}
logger := testhelpers.NewMockLogger()
metrics := testhelpers.NewMockMetrics()
opts := &MemoryManagerOptions{
AgentID: "agent-1",
WorkingCapacity: 5,
ShortTermTTL: 12 * time.Hour,
ImportanceDecay: 0.2,
AutoConsolidate: true,
}
mm := NewMemoryManager(store, vector, logger, metrics, opts)
if mm == nil {
t.Fatal("expected memory manager to be created")
}
if mm.agentID != "agent-1" {
t.Errorf("expected agent ID 'agent-1', got '%s'", mm.agentID)
}
if mm.workingCapacity != 5 {
t.Errorf("expected working capacity 5, got %d", mm.workingCapacity)
}
if mm.shortTermTTL != 12*time.Hour {
t.Errorf("expected short term TTL 12h, got %v", mm.shortTermTTL)
}
}
func TestNewMemoryManager_Defaults(t *testing.T) {
mm := NewMemoryManager(nil, nil, nil, nil, nil)
if mm == nil {
t.Fatal("expected memory manager with defaults")
}
if mm.workingCapacity != 10 {
t.Errorf("expected default working capacity 10, got %d", mm.workingCapacity)
}
if mm.shortTermTTL != 24*time.Hour {
t.Errorf("expected default short term TTL 24h, got %v", mm.shortTermTTL)
}
if mm.importanceDecay != 0.1 {
t.Errorf("expected default importance decay 0.1, got %f", mm.importanceDecay)
}
}
func TestMemoryManager_Store(t *testing.T) {
mm := NewMemoryManager(nil, nil, nil, nil, &MemoryManagerOptions{
AgentID: "agent-1",
WorkingCapacity: 10,
})
metadata := map[string]any{
"type": "conversation",
}
entry, err := mm.Store(context.Background(), "User asked about weather", metadata, 0.8)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if entry == nil {
t.Fatal("expected memory entry to be created")
}
if entry.Content != "User asked about weather" {
t.Error("expected content to match")
}
if entry.Importance != 0.8 {
t.Errorf("expected importance 0.8, got %f", entry.Importance)
}
if entry.Tier != MemoryTierWorking {
t.Errorf("expected tier 'working', got '%s'", entry.Tier)
}
// Verify it's in working memory
if len(mm.working) != 1 {
t.Errorf("expected 1 entry in working memory, got %d", len(mm.working))
}
}
func TestMemoryManager_Store_WithEmbedding(t *testing.T) {
embedFn := func(ctx context.Context, text string) ([]float64, error) {
return []float64{0.1, 0.2, 0.3}, nil
}
mm := NewMemoryManager(nil, nil, nil, nil, &MemoryManagerOptions{
AgentID: "agent-1",
WorkingCapacity: 10,
EmbeddingFunction: embedFn,
})
entry, err := mm.Store(context.Background(), "Test content", nil, 0.5)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if len(entry.Embedding) == 0 {
t.Error("expected embedding to be generated")
}
if len(entry.Embedding) != 3 {
t.Errorf("expected 3 embedding dimensions, got %d", len(entry.Embedding))
}
}
func TestMemoryManager_Recall_Working(t *testing.T) {
mockVector := &MockVectorStore{
UpsertFunc: func(ctx context.Context, vectors []Vector) error {
return nil
},
}
mm := NewMemoryManager(nil, mockVector, nil, nil, &MemoryManagerOptions{
AgentID: "agent-1",
WorkingCapacity: 10, // Ensure enough capacity
})
t.Logf("Working capacity: %d", mm.workingCapacity)
// Store some memories
_, err1 := mm.Store(context.Background(), "Memory 1", nil, 0.9)
if err1 != nil {
t.Fatalf("failed to store memory 1: %v", err1)
}
t.Logf("After mem1: working=%d", len(mm.working))
_, err2 := mm.Store(context.Background(), "Memory 2", nil, 0.7)
if err2 != nil {
t.Fatalf("failed to store memory 2: %v", err2)
}
t.Logf("After mem2: working=%d", len(mm.working))
_, err3 := mm.Store(context.Background(), "Memory 3", nil, 0.5)
if err3 != nil {
t.Fatalf("failed to store memory 3: %v", err3)
}
t.Logf("After mem3: working=%d", len(mm.working))
memories, err := mm.Recall(context.Background(), "test query", MemoryTierWorking, 10)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if len(memories) != 3 {
t.Errorf("expected 3 memories, got %d", len(memories))
}
// Should be sorted by importance (highest first)
if len(memories) >= 2 && memories[0].Importance < memories[1].Importance {
t.Error("expected memories to be sorted by importance")
}
}
func TestMemoryManager_Recall_ShortTerm(t *testing.T) {
// Mock vector store that returns results
mockVector := &MockVectorStore{
QueryFunc: func(ctx context.Context, vector []float64, limit int, filter map[string]any) ([]VectorMatch, error) {
return []VectorMatch{
{
ID: "mem1",
Score: 0.9,
Metadata: map[string]any{
"content": "Short term memory",
"importance": 0.8,
},
},
}, nil
},
}
embedFn := func(ctx context.Context, text string) ([]float64, error) {
return []float64{0.1, 0.2}, nil
}
mm := NewMemoryManager(nil, mockVector, nil, nil, &MemoryManagerOptions{
AgentID: "agent-1",
EmbeddingFunction: embedFn,
})
memories, err := mm.Recall(context.Background(), "test query", MemoryTierShortTerm, 5)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if len(memories) == 0 {
t.Error("expected to recall short-term memories")
}
if memories[0].Content != "Short term memory" {
t.Error("expected content to be retrieved")
}
}
func TestMemoryManager_Recall_Limit(t *testing.T) {
mockVector := &MockVectorStore{
UpsertFunc: func(ctx context.Context, vectors []Vector) error {
return nil
},
}
// Provide embedding function so memories can be consolidated
embedFn := func(ctx context.Context, text string) ([]float64, error) {
return []float64{0.1, 0.2}, nil
}
mm := NewMemoryManager(nil, mockVector, nil, nil, &MemoryManagerOptions{
AgentID: "agent-1",
WorkingCapacity: 3, // Set capacity to 3 to test consolidation
EmbeddingFunction: embedFn,
})
// Store 5 memories (more than capacity)
for range 5 {
_, _ = mm.Store(context.Background(), "Memory", nil, 0.5)
}
// Recall with limit of 3
memories, err := mm.Recall(context.Background(), "", MemoryTierWorking, 3)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if len(memories) != 3 {
t.Errorf("expected 3 memories (limit), got %d", len(memories))
}
}
func TestMemoryManager_ConsolidateWorking(t *testing.T) {
mockVector := &MockVectorStore{
UpsertFunc: func(ctx context.Context, vectors []Vector) error {
return nil
},
}
embedFn := func(ctx context.Context, text string) ([]float64, error) {
return []float64{0.1, 0.2}, nil
}
mm := NewMemoryManager(nil, mockVector, nil, nil, &MemoryManagerOptions{
AgentID: "agent-1",
WorkingCapacity: 2,
EmbeddingFunction: embedFn,
})
// Store more than capacity
_, _ = mm.Store(context.Background(), "Memory 1", nil, 0.9)
_, _ = mm.Store(context.Background(), "Memory 2", nil, 0.7)
_, _ = mm.Store(context.Background(), "Memory 3", nil, 0.5) // Should trigger consolidation
// Working memory should be at capacity
if len(mm.working) > mm.workingCapacity {
t.Errorf("expected working memory to be at capacity %d, got %d", mm.workingCapacity, len(mm.working))
}
}
func TestMemoryManager_Promote(t *testing.T) {
mockVector := &MockVectorStore{
UpsertFunc: func(ctx context.Context, vectors []Vector) error {
return nil
},
QueryFunc: func(ctx context.Context, vector []float64, limit int, filter map[string]any) ([]VectorMatch, error) {
return []VectorMatch{
{
ID: "mem1",
Score: 0.9,
Metadata: map[string]any{
"content": "Memory to promote",
"importance": 0.8,
},
},
}, nil
},
}
embedFn := func(ctx context.Context, text string) ([]float64, error) {
return []float64{0.1, 0.2}, nil
}
mm := NewMemoryManager(nil, mockVector, nil, nil, &MemoryManagerOptions{
AgentID: "agent-1",
EmbeddingFunction: embedFn,
})
err := mm.Promote(context.Background(), "mem1")
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
}
func TestMemoryManager_Promote_NotFound(t *testing.T) {
mockVector := &MockVectorStore{
QueryFunc: func(ctx context.Context, vector []float64, limit int, filter map[string]any) ([]VectorMatch, error) {
return []VectorMatch{}, nil
},
}
embedFn := func(ctx context.Context, text string) ([]float64, error) {
return []float64{0.1, 0.2}, nil
}
mm := NewMemoryManager(nil, mockVector, nil, nil, &MemoryManagerOptions{
AgentID: "agent-1",
EmbeddingFunction: embedFn,
})
err := mm.Promote(context.Background(), "nonexistent")
if err == nil {
t.Error("expected error for non-existent memory")
}
}
func TestMemoryManager_CreateEpisode(t *testing.T) {
store := &MockStateStore{
SaveFunc: func(ctx context.Context, state *AgentState) error {
return nil
},
}
mm := NewMemoryManager(store, nil, nil, nil, &MemoryManagerOptions{
AgentID: "agent-1",
})
memoryIDs := []string{"mem1", "mem2", "mem3"}
metadata := map[string]any{
"category": "meeting",
}
episode, err := mm.CreateEpisode(
context.Background(),
"Daily Standup",
"Team discussed project progress",
memoryIDs,
metadata,
)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if episode == nil {
t.Fatal("expected episode to be created")
}
if episode.Title != "Daily Standup" {
t.Error("expected title to match")
}
if len(episode.Memories) != 3 {
t.Errorf("expected 3 memory IDs, got %d", len(episode.Memories))
}
if episode.Importance != 0.8 {
t.Errorf("expected default importance 0.8, got %f", episode.Importance)
}
// Verify it's stored
if len(mm.episodic) != 1 {
t.Errorf("expected 1 episode in storage, got %d", len(mm.episodic))
}
}
func TestMemoryManager_GetEpisode(t *testing.T) {
mm := NewMemoryManager(nil, nil, nil, nil, &MemoryManagerOptions{
AgentID: "agent-1",
})
// Create an episode
episode, _ := mm.CreateEpisode(
context.Background(),
"Test Episode",
"Description",
[]string{"mem1"},
nil,
)
// Retrieve it
retrieved, err := mm.GetEpisode(episode.ID)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if retrieved.ID != episode.ID {
t.Error("expected retrieved episode to match")
}
if retrieved.Title != "Test Episode" {
t.Error("expected title to match")
}
}
func TestMemoryManager_GetEpisode_NotFound(t *testing.T) {
mm := NewMemoryManager(nil, nil, nil, nil, &MemoryManagerOptions{
AgentID: "agent-1",
})
_, err := mm.GetEpisode("nonexistent")
if err == nil {
t.Error("expected error for non-existent episode")
}
}
func TestMemoryManager_Forget(t *testing.T) {
mockVector := &MockVectorStore{
DeleteFunc: func(ctx context.Context, ids []string) error {
return nil
},
}
mm := NewMemoryManager(nil, mockVector, nil, nil, &MemoryManagerOptions{
AgentID: "agent-1",
})
// Store a memory
entry, _ := mm.Store(context.Background(), "Memory to forget", nil, 0.5)
// Verify it exists
if len(mm.working) != 1 {
t.Fatal("expected memory to be stored")
}
// Forget it
err := mm.Forget(context.Background(), entry.ID)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
// Verify it's gone
if len(mm.working) != 0 {
t.Errorf("expected memory to be forgotten, got %d in working", len(mm.working))
}
}
func TestMemoryManager_PruneExpired(t *testing.T) {
mm := NewMemoryManager(nil, nil, nil, nil, &MemoryManagerOptions{
AgentID: "agent-1",
})
count, err := mm.PruneExpired(context.Background())
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
// Count should be 0 in this simple test
if count < 0 {
t.Errorf("expected non-negative count, got %d", count)
}
}
func TestMemoryManager_GetStats(t *testing.T) {
mockVector := &MockVectorStore{
UpsertFunc: func(ctx context.Context, vectors []Vector) error {
return nil
},
}
mm := NewMemoryManager(nil, mockVector, nil, nil, &MemoryManagerOptions{
AgentID: "agent-1",
WorkingCapacity: 10, // Enough to hold both memories
})
// Store some memories
_, _ = mm.Store(context.Background(), "Memory 1", nil, 0.8)
_, _ = mm.Store(context.Background(), "Memory 2", nil, 0.7)
// Create an episode
_, _ = mm.CreateEpisode(context.Background(), "Episode", "Desc", []string{"mem1"}, nil)
stats := mm.GetStats()
if stats.WorkingCount != 2 {
t.Errorf("expected 2 in working, got %d", stats.WorkingCount)
}
if stats.EpisodicCount != 1 {
t.Errorf("expected 1 episodic, got %d", stats.EpisodicCount)
}
if stats.WorkingCapacity != 10 {
t.Errorf("expected working capacity 10, got %d", stats.WorkingCapacity)
}
}
func TestMemoryManager_Clear(t *testing.T) {
mm := NewMemoryManager(nil, nil, nil, nil, &MemoryManagerOptions{
AgentID: "agent-1",
})
// Store some data
_, _ = mm.Store(context.Background(), "Memory", nil, 0.5)
_, _ = mm.CreateEpisode(context.Background(), "Episode", "Desc", []string{}, nil)
// Clear
err := mm.Clear(context.Background())
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
// Verify everything is cleared
if len(mm.working) != 0 {
t.Error("expected working memory to be cleared")
}
if len(mm.episodic) != 0 {
t.Error("expected episodic memory to be cleared")
}
}
func TestMemoryEntry_AccessTracking(t *testing.T) {
mm := NewMemoryManager(nil, nil, nil, nil, &MemoryManagerOptions{
AgentID: "agent-1",
})
entry, _ := mm.Store(context.Background(), "Test memory", nil, 0.5)
initialAccessCount := entry.AccessCount
// Recall should increment access count
_, _ = mm.Recall(context.Background(), "", MemoryTierWorking, 10)
// Check if access was tracked (in working memory)
if updatedEntry, ok := mm.working[entry.ID]; ok {
if updatedEntry.AccessCount <= initialAccessCount {
t.Error("expected access count to be incremented")
}
}
}
func TestMemoryTier_Constants(t *testing.T) {
if MemoryTierWorking != "working" {
t.Error("expected working tier constant to be 'working'")
}
if MemoryTierShortTerm != "short_term" {
t.Error("expected short term tier constant to be 'short_term'")
}
if MemoryTierLongTerm != "long_term" {
t.Error("expected long term tier constant to be 'long_term'")
}
if MemoryTierEpisodic != "episodic" {
t.Error("expected episodic tier constant to be 'episodic'")
}
}
func TestMemoryManager_ThreadSafety(t *testing.T) {
mm := NewMemoryManager(nil, nil, nil, nil, &MemoryManagerOptions{
AgentID: "agent-1",
})
done := make(chan bool)
// Concurrent writes
go func() {
for range 10 {
_, _ = mm.Store(context.Background(), "Memory", nil, 0.5)
}
done <- true
}()
// Concurrent reads
go func() {
for range 10 {
_, _ = mm.Recall(context.Background(), "", MemoryTierWorking, 10)
mm.GetStats()
}
done <- true
}()
// Wait for both
<-done
<-done
// If we get here without data races, the test passes
}