Public: anyone can read this; the crew can change it.
A new forge is proven once it is up: the spawn answer no longer says "Running, not reachable" for a forge that answers a second later #13801
Check · Review · Land · Live done — Landed and live in production.
trevato/scale-spawn-answers → main 1 commit
3 files
changed +38
−4
src/spawn/door.ts +11 −4
| @@ -12,7 +12,7 @@ import { nameShapeVerdict } from "./name_verdict.ts"; | ||
| 12 | 12 | import { linkRequestVisitor } from "../perf/visitor_link.ts"; |
| 13 | 13 | import { templateOfInput } from "../actions/template_timing.ts"; |
| 14 | 14 | import { startWatch, probeOf, type Probe } from "./live.ts"; |
| 15 | import { firstProof, reachWords, settleChild } from "./reach.ts"; | |
| 15 | import { firstProof, proves, reachWords, settleChild } from "./reach.ts"; | |
| 16 | 16 | import { |
| 17 | 17 | forgeNameVerdict, |
| 18 | 18 | founderClash, |
| @@ -43,7 +43,7 @@ import { | ||
| 43 | 43 | } from "./placement.ts"; |
| 44 | 44 | import { parseConfig, recipeNamed, type ForgeConfig } from "./recipes.ts"; |
| 45 | 45 | import { appEntry, dryRun, planSpawn, KEY_BUDGET_NOTE, MAX_APPS, type CatalogApp, type CrewBrief, type SpawnSource } from "./plan.ts"; |
| 46 | import { bootHostedChild } from "./host.ts"; | |
| 46 | import { bootHostedChild, wakeLimits } from "./host.ts"; | |
| 47 | 47 | import { cellChildren, cellPlacement, ownsBody } from "./cell.ts"; |
| 48 | 48 | import { cellsAvailable } from "../apps/contain.ts"; |
| 49 | 49 | import { isSealed } from "./sealed.ts"; |
| @@ -476,14 +476,21 @@ export async function spawnDoor( | ||
| 476 | 476 | // The visitor who started (or was given) this forge becomes someone, on the host's side (#11265). |
| 477 | 477 | if (!made.value.existed && !paid) linkRequestVisitor(ctx.app, ctx.request, made.value.name, gift === undefined ? "started" : "paid"); |
| 478 | 478 | // Its apps come up now, so the first visit after a claim finds them awake. |
| 479 | let up: Promise<void> | undefined; | |
| 479 | 480 | if (!made.value.existed) { |
| 480 | void bootHostedChild(ctx.app, made.value.name); | |
| 481 | up = bootHostedChild(ctx.app, made.value.name); | |
| 481 | 482 | startWatch(ctx.app, { name: made.value.name, startedAt, ...templateOfInput(raw["template"]), probe: (probes.get(ctx.app) ?? probeOf)(made.value.url) }); |
| 482 | 483 | } |
| 483 | 484 | // Proven from outside before it is called done: the child's home and each |
| 484 | 485 | // app's public address, each with what came back. What does not answer yet |
| 485 | 486 | // is kept proving behind this answer, and is an alarm if it never does. |
| 486 | const proved = made.value.existed || opts.page === true ? undefined : await firstProof(ctx.app, made.value.name); | |
| 487 | const prove = !made.value.existed && opts.page !== true; | |
| 488 | // The proof asks the forge's own address, so it waits for the forge to come up first, for as | |
| 489 | // long as a visitor's request is held for a waking one (`wakeLimits.waitMs`). Asked while its | |
| 490 | // cell was still starting, every new forge read "Running, not reachable: home no answer" in | |
| 491 | // this answer and answered a second later. | |
| 492 | if (prove && up !== undefined && proves(ctx.app)) await Promise.race([up, Bun.sleep(wakeLimits.waitMs)]); | |
| 493 | const proved = prove ? await firstProof(ctx.app, made.value.name) : undefined; | |
| 487 | 494 | if (proved !== undefined || (opts.page === true && !made.value.existed)) void settleChild(ctx.app, made.value.name); |
| 488 | 495 | return { |
| 489 | 496 | status: made.value.existed ? 200 : 201, |
src/spawn/reach.ts +6
| @@ -102,6 +102,12 @@ export function useReachByDefault(o: Options): void { | ||
| 102 | 102 | const optionsOf = (app: App): Options => options.get(app) ?? fallback; |
| 103 | 103 | const off = (app: App): boolean => optionsOf(app).fetch === undefined && optionsOf(app).off === true; |
| 104 | 104 | |
| 105 | /** | |
| 106 | * Whether this forge proves its children from outside at all (not where a test suite turned it | |
| 107 | * off). | |
| 108 | */ | |
| 109 | export const proves = (app: App): boolean => !off(app); | |
| 110 | ||
| 105 | 111 | export const homeUrlOf = (child: Pick<Child, "url">): string => `${child.url}/api/v1/forge`; |
| 106 | 112 | |
| 107 | 113 | /** An app of a child, at the address a visitor types: `<app>--<child>--<parentHost>`. */ |
test/spawn-reach-door.test.ts +21
| @@ -17,6 +17,7 @@ import { serve, type Servers } from "../src/http/server.ts"; | ||
| 17 | 17 | import { ok } from "../src/kernel/result.ts"; |
| 18 | 18 | import { useChildFetch } from "../src/spawn/clients.ts"; |
| 19 | 19 | import { usePlacement, useProbe } from "../src/spawn/door.ts"; |
| 20 | import { childAwake } from "../src/spawn/host.ts"; | |
| 20 | 21 | import { reachAlarms, reachOf, useReach } from "../src/spawn/reach.ts"; |
| 21 | 22 | import { quietLogs } from "./helpers/globals.ts"; |
| 22 | 23 | |
| @@ -84,6 +85,26 @@ test("the spawn result lists each address with its result; a standby page is a f | ||
| 84 | 85 | expect(reachOf(app, "kid")?.alarmed).toBe(true); |
| 85 | 86 | }); |
| 86 | 87 | |
| 88 | test("the forge is proven once it is up: a new forge is not called unreachable while it starts", async () => { | |
| 89 | // The public address answers only while the forge is awake, as a real one's does. | |
| 90 | const asked: boolean[] = []; | |
| 91 | answers.set("https://early--forge.test/api/v1/forge", () => { | |
| 92 | asked.push(childAwake(app, "early")); | |
| 93 | return childAwake(app, "early") ? Response.json({ claimed: false }) : new Response("waking up", { status: 503 }); | |
| 94 | }); | |
| 95 | const res = await fetch(`${servers.url}/api/v1/forges`, { | |
| 96 | method: "POST", | |
| 97 | headers: { "content-type": "application/json", authorization: `Bearer ${token}` }, | |
| 98 | // An app to start makes its boot slower than one question to its address. | |
| 99 | body: JSON.stringify({ name: "early", apps: ["alice/notes"], dryRun: false }), | |
| 100 | }); | |
| 101 | expect(res.status).toBe(201); | |
| 102 | const made = (await res.json()) as any; | |
| 103 | expect(asked[0]).toBe(true); | |
| 104 | expect(made.reach.proofs[0]).toEqual({ label: "home", url: "https://early--forge.test/api/v1/forge", ok: true, status: 200, says: "answered 200" }); | |
| 105 | expect(made.reach.says).not.toContain("home"); | |
| 106 | }); | |
| 107 | ||
| 87 | 108 | test("/forges lists the same results, and the MCP spawn says it", async () => { |
| 88 | 109 | const list = await fetch(`${servers.url}/api/v1/forges`, { headers: { authorization: `Bearer ${token}` } }); |
| 89 | 110 | expect(list.status).toBe(200); |
new above
offline