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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

trevato/scale-spawn-answers → main 1 commit

  • Merged. trevato/scale-spawn-answers was merged into main as 9cda780.

Review

Sign in to review this change.
3 files changed +38 −4
src/spawn/door.ts +11 −4
@@ -12,7 +12,7 @@ import { nameShapeVerdict } from "./name_verdict.ts";
1212 import { linkRequestVisitor } from "../perf/visitor_link.ts";
1313 import { templateOfInput } from "../actions/template_timing.ts";
1414 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";
1616 import {
1717 forgeNameVerdict,
1818 founderClash,
@@ -43,7 +43,7 @@ import {
4343 } from "./placement.ts";
4444 import { parseConfig, recipeNamed, type ForgeConfig } from "./recipes.ts";
4545 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";
4747 import { cellChildren, cellPlacement, ownsBody } from "./cell.ts";
4848 import { cellsAvailable } from "../apps/contain.ts";
4949 import { isSealed } from "./sealed.ts";
@@ -476,14 +476,21 @@ export async function spawnDoor(
476476 // The visitor who started (or was given) this forge becomes someone, on the host's side (#11265).
477477 if (!made.value.existed && !paid) linkRequestVisitor(ctx.app, ctx.request, made.value.name, gift === undefined ? "started" : "paid");
478478 // Its apps come up now, so the first visit after a claim finds them awake.
479 let up: Promise<void> | undefined;
479480 if (!made.value.existed) {
480 void bootHostedChild(ctx.app, made.value.name);
481 up = bootHostedChild(ctx.app, made.value.name);
481482 startWatch(ctx.app, { name: made.value.name, startedAt, ...templateOfInput(raw["template"]), probe: (probes.get(ctx.app) ?? probeOf)(made.value.url) });
482483 }
483484 // Proven from outside before it is called done: the child's home and each
484485 // app's public address, each with what came back. What does not answer yet
485486 // 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;
487494 if (proved !== undefined || (opts.page === true && !made.value.existed)) void settleChild(ctx.app, made.value.name);
488495 return {
489496 status: made.value.existed ? 200 : 201,
src/spawn/reach.ts +6
@@ -102,6 +102,12 @@ export function useReachByDefault(o: Options): void {
102102 const optionsOf = (app: App): Options => options.get(app) ?? fallback;
103103 const off = (app: App): boolean => optionsOf(app).fetch === undefined && optionsOf(app).off === true;
104104
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
105111 export const homeUrlOf = (child: Pick<Child, "url">): string => `${child.url}/api/v1/forge`;
106112
107113 /** 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";
1717 import { ok } from "../src/kernel/result.ts";
1818 import { useChildFetch } from "../src/spawn/clients.ts";
1919 import { usePlacement, useProbe } from "../src/spawn/door.ts";
20 import { childAwake } from "../src/spawn/host.ts";
2021 import { reachAlarms, reachOf, useReach } from "../src/spawn/reach.ts";
2122 import { quietLogs } from "./helpers/globals.ts";
2223
@@ -84,6 +85,26 @@ test("the spawn result lists each address with its result; a standby page is a f
8485 expect(reachOf(app, "kid")?.alarmed).toBe(true);
8586 });
8687
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
87108 test("/forges lists the same results, and the MCP spawn says it", async () => {
88109 const list = await fetch(`${servers.url}/api/v1/forges`, { headers: { authorization: `Bearer ${token}` } });
89110 expect(list.status).toBe(200);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs