X-Git-Url: http://git.giacomolli.uk/onechat/blobdiff_plain/2daf91497c2cc70d7156f7c0d99b1451933a818a..b2fc2d6a737dd5bab25e57ba822dd07258de93e6:/compactor_test.go?ds=sidebyside diff --git a/compactor_test.go b/compactor_test.go index 0cab0d0..baa7f57 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"]) @@ -50,90 +50,6 @@ func TestCompactorSendsThePromptToolsAndTask(t *testing.T) { }, texts) } -func TestCompactorSendsEachWholeBlockOfTheViewAsAMessage(t *testing.T) { - testCases := []struct { - name string - context string - want []string - }{ - { - name: "empty view goes with the task", - context: "\n", - want: []string{"\n\nTASK"}, - }, - { - name: "lines short of a block go with the task", - context: "\n0+1|a\n1+1|b\n2+1|c\n", - want: []string{"\n0+1|a\n1+1|b\n2+1|c\n\nTASK"}, - }, - { - name: "one whole block and the task", - context: "\n0+1|a\n1+1|b\n2+1|c\n3+1|d\n", - want: []string{"\n0+1|a\n1+1|b\n2+1|c\n3+1|d\n", "\nTASK"}, - }, - { - name: "two whole blocks and the rest with the task", - context: "\n0+2|a\n2+1|b\n3+1|c\n4+1|d\n5+1|e\n6+1|f\n7+1|g\n8+1|h\n9+1|i\n", - want: []string{ - "\n0+2|a\n2+1|b\n3+1|c\n4+1|d\n", - "5+1|e\n6+1|f\n7+1|g\n8+1|h\n", - "9+1|i\n\nTASK", - }, - }, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - server := newModelFake(t) - task := compactorLeafTask() - task.Context = tc.context - - _, err := newTestCompactor(t, server).Summarize(t.Context(), task) - require.NoError(t, err) - - text, err := compactorTask(task) - require.NoError(t, err) - - want := make([]string, len(tc.want)) - for n, message := range tc.want { - want[n] = strings.Replace(message, "TASK", text, 1) - } - - texts := compactorInputTexts(t, server.Call(0)) - require.Equal(t, want, texts) - require.Equal(t, tc.context+"\n"+text, strings.Join(texts, "")) - }) - } -} - -func TestCompactorSendsTheSameBlocksForViewsWithTheSameFirstLines(t *testing.T) { - shared := "\n0+4|a\n4+1|b\n5+1|c\n6+1|d\n7+1|e\n8+1|f\n9+1|g\n10+1|h\n" - server := newModelFake(t) - compactor := newTestCompactor(t, server) - _, err := compactor.Summarize(t.Context(), Task{ - Level: 0, - Index: 11, - Context: shared + "11+1|i\n", - Input: []string{"user: eleven"}, - }) - require.NoError(t, err) - - _, err = compactor.Summarize(t.Context(), Task{ - Level: 1, - Index: 5, - Context: shared + "11+1|j\n12+1|k\n", - Input: []string{"left line", "right line"}, - }) - require.NoError(t, err) - - first := compactorInputTexts(t, server.Call(0)) - second := compactorInputTexts(t, server.Call(1)) - require.Len(t, first, 3) - require.Len(t, second, 3) - require.Equal(t, first[:2], second[:2]) - require.NotEqual(t, first[2], second[2]) -} - func TestCompactorWritesTheMergeTask(t *testing.T) { text, err := compactorTask(Task{ Level: 2, @@ -167,22 +83,27 @@ func TestCompactorRetriesALongLineWithTheNewestNote(t *testing.T) { 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) } + text, err := compactorTask(task) + require.NoError(t, err) + first := compactorInputTexts(t, server.Call(0)) - require.Len(t, first, 2) + require.Equal(t, []string{task.Context + "\n" + text}, first) + for n, want := range []string{long[0], long[1]} { call := server.Call(n + 1) texts := compactorInputTexts(t, call) - require.Equal(t, first, texts[:2]) - require.Equal(t, []string{compactorTooLong(want)}, texts[2:]) - require.Contains(t, texts[2], "was "+strconv.Itoa(len(want))+" bytes,") - require.Contains(t, texts[2], "\n"+want[:512]+"| <- LIMIT") + 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[2])) + 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)) } @@ -209,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", @@ -249,6 +171,7 @@ func TestCompactorBuildsNodesInTheCore(t *testing.T) { server.reply = func(w http.ResponseWriter, call ModelFakeCall) { if call.number == 0 { w.WriteHeader(http.StatusServiceUnavailable) + return } @@ -285,7 +208,7 @@ func newTestCompactor(t *testing.T, server *ModelFake) *Compactor { return newCompactor(client, compactorAdaPrompt, ModelConfig{ Model: "gpt-test", Effort: "high", - }) + }, promptTools(), &ImageStore{dir: t.TempDir()}) } // compactorInputTexts returns the text of each user message of call. Each @@ -310,6 +233,7 @@ func compactorInputTexts(t *testing.T, call ModelFakeCall) []string { 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 } @@ -327,5 +251,5 @@ func compactorLeafTask() Task { // compactorAdaPrompt returns the prompt of the agent Ada, without instructions. func compactorAdaPrompt() (string, error) { - return prompt("Ada", ""), nil + return prompt("Ada", "/AGENT/instructions.md", ""), nil }