Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
15.6 KB
Raw Blame
package handler

import (
	"context"
	"encoding/json"
	"fmt"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"github.com/multica-ai/multica/server/internal/featureflags"
	"github.com/multica-ai/multica/server/internal/testutil"
	"github.com/multica-ai/multica/server/pkg/protocol"
)

// claimAgentInstructionsForTest claims the next queued task for runtimeID and
// returns the claimed task id, the agent Instructions carried on the claim
// response (the field the squad-leader briefing is injected into), and the
// is_leader_task flag the daemon derives its squad-leader role from
// (MUL-5811). Empty task id means no task was claimed.
func claimAgentInstructionsForTest(t *testing.T, runtimeID string) (taskID string, instructions string, isLeaderTask bool, raw string) {
	t.Helper()

	w := httptest.NewRecorder()
	req := newOwnerDaemonRequest("POST", "/api/daemon/runtimes/"+runtimeID+"/tasks/claim", nil)
	req = withURLParam(req, "runtimeId", runtimeID)

	testHandler.ClaimTaskByRuntime(w, req)
	if w.Code != http.StatusOK {
		t.Fatalf("ClaimTaskByRuntime: expected 200, got %d: %s", w.Code, w.Body.String())
	}

	var resp struct {
		Task *struct {
			ID                 string `json:"id"`
			IsLeaderTask       bool   `json:"is_leader_task"`
			LeaderRoleResolved bool   `json:"leader_role_resolved"`
			Agent              *struct {
				Instructions string `json:"instructions"`
			} `json:"agent"`
		} `json:"task"`
	}
	if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
		t.Fatalf("decode claim response: %v", err)
	}
	if resp.Task == nil {
		return "", "", false, w.Body.String()
	}
	// Every claim must advertise the capability, leader or not: its absence is
	// how a daemon detects a server too old to resolve the role, and silently
	// dropping it would send every upgraded daemon back to inferring the role
	// from instructions text — the bug MUL-5811 removed.
	if !resp.Task.LeaderRoleResolved {
		t.Fatalf("claim response must set leader_role_resolved=true: %s", w.Body.String())
	}
	var instr string
	if resp.Task.Agent != nil {
		instr = resp.Task.Agent.Instructions
	}
	return resp.Task.ID, instr, resp.Task.IsLeaderTask, w.Body.String()
}

// squadBriefingClaimFixture wires a runtime + leader agent + squad and returns
// the IDs needed to enqueue leader tasks against that runtime.
type squadBriefingClaimFixture struct {
	RuntimeID string
	AgentID   string // squad leader, has the runtime and empty instructions
	SquadID   string
	IssueID   string // assignee_type='agent' (NOT squad) — reproduces MUL-3724
}

func newSquadBriefingClaimFixture(t *testing.T, ctx context.Context, name string) squadBriefingClaimFixture {
	t.Helper()

	runtimeID := createClaimReclaimRuntime(t, ctx, name+" runtime")
	// Leader agent + an issue assigned to that agent (assignee_type='agent').
	agentID, issueID := createClaimReclaimAgentAndIssue(t, ctx, runtimeID, name+" leader")
	// Force empty instructions so the test asserts the briefing alone — this
	// mirrors MUL-3724 where the leader's own instructions were blank.
	if _, err := testPool.Exec(ctx, `UPDATE agent SET instructions = '' WHERE id = $1`, agentID); err != nil {
		t.Fatalf("clear leader instructions: %v", err)
	}
	// Make the issue assignee an agent (NOT the squad). The pre-fix code only
	// injected the briefing when issue.assignee_type='squad', so this is the
	// exact gap the fix closes: a comment @squad-mention leader task running on
	// an agent-assigned issue.
	if _, err := testPool.Exec(ctx, `UPDATE issue SET assignee_type = 'agent', assignee_id = $2 WHERE id = $1`, issueID, agentID); err != nil {
		t.Fatalf("set issue agent assignee: %v", err)
	}

	var squadID string
	if err := testPool.QueryRow(ctx, `
		INSERT INTO squad (workspace_id, name, description, leader_id, creator_id)
		VALUES ($1, $2, '', $3, $4)
		RETURNING id
	`, testWorkspaceID, name+" squad", agentID, testUserID).Scan(&squadID); err != nil {
		t.Fatalf("create squad: %v", err)
	}
	t.Cleanup(func() { testPool.Exec(context.Background(), `DELETE FROM squad WHERE id = $1`, squadID) })

	return squadBriefingClaimFixture{
		RuntimeID: runtimeID,
		AgentID:   agentID,
		SquadID:   squadID,
		IssueID:   issueID,
	}
}

