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
scout/forge:scout/9137-get-api-v1-forges-answers-the-client-lis → main 3 commits fork
5 files
changed +40
−19
src/contract/openapi/routes/getApiV1Forges.ts +1 −1
| @@ -1,3 +1,3 @@ | ||
| 1 | 1 | import type { Route } from "../shared.ts"; |
| 2 | 2 | |
| 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 @@ | ||
| 4 | 4 | * link. Moved out of `api.ts` (#6331); the rules live in `actions/spawn.ts` and `spawn/`. |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | import { listChildren } from "../../actions/spawn.ts"; | |
| 8 | 7 | import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../../core/route_timing.ts"; |
| 8 | import { clientRows } from "../../spawn/clients.ts"; | |
| 9 | 9 | import { reissueDoor, spawnDoor } from "../../spawn/door.ts"; |
| 10 | 10 | import { sizesView } from "../../spawn/sizes.ts"; |
| 11 | 11 | import { templatesView } from "../../spawn/templates.ts"; |
| @@ -32,13 +32,14 @@ export function forgeRoutes(router: Router<Handler>): void { | ||
| 32 | 32 | router.add("GET", "/api/v1/forges/sizes", (ctx) => publicTable(ctx, "sizes", sizesView)); |
| 33 | 33 | router.add("GET", "/api/v1/forges/templates", (ctx) => publicTable(ctx, "templates", templatesView)); |
| 34 | 34 | |
| 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. | |
| 36 | 37 | router.add("GET", "/api/v1/forges", async (ctx) => { |
| 37 | 38 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
| 38 | 39 | const denied = requireScope(ctx, "ro"); |
| 39 | 40 | if (denied) return denied; |
| 40 | 41 | 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)); | |
| 42 | 43 | return timePhase(ctx.request, "render", () => json({ items, next_cursor: null })); |
| 43 | 44 | }); |
| 44 | 45 |
test/client-list.test.ts +31
| @@ -322,6 +322,37 @@ describe("the client list", () => { | ||
| 322 | 322 | expect(step.words).toBe("going: last tell 2030-02-03T04:05:06.000Z"); |
| 323 | 323 | }); |
| 324 | 324 | |
| 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 | ||
| 325 | 356 | test("each step in words, and a beat with a bad value loses the field, not the beat", () => { |
| 326 | 357 | const beat = (o: Record<string, unknown>) => |
| 327 | 358 | 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", () => { | ||
| 155 | 155 | expect(String(made.url)).toContain("kid"); |
| 156 | 156 | expect(placed).toBe(1); |
| 157 | 157 | 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 | ||
| 172 | 161 | // The same request again is answered, not made twice. |
| 173 | 162 | const again = await post(rw, { name: "kid", apps: ["alice/notes"], crew: ["scout"], dryRun: false }); |
| 174 | 163 | 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", () => { | ||
| 138 | 138 | expect(parent.template).toEqual({ id: "starter" }); |
| 139 | 139 | |
| 140 | 140 | 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); | |
| 142 | 142 | |
| 143 | 143 | // Another template is another child: refused, as a different plan is. |
| 144 | 144 | const retry = await post({ name: "tpl", apps: ["alice/notes"], template: "blank", dryRun: false }); |
new above
offline