| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 1 | //go:build goexperiment.synctest |
| 2 | |
| 3 | package loop |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 7 | "encoding/json" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 8 | "fmt" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 9 | "testing" |
| 10 | "testing/synctest" |
| 11 | |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 12 | "sketch.dev/llm" |
| 13 | "sketch.dev/llm/conversation" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | func TestLoop_OneTurn_Basic(t *testing.T) { |
| 17 | synctest.Run(func() { |
| 18 | mockConvo := NewMockConvo(t) |
| 19 | |
| 20 | agent := &Agent{ |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 21 | convo: mockConvo, |
| 22 | inbox: make(chan string, 1), |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 23 | } |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 24 | agent.stateMachine = NewStateMachine() |
| 25 | userMsg := llm.UserStringMessage("hi") |
| 26 | userMsgResponse := &llm.Response{} |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 27 | mockConvo.ExpectCall("SendMessage", userMsg).Return(userMsgResponse, nil) |
| 28 | |
| 29 | ctx, cancel := context.WithCancel(context.Background()) |
| 30 | defer cancel() |
| 31 | |
| 32 | go agent.Loop(ctx) |
| 33 | |
| 34 | agent.UserMessage(ctx, "hi") |
| 35 | |
| 36 | // This makes sure the SendMessage call happens before we assert the expectations. |
| 37 | synctest.Wait() |
| 38 | |
| 39 | // Verify results |
| 40 | mockConvo.AssertExpectations(t) |
| 41 | }) |
| 42 | } |
| 43 | |
| 44 | func TestLoop_ToolCall_Basic(t *testing.T) { |
| 45 | synctest.Run(func() { |
| 46 | mockConvo := NewMockConvo(t) |
| 47 | |
| 48 | agent := &Agent{ |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 49 | convo: mockConvo, |
| 50 | inbox: make(chan string, 1), |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 51 | } |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 52 | agent.stateMachine = NewStateMachine() |
| 53 | userMsg := llm.Message{ |
| 54 | Role: llm.MessageRoleUser, |
| 55 | Content: []llm.Content{ |
| 56 | {Type: llm.ContentTypeText, Text: "hi"}, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 57 | }, |
| 58 | } |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 59 | userMsgResponse := &llm.Response{ |
| 60 | StopReason: llm.StopReasonToolUse, |
| 61 | Content: []llm.Content{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 62 | { |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 63 | Type: llm.ContentTypeToolUse, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 64 | ID: "tool1", |
| 65 | ToolName: "test_tool", |
| 66 | ToolInput: []byte(`{"param":"value"}`), |
| 67 | }, |
| 68 | }, |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 69 | Usage: llm.Usage{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 70 | InputTokens: 100, |
| 71 | OutputTokens: 200, |
| 72 | }, |
| 73 | } |
| 74 | |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 75 | toolUseContents := []llm.Content{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 76 | { |
| Philip Zeyliger | 72252cb | 2025-05-10 17:00:08 -0700 | [diff] [blame] | 77 | Type: llm.ContentTypeToolResult, |
| 78 | ToolUseID: "tool1", |
| 79 | Text: "", |
| 80 | ToolResult: []llm.Content{{ |
| 81 | Type: llm.ContentTypeText, |
| 82 | Text: "This is a tool result", |
| 83 | }}, |
| 84 | ToolError: false, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 85 | }, |
| 86 | } |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 87 | toolUseResultsMsg := llm.Message{ |
| 88 | Role: llm.MessageRoleUser, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 89 | Content: toolUseContents, |
| 90 | } |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 91 | toolUseResponse := &llm.Response{ |
| 92 | StopReason: llm.StopReasonEndTurn, |
| 93 | Content: []llm.Content{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 94 | { |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 95 | Type: llm.ContentTypeText, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 96 | Text: "tool_use contents accepted", |
| 97 | }, |
| 98 | }, |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 99 | Usage: llm.Usage{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 100 | InputTokens: 50, |
| 101 | OutputTokens: 75, |
| 102 | }, |
| 103 | } |
| 104 | |
| 105 | // Set up the mock response for tool results |
| 106 | mockConvo.ExpectCall("SendMessage", userMsg).Return(userMsgResponse, nil) |
| Josh Bleecher Snyder | 64f2aa8 | 2025-05-14 18:31:05 +0000 | [diff] [blame] | 107 | mockConvo.ExpectCall("ToolResultContents", userMsgResponse).Return(toolUseContents, false, nil) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 108 | mockConvo.ExpectCall("SendMessage", toolUseResultsMsg).Return(toolUseResponse, nil) |
| 109 | |
| 110 | ctx, cancel := context.WithCancel(context.Background()) |
| 111 | defer cancel() |
| 112 | |
| 113 | go agent.Loop(ctx) |
| 114 | |
| 115 | agent.UserMessage(ctx, "hi") |
| 116 | |
| 117 | // This makes sure the SendMessage call happens before we assert the expectations. |
| 118 | synctest.Wait() |
| 119 | |
| 120 | // Verify results |
| 121 | mockConvo.AssertExpectations(t) |
| 122 | }) |
| 123 | } |
| 124 | |
| 125 | func TestLoop_ToolCall_UserCancelsDuringToolResultContents(t *testing.T) { |
| 126 | synctest.Run(func() { |
| 127 | mockConvo := NewMockConvo(t) |
| 128 | |
| 129 | agent := &Agent{ |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 130 | convo: mockConvo, |
| 131 | inbox: make(chan string, 1), |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 132 | } |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 133 | agent.stateMachine = NewStateMachine() |
| 134 | userMsg := llm.UserStringMessage("hi") |
| 135 | userMsgResponse := &llm.Response{ |
| 136 | StopReason: llm.StopReasonToolUse, |
| 137 | Content: []llm.Content{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 138 | { |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 139 | Type: llm.ContentTypeToolUse, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 140 | ID: "tool1", |
| 141 | ToolName: "test_tool", |
| 142 | ToolInput: []byte(`{"param":"value"}`), |
| 143 | }, |
| 144 | }, |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 145 | Usage: llm.Usage{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 146 | InputTokens: 100, |
| 147 | OutputTokens: 200, |
| 148 | }, |
| 149 | } |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 150 | toolUseResultsMsg := llm.UserStringMessage(cancelToolUseMessage) |
| 151 | toolUseResponse := &llm.Response{ |
| 152 | StopReason: llm.StopReasonEndTurn, |
| 153 | Content: []llm.Content{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 154 | { |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 155 | Type: llm.ContentTypeText, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 156 | Text: "tool_use contents accepted", |
| 157 | }, |
| 158 | }, |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 159 | Usage: llm.Usage{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 160 | InputTokens: 50, |
| 161 | OutputTokens: 75, |
| 162 | }, |
| 163 | } |
| 164 | |
| 165 | // Set up the mock response for tool results |
| 166 | |
| 167 | userCancelError := fmt.Errorf("user canceled") |
| 168 | // This allows the test to block the InnerLoop goroutine that invokes ToolResultsContents so |
| 169 | // we can force its context to cancel while it's blocked. |
| 170 | waitForToolResultContents := make(chan any, 1) |
| 171 | |
| 172 | mockConvo.ExpectCall("SendMessage", userMsg).Return(userMsgResponse, nil) |
| 173 | mockConvo.ExpectCall("ToolResultContents", |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 174 | userMsgResponse).BlockAndReturn(waitForToolResultContents, []llm.Content{}, userCancelError) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 175 | mockConvo.ExpectCall("SendMessage", toolUseResultsMsg).Return(toolUseResponse, nil) |
| 176 | |
| 177 | ctx, cancel := context.WithCancel(context.Background()) |
| 178 | defer cancel() |
| 179 | |
| 180 | go agent.Loop(ctx) |
| 181 | |
| 182 | // This puts one message into agent.inbox, which should un-block the GatherMessages call |
| 183 | // at the top of agent.InnerLoop. |
| 184 | agent.UserMessage(ctx, "hi") |
| 185 | |
| 186 | // This makes sure the first SendMessage call happens before we proceed with the cancel. |
| 187 | synctest.Wait() |
| 188 | |
| 189 | // The goroutine executing ToolResultContents call should be blocked, simulating a long |
| 190 | // running operation that the user wishes to cancel while it's still in progress. |
| 191 | // This call invokes that InnerLoop context's cancel() func. |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 192 | agent.CancelTurn(userCancelError) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 193 | |
| 194 | // This tells the goroutine that's in mockConvo.ToolResultContents to proceed. |
| 195 | waitForToolResultContents <- nil |
| 196 | |
| 197 | // This makes sure the final SendMessage call happens before we assert the expectations. |
| 198 | synctest.Wait() |
| 199 | |
| 200 | // Verify results |
| 201 | mockConvo.AssertExpectations(t) |
| 202 | }) |
| 203 | } |
| 204 | |
| 205 | func TestLoop_ToolCall_UserCancelsDuringToolResultContents_AndContinuesToChat(t *testing.T) { |
| 206 | synctest.Run(func() { |
| 207 | mockConvo := NewMockConvo(t) |
| 208 | |
| 209 | agent := &Agent{ |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 210 | convo: mockConvo, |
| 211 | inbox: make(chan string, 1), |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 212 | } |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 213 | agent.stateMachine = NewStateMachine() |
| 214 | userMsg := llm.Message{ |
| 215 | Role: llm.MessageRoleUser, |
| 216 | Content: []llm.Content{ |
| 217 | {Type: llm.ContentTypeText, Text: "hi"}, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 218 | }, |
| 219 | } |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 220 | userMsgResponse := &llm.Response{ |
| 221 | StopReason: llm.StopReasonToolUse, |
| 222 | Content: []llm.Content{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 223 | { |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 224 | Type: llm.ContentTypeToolUse, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 225 | ID: "tool1", |
| 226 | ToolName: "test_tool", |
| 227 | ToolInput: []byte(`{"param":"value"}`), |
| 228 | }, |
| 229 | }, |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 230 | Usage: llm.Usage{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 231 | InputTokens: 100, |
| 232 | OutputTokens: 200, |
| 233 | }, |
| 234 | } |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 235 | toolUseResultsMsg := llm.Message{ |
| 236 | Role: llm.MessageRoleUser, |
| 237 | Content: []llm.Content{ |
| 238 | {Type: llm.ContentTypeText, Text: cancelToolUseMessage}, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 239 | }, |
| 240 | } |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 241 | toolUseResultResponse := &llm.Response{ |
| 242 | StopReason: llm.StopReasonEndTurn, |
| 243 | Content: []llm.Content{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 244 | { |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 245 | Type: llm.ContentTypeText, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 246 | Text: "awaiting further instructions", |
| 247 | }, |
| 248 | }, |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 249 | Usage: llm.Usage{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 250 | InputTokens: 50, |
| 251 | OutputTokens: 75, |
| 252 | }, |
| 253 | } |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 254 | userFollowUpMsg := llm.Message{ |
| 255 | Role: llm.MessageRoleUser, |
| 256 | Content: []llm.Content{ |
| 257 | {Type: llm.ContentTypeText, Text: "that was the wrong thing to do"}, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 258 | }, |
| 259 | } |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 260 | userFollowUpResponse := &llm.Response{ |
| 261 | StopReason: llm.StopReasonEndTurn, |
| 262 | Content: []llm.Content{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 263 | { |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 264 | Type: llm.ContentTypeText, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 265 | Text: "sorry about that", |
| 266 | }, |
| 267 | }, |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 268 | Usage: llm.Usage{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 269 | InputTokens: 100, |
| 270 | OutputTokens: 200, |
| 271 | }, |
| 272 | } |
| 273 | // Set up the mock response for tool results |
| 274 | |
| 275 | userCancelError := fmt.Errorf("user canceled") |
| 276 | // This allows the test to block the InnerLoop goroutine that invokes ToolResultsContents so |
| 277 | // we can force its context to cancel while it's blocked. |
| 278 | waitForToolResultContents := make(chan any, 1) |
| 279 | |
| 280 | mockConvo.ExpectCall("SendMessage", userMsg).Return(userMsgResponse, nil) |
| 281 | mockConvo.ExpectCall("ToolResultContents", |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 282 | userMsgResponse).BlockAndReturn(waitForToolResultContents, []llm.Content{}, userCancelError) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 283 | mockConvo.ExpectCall("SendMessage", toolUseResultsMsg).Return(toolUseResultResponse, nil) |
| 284 | |
| 285 | mockConvo.ExpectCall("SendMessage", userFollowUpMsg).Return(userFollowUpResponse, nil) |
| 286 | |
| 287 | ctx, cancel := context.WithCancel(context.Background()) |
| 288 | defer cancel() |
| 289 | |
| 290 | go agent.Loop(ctx) |
| 291 | |
| 292 | // This puts one message into agent.inbox, which should un-block the GatherMessages call |
| 293 | // at the top of agent.InnerLoop. |
| 294 | agent.UserMessage(ctx, "hi") |
| 295 | |
| 296 | // This makes sure the first SendMessage call happens before we proceed with the cancel. |
| 297 | synctest.Wait() |
| 298 | |
| 299 | // The goroutine executing ToolResultContents call should be blocked, simulating a long |
| 300 | // running operation that the user wishes to cancel while it's still in progress. |
| 301 | // This call invokes that InnerLoop context's cancel() func. |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 302 | agent.CancelTurn(userCancelError) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 303 | |
| 304 | // This tells the goroutine that's in mockConvo.ToolResultContents to proceed. |
| 305 | waitForToolResultContents <- nil |
| 306 | |
| 307 | // Allow InnerLoop to handle the cancellation logic before continuing the conversation. |
| 308 | synctest.Wait() |
| 309 | |
| 310 | agent.UserMessage(ctx, "that was the wrong thing to do") |
| 311 | |
| 312 | synctest.Wait() |
| 313 | |
| 314 | // Verify results |
| 315 | mockConvo.AssertExpectations(t) |
| 316 | }) |
| 317 | } |
| 318 | |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 319 | func TestProcessTurn_UserCancels(t *testing.T) { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 320 | synctest.Run(func() { |
| 321 | mockConvo := NewMockConvo(t) |
| 322 | |
| 323 | agent := &Agent{ |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 324 | convo: mockConvo, |
| 325 | inbox: make(chan string, 1), |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 326 | } |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 327 | agent.stateMachine = NewStateMachine() |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 328 | |
| 329 | // Define test message |
| 330 | // This simulates something that would result in claude responding with tool_use responses. |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 331 | userMsg := llm.UserStringMessage("use test_tool for something") |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 332 | // Mock initial response with tool use |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 333 | userMsgResponse := &llm.Response{ |
| 334 | StopReason: llm.StopReasonToolUse, |
| 335 | Content: []llm.Content{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 336 | { |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 337 | Type: llm.ContentTypeToolUse, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 338 | ID: "tool1", |
| 339 | ToolName: "test_tool", |
| 340 | ToolInput: []byte(`{"param":"value"}`), |
| 341 | }, |
| 342 | }, |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 343 | Usage: llm.Usage{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 344 | InputTokens: 100, |
| 345 | OutputTokens: 200, |
| 346 | }, |
| 347 | } |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 348 | canceledToolUseContents := []llm.Content{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 349 | { |
| Philip Zeyliger | 72252cb | 2025-05-10 17:00:08 -0700 | [diff] [blame] | 350 | Type: llm.ContentTypeToolResult, |
| 351 | ToolUseID: "tool1", |
| 352 | ToolError: true, |
| 353 | ToolResult: []llm.Content{{ |
| 354 | Type: llm.ContentTypeText, |
| 355 | Text: "user canceled this tool_use", |
| 356 | }}, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 357 | }, |
| 358 | } |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 359 | canceledToolUseMsg := llm.Message{ |
| 360 | Role: llm.MessageRoleUser, |
| 361 | Content: append(canceledToolUseContents, llm.StringContent(cancelToolUseMessage)), |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 362 | } |
| 363 | // Set up expected behaviors |
| 364 | waitForSendMessage := make(chan any) |
| 365 | mockConvo.ExpectCall("SendMessage", userMsg).BlockAndReturn(waitForSendMessage, userMsgResponse, nil) |
| 366 | |
| 367 | mockConvo.ExpectCall("ToolResultCancelContents", userMsgResponse).Return(canceledToolUseContents, nil) |
| 368 | mockConvo.ExpectCall("SendMessage", canceledToolUseMsg).Return( |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 369 | &llm.Response{ |
| 370 | StopReason: llm.StopReasonToolUse, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 371 | }, nil) |
| 372 | |
| 373 | ctx, cancel := context.WithCancelCause(context.Background()) |
| 374 | |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 375 | // Run one iteration of the processing loop |
| 376 | go agent.processTurn(ctx) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 377 | |
| 378 | // Send a message to the agent's inbox |
| 379 | agent.UserMessage(ctx, "use test_tool for something") |
| 380 | |
| 381 | synctest.Wait() |
| 382 | |
| 383 | // cancel the context before we even call InnerLoop with it, so it will |
| 384 | // be .Done() the first time it checks. |
| 385 | cancel(fmt.Errorf("user canceled")) |
| 386 | |
| 387 | // unblock the InnerLoop goroutine's SendMessage call |
| 388 | waitForSendMessage <- nil |
| 389 | |
| 390 | synctest.Wait() |
| 391 | |
| 392 | // Verify results |
| 393 | mockConvo.AssertExpectations(t) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 394 | }) |
| 395 | } |
| 396 | |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 397 | func TestProcessTurn_UserDoesNotCancel(t *testing.T) { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 398 | mockConvo := NewMockConvo(t) |
| 399 | |
| 400 | agent := &Agent{ |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 401 | convo: mockConvo, |
| 402 | inbox: make(chan string, 100), |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 403 | } |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 404 | agent.stateMachine = NewStateMachine() |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 405 | |
| 406 | // Define test message |
| 407 | // This simulates something that would result in claude |
| 408 | // responding with tool_use responses. |
| 409 | testMsg := "use test_tool for something" |
| 410 | |
| 411 | // Mock initial response with tool use |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 412 | initialResponse := &llm.Response{ |
| 413 | StopReason: llm.StopReasonToolUse, |
| 414 | Content: []llm.Content{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 415 | { |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 416 | Type: llm.ContentTypeToolUse, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 417 | ID: "tool1", |
| 418 | ToolName: "test_tool", |
| 419 | ToolInput: []byte(`{"param":"value"}`), |
| 420 | }, |
| 421 | }, |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 422 | Usage: llm.Usage{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 423 | InputTokens: 100, |
| 424 | OutputTokens: 200, |
| 425 | }, |
| 426 | } |
| 427 | |
| 428 | // Set up expected behaviors |
| 429 | mockConvo.ExpectCall("SendMessage", nil).Return(initialResponse, nil) |
| 430 | |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 431 | toolUseContents := []llm.Content{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 432 | { |
| Philip Zeyliger | 72252cb | 2025-05-10 17:00:08 -0700 | [diff] [blame] | 433 | Type: llm.ContentTypeToolResult, |
| 434 | ToolUseID: "tool1", |
| 435 | Text: "", |
| 436 | ToolResult: []llm.Content{{ |
| 437 | Type: llm.ContentTypeText, |
| 438 | Text: "This is a tool result", |
| 439 | }}, |
| 440 | ToolError: false, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 441 | }, |
| 442 | } |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 443 | toolUseResponse := &llm.Response{ |
| 444 | // StopReason: llm.StopReasonEndTurn, |
| 445 | Content: []llm.Content{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 446 | { |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 447 | Type: llm.ContentTypeText, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 448 | Text: "tool_use contents accepted", |
| 449 | }, |
| 450 | }, |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 451 | Usage: llm.Usage{ |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 452 | InputTokens: 50, |
| 453 | OutputTokens: 75, |
| 454 | }, |
| 455 | } |
| 456 | |
| 457 | ctx, cancel := context.WithCancel(context.Background()) |
| 458 | defer cancel() |
| 459 | |
| 460 | // Setting up the mock response for tool results |
| Josh Bleecher Snyder | 64f2aa8 | 2025-05-14 18:31:05 +0000 | [diff] [blame] | 461 | mockConvo.ExpectCall("ToolResultContents", initialResponse).Return(toolUseContents, false, nil) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 462 | mockConvo.ExpectCall("SendMessage", nil).Return(toolUseResponse, nil) |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 463 | // mockConvo, as a mock, isn't able to run the loop in conversation.Convo that makes this agent.OnToolResult callback. |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 464 | // So we "mock" it out here by calling it explicitly, in order to make sure it calls .pushToOutbox with this message. |
| 465 | // This is not a good situation. |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 466 | // conversation.Convo and loop.Agent seem to be excessively coupled, and aware of each others' internal details. |
| 467 | // TODO: refactor (or clarify in docs somewhere) the boundary between what conversation.Convo is responsible |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 468 | // for vs what loop.Agent is responsible for. |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 469 | antConvo := &conversation.Convo{} |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 470 | res := "" |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 471 | agent.OnToolResult(ctx, antConvo, "tool1", "test_tool", json.RawMessage(`{"param":"value"}`), toolUseContents[0], &res, nil) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 472 | |
| 473 | // Send a message to the agent's inbox |
| 474 | agent.UserMessage(ctx, testMsg) |
| 475 | |
| Sean McCullough | 3871a09 | 2025-05-05 21:54:56 +0000 | [diff] [blame] | 476 | // Run one iteration of the processing loop |
| 477 | agent.processTurn(ctx) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 478 | |
| 479 | // Verify results |
| 480 | mockConvo.AssertExpectations(t) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 481 | } |