Skip to content

Commit e0c99e3

Browse files
committed
perf: make agent history thread safe
1 parent 4a33754 commit e0c99e3

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

agent/agent.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package agent
33
import (
44
"encoding/json"
55
"strings"
6+
"sync"
67

78
"github.com/voocel/mas/llm"
89
"github.com/voocel/mas/runtime"
@@ -132,6 +133,7 @@ type BaseAgent struct {
132133
executor *tools.Executor
133134
history []schema.Message
134135
capabilities *AgentCapabilities
136+
historyMu sync.RWMutex
135137
}
136138

137139
// NewAgent creates a new agent.
@@ -408,12 +410,20 @@ func (a *BaseAgent) buildMessages() []schema.Message {
408410
messages = append(messages, systemMsg)
409411
}
410412

411-
messages = append(messages, a.history...)
413+
a.historyMu.RLock()
414+
historyCopy := make([]schema.Message, len(a.history))
415+
copy(historyCopy, a.history)
416+
a.historyMu.RUnlock()
417+
418+
messages = append(messages, historyCopy...)
412419

413420
return messages
414421
}
415422

416423
func (a *BaseAgent) addToHistory(message schema.Message) {
424+
a.historyMu.Lock()
425+
defer a.historyMu.Unlock()
426+
417427
a.history = append(a.history, message)
418428
if len(a.history) > a.config.MaxHistory {
419429
// Keep recent messages.
@@ -422,11 +432,15 @@ func (a *BaseAgent) addToHistory(message schema.Message) {
422432
}
423433

424434
func (a *BaseAgent) ClearHistory() {
435+
a.historyMu.Lock()
436+
defer a.historyMu.Unlock()
425437
a.history = make([]schema.Message, 0)
426438
}
427439

428440
func (a *BaseAgent) GetHistory() []schema.Message {
429441
// Return a copy to prevent external modification.
442+
a.historyMu.RLock()
443+
defer a.historyMu.RUnlock()
430444
history := make([]schema.Message, len(a.history))
431445
copy(history, a.history)
432446
return history

tools/executor.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,18 @@ func (e *Executor) ExecuteParallel(ctx runtime.Context, toolCalls []schema.ToolC
113113
results := make([]schema.ToolResult, len(toolCalls))
114114
errChan := make(chan error, len(toolCalls))
115115
var wg sync.WaitGroup
116+
maxConcurrency := e.config.MaxConcurrency
117+
if maxConcurrency <= 0 || maxConcurrency > len(toolCalls) {
118+
maxConcurrency = len(toolCalls)
119+
}
120+
sem := make(chan struct{}, maxConcurrency)
116121

117122
for i, toolCall := range toolCalls {
118123
wg.Add(1)
119124
go func(idx int, tc schema.ToolCall) {
120125
defer wg.Done()
126+
sem <- struct{}{}
127+
defer func() { <-sem }()
121128

122129
result, err := e.Execute(ctx, tc)
123130
results[idx] = result

0 commit comments

Comments
 (0)