From a1efa09481bfcca1690596f1885803ab7b97a650 Mon Sep 17 00:00:00 2001 From: Michael Zhou Date: Thu, 3 Dec 2026 11:19:01 +0800 Subject: [PATCH] Stop the agents run route from reporting a run that never happened POST /api/agents/[id]/run answered 202 with status 'queued' and stamped the agent's lastRun, but nothing reads Agent rows: no job was queued and no worker ran the agent. The route now answers 501 and says the run did not happen, and no longer touches lastRun. The Run button is disabled by default and the Agents page says why, so the UI stops offering a run that cannot happen. Co-Authored-By: Claude Code --- apps/web/src/__tests__/agents-api.test.ts | 31 +++++++---- .../src/__tests__/agents-components.test.tsx | 54 ++++++++++++------- apps/web/src/app/agents/page.tsx | 7 ++- apps/web/src/app/api/agents/[id]/run/route.ts | 41 ++++++-------- .../src/components/agents/agent-actions.tsx | 46 ++++++++++++---- .../web/src/components/agents/agent-table.tsx | 20 +++++-- 6 files changed, 130 insertions(+), 69 deletions(-) diff --git a/apps/web/src/__tests__/agents-api.test.ts b/apps/web/src/__tests__/agents-api.test.ts index bb24ee10..7988beef 100644 --- a/apps/web/src/__tests__/agents-api.test.ts +++ b/apps/web/src/__tests__/agents-api.test.ts @@ -124,7 +124,9 @@ describe('GET /api/agents', () => { expect.objectContaining({ where: expect.objectContaining({ OR: expect.arrayContaining([ - expect.objectContaining({ name: expect.objectContaining({ contains: 'classifier' }) }), + expect.objectContaining({ + name: expect.objectContaining({ contains: 'classifier' }), + }), ]), }), }), @@ -207,7 +209,9 @@ describe('DELETE /api/agents/[id]', () => { mockAgentFindUnique.mockResolvedValue(MOCK_AGENT); mockAgentDelete.mockResolvedValue(MOCK_AGENT); - const req = new NextRequest('http://localhost:3000/api/agents/agent-1', { method: 'DELETE' }); + const req = new NextRequest('http://localhost:3000/api/agents/agent-1', { + method: 'DELETE', + }); const res = await DELETE(req as never, { params: Promise.resolve({ id: 'agent-1' }) }); const body = await res.json(); @@ -231,17 +235,18 @@ describe('POST /api/agents/[id]/run', () => { mockGetServerSession.mockResolvedValue(userSession('tm-1', 'ADMIN')); }); - it('triggers a run and updates lastRun', async () => { + it('reports that runs are not available instead of a queued run', async () => { mockAgentFindUnique.mockResolvedValue(MOCK_AGENT); - mockAgentUpdate.mockResolvedValue({ ...MOCK_AGENT, lastRun: new Date() }); const req = makeJsonRequest('http://localhost:3000/api/agents/agent-1/run', {}); const res = await RUN(req as never, { params: Promise.resolve({ id: 'agent-1' }) }); const body = await res.json(); - expect(res.status).toBe(202); - expect(body.status).toBe('queued'); - expect(mockAgentUpdate).toHaveBeenCalledTimes(1); + expect(res.status).toBe(501); + expect(body.error).toContain('not available yet'); + expect(body.status).toBeUndefined(); + // Nothing ran, so nothing may claim it did. + expect(mockAgentUpdate).not.toHaveBeenCalled(); }); it('returns 404 for non-existent agent', async () => { @@ -308,7 +313,11 @@ describe('Agent form validation', () => { it('fails when webhook URL is not valid', () => { const errors = validateAgentForm({ ...validData, - config: { triggerType: 'manual', actionType: 'custom_webhook', webhookUrl: 'not-a-url' }, + config: { + triggerType: 'manual', + actionType: 'custom_webhook', + webhookUrl: 'not-a-url', + }, }); expect(errors.webhookUrl).toBe('Must be a valid URL'); }); @@ -328,7 +337,11 @@ describe('Agent form validation', () => { it('passes for valid interval trigger', () => { const errors = validateAgentForm({ ...validData, - config: { triggerType: 'interval', actionType: 'classify_tickets', intervalMinutes: 15 }, + config: { + triggerType: 'interval', + actionType: 'classify_tickets', + intervalMinutes: 15, + }, }); expect(Object.keys(errors)).toHaveLength(0); }); diff --git a/apps/web/src/__tests__/agents-components.test.tsx b/apps/web/src/__tests__/agents-components.test.tsx index 189361a5..02536369 100644 --- a/apps/web/src/__tests__/agents-components.test.tsx +++ b/apps/web/src/__tests__/agents-components.test.tsx @@ -48,12 +48,7 @@ describe('AgentStatusBadge', () => { describe('AgentActions', () => { it('renders run, edit, and delete buttons', () => { render( - , + , ); expect(screen.getByTestId('agent-run-btn')).toBeTruthy(); expect(screen.getByTestId('agent-edit-btn')).toBeTruthy(); @@ -68,21 +63,33 @@ describe('AgentActions', () => { onRun={onRun} onEdit={vi.fn()} onDelete={vi.fn()} + runsAvailable />, ); - fireEvent.click(screen.getByTestId('agent-run-btn')); + const runButton = screen.getByTestId('agent-run-btn'); + expect(runButton).not.toBeDisabled(); + fireEvent.click(runButton); expect(onRun).toHaveBeenCalledWith('agent-1'); }); + it('disables the run button when runs are unavailable', () => { + const onRun = vi.fn(); + render( + , + ); + const runButton = screen.getByTestId('agent-run-btn'); + expect(runButton).toBeDisabled(); + // A disabled button shows no tooltip of its own, so the wrapper carries + // the reason. + expect(screen.getByTitle('Manual runs are not available yet')).toBeTruthy(); + fireEvent.click(runButton); + expect(onRun).not.toHaveBeenCalled(); + }); + it('calls onEdit with agent ID when edit button clicked', () => { const onEdit = vi.fn(); render( - , + , ); fireEvent.click(screen.getByTestId('agent-edit-btn')); expect(onEdit).toHaveBeenCalledWith('agent-1'); @@ -91,12 +98,7 @@ describe('AgentActions', () => { it('calls onDelete with agent ID when delete button clicked', () => { const onDelete = vi.fn(); render( - , + , ); fireEvent.click(screen.getByTestId('agent-delete-btn')); expect(onDelete).toHaveBeenCalledWith('agent-1'); @@ -145,6 +147,20 @@ describe('AgentTable', () => { render(); expect(screen.getByText('Never')).toBeTruthy(); }); + + it('disables the run button unless runs are available', () => { + render(); + for (const button of screen.getAllByTestId('agent-run-btn')) { + expect(button).toBeDisabled(); + } + }); + + it('enables the run button when runs are available', () => { + render(); + for (const button of screen.getAllByTestId('agent-run-btn')) { + expect(button).not.toBeDisabled(); + } + }); }); describe('AgentForm', () => { diff --git a/apps/web/src/app/agents/page.tsx b/apps/web/src/app/agents/page.tsx index 075a44e3..a0673925 100644 --- a/apps/web/src/app/agents/page.tsx +++ b/apps/web/src/app/agents/page.tsx @@ -85,7 +85,7 @@ export default function AgentsPage() {
@@ -121,6 +121,10 @@ export default function AgentsPage() { {/* Table */}
+ {/* Manual runs have no execution framework behind them yet: the + API answers 501 instead of pretending a run was queued, so + the button is disabled rather than offering a run that + cannot happen. */} {loading ? (
@@ -132,6 +136,7 @@ export default function AgentsPage() { onRun={handleRun} onEdit={handleEdit} onDelete={handleDelete} + runsAvailable={false} /> )}
diff --git a/apps/web/src/app/api/agents/[id]/run/route.ts b/apps/web/src/app/api/agents/[id]/run/route.ts index fc544dee..9b2cfbc5 100644 --- a/apps/web/src/app/api/agents/[id]/run/route.ts +++ b/apps/web/src/app/api/agents/[id]/run/route.ts @@ -5,14 +5,14 @@ import { requireAdmin } from '@/lib/require-admin'; /** * POST /api/agents/[id]/run * - * Manually trigger an agent run. Validates the agent exists and updates - * its lastRun timestamp. In a full implementation, this would also - * enqueue a job via the job queue for the configured action type. + * Manually trigger an agent run. + * + * No execution framework is wired up yet: nothing in the queue or the worker + * reads `Agent` rows, so a run cannot be queued. This route used to answer + * 202 with `status: 'queued'` and stamp `lastRun` — three signals that the + * agent ran, none of them true. It now says the run did not happen. */ -export async function POST( - _request: NextRequest, - { params }: { params: Promise<{ id: string }> }, -) { +export async function POST(_request: NextRequest, { params }: { params: Promise<{ id: string }> }) { const { error } = await requireAdmin(); if (error) return error; @@ -23,23 +23,12 @@ export async function POST( return NextResponse.json({ error: 'Agent not found' }, { status: 404 }); } - const config = agent.config as Record | null; - const actionType = config?.actionType ?? 'unknown'; - - // Update the agent's lastRun timestamp - await prisma.agent.update({ - where: { id }, - data: { lastRun: new Date() }, - }); - - const result = { - agentId: agent.id, - agentName: agent.name, - actionType, - status: 'queued', - triggeredAt: new Date().toISOString(), - message: `Agent "${agent.name}" run has been queued. Action: ${actionType}`, - }; - - return NextResponse.json(result, { status: 202 }); + return NextResponse.json( + { + error: 'Agent runs are not available yet: no execution framework is wired up.', + agentId: agent.id, + agentName: agent.name, + }, + { status: 501 }, + ); } diff --git a/apps/web/src/components/agents/agent-actions.tsx b/apps/web/src/components/agents/agent-actions.tsx index 4ed15784..1e67bf9f 100644 --- a/apps/web/src/components/agents/agent-actions.tsx +++ b/apps/web/src/components/agents/agent-actions.tsx @@ -7,19 +7,47 @@ interface AgentActionsProps { onRun: (id: string) => void; onEdit: (id: string) => void; onDelete: (id: string) => void; + /** + * Manual runs have no execution framework behind them yet, so the run + * button is disabled rather than offering a run that cannot happen. + * Defaults to unavailable: a run button that cannot run is the worse + * default. + */ + runsAvailable?: boolean; } -export function AgentActions({ agentId, onRun, onEdit, onDelete }: AgentActionsProps) { +export function AgentActions({ + agentId, + onRun, + onEdit, + onDelete, + runsAvailable = false, +}: AgentActionsProps) { + const runButton = ( + + ); + return (
- + {/* A disabled button emits no mouse events, so no browser shows a + tooltip for it. The wrapper carries the reason instead. */} + {runsAvailable ? ( + runButton + ) : ( + {runButton} + )}