Tighten subagent prompt file workflow

This commit is contained in:
lpf
2026-03-06 14:12:01 +08:00
parent 49352612ea
commit 5e421bb730
15 changed files with 542 additions and 58 deletions

View File

@@ -55,9 +55,10 @@ func TestHandleSubagentRuntimeUpsertConfigSubagent(t *testing.T) {
cfg := config.DefaultConfig()
cfg.Agents.Router.Enabled = true
cfg.Agents.Subagents["main"] = config.SubagentConfig{
Enabled: true,
Type: "router",
Role: "orchestrator",
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)
@@ -172,9 +173,10 @@ func TestHandleSubagentRuntimeConfirmPendingDraft(t *testing.T) {
cfg := config.DefaultConfig()
cfg.Agents.Router.Enabled = true
cfg.Agents.Subagents["main"] = config.SubagentConfig{
Enabled: true,
Type: "router",
Role: "orchestrator",
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)
@@ -187,7 +189,7 @@ func TestHandleSubagentRuntimeConfirmPendingDraft(t *testing.T) {
configPath: configPath,
subagentManager: manager,
subagentRouter: tools.NewSubagentRouter(manager),
pendingSubagentDraft: map[string]map[string]interface{}{"main": {"agent_id": "tester", "role": "testing", "type": "worker"}},
pendingSubagentDraft: map[string]map[string]interface{}{"main": {"agent_id": "tester", "role": "testing", "type": "worker", "system_prompt_file": "agents/tester/AGENT.md"}},
}
out, err := loop.HandleSubagentRuntime(context.Background(), "confirm_pending_draft", map[string]interface{}{"session_key": "main"})
if err != nil {
@@ -212,17 +214,19 @@ func TestHandleSubagentRuntimeRegistryAndToggleEnabled(t *testing.T) {
cfg := config.DefaultConfig()
cfg.Agents.Router.Enabled = true
cfg.Agents.Subagents["main"] = config.SubagentConfig{
Enabled: true,
Type: "router",
Role: "orchestrator",
Enabled: true,
Type: "router",
Role: "orchestrator",
SystemPromptFile: "agents/main/AGENT.md",
}
cfg.Agents.Subagents["tester"] = config.SubagentConfig{
Enabled: true,
Type: "worker",
Role: "testing",
DisplayName: "Test Agent",
SystemPrompt: "run tests",
MemoryNamespace: "tester",
Enabled: true,
Type: "worker",
Role: "testing",
DisplayName: "Test Agent",
SystemPrompt: "run tests",
SystemPromptFile: "agents/tester/AGENT.md",
MemoryNamespace: "tester",
Tools: config.SubagentToolsConfig{
Allowlist: []string{"shell", "sessions"},
},
@@ -275,14 +279,16 @@ func TestHandleSubagentRuntimeDeleteConfigSubagent(t *testing.T) {
cfg := config.DefaultConfig()
cfg.Agents.Router.Enabled = true
cfg.Agents.Subagents["main"] = config.SubagentConfig{
Enabled: true,
Type: "router",
Role: "orchestrator",
Enabled: true,
Type: "router",
Role: "orchestrator",
SystemPromptFile: "agents/main/AGENT.md",
}
cfg.Agents.Subagents["tester"] = config.SubagentConfig{
Enabled: true,
Type: "worker",
Role: "testing",
Enabled: true,
Type: "worker",
Role: "testing",
SystemPromptFile: "agents/tester/AGENT.md",
}
cfg.Agents.Router.Rules = []config.AgentRouteRule{{AgentID: "tester", Keywords: []string{"test"}}}
if err := config.SaveConfig(configPath, cfg); err != nil {
@@ -316,3 +322,97 @@ func TestHandleSubagentRuntimeDeleteConfigSubagent(t *testing.T) {
t.Fatalf("expected tester route rule to be removed")
}
}
func TestHandleSubagentRuntimePromptFileGetSetBootstrap(t *testing.T) {
workspace := t.TempDir()
manager := tools.NewSubagentManager(nil, workspace, nil, nil)
loop := &AgentLoop{
workspace: workspace,
subagentManager: manager,
subagentRouter: tools.NewSubagentRouter(manager),
}
out, err := loop.HandleSubagentRuntime(context.Background(), "prompt_file_get", map[string]interface{}{
"path": "agents/coder/AGENT.md",
})
if err != nil {
t.Fatalf("prompt_file_get failed: %v", err)
}
payload, ok := out.(map[string]interface{})
if !ok || payload["found"] != false {
t.Fatalf("expected missing prompt file, got %#v", out)
}
out, err = loop.HandleSubagentRuntime(context.Background(), "prompt_file_bootstrap", map[string]interface{}{
"agent_id": "coder",
"role": "coding",
})
if err != nil {
t.Fatalf("prompt_file_bootstrap failed: %v", err)
}
payload, ok = out.(map[string]interface{})
if !ok || payload["created"] != true {
t.Fatalf("expected prompt file bootstrap to create file, got %#v", out)
}
out, err = loop.HandleSubagentRuntime(context.Background(), "prompt_file_set", map[string]interface{}{
"path": "agents/coder/AGENT.md",
"content": "# coder\nupdated",
})
if err != nil {
t.Fatalf("prompt_file_set failed: %v", err)
}
payload, ok = out.(map[string]interface{})
if !ok || payload["ok"] != true {
t.Fatalf("expected prompt_file_set ok, got %#v", out)
}
out, err = loop.HandleSubagentRuntime(context.Background(), "prompt_file_get", map[string]interface{}{
"path": "agents/coder/AGENT.md",
})
if err != nil {
t.Fatalf("prompt_file_get after set failed: %v", err)
}
payload, ok = out.(map[string]interface{})
if !ok || payload["found"] != true || payload["content"] != "# coder\nupdated" {
t.Fatalf("unexpected prompt file payload: %#v", out)
}
}
func TestHandleSubagentRuntimeProtectsMainAgent(t *testing.T) {
workspace := t.TempDir()
configPath := filepath.Join(workspace, "config.json")
cfg := config.DefaultConfig()
cfg.Agents.Router.Enabled = true
cfg.Agents.Router.MainAgentID = "main"
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()) })
manager := tools.NewSubagentManager(nil, workspace, nil, nil)
loop := &AgentLoop{
configPath: configPath,
workspace: workspace,
subagentManager: manager,
subagentRouter: tools.NewSubagentRouter(manager),
}
if _, err := loop.HandleSubagentRuntime(context.Background(), "set_config_subagent_enabled", map[string]interface{}{
"agent_id": "main",
"enabled": false,
}); err == nil {
t.Fatalf("expected disabling main agent to fail")
}
if _, err := loop.HandleSubagentRuntime(context.Background(), "delete_config_subagent", map[string]interface{}{
"agent_id": "main",
}); err == nil {
t.Fatalf("expected deleting main agent to fail")
}
}