Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

【架构优化】合并 dispatchRoomIssue/dispatchToIssue 中重复的多 SOP 派发目标拼接为 roomDispatchTarget (#823)

fx-kurumia594c4e5287a
diff --git a/internal/httpapi/room_dispatch_target_wiring_test.go b/internal/httpapi/room_dispatch_target_wiring_test.go
new file mode 100644
index 00000000..eab73de1
--- /dev/null
+++ b/internal/httpapi/room_dispatch_target_wiring_test.go
@@ -0,0 +1,106 @@
+package httpapi
+
+import (
+ "net/http"
+ "path/filepath"
+ "testing"
+
+ "agentflow/internal/domain"
+ "agentflow/internal/engine"
+ "agentflow/internal/repo"
+ "agentflow/internal/runtime"
+ "agentflow/internal/tracker"
+)
+
+// TestRoomDispatchTargetReachesIssueAndCards pins the two callers of
+// roomDispatchTarget: the issue's create operation and every dispatch card must
+// carry the same multi-SOP target, keyed by the first SOP, and a single-SOP
+// dispatch onto an existing issue must stay an "sop" target.
+func TestRoomDispatchTargetReachesIssueAndCards(t *testing.T) {
+ t.Setenv("AGENTFLOW_SESSION_SECRET", "room-test-session-secret")
+ root := t.TempDir()
+ s, err := repo.OpenTest()
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { s.Close() })
+ for _, wf := range []domain.WorkflowDefinition{
+ {ID: "target-build", Name: "构建"},
+ {ID: "target-review", Name: "评审"},
+ {ID: "target-ship", Name: "上线"},
+ } {
+ wf.Version, wf.ProjectID, wf.Status, wf.SandboxProfileID = 1, "platform", "published", "local"
+ wf.Nodes = []domain.WorkflowNode{{ID: "approval", Name: "人工确认", Type: "approval", MaxAttempts: 1}}
+ if err := s.PutWorkflow(wf); err != nil {
+ t.Fatal(err)
+ }
+ }
+ e := engine.New(s, runtime.NewManager(filepath.Join(root, "workspaces")))
+ h := New(s, e, tracker.NewService(), "", "")
+ owner, _ := s.User(1)
+ room := requestJSONAuthed[domain.ChatRoomView](t, h, owner, http.MethodPost, "/api/rooms", map[string]any{
+ "projectId": "platform", "name": "派发目标群",
+ }, http.StatusCreated)
+ cancelRuns := func(res roomIssueResult) {
+ for _, task := range res.Tasks {
+ runID := task.RunID
+ t.Cleanup(func() { _ = e.Cancel(runID) })
+ }
+ }
+
+ const multiName = "SOP「构建」、「评审」、「上线」"
+ multi := requestJSONAuthed[roomIssueResult](t, h, owner, http.MethodPost, "/api/rooms/"+room.ID+"/issues", map[string]any{
+ "title": "三条流程并行", "workflowIds": []string{"target-build", "target-review", "target-ship"},
+ }, http.StatusCreated)
+ cancelRuns(multi)
+ if multi.Issue.WorkflowID != "target-build" {
+ t.Fatalf("new issue should bind the first SOP: %q", multi.Issue.WorkflowID)
+ }
+ stored, ok := s.Issue(multi.Issue.ID)
+ if !ok || len(stored.Operations) == 0 {
+ t.Fatalf("issue should be stored with operations: %+v %v", stored, ok)
+ }
+ if create := stored.Operations[0]; create.Action != "create" || create.Detail != "从协作群「派发目标群」建单,派给"+multiName {
+ t.Fatalf("create operation should name every SOP: %+v", create)
+ }
+ if len(multi.Messages) != 3 {
+ t.Fatalf("one dispatch card per SOP: %+v", multi.Messages)
+ }
+ for i, msg := range multi.Messages {
+ if msg.TargetType != "multi" || msg.TargetID != "target-build" || msg.TargetName != multiName {
+ t.Fatalf("card %d target = (%q, %q, %q)", i, msg.TargetType, msg.TargetID, msg.TargetName)
+ }
+ }
+
+ // Dispatching onto the existing issue skips the create operation but the
+ // cards still describe the target the same way.
+ again := requestJSONAuthed[roomIssueResult](t, h, owner, http.MethodPost, "/api/rooms/"+room.ID+"/issues", map[string]any{
+ "issueId": multi.Issue.ID, "workflowIds": []string{"target-ship", "target-review"},
+ }, http.StatusCreated)
+ cancelRuns(again)
+ if len(again.Messages) != 2 {
+ t.Fatalf("one card per SOP on the existing issue: %+v", again.Messages)
+ }
+ for i, msg := range again.Messages {
+ if msg.TargetType != "multi" || msg.TargetID != "target-ship" || msg.TargetName != "SOP「上线」、「评审」" {
+ t.Fatalf("existing-issue card %d target = (%q, %q, %q)", i, msg.TargetType, msg.TargetID, msg.TargetName)
+ }
+ }
+ single := requestJSONAuthed[roomIssueResult](t, h, owner, http.MethodPost, "/api/rooms/"+room.ID+"/issues", map[string]any{
+ "issueId": multi.Issue.ID, "workflowId": "target-review",
+ }, http.StatusCreated)
+ cancelRuns(single)
+ if msg := single.Message; msg.TargetType != "sop" || msg.TargetID != "target-review" || msg.TargetName != "SOP「评审」" {
+ t.Fatalf("single-SOP card target = (%q, %q, %q)", msg.TargetType, msg.TargetID, msg.TargetName)
+ }
+ latest, _ := s.Issue(multi.Issue.ID)
+ creates := 0
+ for _, op := range latest.Operations {
+ if op.Action == "create" {
+ creates++
+ }
+ }
+ if creates != 1 {
+ t.Fatalf("dispatching onto an existing issue must not add create operations: %+v", latest.Operations)
+ }
+}
diff --git a/internal/httpapi/rooms.go b/internal/httpapi/rooms.go
index 06a1244d..0e90a190 100644
--- a/internal/httpapi/rooms.go
+++ b/internal/httpapi/rooms.go
@@ -249,7 +249,6 @@ func (h *roomsHandlers) dispatchRoomIssue(c *gin.Context, room domain.ChatRoom,
}
workflowNames[i] = wf.Name
}
- firstWorkflowID, firstWorkflowName := workflowIDs[0], workflowNames[0]
userID, author := h.roomActor(c)
if id := strings.TrimSpace(req.IssueID); id != "" {
existing, found := h.repo.Issue(id)
@@ -268,19 +267,12 @@ func (h *roomsHandlers) dispatchRoomIssue(c *gin.Context, room domain.ChatRoom,
Priority: "medium",
Kind: "issue",
Source: "local",
- WorkflowID: firstWorkflowID,
+ WorkflowID: workflowIDs[0],
}
if userID > 0 {
issue.AssigneeUserID = userID
}
- _, _, targetName := roomTarget(firstWorkflowID, firstWorkflowName, "", "")
- if len(workflowIDs) > 1 {
- quoted := make([]string, len(workflowNames))
- for i, name := range workflowNames {
- quoted[i] = "「" + fallbackName(name, workflowIDs[i]) + "」"
- }
- targetName = "SOP" + strings.Join(quoted, "、")
- }
+ _, _, targetName := roomDispatchTarget(workflowIDs, workflowNames)
issue.AppendOperation(domain.IssueOperation{
Actor: author, ActorUserID: userID, Action: "create",
Detail: "从协作群「" + room.Name + "」建单,派给" + targetName,
@@ -296,14 +288,7 @@ func (h *roomsHandlers) dispatchRoomIssue(c *gin.Context, room domain.ChatRoom,
// dispatchToIssue starts each workflow on stored and records one dispatch
// message per lane on the room timeline.
func (h *roomsHandlers) dispatchToIssue(c *gin.Context, room domain.ChatRoom, stored domain.Issue, description string, workflowIDs, workflowNames []string, userID int64, author string) (roomIssueResult, bool) {
- targetType, targetID, targetName := roomTarget(workflowIDs[0], workflowNames[0], "", "")
- if len(workflowIDs) > 1 {
- quoted := make([]string, len(workflowNames))
- for i, name := range workflowNames {
- quoted[i] = "「" + fallbackName(name, workflowIDs[i]) + "」"
- }
- targetType, targetName = "multi", "SOP"+strings.Join(quoted, "、")
- }
+ targetType, targetID, targetName := roomDispatchTarget(workflowIDs, workflowNames)
result := roomIssueResult{Issue: stored}
base := domain.ChatMessage{
RoomID: room.ID, UserID: userID, Author: author, Kind: domain.ChatKindDispatch,
@@ -357,6 +342,21 @@ func (h *roomsHandlers) dispatchToIssue(c *gin.Context, room domain.ChatRoom, st
return result, true
}
+// roomDispatchTarget describes the SOPs a room dispatches to. A single SOP
+// reuses roomTarget; several SOPs become a "multi" target keyed by the first
+// SOP and named after every SOP in dispatch order.
+func roomDispatchTarget(workflowIDs, workflowNames []string) (typ, id, name string) {
+ typ, id, name = roomTarget(workflowIDs[0], workflowNames[0], "", "")
+ if len(workflowIDs) > 1 {
+ quoted := make([]string, len(workflowNames))
+ for i, workflowName := range workflowNames {
+ quoted[i] = "「" + fallbackName(workflowName, workflowIDs[i]) + "」"
+ }
+ typ, name = "multi", "SOP"+strings.Join(quoted, "、")
+ }
+ return typ, id, name
+}
+
func roomTarget(workflowID, workflowName, agentID, agentName string) (typ, id, name string) {
switch {
case workflowID != "" && agentID != "":
diff --git a/internal/httpapi/rooms_cases_test.go b/internal/httpapi/rooms_cases_test.go
index 537a3bd4..95741fc1 100644
--- a/internal/httpapi/rooms_cases_test.go
+++ b/internal/httpapi/rooms_cases_test.go
@@ -84,3 +84,20 @@ func TestRoomPureHelperBranches(t *testing.T) {
t.Fatal("fallbackName")
}
}
+
+func TestRoomDispatchTarget(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ ids, names []string
+ typ, id, targetStr string
+ }{
+ {"single", []string{"wf-1"}, []string{"Build"}, "sop", "wf-1", "SOP「Build」"},
+ {"single without name", []string{"wf-1"}, []string{""}, "sop", "wf-1", "SOP「wf-1」"},
+ {"multi", []string{"wf-1", "wf-2", "wf-3"}, []string{"Build", "", "Ship"}, "multi", "wf-1", "SOP「Build」、「wf-2」、「Ship」"},
+ } {
+ typ, id, name := roomDispatchTarget(tc.ids, tc.names)
+ if typ != tc.typ || id != tc.id || name != tc.targetStr {
+ t.Errorf("%s: roomDispatchTarget = (%q, %q, %q) want (%q, %q, %q)", tc.name, typ, id, name, tc.typ, tc.id, tc.targetStr)
+ }
+ }
+}