X-Git-Url: http://git.giacomolli.uk/onechat/blobdiff_plain/7088b82414bdf947a6f88918c0a7b2c7fb0c53ef..522d7fd117285e4e4ce3ea5ec9b5a1722521b681:/subagent_run_test.go diff --git a/subagent_run_test.go b/subagent_run_test.go index 125ca33..f637dcf 100644 --- a/subagent_run_test.go +++ b/subagent_run_test.go @@ -21,7 +21,9 @@ const subagentTestFullContext = `{"error": {"message": "Your input exceeds the c func TestSpawnRunsASubagentInTheBackground(t *testing.T) { server := newModelFake(t) + var test *TurnTestLoop + server.reply = func(w http.ResponseWriter, call ModelFakeCall) { last := subagentTestLastItem(call) switch { @@ -47,6 +49,7 @@ func TestSpawnRunsASubagentInTheBackground(t *testing.T) { name := test.events.Last("subagent") require.True(t, subagentNameValid(name), name) + work := filepath.Join(test.loop.runs.subagents.dir, name, "work") require.DirExists(t, work) @@ -66,9 +69,10 @@ func TestSpawnRunsASubagentInTheBackground(t *testing.T) { Source: name, Status: subagentFinished, }, test.events.LastEvent("work")) - require.Equal(t, "subagent "+name+" done\n", test.logs.String()) + require.Equal(t, "subagent "+name+" started\nsubagent "+name+" done after 0s\n", test.logs.String()) var first, second *ModelFakeCall + for n := range server.Calls() { call := server.Call(n) if !subagentTestCall(call) { @@ -97,6 +101,7 @@ func TestSpawnRunsASubagentInTheBackground(t *testing.T) { require.Equal(t, instructions, first.body["instructions"]) var tools []string + for _, tool := range first.body["tools"].([]any) { tools = append(tools, tool.(map[string]any)["name"].(string)) } @@ -112,6 +117,7 @@ func TestSpawnRunsASubagentInTheBackground(t *testing.T) { require.Equal(t, "0|note: subagent\n\n1|user: Count the files.\n\n2|tool: shell {\"command\":\"pwd\"}\n\n3|echo: "+work+"\n\n\n4|reply: There are 3 files.", log) var agents []string + for _, record := range modelReadUsage(t, test.usagePath) { agents = append(agents, record.Agent) } @@ -140,6 +146,7 @@ func TestReportJoinsTheNextRequestOfARunningTurn(t *testing.T) { require.NoError(t, err) <-arrived + report := subagentReport("GreenOtter", []string{"It is done."}) test.loop.Report("GreenOtter", report, subagentFinished) close(release) @@ -170,6 +177,7 @@ func TestFullContextGivesAFailedReport(t *testing.T) { server.reply = func(w http.ResponseWriter, call ModelFakeCall) { if subagentTestCall(call) { w.WriteHeader(http.StatusBadRequest) + _, _ = io.WriteString(w, subagentTestFullContext) return @@ -185,11 +193,11 @@ func TestFullContextGivesAFailedReport(t *testing.T) { name := test.events.Last("subagent") require.Equal(t, []string{ - "work: [" + name + "]\nfailed: model call: HTTP 400: " + subagentTestFullContext + "\n\nFull chat: zoom(\"" + name + "\")", + "work: [" + name + "]\nfailed: create model response: model call: HTTP 400: " + subagentTestFullContext + "\n\nFull chat: zoom(\"" + name + "\")", "reply: It failed.", }, test.Log(t)) require.Equal(t, subagentFailed, test.events.LastEvent("work").Status) - require.Equal(t, "subagent "+name+" failed: model call: HTTP 400: "+subagentTestFullContext+"\n", test.logs.String()) + require.Equal(t, "subagent "+name+" started\nsubagent "+name+" failed after 0s: create model response: model call: HTTP 400: "+subagentTestFullContext+"\n", test.logs.String()) } func TestSubagentHasItsOwnEffortAndNoSpawn(t *testing.T) { @@ -260,6 +268,7 @@ func TestTellReachesARunningSubagentBetweenItsToolCalls(t *testing.T) { require.NoError(t, err) <-arrived + name := test.events.Last("subagent") output, err := runs.Tell(t.Context(), name, "Use the second disk.") require.NoError(t, err) @@ -294,6 +303,7 @@ func TestTellAfterTheFinalAnswerStartsAnotherChain(t *testing.T) { require.NoError(t, err) <-arrived + name := test.events.Last("subagent") _, err = runs.Tell(t.Context(), name, "Count again.") require.NoError(t, err) @@ -344,7 +354,7 @@ func TestTellToAFinishedSubagentGoesOnFromItsLog(t *testing.T) { func TestStopEndsASubagentAndItsCommand(t *testing.T) { server := newModelFake(t) - server.reply = func(w http.ResponseWriter, call ModelFakeCall) { + server.reply = func(w http.ResponseWriter, _ ModelFakeCall) { modelWriteEvents(w, modelCompletedEvent([]any{modelFunctionCallItem("fc_1", "call_1", "shell", `{"command":"touch started && sleep 30"}`)}, 100, 0, 5)) } @@ -374,7 +384,7 @@ func TestStopEndsASubagentAndItsCommand(t *testing.T) { "work: " + subagentReport(name, []string{"stopped"}), }, test.Log(t)) require.Equal(t, subagentStopped, test.events.LastEvent("work").Status) - require.Equal(t, "subagent "+name+" stopped\n", test.logs.String()) + require.Equal(t, "subagent "+name+" started\nsubagent "+name+" stopped after 0s\n", test.logs.String()) log, err := subagent.Show() require.NoError(t, err) @@ -396,7 +406,7 @@ func TestCloseEndsTheSubagentsWithNoReport(t *testing.T) { close(release) }) - server.reply = func(w http.ResponseWriter, call ModelFakeCall) { + server.reply = func(_ http.ResponseWriter, _ ModelFakeCall) { close(arrived) <-release } @@ -428,10 +438,12 @@ func TestRestartResumesTheCutSubagents(t *testing.T) { }) var restarted atomic.Bool + server.reply = func(w http.ResponseWriter, call ModelFakeCall) { switch { case subagentTestCall(call) && !restarted.Load(): arrived <- struct{}{} + <-release case subagentTestCall(call): modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("Built.")}, 100, 0, 5)) @@ -449,7 +461,9 @@ func TestRestartResumesTheCutSubagents(t *testing.T) { require.NoError(t, err) <-arrived + name := first.Last("subagent") + stop() require.Zero(t, first.Count("work")) @@ -458,11 +472,13 @@ func TestRestartResumesTheCutSubagents(t *testing.T) { require.JSONEq(t, `{"running": ["`+name+`"]}`, string(data)) restarted.Store(true) + second := &TurnEventRecorder{} loop, memory, stop := subagentTestStart(t, agent, server, second) defer stop() second.WaitFor(t, "idle", 1) + note := "The daemon restarted, cutting " + name + "; nothing was lost: go on." log := subagentTestChat(t, memory) require.Equal(t, []string{ @@ -543,6 +559,7 @@ func subagentTestChat(t *testing.T, memory *Memory) []string { t.Helper() var lines []string + for id := range memory.Len() { message, err := memory.Message(id) require.NoError(t, err) @@ -571,7 +588,9 @@ func subagentTestLastItem(call ModelFakeCall) map[string]any { // another item. func subagentTestText(item map[string]any) string { content, _ := item["content"].([]any) + var text strings.Builder + for _, part := range content { value, _ := part.(map[string]any)["text"].(string) text.WriteString(value)