Skip to content

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

Go
5.5 KB
Raw Blame
package handler

import (
	"context"
	"os"
	"path/filepath"
	"strings"
	"testing"

	"github.com/multica-ai/multica/server/internal/daemon/execenv"
)

// The two tests below are composition tests, not text-presence tests. The
// parent-status contract is split across two systems that never see each
// other: the squad briefing (server-side, appended to the leader's
// Instructions) and the runtime brief (daemon-side CLAUDE.md). Asserting each
// half in isolation is exactly how the original contradiction shipped — the
// briefing said "move it to in_review when the goal is met" while the runtime
// brief said "do not change status unless the comment asks", and a member's
// delivery comment never asks.
//
// So each test assembles both halves for one real scenario and asserts the
// combined instruction set points one way.

// leaderCommentRuntimeBrief renders the CLAUDE.md a squad leader receives on a
// comment-triggered turn.
func leaderCommentRuntimeBrief(t *testing.T, instructions string) string {
	t.Helper()
	dir := t.TempDir()
	if _, err := execenv.InjectRuntimeConfig(dir, "claude", execenv.TaskContextForEnv{
		IssueID:           "issue-1",
		TriggerCommentID:  "comment-1",
		AgentInstructions: instructions,
		IsSquadLeader:     true,
	}); err != nil {
		t.Fatalf("InjectRuntimeConfig: %v", err)
	}
	data, err := os.ReadFile(filepath.Join(dir, "CLAUDE.md"))
	if err != nil {
		t.Fatalf("read CLAUDE.md: %v", err)
	}
	return string(data)
}

// TestSquadAssignedLeaderCanWrapUpOnCommentTurn covers the squad's most common
// shape: work dispatched by @mention with no child issues, so no child-done
// system comment ever arrives to carry an explicit status ask. The member
// simply posts "done". The leader must still be able to close the parent out.
func TestSquadAssignedLeaderCanWrapUpOnCommentTurn(t *testing.T) {
	ctx := context.Background()
	leaderID, _ := seededLeaderAgent(t)
	squad := seedSquadForBriefing(t, leaderID, "Owning Squad", "")

	// The issue is assigned to this squad → the server grants status ownership.
	briefing := buildSquadLeaderBriefing(ctx, testHandler.Queries, squad, true, leaderNoWorkflow, false)
	brief := leaderCommentRuntimeBrief(t, briefing)

	if !strings.Contains(briefing, "Own the parent issue status") {
		t.Fatalf("squad-assigned briefing must grant status ownership:\n%s", briefing)
	}

	// The runtime brief must not carry a blanket no-status-change rule: any
	// unqualified form of it contradicts the grant this leader just received.
	if strings.Contains(brief, "Do NOT change the issue status") {
		t.Error("leader runtime brief still carries an unqualified no-status-change rule, " +
			"which contradicts the Own-the-parent-issue-status grant")
	}
	for _, want := range []string{
		// MUL-6417: the brief's status rule is a fact judgment, and the
		// leader bullet must point the same way as the briefing's grant —
		// in_review is reached on the confirming turn, not on dispatch.
		"dispatching members is not delivery",
		"a dispatch turn leaves the parent `in_progress`",
		"where you confirm the overall goal is met",
	} {
		if !strings.Contains(brief, want) {
			t.Errorf("leader runtime brief missing %q\n--- brief ---\n%s", want, brief)
		}
	}

	// End to end: both halves must agree that in_review is reachable here.
	combined := briefing + "\n" + brief
	if !strings.Contains(combined, "multica issue status <issue-id> in_review") {
		t.Error("combined instructions never tell the owning leader how to wrap up")
	}
}

// TestGuestLeaderCannotChangeStatusOnCommentTurn is the other half of the
// scope fix (MUL-3724 path): the issue belongs to a plain agent and this squad
// was only @mentioned for help. The briefing still gets injected — the leader
// needs its roster — but no combination of the two halves may authorize a
// status change on someone else's issue.
func TestGuestLeaderCannotChangeStatusOnCommentTurn(t *testing.T) {
	ctx := context.Background()
	leaderID, _ := seededLeaderAgent(t)
	squad := seedSquadForBriefing(t, leaderID, "Guest Squad", "")

	// The issue is assigned to someone else → no status ownership.
	briefing := buildSquadLeaderBriefing(ctx, testHandler.Queries, squad, false, leaderNoWorkflow, false)
	brief := leaderCommentRuntimeBrief(t, briefing)

	// The leader still gets the coordination context it was pulled in for —
	// withholding status authority must not withhold the roster too.
	for _, want := range []string{
		"## Squad Roster",
		"Leader (you):",
		"Delegate by @mention",
		"Record your evaluation",
	} {
		if !strings.Contains(briefing, want) {
			t.Fatalf("guest leader lost coordination context %q:\n%s", want, briefing)
		}
	}

	// But the grant is absent: the briefing's prohibition governs (Agent
	// Identity outranks the workflow), and the brief's fact-judgment rule
	// (MUL-6417) hands the guest no runnable status command to contradict it.
	if strings.Contains(briefing, "Own the parent issue status") {
		t.Errorf("guest leader must not receive the status-ownership grant:\n%s", briefing)
	}
	combined := briefing + "\n" + brief
	if strings.Contains(combined, "multica issue status <issue-id> in_review") {
		t.Error("combined instructions hand a guest leader an in_review command for " +
			"an issue assigned to someone else")
	}
	// The prohibition wraps across source lines, so match on compacted text.
	compact := strings.Join(strings.Fields(briefing), " ")
	for _, want := range []string{
		"Do NOT change this issue's status",
		"never run `multica issue status` on it",
	} {
		if !strings.Contains(compact, want) {
			t.Errorf("guest-leader briefing missing %q\n--- briefing ---\n%s", want, briefing)
		}
	}
}