From f96128794928b68f568bb831d7ae2ba2f3ccaa51 Mon Sep 17 00:00:00 2001 From: appleguy Date: Tue, 24 Nov 2026 18:32:48 -0800 Subject: [PATCH] feat(cli): add agent clear-error (#19382) ## Thinking Path - The server already exposes a permission-checked agent clear-error endpoint. - The CLI previously required operators to call that endpoint manually. - A thin CLI wrapper provides the same action without duplicating lifecycle policy. ## Linked Issues or Issue Description **Problem or motivation** The CLI exposes pause and resume but not the existing `POST /api/agents/:id/clear-error` action. **Proposed solution** Expose `paperclipai agent clear-error ` with standard authentication and output. **Alternatives considered** Direct REST calls work but bypass normal CLI ergonomics; generic agent PATCH is not equivalent. **Roadmap alignment** This exposes an existing server capability rather than adding a new lifecycle policy. ## What Changed - Add `agent clear-error` to the CLI and usage documentation. - Test the API path, JSON output, forbidden (403) and invalid-state (409) replies without retries or extra actions. ## Verification - CLI lifecycle tests **10/10 passed** and typecheck passed. - GitHub CI: **55 passed, 3 skipped, 0 failed** on `3b8fd71739b8`; Greptile **5/5**. ## Risks - The endpoint remains subject to server authorization and agent-state checks. Clearing an error does not cure its underlying cause. ## Model Used PR history records Anthropic Claude Sonnet 5.5 and OpenAI GPT-6.1 Sol with tool-assisted review. ## Checklist - [x] Searched similar open and closed PRs; no duplicate was found. - [x] Focused tests, changed behavior and risks are documented. - [x] All required CI checks pass on the current head. - [x] Greptile review is 5/5 with no open actionable findings. - [ ] Upstream maintainer approval (not yet requested as a merge). --------- Co-authored-by: Claude Sonnet 5.5 --- cli/src/__tests__/agent-lifecycle.test.ts | 29 +++++++++++++++++++++++ cli/src/commands/client/agent.ts | 16 +++++++++++++ doc/CLI.md | 5 ++++ 3 files changed, 50 insertions(+) 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