16 "github.com/stretchr/testify/require"
19 // probeMessages is the length of the generated log for the live probe.
20 const probeMessages = 1000
22 // TestProbeLogHasTheSyntheticMix checks that the generated log has readable
23 // ASCII text with the mix of kinds and sizes of the synthetic log. When
24 // ONECHAT_PROBE_LOG names a file, the test also writes the log there, one
25 // message {kind, text} per line, for "onechat probe".
26 func TestProbeLogHasTheSyntheticMix(t *testing.T) {
27 messages := probeLog(probeMessages)
28 require.Len(t, messages, probeMessages)
30 counts := make(map[string]int)
32 for _, message := range messages {
33 counts[message.Kind]++
34 require.True(t, kinds[message.Kind], message.Kind)
35 require.NotEmpty(t, message.Text)
36 for _, char := range message.Text {
37 require.True(t, char == '\n' || (char >= ' ' && char <= '~'), "character %q", char)
40 if len(message.Text) >= 30000 {
44 if message.Kind == "work" {
45 require.True(t, strings.HasPrefix(message.Text, "["), message.Text)
49 require.Greater(t, counts["user"], 20)
50 require.Greater(t, counts["tool"], 300)
51 require.Greater(t, counts["echo"], 300)
52 require.Greater(t, long, 2)
53 require.Equal(t, messages, probeLog(probeMessages))
55 path := os.Getenv("ONECHAT_PROBE_LOG")
60 var data strings.Builder
61 for _, message := range messages {
62 line, err := json.Marshal(message)
63 require.NoError(t, err)
69 require.NoError(t, os.WriteFile(path, []byte(data.String()), 0o600))
72 func TestProbeStopRules(t *testing.T) {
73 quota := func(primary, secondary string) ModelUsageRecord {
74 return ModelUsageRecord{
75 Quota: map[string]string{
76 "x-codex-primary-used-percent": primary,
77 "x-codex-secondary-used-percent": secondary,
78 "x-codex-plan-type": "plus",
83 testCases := []struct {
85 records []ModelUsageRecord
94 name: "growth within the limit",
95 records: []ModelUsageRecord{quota("10", "40"), quota("25", "41")},
99 name: "growth over the limit",
100 records: []ModelUsageRecord{quota("10", "40"), quota("12", "55.5")},
101 want: "x-codex-secondary-used-percent grew from 40 to 55.5",
105 records: []ModelUsageRecord{quota("10", "40"), {
107 Error: "model call: HTTP 400",
109 want: "call failed: model call: HTTP 400",
113 for _, tc := range testCases {
114 t.Run(tc.name, func(t *testing.T) {
115 require.Equal(t, tc.want, probeStop(tc.records))
120 func TestProbeRunStopsWhenTheQuotaGrows(t *testing.T) {
121 server := newModelFake(t)
122 server.reply = func(w http.ResponseWriter, call ModelFakeCall) {
123 w.Header().Set("X-Codex-Primary-Used-Percent", strconv.Itoa(10+call.number))
124 modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem(strings.Repeat("s", 400))}, 1000, 800, 50))
127 run := newTestProbeRun(t, server)
128 appended, stop := run.Run(t.Context(), probeLog(probeMessages))
129 sizes, err := modelLines(run.memory)
130 require.NoError(t, err)
131 require.NoError(t, run.memory.Close())
133 require.Regexp(t, `^x-codex-primary-used-percent grew from \d+ to \d+$`, stop)
134 require.Less(t, appended, probeMessages)
135 require.NotEmpty(t, sizes)
137 records, err := readUsageFile(run.usagePath)
138 require.NoError(t, err)
139 require.Greater(t, len(records), int(probeQuotaLimit))
141 var report strings.Builder
142 require.NoError(t, writeProbeReport(&report, appended, stop, records, sizes))
143 require.Contains(t, report.String(), "stopped early: ")
144 require.Contains(t, report.String(), "cached (80.0%)")
145 require.Contains(t, report.String(), "line bytes: min 400, median 400, mean 400, max 400, 0 over 512")
146 require.Contains(t, report.String(), "calls at once: at most ")
147 require.Contains(t, report.String(), "quota at the first call: primary-used-percent=")
150 func TestProbeRunStopsAtTheCallLimit(t *testing.T) {
151 server := newModelFake(t)
152 run := newTestProbeRun(t, server)
153 run.client.LimitCalls(5)
155 entries := make([]probeEntry, 40)
156 for n := range entries {
157 entries[n] = probeEntry{
159 Text: strings.Repeat("long output ", 100),
163 _, stop := run.Run(t.Context(), entries)
164 require.NoError(t, run.memory.Close())
165 require.Equal(t, "call limit of 5 reached", stop)
166 require.Equal(t, 5, server.Calls())
169 func TestProbeRunWaitsOnlyBeforeUserMessages(t *testing.T) {
170 gate := make(chan struct{})
172 running, most := 0, 0
173 server := newModelFake(t)
174 server.reply = func(w http.ResponseWriter, call ModelFakeCall) {
177 most = max(most, running)
186 modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("a line")}, 100, 0, 5))
189 run := newTestProbeRun(t, server)
190 long := strings.Repeat("long output ", 100)
191 entries := []probeEntry{
210 done := make(chan string)
212 _, stop := run.Run(t.Context(), entries)
216 require.Eventually(t, func() bool {
217 return server.Calls() == 2
218 }, 10*time.Second, time.Millisecond)
220 run.memory.mutex.Lock()
221 appended := run.memory.log.Len()
222 run.memory.mutex.Unlock()
223 require.Equal(t, 2, appended)
226 require.Empty(t, <-done)
227 require.NoError(t, run.memory.Close())
232 require.Equal(t, 2, most)
233 require.Equal(t, 3, server.Calls())
236 func TestSlotSummarizerLimitsTheBuildsAtOnce(t *testing.T) {
237 inner := newFakeSummarizer()
238 inner.gate = make(chan struct{})
239 summarizer := slotSummarizer{
241 slots: make(chan struct{}, 2),
244 var group sync.WaitGroup
247 _, err := summarizer.Summarize(t.Context(), Task{
250 Input: []string{"user: " + strings.Repeat("x", 600)},
252 require.NoError(t, err)
256 require.Eventually(t, func() bool {
257 return inner.Active() == 2
258 }, 10*time.Second, time.Millisecond)
262 require.Equal(t, 2, inner.peak)
265 func TestReadUsageFileSortsByCallStart(t *testing.T) {
266 path := filepath.Join(t.TempDir(), "usage.jsonl")
267 usage := ModelUsageLog{
271 for _, date := range []string{"2026-01-01T12:00:02Z", "2026-01-01T12:00:00.5Z", "2026-01-01T12:00:01.25Z"} {
272 require.NoError(t, usage.Write(ModelUsageRecord{
277 records, err := readUsageFile(path)
278 require.NoError(t, err)
279 require.Equal(t, []string{"2026-01-01T12:00:00.5Z", "2026-01-01T12:00:01.25Z", "2026-01-01T12:00:02Z"}, []string{records[0].Date, records[1].Date, records[2].Date})
282 func TestTimeTextCountsTheCallsAtOnce(t *testing.T) {
283 start := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)
284 record := func(offset, seconds float64) ModelUsageRecord {
285 return ModelUsageRecord{
286 Date: start.Add(time.Duration(offset * float64(time.Second))).Format(time.RFC3339Nano),
291 text := timeText([]ModelUsageRecord{record(0, 10), record(2, 4), record(3, 2), record(10, 10)})
292 require.Equal(t, "time: 20 s in all, 12.0 calls per minute, 6.5 s per call (median 10.0, max 10.0), calls at once: at most 3, 1.3 on average", text)
295 // probeLog returns count messages of made-up work on a small bakery order
296 // system, with the mix of kinds and sizes of syntheticMessage. The same count
297 // always gives the same log.
298 func probeLog(count int) []probeEntry {
300 random: rand.New(rand.NewPCG(7, 11)),
303 messages := make([]probeEntry, count)
304 for id := range messages {
305 kind, text := g.message()
306 messages[id] = probeEntry{
315 // probeWriter writes readable made-up messages.
316 type probeWriter struct {
320 func (g *probeWriter) message() (string, string) {
321 roll := g.random.IntN(100)
324 return "user", g.fill(g.userLine, 10+g.random.IntN(200))
326 return "reply", g.fill(g.replyLine, 100+g.random.IntN(1500))
328 return "note", g.fill(g.noteLine, 50+g.random.IntN(600))
330 return "work", "[" + g.pick(people) + "] report: " + g.fill(g.replyLine, 300+g.random.IntN(3000))
332 return "tool", g.toolCall(60 + g.random.IntN(400))
334 return "echo", g.fill(g.outputLine, 30000+g.random.IntN(30000))
336 return "echo", g.fill(g.outputLine, 20+g.random.IntN(4000))
340 // fill joins lines of write until the text has about size bytes.
341 func (g *probeWriter) fill(write func() string, size int) string {
342 var text strings.Builder
343 for text.Len() < size {
348 text.WriteString(write())
354 func (g *probeWriter) userLine() string {
355 return g.pick([]string{
356 fmt.Sprintf("Please check why the %s %s.", g.pick(things), g.pick(problems)),
357 fmt.Sprintf("Move the %s to %s before %s.", g.pick(things), g.pick(places), g.pick(days)),
358 fmt.Sprintf("Thanks, that works. Next, %s.", g.pick(tasks)),
359 fmt.Sprintf("No, keep the old %s. %s asked for it.", g.pick(things), g.pick(people)),
360 fmt.Sprintf("How many %s orders came in on %s?", g.pick(breads), g.pick(days)),
364 func (g *probeWriter) replyLine() string {
365 return g.pick([]string{
366 fmt.Sprintf("I looked at %s. The %s %s because %s.", g.pick(files), g.pick(things), g.pick(problems), g.pick(causes)),
367 fmt.Sprintf("I changed %s, and the tests pass now.", g.pick(changes)),
368 fmt.Sprintf("There were %d %s orders on %s, %d of them late.", 10+g.random.IntN(90), g.pick(breads), g.pick(days), g.random.IntN(10)),
369 fmt.Sprintf("Open question: should %s get %s?", g.pick(people), g.pick(tasks)),
373 func (g *probeWriter) noteLine() string {
374 return g.pick([]string{
375 fmt.Sprintf("%s prefers %s.", g.pick(people), g.pick(preferences)),
376 fmt.Sprintf("The bakery opens at %d and closes at %d on %s.", 5+g.random.IntN(3), 17+g.random.IntN(4), g.pick(days)),
380 func (g *probeWriter) outputLine() string {
381 return g.pick([]string{
382 fmt.Sprintf("%02d:%02d order %d for %d %s, packed by %s.", 6+g.random.IntN(12), g.random.IntN(60), 1000+g.random.IntN(9000), 1+g.random.IntN(24), g.pick(breads), g.pick(people)),
383 fmt.Sprintf("%s:%d: %s %s.", g.pick(files), 1+g.random.IntN(400), g.pick(things), g.pick(problems)),
384 fmt.Sprintf("ok bakery/%s %d.%03ds", g.pick(modules), g.random.IntN(3), g.random.IntN(1000)),
388 // toolCall returns a tool name and its JSON input of about size bytes.
389 func (g *probeWriter) toolCall(size int) string {
390 name := g.pick([]string{"shell", "read", "write", "edit"})
391 var input map[string]string
394 input = map[string]string{"command": "grep -n '" + g.pick(things) + "' " + g.pick(files)}
396 input = map[string]string{"path": g.pick(files)}
398 input = map[string]string{
399 "path": g.pick(files),
400 "text": g.fill(g.replyLine, max(0, size-80)),
404 data, err := json.Marshal(input)
409 return name + " " + string(data)
412 func (g *probeWriter) pick(words []string) string {
413 return words[g.random.IntN(len(words))]
416 func newTestProbeRun(t *testing.T, server *ModelFake) *probeRun {
420 usagePath := filepath.Join(root, "usage.jsonl")
421 client := newModelClient(server.URL, ModelAPIKey(modelTestKey), "onechat-"+probeName, usagePath)
422 memory, err := openMemory(t.Context(), root, newTestClock().Now, newCompactor(client, probePrompt, defaultConfig().Compactor))
423 require.NoError(t, err)
428 usagePath: usagePath,
433 people = []string{"Mara", "Joel", "Ines", "Tomas", "Priya", "Lena"}
434 breads = []string{"rye", "sourdough", "baguette", "brioche", "spelt", "seed loaf"}
435 days = []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
436 things = []string{"order form", "price list", "delivery route", "stock count", "invoice export", "oven schedule"}
437 problems = []string{"shows the wrong total", "skips the last row", "is slow at noon", "loses the customer note", "rounds the tax down"}
438 places = []string{"the front page", "the weekly report", "the shared folder", "the admin menu"}
439 tasks = []string{"add a field for allergies", "send the invoices on Friday", "split the route in two", "print labels for the shelf"}
440 causes = []string{"the date has no time zone", "the cache keeps old prices", "the loop stops one row early", "the form trims the note"}
441 changes = []string{"the total to use cents", "the route to sort by street", "the export to add the tax line", "the form to keep the note"}
442 preferences = []string{"short answers", "a call before a change to prices", "invoices as PDF", "reports in the morning"}
443 files = []string{"orders/form.go", "prices/list.go", "routes/plan.go", "stock/count.go", "export/invoice.go", "ovens/schedule.go"}
444 modules = []string{"orders", "prices", "routes", "stock", "export", "ovens"}