| 1 | package sessioncatalog |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/agent" |
| 9 | "reasonix/internal/provider" |
| 10 | ) |
| 11 | |
| 12 | func recoveryHeavyDirectory(t *testing.T, branches int) (string, DirectoryTarget) { |
| 13 | t.Helper() |
| 14 | dir := t.TempDir() |
| 15 | root := filepath.Join(dir, "root.jsonl") |
| 16 | saveLineageSession(t, root, "question", "answer", "continued", "done") |
| 17 | for range branches { |
| 18 | session := agent.NewSession("sys") |
| 19 | session.Add(provider.Message{Role: provider.RoleUser, Content: "question"}) |
| 20 | session.Add(provider.Message{Role: provider.RoleAssistant, Content: "answer"}) |
| 21 | if _, err := session.SaveConflictRecoveryBranch(agent.RecoveryBranchOptions{OriginalPath: root}); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | } |
| 25 | return dir, DirectoryTarget{Path: dir, Scope: "global"} |
| 26 | } |
| 27 | |
| 28 | func TestRecoveryDirectoryPreferredBranchLoadsOnceThenIdleLoadsNone(t *testing.T) { |
| 29 | ctx := context.Background() |
| 30 | dir, target := recoveryHeavyDirectory(t, 8) |
| 31 | ordered, err := agent.ListSessionOrder(dir) |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | paths := make([]string, 0, len(ordered)) |
| 36 | preferred := "" |
| 37 | for _, info := range ordered { |
| 38 | paths = append(paths, info.Path) |
| 39 | if info.Recovered && preferred == "" { |
| 40 | preferred = info.Path |
| 41 | } |
| 42 | } |
| 43 | if preferred == "" { |
| 44 | t.Fatal("recovery-heavy fixture did not create a recovery branch") |
| 45 | } |
| 46 | if err := agent.SetRecoveryPreferred(paths, preferred); err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | catalog, err := Open(ctx, Options{Path: filepath.Join(t.TempDir(), "catalog.sqlite"), DisableRepair: true}) |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | t.Cleanup(func() { _ = catalog.Close(context.Background()) }) |
| 54 | loads := map[string]int{} |
| 55 | catalog.testSessionContentLoadHook = func(path string) { loads[catalog.pathKey(path)]++ } |
| 56 | if err := catalog.ReconcileDirectory(ctx, target); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | if len(loads) != 9 { |
| 60 | t.Fatalf("loaded transcripts = %d, want root + 8 branches", len(loads)) |
| 61 | } |
| 62 | for path, count := range loads { |
| 63 | if count != 1 { |
| 64 | t.Fatalf("%s loaded %d times in one wave", path, count) |
| 65 | } |
| 66 | } |
| 67 | loads = map[string]int{} |
| 68 | if err := catalog.ReconcileDirectory(ctx, DirectoryTarget{Path: dir, Scope: "global"}); err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | if len(loads) != 0 { |
| 72 | t.Fatalf("unchanged verified reconcile decoded %d transcripts", len(loads)) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func BenchmarkRecoveryDirectoryVerifiedIdleReconcile(b *testing.B) { |
| 77 | ctx := context.Background() |
| 78 | dir := b.TempDir() |
| 79 | root := filepath.Join(dir, "root.jsonl") |
| 80 | s := agent.NewSession("sys") |
| 81 | s.Add(provider.Message{Role: provider.RoleUser, Content: "question"}) |
| 82 | s.Add(provider.Message{Role: provider.RoleAssistant, Content: "answer"}) |
| 83 | if err := s.Save(root); err != nil { |
| 84 | b.Fatal(err) |
| 85 | } |
| 86 | for range 128 { |
| 87 | branch := agent.NewSession("sys") |
| 88 | branch.Add(provider.Message{Role: provider.RoleUser, Content: "question"}) |
| 89 | if _, err := branch.SaveConflictRecoveryBranch(agent.RecoveryBranchOptions{OriginalPath: root}); err != nil { |
| 90 | b.Fatal(err) |
| 91 | } |
| 92 | } |
| 93 | target := DirectoryTarget{Path: dir, Scope: "global"} |
| 94 | catalog, err := Open(ctx, Options{Path: filepath.Join(b.TempDir(), "catalog.sqlite"), DisableRepair: true}) |
| 95 | if err != nil { |
| 96 | b.Fatal(err) |
| 97 | } |
| 98 | b.Cleanup(func() { _ = catalog.Close(context.Background()) }) |
| 99 | if err := catalog.ReconcileDirectory(ctx, target); err != nil { |
| 100 | b.Fatal(err) |
| 101 | } |
| 102 | b.ResetTimer() |
| 103 | for range b.N { |
| 104 | if err := catalog.ReconcileDirectory(ctx, target); err != nil { |
| 105 | b.Fatal(err) |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 |