Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(issues): log why an issue write did not start a run (#10857)
xiaoke12345678cd969f848291
diff --git a/server/internal/handler/issue.go b/server/internal/handler/issue.goindex 7f0d1b2b79..2e93c22c52 100644--- a/server/internal/handler/issue.go+++ b/server/internal/handler/issue.go@@ -4363,6 +4363,7 @@ func (h *Handler) UpdateIssue(w http.ResponseWriter, r *http.Request) {},h.issueTriggerWriteProbe(r, actorType, actorID, issue),)+ logDeclinedIssueRun(r.Context(), issue, trigger)willRun = willRun && !req.SuppressRunupdatedPayload := map[string]any{@@ -5228,6 +5229,7 @@ func (h *Handler) BatchUpdateIssues(w http.ResponseWriter, r *http.Request) {},h.issueTriggerWriteProbe(r, actorType, actorID, issue),)+ logDeclinedIssueRun(r.Context(), issue, trigger)willRun = willRun && !req.Updates.SuppressRunbatchPayload := map[string]any{"issue": resp,diff --git a/server/internal/handler/issue_trigger.go b/server/internal/handler/issue_trigger.goindex f03d2a5b3d..889d3430ed 100644--- a/server/internal/handler/issue_trigger.go+++ b/server/internal/handler/issue_trigger.go@@ -3,6 +3,7 @@ package handlerimport ("context""encoding/json"+ "errors""log/slog""net/http"@@ -77,6 +78,26 @@ func (h *Handler) shouldSuppressActiveSelfAssignment(ctx context.Context, actorTreturn active || err != nil}+// logDeclinedIssueRun records why an issue write that could have started a run+// did not (#10845). The precise cause goes to server logs, not the write+// response (see admission.go). Parking an assignment in backlog is routine, so+// it logs at debug.+func logDeclinedIssueRun(ctx context.Context, issue db.Issue, trigger service.IssueRunTrigger) {+ if trigger.DeclineReason == "" {+ return+ }+ level := slog.LevelInfo+ if trigger.DeclineReason == ReasonDeferred {+ level = slog.LevelDebug+ }+ slog.Log(ctx, level, "issue write did not start a run",+ "issue_id", uuidToString(issue.ID),+ "assignee_type", issue.AssigneeType.String,+ "assignee_id", uuidToString(issue.AssigneeID),+ "reason", trigger.DeclineReason,+ )+}+// dispatchIssueRun executes the enqueue side effect for a decision produced by// WillEnqueueRun. handoffNote is a legacy API input retained for installed// clients and travels only with a run that actually starts. The squad path@@ -88,7 +109,15 @@ func (h *Handler) dispatchIssueRun(ctx context.Context, issue db.Issue, trigger// The member who performed this assign/promote is the accountable human// for the run (MUL-4302 §4). An agent actor is not a human, so only a// member actor is threaded; otherwise attribution falls back to the chain.- _, _ = h.TaskService.EnqueueTaskForIssueWithHandoff(ctx, issue, handoffNote, memberActorUserID(actorType, actorID))+ // A pending task that won the slot already covers this write; the+ // service logs that coalesce at debug.+ if _, err := h.TaskService.EnqueueTaskForIssueWithHandoff(ctx, issue, handoffNote, memberActorUserID(actorType, actorID)); err != nil && !errors.Is(err, service.ErrDuplicatePendingTask) {+ slog.Warn("issue write: run enqueue failed",+ "issue_id", uuidToString(issue.ID),+ "agent_id", uuidToString(issue.AssigneeID),+ "error", err,+ )+ }case "squad":h.enqueueSquadLeaderTask(ctx, issue, actorType, actorID, handoffNote, trigger.WorkflowHandoff)}diff --git a/server/internal/handler/issue_trigger_preview_test.go b/server/internal/handler/issue_trigger_preview_test.goindex fe2534a6fc..0e51df90d2 100644--- a/server/internal/handler/issue_trigger_preview_test.go+++ b/server/internal/handler/issue_trigger_preview_test.go@@ -1,14 +1,17 @@package handlerimport (+ "bytes""context""encoding/json"+ "log/slog""net/http""net/http/httptest""strings""testing""github.com/multica-ai/multica/server/internal/daemon"+ "github.com/multica-ai/multica/server/internal/testutil""github.com/multica-ai/multica/server/internal/util")@@ -223,6 +226,65 @@ func TestUpdateIssueSuppressRunSkipsEnqueue(t *testing.T) {}}+// Both write paths explain declined agent runs in Triage (#10845), while+// member assignments never imply that a run could have started.+func TestIssueWritesLogWhyARunDidNotStart(t *testing.T) {+ agentID := seededReadyAgentID(t)+ var logs bytes.Buffer+ prev := slog.Default()+ slog.SetDefault(slog.New(slog.NewTextHandler(&logs, &slog.HandlerOptions{Level: slog.LevelInfo})))+ t.Cleanup(func() { slog.SetDefault(prev) })++ inTriage := func(title string) IssueResponse {+ issue := createIssueForTest(t, map[string]any{"title": title, "status": "todo"})+ dbfx.Exec(t, `UPDATE issue SET triage_state = 'pending' WHERE id = $1`, issue.ID)+ return issue+ }+ for _, tc := range []struct {+ assigneeType string+ assigneeID string+ wantLog bool+ }{+ {assigneeType: "agent", assigneeID: agentID, wantLog: true},+ {assigneeType: "member", assigneeID: testUserID},+ } {+ t.Run(tc.assigneeType, func(t *testing.T) {+ assign := map[string]any{"assignee_type": tc.assigneeType, "assignee_id": tc.assigneeID}+ single := inTriage("triage single write " + tc.assigneeType)+ testutil.Call(t, testHandler.UpdateIssue,+ withURLParam(newRequest("PUT", "/api/issues/"+single.ID, assign), "id", single.ID)).Want(http.StatusOK)+ batch := inTriage("triage batch write " + tc.assigneeType)+ testutil.Call(t, testHandler.BatchUpdateIssues, newRequest("PATCH", "/api/issues/batch?workspace_id="+testWorkspaceID,+ map[string]any{"issue_ids": []string{batch.ID}, "updates": assign})).Want(http.StatusOK)++ for _, issue := range []IssueResponse{single, batch} {+ if got := taskCountFor(t, issue.ID, agentID); got != 0 {+ t.Fatalf("issue %s in triage got %d tasks, want none", issue.ID, got)+ }+ if got := hasLogLine(logs.String(), `msg="issue write did not start a run"`, "issue_id="+issue.ID); got != tc.wantLog {+ t.Fatalf("decline log for issue %s = %v, want %v; logs:\n%s", issue.ID, got, tc.wantLog, logs.String())+ }+ if tc.wantLog && !hasLogLine(logs.String(), `msg="issue write did not start a run"`, "issue_id="+issue.ID, "reason=issue_in_triage") {+ t.Fatalf("no triage decline reason for issue %s; logs:\n%s", issue.ID, logs.String())+ }+ }+ })+ }+}++func hasLogLine(logs string, parts ...string) bool {+ for _, line := range strings.Split(logs, "\n") {+ found := true+ for _, part := range parts {+ found = found && strings.Contains(line, part)+ }+ if found {+ return true+ }+ }+ return false+}+// TestLegacyV0438HandoffPayloadReachesTaskPrompt is the cross-version guard// for installed clients: the v0.4.38 update payload must survive the current// API, task queue, claim wire shape, and daemon prompt rendering.diff --git a/server/internal/service/issue_trigger.go b/server/internal/service/issue_trigger.goindex 5f41081a52..01712627a1 100644--- a/server/internal/service/issue_trigger.go+++ b/server/internal/service/issue_trigger.go@@ -4,6 +4,7 @@ import ("context""github.com/jackc/pgx/v5/pgtype"+ "github.com/multica-ai/multica/server/internal/dispatch""github.com/multica-ai/multica/server/internal/issuestatus"db "github.com/multica-ai/multica/server/pkg/db/generated")@@ -66,12 +67,22 @@ type IssueTriggerInput struct {// IssueRunTrigger is the resolved decision shared by preview and the write// paths. AgentID is the agent that will actually run — the assignee for an// agent issue, the squad leader for a squad issue.+//+// DeclineReason is set only when the write could have started a run (a create,+// an assignment, or a promotion out of backlog) and does not. The write paths+// log it so a run that silently did not start can be explained (#10845).+// Writes that never start a run leave it empty.type IssueRunTrigger struct {IssueID pgtype.UUIDAgentID pgtype.UUIDAssigneeType stringSource RunEnqueueSourceWorkflowHandoff bool+ DeclineReason dispatch.ReasonCode+}++func declineRun(reason dispatch.ReasonCode) (IssueRunTrigger, bool) {+ return IssueRunTrigger{DeclineReason: reason}, false}func allowAllAgents(db.Agent) bool { return true }@@ -106,6 +117,9 @@ func (s *IssueService) WillEnqueueRun(ctx context.Context, in IssueTriggerInput,if !issue.AssigneeType.Valid || !issue.AssigneeID.Valid {return IssueRunTrigger{}, false}+ if issue.AssigneeType.String != "agent" && issue.AssigneeType.String != "squad" {+ return IssueRunTrigger{}, false+ }canAccess := probe.CanAccessAgentif canAccess == nil {canAccess = allowAllAgents@@ -118,25 +132,9 @@ func (s *IssueService) WillEnqueueRun(ctx context.Context, in IssueTriggerInput,currentStatus := issuestatus.Effective(ctx, s.Queries, issue.WorkspaceID, issue.Status)prevStatus := issuestatus.Effective(ctx, s.Queries, issue.WorkspaceID, in.PrevStatus)- // Triage is stricter than the backlog parking lot: backlog defers a run,- // Triage refuses one outright (MUL-7189 §2.3). Deciding it here is what- // keeps the trigger PREVIEW honest — the queue door would refuse the insert- // either way, but silently, and the preview would have promised a run.- //- // Leaving Triage needs no case of its own. Triage is not a status, so accept- // clears this field and then takes the ordinary create / assign path; there- // is no "was in triage" transition for this predicate to recognise.- if issue.TriageState.Valid {- return IssueRunTrigger{}, false- }-var source RunEnqueueSourceswitch {case in.IsCreate || in.AssigneeChanged:- // Backlog is the parking lot: assigning into backlog never starts a run.- if currentStatus == "backlog" {- return IssueRunTrigger{}, false- }source = RunSourceAssigncase in.StatusChanged && prevStatus == "backlog" &¤tStatus != "backlog" &&@@ -146,26 +144,45 @@ func (s *IssueService) WillEnqueueRun(ctx context.Context, in IssueTriggerInput,return IssueRunTrigger{}, false}+ // Triage is stricter than the backlog parking lot: backlog defers a run,+ // Triage refuses one outright (MUL-7189 §2.3). Deciding it here is what+ // keeps the trigger PREVIEW honest — the queue door would refuse the insert+ // either way, but silently, and the preview would have promised a run.+ //+ // Leaving Triage needs no case of its own. Triage is not a status, so accept+ // clears this field and then takes the ordinary create / assign path; there+ // is no "was in triage" transition for this predicate to recognise.+ if issue.TriageState.Valid {+ return declineRun(dispatch.ReasonIssueInTriage)+ }+ // Backlog is the parking lot: assigning into backlog never starts a run.+ if source == RunSourceAssign && currentStatus == "backlog" {+ return declineRun(dispatch.ReasonDeferred)+ }+switch issue.AssigneeType.String {case "agent":agent, err := s.Queries.GetAgent(ctx, issue.AssigneeID)- if err != nil || !agent.RuntimeID.Valid || agent.ArchivedAt.Valid {- return IssueRunTrigger{}, false+ if err != nil || agent.ArchivedAt.Valid {+ return declineRun(dispatch.ReasonTargetUnavailable)+ }+ if !agent.RuntimeID.Valid {+ return declineRun(dispatch.ReasonAgentRuntimeRequired)}if !canAccess(agent) {- return IssueRunTrigger{}, false+ return declineRun(dispatch.ReasonInvocationNotAllowed)}if source == RunSourceStatus && probe.IsSelfLoop != nil && probe.IsSelfLoop(issue.AssigneeID) {- return IssueRunTrigger{}, false+ return declineRun(dispatch.ReasonAlreadyActive)}if source == RunSourceAssign && !in.IsCreate && !in.EnteredHandoffStep && probe.SuppressActiveSelfAssignment != nil &&probe.SuppressActiveSelfAssignment(issue.AssigneeID) {- return IssueRunTrigger{}, false+ return declineRun(dispatch.ReasonAlreadyActive)}// A step entered under the agent's own run queues behind it; one// already queued covers the new step too.if (source == RunSourceStatus || in.EnteredHandoffStep) && s.hasPendingRun(ctx, issue.ID, issue.AssigneeID) {- return IssueRunTrigger{}, false+ return declineRun(dispatch.ReasonCoalesced)}return IssueRunTrigger{IssueID: issue.ID,@@ -186,24 +203,27 @@ func (s *IssueService) WillEnqueueRun(ctx context.Context, in IssueTriggerInput,WorkspaceID: issue.WorkspaceID,})if err != nil {- return IssueRunTrigger{}, false+ return declineRun(dispatch.ReasonTargetUnavailable)}leader, err := s.Queries.GetAgent(ctx, squad.LeaderID)if err != nil {- return IssueRunTrigger{}, false+ return declineRun(dispatch.ReasonTargetUnavailable)}verdict, err := AgentReadiness(ctx, s.runtimeLookup(s.Queries), leader)- if err != nil || !verdict.Ready() {- return IssueRunTrigger{}, false+ if err != nil {+ return declineRun(dispatch.ReasonInternalError)+ }+ if !verdict.Ready() {+ return declineRun(verdict.Reason)}if !canAccess(leader) {- return IssueRunTrigger{}, false+ return declineRun(dispatch.ReasonInvocationNotAllowed)}if source == RunSourceStatus && probe.IsSelfLoop != nil && probe.IsSelfLoop(squad.LeaderID) {- return IssueRunTrigger{}, false+ return declineRun(dispatch.ReasonAlreadyActive)}if source == RunSourceStatus && s.hasPendingRun(ctx, issue.ID, squad.LeaderID) {- return IssueRunTrigger{}, false+ return declineRun(dispatch.ReasonCoalesced)}return IssueRunTrigger{IssueID: issue.ID,diff --git a/server/internal/service/issue_trigger_test.go b/server/internal/service/issue_trigger_test.gonew file mode 100644index 0000000000..cd450b1432--- /dev/null+++ b/server/internal/service/issue_trigger_test.go@@ -0,0 +1,115 @@+package service++import (+ "context"+ "fmt"+ "testing"+ "time"++ "github.com/jackc/pgx/v5/pgtype"+ "github.com/multica-ai/multica/server/internal/dispatch"+ "github.com/multica-ai/multica/server/internal/events"+ "github.com/multica-ai/multica/server/internal/testutil"+ "github.com/multica-ai/multica/server/internal/util"+ db "github.com/multica-ai/multica/server/pkg/db/generated"+)++// An issue write that could have started a run but did not names why, so the+// write paths can log it (#10845). Writes that never start a run, such as a+// title edit, carry no reason and therefore log nothing.+func TestWillEnqueueRunNamesWhyAWriteStartsNoRun(t *testing.T) {+ ctx := context.Background()+ pool := newResolveOriginatorPool(t)+ q := db.New(pool)+ bootstrap := testutil.New(pool, "", "")+ suffix := time.Now().UnixNano()+ userID := bootstrap.User(t, "trigger-owner", fmt.Sprintf("trigger-owner-%[email protected]", suffix))+ workspaceID := bootstrap.Workspace(t, "issue trigger reasons", fmt.Sprintf("issue-trigger-%d", suffix))+ f := testutil.New(pool, workspaceID, userID)+ f.Member(t, workspaceID, userID, "owner")+ if err := q.SeedIssueStatusEntries(ctx, util.MustParseUUID(workspaceID)); err != nil {+ t.Fatalf("seed statuses: %v", err)+ }+ runtimeID := f.Runtime(t, "online runtime")+ agentID := f.Agent(t, "ready agent", runtimeID)+ unboundAgentID := f.Agent(t, "unbound agent", "")+ archivedAgentID := f.Agent(t, "archived agent", runtimeID, testutil.Cols{"archived_at": testutil.Raw("now()")})+ squadID := f.Squad(t, "squad with unbound leader", f.Agent(t, "unbound leader", ""))+ svc := &IssueService{Queries: q, TaskService: NewTaskService(q, pool, nil, events.New())}++ assigned := func(assigneeType, assigneeID, status string) testutil.Cols {+ return testutil.Cols{"assignee_type": assigneeType, "assignee_id": assigneeID, "status": status}+ }+ assign := func(issue db.Issue) IssueTriggerInput {+ return IssueTriggerInput{Issue: issue, PrevStatus: issue.Status, AssigneeChanged: true}+ }+ create := func(issue db.Issue) IssueTriggerInput {+ return IssueTriggerInput{Issue: issue, IsCreate: true}+ }+ promote := func(issue db.Issue) IssueTriggerInput {+ return IssueTriggerInput{Issue: issue, PrevStatus: "backlog", StatusChanged: true}+ }+ edit := func(issue db.Issue) IssueTriggerInput {+ return IssueTriggerInput{Issue: issue, PrevStatus: issue.Status}+ }+ yes := func(pgtype.UUID) bool { return true }++ for _, tc := range []struct {+ name string+ issue testutil.Cols+ input func(db.Issue) IssueTriggerInput+ probe IssueTriggerProbe+ pendingTask bool+ wantRun bool+ want dispatch.ReasonCode+ }{+ {name: "assignment starts a run", issue: assigned("agent", agentID, "todo"), input: assign, wantRun: true},+ {name: "edit that triggers nothing", issue: assigned("agent", agentID, "todo"), input: edit},+ {name: "unassigned issue", issue: testutil.Cols{"status": "todo"}, input: promote},+ {name: "promotion straight to done", issue: assigned("agent", agentID, "done"), input: promote},+ {name: "edit while in triage", issue: inTriage(assigned("agent", agentID, "todo")), input: edit},+ {name: "assignment while in triage", issue: inTriage(assigned("agent", agentID, "todo")), input: assign, want: dispatch.ReasonIssueInTriage},+ {name: "assignment into backlog", issue: assigned("agent", agentID, "backlog"), input: assign, want: dispatch.ReasonDeferred},+ {name: "member assignment while in triage", issue: inTriage(assigned("member", userID, "todo")), input: assign},+ {name: "member assignment into backlog", issue: assigned("member", userID, "backlog"), input: assign},+ {name: "member creation while in triage", issue: inTriage(assigned("member", userID, "todo")), input: create},+ {name: "member creation in backlog", issue: assigned("member", userID, "backlog"), input: create},+ {name: "member promotion while in triage", issue: inTriage(assigned("member", userID, "todo")), input: promote},+ {name: "member promotion out of backlog", issue: assigned("member", userID, "todo"), input: promote},+ {name: "squad assignment while in triage", issue: inTriage(assigned("squad", squadID, "todo")), input: assign, want: dispatch.ReasonIssueInTriage},+ {name: "squad assignment into backlog", issue: assigned("squad", squadID, "backlog"), input: assign, want: dispatch.ReasonDeferred},+ {name: "agent without a runtime", issue: assigned("agent", unboundAgentID, "todo"), input: assign, want: dispatch.ReasonAgentRuntimeRequired},+ {name: "archived agent", issue: assigned("agent", archivedAgentID, "todo"), input: assign, want: dispatch.ReasonTargetUnavailable},+ {name: "agent the caller cannot invoke", issue: assigned("agent", agentID, "todo"), input: assign,+ probe: IssueTriggerProbe{CanAccessAgent: func(db.Agent) bool { return false }}, want: dispatch.ReasonInvocationNotAllowed},+ {name: "promotion from the assignee's own run", issue: assigned("agent", agentID, "todo"), input: promote,+ probe: IssueTriggerProbe{IsSelfLoop: yes}, want: dispatch.ReasonAlreadyActive},+ {name: "agent assigning itself while already active", issue: assigned("agent", agentID, "todo"), input: assign,+ probe: IssueTriggerProbe{SuppressActiveSelfAssignment: yes}, want: dispatch.ReasonAlreadyActive},+ {name: "promotion with a run already pending", issue: assigned("agent", agentID, "todo"), input: promote,+ pendingTask: true, want: dispatch.ReasonCoalesced},+ {name: "squad whose leader has no runtime", issue: assigned("squad", squadID, "todo"), input: assign, want: dispatch.ReasonAgentRuntimeRequired},+ } {+ t.Run(tc.name, func(t *testing.T) {+ issueID := f.Issue(t, tc.name, tc.issue)+ f.Cleanup(t, `DELETE FROM agent_task_queue WHERE issue_id = $1`, issueID)+ if tc.pendingTask {+ f.Task(t, agentID, testutil.Cols{"issue_id": issueID, "runtime_id": runtimeID, "status": "queued"})+ }+ issue, err := q.GetIssue(ctx, util.MustParseUUID(issueID))+ if err != nil {+ t.Fatalf("load issue: %v", err)+ }++ trigger, willRun := svc.WillEnqueueRun(ctx, tc.input(issue), tc.probe)+ if willRun != tc.wantRun || trigger.DeclineReason != tc.want {+ t.Fatalf("WillEnqueueRun = run:%v reason:%q, want run:%v reason:%q", willRun, trigger.DeclineReason, tc.wantRun, tc.want)+ }+ })+ }+}++func inTriage(cols testutil.Cols) testutil.Cols {+ cols["triage_state"] = "pending"+ return cols+}