diff --git a/internal/agent/confirmation_handler.go b/internal/agent/confirmation_handler.go index da2d583de3ff9a637a1d5c9e9f5fdfc54d5ca597..ceb5172ea679579acffbba6fc5811ab157414376 100644 --- a/internal/agent/confirmation_handler.go +++ b/internal/agent/confirmation_handler.go @@ -108,8 +108,11 @@ func (h *AsyncConfirmationHandler) AddSubAgentTool(toolName string) { // generateID generates a unique request ID func (h *AsyncConfirmationHandler) generateID() string { + h.mu.Lock() h.nextID++ - return fmt.Sprintf("confirm-%d-%d", time.Now().UnixNano(), h.nextID) + nextID := h.nextID + h.mu.Unlock() + return fmt.Sprintf("confirm-%d-%d", time.Now().UnixNano(), nextID) } // RequestConfirmation requests user confirmation for a tool call @@ -623,4 +626,3 @@ func extractToolContent(toolCall *tools.ToolCall) string { } return "" } - diff --git a/internal/agent/confirmation_handler_id_test.go b/internal/agent/confirmation_handler_id_test.go new file mode 100644 index 0000000000000000000000000000000000000000..e1639640bb7ec12434c015e33c335b8bb6f8d5e3 --- /dev/null +++ b/internal/agent/confirmation_handler_id_test.go @@ -0,0 +1,37 @@ +package agent + +import ( + "sync" + "testing" +) + +func TestGenerateIDConcurrent(t *testing.T) { + const count = 1000 + handler := NewAsyncConfirmationHandler(nil) + ids := make(chan string, count) + var wg sync.WaitGroup + + for range count { + wg.Add(1) + go func() { + defer wg.Done() + ids <- handler.generateID() + }() + } + wg.Wait() + close(ids) + + seen := make(map[string]struct{}, count) + for id := range ids { + if _, exists := seen[id]; exists { + t.Fatalf("duplicate confirmation ID generated: %s", id) + } + seen[id] = struct{}{} + } + if len(seen) != count { + t.Fatalf("generated %d unique IDs, want %d", len(seen), count) + } + if handler.nextID != count { + t.Fatalf("nextID = %d, want %d", handler.nextID, count) + } +}