Public: anyone can read this; the crew can change it.
A forge's size is written to the ledger and parent.json and listed by GET /api/v1/forges and MCP spawn #4379
scout/forge:scout/3094-a-forge-s-size-is-written-to-the-ledger → main 5 commits fork
7 files
changed +52
−3
README.md +5
| @@ -162,6 +162,11 @@ curl -sX POST localhost:3000/api/v1/forges -H "authorization: Bearer $TOKEN" \ | ||
| 162 | 162 | bun src/main.ts --first-admin --child kid |
| 163 | 163 | |
| 164 | 164 | |
| 165 | A child has a `size` (`small` by default, `medium` or `large`; send `"size"` to | |
| 166 | choose). It is written to the ledger and the child's `parent.json`, answered by | |
| 167 | the spawn, and listed by `GET /api/v1/forges` and the MCP `forges` tool; a | |
| 168 | ledger row with none reads as `small`. Nothing enforces its limits yet. | |
| 169 | ||
| 165 | 170 | The child keeps this forge's scheme and port at `<name>--<host>`, on its own |
| 166 | 171 | data under `$FORGE_DATA/children/`. Browsers send `*.localhost` to this |
| 167 | 172 | machine; curl needs `--resolve kid--localhost:3000:127.0.0.1`. The rest is in |
src/actions/spawn.ts +5 −1
| @@ -53,6 +53,7 @@ import { loadKey, open } from "../kernel/secret.ts"; | ||
| 53 | 53 | import { loadOrCreateHostKey } from "../ssh/keys.ts"; |
| 54 | 54 | import { claimLink, hasAdmin, mintClaim, spawnedOrg } from "../spawn/claim.ts"; |
| 55 | 55 | import { childPublicUrl, parentOrigin, type Placement } from "../spawn/placement.ts"; |
| 56 | import { DEFAULT_SIZE, type Size } from "../spawn/sizes.ts"; | |
| 56 | 57 | import { planSpawn, type SpawnInput, type SpawnPlan, type SpawnSource } from "../spawn/plan.ts"; |
| 57 | 58 | import { DEFAULT_SIZE, isSize, type Size } from "../spawn/sizes.ts"; |
| 58 | 59 | import { chainForChild, DEFAULT_MODE, forgeLinkOf, type FollowMode, type Hop } from "../spawn/chain.ts"; |
| @@ -63,6 +64,8 @@ export interface Spawned { | ||
| 63 | 64 | readonly org: string; |
| 64 | 65 | readonly url: string; |
| 65 | 66 | readonly bodyId: string; |
| 67 | /** The size the child was made at. */ | |
| 68 | readonly size: Size; | |
| 66 | 69 | /** True when this call found the child already made and changed nothing. */ |
| 67 | 70 | readonly existed: boolean; |
| 68 | 71 | /** |
| @@ -233,7 +236,7 @@ async function make( | ||
| 233 | 236 | }); |
| 234 | 237 | } |
| 235 | 238 | if (ledger.done) |
| 236 | return ok({ name: plan.name, org: plan.org, url: liveChildUrl(app, plan.name), bodyId: ledger.bodyId, existed: true }); | |
| 239 | return ok({ name: plan.name, org: plan.org, url: liveChildUrl(app, plan.name), bodyId: ledger.bodyId, size: ledger.size ?? DEFAULT_SIZE, existed: true }); | |
| 237 | 240 | if (ledger.statusToken === undefined || ledger.beatKey === undefined) { |
| 238 | 241 | ledger.statusToken ??= secret(); |
| 239 | 242 | ledger.beatKey ??= secret(); |
| @@ -281,6 +284,7 @@ async function make( | ||
| 281 | 284 | org: plan.org, |
| 282 | 285 | url: liveChildUrl(app, plan.name), |
| 283 | 286 | bodyId: ledger.bodyId, |
| 287 | size: ledger.size ?? DEFAULT_SIZE, | |
| 284 | 288 | existed: false, |
| 285 | 289 | claimUrl, |
| 286 | 290 | }); |
src/http/mcp.ts +2 −2
| @@ -1983,7 +1983,7 @@ const TOOLS: readonly Tool[] = [ | ||
| 1983 | 1983 | return good({ |
| 1984 | 1984 | says: !made |
| 1985 | 1985 | ? "This is the plan; nothing was made. Send dryRun: false to spawn." |
| 1986 | : `Spawned ${String(door.body["name"])} at ${String(door.body["url"])}. ${reveal ? LINK_REVEALED : LINK_KEPT}`, | |
| 1986 | : `Spawned ${String(door.body["name"])} (${String(door.body["size"])}) at ${String(door.body["url"])}. ${reveal ? LINK_REVEALED : LINK_KEPT}`, | |
| 1987 | 1987 | ...rest, |
| 1988 | 1988 | ...(reveal ? { claimUrl } : {}), |
| 1989 | 1989 | }); |
| @@ -1992,7 +1992,7 @@ const TOOLS: readonly Tool[] = [ | ||
| 1992 | 1992 | { |
| 1993 | 1993 | name: "forges", |
| 1994 | 1994 | description: |
| 1995 | "The forges you sent from here (an admin's list): name, address, whom for, apps, claimed or not, last activity.", | |
| 1995 | "The forges you sent from here (an admin's list): name, address, whom for, apps, size, claimed or not, last activity.", | |
| 1996 | 1996 | inputSchema: { type: "object", properties: {} }, |
| 1997 | 1997 | writes: false, |
| 1998 | 1998 | async run(ctx, user) { |
src/spawn/clients.ts +3
| @@ -13,6 +13,7 @@ import type { App } from "../app.ts"; | ||
| 13 | 13 | import { listChildren, statusTokenOf, type Child } from "../actions/spawn.ts"; |
| 14 | 14 | import { timingSafeEqual } from "node:crypto"; |
| 15 | 15 | import { sha256 } from "../kernel/hash.ts"; |
| 16 | import type { Size } from "./sizes.ts"; | |
| 16 | 17 | import type { ChildAppsSummary } from "./child-apps.ts"; |
| 17 | 18 | import { hostedChildApps } from "./host.ts"; |
| 18 | 19 | import { hasAdmin, spawnedOrg } from "./claim.ts"; |
| @@ -29,6 +30,7 @@ export interface ClientRow { | ||
| 29 | 30 | readonly url: string; |
| 30 | 31 | readonly for: string | null; |
| 31 | 32 | readonly apps: readonly string[]; |
| 33 | readonly size: Size; | |
| 32 | 34 | readonly sentAt: string | null; |
| 33 | 35 | /** False while the child is still being made (a resumed spawn finishes it). */ |
| 34 | 36 | readonly made: boolean; |
| @@ -159,6 +161,7 @@ export async function clientRows(app: App): Promise<ClientRow[]> { | ||
| 159 | 161 | url: c.url, |
| 160 | 162 | for: c.for, |
| 161 | 163 | apps: c.apps, |
| 164 | size: c.size, | |
| 162 | 165 | sentAt: c.createdAt, |
| 163 | 166 | made: c.done, |
| 164 | 167 | claimed: states[i]?.claimed ?? null, |
test/client-list.test.ts +3
| @@ -126,6 +126,7 @@ describe("the client list", () => { | ||
| 126 | 126 | const lost = await spawn({ |
| 127 | 127 | name: "lou", |
| 128 | 128 | apps: ["alice/bakery"], |
| 129 | size: "large", | |
| 129 | 130 | dryRun: false, |
| 130 | 131 | }); |
| 131 | 132 | if (lost.status !== 201) throw new Error(`spawn lou ${lost.status}`); |
| @@ -262,6 +263,8 @@ describe("the client list", () => { | ||
| 262 | 263 | expect(rows.maria.claimed).toBe(true); |
| 263 | 264 | expect(typeof rows.maria.lastActivityAt).toBe("string"); |
| 264 | 265 | expect(rows.lou.claimed).toBeNull(); |
| 266 | expect(rows.lou.size).toBe("large"); | |
| 267 | expect(rows.maria.size).toBe("small"); | |
| 265 | 268 | }); |
| 266 | 269 | }); |
| 267 | 270 |
test/spawn-api.test.ts +33
| @@ -214,6 +214,39 @@ describe("spawn over the API and MCP", () => { | ||
| 214 | 214 | expect(body.says).toContain("live secret"); |
| 215 | 215 | }); |
| 216 | 216 | |
| 217 | test("a made child's size is in the spawn answer, the ledger list and parent.json", async () => { | |
| 218 | const res = await post(rw, { name: "big-kid", apps: ["alice/notes"], size: "large", dryRun: false }); | |
| 219 | expect(res.status).toBe(201); | |
| 220 | expect(((await res.json()) as any).size).toBe("large"); | |
| 221 | const items = ((await (await get(ro)).json()) as any).items as any[]; | |
| 222 | const row = items.find((i) => i.name === "big-kid"); | |
| 223 | expect(row.size).toBe("large"); | |
| 224 | const doc = await Bun.file(join(dataDir, "children", row.bodyId, "parent.json")).json(); | |
| 225 | expect(doc.size).toBe("large"); | |
| 226 | const kid = items.find((i) => i.name === "kid"); | |
| 227 | const kidDoc = await Bun.file(join(dataDir, "children", kid.bodyId, "parent.json")).json(); | |
| 228 | expect(kidDoc.size).toBe("small"); | |
| 229 | }); | |
| 230 | ||
| 231 | test("MCP spawn names the size it made", async () => { | |
| 232 | const made = await mcp(rw, { name: "mcp-kid", apps: ["alice/notes"], size: "medium", dryRun: false }); | |
| 233 | expect(made.error).toBe(false); | |
| 234 | const body = JSON.parse(made.text); | |
| 235 | expect(body.size).toBe("medium"); | |
| 236 | expect(body.says).toContain("(medium)"); | |
| 237 | }); | |
| 238 | ||
| 239 | test("a ledger row with no size reads as small", async () => { | |
| 240 | expect((await post(rw, { name: "old-kid", apps: ["alice/notes"], dryRun: false })).status).toBe(201); | |
| 241 | const path = join(dataDir, "spawn", "old-kid.json"); | |
| 242 | const ledger = await Bun.file(path).json(); | |
| 243 | expect(ledger.size).toBe("small"); | |
| 244 | delete ledger.size; | |
| 245 | await Bun.write(path, JSON.stringify(ledger)); | |
| 246 | const items = ((await (await get(ro)).json()) as any).items as any[]; | |
| 247 | expect(items.find((i) => i.name === "old-kid").size).toBe("small"); | |
| 248 | }); | |
| 249 | ||
| 217 | 250 | const ledgerOf = async (name: string) => JSON.parse(await Bun.file(join(dataDir, "spawn", `${name}.json`)).text()); |
| 218 | 251 | const parentOf = async (bodyId: string) => JSON.parse(await Bun.file(join(dataDir, "children", bodyId, "parent.json")).text()); |
| 219 | 252 | const sizeListed = async (name: string) => ((await (await get(ro)).json()) as any).items.find((i: any) => i.name === name)?.size; |
test/tui-forges.test.ts +1
| @@ -20,6 +20,7 @@ const row = (over: Partial<ClientRow>): ClientRow => ({ | ||
| 20 | 20 | url: "https://maria.test", |
| 21 | 21 | for: "Maria's bakery", |
| 22 | 22 | apps: [], |
| 23 | size: "small", | |
| 23 | 24 | sentAt: null, |
| 24 | 25 | made: true, |
| 25 | 26 | claimed: false, |
new above
offline