Public: anyone can read this; the crew can change it.

Spawn tells the owner when the claim link is ready to send; a fresh forge is not announced as "not reachable" #13799

Merged opened this in answer to

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

Merged

scout/forge:scout/13794-spawn-tells-the-owner-when-the-claim-lin → main 4 commits fork 2 threads · 2 unresolved

  • Merged. scout/13794-spawn-tells-the-owner-when-the-claim-lin was merged into main as 771c263.

Review

Sign in to review this change.
4 files changed +80 −8
src/spawn/door.ts +19 −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, proves, reachWords, settleChild } from "./reach.ts";
15 import { firstProof, homeBudgetOf, proves, reachWords, settleChild, type ChildReach } from "./reach.ts";
1616 import {
1717 forgeNameVerdict,
1818 founderClash,
@@ -489,8 +489,12 @@ export async function spawnDoor(
489489 // long as a visitor's request is held for a waking one (`wakeLimits.waitMs`). Asked while its
490490 // cell was still starting, every new forge read "Running, not reachable: home no answer" in
491491 // 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;
492 // Both waits are one budget (`HOME_WAIT_MS`): what the cell's start took is not asked again of
493 // the home.
494 const began = Date.now();
495 const budget = homeBudgetOf(ctx.app);
496 if (prove && up !== undefined && proves(ctx.app)) await Promise.race([up, Bun.sleep(Math.min(wakeLimits.waitMs, budget))]);
497 const proved = prove ? await firstProof(ctx.app, made.value.name, budget - (Date.now() - began)) : undefined;
494498 if (proved !== undefined || (opts.page === true && !made.value.existed)) void settleChild(ctx.app, made.value.name);
495499 return {
496500 status: made.value.existed ? 200 : 201,
@@ -502,7 +506,7 @@ export async function spawnDoor(
502506 : {
503507 reach: {
504508 ok: proved.ok,
505 says: proved.ok ? "Every public address answered." : `${reachWords(proved)}. Still checking; the owner is alarmed if it does not come up in 3 minutes.`,
509 says: reachSays(proved),
506510 proofs: proved.proofs,
507511 },
508512 }),
@@ -511,6 +515,17 @@ export async function spawnDoor(
511515 };
512516 }
513517
518 /**
519 * What a spawn says of the new forge: ready, still starting (never "not reachable"), or what is
520 * wrong.
521 */
522 function reachSays(proved: ChildReach): string {
523 if (proved.ok) return "Ready: send the link. Every public address answered.";
524 if (proved.proofs.some((p) => p.label === "home" && !p.ok))
525 return "It will answer in about a minute; the link works from then. Still checking; the owner is told only if it does not come up in 3 minutes.";
526 return `The claim link works. ${reachWords(proved)}. Still checking; the owner is alarmed if it does not come up in 3 minutes.`;
527 }
528
514529 /** A new claim link for an unclaimed child (#729): the old one dies. */
515530 export async function reissueDoor(ctx: Ctx, user: User, name: string): Promise<Door> {
516531 const made = await reissueClaim(ctx.app, user, name, { request: ctx.request, url: ctx.url });
src/spawn/reach.ts +24 −4
@@ -36,6 +36,11 @@ export const REACH_EVERY_MS = 5 * 60_000;
3636 /** A new child gets this long to come up before its failure is an alarm. */
3737 export const SETTLE_MS = 3 * 60_000;
3838 const SETTLE_POLL_MS = 3000;
39 /**
40 * One budget for a spawn's whole wait: the cell coming up and then its home answering (#13794).
outdated

HOME_WAIT_MS = 60_000 is too long for a synchronous tool call. Lower it, and make sure the first-boot wait in door.ts counts against it.

41 * Past it the reply says "It will answer in about a minute" and `settleChild` keeps watching.
42 */
43 export const HOME_WAIT_MS = 20_000;
3944 /** A failing sweep is confirmed once more after this, so one dropped request pages nobody. */
4045 const CONFIRM_MS = 5000;
4146 const BATCH = 4;
@@ -81,6 +86,8 @@ interface Options {
8186 readonly off?: boolean;
8287 readonly fetch?: FetchLike;
8388 readonly settleMs?: number;
89 /** How long a spawn waits for the new home to answer before it replies. */
90 readonly homeWaitMs?: number;
8491 readonly pollMs?: number;
8592 readonly confirmMs?: number;
8693 }
@@ -100,6 +107,8 @@ export function useReachByDefault(o: Options): void {
100107 }
101108
102109 const optionsOf = (app: App): Options => options.get(app) ?? fallback;
110 /** The most a spawn holds its reply for the new forge to come up and answer. */
111 export const homeBudgetOf = (app: App): number => optionsOf(app).homeWaitMs ?? HOME_WAIT_MS;
103112 const off = (app: App): boolean => optionsOf(app).fetch === undefined && optionsOf(app).off === true;
104113
105114 /**
@@ -196,10 +205,10 @@ export function proveBoots(app: App, child: Pick<Child, "url" | "bodyId">): Proo
196205 }
197206
198207 /** Ask every public address of a child, as a visitor does. Nothing is kept. */
199 export async function proveChild(app: App, child: Child): Promise<ChildReach> {
208 export async function proveChild(app: App, child: Child, home?: Proof): Promise<ChildReach> {
200209 const fetcher = optionsOf(app).fetch ?? fetch;
201210 const proofs = await Promise.all([
202 proveHome(fetcher, child),
211 home ?? proveHome(fetcher, child),
203212 ...(await slugsOf(app, child)).map((slug) => proveApp(fetcher, appUrlOf(app, child, slug), slug)),
204213 ]);
205214 const boots = proveBoots(app, child);
@@ -261,11 +270,22 @@ function record(app: App, proved: ChildReach, alarm: boolean): ChildReach {
261270 * Prove a child once and say what came back. A new child is the caller's
262271 * to settle (`settleChild`); a failure here is kept without an alarm.
263272 */
264 export async function firstProof(app: App, name: string): Promise<ChildReach | undefined> {
273 export async function firstProof(app: App, name: string, budgetMs?: number): Promise<ChildReach | undefined> {
265274 if (off(app)) return undefined;
266275 const child = (await listChildren(app)).find((c) => c.name === name);
267276 if (child === undefined || !child.done) return undefined;
268 return record(app, await proveChild(app, child), false);
277 // The link is only worth sending once the home answers: wait for it within the budget left.
outdated

A 60 s blocking wait in the request path. Bound it so that this wait and the #13801 boot wait together stay under about 25 s. Reuse the proof from the last probe instead of calling proveChild straight after, which probes the home a second time.

278 // Each probe is bounded (`PROBE_MS`) and the last one is kept, so the home is asked once, not
279 // twice.
280 const o = optionsOf(app);
281 const until = Date.now() + Math.max(0, budgetMs ?? o.homeWaitMs ?? HOME_WAIT_MS);
282 const fetcher = o.fetch ?? fetch;
283 let home = await proveHome(fetcher, child);
284 while (!home.ok && Date.now() < until && !app.closing.signal.aborted) {
285 await Bun.sleep(Math.min(o.pollMs ?? SETTLE_POLL_MS, 2000, Math.max(0, until - Date.now())));
286 home = await proveHome(fetcher, child);
287 }
288 return record(app, await proveChild(app, child, home), false);
269289 }
270290
271291 /**
test/spawn-reach-door.test.ts +32
@@ -141,3 +141,35 @@ test("the owner is paged: the notifier's alarm lines carry the unreachable child
141141 expect(lines.map((l) => l.line)).toContainEqual(expect.stringContaining("kid is running, not reachable"));
142142 expect(lines.find((l) => l.line.startsWith("kid"))?.link).toBe("/forges");
143143 });
144
145 test("a forge whose home answers late is waited for: Ready, never not reachable (#13794)", async () => {
146 let asked = 0;
147 answers.set("https://slow--forge.test/api/v1/forge", () => (++asked < 3 ? new Response("", { status: 502 }) : Response.json({ claimed: false })));
148 answers.set("https://notes--slow--forge.test/", () => new Response("ok", { headers: { "x-forge-app": "notes" } }));
149 const made = (await (await spawn("slow")).json()) as any;
150 expect(asked).toBeGreaterThanOrEqual(3);
151 expect(made.reach.ok).toBe(true);
152 expect(made.reach.says).toContain("Ready: send the link.");
153 expect(made.reach.says).not.toContain("not reachable");
154 });
155
156 test("a home that does not answer in the wait is starting, not unreachable (#13794)", async () => {
157 useReach(app, { settleMs: 0, pollMs: 1, confirmMs: 0, homeWaitMs: 5, fetch: async () => new Response("", { status: 502 }) });
158 const made = (await (await spawn("late")).json()) as any;
159 expect(made.reach.says).toContain("It will answer in about a minute; the link works from then.");
160 expect(made.reach.says).not.toContain("not reachable");
161 });
162
163 test("the wait is one budget under 25 s, and a home that answered is asked once (#13794)", async () => {
164 const { HOME_WAIT_MS } = await import("../src/spawn/reach.ts");
165 expect(HOME_WAIT_MS).toBeLessThanOrEqual(25_000);
166 let asked = 0;
167 useReach(app, { settleMs: 0, pollMs: 1, confirmMs: 0, fetch: async (url) => {
168 const u = String(url);
169 if (u === "https://once--forge.test/api/v1/forge") { asked++; return Response.json({ claimed: false }); }
170 return new Response("ok", { headers: { "x-forge-app": "notes" } });
171 } });
172 const made = (await (await spawn("once")).json()) as any;
173 expect(made.reach.says).toContain("Ready: send the link.");
174 expect(asked).toBe(1);
175 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs