| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "reasonix/internal/agent" |
| 13 | "reasonix/internal/config" |
| 14 | "reasonix/internal/provider" |
| 15 | "reasonix/internal/session" |
| 16 | "reasonix/internal/store" |
| 17 | ) |
| 18 | |
| 19 | func TestCompatibilityColdContentUsesReadOnlyNativePreparation(t *testing.T) { |
| 20 | for _, kind := range []string{"checkpoint", "schema1"} { |
| 21 | t.Run(kind, func(t *testing.T) { |
| 22 | a := historySliceTestApp(t) |
| 23 | t.Cleanup(a.closeHistoryReaders) |
| 24 | tab := newColdHistoryTab(t, a) |
| 25 | // Production global tabs retain the current workspace root even |
| 26 | // when opening a source in the pre-workspace global directory. |
| 27 | tab.WorkspaceRoot = globalWorkspaceRoot() |
| 28 | dir := config.SessionDir() |
| 29 | if err := os.MkdirAll(dir, 0700); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | tab.SessionPath = filepath.Join(dir, kind+".jsonl") |
| 33 | answer := strings.Repeat("完整正文🧭", 25000) |
| 34 | messages := []provider.Message{historySliceUser(0, "question"), historySliceAssistant(0, answer)} |
| 35 | var checkpoint []byte |
| 36 | for _, message := range messages { |
| 37 | body, _ := json.Marshal(message) |
| 38 | checkpoint = append(checkpoint, append(body, '\n')...) |
| 39 | } |
| 40 | var event []byte |
| 41 | if kind == "schema1" { |
| 42 | checkpoint = []byte("{\"role\":\"user\",\"content\":\"obsolete\"}\n") |
| 43 | event, _ = json.Marshal(map[string]any{"schema_version": 1, "type": "replace", "messages": messages}) |
| 44 | if err := os.WriteFile(store.SessionEventLog(tab.SessionPath), event, 0600); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | } |
| 48 | if err := os.WriteFile(tab.SessionPath, checkpoint, 0600); err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | page := a.HistorySliceForTab(tab.ID, HistorySliceRequest{Entries: 2}) |
| 52 | if page.Error != "" || len(page.Entries) != 2 || len(page.Entries[1].Refs) == 0 { |
| 53 | t.Fatalf("compatibility slice: %+v", page) |
| 54 | } |
| 55 | ref := page.Entries[1].Refs[0] |
| 56 | if ref.ReadHandleID != "" { |
| 57 | t.Fatal("fixture did not exercise the unbound compatibility RPC") |
| 58 | } |
| 59 | var full strings.Builder |
| 60 | for i := 0; ; i++ { |
| 61 | chunk := a.HistoryContentForTab(tab.ID, ref, i) |
| 62 | if chunk.Stale { |
| 63 | t.Fatalf("compatibility content became stale at %d", i) |
| 64 | } |
| 65 | full.WriteString(chunk.Data) |
| 66 | if chunk.Done { |
| 67 | break |
| 68 | } |
| 69 | } |
| 70 | if full.String() != answer { |
| 71 | t.Fatal("compatibility content was truncated or used an obsolete checkpoint") |
| 72 | } |
| 73 | chunk, err := a.HistoryContentForTarget(SessionSelector{SessionPath: tab.SessionPath}, ref, 0) |
| 74 | if err != nil || chunk.Stale || chunk.Data == "" || !strings.HasPrefix(answer, chunk.Data) { |
| 75 | t.Fatalf("explicit compatibility target: %+v %v", chunk, err) |
| 76 | } |
| 77 | after, _ := os.ReadFile(tab.SessionPath) |
| 78 | afterEvent, _ := os.ReadFile(store.SessionEventLog(tab.SessionPath)) |
| 79 | if string(after) != string(checkpoint) || string(afterEvent) != string(event) { |
| 80 | t.Fatal("unbound content repaired authoritative storage") |
| 81 | } |
| 82 | if _, err := os.Stat(store.SessionDisplayIndex(tab.SessionPath)); !os.IsNotExist(err) { |
| 83 | t.Fatal("unbound content wrote a compatibility display sidecar") |
| 84 | } |
| 85 | }) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestCompatibilityNativeBorrowCancellationKeepsOtherReader(t *testing.T) { |
| 90 | a := historySliceTestApp(t) |
| 91 | t.Cleanup(a.closeHistoryReaders) |
| 92 | tab := newColdHistoryTab(t, a) |
| 93 | dir := tabSessionDir(tab) |
| 94 | if err := os.MkdirAll(dir, 0700); err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | tab.SessionPath = filepath.Join(dir, "shared.jsonl") |
| 98 | if err := os.WriteFile(tab.SessionPath, []byte("{\"role\":\"user\",\"content\":\"question\"}\n"), 0600); err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | handle, err := a.BeginSessionHistoryReadForTab(tab.ID) |
| 102 | if err != nil { |
| 103 | t.Fatal(err) |
| 104 | } |
| 105 | defer a.ReleaseSessionHistoryRead(handle.ID) |
| 106 | ctx, cancel := context.WithCancel(t.Context()) |
| 107 | defer cancel() |
| 108 | err = a.withNativeHistoryPager(ctx, tab.SessionPath, "", func(ctx context.Context, pager *agent.DisplayPager, _ string) error { |
| 109 | cancel() |
| 110 | _, err := pager.Entries(0, 1) |
| 111 | return err |
| 112 | }) |
| 113 | if !errors.Is(err, context.Canceled) { |
| 114 | t.Fatalf("cancelled compatibility reader continued: %v", err) |
| 115 | } |
| 116 | page, err := a.ReadSessionHistorySlice(handle.ID, HistorySliceRequest{Entries: 1}) |
| 117 | if err != nil || page.Status != "ready" || len(page.Page.Entries) != 1 { |
| 118 | t.Fatalf("borrower cancelled its peer: %+v %v", page, err) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | func TestNativeHistorySchemaOneColdContentStaysOnBinding(t *testing.T) { |
| 123 | a := historySliceTestApp(t) |
| 124 | t.Cleanup(a.closeHistoryReaders) |
| 125 | tab := newColdHistoryTab(t, a) |
| 126 | dir := tabSessionDir(tab) |
| 127 | if err := os.MkdirAll(dir, 0700); err != nil { |
| 128 | t.Fatal(err) |
| 129 | } |
| 130 | tab.SessionPath = filepath.Join(dir, "schema1.jsonl") |
| 131 | checkpoint := []byte("{\"role\":\"user\",\"content\":\"obsolete checkpoint\"}\n") |
| 132 | if err := os.WriteFile(tab.SessionPath, checkpoint, 0600); err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | answer := strings.Repeat("原始内容🧭", 60000) |
| 136 | event, err := json.Marshal(map[string]any{"schema_version": 1, "type": "replace", "messages": []provider.Message{historySliceUser(0, "event question"), historySliceAssistant(0, answer)}}) |
| 137 | if err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | if err := os.WriteFile(store.SessionEventLog(tab.SessionPath), event, 0600); err != nil { |
| 141 | t.Fatal(err) |
| 142 | } |
| 143 | handle, err := a.BeginSessionHistoryReadForTab(tab.ID) |
| 144 | if err != nil { |
| 145 | t.Fatal(err) |
| 146 | } |
| 147 | page, err := a.ReadSessionHistorySlice(handle.ID, HistorySliceRequest{Entries: 2}) |
| 148 | if err != nil || page.Status != "ready" || page.Page.Source != "event-log" || len(page.Page.Entries) != 2 { |
| 149 | t.Fatalf("cold event slice: %+v %v", page, err) |
| 150 | } |
| 151 | outline, err := a.ReadSessionHistoryOutline(handle.ID, session.HistoryOutlineRequest{}) |
| 152 | if err != nil || outline.Status != "ready" || len(outline.Entries) != 1 || outline.Entries[0].Prompt != "event question" { |
| 153 | t.Fatalf("cold event outline: %+v %v", outline, err) |
| 154 | } |
| 155 | var ref HistoryContentRef |
| 156 | for _, entry := range page.Page.Entries { |
| 157 | for _, candidate := range entry.Refs { |
| 158 | if candidate.Field == "content" { |
| 159 | ref = candidate |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | if ref.ReadHandleID != handle.ID { |
| 164 | t.Fatalf("content lost its read owner: %+v", ref) |
| 165 | } |
| 166 | var full strings.Builder |
| 167 | for i := 0; ; i++ { |
| 168 | chunk := a.HistoryContentForTab(tab.ID, ref, i) |
| 169 | if chunk.Stale { |
| 170 | t.Fatalf("bound content went stale at chunk %d", i) |
| 171 | } |
| 172 | full.WriteString(chunk.Data) |
| 173 | if chunk.Done { |
| 174 | break |
| 175 | } |
| 176 | } |
| 177 | if full.String() != answer { |
| 178 | t.Fatal("cold event content was truncated or read from the obsolete checkpoint") |
| 179 | } |
| 180 | if chunk := a.HistoryContentForTab("other-tab", ref, 0); !chunk.Stale || chunk.Data != "" { |
| 181 | t.Fatal("another navigation accepted this content ref") |
| 182 | } |
| 183 | if chunk, err := a.HistoryContentForTarget(SessionSelector{}, ref, 0); err != nil || !chunk.Stale { |
| 184 | t.Fatalf("bound ref fell back to a management target: %+v %v", chunk, err) |
| 185 | } |
| 186 | a.ReleaseSessionHistoryRead(handle.ID) |
| 187 | // Even reopening the same physical source does not rebind an old ref. |
| 188 | next, err := a.BeginSessionHistoryReadForTab(tab.ID) |
| 189 | if err != nil { |
| 190 | t.Fatal(err) |
| 191 | } |
| 192 | defer a.ReleaseSessionHistoryRead(next.ID) |
| 193 | if chunk := a.HistoryContentForTab(tab.ID, ref, 0); !chunk.Stale || chunk.Data != "" { |
| 194 | t.Fatal("released content ref adopted the successor's reader") |
| 195 | } |
| 196 | after, _ := os.ReadFile(tab.SessionPath) |
| 197 | afterEvent, _ := os.ReadFile(store.SessionEventLog(tab.SessionPath)) |
| 198 | if string(after) != string(checkpoint) || string(afterEvent) != string(event) || tab.Ctrl != nil { |
| 199 | t.Fatal("cold content rewrote source storage or created a controller") |
| 200 | } |
| 201 | if _, err := os.Stat(store.SessionDisplayIndex(tab.SessionPath)); !os.IsNotExist(err) { |
| 202 | t.Fatal("cold content triggered compatibility-sidecar repair") |
| 203 | } |
| 204 | } |
| 205 |