X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/baf7a940be8927278e3b4b87e7366cebfc16532f..48d7bce28b18ecef4c52c98821675605464cecb9:/compactor_test.go
diff --git a/compactor_test.go b/compactor_test.go
index a128bc7..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"])
@@ -40,14 +40,14 @@ func TestCompactorSendsThePromptToolsAndTask(t *testing.T) {
require.NoError(t, err)
require.JSONEq(t, string(tools), string(sent))
- parts := compactorInputParts(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) {
@@ -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(), compactorLeafTask())
+ 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 := compactorInputParts(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 := compactorInputParts(t, call)
- require.Equal(t, first, parts[:2])
- require.Equal(t, []string{compactorTooLong(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))
}
@@ -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",
@@ -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() {
@@ -201,11 +208,12 @@ func newTestCompactor(t *testing.T, server *ModelFake) *Compactor {
return newCompactor(client, compactorAdaPrompt, ModelConfig{
Model: "gpt-test",
Effort: "high",
- })
+ }, promptTools(), &ImageStore{dir: t.TempDir()})
}
-// compactorInputParts returns the texts of the one user message of call.
-func compactorInputParts(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 {
@@ -219,13 +227,14 @@ func compactorInputParts(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
@@ -242,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
}