12 "github.com/stretchr/testify/require"
15 // syntheticMessages is the length of the synthetic log.
16 const syntheticMessages = 100000
18 // TestReplaySyntheticLog runs a synthetic log through the memory core with the
19 // fake summarizer. After each message it waits for the builds, as a turn does.
20 // It checks that each view stays within its sawtooth, that no view line holds a
21 // whole long message or a part of an unbuilt one, and that a restart in the
22 // middle and at the end loads the same views byte for byte.
23 func TestReplaySyntheticLog(t *testing.T) {
26 clock := newTestClock()
27 random := rand.New(rand.NewPCG(1, 2))
28 memory, err := openMemory(t.Context(), root, clock.Now, newFakeSummarizer())
29 require.NoError(t, err)
32 require.NoError(t, memory.Close())
37 for id := range syntheticMessages {
38 if id == syntheticMessages/2 {
39 memory = requireRestart(t, memory, root, clock)
42 clock.now = clock.now.Add(time.Minute)
43 kind, text := syntheticMessage(random, id)
44 lines := viewLines(memory)
45 _, err = memory.Append(kind, text)
46 require.NoError(t, err)
48 size, merged := requireViews(t, memory, lines)
54 require.NoError(t, memory.Wait(t.Context()))
57 memory = requireRestart(t, memory, root, clock)
58 require.Equal(t, syntheticMessages, memory.log.Len())
59 require.Greater(t, batches, 0)
61 require.NoError(t, memory.Wait(t.Context()))
62 t.Logf("%d messages, %d nodes, %d batches in %s", syntheticMessages, len(memory.tree.nodes), batches, time.Since(start).Round(time.Second))
63 t.Logf("chat view: %d lines, %d bytes, lowest after a batch %d bytes", len(memory.chat.lines), memory.chat.Size(memory.tree), low)
64 t.Logf("compaction view: %d lines, %d bytes", len(memory.cview.lines), memory.cview.Size(memory.tree))
67 // syntheticMessage returns the kind and the text of message id. Most messages
68 // are tool calls and their output. Users write short lines. One output in a
69 // hundred is over 30 KB.
70 func syntheticMessage(random *rand.Rand, id int) (string, string) {
71 roll := random.IntN(100)
74 return "user", syntheticText(id, 10+random.IntN(200))
76 return "reply", syntheticText(id, 100+random.IntN(1500))
78 return "note", syntheticText(id, 50+random.IntN(600))
80 return "work", syntheticText(id, 300+random.IntN(3000))
82 return "tool", syntheticText(id, 60+random.IntN(400))
84 return "echo", syntheticText(id, 30000+random.IntN(30000))
86 return "echo", syntheticText(id, 20+random.IntN(4000))
90 // syntheticText returns a text of about size bytes that starts with its message
91 // ID and holds some non-ASCII characters.
92 func syntheticText(id, size int) string {
93 head := fmt.Sprintf("m%d ", id)
94 words := "ação data line café "
96 return head + strings.Repeat(words, max(1, (size-len(head))/len(words)))
99 // requireViews checks the views after a new message. Each view is at most its
100 // high limit, and a batch merges the chat view to its low limit. The leaf of
101 // the new message can be built after the append, so the sizes count it as the
102 // pending mark, as the append did. It returns the size of the chat view and if
103 // a batch merged it, from the number of lines before the message.
104 func requireViews(t *testing.T, memory *Memory, lines int) (int, bool) {
108 defer memory.mutex.Unlock()
110 size := appendSize(memory.chat, memory.tree)
111 require.LessOrEqual(t, size, chatHigh)
112 require.LessOrEqual(t, appendSize(memory.cview, memory.tree), compactionHigh)
114 merged := len(memory.chat.lines) < lines
116 require.LessOrEqual(t, size, chatLow)
119 requireOnlySummaries(t, memory)
124 // requireOnlySummaries checks each line of the chat view as the model reads it.
125 // A built line holds its node, a long message has a summary and not its text,
126 // and an unbuilt line holds only the pending mark. The caller holds the lock of
128 func requireOnlySummaries(t *testing.T, memory *Memory) {
131 rendered := strings.Split(memory.chat.Render(memory.tree), "\n")
132 require.Len(t, rendered, len(memory.chat.lines)+2)
134 for n, line := range memory.chat.lines {
135 head := fmt.Sprintf("%d+%d|", line.index<<line.level, 1<<line.level)
136 node, built := memory.tree.Node(line.level, line.index)
138 require.Equal(t, 0, line.level)
139 require.Equal(t, head+pending, rendered[n+1])
144 require.Equal(t, head+strings.ReplaceAll(node.Text, "\n", " "), rendered[n+1])
146 source := leafSource(memory.log.Message(line.index))
147 if len(source) > nodeBytes {
148 require.NotEqual(t, source, node.Text, "message %d", line.index)
154 // appendSize returns the size of view with its last line as the pending mark.
155 func appendSize(view *View, tree nodeReader) int {
156 last := view.lines[len(view.lines)-1]
157 return view.Size(tree) - lineSize(tree, last) + len(pending)
160 // viewLines returns the number of lines in the chat view of memory.
161 func viewLines(memory *Memory) int {
163 defer memory.mutex.Unlock()
165 return len(memory.chat.lines)
168 // requireRestart closes memory, opens it again, and checks that the views and
169 // view.json stay the same byte for byte.
170 func requireRestart(t *testing.T, memory *Memory, root string, clock *testClock) *Memory {
173 require.NoError(t, memory.Wait(t.Context()))
175 view := memory.View()
176 context := memory.Context(0, memory.log.Len())
177 saved, err := os.ReadFile(filepath.Join(root, "view.json"))
178 require.NoError(t, err)
179 require.NoError(t, memory.Close())
181 memory, err = openMemory(t.Context(), root, clock.Now, newFakeSummarizer())
182 require.NoError(t, err)
183 require.NoError(t, memory.Wait(t.Context()))
184 require.Equal(t, view, memory.View())
185 require.Equal(t, context, memory.Context(0, memory.log.Len()))
187 loaded, err := os.ReadFile(filepath.Join(root, "view.json"))
188 require.NoError(t, err)
189 require.Equal(t, saved, loaded)