feat: add agent next-task endpoint#403
Conversation
Define discriminated union AgentTask with four task types: - idle: no work available - implement: claim or work an issue, open/update one PR - followup-pr: fix an existing PR - groom: enrich issues with labels, lane, and status Include helper builders (createIdleTask, createImplementTask, createFollowupPrTask, createGroomTask) with strict default instructions and forbidden actions. No API routes, no DB changes.
Superseded by a newer automated review for this pull request.
There was a problem hiding this comment.
AI Automated Review
Full PR review.
Analysis engine: MiniMax-M2.7@https://litellm.jory.dev/v1 (anthropic) — routed smart (risk match: public_route_changes)
PR PR 403: feat: add agent next-task endpoint
Recommendation
Approve. The new GET /api/agents/[agentName]/next-task endpoint is correctly implemented as a read-only preflight endpoint that follows existing repository conventions for agent queue endpoints.
Change-by-Change Findings
src/app/api/agents/[agentName]/next-task/route.ts (new file, 135 lines)
- Adds a GET handler that returns exactly one
AgentTaskfor an agent - Correctly queries PR-fix queue items first (precedence), then falls back to issues
- Uses
findLeasedIssueIdsto filter already-leased issues, respecting concurrent work safety - Passes through existing query params:
lane,exclude_decomposed,includeClaimed,includeRenovate - Returns
idletask when no work is available (harness-friendly) - No database mutations — verified by tests checking
issueFindManyis called withstate: "open"filter
src/app/api/agents/[agentName]/next-task/route.test.ts (new file, 397 lines)
- 13 comprehensive tests covering: empty queue, single implement task, followup-pr precedence, linked issue context, reason deduplication, lane filtering, includeClaimed behavior, no harness-specific fields, and mutation verification
Standards Compliance
✅ Agent Task Contract: Endpoint reuses createIdleTask, createImplementTask, createFollowupPrTask from src/lib/agent-task.ts, ensuring consistency with the harness-agnostic contract added in PR 402.
✅ Queue Integration: Reuses buildAgentQueue from src/lib/agent-queue.ts, listQueuedPrFixItems from src/lib/pr-fix-queue.ts — consistent with existing agent queue patterns.
✅ PR-Fix Precedence: Correctly prioritizes PR-fix items over issue work per the worker execution contract.
✅ Repository Conventions: Read-only endpoint follows the established pattern of other GET agent endpoints (e.g., /queue) that do not require explicit auth. Mutating endpoints (e.g., /agent-runs POST) require DISPATCH_AGENT_TOKEN bearer auth per AGENTS.md.
Linked Issue Fit
Issue PR 395 acceptance criteria verified:
- ✅ "no LLM required to check for work" — endpoint returns structured
AgentTaskwithshouldRunboolean - ✅ "one task per response" — tests explicitly verify
Array.isArray(body) === false - ✅ "compatible with existing queue logic" — uses
buildAgentQueuewith same filtering options - ✅ "idle response lets any harness stop cleanly" — returns
createIdleTask("No work available")
Issue states this closes PR 395. ✅
Required Checks
1. verify route access controls are in place
✅ Verified — The endpoint follows the repository's established pattern for read-only agent queue endpoints. The /api/agents/[agentName]/queue endpoint (documented in AGENTS.md) similarly requires no explicit auth for GET operations. Authentication (DISPATCH_AGENT_TOKEN bearer auth) is required only for mutating agent endpoints per README: "Agent/Worker Auth (DISPATCH_AGENT_TOKEN): Bearer token authentication for API calls from agents... This is required for all mutating API endpoints." The new endpoint only reads data and does not mutate state, consistent with unprotected read-only endpoints.
2. check for unintended public endpoints
✅ Verified — No authentication bypass or middleware changes introduced. The endpoint is scoped to /api/agents/[agentName]/next-task and only returns queue position data. It exposes no additional repository metadata beyond what is already accessible via the existing /api/agents/[agentName]/queue endpoint.
Evidence Provider Findings
None.
Tool Harness Findings
- File read of
route.tsconfirmed implementation matches diff - No middleware, auth, or bearer token patterns found in the new route file (expected — follows existing read-only pattern)
- Attempted read of sibling
route.tsin[agentName]directory failed (not required for this review)
Unknowns / Needs Verification
None. CI checks (Docker Build, Validate) passed successfully.
Closes #395.
Adds
GET /api/agents/[agentName]/next-task— a cheap preflight that returns exactly oneAgentTaskfor an agent.Behavior
followup-prtask when a queued PR-fix item is firstissue.repoFullName,issue.number) when the PR-fix has an issue numberreasonsfrom both the high-levelreasonandfeedback[](deduplicated)implementtask when an issue is first in the queueidlewhen nothing is available (harness exits without burning tokens)lane,exclude_decomposed,includeClaimed,includeRenovateChanges
src/app/api/agents/[agentName]/next-task/route.ts— endpoint handlersrc/app/api/agents/[agentName]/next-task/route.test.ts— 13 testsReuses
buildAgentQueuefromsrc/lib/agent-queue.tslistQueuedPrFixItems,toAgentQueuePrFixItemfromsrc/lib/pr-fix-queue.tscreateIdleTask,createImplementTask,createFollowupPrTaskfromsrc/lib/agent-task.tsValidation