Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app/api/agents/[agentName]/next-task/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
createFollowupPrTask,
createGroomTask,
} from "@/lib/agent-task";
import { isBacklogLane, isValidLane, getLaneIds } from "@/lib/lane-config";
import { isBacklogLane, isValidLane, getLaneIds, getBacklogLane } from "@/lib/lane-config";

export async function GET(
request: Request,
Expand Down Expand Up @@ -84,7 +84,7 @@ export async function GET(
const best = candidates[0].issue;
const task = createGroomTask({
agentName,
lane: best.currentLane ?? "backlog",
lane: best.currentLane ?? getBacklogLane()?.id ?? "backlog",
issue: {
repoFullName: best.repository.fullName,
number: best.number,
Expand Down
135 changes: 111 additions & 24 deletions src/app/api/agents/[agentName]/work-summary/route.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { describe, expect, it, vi, beforeEach } from "vitest";

const mockToken = "test-agent-token";
process.env.DISPATCH_AGENT_TOKEN = mockToken;

vi.mock("@/lib/dispatch-env", () => ({
isAuthorizedAgentToken: vi.fn((token) => token === mockToken),
isAuthorizedBearerToken: vi.fn((token) => token === mockToken),
getAcceptedAgentTokens: vi.fn(() => [mockToken]),
resetCaches: vi.fn(),
}));

const { mocks } = vi.hoisted(() => ({
mocks: {
issueFindMany: vi.fn(),
Expand All @@ -16,20 +26,97 @@ vi.mock("@/lib/prisma", () => ({
}));

import { GET } from "./route";
import { resetAuthCaches } from "@/lib/auth";

const TEST_AGENT = "test-agent";

function makeRequest(urlString: string, includeAuth = true) {
const headers: Record<string, string> = {};
if (includeAuth) headers.Authorization = `Bearer ${mockToken}`;
return GET(new Request(urlString, { headers }), {
params: Promise.resolve({ agentName: TEST_AGENT }),
});
}

describe("GET /api/agents/[agentName]/work-summary — auth", () => {
beforeEach(() => {
delete process.env.DISPATCH_AUTH_MODE;
resetAuthCaches();
vi.clearAllMocks();
mocks.issueFindMany.mockResolvedValue([]);
mocks.prFixFindMany.mockResolvedValue([]);
});

it("returns 401 when no auth header is present", async () => {
const res = await makeRequest(
`http://localhost/api/agents/${TEST_AGENT}/work-summary`,
false,
);

expect(res.status).toBe(401);
const body = await res.json();
expect(body.error).toBe("Unauthorized");
});

it("returns 401 for bad bearer token", async () => {
const headers: Record<string, string> = { Authorization: "Bearer wrong-token" };
const res = await GET(
new Request(`http://localhost/api/agents/${TEST_AGENT}/work-summary`, { headers }),
{ params: Promise.resolve({ agentName: TEST_AGENT }) },
);

expect(res.status).toBe(401);
const body = await res.json();
expect(body.error).toBe("Unauthorized");
});

it("authorized request preserves existing summary response", async () => {
mocks.issueFindMany.mockResolvedValue([]);
mocks.prFixFindMany.mockResolvedValue([]);

const res = await makeRequest(
`http://localhost/api/agents/${TEST_AGENT}/work-summary`,
);

expect(res.status).toBe(200);
const body = await res.json();
expect(body.agentName).toBe(TEST_AGENT);
expect(body.issues).toHaveProperty("normal");
expect(body.prFixes).toHaveProperty("normal");
});

it("unauthorized request does not call prisma.issue.findMany", async () => {
await makeRequest(
`http://localhost/api/agents/${TEST_AGENT}/work-summary`,
false,
);

expect(mocks.issueFindMany).not.toHaveBeenCalled();
});

it("unauthorized request does not call listQueuedPrFixItems", async () => {
await makeRequest(
`http://localhost/api/agents/${TEST_AGENT}/work-summary`,
false,
);

expect(mocks.prFixFindMany).not.toHaveBeenCalled();
});
});

describe("GET /api/agents/[agentName]/work-summary", () => {
beforeEach(() => {
delete process.env.DISPATCH_AUTH_MODE;
resetAuthCaches();
vi.clearAllMocks();
mocks.issueFindMany.mockResolvedValue([]);
mocks.prFixFindMany.mockResolvedValue([]);
});

it("returns agent name and empty lane counts when no issues or PR fixes exist", async () => {
const res = await GET(new Request(`http://localhost/api/agents/${TEST_AGENT}/work-summary`), {
params: Promise.resolve({ agentName: TEST_AGENT }),
});
const res = await makeRequest(
`http://localhost/api/agents/${TEST_AGENT}/work-summary`,
);

expect(res.status).toBe(200);
const body = await res.json();
Expand Down Expand Up @@ -58,9 +145,9 @@ describe("GET /api/agents/[agentName]/work-summary", () => {
{ labels: ["status/backlog"], currentLane: "backlog" },
]);

const res = await GET(new Request(`http://localhost/api/agents/${TEST_AGENT}/work-summary`), {
params: Promise.resolve({ agentName: TEST_AGENT }),
});
const res = await makeRequest(
`http://localhost/api/agents/${TEST_AGENT}/work-summary`,
);

expect(res.status).toBe(200);
const body = await res.json();
Expand All @@ -75,9 +162,9 @@ describe("GET /api/agents/[agentName]/work-summary", () => {
{ labels: ["type/bug"], currentLane: "normal" },
]);

const res = await GET(new Request(`http://localhost/api/agents/${TEST_AGENT}/work-summary`), {
params: Promise.resolve({ agentName: TEST_AGENT }),
});
const res = await makeRequest(
`http://localhost/api/agents/${TEST_AGENT}/work-summary`,
);

expect(res.status).toBe(200);
const body = await res.json();
Expand All @@ -94,9 +181,9 @@ describe("GET /api/agents/[agentName]/work-summary", () => {
{ lane: "NORMAL", status: "BLOCKED" },
]);

const res = await GET(new Request(`http://localhost/api/agents/${TEST_AGENT}/work-summary`), {
params: Promise.resolve({ agentName: TEST_AGENT }),
});
const res = await makeRequest(
`http://localhost/api/agents/${TEST_AGENT}/work-summary`,
);

expect(res.status).toBe(200);
const body = await res.json();
Expand All @@ -110,9 +197,9 @@ describe("GET /api/agents/[agentName]/work-summary", () => {
{ lane: null, status: "QUEUED" },
]);

const res = await GET(new Request(`http://localhost/api/agents/${TEST_AGENT}/work-summary`), {
params: Promise.resolve({ agentName: TEST_AGENT }),
});
const res = await makeRequest(
`http://localhost/api/agents/${TEST_AGENT}/work-summary`,
);

expect(res.status).toBe(200);
const body = await res.json();
Expand All @@ -124,9 +211,9 @@ describe("GET /api/agents/[agentName]/work-summary", () => {
{ labels: ["status/ready"], currentLane: null },
]);

const res = await GET(new Request(`http://localhost/api/agents/${TEST_AGENT}/work-summary`), {
params: Promise.resolve({ agentName: TEST_AGENT }),
});
const res = await makeRequest(
`http://localhost/api/agents/${TEST_AGENT}/work-summary`,
);

expect(res.status).toBe(200);
const body = await res.json();
Expand All @@ -138,9 +225,9 @@ describe("GET /api/agents/[agentName]/work-summary", () => {
{ labels: ["status/done"], currentLane: "normal" },
]);

const res = await GET(new Request(`http://localhost/api/agents/${TEST_AGENT}/work-summary`), {
params: Promise.resolve({ agentName: TEST_AGENT }),
});
const res = await makeRequest(
`http://localhost/api/agents/${TEST_AGENT}/work-summary`,
);

expect(res.status).toBe(200);
const body = await res.json();
Expand All @@ -150,9 +237,9 @@ describe("GET /api/agents/[agentName]/work-summary", () => {
it("returns 500 on database error", async () => {
mocks.issueFindMany.mockRejectedValue(new Error("connection refused"));

const res = await GET(new Request(`http://localhost/api/agents/${TEST_AGENT}/work-summary`), {
params: Promise.resolve({ agentName: TEST_AGENT }),
});
const res = await makeRequest(
`http://localhost/api/agents/${TEST_AGENT}/work-summary`,
);

expect(res.status).toBe(500);
const body = await res.json();
Expand Down
15 changes: 11 additions & 4 deletions src/app/api/agents/[agentName]/work-summary/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NextResponse } from "next/server";
import { authorizeRequest } from "@/lib/auth";
import { prisma, asPrFixQueueClient } from "@/lib/prisma";
import { listQueuedPrFixItems } from "@/lib/pr-fix-queue";
import { VALID_LANES } from "@/types";
import { getConfiguredLanes, getDefaultClaimableLane } from "@/lib/lane-config";

type WorkSummaryLaneCounts = { queued: number; inProgress: number };
type PrFixLaneCounts = { total: number; blocked: number };
Expand All @@ -24,6 +25,10 @@ function classifyIssueStatus(labels: string[]): "queued" | "inProgress" {
export async function GET(request: Request, { params }: { params: Promise<{ agentName: string }> }) {
const { agentName } = await params;

if (!(await authorizeRequest(request)).authorized) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}

try {
const issues = await prisma.issue.findMany({
where: {
Expand All @@ -36,13 +41,15 @@ export async function GET(request: Request, { params }: { params: Promise<{ agen
},
});

const configuredLanes = getConfiguredLanes();
const laneCounts: Record<string, WorkSummaryLaneCounts> = {};
for (const lane of VALID_LANES) {
laneCounts[lane] = { queued: 0, inProgress: 0 };
for (const lane of configuredLanes) {
laneCounts[lane.id] = { queued: 0, inProgress: 0 };
}

for (const issue of issues) {
const lane = (issue.currentLane ?? "normal").toLowerCase();
const defaultLane = getDefaultClaimableLane()?.id ?? "normal";
const lane = (issue.currentLane ?? defaultLane).toLowerCase();
if (!laneCounts[lane]) continue;

const status = classifyIssueStatus(issue.labels);
Expand Down
5 changes: 3 additions & 2 deletions src/app/api/issues/groom/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { removeIssueLabel, addIssueLabel } from "@/lib/github";
import { authorizeRequest } from "@/lib/auth";
import { getEscalationLane } from "@/lib/lane-config";

/**
* Resolve the actor name for grooming attribution.
Expand Down Expand Up @@ -148,8 +149,8 @@ export async function POST(request: Request) {
}

case "escalate": {
// Set lane to escalated
groomingData.currentLane = "escalated";
const escalationLane = getEscalationLane();
groomingData.currentLane = escalationLane?.id ?? "escalated";
groomingData.nextGroomingAction = "Implement or decompose into actionable sub-tasks";
break;
}
Expand Down
3 changes: 2 additions & 1 deletion src/app/api/issues/reconcile/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
shouldReclassifyStaleBacklog,
executeActions,
} from "@/lib/issue-reconciliation";
import { isBacklogLane } from "@/lib/lane-config";
import { computeLinkedPrHealth, toPersistedLinkedPrHealth, type LinkedPrHealth } from "@/lib/linked-pr-health";
import { authorizeRequest } from "@/lib/auth";
import { reconcileStalePrFixItems } from "@/lib/pr-fix-queue";
Expand Down Expand Up @@ -216,7 +217,7 @@ export async function POST(request: Request) {
data: { currentLane: classification.lane },
});
totalLaneClassified++;
} else if (existingIssue && existingIssue.currentLane === "backlog") {
} else if (existingIssue && existingIssue.currentLane && isBacklogLane(existingIssue.currentLane)) {
// Stale-backlog reclassification: the issue has an active status label
// but is stuck in the backlog lane. Reclassify to normal or escalated.
const reclassify = shouldReclassifyStaleBacklog(
Expand Down
Loading