mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-04-28 09:57:29 +08:00
fix subagent completion notify path and pipeline origin routing
This commit is contained in:
@@ -53,6 +53,14 @@ func (t *PipelineCreateTool) Parameters() map[string]interface{} {
|
||||
"required": []string{"id", "goal"},
|
||||
},
|
||||
},
|
||||
"channel": map[string]interface{}{
|
||||
"type": "string",
|
||||
"description": "Optional origin channel for completion notifications (auto-injected in normal chat flow)",
|
||||
},
|
||||
"chat_id": map[string]interface{}{
|
||||
"type": "string",
|
||||
"description": "Optional origin chat ID for completion notifications (auto-injected in normal chat flow)",
|
||||
},
|
||||
},
|
||||
"required": []string{"objective", "tasks"},
|
||||
}
|
||||
@@ -97,7 +105,8 @@ func (t *PipelineCreateTool) Execute(_ context.Context, args map[string]interfac
|
||||
})
|
||||
}
|
||||
|
||||
p, err := t.orc.CreatePipeline(label, objective, "tool", "tool", specs)
|
||||
originChannel, originChatID := resolvePipelineOrigin(args, "tool", "tool")
|
||||
p, err := t.orc.CreatePipeline(label, objective, originChannel, originChatID, specs)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -227,6 +236,14 @@ func (t *PipelineDispatchTool) Parameters() map[string]interface{} {
|
||||
"description": "Maximum number of tasks to dispatch in this call (default 3)",
|
||||
"default": 3,
|
||||
},
|
||||
"channel": map[string]interface{}{
|
||||
"type": "string",
|
||||
"description": "Optional origin channel override for spawned subagents",
|
||||
},
|
||||
"chat_id": map[string]interface{}{
|
||||
"type": "string",
|
||||
"description": "Optional origin chat ID override for spawned subagents",
|
||||
},
|
||||
},
|
||||
"required": []string{"pipeline_id"},
|
||||
}
|
||||
@@ -247,6 +264,21 @@ func (t *PipelineDispatchTool) Execute(ctx context.Context, args map[string]inte
|
||||
if raw, ok := args["max_dispatch"].(float64); ok && raw > 0 {
|
||||
maxDispatch = int(raw)
|
||||
}
|
||||
originChannel, originChatID := resolvePipelineOrigin(args, "", "")
|
||||
if p, ok := t.orc.GetPipeline(pipelineID); ok && p != nil {
|
||||
if strings.TrimSpace(originChannel) == "" {
|
||||
originChannel = strings.TrimSpace(p.OriginChannel)
|
||||
}
|
||||
if strings.TrimSpace(originChatID) == "" {
|
||||
originChatID = strings.TrimSpace(p.OriginChatID)
|
||||
}
|
||||
}
|
||||
if strings.TrimSpace(originChannel) == "" {
|
||||
originChannel = "tool"
|
||||
}
|
||||
if strings.TrimSpace(originChatID) == "" {
|
||||
originChatID = "tool"
|
||||
}
|
||||
|
||||
ready, err := t.orc.ReadyTasks(pipelineID)
|
||||
if err != nil {
|
||||
@@ -289,8 +321,8 @@ func (t *PipelineDispatchTool) Execute(ctx context.Context, args map[string]inte
|
||||
Label: label,
|
||||
Role: task.Role,
|
||||
AgentID: agentID,
|
||||
OriginChannel: "tool",
|
||||
OriginChatID: "tool",
|
||||
OriginChannel: originChannel,
|
||||
OriginChatID: originChatID,
|
||||
PipelineID: pipelineID,
|
||||
PipelineTask: task.ID,
|
||||
}); err != nil {
|
||||
@@ -306,3 +338,17 @@ func (t *PipelineDispatchTool) Execute(ctx context.Context, args map[string]inte
|
||||
}
|
||||
return fmt.Sprintf("Pipeline %s dispatch result:\n%s", pipelineID, strings.Join(lines, "\n")), nil
|
||||
}
|
||||
|
||||
func resolvePipelineOrigin(args map[string]interface{}, defaultChannel, defaultChatID string) (string, string) {
|
||||
originChannel, _ := args["channel"].(string)
|
||||
originChatID, _ := args["chat_id"].(string)
|
||||
originChannel = strings.TrimSpace(originChannel)
|
||||
originChatID = strings.TrimSpace(originChatID)
|
||||
if originChannel == "" {
|
||||
originChannel = strings.TrimSpace(defaultChannel)
|
||||
}
|
||||
if originChatID == "" {
|
||||
originChatID = strings.TrimSpace(defaultChatID)
|
||||
}
|
||||
return originChannel, originChatID
|
||||
}
|
||||
|
||||
@@ -275,8 +275,15 @@ func (sm *SubagentManager) runTask(ctx context.Context, task *SubagentTask) {
|
||||
// 2. Result broadcast (keep existing behavior)
|
||||
if sm.bus != nil {
|
||||
prefix := "Task completed"
|
||||
if runErr != nil {
|
||||
prefix = "Task failed"
|
||||
}
|
||||
if task.Label != "" {
|
||||
prefix = fmt.Sprintf("Task '%s' completed", task.Label)
|
||||
if runErr != nil {
|
||||
prefix = fmt.Sprintf("Task '%s' failed", task.Label)
|
||||
} else {
|
||||
prefix = fmt.Sprintf("Task '%s' completed", task.Label)
|
||||
}
|
||||
}
|
||||
announceContent := fmt.Sprintf("%s.\n\nResult:\n%s", prefix, task.Result)
|
||||
if task.PipelineID != "" && task.PipelineTask != "" {
|
||||
@@ -299,6 +306,7 @@ func (sm *SubagentManager) runTask(ctx context.Context, task *SubagentTask) {
|
||||
"timeout_sec": fmt.Sprintf("%d", task.TimeoutSec),
|
||||
"pipeline_id": task.PipelineID,
|
||||
"pipeline_task": task.PipelineTask,
|
||||
"status": task.Status,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,8 +3,11 @@ package tools
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"clawgo/pkg/bus"
|
||||
)
|
||||
|
||||
func TestSubagentSpawnEnforcesTaskQuota(t *testing.T) {
|
||||
@@ -105,6 +108,45 @@ func TestSubagentRunWithTimeoutFails(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubagentBroadcastIncludesFailureStatus(t *testing.T) {
|
||||
workspace := t.TempDir()
|
||||
msgBus := bus.NewMessageBus()
|
||||
defer msgBus.Close()
|
||||
|
||||
manager := NewSubagentManager(nil, workspace, msgBus, nil)
|
||||
manager.SetRunFunc(func(ctx context.Context, task *SubagentTask) (string, error) {
|
||||
return "", errors.New("boom")
|
||||
})
|
||||
|
||||
_, err := manager.Spawn(context.Background(), SubagentSpawnOptions{
|
||||
Task: "failing task",
|
||||
AgentID: "coder",
|
||||
OriginChannel: "cli",
|
||||
OriginChatID: "direct",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("spawn failed: %v", err)
|
||||
}
|
||||
|
||||
task := waitSubagentDone(t, manager, 4*time.Second)
|
||||
if task.Status != "failed" {
|
||||
t.Fatalf("expected failed task, got %s", task.Status)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
msg, ok := msgBus.ConsumeInbound(ctx)
|
||||
if !ok {
|
||||
t.Fatalf("expected subagent completion message")
|
||||
}
|
||||
if got := strings.TrimSpace(msg.Metadata["status"]); got != "failed" {
|
||||
t.Fatalf("expected metadata status=failed, got %q", got)
|
||||
}
|
||||
if !strings.Contains(strings.ToLower(msg.Content), "failed") {
|
||||
t.Fatalf("expected failure wording in content, got %q", msg.Content)
|
||||
}
|
||||
}
|
||||
|
||||
func waitSubagentDone(t *testing.T, manager *SubagentManager, timeout time.Duration) *SubagentTask {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(timeout)
|
||||
|
||||
Reference in New Issue
Block a user