|
4 | 4 | "context" |
5 | 5 | "encoding/json" |
6 | 6 | "fmt" |
| 7 | + "strings" |
7 | 8 | "testing" |
8 | 9 |
|
9 | 10 | "github.com/priyanshujain/openbotkit/provider" |
@@ -161,6 +162,30 @@ func TestLoop_MultiToolSequence(t *testing.T) { |
161 | 162 | } |
162 | 163 | } |
163 | 164 |
|
| 165 | +// errorExecutor returns an error for specified tools. |
| 166 | +type errorExecutor struct { |
| 167 | + successes map[string]string |
| 168 | + errors map[string]string |
| 169 | + calls []provider.ToolCall |
| 170 | +} |
| 171 | + |
| 172 | +func (m *errorExecutor) Execute(_ context.Context, call provider.ToolCall) (string, error) { |
| 173 | + m.calls = append(m.calls, call) |
| 174 | + if errMsg, ok := m.errors[call.Name]; ok { |
| 175 | + return "", fmt.Errorf("%s", errMsg) |
| 176 | + } |
| 177 | + if result, ok := m.successes[call.Name]; ok { |
| 178 | + return result, nil |
| 179 | + } |
| 180 | + return "", fmt.Errorf("unknown tool %q", call.Name) |
| 181 | +} |
| 182 | + |
| 183 | +func (m *errorExecutor) ToolSchemas() []provider.Tool { |
| 184 | + return []provider.Tool{ |
| 185 | + {Name: "bash", Description: "Run a command", InputSchema: json.RawMessage(`{"type":"object"}`)}, |
| 186 | + } |
| 187 | +} |
| 188 | + |
164 | 189 | func TestLoop_MaxIterations(t *testing.T) { |
165 | 190 | // Provider always returns tool_use — should stop at max iterations. |
166 | 191 | alwaysToolUse := &provider.ChatResponse{ |
@@ -188,3 +213,173 @@ func TestLoop_MaxIterations(t *testing.T) { |
188 | 213 | t.Errorf("error = %q", got) |
189 | 214 | } |
190 | 215 | } |
| 216 | + |
| 217 | +func TestLoop_ScrubsToolOutput(t *testing.T) { |
| 218 | + mp := &mockProvider{ |
| 219 | + responses: []*provider.ChatResponse{ |
| 220 | + { |
| 221 | + Content: []provider.ContentBlock{ |
| 222 | + {Type: provider.ContentToolUse, ToolCall: &provider.ToolCall{ |
| 223 | + ID: "c1", Name: "bash", Input: json.RawMessage(`{}`), |
| 224 | + }}, |
| 225 | + }, |
| 226 | + StopReason: provider.StopToolUse, |
| 227 | + }, |
| 228 | + { |
| 229 | + Content: []provider.ContentBlock{{Type: provider.ContentText, Text: "Done"}}, |
| 230 | + StopReason: provider.StopEndTurn, |
| 231 | + }, |
| 232 | + }, |
| 233 | + } |
| 234 | + exec := &mockExecutor{results: map[string]string{ |
| 235 | + "bash": "TOKEN=sk-secret-key-12345678", |
| 236 | + }} |
| 237 | + a := New(mp, "test-model", exec) |
| 238 | + |
| 239 | + _, err := a.Run(context.Background(), "show env") |
| 240 | + if err != nil { |
| 241 | + t.Fatalf("Run: %v", err) |
| 242 | + } |
| 243 | + |
| 244 | + // The second request should contain the scrubbed tool result. |
| 245 | + if len(mp.requests) < 2 { |
| 246 | + t.Fatalf("expected 2 requests, got %d", len(mp.requests)) |
| 247 | + } |
| 248 | + msgs := mp.requests[1].Messages |
| 249 | + // Last message should be the tool result. |
| 250 | + last := msgs[len(msgs)-1] |
| 251 | + content := last.Content[0].ToolResult.Content |
| 252 | + if strings.Contains(content, "sk-secret-key-12345678") { |
| 253 | + t.Errorf("tool output not scrubbed: %q", content) |
| 254 | + } |
| 255 | + if !strings.Contains(content, "****") { |
| 256 | + t.Errorf("expected redacted content, got: %q", content) |
| 257 | + } |
| 258 | +} |
| 259 | + |
| 260 | +func TestLoop_ScrubsToolError(t *testing.T) { |
| 261 | + mp := &mockProvider{ |
| 262 | + responses: []*provider.ChatResponse{ |
| 263 | + { |
| 264 | + Content: []provider.ContentBlock{ |
| 265 | + {Type: provider.ContentToolUse, ToolCall: &provider.ToolCall{ |
| 266 | + ID: "c1", Name: "bash", Input: json.RawMessage(`{}`), |
| 267 | + }}, |
| 268 | + }, |
| 269 | + StopReason: provider.StopToolUse, |
| 270 | + }, |
| 271 | + { |
| 272 | + Content: []provider.ContentBlock{{Type: provider.ContentText, Text: "Done"}}, |
| 273 | + StopReason: provider.StopEndTurn, |
| 274 | + }, |
| 275 | + }, |
| 276 | + } |
| 277 | + exec := &errorExecutor{ |
| 278 | + errors: map[string]string{"bash": "failed: password=supersecret123"}, |
| 279 | + } |
| 280 | + a := New(mp, "test-model", exec) |
| 281 | + |
| 282 | + _, err := a.Run(context.Background(), "try") |
| 283 | + if err != nil { |
| 284 | + t.Fatalf("Run: %v", err) |
| 285 | + } |
| 286 | + |
| 287 | + msgs := mp.requests[1].Messages |
| 288 | + last := msgs[len(msgs)-1] |
| 289 | + content := last.Content[0].ToolResult.Content |
| 290 | + if strings.Contains(content, "supersecret123") { |
| 291 | + t.Errorf("tool error not scrubbed: %q", content) |
| 292 | + } |
| 293 | + if !last.Content[0].ToolResult.IsError { |
| 294 | + t.Error("expected IsError=true") |
| 295 | + } |
| 296 | +} |
| 297 | + |
| 298 | +func TestLoop_ProviderChatError(t *testing.T) { |
| 299 | + mp := &mockProvider{responses: nil} // no responses = error on first call |
| 300 | + exec := &mockExecutor{results: map[string]string{}} |
| 301 | + a := New(mp, "test-model", exec) |
| 302 | + |
| 303 | + _, err := a.Run(context.Background(), "hi") |
| 304 | + if err == nil { |
| 305 | + t.Fatal("expected error from provider") |
| 306 | + } |
| 307 | + if !strings.Contains(err.Error(), "chat (iteration 0)") { |
| 308 | + t.Errorf("error = %q, expected chat iteration error", err.Error()) |
| 309 | + } |
| 310 | +} |
| 311 | + |
| 312 | +func TestLoop_CompactsHistory(t *testing.T) { |
| 313 | + // Build a provider that does one tool call per iteration for 15 rounds, then ends. |
| 314 | + var responses []*provider.ChatResponse |
| 315 | + for i := range 15 { |
| 316 | + responses = append(responses, &provider.ChatResponse{ |
| 317 | + Content: []provider.ContentBlock{ |
| 318 | + {Type: provider.ContentToolUse, ToolCall: &provider.ToolCall{ |
| 319 | + ID: fmt.Sprintf("c%d", i), Name: "bash", Input: json.RawMessage(`{}`), |
| 320 | + }}, |
| 321 | + }, |
| 322 | + StopReason: provider.StopToolUse, |
| 323 | + }) |
| 324 | + } |
| 325 | + responses = append(responses, &provider.ChatResponse{ |
| 326 | + Content: []provider.ContentBlock{{Type: provider.ContentText, Text: "Done"}}, |
| 327 | + StopReason: provider.StopEndTurn, |
| 328 | + }) |
| 329 | + |
| 330 | + mp := &mockProvider{responses: responses} |
| 331 | + exec := &mockExecutor{results: map[string]string{"bash": "ok"}} |
| 332 | + // Without compaction, history would be 1 user + 15*(assistant+result) + final assistant = 32 messages. |
| 333 | + // With maxHistory=10, compaction fires repeatedly, keeping history bounded. |
| 334 | + a := New(mp, "test-model", exec, WithMaxHistory(10), WithMaxIterations(20)) |
| 335 | + |
| 336 | + _, err := a.Run(context.Background(), "go") |
| 337 | + if err != nil { |
| 338 | + t.Fatalf("Run: %v", err) |
| 339 | + } |
| 340 | + |
| 341 | + // History should have been compacted (not 32 messages). |
| 342 | + if len(a.history) > 22 { |
| 343 | + t.Errorf("history not compacted: len=%d, want <=22", len(a.history)) |
| 344 | + } |
| 345 | +} |
| 346 | + |
| 347 | +func TestLoop_RateLimiterContextCancel(t *testing.T) { |
| 348 | + mp := &mockProvider{ |
| 349 | + responses: []*provider.ChatResponse{ |
| 350 | + {Content: []provider.ContentBlock{{Type: provider.ContentText, Text: "ok"}}, StopReason: provider.StopEndTurn}, |
| 351 | + }, |
| 352 | + } |
| 353 | + exec := &mockExecutor{results: map[string]string{}} |
| 354 | + |
| 355 | + // Create agent with extremely low rate limit (1/hour). |
| 356 | + a := New(mp, "test-model", exec, WithRateLimit(1)) |
| 357 | + |
| 358 | + // First call uses burst, should succeed. |
| 359 | + _, err := a.Run(context.Background(), "first") |
| 360 | + if err != nil { |
| 361 | + t.Fatalf("first Run: %v", err) |
| 362 | + } |
| 363 | + |
| 364 | + // Exhaust remaining burst by calling multiple times. |
| 365 | + for range 9 { |
| 366 | + mp.idx = 0 |
| 367 | + mp.requests = nil |
| 368 | + a.history = nil |
| 369 | + _, _ = a.Run(context.Background(), "burst") |
| 370 | + } |
| 371 | + |
| 372 | + // Now cancel context; should fail on rate limiter. |
| 373 | + ctx, cancel := context.WithCancel(context.Background()) |
| 374 | + cancel() |
| 375 | + mp.idx = 0 |
| 376 | + mp.requests = nil |
| 377 | + a.history = nil |
| 378 | + _, err = a.Run(ctx, "should fail") |
| 379 | + if err == nil { |
| 380 | + t.Fatal("expected rate limiter error") |
| 381 | + } |
| 382 | + if !strings.Contains(err.Error(), "rate limiter") { |
| 383 | + t.Errorf("error = %q, expected rate limiter error", err.Error()) |
| 384 | + } |
| 385 | +} |
0 commit comments