Fix config reload and subagent config feedback

This commit is contained in:
lpf
2026-03-06 18:45:43 +08:00
parent 61c46fd710
commit 623b401850
3 changed files with 168 additions and 150 deletions

View File

@@ -1,71 +1,43 @@
package agent
import (
"context"
"path/filepath"
"strings"
"testing"
"clawgo/pkg/bus"
"clawgo/pkg/config"
"clawgo/pkg/runtimecfg"
)
func TestMaybeHandleSubagentConfigIntentCreatePersistsImmediately(t *testing.T) {
workspace := t.TempDir()
configPath := filepath.Join(workspace, "config.json")
cfg := config.DefaultConfig()
cfg.Agents.Router.Enabled = true
cfg.Agents.Subagents["main"] = config.SubagentConfig{
Enabled: true,
Type: "router",
Role: "orchestrator",
SystemPromptFile: "agents/main/AGENT.md",
}
if err := config.SaveConfig(configPath, cfg); err != nil {
t.Fatalf("save config failed: %v", err)
}
runtimecfg.Set(cfg)
t.Cleanup(func() { runtimecfg.Set(config.DefaultConfig()) })
func TestFormatCreatedSubagentForUserReadsNestedFields(t *testing.T) {
t.Parallel()
loop := &AgentLoop{configPath: configPath}
out, handled, err := loop.maybeHandleSubagentConfigIntent(context.Background(), bus.InboundMessage{
SessionKey: "main",
Channel: "cli",
Content: "创建一个负责回归测试和验证修复结果的子代理",
})
if err != nil {
t.Fatalf("create subagent failed: %v", err)
}
if !handled || !strings.Contains(out, "已写入 config.json") {
t.Fatalf("expected immediate persist response, got handled=%v out=%q", handled, out)
}
if !strings.Contains(out, configPath) {
t.Fatalf("expected response to include config path, got %q", out)
}
out := formatCreatedSubagentForUser(map[string]interface{}{
"agent_id": "coder",
"subagent": map[string]interface{}{
"role": "coding",
"display_name": "Code Agent",
"system_prompt_file": "agents/coder/AGENT.md",
"tools": map[string]interface{}{
"allowlist": []interface{}{"filesystem", "shell"},
},
},
"rules": []interface{}{
map[string]interface{}{
"agent_id": "coder",
"keywords": []interface{}{"code", "fix"},
},
},
}, "/tmp/config.json")
reloaded, err := config.LoadConfig(configPath)
if err != nil {
t.Fatalf("reload config failed: %v", err)
}
if _, ok := reloaded.Agents.Subagents["tester"]; !ok {
t.Fatalf("expected tester subagent to persist, got %+v", reloaded.Agents.Subagents)
}
}
func TestMaybeHandleSubagentConfigIntentConfirmCancelNoLongerHandled(t *testing.T) {
loop := &AgentLoop{}
for _, content := range []string{"确认创建", "取消创建"} {
out, handled, err := loop.maybeHandleSubagentConfigIntent(context.Background(), bus.InboundMessage{
SessionKey: "main",
Channel: "cli",
Content: content,
})
if err != nil {
t.Fatalf("unexpected error for %q: %v", content, err)
}
if handled || out != "" {
t.Fatalf("expected %q to pass through, got handled=%v out=%q", content, handled, out)
for _, want := range []string{
"agent_id: coder",
"role: coding",
"display_name: Code Agent",
"system_prompt_file: agents/coder/AGENT.md",
"routing_keywords: [code fix]",
} {
if !strings.Contains(out, want) {
t.Fatalf("expected output to contain %q, got:\n%s", want, out)
}
}
if strings.Contains(out, "<nil>") {
t.Fatalf("did not expect nil placeholders, got:\n%s", out)
}
}