From d825822625bdfb215e130ba635d660b9b68b9592 Mon Sep 17 00:00:00 2001 From: huaiyj <8699003+huaiyj@user.noreply.gitee.com> Date: Mon, 24 Aug 2026 21:11:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=A1=AE=E8=AE=A4?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=20ID=20=E5=B9=B6=E5=8F=91=E7=AB=9E=E4=BA=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/agent/confirmation_handler.go | 6 ++- .../agent/confirmation_handler_id_test.go | 37 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 internal/agent/confirmation_handler_id_test.go diff --git a/internal/agent/confirmation_handler.go b/internal/agent/confirmation_handler.go index da2d583..ceb5172 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 0000000..e163964 --- /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) + } +} -- Gitee