返回 DeepSeek-Reasonix
legacy_lineage_identity_test.go
根目录 / internal / sessioncatalog / legacy_lineage_identity_test.go
1 package sessioncatalog
2
3 import (
4 "context"
5 "os"
6 "path/filepath"
7 "testing"
8
9 "reasonix/internal/agent"
10 )
11
12 func TestLegacyRecoveryIdentitySurvivesContentProofAndParentDeletion(t *testing.T) {
13 for _, metadataOnly := range []bool{false, true} {
14 t.Run(map[bool]string{false: "full", true: "metadata"}[metadataOnly], func(t *testing.T) {
15 ctx := t.Context()
16 dir := t.TempDir()
17 parent, recovery, _ := forkCatalogRecoveryBranch(t, dir, "legacy-parent")
18 coverCatalogRecoveryParent(t, parent, recovery)
19 before := map[string]string{}
20 for _, path := range []string{parent, recovery, agent.BranchMetaPath(parent), agent.BranchMetaPath(recovery)} {
21 data, err := os.ReadFile(path)
22 if err != nil {
23 t.Fatal(err)
24 }
25 before[path] = string(data)
26 }
27 opts := Options{Path: filepath.Join(t.TempDir(), "catalog.sqlite"), MetadataOnly: metadataOnly, DisableRepair: true, StartPaused: true}
28 catalog, err := Open(ctx, opts)
29 if err != nil {
30 t.Fatal(err)
31 }
32 t.Cleanup(func() {
33 if err := catalog.Close(context.Background()); err != nil {
34 t.Error(err)
35 }
36 })
37 reopen := func() {
38 t.Helper()
39 if err := catalog.Close(ctx); err != nil {
40 t.Fatal(err)
41 }
42 catalog, err = Open(ctx, opts)
43 if err != nil {
44 t.Fatal(err)
45 }
46 }
47 target := DirectoryTarget{Path: dir, Scope: "global"}
48 check := func(path, topic string, visible bool) {
49 t.Helper()
50 record, ok, err := catalog.GetSession(ctx, path)
51 if err != nil || !ok || record.TopicID != topic || record.OrdinaryVisible != visible {
52 t.Fatalf("record = %+v, ok=%v err=%v; want topic=%q visible=%v", record, ok, err, topic, visible)
53 }
54 }
55 reconcile := func() {
56 t.Helper()
57 if err := catalog.ReconcileDirectory(ctx, target); err != nil {
58 t.Fatal(err)
59 }
60 if err := catalog.ProveRecoveryLineage(ctx); err != nil {
61 t.Fatal(err)
62 }
63 }
64 reconcile()
65 check(parent, agent.LegacySessionTopicID(parent), true)
66 check(recovery, agent.LegacySessionTopicID(parent), false)
67 // An older executable may have certified this unchanged directory
68 // with the incompatible fallback. Reopen must repair that cache too.
69 oldTopic := "recovery:" + agent.BranchID(parent)
70 for _, query := range []string{
71 "UPDATE catalog_sessions SET topic_id=?,logical_topic_id=?",
72 "UPDATE catalog_topics SET topic_id=? WHERE topic_id<>?",
73 } {
74 if _, err := catalog.db.ExecContext(ctx, query, oldTopic, oldTopic); err != nil {
75 t.Fatal(err)
76 }
77 }
78 reopen()
79 reconcile()
80 check(parent, agent.LegacySessionTopicID(parent), true)
81 check(recovery, agent.LegacySessionTopicID(parent), false)
82 if err := os.Remove(parent); err != nil {
83 t.Fatal(err)
84 }
85 if err := os.Remove(agent.SessionEventLogPath(parent)); err != nil {
86 t.Fatal(err)
87 }
88 reconcile()
89 check(recovery, agent.LegacySessionTopicID(recovery), true)
90 reopen()
91 reconcile()
92 check(recovery, agent.LegacySessionTopicID(recovery), true)
93 // Reopening a rebuilt projection must converge without migrating the
94 // authoritative transcript or either legacy branch sidecar.
95 for _, path := range []string{recovery, agent.BranchMetaPath(parent), agent.BranchMetaPath(recovery)} {
96 data, err := os.ReadFile(filepath.Clean(path))
97 if err != nil || string(data) != before[path] {
98 t.Fatalf("catalog changed authority %s: %v", filepath.Base(path), err)
99 }
100 }
101 })
102 }
103 }
104
105 func TestLegacyRootlessRecoveryGroupUsesOneRepresentativeIdentity(t *testing.T) {
106 leaf := SessionRecord{Path: filepath.Join(t.TempDir(), "leaf.jsonl"), Recovered: true, ParentID: "gone", RecoveryGroupID: "gone", RecoveryCanonical: true}
107 peer := SessionRecord{Path: filepath.Join(filepath.Dir(leaf.Path), "peer.jsonl"), Recovered: true, ParentID: "gone", RecoveryGroupID: "gone"}
108 for _, input := range [][]SessionRecord{{leaf, peer}, {peer, leaf}} {
109 visible := 0
110 for _, got := range projectLogicalSessions(input) {
111 if got.TopicID != agent.LegacySessionTopicID(leaf.Path) || got.LogicalTopicID != got.TopicID {
112 t.Fatalf("rootless group identity = %+v", got)
113 }
114 if got.OrdinaryVisible {
115 visible++
116 if got.Path != leaf.Path {
117 t.Fatalf("visible recovery = %q, want canonical leaf", got.Path)
118 }
119 }
120 }
121 if visible != 1 {
122 t.Fatalf("visible recovery rows = %d, want 1", visible)
123 }
124 }
125 }
126
127 func TestExplicitRecoveryPrefixedTopicIdentityIsPreserved(t *testing.T) {
128 rootPath := filepath.Join(t.TempDir(), "root.jsonl")
129 group := agent.BranchID(rootPath)
130 oldTopic := "recovery:" + group
131 root := SessionRecord{Path: rootPath}
132 leaf := SessionRecord{Path: filepath.Join(filepath.Dir(rootPath), "leaf.jsonl"), Recovered: true, ParentID: group, RecoveryGroupID: group, RecoveryCanonical: true}
133 for _, placement := range []string{"root", "member", "rootless"} {
134 t.Run(placement, func(t *testing.T) {
135 parent, recovery := root, leaf
136 if placement == "root" {
137 parent.TopicID, parent.TopicTitle = oldTopic, "Persisted title"
138 } else {
139 recovery.TopicID, recovery.TopicTitle = oldTopic, "Persisted title"
140 }
141 records := []SessionRecord{parent, recovery}
142 if placement == "rootless" {
143 records = records[1:]
144 }
145 for _, got := range projectLogicalSessions(records) {
146 if got.TopicID != oldTopic || got.LogicalTopicID != oldTopic || got.TopicTitle != "Persisted title" {
147 t.Fatalf("explicit recovery-prefixed authority changed: %+v", got)
148 }
149 }
150 })
151 }
152 }
153
153 lines GO