13 "github.com/stretchr/testify/require"
16 // syntheticMessages is the length of the synthetic log.
17 const syntheticMessages = 100000
19 // TestReplaySyntheticLog runs a synthetic log through the memory core with the
20 // fake summarizer. After each message it waits for the builds, as a turn does.
21 // It checks that each view stays within its sawtooth, that no view line holds a
22 // whole long message or a part of an unbuilt one, and that a restart in the
23 // middle and at the end loads the same views byte for byte.
24 func TestReplaySyntheticLog(t *testing.T) {
27 clock := newTestClock()
28 random := rand.New(rand.NewPCG(1, 2))
29 memory, err := openMemory(t.Context(), root, clock.Now, newFakeSummarizer())
30 require.NoError(t, err)
33 require.NoError(t, memory.Close())
38 long := make([]bool, syntheticMessages)
39 for id := range syntheticMessages {
40 if id == syntheticMessages/2 {
41 memory = requireRestart(t, memory, root, clock)
44 clock.now = clock.now.Add(time.Minute)
45 kind, text := syntheticMessage(random, id)
46 long[id] = len(kind)+len(": ")+len(text) > nodeBytes
47 lines := viewLines(memory)
48 _, err = memory.Append(kind, text)
49 require.NoError(t, err)
51 size, merged := requireViews(t, memory, lines, long, id%1000 == 0)
57 require.NoError(t, memory.Wait(t.Context()))
60 memory = requireRestart(t, memory, root, clock)
61 require.Equal(t, syntheticMessages, memory.log.Len())
62 require.Greater(t, batches, 0)
64 require.NoError(t, memory.Wait(t.Context()))
65 t.Logf("%d messages, %d nodes, %d batches in %s", syntheticMessages, memory.tree.Count(), batches, time.Since(start).Round(time.Second))
66 t.Logf("chat view: %d lines, %d bytes, lowest after a batch %d bytes", len(memory.chat.lines), memory.chat.Size(), low)
67 t.Logf("compaction view: %d lines, %d bytes", len(memory.cview.lines), memory.cview.Size())
71 var stats runtime.MemStats
72 runtime.ReadMemStats(&stats)
73 t.Logf("heap in use: %d KB", stats.HeapInuse/1024)
76 // syntheticMessage returns the kind and the text of message id. Most messages
77 // are tool calls and their output. Users write short lines. One output in a
78 // hundred is over 30 KB.
79 func syntheticMessage(random *rand.Rand, id int) (string, string) {
80 roll := random.IntN(100)
83 return "user", syntheticText(id, 10+random.IntN(200))
85 return "reply", syntheticText(id, 100+random.IntN(1500))
87 return "note", syntheticText(id, 50+random.IntN(600))
89 return "work", syntheticText(id, 300+random.IntN(3000))
91 return "tool", syntheticText(id, 60+random.IntN(400))
93 return "echo", syntheticText(id, 30000+random.IntN(30000))
95 return "echo", syntheticText(id, 20+random.IntN(4000))
99 // syntheticText returns a text of about size bytes that starts with its message
100 // ID and holds some non-ASCII characters.
101 func syntheticText(id, size int) string {
102 head := fmt.Sprintf("m%d ", id)
103 words := "ação data line café "
105 return head + strings.Repeat(words, max(1, (size-len(head))/len(words)))
108 // requireViews checks the views after a new message, and with full compares
109 // them with the tree. Each view is at most its high limit, and a batch merges
110 // the chat view to its low limit. The leaf of the new message can be built
111 // after the append, so the sizes count it as the pending mark, as the append
112 // did. It returns the size of the chat view and if a batch merged it, from the
113 // number of lines before the message.
114 func requireViews(t *testing.T, memory *Memory, lines int, long []bool, full bool) (int, bool) {
118 defer memory.mutex.Unlock()
120 size := appendSize(memory.chat)
121 require.LessOrEqual(t, size, chatHigh)
122 require.LessOrEqual(t, appendSize(memory.cview), compactionHigh)
124 merged := len(memory.chat.lines) < lines
126 require.LessOrEqual(t, size, chatLow)
129 requireOnlySummaries(t, memory, long, full)
134 // requireOnlySummaries checks each line of the chat view as the model reads it.
135 // A built line holds its node, a long message has a summary of the fake
136 // summarizer and not its text, and an unbuilt line holds only the pending mark.
137 // With full, it also checks that the view keeps the node of each built line.
138 // The caller holds the lock of memory.
139 func requireOnlySummaries(t *testing.T, memory *Memory, long []bool, full bool) {
142 rendered := strings.Split(memory.chat.Render(), "\n")
143 require.Len(t, rendered, len(memory.chat.lines)+2)
145 for n, line := range memory.chat.lines {
146 head := fmt.Sprintf("%d+%d|", line.index<<line.level, 1<<line.level)
147 node, built := memory.chat.nodes[line]
149 require.Equal(t, isBuilt(t, memory.tree, line.level, line.index), built)
153 require.Equal(t, 0, line.level)
154 require.Equal(t, head+pending, rendered[n+1])
159 require.Equal(t, head+strings.ReplaceAll(node.Text, "\n", " "), rendered[n+1])
160 if line.level == 0 && long[line.index] {
161 require.True(t, strings.HasPrefix(node.Text, "summary "), "message %d", line.index)
166 // appendSize returns the size of view with its last line as the pending mark.
167 func appendSize(view *View) int {
168 last := view.lines[len(view.lines)-1]
169 return view.Size() - view.lineSize(last) + len(pending)
172 // viewLines returns the number of lines in the chat view of memory.
173 func viewLines(memory *Memory) int {
175 defer memory.mutex.Unlock()
177 return len(memory.chat.lines)
180 // requireRestart closes memory, opens it again, and checks that the views and
181 // view.json stay the same byte for byte.
182 func requireRestart(t *testing.T, memory *Memory, root string, clock *testClock) *Memory {
185 require.NoError(t, memory.Wait(t.Context()))
187 view := memory.View()
188 context := memory.Context(0, memory.log.Len())
189 saved, err := os.ReadFile(filepath.Join(root, "view.json"))
190 require.NoError(t, err)
191 require.NoError(t, memory.Close())
193 memory, err = openMemory(t.Context(), root, clock.Now, newFakeSummarizer())
194 require.NoError(t, err)
195 require.NoError(t, memory.Wait(t.Context()))
196 require.Equal(t, view, memory.View())
197 require.Equal(t, context, memory.Context(0, memory.log.Len()))
199 loaded, err := os.ReadFile(filepath.Join(root, "view.json"))
200 require.NoError(t, err)
201 require.Equal(t, saved, loaded)