diff --git a/cli/src/__tests__/agent-lifecycle.test.ts b/cli/src/__tests__/agent-lifecycle.test.ts index 204e84ec7cc..52876f8ef13 100644 --- a/cli/src/__tests__/agent-lifecycle.test.ts +++ b/cli/src/__tests__/agent-lifecycle.test.ts @@ -50,6 +50,7 @@ describe("agent lifecycle commands", () => { await run(["agent", "update", AGENT_ID, "--payload-json", JSON.stringify({ title: "Senior Builder" })]); await run(["agent", "pause", AGENT_ID]); await run(["agent", "resume", AGENT_ID]); + await run(["agent", "clear-error", AGENT_ID]); await run(["agent", "approve", AGENT_ID]); await run(["agent", "terminate", AGENT_ID]); await run(["agent", "heartbeat:invoke", AGENT_ID]); @@ -62,6 +63,7 @@ describe("agent lifecycle commands", () => { ["PATCH", `http://localhost:3100/api/agents/${AGENT_ID}`], ["POST", `http://localhost:3100/api/agents/${AGENT_ID}/pause`], ["POST", `http://localhost:3100/api/agents/${AGENT_ID}/resume`], + ["POST", `http://localhost:3100/api/agents/${AGENT_ID}/clear-error`], ["POST", `http://localhost:3100/api/agents/${AGENT_ID}/approve`], ["POST", `http://localhost:3100/api/agents/${AGENT_ID}/terminate`], ["POST", `http://localhost:3100/api/agents/${AGENT_ID}/heartbeat/invoke`], @@ -70,6 +72,33 @@ describe("agent lifecycle commands", () => { ]); }); + it("prints the server result when clearing an error with JSON output", async () => { + const cleared = { id: AGENT_ID, status: "idle", errorReason: null }; + const fetchMock = vi.fn().mockResolvedValue(jsonResponse(cleared)); + vi.stubGlobal("fetch", fetchMock); + + await run(["agent", "clear-error", AGENT_ID, "--json"]); + + expect(fetchMock).toHaveBeenCalledTimes(1); + expect(fetchMock.mock.calls[0]?.[1]).toMatchObject({ method: "POST", body: "{}" }); + expect(console.log).toHaveBeenCalledWith(JSON.stringify(cleared, null, 2)); + }); + + it.each([ + { status: 403, message: "Insufficient permission to clear this agent's error" }, + { status: 409, message: "Agent is not in error status" }, + ])("reports a $status clear-error refusal without attempting another lifecycle action", async ({ status, message }) => { + const fetchMock = vi.fn().mockResolvedValue(jsonResponse({ error: message }, { status })); + vi.stubGlobal("fetch", fetchMock); + vi.spyOn(console, "error").mockImplementation(() => {}); + vi.spyOn(process, "exit").mockImplementation(((code?: string | number | null) => { + throw new Error(`exit:${code ?? 0}`); + }) as typeof process.exit); + + await expect(run(["agent", "clear-error", AGENT_ID])).rejects.toThrow("exit:1"); + expect(fetchMock).toHaveBeenCalledTimes(1); + }); + it("sends automatic selection when omitted and supports explicit runner overrides", async () => { const fetchMock = vi.fn().mockImplementation(() => Promise.resolve(jsonResponse())); vi.stubGlobal("fetch", fetchMock); diff --git a/cli/src/commands/client/agent.ts b/cli/src/commands/client/agent.ts index 4d9fc75578e..0930251879f 100644 --- a/cli/src/commands/client/agent.ts +++ b/cli/src/commands/client/agent.ts @@ -405,6 +405,22 @@ export function registerAgentCommands(program: Command): void { }), ); + addCommonClientOptions( + agent + .command("clear-error") + .description("Clear an agent's error status") + .argument("", "Agent ID") + .action(async (agentId: string, opts: BaseClientOptions) => { + try { + const ctx = resolveCommandContext(opts); + const cleared = await ctx.api.post(apiPath`/api/agents/${agentId}/clear-error`, {}); + printOutput(cleared, { json: ctx.json }); + } catch (err) { + handleCommandError(err); + } + }), + ); + addCommonClientOptions( agent .command("delete") diff --git a/doc/CLI.md b/doc/CLI.md index 67375e2665a..7aa17b57b4a 100644 --- a/doc/CLI.md +++ b/doc/CLI.md @@ -577,6 +577,7 @@ npx paperclipai agent inbox-mine --user-id npx paperclipai agent wake [--company-id ] [--reason "..."] [--payload '{"issueId":"..."}'] npx paperclipai agent pause npx paperclipai agent resume +npx paperclipai agent clear-error npx paperclipai agent approve npx paperclipai agent terminate npx paperclipai agent heartbeat:invoke @@ -584,6 +585,10 @@ npx paperclipai agent claude-login npx paperclipai agent local-cli --company-id ``` +`agent clear-error` uses the server's existing clear-error action. The server +checks permissions and the agent state. Use it to clear an error after addressing +its cause; use `agent resume` to resume a paused agent. + Agent configuration and runtime endpoints: ```sh