| 1 | package memory |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestResolveActivationLegacyDefaults(t *testing.T) { |
| 9 | legacyGlobalUser := Memory{Scope: FactScopeGlobal, Type: TypeUser} |
| 10 | if ResolveActivation(legacyGlobalUser) != ActivationPinned { |
| 11 | t.Fatal("legacy global user facts must stay pinned for compatibility") |
| 12 | } |
| 13 | if ResolveActivation(Memory{Scope: FactScopeGlobal, Type: TypeReference}) != ActivationRelevant { |
| 14 | t.Fatal("global references were never guidance; they resolve relevant") |
| 15 | } |
| 16 | if ResolveActivation(Memory{Scope: FactScopeProject, Type: TypeFeedback}) != ActivationRelevant { |
| 17 | t.Fatal("project facts without an explicit choice resolve relevant") |
| 18 | } |
| 19 | explicit := Memory{Scope: FactScopeGlobal, Type: TypeUser, Activation: ActivationRelevant} |
| 20 | if ResolveActivation(explicit) != ActivationRelevant { |
| 21 | t.Fatal("an explicit relevant choice beats the legacy default") |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // The coupling both #7995-adjacent layers must agree on: a fact is either in |
| 26 | // the stable prefix (pinned) or in the retrieval pool (relevant) — never in |
| 27 | // both, never in neither. |
| 28 | func TestRelevantGlobalGuidanceLeavesPrefixAndEntersRecall(t *testing.T) { |
| 29 | store := recallTestStore(t) |
| 30 | recallTestWrite(t, store.GlobalDir, Memory{ |
| 31 | ID: "mem-vue", Name: "vue2-preference", Title: "Vue 2 preference", |
| 32 | Description: "Historic frontend preference", Type: TypeUser, |
| 33 | Scope: FactScopeGlobal, Activation: ActivationRelevant, |
| 34 | Body: "I mainly write Vue 2 with the options API and webpack tooling.", |
| 35 | }) |
| 36 | |
| 37 | if pinned := store.pinnedGuidanceForProject(); len(pinned) != 0 { |
| 38 | t.Fatalf("relevant global guidance must not ride the prefix: %+v", pinned) |
| 39 | } |
| 40 | result := AutoRecall(store, "帮我看看这个 Vue 2 options API webpack 配置", RecallOptions{}) |
| 41 | if len(result.Hits) != 1 || result.Hits[0].Memory.ID != "mem-vue" { |
| 42 | t.Fatalf("relevant global user facts must be auto-recallable, got %+v", result.Hits) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestPinnedProjectFactRidesPrefixWithoutSelfShadowing(t *testing.T) { |
| 47 | store := recallTestStore(t) |
| 48 | recallTestWrite(t, store.Dir, Memory{ |
| 49 | ID: "mem-proj-pin", Name: "review-checklist", Title: "Review checklist", |
| 50 | Description: "Always-on review checklist", Type: TypeProject, |
| 51 | Scope: FactScopeProject, Activation: ActivationPinned, |
| 52 | Body: "Always run the repolint before pushing.", |
| 53 | }) |
| 54 | |
| 55 | pinned := store.pinnedGuidanceForProject() |
| 56 | if len(pinned) != 1 || pinned[0].ID != "mem-proj-pin" { |
| 57 | t.Fatalf("project pinned fact must survive the project-shadow filter: %+v", pinned) |
| 58 | } |
| 59 | if hits := AutoRecall(store, "run the repolint review checklist", RecallOptions{}); len(hits.Hits) != 0 { |
| 60 | t.Fatalf("pinned bodies must not be duplicated by recall: %+v", hits.Hits) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func TestPinnedBudgetRejectsOverflowAndAllowsUpdates(t *testing.T) { |
| 65 | store := recallTestStore(t) |
| 66 | big := strings.Repeat("规", PinnedGuidanceBudgetChars-100) |
| 67 | saved, err := store.SaveWithOptions(Memory{ |
| 68 | Name: "big-pin", Description: "large pinned fact", |
| 69 | Activation: ActivationPinned, Body: big, |
| 70 | }, SaveOptions{}) |
| 71 | if err != nil { |
| 72 | t.Fatalf("within-budget pin rejected: %v", err) |
| 73 | } |
| 74 | |
| 75 | if _, err := store.SaveWithOptions(Memory{ |
| 76 | Name: "second-pin", Description: "overflowing pinned fact", |
| 77 | Activation: ActivationPinned, Body: strings.Repeat("x", 200), |
| 78 | }, SaveOptions{}); err == nil || !strings.Contains(err.Error(), "REASONIX.md") { |
| 79 | t.Fatalf("overflow must be rejected with the instructions hint, got %v", err) |
| 80 | } |
| 81 | |
| 82 | // Updating the existing pinned fact must not double-count itself. |
| 83 | if _, err := store.SaveWithOptions(Memory{ |
| 84 | ID: saved.Memory.ID, Description: "large pinned fact v2", Body: big + "改", |
| 85 | }, SaveOptions{}); err != nil { |
| 86 | t.Fatalf("self-update within budget rejected: %v", err) |
| 87 | } |
| 88 | updated, _ := store.Read(saved.Memory.ID) |
| 89 | if ResolveActivation(updated) != ActivationPinned { |
| 90 | t.Fatalf("update omitting activation must inherit pinned, got %+v", updated) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func TestUnpinPersistsExplicitRelevantForLegacyGuidance(t *testing.T) { |
| 95 | store := recallTestStore(t) |
| 96 | recallTestWrite(t, store.GlobalDir, Memory{ |
| 97 | ID: "mem-legacy", Name: "legacy-pref", Title: "Legacy preference", |
| 98 | Description: "Legacy global preference", Type: TypeUser, |
| 99 | Scope: FactScopeGlobal, Body: "Old habit body.", |
| 100 | }) |
| 101 | loaded, ok := store.Read("mem-legacy") |
| 102 | if !ok || ResolveActivation(loaded) != ActivationPinned { |
| 103 | t.Fatalf("legacy guidance should load virtually pinned, got %+v", loaded) |
| 104 | } |
| 105 | |
| 106 | loaded.Activation = ActivationRelevant |
| 107 | if _, err := store.SaveWithOptions(loaded, SaveOptions{}); err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | after, _ := store.Read("mem-legacy") |
| 111 | if ResolveActivation(after) != ActivationRelevant { |
| 112 | t.Fatalf("unpin must stick across reloads (explicit activation persisted), got %+v", after) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | func TestIndexMarksPinnedFacts(t *testing.T) { |
| 117 | store := recallTestStore(t) |
| 118 | if _, err := store.SaveWithOptions(Memory{ |
| 119 | Name: "pinned-fact", Description: "always on", |
| 120 | Activation: ActivationPinned, Body: "short", |
| 121 | }, SaveOptions{}); err != nil { |
| 122 | t.Fatal(err) |
| 123 | } |
| 124 | if _, err := store.SaveWithOptions(Memory{ |
| 125 | Name: "plain-fact", Description: "retrieval only", Body: "short", |
| 126 | }, SaveOptions{}); err != nil { |
| 127 | t.Fatal(err) |
| 128 | } |
| 129 | index := store.Index() |
| 130 | if !strings.Contains(index, "[project/project pinned]") { |
| 131 | t.Fatalf("index must mark pinned facts:\n%s", index) |
| 132 | } |
| 133 | if strings.Contains(index, "plain-fact.md) — [project/project pinned]") { |
| 134 | t.Fatalf("relevant facts must not carry the pinned marker:\n%s", index) |
| 135 | } |
| 136 | } |
| 137 |