func enqueueClaimTask(t *testing.T, ctx context.Context, fx squadBriefingClaimFixture, isLeader bool, withSquadID bool) string {
	t.Helper()
	var taskID string
	var squadArg any
	if withSquadID {
		squadArg = fx.SquadID
	} else {
		squadArg = nil
	}
	if err := testPool.QueryRow(ctx, `
		INSERT INTO agent_task_queue (agent_id, runtime_id, issue_id, status, priority, is_leader_task, squad_id)
		VALUES ($1, $2, $3, 'queued', 0, $4, $5)
		RETURNING id
	`, fx.AgentID, fx.RuntimeID, fx.IssueID, isLeader, squadArg).Scan(&taskID); err != nil {
		t.Fatalf("enqueue claim task: %v", err)
	}
	t.Cleanup(func() { testPool.Exec(context.Background(), `DELETE FROM agent_task_queue WHERE id = $1`, taskID) })
	return taskID
}

// TestClaim_LeaderTaskFromCommentMention_InjectsBriefing is the MUL-3724
// reproduction: a leader task (is_leader_task=true) carrying a squad_id, on an
// issue assigned to a plain AGENT (not the squad). The pre-fix gate
// (issue.assignee_type='squad') would NOT inject the briefing here, so the
// leader booted with no squad context and degraded into doing the work itself.
// After the fix the briefing is keyed off the task flag + squad_id, so it is
// injected regardless of issue assignee.
func TestClaim_LeaderTaskFromCommentMention_InjectsBriefing(t *testing.T) {
	if testHandler == nil || testPool == nil {
		t.Skip("database not available")
	}
	ctx := context.Background()
	fx := newSquadBriefingClaimFixture(t, ctx, "Briefing inject")
	want := enqueueClaimTask(t, ctx, fx, true /*isLeader*/, true /*withSquadID*/)

	got, instr, isLeader, raw := claimAgentInstructionsForTest(t, fx.RuntimeID)
	if got != want {
		t.Fatalf("claimed task id = %q, want %q: %s", got, want, raw)
	}
	if !strings.Contains(instr, "## Squad Operating Protocol") || !strings.Contains(instr, "## Squad Roster") {
		t.Fatalf("expected squad-leader briefing in agent instructions, got:\n%s", instr)
	}
	// The daemon reads its leader role off this flag (MUL-5811), so an
	// injected briefing must arrive with the flag set.
	if !isLeader {
		t.Fatalf("claim injected the briefing but reported is_leader_task=false: %s", raw)
	}
}

func TestClaim_LeaderTaskWithMovedStepLeavesStatusUnchanged(t *testing.T) {
	seedTestCatalog(t)
	for _, bound := range []string{"waiting_for_merge", "in_progress", ""} {
		for _, daemon := range []string{"legacy", "commands", "map"} {
			t.Run(fmt.Sprintf("bound=%s/%s", bound, daemon), func(t *testing.T) {
				fx := newSquadBriefingClaimFixture(t, t.Context(), "Workflow leader")
				projectID := deliveryProject(t, "Leader workflow project", fx.AgentID)
				dbfx.Exec(t, `UPDATE issue SET project_id=$2,status='in_progress',assignee_type='squad',assignee_id=$3 WHERE id=$1`, fx.IssueID, projectID, fx.SquadID)
				cols := testutil.Cols{
					"runtime_id": fx.RuntimeID, "issue_id": fx.IssueID,
					"is_leader_task": true, "squad_id": fx.SquadID,
				}
				if bound != "" {
					cols["workflow_step"] = bound
				}
				taskID := dbfx.Task(t, fx.AgentID, cols)
				req := withURLParam(newOwnerDaemonRequest(http.MethodPost, "/api/daemon/runtimes/"+fx.RuntimeID+"/tasks/claim", nil), "runtimeId", fx.RuntimeID)
				switch daemon {
				case "commands":
					req.Header.Set("X-Client-Capabilities", protocol.DaemonCapabilityWorkflowStatusRulesV1)
				case "map":
					req.Header.Set("X-Client-Capabilities", protocol.DaemonCapabilityWorkflowStatusRulesV1+","+protocol.DaemonCapabilityWorkflowStatusMapV1)
				}
				var resp struct {
					Task *AgentTaskResponse `json:"task"`
				}
				testutil.Call(t, testHandler.ClaimTaskByRuntime, req).Want(http.StatusOK).JSON(&resp)
				if resp.Task == nil || resp.Task.ID != taskID || !resp.Task.IsLeaderTask || resp.Task.Agent == nil || resp.Task.ProjectWorkflow == nil {
					t.Fatalf("missing workflow leader claim: %+v", resp.Task)
				}
				instructions := resp.Task.Agent.Instructions
				if resp.Task.ProjectWorkflow.StatusMap != (daemon == "map") {
					t.Fatalf("status_map = %v for a %s daemon", resp.Task.ProjectWorkflow.StatusMap, daemon)
				}
				wrapUp := "run that block's Step done command"
				if daemon == "map" {
					wrapUp = "move it to the status the map gives for that step's finished work"
				}
				compact := strings.Join(strings.Fields(instructions), " ")
				if !strings.Contains(instructions, "## Squad Roster") {
					t.Fatal("workflow restriction removed the leader's coordination briefing")
				}
				if bound == "waiting_for_merge" {
					if !strings.Contains(instructions, "Leave the parent issue's status unchanged") || strings.Contains(compact, wrapUp) {
						t.Fatalf("stale leader's instructions contradict its workflow brief:\n%s", instructions)
					}
					for _, move := range builtInParentMoves {
						if strings.Contains(strings.Join(strings.Fields(instructions), " "), move) {
							t.Errorf("stale leader received built-in move %q", move)
						}
					}
				} else if !strings.Contains(compact, wrapUp) {
					t.Fatalf("current or unbound leader lost its wrap-up instruction:\n%s", instructions)
				}
			})
		}
	}
}

