X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/a4cfb030bf949e2fc9cd0ddace9e33f24b791b18..f226928c3a074ac4856ba07e8fd7107aba5444a3:/compactor_test.go diff --git a/compactor_test.go b/compactor_test.go index 5a4474c..eaed4fe 100644 --- a/compactor_test.go +++ b/compactor_test.go @@ -28,7 +28,7 @@ func TestCompactorSendsThePromptToolsAndTask(t *testing.T) { require.Equal(t, 1, server.Calls()) body := server.Call(0).body - require.Equal(t, prompt("Ada", ""), body["instructions"]) + require.Equal(t, prompt("Ada", "/AGENT/instructions.md", ""), body["instructions"]) require.Equal(t, "gpt-test", body["model"]) require.Equal(t, map[string]any{"effort": "high"}, body["reasoning"]) require.Equal(t, "agent-key", body["prompt_cache_key"]) @@ -40,18 +40,18 @@ func TestCompactorSendsThePromptToolsAndTask(t *testing.T) { require.NoError(t, err) require.JSONEq(t, string(tools), string(sent)) - parts := inputParts(t, server.Call(0)) + texts := compactorInputTexts(t, server.Call(0)) require.Equal(t, []string{ - "\n0+4|user: earlier\n\n", - "Compaction: compress message 7 into one line of at most 512 bytes\n" + + "\n0+4|user: earlier\n\n" + + "Compaction: compress message 7 into one line of at most 512 bytes\n" + "(about 70 words), the length of this ruler:\n" + strings.Repeat("-", 512) + "\n" + "\nuser: " + strings.Repeat("x", 600) + "\n", - }, parts) + }, texts) } func TestCompactorWritesTheMergeTask(t *testing.T) { - text, err := compactionTask(Task{ + text, err := compactorTask(Task{ Level: 2, Index: 3, Input: []string{"left line", "right line"}, @@ -72,33 +72,38 @@ func TestCompactorRetriesALongLineWithTheNewestNote(t *testing.T) { modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem(long[call.number])}, 100, 0, 5)) } + task := compactorLeafTask() + task.Context = "\n0+1|a\n1+1|b\n2+1|c\n3+1|d\n4+1|e\n" + compactor := newTestCompactor(t, server) - line, err := compactor.Summarize(t.Context(), leafTask()) + line, err := compactor.Summarize(t.Context(), task) require.NoError(t, err) require.Equal(t, long[2], line) require.Equal(t, 3, server.Calls()) records := modelReadUsage(t, compactor.client.usage.path) require.Len(t, records, 3) + for n, record := range records { require.Equal(t, &[2]int{0, 0}, record.Node) require.Equal(t, len(long[n]), record.TextBytes) } - first := inputParts(t, server.Call(0)) - require.Len(t, first, 2) + text, err := compactorTask(task) + require.NoError(t, err) + + first := compactorInputTexts(t, server.Call(0)) + require.Equal(t, []string{task.Context + "\n" + text}, first) + for n, want := range []string{long[0], long[1]} { call := server.Call(n + 1) - parts := inputParts(t, call) - require.Equal(t, first, parts[:2]) - require.Equal(t, []string{tooLong(want)}, parts[2:]) - require.Contains(t, parts[2], "was "+strconv.Itoa(len(want))+" bytes,") - require.Contains(t, parts[2], "\n"+want[:512]+"| <- LIMIT") - - note, err := json.Marshal(map[string]string{ - "type": "input_text", - "text": parts[2], - }) + texts := compactorInputTexts(t, call) + require.Equal(t, first, texts[:1]) + require.Equal(t, []string{compactorTooLong(want)}, texts[1:]) + require.Contains(t, texts[1], "was "+strconv.Itoa(len(want))+" bytes,") + require.Contains(t, texts[1], "\n"+want[:512]+"| <- LIMIT") + + note, err := json.Marshal(modelUserMessage(texts[1])) require.NoError(t, err) require.LessOrEqual(t, len(call.raw), len(server.Call(0).raw)+len(",")+len(note)) } @@ -111,9 +116,9 @@ func TestCompactorKeepsTheShortestOfFiveLines(t *testing.T) { modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem(strings.Repeat("z", sizes[call.number]))}, 100, 0, 5)) } - line, err := newTestCompactor(t, server).Summarize(t.Context(), leafTask()) + line, err := newTestCompactor(t, server).Summarize(t.Context(), compactorLeafTask()) require.NoError(t, err) - require.Equal(t, compactionTries, server.Calls()) + require.Equal(t, compactorTries, server.Calls()) require.Len(t, line, 520) } @@ -125,15 +130,16 @@ func TestCompactorFailsWithoutALine(t *testing.T) { }{ { name: "failed call", - reply: func(w http.ResponseWriter, call ModelFakeCall) { + reply: func(w http.ResponseWriter, _ ModelFakeCall) { w.WriteHeader(http.StatusInternalServerError) + _, _ = io.WriteString(w, "down") }, want: "HTTP 500", }, { name: "tool call and no text", - reply: func(w http.ResponseWriter, call ModelFakeCall) { + reply: func(w http.ResponseWriter, _ ModelFakeCall) { modelWriteEvents(w, modelCompletedEvent([]any{map[string]any{"type": "function_call", "name": "zoom", "arguments": "{}"}}, 100, 0, 5)) }, want: "no line", @@ -145,7 +151,7 @@ func TestCompactorFailsWithoutALine(t *testing.T) { server := newModelFake(t) server.reply = tc.reply - _, err := newTestCompactor(t, server).Summarize(t.Context(), leafTask()) + _, err := newTestCompactor(t, server).Summarize(t.Context(), compactorLeafTask()) require.ErrorContains(t, err, tc.want) require.Equal(t, 1, server.Calls()) }) @@ -154,10 +160,10 @@ func TestCompactorFailsWithoutALine(t *testing.T) { func TestCutLineKeepsWholeCharacters(t *testing.T) { line := strings.Repeat("a", 511) + "é" + "tail" - cut := cutLine(line) + cut := compactorCutLine(line) require.Equal(t, strings.Repeat("a", 511), cut) require.True(t, utf8.ValidString(cut)) - require.Equal(t, "short", cutLine("short")) + require.Equal(t, "short", compactorCutLine("short")) } func TestCompactorBuildsNodesInTheCore(t *testing.T) { @@ -165,13 +171,14 @@ func TestCompactorBuildsNodesInTheCore(t *testing.T) { server.reply = func(w http.ResponseWriter, call ModelFakeCall) { if call.number == 0 { w.WriteHeader(http.StatusServiceUnavailable) + return } modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("summary of the long output")}, 100, 0, 5)) } - memory, err := openMemory(t.Context(), t.TempDir(), newTestClock().Now, newTestCompactor(t, server)) + memory, err := openMemory(t.Context(), t.TempDir(), newTestClock().Now, newTestCompactor(t, server), discardLog()) require.NoError(t, err) defer func() { @@ -181,13 +188,13 @@ func TestCompactorBuildsNodesInTheCore(t *testing.T) { _, err = memory.Append("echo", strings.Repeat("output ", 200)) require.NoError(t, err) require.NoError(t, memory.Wait(t.Context())) - require.False(t, isBuilt(t, memory.tree, 0, 0)) + require.False(t, treeIsBuilt(t, memory.tree, 0, 0)) _, err = memory.Append("user", "next") require.NoError(t, err) require.NoError(t, memory.Wait(t.Context())) - node := readNode(t, memory.tree, 0, 0) + node := treeReadNode(t, memory.tree, 0, 0) require.Equal(t, "summary of the long output", node.Text) require.Equal(t, "\n0+1|summary of the long output\n1+1|user: next\n", memory.View()) require.Equal(t, 2, server.Calls()) @@ -198,28 +205,15 @@ func newTestCompactor(t *testing.T, server *ModelFake) *Compactor { client := newModelClient(server.URL, ModelAPIKey(modelTestKey), "agent-key", filepath.Join(t.TempDir(), "usage.jsonl")) - return newCompactor(client, adaPrompt, ModelConfig{ + return newCompactor(client, compactorAdaPrompt, ModelConfig{ Model: "gpt-test", Effort: "high", }) } -// adaPrompt returns the prompt of the agent Ada, without instructions. -func adaPrompt() (string, error) { - return prompt("Ada", ""), nil -} - -func leafTask() Task { - return Task{ - Level: 0, - Index: 0, - Context: "\n", - Input: []string{"user: " + strings.Repeat("y", 600)}, - } -} - -// inputParts returns the texts of the one user message of call. -func inputParts(t *testing.T, call ModelFakeCall) []string { +// compactorInputTexts returns the text of each user message of call. Each +// message has one text part. +func compactorInputTexts(t *testing.T, call ModelFakeCall) []string { t.Helper() var body struct { @@ -233,14 +227,29 @@ func inputParts(t *testing.T, call ModelFakeCall) []string { } require.NoError(t, json.Unmarshal(call.raw, &body)) - require.Len(t, body.Input, 1) - require.Equal(t, "user", body.Input[0].Role) - texts := make([]string, len(body.Input[0].Content)) - for n, part := range body.Input[0].Content { - require.Equal(t, "input_text", part.Type) - texts[n] = part.Text + texts := make([]string, len(body.Input)) + for n, message := range body.Input { + require.Equal(t, "user", message.Role) + require.Len(t, message.Content, 1) + require.Equal(t, "input_text", message.Content[0].Type) + + texts[n] = message.Content[0].Text } return texts } + +func compactorLeafTask() Task { + return Task{ + Level: 0, + Index: 0, + Context: "\n", + Input: []string{"user: " + strings.Repeat("y", 600)}, + } +} + +// compactorAdaPrompt returns the prompt of the agent Ada, without instructions. +func compactorAdaPrompt() (string, error) { + return prompt("Ada", "/AGENT/instructions.md", ""), nil +}