Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
package handler
import (
"errors"
"fmt"
"net/http"
"strings"
"sync"
"testing"
"time"
"github.com/multica-ai/multica/server/internal/issueworkflow"
"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"
"github.com/multica-ai/multica/server/pkg/protocol"
)
func TestWorkflowHandoffCancelsDeferredSquadStages(t *testing.T) {
seedTestCatalog(t)
for _, queued := range []bool{true, false} {
for _, stop := range []bool{true, false} {
t.Run(fmt.Sprintf("queued=%v/stop=%v", queued, stop), func(t *testing.T) {
fx := newSquadBriefingClaimFixture(t, t.Context(), "Cancel waiting squad stages")
wf := createTestWorkflow(t, "Cancel waiting flow "+workflowTestSuffix(), "in_progress", "in_review")
projectID := createWorkflowTestProject(t, "Cancel waiting project")
setProjectWorkflow(t, projectID, map[string]any{"workflow_id": wf.ID}).Want(http.StatusOK)
for _, status := range []string{"in_progress", "in_review"} {
setWorkflowHandoff(t, wf.ID, status, map[string]any{"handler": map[string]any{"type": "squad", "id": fx.SquadID}}).Want(http.StatusOK)
}
setWorkflowHandoff(t, wf.ID, "todo", map[string]any{"handler": map[string]any{"type": "member", "id": testUserID}}).Want(http.StatusOK)
dbfx.Exec(t, `UPDATE issue SET project_id=$2,status='backlog' WHERE id=$1`, fx.IssueID, projectID)
updateIssueForTest(t, fx.IssueID, map[string]any{"status": "in_progress"}).Want(http.StatusOK)
updateIssueForTest(t, fx.IssueID, map[string]any{"status": "in_review"}).Want(http.StatusOK)
if !queued {
dbfx.Exec(t, `UPDATE agent_task_queue SET status='completed',completed_at=now() WHERE issue_id=$1 AND workflow_step='in_progress'`, fx.IssueID)
}
before, err := testHandler.Queries.ListTasksByIssue(t.Context(), parseUUID(fx.IssueID))
if err != nil || len(before) != 2 {
t.Fatalf("handoff fixture tasks = %+v, %v", before, err)
}
var deferred bool
for _, task := range before {
deferred = deferred || task.Status == "deferred"
}
if !deferred {
t.Fatal("fixture did not retain a deferred stage handoff")
}
var preview WorkflowHandoffPreviewResponse
req := withURLParam(newRequest(http.MethodGet, "/api/issues/"+fx.IssueID+"/workflow-handoff?status=todo", nil), "id", fx.IssueID)
testutil.Call(t, testHandler.PreviewWorkflowHandoff, req).Want(http.StatusOK).JSON(&preview)
wantRuns := 1
if queued {
wantRuns++
}
if len(preview.PreviousRuns) != wantRuns {
t.Errorf("preview lists %d runs, want %d including deferred stages", len(preview.PreviousRuns), wantRuns)
}
updateIssueForTest(t, fx.IssueID, map[string]any{"status": "todo", "stop_previous_assignee_runs": stop}).Want(http.StatusOK)
for _, previous := range before {
after, err := testHandler.Queries.GetAgentTask(t.Context(), previous.ID)
if err != nil {
t.Fatal(err)
}
wantStatus := previous.Status
if stop && previous.Status != "completed" {
wantStatus = "cancelled"
if after.CancelledByType.String != "member" || after.CancelledByID != parseUUID(testUserID) {
t.Errorf("stage %s lost cancellation attribution", previous.WorkflowStep.String)
}
}
if after.Status != wantStatus {
t.Errorf("stage %s: status = %s, want %s", previous.WorkflowStep.String, after.Status, wantStatus)
}
}
claimed, _, _, _ := claimAgentInstructionsForTest(t, fx.RuntimeID)
if stop && claimed != "" {
t.Errorf("explicitly stopped squad task was claimed: %s", claimed)
} else if !stop && claimed == "" {
t.Error("opting out of stopping must preserve runnable squad work")
}
})
}
}
}
func TestWorkflowSquadHandoffReplayDoesNotDuplicate(t *testing.T) {
seedTestCatalog(t)
fx := newSquadBriefingClaimFixture(t, t.Context(), "Idempotent squad handoff")
projectID := createWorkflowTestProject(t, "Idempotent squad project")
setProjectWorkflow(t, projectID, map[string]any{"workflow_id": createTestWorkflow(t, "Idempotent flow "+workflowTestSuffix(), "in_progress").ID}).Want(http.StatusOK)
setColumnHandoff(t, projectID, "in_progress", map[string]any{"handler": map[string]any{"type": "squad", "id": fx.SquadID}}).Want(http.StatusOK)
dbfx.Exec(t, `UPDATE issue SET project_id=$2,status='todo' WHERE id=$1`, fx.IssueID, projectID)
updateIssueForTest(t, fx.IssueID, map[string]any{"status": "in_progress"}).Want(http.StatusOK)
issue, err := testHandler.Queries.GetIssue(t.Context(), parseUUID(fx.IssueID))
if err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
errs := make(chan error, 8)
for range 8 {
wg.Go(func() {
_, err := testHandler.TaskService.EnqueueTaskForWorkflowSquadLeader(t.Context(), issue, parseUUID(fx.AgentID), parseUUID(fx.SquadID), "", parseUUID(testUserID))
errs <- err
})
}
wg.Wait()
close(errs)
for err := range errs {
if !errors.Is(err, service.ErrDuplicatePendingTask) {
t.Fatalf("same handoff replay = %v, want coalesced", err)
}
}
tasks, err := testHandler.Queries.ListTasksByIssue(t.Context(), issue.ID)
if err != nil || len(tasks) != 1 {
t.Fatalf("handoff replay created duplicate tasks: %+v, %v", tasks, err)
}
// A run may advance its binding itself; the original handoff identity
// still refers to the source status write, not the mutable run binding.
dbfx.Exec(t, `UPDATE agent_task_queue SET workflow_step='todo' WHERE id=$1`, tasks[0].ID)
if _, err := testHandler.TaskService.EnqueueTaskForWorkflowSquadLeader(t.Context(), issue, parseUUID(fx.AgentID), parseUUID(fx.SquadID), "", parseUUID(testUserID)); !errors.Is(err, service.ErrDuplicatePendingTask) {
t.Fatalf("advanced handoff replay = %v, want coalesced", err)
}
// Re-entering a stage is new work even if an earlier visit is still pending.
updateIssueForTest(t, fx.IssueID, map[string]any{"status": "todo"}).Want(http.StatusOK)
updateIssueForTest(t, fx.IssueID, map[string]any{"status": "in_progress"}).Want(http.StatusOK)
tasks, err = testHandler.Queries.ListTasksByIssue(t.Context(), issue.ID)
if err != nil || len(tasks) != 2 {
t.Fatalf("a new visit to the same stage was coalesced: %+v, %v", tasks, err)
}
}
func TestWorkflowSquadHandoffPreservesPendingStages(t *testing.T) {
seedTestCatalog(t)
for _, sharedLeader := range []bool{false, true} {
for _, oldStatus := range []string{"queued", "dispatched", "running"} {
t.Run(fmt.Sprintf("sharedLeader=%v/old=%s", sharedLeader, oldStatus), func(t *testing.T) {
fx := newSquadBriefingClaimFixture(t, t.Context(), "Pending stage handoff")
nextSquad := fx.SquadID
if sharedLeader {
nextSquad = dbfx.Squad(t, "Next stage squad", fx.AgentID)
}
wf := createTestWorkflow(t, "Pending squad flow "+workflowTestSuffix(), "in_progress", "in_review")
projectID := createWorkflowTestProject(t, "Pending squad project")
setProjectWorkflow(t, projectID, map[string]any{"workflow_id": wf.ID}).Want(http.StatusOK)
for status, squad := range map[string]string{"in_progress": fx.SquadID, "in_review": nextSquad} {
setWorkflowHandoff(t, wf.ID, status, map[string]any{
"handler": map[string]any{"type": "squad", "id": squad}, "instructions": "Handle only " + status,
}).Want(http.StatusOK)
}
dbfx.Exec(t, `UPDATE issue SET project_id=$2,status='todo' WHERE id=$1`, fx.IssueID, projectID)
updateIssueForTest(t, fx.IssueID, map[string]any{"status": "in_progress"}).Want(http.StatusOK)
old, err := testHandler.Queries.ListTasksByIssue(t.Context(), parseUUID(fx.IssueID))
if err != nil || len(old) != 1 {
t.Fatalf("first handoff tasks = %+v, %v", old, err)
}
dbfx.Exec(t, `UPDATE agent_task_queue SET status=$2 WHERE id=$1`, old[0].ID, oldStatus)
// Enter the next stage while the old run still occupies the queue.
updateIssueForTest(t, fx.IssueID, map[string]any{"status": "in_review"}).Want(http.StatusOK)
tasks, err := testHandler.Queries.ListTasksByIssue(t.Context(), parseUUID(fx.IssueID))
if err != nil || len(tasks) != 2 {
t.Fatalf("second handoff tasks = %+v, %v; want both stages persisted", tasks, err)
}
var nextID string
for _, task := range tasks {
if task.ID == old[0].ID {
if task.Status != oldStatus || task.WorkflowStep.String != "in_progress" || task.SquadID != parseUUID(fx.SquadID) {
t.Fatalf("handoff changed the existing run: %+v", task)
}
continue
}
if task.WorkflowStep.String != "in_review" || task.SquadID != parseUUID(nextSquad) || !task.IsLeaderTask {
t.Fatalf("new stage lost its execution context: %+v", task)
}
nextID = uuidToString(task.ID)
}
// Releasing the old run must make the new stage claimable without
// another status update, mention or manually enqueued task.
dbfx.Exec(t, `UPDATE agent_task_queue SET status='completed',completed_at=now() WHERE id=$1`, old[0].ID)
claim := withURLParam(newOwnerDaemonRequest(http.MethodPost, "/api/daemon/runtimes/"+fx.RuntimeID+"/tasks/claim", nil), "runtimeId", fx.RuntimeID)
claim.Header.Set("X-Client-Capabilities", protocol.DaemonCapabilityWorkflowStatusRulesV1)
var resp struct {
Task *AgentTaskResponse `json:"task"`
}
testutil.Call(t, testHandler.ClaimTaskByRuntime, claim).Want(http.StatusOK).JSON(&resp)
if resp.Task == nil || resp.Task.ID != nextID || !resp.Task.IsLeaderTask || resp.Task.ProjectWorkflow == nil || resp.Task.Agent == nil {
t.Fatalf("new stage was not delivered: %+v", resp.Task)
}
if !strings.Contains(resp.Task.ProjectWorkflow.Brief, "Handle only in_review") || strings.Contains(resp.Task.ProjectWorkflow.Brief, "This run remains bound") || !strings.Contains(resp.Task.Agent.Instructions, "Own the parent issue status — through its project workflow") {
t.Fatalf("new stage received stale or guest instructions: %+v", resp.Task)
}
})
}
}
}
func TestWorkflowHandoffToSquadStartsLeaderAtEachStep(t *testing.T) {
seedTestCatalog(t)
fx := newSquadBriefingClaimFixture(t, t.Context(), "Workflow squad handoff")
wf := createTestWorkflow(t, "Squad flow "+workflowTestSuffix(), "in_progress", "in_review")
projectID := createWorkflowTestProject(t, "Squad handoff project")
setProjectWorkflow(t, projectID, map[string]any{"workflow_id": wf.ID}).Want(http.StatusOK)
dbfx.Exec(t, `UPDATE issue SET project_id=$2,status='todo' WHERE id=$1`, fx.IssueID, projectID)
for _, status := range []string{"in_progress", "in_review"} {
instruction := "Coordinate only the " + status + " step."
setWorkflowHandoff(t, wf.ID, status, map[string]any{
"handler": map[string]any{"type": "squad", "id": fx.SquadID}, "instructions": instruction,
"next_status_key": "done", "back_status_key": "todo",
}).Want(http.StatusOK)
var preview WorkflowHandoffPreviewResponse
req := withURLParam(newRequest(http.MethodGet, "/api/issues/"+fx.IssueID+"/workflow-handoff?status="+status, nil), "id", fx.IssueID)
testutil.Call(t, testHandler.PreviewWorkflowHandoff, req).Want(http.StatusOK).JSON(&preview)
if !preview.Handoff || preview.HandlerType != "squad" || preview.HandlerID != fx.SquadID || !strings.Contains(preview.Brief, instruction) {
t.Fatalf("squad handoff preview = %+v", preview)
}
updateIssueForTest(t, fx.IssueID, map[string]any{"status": status}).Want(http.StatusOK)
_, assigneeType, assigneeID := issueStatusAndAssignee(t, fx.IssueID)
if assigneeType != "squad" || assigneeID != fx.SquadID {
t.Fatalf("handoff assignee = %s/%s, want squad/%s", assigneeType, assigneeID, fx.SquadID)
}
tasks, err := testHandler.Queries.ListActiveTasksByIssue(t.Context(), parseUUID(fx.IssueID))
if err != nil || len(tasks) != 1 {
t.Fatalf("active tasks = %+v, %v; want one leader run", tasks, err)
}
task := tasks[0]
if task.AgentID != parseUUID(fx.AgentID) || task.SquadID != parseUUID(fx.SquadID) || !task.IsLeaderTask || task.WorkflowStep.String != status {
t.Fatalf("handoff lost leader role or workflow binding: %+v", task)
}
claim := withURLParam(newOwnerDaemonRequest(http.MethodPost, "/api/daemon/runtimes/"+fx.RuntimeID+"/tasks/claim", nil), "runtimeId", fx.RuntimeID)
claim.Header.Set("X-Client-Capabilities", protocol.DaemonCapabilityWorkflowStatusRulesV1)
var resp struct {
Task *AgentTaskResponse `json:"task"`
}
testutil.Call(t, testHandler.ClaimTaskByRuntime, claim).Want(http.StatusOK).JSON(&resp)
if resp.Task == nil || resp.Task.ID != uuidToString(task.ID) || !resp.Task.IsLeaderTask || resp.Task.Agent == nil || resp.Task.ProjectWorkflow == nil {
t.Fatalf("missing leader workflow claim: %+v", resp.Task)
}
if !strings.Contains(resp.Task.Agent.Instructions, "## Squad Roster") || !strings.Contains(resp.Task.Agent.Instructions, "current step's goal") || !strings.Contains(resp.Task.ProjectWorkflow.Brief, instruction) {
t.Fatalf("claim lost squad coordination or step instructions: %+v", resp.Task)
}
dbfx.Exec(t, `UPDATE agent_task_queue SET status='completed',completed_at=now() WHERE id=$1`, task.ID)
}
}
func TestWorkflowSquadHandoffKeepsAssignmentGates(t *testing.T) {
seedTestCatalog(t)
wf := createTestWorkflow(t, "Squad permissions "+workflowTestSuffix(), "in_progress")
leaderID := seededReadyAgentID(t)
otherWS := dbfx.Workspace(t, "Other squad workspace", "squad-handoff-"+workflowTestSuffix())
privateLeaderID, _, _ := privateAgentTestFixture(t)
cases := []struct {
name, squadID string
status int
}{
{"another workspace", dbfx.Squad(t, "Foreign squad", leaderID, testutil.Cols{"workspace_id": otherWS}), http.StatusBadRequest},
{"archived squad", dbfx.Squad(t, "Archived squad", leaderID, testutil.Cols{"archived_at": time.Now()}), http.StatusBadRequest},
{"private leader", dbfx.Squad(t, "Private leader squad", privateLeaderID), http.StatusForbidden},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
setWorkflowHandoff(t, wf.ID, "in_progress", map[string]any{
"handler": map[string]any{"type": "squad", "id": tc.squadID},
}).Want(tc.status)
})
}
}
func TestDeleteSquadHandsItsWorkflowStepsToTheLeader(t *testing.T) {
seedTestCatalog(t)
leaderID := seededReadyAgentID(t)
squadID := dbfx.Squad(t, "Retired reviewers", leaderID)
otherSquadID := dbfx.Squad(t, "Kept reviewers", leaderID)
wf := createTestWorkflow(t, "Squad archive "+workflowTestSuffix(), "in_progress", "in_review")
projectID := createWorkflowTestProject(t, "Squad archive")
setProjectWorkflow(t, projectID, map[string]any{"workflow_id": wf.ID}).Want(http.StatusOK)
for status, id := range map[string]string{"in_progress": squadID, "in_review": otherSquadID} {
setWorkflowHandoff(t, wf.ID, status, map[string]any{
"handler": map[string]any{"type": "squad", "id": id}, "instructions": "Work on it",
}).Want(http.StatusOK)
}
testutil.Call(t, testHandler.DeleteSquad, squadScopeReq(testUserID, http.MethodDelete, "/api/squads", nil, map[string]string{"id": squadID})).Want(http.StatusNoContent)
issueID := dbfx.Issue(t, "Enters the retired squad's step", testutil.Cols{"project_id": projectID, "status": "todo"})
updateIssueForTest(t, issueID, map[string]any{"status": "in_progress"}).Want(http.StatusOK)
if status, assigneeType, assigneeID := issueStatusAndAssignee(t, issueID); status != "in_progress" || assigneeType != "agent" || assigneeID != leaderID {
t.Fatalf("issue = %s, %s %s; want in_progress with leader agent %s", status, assigneeType, assigneeID, leaderID)
}
row, err := testHandler.Queries.GetIssueWorkflow(t.Context(), db.GetIssueWorkflowParams{ID: parseUUID(wf.ID), WorkspaceID: parseUUID(testWorkspaceID)})
if err != nil {
t.Fatal(err)
}
def, err := issueworkflow.Decode(row)
if err != nil {
t.Fatal(err)
}
if step, _ := def.Step("in_progress"); step.Handler != (issueworkflow.Handler{Type: issueworkflow.HandlerAgent, ID: leaderID}) || step.Instructions != "Work on it" {
t.Fatalf("in_progress step = %+v", step)
}
if step, _ := def.Step("in_review"); step.Handler != (issueworkflow.Handler{Type: issueworkflow.HandlerSquad, ID: otherSquadID}) {
t.Fatalf("in_review step = %+v, want the other squad kept", step)
}
}