// TestClaim_NonLeaderTask_NoBriefing guards the negative: a task that is NOT a
// leader task (is_leader_task=false), even with a squad_id present, must not
// receive the briefing. This keeps worker/mention runs free of leader framing.
func TestClaim_NonLeaderTask_NoBriefing(t *testing.T) {
	if testHandler == nil || testPool == nil {
		t.Skip("database not available")
	}
	ctx := context.Background()
	fx := newSquadBriefingClaimFixture(t, ctx, "Briefing nonleader")
	enqueueClaimTask(t, ctx, fx, false /*isLeader*/, true /*withSquadID*/)

	_, instr, isLeader, raw := claimAgentInstructionsForTest(t, fx.RuntimeID)
	if strings.Contains(instr, "## Squad Operating Protocol") || strings.Contains(instr, "## Squad Roster") {
		t.Fatalf("non-leader task must NOT get squad briefing, got:\n%s", instr)
	}
	if isLeader {
		t.Fatalf("non-leader task must not report is_leader_task=true: %s", raw)
	}
}

// TestClaim_DocsEnabledFollowsFlag pins docs_enabled on the claim: the daemon
// writes `multica doc` into the agent brief only when the server says so.
func TestClaim_DocsEnabledFollowsFlag(t *testing.T) {
	if testHandler == nil || testPool == nil {
		t.Skip("database not available")
	}
	for _, enabled := range []bool{false, true} {
		t.Run(fmt.Sprintf("docs_v1=%v", enabled), func(t *testing.T) {
			withFeatureFlag(t, testHandler, featureflags.DocsV1, enabled)
			ctx := context.Background()
			fx := newSquadBriefingClaimFixture(t, ctx, fmt.Sprintf("Docs flag %v", enabled))
			enqueueClaimTask(t, ctx, fx, false /*isLeader*/, true /*withSquadID*/)

			_, _, _, raw := claimAgentInstructionsForTest(t, fx.RuntimeID)
			if got := strings.Contains(raw, `"docs_enabled":true`); got != enabled {
				t.Fatalf("docs_enabled in claim = %v, want %v: %s", got, enabled, raw)
			}
		})
	}
}

