Optimize agent planning and subagent runtime

This commit is contained in:
lpf
2026-03-09 12:33:00 +08:00
parent acf8a22c0a
commit d1abd73e63
11 changed files with 984 additions and 108 deletions

View File

@@ -1,8 +1,10 @@
package agent
import (
"context"
"strings"
"testing"
"time"
"clawgo/pkg/bus"
)
@@ -66,3 +68,92 @@ func TestPrepareOutboundSubagentNoReplyFallbackWithMissingOrigin(t *testing.T) {
t.Fatalf("unexpected fallback content: %q", outbound.Content)
}
}
func TestProcessSystemMessageSubagentBlockedQueuedIntoDigest(t *testing.T) {
msgBus := bus.NewMessageBus()
al := &AgentLoop{
bus: msgBus,
subagentDigestDelay: 10 * time.Millisecond,
subagentDigests: map[string]*subagentDigestState{},
}
out, err := al.processSystemMessage(context.Background(), bus.InboundMessage{
Channel: "system",
SenderID: "subagent:subagent-3",
ChatID: "telegram:9527",
Content: "Subagent update\nagent: coder\nrun: subagent-3\nstatus: blocked\nreason: blocked\ntask: 修复登录\nsummary: rate limit",
Metadata: map[string]string{
"trigger": "subagent",
"agent_id": "coder",
"status": "failed",
"notify_reason": "blocked",
},
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out != "" {
t.Fatalf("expected queued digest with no immediate output, got %q", out)
}
al.flushDueSubagentDigests(time.Now().Add(20 * time.Millisecond))
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
outbound, ok := msgBus.SubscribeOutbound(ctx)
if !ok {
t.Fatalf("expected outbound digest")
}
if !strings.Contains(outbound.Content, "阶段总结") || !strings.Contains(outbound.Content, "coder") || !strings.Contains(outbound.Content, "受阻") {
t.Fatalf("unexpected digest content: %q", outbound.Content)
}
}
func TestProcessSystemMessageSubagentDigestMergesMultipleUpdates(t *testing.T) {
msgBus := bus.NewMessageBus()
al := &AgentLoop{
bus: msgBus,
subagentDigestDelay: 10 * time.Millisecond,
subagentDigests: map[string]*subagentDigestState{},
}
first := bus.InboundMessage{
Channel: "system",
SenderID: "subagent:subagent-7",
ChatID: "telegram:9527",
Content: "Subagent update\nagent: tester\nrun: subagent-7\nstatus: completed\nreason: final\ntask: 回归测试\nsummary: 所有测试通过",
Metadata: map[string]string{
"trigger": "subagent",
"agent_id": "tester",
"status": "completed",
"notify_reason": "final",
},
}
second := bus.InboundMessage{
Channel: "system",
SenderID: "subagent:subagent-8",
ChatID: "telegram:9527",
Content: "Subagent update\nagent: coder\nrun: subagent-8\nstatus: completed\nreason: final\ntask: 修复登录\nsummary: 接口已联调",
Metadata: map[string]string{
"trigger": "subagent",
"agent_id": "coder",
"status": "completed",
"notify_reason": "final",
},
}
if out, err := al.processSystemMessage(context.Background(), first); err != nil || out != "" {
t.Fatalf("unexpected first result out=%q err=%v", out, err)
}
if out, err := al.processSystemMessage(context.Background(), second); err != nil || out != "" {
t.Fatalf("unexpected second result out=%q err=%v", out, err)
}
al.flushDueSubagentDigests(time.Now().Add(20 * time.Millisecond))
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
outbound, ok := msgBus.SubscribeOutbound(ctx)
if !ok {
t.Fatalf("expected merged outbound digest")
}
if strings.Count(outbound.Content, "\n- ") != 2 {
t.Fatalf("expected two digest lines, got: %q", outbound.Content)
}
if !strings.Contains(outbound.Content, "完成 2") {
t.Fatalf("expected aggregate completion count, got: %q", outbound.Content)
}
}