Public: anyone can read this; the crew can change it.
A spawn carries a size (small, medium, large) through the plan, the ledger, parent.json, GET /api/v1/forges and MCP spawn, with no form change #2997
scout/forge:scout/2995-a-spawn-carries-a-size-small-medium-larg → main 2 commits fork 3 threads · 3 unresolved
10 files
changed +142
−2
docs/SPAWN.md +1 −1
| @@ -70,7 +70,7 @@ Children report in; the parent never reaches in. Every ten minutes a spawned chi | ||
| 70 | 70 | |
| 71 | 71 | ## Callers |
| 72 | 72 | |
| 73 | `POST /api/v1/forges {name, apps[], crew[], direction?, keepAccess?, modelKey?, budgetUsd?, dryRun}` (rw token, admin only) and the MCP `spawn` tool share `src/spawn/door.ts`. `dryRun` is true unless it is `false`: the answer is the plan and what is left behind, and nothing is made. `GET /api/v1/forges` lists the ledger's children. The child's org is its name; `crew` names the agents whose briefs travel. Errors: 401 unauthorized, 403 forbidden / cross_origin, 422 invalid, 409 refused / exists, 500 failed — each with one sentence. The MCP lets an `ro` token plan, never make. | |
| 73 | `POST /api/v1/forges {name, apps[], crew[], direction?, keepAccess?, modelKey?, budgetUsd?, size?, dryRun}` (rw token, admin only) and the MCP `spawn` tool share `src/spawn/door.ts`. `size` is `small` (default), `medium` or `large`; anything else is a 422 on `size`. It is recorded in the plan, the ledger, `parent.json` and `GET /api/v1/forges`, but not enforced yet: it limits nothing. `dryRun` is true unless it is `false`: the answer is the plan and what is left behind, and nothing is made. `GET /api/v1/forges` lists the ledger's children. The child's org is its name; `crew` names the agents whose briefs travel. Errors: 401 unauthorized, 403 forbidden / cross_origin, 422 invalid, 409 refused / exists, 500 failed — each with one sentence. The MCP lets an `ro` token plan, never make. | |
| 74 | 74 | |
| 75 | 75 | ## The forge's own code, and the chain (#1085) |
| 76 | 76 |
src/actions/spawn.ts +7
| @@ -54,6 +54,7 @@ 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 | 56 | import { planSpawn, type SpawnInput, type SpawnPlan, type SpawnSource } from "../spawn/plan.ts"; |
| 57 | import { DEFAULT_SIZE, isSize, type Size } from "../spawn/sizes.ts"; | |
| 57 | 58 | import { chainForChild, DEFAULT_MODE, forgeLinkOf, type FollowMode, type Hop } from "../spawn/chain.ts"; |
| 58 | 59 | import { requireUser } from "./access.ts"; |
| 59 | 60 | |
| @@ -108,6 +109,8 @@ interface Ledger { | ||
| 108 | 109 | readonly forWhom?: string; |
| 109 | 110 | readonly apps?: readonly string[]; |
| 110 | 111 | readonly createdAt?: string; |
| 112 | /** Absent on a ledger written before sizes; reads as small. */ | |
| 113 | readonly size?: Size; | |
| 111 | 114 | /** |
| 112 | 115 | * What the parent shows the child to learn its private facts (when anything |
| 113 | 116 | * last happened); the child keeps only its hash. Never in an answer. |
| @@ -254,6 +257,7 @@ async function make( | ||
| 254 | 257 | ...(plan.forWhom === undefined ? {} : { forWhom: plan.forWhom }), |
| 255 | 258 | apps: plan.apps.map((a) => a.ref), |
| 256 | 259 | createdAt: new Date().toISOString(), |
| 260 | size: plan.size ?? DEFAULT_SIZE, | |
| 257 | 261 | statusToken: secret(), |
| 258 | 262 | beatKey: secret(), |
| 259 | 263 | }; |
| @@ -448,6 +452,7 @@ async function copyInto( | ||
| 448 | 452 | // The child's own address: <name>--<parent's host>. |
| 449 | 453 | url: liveChildUrl(app, plan.name), |
| 450 | 454 | keepAccess: plan.keepAccess, |
| 455 | size: plan.size ?? DEFAULT_SIZE, | |
| 451 | 456 | // Each app is a fork: where it came from, so the child can follow it. |
| 452 | 457 | apps: plan.apps.map((a) => ({ repo: `${plan.org}/${a.ref.split("/")[1]}`, upstream: a.ref })), |
| 453 | 458 | ...(plan.fresh === undefined ? {} : { fresh: `${plan.org}/${plan.fresh.name}` }), |
| @@ -656,6 +661,7 @@ export interface Child { | ||
| 656 | 661 | readonly apps: readonly string[]; |
| 657 | 662 | /** When it was begun; null on a ledger older than the client list. */ |
| 658 | 663 | readonly createdAt: string | null; |
| 664 | readonly size: Size; | |
| 659 | 665 | } |
| 660 | 666 | |
| 661 | 667 | /** The children this forge has made or begun, from its ledger, by name. */ |
| @@ -674,6 +680,7 @@ export async function listChildren(app: App): Promise<Child[]> { | ||
| 674 | 680 | for: ledger.forWhom ?? null, |
| 675 | 681 | apps: ledger.apps ?? [], |
| 676 | 682 | createdAt: ledger.createdAt ?? null, |
| 683 | size: isSize(ledger.size) ? ledger.size : DEFAULT_SIZE, | |
| 677 | 684 | }); |
| 678 | 685 | } |
| 679 | 686 | return out; |
src/http/mcp.ts +5
| @@ -1490,6 +1490,11 @@ const TOOLS: readonly Tool[] = [ | ||
| 1490 | 1490 | description: |
| 1491 | 1491 | "How the child follows this forge's code: tell-me (default) says when the parent has moved; take-updates takes them.", |
| 1492 | 1492 | }, |
| 1493 | size: { | |
Add a test that MCP |
||
| 1494 | type: "string", | |
| 1495 | enum: ["small", "medium", "large"], | |
| 1496 | description: "How much the child gets: small (default), medium or large.", | |
| 1497 | }, | |
| 1493 | 1498 | keepAccess: { |
| 1494 | 1499 | type: "boolean", |
| 1495 | 1500 | description: |
src/spawn/door.ts +1
| @@ -257,6 +257,7 @@ export async function spawnDoor( | ||
| 257 | 257 | budgetUsd: raw["budgetUsd"], |
| 258 | 258 | fresh: raw["fresh"], |
| 259 | 259 | followMode: raw["followMode"], |
| 260 | size: raw["size"], | |
No test covers this line: remove it and every suite stays green. Add one: POST /api/v1/forges with |
||
| 260 | 261 | ...(anon ? {} : { for: raw["for"] }), |
| 261 | 262 | }; |
| 262 | 263 |
src/spawn/plan.ts +14
| @@ -9,6 +9,7 @@ | ||
| 9 | 9 | |
| 10 | 10 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 11 | 11 | import { DEFAULT_MODE, type FollowMode } from "./chain.ts"; |
| 12 | import { parseSize, type Size } from "./sizes.ts"; | |
| 12 | 13 | |
| 13 | 14 | export const MAX_APPS = 50; |
| 14 | 15 | export const MAX_BRIEFS = 100; |
| @@ -69,6 +70,8 @@ export interface SpawnInput { | ||
| 69 | 70 | readonly fresh?: unknown; |
| 70 | 71 | /** How the child follows the forge's code: `tell-me` (default) or `take-updates`. */ |
| 71 | 72 | readonly followMode?: unknown; |
| 73 | /** `small`, `medium` or `large`; optional. */ | |
| 74 | readonly size?: unknown; | |
| 72 | 75 | } |
| 73 | 76 | |
| 74 | 77 | /** What the parent has to offer. Extra fields are ignored, never copied. */ |
| @@ -109,6 +112,8 @@ export interface SpawnPlan { | ||
| 109 | 112 | readonly fresh?: PlannedFresh; |
| 110 | 113 | /** How the child follows its parent's forge code; absent means `tell-me`. */ |
| 111 | 114 | readonly followMode?: FollowMode; |
| 115 | /** The size asked for; absent means `small`. */ | |
| 116 | readonly size?: Size; | |
| 112 | 117 | } |
| 113 | 118 | |
| 114 | 119 | export interface DryRun { |
| @@ -250,6 +255,14 @@ export function planSpawn( | ||
| 250 | 255 | if (input.followMode !== DEFAULT_MODE) followMode = input.followMode; |
| 251 | 256 | } |
| 252 | 257 | |
| 258 | // Checked last, so every older refusal keeps its place. | |
| 259 | let size: Size | undefined; | |
| 260 | if (input.size !== undefined && input.size !== null && input.size !== "") { | |
| 261 | const parsed = parseSize(input.size); | |
| 262 | if (!parsed.ok) return refuse("size", parsed.error); | |
| 263 | size = parsed.size; | |
| 264 | } | |
| 265 | ||
| 253 | 266 | // `forWhom` only when said, so a plan without it hashes as it always did. |
| 254 | 267 | return ok({ |
| 255 | 268 | name, |
| @@ -263,6 +276,7 @@ export function planSpawn( | ||
| 263 | 276 | ...(forWhom === "" ? {} : { forWhom }), |
| 264 | 277 | ...(fresh === undefined ? {} : { fresh }), |
| 265 | 278 | ...(followMode === undefined ? {} : { followMode }), |
| 279 | ...(size === undefined ? {} : { size }), | |
| 266 | 280 | }); |
| 267 | 281 | } |
| 268 | 282 |
src/spawn/sizes.ts added +29
| @@ -0,0 +1,29 @@ | ||
| 1 | /** | |
| 2 | * Sizes: what `small`, `medium` and `large` mean for a child, in one table. | |
| 3 | * Pure data and one parser; nothing here reads or writes. | |
| 4 | */ | |
| 5 | ||
| 6 | export type Size = "small" | "medium" | "large"; | |
| 7 | ||
| 8 | export interface SizeSpec { | |
| 9 | readonly cpu: number; | |
| 10 | readonly memoryMiB: number; | |
| 11 | readonly diskMiB: number; | |
| 12 | } | |
| 13 | ||
| 14 | export const SIZES: Readonly<Record<Size, SizeSpec>> = { | |
| 15 | small: { cpu: 1, memoryMiB: 512, diskMiB: 2048 }, | |
| 16 | medium: { cpu: 2, memoryMiB: 2048, diskMiB: 8192 }, | |
| 17 | large: { cpu: 4, memoryMiB: 8192, diskMiB: 32768 }, | |
| 18 | }; | |
| 19 | ||
| 20 | export const DEFAULT_SIZE: Size = "small"; | |
| 21 | ||
| 22 | export const isSize = (v: unknown): v is Size => | |
| 23 | typeof v === "string" && Object.hasOwn(SIZES, v); | |
| 24 | ||
| 25 | /** A size, or one sentence saying which are allowed. */ | |
| 26 | export function parseSize(v: unknown): { ok: true; size: Size } | { ok: false; error: string } { | |
| 27 | if (isSize(v)) return { ok: true, size: v }; | |
| 28 | return { ok: false, error: "must be small, medium or large" }; | |
| 29 | } |
test/spawn-api.test.ts +36 −1
| @@ -4,7 +4,7 @@ | ||
| 4 | 4 | * fake placement stands in for where the child lives. |
| 5 | 5 | */ |
| 6 | 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 7 | import { mkdtemp, readdir, rm } from "node:fs/promises"; | |
| 7 | import { mkdir, mkdtemp, readdir, rm } from "node:fs/promises"; | |
| 8 | 8 | import { tmpdir } from "node:os"; |
| 9 | 9 | import { join } from "node:path"; |
| 10 | 10 | import { createApp, type App } from "../src/app.ts"; |
| @@ -150,6 +150,7 @@ describe("spawn over the API and MCP", () => { | ||
| 150 | 150 | for: null, |
| 151 | 151 | apps: expect.any(Array), |
| 152 | 152 | createdAt: expect.any(String), |
| 153 | size: "small", | |
| 153 | 154 | }, |
| 154 | 155 | ]); |
| 155 | 156 | // The same request again is answered, not made twice. |
| @@ -198,4 +199,38 @@ describe("spawn over the API and MCP", () => { | ||
| 198 | 199 | expect(body.claimUrl).toContain("/claim#"); |
| 199 | 200 | expect(body.says).toContain("live secret"); |
| 200 | 201 | }); |
| 202 | ||
| 203 | const ledgerOf = async (name: string) => JSON.parse(await Bun.file(join(dataDir, "spawn", `${name}.json`)).text()); | |
| 204 | const parentOf = async (bodyId: string) => JSON.parse(await Bun.file(join(dataDir, "children", bodyId, "parent.json")).text()); | |
| 205 | const sizeListed = async (name: string) => ((await (await get(ro)).json()) as any).items.find((i: any) => i.name === name)?.size; | |
| 206 | ||
| 207 | test("size travels through POST /api/v1/forges into the ledger, parent.json and the list; huge is refused", async () => { | |
| 208 | const bad = await post(rw, { name: "sized-api", apps: [], size: "huge" }); | |
| 209 | expect(bad.status).toBe(422); | |
| 210 | expect(((await bad.json()) as any).message).toStartWith("size:"); | |
| 211 | const res = await post(rw, { name: "sized-api", apps: [], size: "medium", dryRun: false }); | |
| 212 | expect(res.status).toBe(201); | |
| 213 | const ledger = await ledgerOf("sized-api"); | |
| 214 | expect(ledger.size).toBe("medium"); | |
| 215 | expect((await parentOf(ledger.bodyId)).size).toBe("medium"); | |
| 216 | expect(await sizeListed("sized-api")).toBe("medium"); | |
| 217 | }); | |
| 218 | ||
| 219 | test("size travels through MCP spawn into the ledger, parent.json and the list", async () => { | |
| 220 | const m = await mcp(rw, { name: "sized-mcp", apps: [], size: "large", dryRun: false }); | |
| 221 | expect(m.error).toBe(false); | |
| 222 | const ledger = await ledgerOf("sized-mcp"); | |
| 223 | expect(ledger.size).toBe("large"); | |
| 224 | expect((await parentOf(ledger.bodyId)).size).toBe("large"); | |
| 225 | expect(await sizeListed("sized-mcp")).toBe("large"); | |
| 226 | }); | |
| 227 | ||
| 228 | test("a ledger without a size, or with a bad one, lists as small", async () => { | |
| 229 | const dir = join(dataDir, "spawn"); | |
| 230 | await mkdir(dir, { recursive: true }); | |
| 231 | await Bun.write(join(dir, "legacy-a.json"), JSON.stringify({ name: "legacy-a", bodyId: "old-a", done: true })); | |
| 232 | await Bun.write(join(dir, "legacy-b.json"), JSON.stringify({ name: "legacy-b", bodyId: "old-b", done: true, size: "__proto__" })); | |
| 233 | expect(await sizeListed("legacy-a")).toBe("small"); | |
| 234 | expect(await sizeListed("legacy-b")).toBe("small"); | |
| 235 | }); | |
| 201 | 236 | }); |
test/spawn-keys.test.ts +1
| @@ -364,6 +364,7 @@ describe("a spawned child's own keys and domain", () => { | ||
| 364 | 364 | "keepAccess", |
| 365 | 365 | "name", |
| 366 | 366 | "org", |
| 367 | "size", | |
| 367 | 368 | // The hash of the parent's status secret (#776): the child keeps only |
| 368 | 369 | // the hash, so this names nothing in the parent and reveals no secret. |
| 369 | 370 | "statusHash", |
test/spawn-sizes.test.ts added +47
| @@ -0,0 +1,47 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 2 | import { planSpawn } from "../src/spawn/plan.ts"; | |
| 3 | import { parseSize, SIZES } from "../src/spawn/sizes.ts"; | |
| 4 | import { sha256 } from "../src/kernel/hash.ts"; | |
| 5 | ||
| 6 | const source = { catalog: [], briefs: [], subscriptions: [] }; | |
| 7 | const base = { name: "kid", org: "kid", apps: [], carryBriefs: false, carrySubscriptions: false }; | |
| 8 | ||
| 9 | describe("sizes", () => { | |
| 10 | test("one table maps each size to cpu, memory and disk", () => { | |
| 11 | expect(Object.keys(SIZES)).toEqual(["small", "medium", "large"]); | |
| 12 | for (const s of Object.values(SIZES)) { | |
| 13 | expect(s.cpu).toBeGreaterThan(0); | |
| 14 | expect(s.memoryMiB).toBeGreaterThan(0); | |
| 15 | expect(s.diskMiB).toBeGreaterThan(0); | |
| 16 | } | |
| 17 | }); | |
| 18 | ||
| 19 | test("parseSize returns a size or one sentence", () => { | |
| 20 | expect(parseSize("large")).toEqual({ ok: true, size: "large" }); | |
| 21 | for (const bad of ["huge", "", 3, null, "toString", "__proto__"]) { | |
| 22 | const r = parseSize(bad); | |
| 23 | expect(r.ok).toBe(false); | |
| 24 | if (!r.ok) expect(r.error).toBe("must be small, medium or large"); | |
| 25 | } | |
| 26 | }); | |
| 27 | ||
| 28 | test("a plan without a size hashes as it always did", () => { | |
| 29 | const r = planSpawn(base, source); | |
| 30 | expect(r.ok).toBe(true); | |
| 31 | if (!r.ok) return; | |
| 32 | expect("size" in r.value).toBe(false); | |
| 33 | expect(sha256(JSON.stringify(r.value))).toBe( | |
| 34 | "24a0aabd3a29917ba38154bc05fb1a8a751133adf0bf4b0815ba02f96525570b", | |
| 35 | ); | |
| 36 | }); | |
| 37 | ||
| 38 | test("a size given is in the plan; an unknown one is refused after the other checks", () => { | |
| 39 | const r = planSpawn({ ...base, size: "medium" }, source); | |
| 40 | expect(r.ok && r.value.size).toBe("medium"); | |
| 41 | const bad = planSpawn({ ...base, size: "huge" }, source); | |
| 42 | expect(bad).toEqual({ ok: false, error: { field: "size", reason: "must be small, medium or large" } }); | |
| 43 | // An earlier field's refusal wins. | |
| 44 | const first = planSpawn({ ...base, direction: 5, size: "huge" }, source); | |
| 45 | expect(!first.ok && first.error.field).toBe("direction"); | |
| 46 | }); | |
| 47 | }); |
test/spawn.test.ts +1
| @@ -85,6 +85,7 @@ describe("spawnForge", () => { | ||
| 85 | 85 | org: "kids", |
| 86 | 86 | url: "https://kid--forge.test", |
| 87 | 87 | keepAccess: false, |
| 88 | size: "small", | |
| 88 | 89 | // A hash only: what the parent answers to stays in its own ledger. |
| 89 | 90 | statusHash: expect.stringMatching(/^[0-9a-f]{64}$/), |
| 90 | 91 | // The child signs heartbeats to its parent with it (#425). |
new above
offline
The legacy fallback is untested, because every ledger in the tests is written by new code that always sets
size. Write a ledger withoutsize(or with a bad value) and assert that the list readssmall.