// TestClaim_LeaderTaskWithDanglingSquadID_NoBriefing is the load-bearing
// contract for dropping the FK on agent_task_queue.squad_id (migration 127):
// when a squad is hard-deleted AFTER a leader task was enqueued, the task row
// keeps a now-dangling squad_id. The claim must still succeed (HTTP 200, task
// delivered) and simply skip briefing injection — GetSquadInWorkspace returns
// no row, so the err != nil guard makes this identical to "condition not
// matched". Never a 500, never a stale/empty briefing. Without the FK nothing
// in the DB prevents the dangling row, so this guard lives entirely in the
// claim handler and must stay tested.
func TestClaim_LeaderTaskWithDanglingSquadID_NoBriefing(t *testing.T) {
	if testHandler == nil || testPool == nil {
		t.Skip("database not available")
	}
	ctx := context.Background()
	fx := newSquadBriefingClaimFixture(t, ctx, "Briefing dangling")
	want := enqueueClaimTask(t, ctx, fx, true /*isLeader*/, true /*withSquadID*/)

	// Hard-delete the squad AFTER enqueue, leaving task.squad_id dangling.
	// There is no FK (migration 127), so the task row is untouched.
	if _, err := testPool.Exec(ctx, `DELETE FROM squad WHERE id = $1`, fx.SquadID); err != nil {
		t.Fatalf("delete squad: %v", err)
	}
	// Confirm the task still carries the (now orphaned) squad_id — i.e. the
	// delete did not cascade/null it, which is the whole point of no-FK.
	var stillSet bool
	if err := testPool.QueryRow(ctx,
		`SELECT squad_id = $2 FROM agent_task_queue WHERE id = $1`, want, fx.SquadID,
	).Scan(&stillSet); err != nil {
		t.Fatalf("reload task squad_id: %v", err)
	}
	if !stillSet {
		t.Fatalf("expected task.squad_id to remain the dangling UUID after squad delete (no FK)")
	}

	got, instr, isLeader, raw := claimAgentInstructionsForTest(t, fx.RuntimeID)
	if got != want {
		t.Fatalf("claimed task id = %q, want %q (claim must still succeed 200): %s", got, want, raw)
	}
	if strings.Contains(instr, "## Squad Operating Protocol") || strings.Contains(instr, "## Squad Roster") {
		t.Fatalf("dangling squad_id must NOT get squad briefing, got:\n%s", instr)
	}
	// A leader task with no briefing has no roster to delegate to, so the
	// daemon must not run it in the leader role either (MUL-5811).
	if isLeader {
		t.Fatalf("skipped injection must clear is_leader_task on the claim response: %s", raw)
	}
}

// leader tasks enqueued before migration 127 (or by an old binary) have a NULL
// squad_id. The claim handler must skip injection rather than panic or guess —
// equivalent to the pre-fix "condition not matched" behavior, never a stale
// briefing.
func TestClaim_LeaderTaskWithoutSquadID_NoBriefing(t *testing.T) {
	if testHandler == nil || testPool == nil {
		t.Skip("database not available")
	}
	ctx := context.Background()
	fx := newSquadBriefingClaimFixture(t, ctx, "Briefing nullsquad")
	enqueueClaimTask(t, ctx, fx, true /*isLeader*/, false /*withSquadID*/)

	_, instr, isLeader, raw := claimAgentInstructionsForTest(t, fx.RuntimeID)
	if strings.Contains(instr, "## Squad Operating Protocol") || strings.Contains(instr, "## Squad Roster") {
		t.Fatalf("leader task with NULL squad_id must NOT get squad briefing, got:\n%s", instr)
	}
	if isLeader {
		t.Fatalf("skipped injection must clear is_leader_task on the claim response: %s", raw)
	}
}

// TestClaim_LeaderSwappedAfterEnqueue_NoBriefingAndNoLeaderRole covers the
// third skip path: the squad still exists, but its leader was reassigned after
// this task was enqueued, so the claiming agent is no longer the leader. The
// defensive gate already withheld the briefing; the flag must follow it down,
// otherwise the daemon would boot a former leader into the leader role with no
// roster and no protocol (MUL-5811).
func TestClaim_LeaderSwappedAfterEnqueue_NoBriefingAndNoLeaderRole(t *testing.T) {
	if testHandler == nil || testPool == nil {
		t.Skip("database not available")
	}
	ctx := context.Background()
	fx := newSquadBriefingClaimFixture(t, ctx, "Briefing swapped")
	want := enqueueClaimTask(t, ctx, fx, true /*isLeader*/, true /*withSquadID*/)

	// Hand the squad to a different agent AFTER enqueue.
	otherAgentID, _ := createClaimReclaimAgentAndIssue(t, ctx, fx.RuntimeID, "Briefing swapped newleader")
	if _, err := testPool.Exec(ctx, `UPDATE squad SET leader_id = $2 WHERE id = $1`, fx.SquadID, otherAgentID); err != nil {
		t.Fatalf("swap squad leader: %v", err)
	}

	got, instr, isLeader, raw := claimAgentInstructionsForTest(t, fx.RuntimeID)
	if got != want {
		t.Fatalf("claimed task id = %q, want %q (claim must still succeed 200): %s", got, want, raw)
	}
	if strings.Contains(instr, "## Squad Operating Protocol") || strings.Contains(instr, "## Squad Roster") {
		t.Fatalf("former leader must NOT get squad briefing, got:\n%s", instr)
	}
	if isLeader {
		t.Fatalf("skipped injection must clear is_leader_task on the claim response: %s", raw)
	}
}