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

Closed opened this
Closed without merging

scout/forge:scout/3094-a-forge-s-size-is-written-to-the-ledger → main 5 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
7 files changed +52 −3
README.md +5
@@ -162,6 +162,11 @@ curl -sX POST localhost:3000/api/v1/forges -H "authorization: Bearer $TOKEN" \
162162 bun src/main.ts --first-admin --child kid
163163 ```
164164
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
165170 The child keeps this forge's scheme and port at `<name>--<host>`, on its own
166171 data under `$FORGE_DATA/children/`. Browsers send `*.localhost` to this
167172 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";
5353 import { loadOrCreateHostKey } from "../ssh/keys.ts";
5454 import { claimLink, hasAdmin, mintClaim, spawnedOrg } from "../spawn/claim.ts";
5555 import { childPublicUrl, parentOrigin, type Placement } from "../spawn/placement.ts";
56 import { DEFAULT_SIZE, type Size } from "../spawn/sizes.ts";
5657 import { planSpawn, type SpawnInput, type SpawnPlan, type SpawnSource } from "../spawn/plan.ts";
5758 import { DEFAULT_SIZE, isSize, type Size } from "../spawn/sizes.ts";
5859 import { chainForChild, DEFAULT_MODE, forgeLinkOf, type FollowMode, type Hop } from "../spawn/chain.ts";
@@ -63,6 +64,8 @@ export interface Spawned {
6364 readonly org: string;
6465 readonly url: string;
6566 readonly bodyId: string;
67 /** The size the child was made at. */
68 readonly size: Size;
6669 /** True when this call found the child already made and changed nothing. */
6770 readonly existed: boolean;
6871 /**
@@ -233,7 +236,7 @@ async function make(
233236 });
234237 }
235238 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 });
237240 if (ledger.statusToken === undefined || ledger.beatKey === undefined) {
238241 ledger.statusToken ??= secret();
239242 ledger.beatKey ??= secret();
@@ -281,6 +284,7 @@ async function make(
281284 org: plan.org,
282285 url: liveChildUrl(app, plan.name),
283286 bodyId: ledger.bodyId,
287 size: ledger.size ?? DEFAULT_SIZE,
284288 existed: false,
285289 claimUrl,
286290 });
src/http/mcp.ts +2 −2
@@ -1983,7 +1983,7 @@ const TOOLS: readonly Tool[] = [
19831983 return good({
19841984 says: !made
19851985 ? "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}`,
19871987 ...rest,
19881988 ...(reveal ? { claimUrl } : {}),
19891989 });
@@ -1992,7 +1992,7 @@ const TOOLS: readonly Tool[] = [
19921992 {
19931993 name: "forges",
19941994 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.",
19961996 inputSchema: { type: "object", properties: {} },
19971997 writes: false,
19981998 async run(ctx, user) {
src/spawn/clients.ts +3
@@ -13,6 +13,7 @@ import type { App } from "../app.ts";
1313 import { listChildren, statusTokenOf, type Child } from "../actions/spawn.ts";
1414 import { timingSafeEqual } from "node:crypto";
1515 import { sha256 } from "../kernel/hash.ts";
16 import type { Size } from "./sizes.ts";
1617 import type { ChildAppsSummary } from "./child-apps.ts";
1718 import { hostedChildApps } from "./host.ts";
1819 import { hasAdmin, spawnedOrg } from "./claim.ts";
@@ -29,6 +30,7 @@ export interface ClientRow {
2930 readonly url: string;
3031 readonly for: string | null;
3132 readonly apps: readonly string[];
33 readonly size: Size;
3234 readonly sentAt: string | null;
3335 /** False while the child is still being made (a resumed spawn finishes it). */
3436 readonly made: boolean;
@@ -159,6 +161,7 @@ export async function clientRows(app: App): Promise<ClientRow[]> {
159161 url: c.url,
160162 for: c.for,
161163 apps: c.apps,
164 size: c.size,
162165 sentAt: c.createdAt,
163166 made: c.done,
164167 claimed: states[i]?.claimed ?? null,
test/client-list.test.ts +3
@@ -126,6 +126,7 @@ describe("the client list", () => {
126126 const lost = await spawn({
127127 name: "lou",
128128 apps: ["alice/bakery"],
129 size: "large",
129130 dryRun: false,
130131 });
131132 if (lost.status !== 201) throw new Error(`spawn lou ${lost.status}`);
@@ -262,6 +263,8 @@ describe("the client list", () => {
262263 expect(rows.maria.claimed).toBe(true);
263264 expect(typeof rows.maria.lastActivityAt).toBe("string");
264265 expect(rows.lou.claimed).toBeNull();
266 expect(rows.lou.size).toBe("large");
267 expect(rows.maria.size).toBe("small");
265268 });
266269 });
267270
test/spawn-api.test.ts +33
@@ -214,6 +214,39 @@ describe("spawn over the API and MCP", () => {
214214 expect(body.says).toContain("live secret");
215215 });
216216
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
217250 const ledgerOf = async (name: string) => JSON.parse(await Bun.file(join(dataDir, "spawn", `${name}.json`)).text());
218251 const parentOf = async (bodyId: string) => JSON.parse(await Bun.file(join(dataDir, "children", bodyId, "parent.json")).text());
219252 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 => ({
2020 url: "https://maria.test",
2121 for: "Maria's bakery",
2222 apps: [],
23 size: "small",
2324 sentAt: null,
2425 made: true,
2526 claimed: false,

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs