Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(squads): stop squad usage attribution leaking onto direct runs (MUL-10072) (#11423)
Bohan Jiang04716c696e41
diff --git a/server/internal/handler/dashboard_squad_integration_test.go b/server/internal/handler/dashboard_squad_integration_test.goindex 8676b24659..2295b12ea2 100644--- a/server/internal/handler/dashboard_squad_integration_test.go+++ b/server/internal/handler/dashboard_squad_integration_test.go@@ -11,6 +11,7 @@ import ("github.com/multica-ai/multica/server/internal/service""github.com/multica-ai/multica/server/internal/testutil"+ db "github.com/multica-ai/multica/server/pkg/db/generated")// TestDashboardSquadAttributionAndEndpoints verifies the end-to-end Squad usage@@ -445,6 +446,63 @@ func TestSquadInheritanceAndDaemonClaimSafety(t *testing.T) {t.Fatalf("sub-issue task must have is_leader_task=false")}+ // Case D: On a squad-created sub-issue, a member directly @mentioning an+ // agent outside the squad starts a standalone run, and so does that agent+ // handing on to another outside agent; the same mention of a squad member+ // stays the squad's (MUL-10072).+ dbfx.Exec(t, `DELETE FROM agent_task_queue WHERE id = $1`, uuidToString(childTask.ID))+ outsiderRuntimeID := createClaimReclaimRuntime(t, ctx, "inherit-outsider-rt")+ outsiderID, _ := createClaimReclaimAgentAndIssue(t, ctx, outsiderRuntimeID, "inherit-outsider")+ mentionByMember := func(agentID string) db.AgentTaskQueue {+ t.Helper()+ var cid string+ if err := testPool.QueryRow(ctx, `+ INSERT INTO comment (workspace_id, issue_id, author_type, author_id, content)+ VALUES ($1, $2, 'member', $3, 'please take a look')+ RETURNING id+ `, testWorkspaceID, childIssueID, testUserID).Scan(&cid); err != nil {+ t.Fatalf("create member comment: %v", err)+ }+ t.Cleanup(func() { testPool.Exec(context.Background(), `DELETE FROM comment WHERE id = $1`, cid) })+ task, err := testHandler.TaskService.EnqueueTaskForMention(ctx, childIssue, parseUUID(agentID), parseUUID(cid), service.OriginNamed)+ if err != nil {+ t.Fatalf("EnqueueTaskForMention(%s): %v", agentID, err)+ }+ t.Cleanup(func() { testPool.Exec(context.Background(), `DELETE FROM agent_task_queue WHERE id = $1`, uuidToString(task.ID)) })+ return task+ }+ outsiderTask := mentionByMember(outsiderID)+ if outsiderTask.UsageSquadID.Valid || outsiderTask.SquadID.Valid {+ t.Fatalf("member mention of an outside agent on a squad sub-issue must be standalone, got usage_squad=%q squad=%q",+ uuidToString(outsiderTask.UsageSquadID), uuidToString(outsiderTask.SquadID))+ }+ // That outside agent handing on to another outside agent is not the+ // squad's either, though the hop is a delegation.+ secondRuntimeID := createClaimReclaimRuntime(t, ctx, "inherit-outsider2-rt")+ secondOutsiderID, _ := createClaimReclaimAgentAndIssue(t, ctx, secondRuntimeID, "inherit-outsider2")+ var handoffID string+ if err := testPool.QueryRow(ctx, `+ INSERT INTO comment (workspace_id, issue_id, author_type, author_id, content, source_task_id)+ VALUES ($1, $2, 'agent', $3, 'please review', $4)+ RETURNING id+ `, testWorkspaceID, childIssueID, outsiderID, uuidToString(outsiderTask.ID)).Scan(&handoffID); err != nil {+ t.Fatalf("create outside agent handoff comment: %v", err)+ }+ t.Cleanup(func() { testPool.Exec(context.Background(), `DELETE FROM comment WHERE id = $1`, handoffID) })+ handoffTask, err := testHandler.TaskService.EnqueueTaskForMention(ctx, childIssue, parseUUID(secondOutsiderID), parseUUID(handoffID), service.OriginNamed)+ if err != nil {+ t.Fatalf("EnqueueTaskForMention(second outsider): %v", err)+ }+ t.Cleanup(func() { testPool.Exec(context.Background(), `DELETE FROM agent_task_queue WHERE id = $1`, uuidToString(handoffTask.ID)) })+ if handoffTask.UsageSquadID.Valid {+ t.Fatalf("outside agent handing on to another outside agent usage_squad_id = %q, want NULL (source %q)",+ uuidToString(handoffTask.UsageSquadID), handoffTask.OriginatorSource.String)+ }+ dbfx.Exec(t, `INSERT INTO squad_member (squad_id, member_type, member_id, role) VALUES ($1, 'agent', $2, '')`, squadID, memberID)+ if task := mentionByMember(memberID); uuidToString(task.UsageSquadID) != squadID {+ t.Fatalf("member mention of a squad member on a squad sub-issue usage_squad_id = %q, want %q", uuidToString(task.UsageSquadID), squadID)+ }+// Case C: Standalone human-created issue assigned to memberID stays usage_squad_id = NULL & squad_id = NULL.dbfx.Exec(t, `UPDATE issue SET assignee_type = 'agent', assignee_id = $2 WHERE id = $1`, standaloneIssueID, memberID)standaloneIssue, err := testHandler.Queries.GetIssue(ctx, parseUUID(standaloneIssueID))diff --git a/server/internal/service/task.go b/server/internal/service/task.goindex 5742f3da42..b45a1ade32 100644--- a/server/internal/service/task.go+++ b/server/internal/service/task.go@@ -831,10 +831,16 @@ func effectiveTaskSquadID(task db.AgentTaskQueue) pgtype.UUID {// squad-created sub-issues) while leaving direct standalone human triggers// unattributed.//+// An issue a squad run created stays the squad's work only for the squad's own+// agents: a run reaching it without a squad from its trigger is the squad's+// only when its agent leads or belongs to the squad. Without that, every later+// run on the issue, and every issue those runs create, would count as the+// squad's (MUL-10072).+//// Callers write the result to usage_squad_id — NEVER to squad_id — so existing// leader/worker routing semantics keyed off squad_id (workflow step ownership,// self-trigger suppression, conversation root ownership) remain untouched.-func (s *TaskService) resolveInheritedSquadID(ctx context.Context, issue db.Issue, explicitSquadID, delegatedFromTaskID pgtype.UUID) pgtype.UUID {+func (s *TaskService) resolveInheritedSquadID(ctx context.Context, issue db.Issue, agentID pgtype.UUID, explicitSquadID, delegatedFromTaskID pgtype.UUID) pgtype.UUID {if explicitSquadID.Valid {return explicitSquadID}@@ -852,7 +858,7 @@ func (s *TaskService) resolveInheritedSquadID(ctx context.Context, issue db.Issu(issue.OriginType.String == "agent_create" || issue.OriginType.String == "quick_create") &&issue.OriginID.Valid {if originTask, err := s.Queries.GetAgentTask(ctx, issue.OriginID); err == nil {- if sid := effectiveTaskSquadID(originTask); sid.Valid {+ if sid := effectiveTaskSquadID(originTask); sid.Valid && s.agentInSquad(ctx, sid, agentID) {return sid}}@@ -860,6 +866,15 @@ func (s *TaskService) resolveInheritedSquadID(ctx context.Context, issue db.Issureturn pgtype.UUID{}}+// agentInSquad reports whether agentID leads or belongs to squadID.+func (s *TaskService) agentInSquad(ctx context.Context, squadID, agentID pgtype.UUID) bool {+ if squad, err := s.Queries.GetSquad(ctx, squadID); err == nil && squad.LeaderID == agentID {+ return true+ }+ isMember, err := s.Queries.IsSquadMember(ctx, db.IsSquadMemberParams{SquadID: squadID, MemberType: "agent", MemberID: agentID})+ return err == nil && isMember+}+// RecordHumanDelegation notes that attr's member personally started an agent// run, for the self-host telemetry "delegating member" counts. Call it once the// run row (or the fold into a queued row) is committed, with the attribution@@ -1366,7 +1381,7 @@ func (s *TaskService) enqueueIssueTaskWithCommentPlan(ctx context.Context, issueoriginatorUserID := attr.UserIDruntimeMCPOverlay := s.buildRuntimeMCPOverlay(ctx, originatorUserID, agent)attrSource, attrDelegatedFrom, attrEvidenceKind, attrEvidenceRef := attributionCreateParams(attr)- inheritedSquadID := s.resolveInheritedSquadID(ctx, issue, pgtype.UUID{}, attrDelegatedFrom)+ inheritedSquadID := s.resolveInheritedSquadID(ctx, issue, issue.AssigneeID, pgtype.UUID{}, attrDelegatedFrom)createParams := db.CreateAgentTaskParams{ID: dbid.NewV7(),AgentID: issue.AssigneeID,@@ -1544,7 +1559,7 @@ func (s *TaskService) enqueueMentionTaskWithCommentPlan(ctx context.Context, issoriginatorUserID := attr.UserIDruntimeMCPOverlay := s.buildRuntimeMCPOverlay(ctx, originatorUserID, agent)attrSource, attrDelegatedFrom, attrEvidenceKind, attrEvidenceRef := attributionCreateParams(attr)- resolvedSquadID := s.resolveInheritedSquadID(ctx, issue, squadID, attrDelegatedFrom)+ resolvedSquadID := s.resolveInheritedSquadID(ctx, issue, agentID, squadID, attrDelegatedFrom)params := db.CreateAgentTaskParams{ID: dbid.NewV7(),AgentID: agentID,