| 1 | package sessioncatalog |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "path/filepath" |
| 6 | "slices" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/agent" |
| 10 | "reasonix/internal/provider" |
| 11 | ) |
| 12 | |
| 13 | func saveRecoveryCopies(t *testing.T, dir string, copies int) string { |
| 14 | t.Helper() |
| 15 | root := filepath.Join(dir, "root.jsonl") |
| 16 | saveLineageSession(t, root, "q", "a", "next", "answer") |
| 17 | for range copies { |
| 18 | branch := agent.NewSession("sys") |
| 19 | branch.Add(provider.Message{Role: provider.RoleUser, Content: "q"}) |
| 20 | branch.Add(provider.Message{Role: provider.RoleAssistant, Content: "a"}) |
| 21 | if _, err := branch.SaveConflictRecoveryBranch(agent.RecoveryBranchOptions{OriginalPath: root}); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | } |
| 25 | return root |
| 26 | } |
| 27 | |
| 28 | func openMetadataCatalog(t *testing.T) *Catalog { |
| 29 | t.Helper() |
| 30 | catalog, err := Open(t.Context(), Options{Path: filepath.Join(t.TempDir(), "v10.sqlite"), MetadataOnly: true}) |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | t.Cleanup(func() { _ = catalog.Close(context.Background()) }) |
| 35 | return catalog |
| 36 | } |
| 37 | |
| 38 | func ordinaryPaths(t *testing.T, catalog *Catalog, dir string) []string { |
| 39 | t.Helper() |
| 40 | rows, err := catalog.ListOrdinarySessions(t.Context(), OrdinaryPageRequest{Scope: "global", WorkspaceRoot: dir, Limit: 50}) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | paths := make([]string, 0, len(rows)) |
| 45 | for _, row := range rows { |
| 46 | paths = append(paths, row.Path) |
| 47 | } |
| 48 | return paths |
| 49 | } |
| 50 | |
| 51 | func TestProveRecoveryLineageHidesCopiesAMetadataScanListed(t *testing.T) { |
| 52 | dir := t.TempDir() |
| 53 | root := saveRecoveryCopies(t, dir, 3) |
| 54 | catalog := openMetadataCatalog(t) |
| 55 | loads := 0 |
| 56 | catalog.testSessionContentLoadHook = func(string) { loads++ } |
| 57 | if err := catalog.ReconcileDirectory(t.Context(), DirectoryTarget{Path: dir, Scope: "global"}); err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | if got := ordinaryPaths(t, catalog, dir); len(got) != 4 || loads != 0 { |
| 61 | t.Fatalf("before the proof: %d rows, %d content loads; discovery alone lists every copy and reads nothing", len(got), loads) |
| 62 | } |
| 63 | if err := catalog.ProveRecoveryLineage(t.Context()); err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | if got := ordinaryPaths(t, catalog, dir); len(got) != 1 || got[0] != root { |
| 67 | t.Fatalf("after the proof: %v, want only %s", got, filepath.Base(root)) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestProveRecoveryLineageMatchesTheFullReconcile(t *testing.T) { |
| 72 | dir := t.TempDir() |
| 73 | saveRecoveryCopies(t, dir, 2) |
| 74 | diverged := agent.NewSession("sys") |
| 75 | for i, message := range []string{"q", "a", "something else", "other answer"} { |
| 76 | role := provider.RoleUser |
| 77 | if i%2 == 1 { |
| 78 | role = provider.RoleAssistant |
| 79 | } |
| 80 | diverged.Add(provider.Message{Role: role, Content: message}) |
| 81 | } |
| 82 | if _, err := diverged.SaveConflictRecoveryBranch(agent.RecoveryBranchOptions{OriginalPath: filepath.Join(dir, "root.jsonl")}); err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
| 85 | saveLineageSession(t, filepath.Join(dir, "plain.jsonl"), "x", "y") |
| 86 | |
| 87 | full, err := Open(t.Context(), Options{InMemory: true, DisableRepair: true}) |
| 88 | if err != nil { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | t.Cleanup(func() { _ = full.Close(context.Background()) }) |
| 92 | if err := full.ReconcileDirectory(t.Context(), DirectoryTarget{Path: dir, Scope: "global"}); err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | want := ordinaryPaths(t, full, dir) |
| 96 | |
| 97 | catalog := openMetadataCatalog(t) |
| 98 | if err := catalog.ReconcileDirectory(t.Context(), DirectoryTarget{Path: dir, Scope: "global"}); err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | if err := catalog.ProveRecoveryLineage(t.Context()); err != nil { |
| 102 | t.Fatal(err) |
| 103 | } |
| 104 | got := ordinaryPaths(t, catalog, dir) |
| 105 | slices.Sort(want) |
| 106 | slices.Sort(got) |
| 107 | if !slices.Equal(got, want) { |
| 108 | t.Fatalf("visible after the proof = %v, full reconcile = %v", got, want) |
| 109 | } |
| 110 | if len(want) >= 5 { |
| 111 | t.Fatalf("fixture hid nothing: %v", want) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func TestProveRecoveryLineageLeavesPlainDirectoriesAlone(t *testing.T) { |
| 116 | dir := t.TempDir() |
| 117 | saveLineageSession(t, filepath.Join(dir, "a.jsonl"), "q", "a") |
| 118 | saveLineageSession(t, filepath.Join(dir, "b.jsonl"), "q", "a") |
| 119 | catalog := openMetadataCatalog(t) |
| 120 | if err := catalog.ReconcileDirectory(t.Context(), DirectoryTarget{Path: dir, Scope: "global"}); err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | loads := 0 |
| 124 | catalog.testSessionContentLoadHook = func(string) { loads++ } |
| 125 | if err := catalog.ProveRecoveryLineage(t.Context()); err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | if loads != 0 || len(ordinaryPaths(t, catalog, dir)) != 2 { |
| 129 | t.Fatalf("a directory without recovery copies was read (%d loads) or changed", loads) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | func TestProveRecoveryLineageSurvivesARescan(t *testing.T) { |
| 134 | dir := t.TempDir() |
| 135 | root := saveRecoveryCopies(t, dir, 2) |
| 136 | catalog := openMetadataCatalog(t) |
| 137 | if err := catalog.ReconcileDirectory(t.Context(), DirectoryTarget{Path: dir, Scope: "global"}); err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | if err := catalog.ProveRecoveryLineage(t.Context()); err != nil { |
| 141 | t.Fatal(err) |
| 142 | } |
| 143 | if err := catalog.ReconcileDirectory(t.Context(), DirectoryTarget{Path: dir, Scope: "global"}); err != nil { |
| 144 | t.Fatal(err) |
| 145 | } |
| 146 | if got := ordinaryPaths(t, catalog, dir); len(got) != 1 || got[0] != root { |
| 147 | t.Fatalf("a metadata rescan undid the proof: %v", got) |
| 148 | } |
| 149 | } |
| 150 |