diff --git a/packages/cli/src/commands/agent/wait.test.ts b/packages/cli/src/commands/agent/wait.test.ts new file mode 100644 index 000000000..4cb0b7b7e --- /dev/null +++ b/packages/cli/src/commands/agent/wait.test.ts @@ -0,0 +1,49 @@ +import { describe, expect, it } from "vitest"; +import type { AgentSnapshotPayload } from "@getpaseo/protocol/messages"; +import { buildWaitResult } from "./wait.js"; + +const agentId = "11111111-1111-4111-8111-111111111111"; + +describe("buildWaitResult", () => { + it("reports an identifier the daemon could not resolve as a missing agent", () => { + expect(() => + buildWaitResult({ + state: { + status: "error", + final: null, + error: "Agent not found: does-not-exist", + lastMessage: null, + }, + resolvedAgentId: "does-not-exist", + recentActivity: null, + timeoutLabel: null, + }), + ).toThrow( + expect.objectContaining({ + code: "AGENT_NOT_FOUND", + message: "Agent not found: does-not-exist", + details: "Use `paseo ls` to list available agents", + }), + ); + }); + + it("still reports an agent that finished in error as a result", () => { + const result = buildWaitResult({ + state: { + status: "error", + final: { id: agentId, lastError: "The provider returned 500" } as AgentSnapshotPayload, + error: "The provider returned 500", + lastMessage: null, + }, + resolvedAgentId: agentId, + recentActivity: null, + timeoutLabel: null, + }); + + expect(result).toEqual({ + agentId, + status: "error", + message: "The provider returned 500", + }); + }); +}); diff --git a/packages/cli/src/commands/agent/wait.ts b/packages/cli/src/commands/agent/wait.ts index 3e5e5b533..1fc91f677 100644 --- a/packages/cli/src/commands/agent/wait.ts +++ b/packages/cli/src/commands/agent/wait.ts @@ -86,7 +86,12 @@ type WaitFinishState = Awaited< ReturnType>["waitForFinish"]> >; -function buildWaitResult(args: { +/** + * Turn the daemon's wait reply into the command result. Throws AGENT_NOT_FOUND when the + * daemon resolved no agent: it answers an unknown identifier with an error and no agent + * payload, while an agent that finished in error always comes with its payload. + */ +export function buildWaitResult(args: { state: WaitFinishState; resolvedAgentId: string; recentActivity: string | null; @@ -117,6 +122,13 @@ function buildWaitResult(args: { } if (state.status === "error") { + if (state.final === null) { + throw { + code: "AGENT_NOT_FOUND", + message: state.error ?? `Agent not found: ${resolvedAgentId}`, + details: "Use `paseo ls` to list available agents", + } satisfies CommandError; + } return { agentId: resolvedAgentId, status: "error", @@ -155,6 +167,7 @@ export async function runWaitCommand( const client = await connectToDaemon({ target: options.daemonTarget }); + let reply: Omit[0], "timeoutLabel">; try { try { const state = await client.waitForFinish(agentIdArg, timeoutMs); @@ -166,11 +179,7 @@ export async function runWaitCommand( await client.close(); - return { - type: "single", - data: buildWaitResult({ state, resolvedAgentId, recentActivity, timeoutLabel }), - schema: agentWaitSchema, - }; + reply = { state, resolvedAgentId, recentActivity }; } catch (waitErr) { await client.close().catch(() => {}); @@ -198,4 +207,10 @@ export async function runWaitCommand( }; throw error; } + + return { + type: "single", + data: buildWaitResult({ ...reply, timeoutLabel }), + schema: agentWaitSchema, + }; } diff --git a/packages/cli/tests/11-agent-wait.test.ts b/packages/cli/tests/11-agent-wait.test.ts index 198fa0891..5b179b1c3 100644 --- a/packages/cli/tests/11-agent-wait.test.ts +++ b/packages/cli/tests/11-agent-wait.test.ts @@ -20,6 +20,7 @@ import assert from "node:assert"; import { getAvailablePort } from "./helpers/network.ts"; +import { runPaseoCli, startTestDaemon } from "./helpers/test-daemon.ts"; import { $ } from "zx"; import { mkdtemp, rm } from "fs/promises"; import { tmpdir } from "os"; @@ -177,6 +178,28 @@ try { ); console.log("timeout option documents no default limit\n"); } + + // Test 13: wait reports AGENT_NOT_FOUND for an unknown ID + { + console.log("Test 13: wait reports AGENT_NOT_FOUND for an unknown ID"); + const daemon = await startTestDaemon(); + try { + const result = await runPaseoCli(daemon, [ + "wait", + "does-not-exist", + "--host", + `127.0.0.1:${daemon.port}`, + "--json", + ]); + assert.notStrictEqual(result.exitCode, 0, "wait should fail for an unknown ID"); + const { error } = JSON.parse(result.stderr); + assert.strictEqual(error.code, "AGENT_NOT_FOUND", result.stderr); + assert.match(error.details, /paseo ls/); + } finally { + await daemon.stop(); + } + console.log("✓ wait reports AGENT_NOT_FOUND for an unknown ID\n"); + } } finally { // Clean up temp directory await rm(paseoHome, { recursive: true, force: true });