| 1 | package transcript |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/provider" |
| 9 | ) |
| 10 | |
| 11 | func TestSnapshotLargeSessionAndNestedContent(t *testing.T) { |
| 12 | body := strings.Repeat("正文", 12<<20) // 72 MiB: larger than the unpinned cut budget. |
| 13 | baseline := []Message{{RecordID: "m:u", MessageID: "u", Role: "user", Content: "question"}, |
| 14 | {RecordID: "m:a", MessageID: "a", Role: "assistant", Content: "answer", ToolCalls: []ToolCall{{ID: "call", Name: "write_file", Arguments: body}}, |
| 15 | MemoryCitations: []provider.MemoryCitation{{ID: "citation", Note: strings.Repeat("note", 20000)}}}} |
| 16 | projection, err := NewProjection(Identity{SessionID: "s", RuntimeEpoch: "epoch"}, baseline, 0) |
| 17 | if err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | snapshot, err := projection.Snapshot(PageRequest{}) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | encoded, err := json.Marshal(snapshot) |
| 25 | if err != nil || len(encoded) > 32<<10 { |
| 26 | t.Fatalf("bounded response bytes=%d error=%v", len(encoded), err) |
| 27 | } |
| 28 | var arguments *ContentRef |
| 29 | for _, ref := range snapshot.Records[1].Refs { |
| 30 | if strings.Join(ref.Path, "/") == "toolCalls/0/arguments" { |
| 31 | arguments = &ref |
| 32 | } |
| 33 | chunk, err := projection.Content(ContentRequest{ContentRef: ref}) |
| 34 | if err != nil || len(chunk.Data) > contentChunkBytes || chunk.NextOffset != len(chunk.Data) { |
| 35 | t.Fatalf("nested ref %v: %+v %v", ref.Path, chunk, err) |
| 36 | } |
| 37 | } |
| 38 | if arguments == nil || arguments.Bytes != len(body) { |
| 39 | t.Fatalf("missing exact argument ref: %+v", snapshot.Records[1].Refs) |
| 40 | } |
| 41 | end := len(body) - 6 |
| 42 | chunk, err := projection.Content(ContentRequest{ContentRef: *arguments, Offset: end}) |
| 43 | if err != nil || !chunk.Done || chunk.Data != body[end:] || chunk.NextOffset != len(body) { |
| 44 | t.Fatalf("tail chunk %+v %v", chunk, err) |
| 45 | } |
| 46 | if len(projection.snapshots) != 1 { |
| 47 | t.Fatalf("large current cut must remain available: %d", len(projection.snapshots)) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func BenchmarkSnapshotLargeContentChunk(b *testing.B) { |
| 52 | body := strings.Repeat("x", 8<<20) |
| 53 | projection, err := NewProjection(Identity{SessionID: "s"}, []Message{{RecordID: "m:a", Role: "assistant", MessageID: "a", Content: body}}, 0) |
| 54 | if err != nil { |
| 55 | b.Fatal(err) |
| 56 | } |
| 57 | snapshot, err := projection.Snapshot(PageRequest{}) |
| 58 | if err != nil { |
| 59 | b.Fatal(err) |
| 60 | } |
| 61 | request := ContentRequest{ContentRef: snapshot.Records[0].Refs[0], Offset: 1234} |
| 62 | b.ReportAllocs() |
| 63 | b.ResetTimer() |
| 64 | for range b.N { |
| 65 | chunk, err := projection.Content(request) |
| 66 | if err != nil || len(chunk.Data) != contentChunkBytes { |
| 67 | b.Fatal(err) |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 |