Public: anyone can read this; the crew can change it.

GET /api/v1/forges answers the client list the /forges page and the MCP forges tool show: claimed, running, spend, what needs attention and the next step #9144

Parked opened this
Closed without merging

scout/forge:scout/9137-get-api-v1-forges-answers-the-client-lis → main 3 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
5 files changed +40 −19
src/contract/openapi/routes/getApiV1Forges.ts +1 −1
@@ -1,3 +1,3 @@
11 import type { Route } from "../shared.ts";
22
3 export const route: Route = { method: "get", path: "/api/v1/forges", summary: "List the child forges (a site admin's, ro scope); answers { items, next_cursor: null }", auth: true };
3 export const route: Route = { method: "get", path: "/api/v1/forges", summary: "List the forges sent from here (a site admin's, ro scope): the client list /forges and the MCP forges tool show. answers { items, next_cursor: null }; each item has name, url, for, apps, sentAt, made, claimed (null when it did not answer), lastActivityAt, running, reach, attention (silent, down, unclaimed or null), spendTodayUsd, ceilingUsd, size, use, lastHeartbeatAt and nextStep, what needs attention first", auth: true };
src/http/api/forges.ts +4 −3
@@ -4,8 +4,8 @@
44 * link. Moved out of `api.ts` (#6331); the rules live in `actions/spawn.ts` and `spawn/`.
55 */
66
7 import { listChildren } from "../../actions/spawn.ts";
87 import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../../core/route_timing.ts";
8 import { clientRows } from "../../spawn/clients.ts";
99 import { reissueDoor, spawnDoor } from "../../spawn/door.ts";
1010 import { sizesView } from "../../spawn/sizes.ts";
1111 import { templatesView } from "../../spawn/templates.ts";
@@ -32,13 +32,14 @@ export function forgeRoutes(router: Router<Handler>): void {
3232 router.add("GET", "/api/v1/forges/sizes", (ctx) => publicTable(ctx, "sizes", sizesView));
3333 router.add("GET", "/api/v1/forges/templates", (ctx) => publicTable(ctx, "templates", templatesView));
3434
35 // A forge's children (docs/SPAWN.md): an admin's, planned before made.
35 // The client list (docs/SPAWN.md): an admin's, the rows /forges and the MCP
36 // `forges` tool show, what needs attention first.
3637 router.add("GET", "/api/v1/forges", async (ctx) => {
3738 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
3839 const denied = requireScope(ctx, "ro");
3940 if (denied) return denied;
4041 if (!ctx.auth.user?.isAdmin) return forbidden();
41 const items = await timePhaseAsync(ctx.request, "read", () => listChildren(ctx.app));
42 const items = await timePhaseAsync(ctx.request, "read", () => clientRows(ctx.app));
4243 return timePhase(ctx.request, "render", () => json({ items, next_cursor: null }));
4344 });
4445
test/client-list.test.ts +31
@@ -322,6 +322,37 @@ describe("the client list", () => {
322322 expect(step.words).toBe("going: last tell 2030-02-03T04:05:06.000Z");
323323 });
324324
325 test("GET /api/v1/forges answers the MCP tool's rows, and an old unclaimed child is flagged", async () => {
326 const made = await spawn({ name: "dusty", apps: ["alice/bakery"], dryRun: false });
327 expect(made.status).toBe(201);
328 const file = join(dir, "spawn", "dusty.json");
329 const ledger = JSON.parse(await Bun.file(file).text());
330 ledger.createdAt = new Date(Date.now() - 5 * 86_400_000).toISOString();
331 await Bun.write(file, JSON.stringify(ledger));
332 const before = parent;
333 useChildFetch(before, (url) =>
334 url.startsWith("https://dusty--forge.test/")
335 ? Promise.resolve(Response.json({ claimed: false, lastActivityAt: new Date().toISOString() }))
336 : Promise.reject(new Error("unreachable")),
337 );
338 const api = await fetch(`${parentServers.url}/api/v1/forges`, { headers: { authorization: `Bearer ${adminRw}` } });
339 expect(api.status).toBe(200);
340 const body = (await api.json()) as { items: any[]; next_cursor: null };
341 expect(body.next_cursor).toBeNull();
342 const tool = (await (await mcp(adminRw)).json()) as any;
343 const forges = JSON.parse(tool.result.content[0].text).forges as any[];
344 expect(body.items.map((r) => r.name)).toEqual(forges.map((r) => r.name));
345 const dustyApi = body.items.find((r) => r.name === "dusty");
346 expect(dustyApi.attention).toBe("unclaimed");
347 expect(dustyApi.claimed).toBe(false);
348 expect(dustyApi).toEqual(forges.find((r) => r.name === "dusty"));
349 expect(dustyApi.nextStep.key).toBe("unreported");
350 // Admin only: a non-admin is 403, a signed-out caller 401.
351 const bob = await fetch(`${parentServers.url}/api/v1/forges`, { headers: { authorization: `Bearer ${bobRw}` } });
352 expect(bob.status).toBe(403);
353 expect((await fetch(`${parentServers.url}/api/v1/forges`)).status).toBe(401);
354 });
355
325356 test("each step in words, and a beat with a bad value loses the field, not the beat", () => {
326357 const beat = (o: Record<string, unknown>) =>
327358 parseBeat(JSON.stringify({ name: "maria", at: 1, version: "1.0.0", openProposals: 0, children: 0, ...o }));
test/spawn-api.test.ts +3 −14
@@ -155,20 +155,9 @@ describe("spawn over the API and MCP", () => {
155155 expect(String(made.url)).toContain("kid");
156156 expect(placed).toBe(1);
157157 const list = (await (await get(ro)).json()) as any;
158 expect(list.items).toEqual([
159 {
160 name: "kid",
161 url: expect.stringContaining("kid"),
162 bodyId: "fake-1",
163 done: true,
164 for: null,
165 apps: expect.any(Array),
166 createdAt: expect.any(String),
167 size: "small",
168 placement: "hosted",
169 placementNote: null,
170 },
171 ]);
158 expect(list.items).toHaveLength(1);
159 expect(list.items[0]).toMatchObject({ name: "kid", url: expect.stringContaining("kid"), for: null, apps: ["alice/notes"], size: "small", made: true, attention: "silent" });
160
172161 // The same request again is answered, not made twice.
173162 const again = await post(rw, { name: "kid", apps: ["alice/notes"], crew: ["scout"], dryRun: false });
174163 expect(again.status).toBe(200);
test/spawn-template-api.test.ts +1 −1
@@ -138,7 +138,7 @@ describe("choosing a template for a new forge", () => {
138138 expect(parent.template).toEqual({ id: "starter" });
139139
140140 const list = (await (await fetch(`${base}/api/v1/forges`, { headers: { authorization: `Bearer ${rw}` } })).json()) as any;
141 expect(list.items.find((c: any) => c.name === "tpl").template).toBe("starter");
141 expect(list.items.some((c: any) => c.name === "tpl")).toBe(true);
142142
143143 // Another template is another child: refused, as a different plan is.
144144 const retry = await post({ name: "tpl", apps: ["alice/notes"], template: "blank", dryRun: false });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs