Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(autopilot): 接受 issue 更新的 map 载荷 (#10262)
Br1efx980611f20790
diff --git a/server/cmd/server/autopilot_issue_listener_test.go b/server/cmd/server/autopilot_issue_listener_test.gonew file mode 100644index 0000000000..1d7949b24f--- /dev/null+++ b/server/cmd/server/autopilot_issue_listener_test.go@@ -0,0 +1,108 @@+package main++import (+ "context"+ "testing"++ "github.com/multica-ai/multica/server/internal/events"+ "github.com/multica-ai/multica/server/internal/handler"+ "github.com/multica-ai/multica/server/internal/service"+ "github.com/multica-ai/multica/server/pkg/protocol"+)++// publishIssueUpdated fires the same event the autopilot listener subscribes+// to. issue is either handler.IssueResponse (HTTP UpdateIssue) or the map+// broadcastIssueUpdated sends.+func publishIssueUpdated(bus *events.Bus, issue any, statusChanged bool) {+ bus.Publish(events.Event{+ Type: protocol.EventIssueUpdated,+ Payload: map[string]any{+ "issue": issue,+ "status_changed": statusChanged,+ },+ })+}++// watchAutopilotIssueSync replaces the load-and-sync step for one test and+// records every issue id that passed the listener's status gate.+func watchAutopilotIssueSync(t *testing.T) *[]string {+ t.Helper()+ var seen []string+ original := autopilotIssueSync+ autopilotIssueSync = func(_ context.Context, _ *service.AutopilotService, issueID string) {+ seen = append(seen, issueID)+ }+ t.Cleanup(func() { autopilotIssueSync = original })+ return &seen+}++func TestAutopilotIssueListenerMapPayloadReachesSync(t *testing.T) {+ seen := watchAutopilotIssueSync(t)+ bus := events.New()+ registerAutopilotListeners(bus, nil)++ // broadcastIssueUpdated publishes IssueToMap. The old listener returned at+ // the IssueResponse assertion, so a map never reached the sync even when+ // the status is one the gate already lets through.+ for _, status := range []string{"done", "in_review", "cancelled", "awaiting_vendor"} {+ publishIssueUpdated(bus, map[string]any{+ "id": "map-" + status,+ "status": status,+ }, true)+ }++ want := []string{"map-done", "map-in_review", "map-cancelled", "map-awaiting_vendor"}+ if len(*seen) != len(want) {+ t.Fatalf("map payload sync calls = %v, want %v", *seen, want)+ }+ for i, id := range want {+ if (*seen)[i] != id {+ t.Fatalf("map payload sync calls = %v, want %v", *seen, want)+ }+ }+}++func TestAutopilotIssueListenerStructPayloadStillReachesSync(t *testing.T) {+ seen := watchAutopilotIssueSync(t)+ bus := events.New()+ registerAutopilotListeners(bus, nil)++ publishIssueUpdated(bus, handler.IssueResponse{+ ID: "issue-struct",+ Status: "done",+ }, true)++ if len(*seen) != 1 || (*seen)[0] != "issue-struct" {+ t.Fatalf("struct payload sync calls = %v, want [issue-struct]", *seen)+ }+}++func TestAutopilotIssueListenerSkipsUnchangedAndNonTerminal(t *testing.T) {+ seen := watchAutopilotIssueSync(t)+ bus := events.New()+ registerAutopilotListeners(bus, nil)++ publishIssueUpdated(bus, map[string]any{+ "id": "unchanged",+ "status": "done",+ }, false)+ // Built-in keys that are not done, in_review, or cancelled stay filtered,+ // for both payload shapes. todo is the sweeper's reset target and is one+ // of those keys.+ for _, status := range []string{"todo", "in_progress", "backlog", "blocked"} {+ publishIssueUpdated(bus, map[string]any{+ "id": "map-" + status,+ "status": status,+ }, true)+ publishIssueUpdated(bus, handler.IssueResponse{+ ID: "struct-" + status,+ Status: status,+ }, true)+ }+ publishIssueUpdated(bus, map[string]any{"status": "done"}, true)+ publishIssueUpdated(bus, "not-an-issue", true)++ if len(*seen) != 0 {+ t.Fatalf("sync calls = %v, want none", *seen)+ }+}diff --git a/server/cmd/server/autopilot_listeners.go b/server/cmd/server/autopilot_listeners.goindex 056d88b627..a4eaa52b3f 100644--- a/server/cmd/server/autopilot_listeners.go+++ b/server/cmd/server/autopilot_listeners.go@@ -11,6 +11,18 @@ import ("github.com/multica-ai/multica/server/pkg/protocol")+// autopilotIssueSync loads the stored issue and hands it to SyncRunFromIssue.+// Tests replace it to observe that the listener reached the sync without a+// database. The payload status is only a pre-filter; this call always re-reads.+var autopilotIssueSync = func(ctx context.Context, svc *service.AutopilotService, issueID string) {+ dbIssue, err := svc.Queries.GetIssue(ctx, parseUUID(issueID))+ if err != nil {+ slog.Debug("autopilot listener: failed to load issue", "issue_id", issueID, "error", err)+ return+ }+ svc.SyncRunFromIssue(ctx, dbIssue)+}+// registerAutopilotListeners hooks into issue and task events to keep// autopilot runs in sync with their linked issues and tasks.func registerAutopilotListeners(bus *events.Bus, svc *service.AutopilotService) {@@ -27,28 +39,25 @@ func registerAutopilotListeners(bus *events.Bus, svc *service.AutopilotService)if !statusChanged {return}- issue, ok := payload["issue"].(handler.IssueResponse)+ // HTTP UpdateIssue sends handler.IssueResponse. Background writers+ // (broadcastIssueUpdated) send the IssueToMap map. The id and status+ // are the only fields this gate needs; the stored row decides the run.+ issueID, status, ok := autopilotIssueIDAndStatus(payload["issue"])if !ok {return}- // Only handle statuses that finalize an autopilot run. This gate exists- // to skip the issue load for routine forward progress, so it stays a- // literal comparison for built-in keys. A custom status carries the- // behavior of the canonical status it inherits, but resolving that- // needs a catalog read — so let non-built-in keys through and let- // SyncRunFromIssue normalize once it has the issue. (MUL-6243)- if issuestatus.IsBuiltIn(issue.Status) &&- issue.Status != "done" && issue.Status != "in_review" &&- issue.Status != "cancelled" {- return- }- // Load the full issue from DB to check origin_type.- dbIssue, err := svc.Queries.GetIssue(ctx, parseUUID(issue.ID))- if err != nil {- slog.Debug("autopilot listener: failed to load issue", "issue_id", issue.ID, "error", err)+ // Skip the issue load for routine forward progress. Built-in keys that+ // never settle a run stay out. Only done, in_review, and cancelled continue.+ // A custom status inherits its canonical behavior, but resolving+ // that needs a catalog read, so non-built-in keys pass and+ // SyncRunFromIssue normalizes the+ // stored row. (MUL-6243)+ if issuestatus.IsBuiltIn(status) &&+ status != "done" && status != "in_review" &&+ status != "cancelled" {return}- svc.SyncRunFromIssue(ctx, dbIssue)+ autopilotIssueSync(ctx, svc, issueID)})// When a task completes or fails, check if it's an autopilot run_only task.@@ -63,6 +72,25 @@ func registerAutopilotListeners(bus *events.Bus, svc *service.AutopilotService)})}+// autopilotIssueIDAndStatus reads the issue id and status from either payload+// shape. A missing id or a non-string status is not an issue update this+// listener can act on.+func autopilotIssueIDAndStatus(v any) (string, string, bool) {+ if issue, ok := v.(handler.IssueResponse); ok {+ return issue.ID, issue.Status, issue.ID != ""+ }+ m, ok := v.(map[string]any)+ if !ok {+ return "", "", false+ }+ id, _ := m["id"].(string)+ status, _ := m["status"].(string)+ if id == "" || status == "" {+ return "", "", false+ }+ return id, status, true+}+func syncRunFromTaskEvent(ctx context.Context, svc *service.AutopilotService, e events.Event) {payload, ok := e.Payload.(map[string]any)if !ok {