fix(message): preserve same-round dedup by resetting only once per round

This commit is contained in:
mosir 2026-02-28 12:47:38 +08:00
parent a49803ec90
commit f34ca8369b
3 changed files with 39 additions and 1 deletions

View file

@ -879,6 +879,9 @@ func (al *AgentLoop) runLLMIteration(
func (al *AgentLoop) updateToolContexts(agent *AgentInstance, channel, chatID string) {
// Use ContextualTool interface instead of type assertions
if tool, ok := agent.Tools.Get("message"); ok {
if rt, ok := tool.(interface{ BeginRound() }); ok {
rt.BeginRound()
}
if mt, ok := tool.(tools.ContextualTool); ok {
mt.SetContext(channel, chatID)
}

View file

@ -53,7 +53,12 @@ func (t *MessageTool) Parameters() map[string]any {
func (t *MessageTool) SetContext(channel, chatID string) {
t.defaultChannel = channel
t.defaultChatID = chatID
t.sentInRound = false // Reset send tracking for new processing round
}
// BeginRound resets per-round send tracking.
// AgentLoop calls this once per inbound message before tool iterations begin.
func (t *MessageTool) BeginRound() {
t.sentInRound = false
t.lastChannel = ""
t.lastChatID = ""
t.lastContent = ""

View file

@ -226,6 +226,36 @@ func TestMessageTool_Execute_SuppressDuplicateInSameRound(t *testing.T) {
}
}
func TestMessageTool_Execute_AllowsSameContentAfterBeginRound(t *testing.T) {
tool := NewMessageTool()
tool.SetContext("test-channel", "test-chat-id")
callCount := 0
tool.SetSendCallback(func(channel, chatID, content string) error {
callCount++
return nil
})
ctx := context.Background()
args := map[string]any{"content": "same message"}
first := tool.Execute(ctx, args)
if first.IsError || !first.Silent {
t.Fatalf("first result unexpected: %+v", first)
}
tool.BeginRound()
tool.SetContext("test-channel", "test-chat-id")
second := tool.Execute(ctx, args)
if second.IsError || !second.Silent {
t.Fatalf("second result unexpected: %+v", second)
}
if callCount != 2 {
t.Fatalf("send callback call count = %d, want 2", callCount)
}
}
func TestMessageTool_Name(t *testing.T) {
tool := NewMessageTool()
if tool.Name() != "message" {