| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestScanMemoryRecallCountsAndPointOfUse(t *testing.T) { |
| 12 | dir := t.TempDir() |
| 13 | path := filepath.Join(dir, "run.trajectory.jsonl") |
| 14 | lines := []string{ |
| 15 | `{"seq":1,"event":{"kind":"tool_result","tool":{"args":"{\"command\":\"make check-fast --tag=MEMKEY-EARLY\"}"}}}`, |
| 16 | `{"seq":2,"memory_recall":{"hits":[{"id":"a"},{"id":"b"}],"used_chars":420}}`, |
| 17 | `{"seq":3,"event":{"kind":"tool_result","tool":{"args":"{\"path\":\"answer.txt\",\"content\":\"make check-fast --tag=MEMKEY-USED\"}"}}}`, |
| 18 | `{"seq":4,"memory_recall":{"suppressed":"generic user turn"}}`, |
| 19 | `{"seq":5,"event":{"kind":"text","text":"done, MEMKEY-TEXT too"}}`, |
| 20 | } |
| 21 | if err := os.WriteFile(path, []byte(strings.Join(lines, "\n")+"\n"), 0o644); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | stats := scanMemoryRecall(path, []string{"MEMKEY-USED", "MEMKEY-TEXT", "MEMKEY-EARLY", "MEMKEY-NEVER"}, false) |
| 25 | if stats.RecallEvents != 1 || stats.RecallHits != 2 || stats.RecallChars != 420 || stats.Suppressed != 1 { |
| 26 | t.Fatalf("stats = %+v, want 1 event / 2 hits / 420 chars / 1 suppressed", stats) |
| 27 | } |
| 28 | // MEMKEY-EARLY appears only BEFORE the recall: not point-of-use evidence. |
| 29 | if stats.MarkersUsed != 2 { |
| 30 | t.Fatalf("markers used = %d, want 2 (post-recall args + answer text only)", stats.MarkersUsed) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestMemoryUtilitySectionPairsArms(t *testing.T) { |
| 35 | dir := t.TempDir() |
| 36 | on := []result{ |
| 37 | {task: task{ID: "helped"}, Passed: true, MemoryRecallEvents: 1, MemoryRecallChars: 300}, |
| 38 | {task: task{ID: "hurt"}, Passed: false, MemoryRecallEvents: 1, MemoryRecallChars: 500}, |
| 39 | {task: task{ID: "same"}, Passed: true, MemoryRecallEvents: 1, MemoryRecallChars: 100}, |
| 40 | } |
| 41 | off := []result{ |
| 42 | {task: task{ID: "helped"}, Passed: false}, |
| 43 | {task: task{ID: "hurt"}, Passed: true}, |
| 44 | {task: task{ID: "same"}, Passed: true}, |
| 45 | } |
| 46 | onPath, offPath := filepath.Join(dir, "on.json"), filepath.Join(dir, "off.json") |
| 47 | for path, rows := range map[string][]result{onPath: on, offPath: off} { |
| 48 | data, _ := json.Marshal(rows) |
| 49 | if err := os.WriteFile(path, data, 0o644); err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | } |
| 53 | section := memoryUtilitySection(offPath, onPath) // order must not matter |
| 54 | for _, want := range []string{"Memory utility", "3 paired tasks", "helpful** 1", "harmful** 1", "helped", "hurt"} { |
| 55 | if !strings.Contains(section, want) { |
| 56 | t.Fatalf("section missing %q:\n%s", want, section) |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestSeedTaskMemoryBuildsIsolatedStateRoot(t *testing.T) { |
| 62 | taskDir := t.TempDir() |
| 63 | work := t.TempDir() |
| 64 | for _, seed := range []string{"project/fact.md", "global/pref.md"} { |
| 65 | p := filepath.Join(taskDir, "memory", seed) |
| 66 | if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | if err := os.WriteFile(p, []byte("---\nname: x\ndescription: y\n---\n\nbody\n"), 0o644); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | } |
| 73 | env, err := seedTaskMemory(taskDir, work) |
| 74 | if err != nil || len(env) != 1 || !strings.HasPrefix(env[0], "REASONIX_STATE_HOME=") { |
| 75 | t.Fatalf("env = %v err = %v", env, err) |
| 76 | } |
| 77 | stateHome := strings.TrimPrefix(env[0], "REASONIX_STATE_HOME=") |
| 78 | if _, err := os.Stat(filepath.Join(stateHome, "memory", "global", "pref.md")); err != nil { |
| 79 | t.Fatalf("global seed missing: %v", err) |
| 80 | } |
| 81 | matches, _ := filepath.Glob(filepath.Join(stateHome, "projects", "*", "memory", "fact.md")) |
| 82 | if len(matches) != 1 { |
| 83 | t.Fatalf("project seed not under the work dir's slug: %v", matches) |
| 84 | } |
| 85 | |
| 86 | if env, err := seedTaskMemory(t.TempDir(), work); err != nil || env != nil { |
| 87 | t.Fatalf("task without seeds must be a no-op, got %v %v", env, err) |
| 88 | } |
| 89 | } |
| 90 |