diff --git a/cli/src/__tests__/run.test.ts b/cli/src/__tests__/run.test.ts index 97aac540e34..bbb57b8a167 100644 --- a/cli/src/__tests__/run.test.ts +++ b/cli/src/__tests__/run.test.ts @@ -217,4 +217,97 @@ describe("run inspection commands", () => { expect(fetchMock.mock.calls[3]?.[0]).toBe("http://localhost:3100/api/issues/PC-1/live-runs"); expect(fetchMock.mock.calls[4]?.[0]).toBe("http://localhost:3100/api/issues/PC-1/active-run"); }); + it("warns on stderr when run list returns a full server-capped page", async () => { + const page = Array.from({ length: 200 }, (_, i) => ({ + id: `33333333-3333-4333-8333-${String(i).padStart(12, "0")}`, + companyId: COMPANY_ID, + agentId: AGENT_ID, + status: "succeeded", + invocationSource: "on_demand", + })); + const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify(page), { status: 200 })); + vi.stubGlobal("fetch", fetchMock); + vi.spyOn(console, "log").mockImplementation(() => {}); + const errSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + await createProgram().parseAsync([ + "run", "list", + "--api-base", "http://localhost:3100", + "--api-key", "board-token", + "--company-id", COMPANY_ID, + ], { from: "user" }); + + // No limit is sent by default, and the full 200-row page is flagged. + expect(fetchMock.mock.calls[0]?.[0]).toBe( + `http://localhost:3100/api/companies/${COMPANY_ID}/heartbeat-runs`, + ); + expect(errSpy).toHaveBeenCalledTimes(1); + expect(errSpy.mock.calls[0]?.[0]).toContain("newest 200 runs"); + expect(errSpy.mock.calls[0]?.[0]).toContain("1000 max"); + }); + + it("warns when a negative limit makes the server return a single run", async () => { + const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify([ + { id: RUN_ID, companyId: COMPANY_ID, agentId: AGENT_ID, status: "succeeded", invocationSource: "on_demand" }, + ]), { status: 200 })); + vi.stubGlobal("fetch", fetchMock); + vi.spyOn(console, "log").mockImplementation(() => {}); + const errSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + await createProgram().parseAsync([ + "run", "list", + "--api-base", "http://localhost:3100", + "--api-key", "board-token", + "--company-id", COMPANY_ID, + "--limit=-1", + ], { from: "user" }); + + expect(errSpy).toHaveBeenCalledTimes(1); + expect(errSpy.mock.calls[0]?.[0]).toContain("newest 1 runs"); + }); + + it("does not advise raising --limit when the page already hit the server ceiling", async () => { + const page = Array.from({ length: 1000 }, (_, i) => ({ + id: `33333333-3333-4333-8333-${String(i).padStart(12, "0")}`, + companyId: COMPANY_ID, + agentId: AGENT_ID, + status: "succeeded", + invocationSource: "on_demand", + })); + const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify(page), { status: 200 })); + vi.stubGlobal("fetch", fetchMock); + vi.spyOn(console, "log").mockImplementation(() => {}); + const errSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + await createProgram().parseAsync([ + "run", "list", + "--api-base", "http://localhost:3100", + "--api-key", "board-token", + "--company-id", COMPANY_ID, + "--limit", "5000", + ], { from: "user" }); + + expect(errSpy).toHaveBeenCalledTimes(1); + expect(errSpy.mock.calls[0]?.[0]).toContain("newest 1000 runs"); + expect(errSpy.mock.calls[0]?.[0]).not.toContain("Raise --limit"); + }); + + it("stays quiet when run list returns a partial page", async () => { + const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify([ + { id: RUN_ID, companyId: COMPANY_ID, agentId: AGENT_ID, status: "running", invocationSource: "on_demand" }, + ]), { status: 200 })); + vi.stubGlobal("fetch", fetchMock); + vi.spyOn(console, "log").mockImplementation(() => {}); + const errSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + + await createProgram().parseAsync([ + "run", "list", + "--api-base", "http://localhost:3100", + "--api-key", "board-token", + "--company-id", COMPANY_ID, + "--limit", "25", + ], { from: "user" }); + + expect(errSpy).not.toHaveBeenCalled(); + }); }); diff --git a/cli/src/commands/client/run.ts b/cli/src/commands/client/run.ts index 1f0776b9731..f1b88600525 100644 --- a/cli/src/commands/client/run.ts +++ b/cli/src/commands/client/run.ts @@ -1,4 +1,5 @@ import { Command } from "commander"; +import { HEARTBEAT_RUN_LIST_DEFAULT_LIMIT, HEARTBEAT_RUN_LIST_MAX_LIMIT } from "@paperclipai/shared"; import type { HeartbeatRun, HeartbeatRunEvent, Issue, WorkspaceOperation } from "@paperclipai/shared"; import { addCommonClientOptions, @@ -50,7 +51,10 @@ export function registerRunCommands(command: Command): void { .description("List heartbeat runs for a company") .option("-C, --company-id ", "Company ID") .option("--agent-id ", "Filter by agent ID") - .option("--limit ", "Maximum runs to return") + .option( + "--limit ", + `Maximum runs to return (server default ${HEARTBEAT_RUN_LIST_DEFAULT_LIMIT}, max ${HEARTBEAT_RUN_LIST_MAX_LIMIT})`, + ) .action(async (opts: RunListOptions) => { try { const ctx = resolveCommandContext(opts, { requireCompany: true }); @@ -62,6 +66,32 @@ export function registerRunCommands(command: Command): void { `${apiPath`/api/companies/${ctx.companyId}/heartbeat-runs`}${query ? `?${query}` : ""}`, )) ?? []; printRuns(rows, ctx.json); + // The route returns one bounded page (200 by default, 1,000 at + // most) with no offset and no truncation signal, so a full page + // is the only sign that more runs may exist. Say so on stderr + // instead of silently stopping; stdout stays clean for --json. + // Real paging needs the offset/headers work in #14958. + const requestedLimit = opts.limit ? Number.parseInt(opts.limit, 10) : Number.NaN; + // Mirror the route's own clamp exactly: an unparsable or zero + // limit falls back to the default, and any other value is + // clamped into [1, MAX] (so a negative limit means one run). + const effectiveLimit = Math.max( + 1, + Math.min( + requestedLimit || HEARTBEAT_RUN_LIST_DEFAULT_LIMIT, + HEARTBEAT_RUN_LIST_MAX_LIMIT, + ), + ); + if (rows.length >= effectiveLimit) { + console.error( + effectiveLimit >= HEARTBEAT_RUN_LIST_MAX_LIMIT + ? `Showing the newest ${rows.length} runs. The server caps this list at ${HEARTBEAT_RUN_LIST_MAX_LIMIT} runs per request, ` + + "so more runs may exist, but this command cannot page past the cap yet." + : `Showing the newest ${rows.length} runs. The server caps this list at ${effectiveLimit} runs per request ` + + `(${HEARTBEAT_RUN_LIST_DEFAULT_LIMIT} by default, ${HEARTBEAT_RUN_LIST_MAX_LIMIT} max), so more runs may exist. ` + + "Raise --limit to see more.", + ); + } } catch (err) { handleCommandError(err); }