|
| 1 | +package memory |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "sync/atomic" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// callCountLLM counts every LLM invocation and proxies to a mockLLM. |
| 12 | +type callCountLLM struct { |
| 13 | + calls int64 |
| 14 | + inner *mockLLM |
| 15 | +} |
| 16 | + |
| 17 | +func (c *callCountLLM) SimpleCall(ctx context.Context, system, user string) (string, error) { |
| 18 | + atomic.AddInt64(&c.calls, 1) |
| 19 | + return c.inner.SimpleCall(ctx, system, user) |
| 20 | +} |
| 21 | + |
| 22 | +// TestAddFact_NoLLMCalls: AddFact must complete without making any LLM calls, |
| 23 | +// even when merge-on-write classifies a new entry as "merge" or "judge". |
| 24 | +func TestAddFact_NoLLMCalls(t *testing.T) { |
| 25 | + dir := t.TempDir() |
| 26 | + llm := &callCountLLM{inner: &mockLLM{responses: map[string]string{}}} |
| 27 | + mm := NewMemoryManager(dir, llm, DefaultMemoryConfig()) |
| 28 | + |
| 29 | + // Seed an entry so subsequent adds trigger merge-on-write comparisons. |
| 30 | + if err := mm.AddFact("user", "project uses postgres for all data storage"); err != nil { |
| 31 | + t.Fatalf("seed: %v", err) |
| 32 | + } |
| 33 | + before := atomic.LoadInt64(&llm.calls) |
| 34 | + |
| 35 | + // Add a very similar entry — should be classified "merge" or "judge" by RP. |
| 36 | + if err := mm.AddFact("user", "project database is postgres"); err != nil { |
| 37 | + t.Fatalf("AddFact: %v", err) |
| 38 | + } |
| 39 | + after := atomic.LoadInt64(&llm.calls) |
| 40 | + |
| 41 | + if after != before { |
| 42 | + t.Errorf("AddFact made %d LLM call(s); want 0 — AddFact must never block on LLM", |
| 43 | + after-before) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// TestAddFact_MergeUsesSimpleFallback: when merge-on-write fires, the entries |
| 48 | +// are merged using the non-LLM (simple) path. The result must contain content |
| 49 | +// from both entries OR one must be a substring of the other. |
| 50 | +func TestAddFact_MergeUsesSimpleFallback(t *testing.T) { |
| 51 | + dir := t.TempDir() |
| 52 | + cfg := DefaultMemoryConfig() |
| 53 | + cfg.MergeOnWrite = boolPtr(true) |
| 54 | + // Use a high merge threshold so the similar entries definitely trigger "merge". |
| 55 | + cfg.MergeThreshold = 0.01 // effectively always merge |
| 56 | + mm := NewMemoryManager(dir, nil, cfg) |
| 57 | + |
| 58 | + _ = mm.AddFact("env", "go 1.22") |
| 59 | + _ = mm.AddFact("env", "golang 1.22") |
| 60 | + |
| 61 | + _, env, err := mm.ReadFacts() |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("ReadFacts: %v", err) |
| 64 | + } |
| 65 | + // The simple merge either returns the longer entry (substring case) or |
| 66 | + // concatenates them. Either way the result is non-empty. |
| 67 | + if strings.TrimSpace(env) == "" { |
| 68 | + t.Errorf("expected a merged entry in env, got empty") |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// TestConsolidateOnEnd_Default: the default config has consolidate_on_end=true. |
| 73 | +func TestConsolidateOnEnd_Default(t *testing.T) { |
| 74 | + d := DefaultMemoryConfig() |
| 75 | + if d.ConsolidateOnEnd == nil || !*d.ConsolidateOnEnd { |
| 76 | + t.Errorf("ConsolidateOnEnd default should be true, got %v", d.ConsolidateOnEnd) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// TestConsolidateOnEnd_FiresAtSessionEnd: with consolidate_on_end=true, facts |
| 81 | +// are consolidated in the background at session end. We verify this by seeding |
| 82 | +// three distinct facts and confirming the count decreases (Consolidate merged |
| 83 | +// them) within a generous timeout. |
| 84 | +func TestConsolidateOnEnd_FiresAtSessionEnd(t *testing.T) { |
| 85 | + dir := t.TempDir() |
| 86 | + // LLM that: |
| 87 | + // - returns "session summary" for the episode extraction call |
| 88 | + // - returns a 2-element JSON array for any consolidation call |
| 89 | + llm := &mockLLM{responses: map[string]string{ |
| 90 | + "Summarize": "session summary", |
| 91 | + // Consolidate prompt contains "memory entries" — return a merged 2-item list |
| 92 | + "memory entri": `["dark mode + vim keybindings preference","works in Go for backend"]`, |
| 93 | + }} |
| 94 | + |
| 95 | + cfg := DefaultMemoryConfig() |
| 96 | + cfg.ConsolidateOnEnd = boolPtr(true) |
| 97 | + cfg.LLMConsolidate = boolPtr(true) |
| 98 | + cfg.ExtractOnEnd = boolPtr(true) |
| 99 | + cfg.MergeOnWrite = boolPtr(false) // keep all three facts separate |
| 100 | + mm := NewMemoryManager(dir, llm, cfg) |
| 101 | + |
| 102 | + // Seed three facts that will survive without merge-on-write. |
| 103 | + _ = mm.AddFact("user", "prefers dark mode in all editors") |
| 104 | + _ = mm.AddFact("user", "uses vim keybindings everywhere") |
| 105 | + _ = mm.AddFact("user", "works primarily in Go for backend services") |
| 106 | + |
| 107 | + entries0, _ := mm.facts.Entries("user") |
| 108 | + if len(entries0) != 3 { |
| 109 | + t.Fatalf("expected 3 seeded entries, got %d", len(entries0)) |
| 110 | + } |
| 111 | + |
| 112 | + msgs := []string{"user: hi", "assistant: ok", "user: more", "assistant: done"} |
| 113 | + mm.OnSessionEndWithProvenance("20260801-a", 5, msgs, EpisodeProvenance{}) |
| 114 | + |
| 115 | + // Poll until consolidation reduces the entry count or timeout. |
| 116 | + deadline := time.Now().Add(3 * time.Second) |
| 117 | + for time.Now().Before(deadline) { |
| 118 | + entries, _ := mm.facts.Entries("user") |
| 119 | + if len(entries) < 3 { |
| 120 | + return // consolidation ran and reduced entries — success |
| 121 | + } |
| 122 | + time.Sleep(50 * time.Millisecond) |
| 123 | + } |
| 124 | + t.Error("session-end consolidation did not reduce fact count within 3 seconds") |
| 125 | +} |
| 126 | + |
| 127 | +// TestConsolidateOnEnd_FlagOff: with consolidate_on_end=false, fact count must |
| 128 | +// remain stable at session end (no consolidation LLM call). |
| 129 | +func TestConsolidateOnEnd_FlagOff(t *testing.T) { |
| 130 | + dir := t.TempDir() |
| 131 | + llm := &callCountLLM{ |
| 132 | + inner: &mockLLM{responses: map[string]string{"Summarize": "summary"}}, |
| 133 | + } |
| 134 | + cfg := DefaultMemoryConfig() |
| 135 | + cfg.ConsolidateOnEnd = boolPtr(false) |
| 136 | + cfg.MergeOnWrite = boolPtr(false) |
| 137 | + mm := NewMemoryManager(dir, llm, cfg) |
| 138 | + |
| 139 | + _ = mm.AddFact("user", "prefers dark mode") |
| 140 | + _ = mm.AddFact("user", "uses Go for backend work") |
| 141 | + |
| 142 | + msgs := []string{"user: hi", "assistant: ok", "user: more", "assistant: done"} |
| 143 | + before := atomic.LoadInt64(&llm.calls) |
| 144 | + mm.OnSessionEndWithProvenance("20260801-b", 5, msgs, EpisodeProvenance{}) |
| 145 | + |
| 146 | + // Give any goroutine 300 ms to (incorrectly) run. |
| 147 | + time.Sleep(300 * time.Millisecond) |
| 148 | + after := atomic.LoadInt64(&llm.calls) |
| 149 | + |
| 150 | + // Only the episode extraction call should have fired (Summarize), not Consolidate. |
| 151 | + episodeCalls := after - before |
| 152 | + entries, _ := mm.facts.Entries("user") |
| 153 | + if len(entries) < 2 { |
| 154 | + t.Errorf("consolidate_on_end=false must not consolidate facts, got %d entries", len(entries)) |
| 155 | + } |
| 156 | + _ = episodeCalls // 1 call for Summarize is expected |
| 157 | +} |
0 commit comments