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
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13794-spawn-tells-the-owner-when-the-claim-lin → main 4 commits fork 2 threads · 2 unresolved
4 files
changed +80
−8
docs/features/spawner-claim-link.md +5
| @@ -17,4 +17,9 @@ The owner check is `reissueClaim` in `src/actions/spawn.ts`: the ledger's owner | ||
| 17 | 17 | the same-origin check. A forge spawned with no plan and no key runs its crew on the host's included credit; a run that |
| 18 | 18 | finds its copy of that credit short asks the host first, at most once in 15 s (`refreshFundingOnce`). |
| 19 | 19 | |
| 20 | A spawn through the API or MCP holds its reply at most `HOME_WAIT_MS` (20 s, one budget for the cell's start and the | |
| 21 | home's first answer). A home that answers in time gets "Ready: send the link"; otherwise "It will answer in about a | |
| 22 | minute", and `settleChild` keeps watching for the 3-minute alarm. The page spawn (`opts.page`) is not held: it returns | |
| 23 | at once and the page is where the person is already waiting on the forge itself. | |
| 24 | ||
| 20 | 25 | Pinned by `test/agent-owns-forge.test.ts`, `test/crew-lent-refresh.test.ts` and `test/funding-sync.test.ts`. |
src/spawn/door.ts +19 −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, proves, reachWords, settleChild } from "./reach.ts"; | |
| 15 | import { firstProof, homeBudgetOf, proves, reachWords, settleChild, type ChildReach } from "./reach.ts"; | |
| 16 | 16 | import { |
| 17 | 17 | forgeNameVerdict, |
| 18 | 18 | founderClash, |
| @@ -489,8 +489,12 @@ export async function spawnDoor( | ||
| 489 | 489 | // long as a visitor's request is held for a waking one (`wakeLimits.waitMs`). Asked while its |
| 490 | 490 | // cell was still starting, every new forge read "Running, not reachable: home no answer" in |
| 491 | 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; | |
| 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; | |
| 494 | 498 | if (proved !== undefined || (opts.page === true && !made.value.existed)) void settleChild(ctx.app, made.value.name); |
| 495 | 499 | return { |
| 496 | 500 | status: made.value.existed ? 200 : 201, |
| @@ -502,7 +506,7 @@ export async function spawnDoor( | ||
| 502 | 506 | : { |
| 503 | 507 | reach: { |
| 504 | 508 | 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), | |
| 506 | 510 | proofs: proved.proofs, |
| 507 | 511 | }, |
| 508 | 512 | }), |
| @@ -511,6 +515,17 @@ export async function spawnDoor( | ||
| 511 | 515 | }; |
| 512 | 516 | } |
| 513 | 517 | |
| 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 | ||
| 514 | 529 | /** A new claim link for an unclaimed child (#729): the old one dies. */ |
| 515 | 530 | export async function reissueDoor(ctx: Ctx, user: User, name: string): Promise<Door> { |
| 516 | 531 | 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; | ||
| 36 | 36 | /** A new child gets this long to come up before its failure is an alarm. */ |
| 37 | 37 | export const SETTLE_MS = 3 * 60_000; |
| 38 | 38 | 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). | |
| 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; | |
| 39 | 44 | /** A failing sweep is confirmed once more after this, so one dropped request pages nobody. */ |
| 40 | 45 | const CONFIRM_MS = 5000; |
| 41 | 46 | const BATCH = 4; |
| @@ -81,6 +86,8 @@ interface Options { | ||
| 81 | 86 | readonly off?: boolean; |
| 82 | 87 | readonly fetch?: FetchLike; |
| 83 | 88 | readonly settleMs?: number; |
| 89 | /** How long a spawn waits for the new home to answer before it replies. */ | |
| 90 | readonly homeWaitMs?: number; | |
| 84 | 91 | readonly pollMs?: number; |
| 85 | 92 | readonly confirmMs?: number; |
| 86 | 93 | } |
| @@ -100,6 +107,8 @@ export function useReachByDefault(o: Options): void { | ||
| 100 | 107 | } |
| 101 | 108 | |
| 102 | 109 | 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; | |
| 103 | 112 | const off = (app: App): boolean => optionsOf(app).fetch === undefined && optionsOf(app).off === true; |
| 104 | 113 | |
| 105 | 114 | /** |
| @@ -196,10 +205,10 @@ export function proveBoots(app: App, child: Pick<Child, "url" | "bodyId">): Proo | ||
| 196 | 205 | } |
| 197 | 206 | |
| 198 | 207 | /** 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> { | |
| 200 | 209 | const fetcher = optionsOf(app).fetch ?? fetch; |
| 201 | 210 | const proofs = await Promise.all([ |
| 202 | proveHome(fetcher, child), | |
| 211 | home ?? proveHome(fetcher, child), | |
| 203 | 212 | ...(await slugsOf(app, child)).map((slug) => proveApp(fetcher, appUrlOf(app, child, slug), slug)), |
| 204 | 213 | ]); |
| 205 | 214 | const boots = proveBoots(app, child); |
| @@ -261,11 +270,22 @@ function record(app: App, proved: ChildReach, alarm: boolean): ChildReach { | ||
| 261 | 270 | * Prove a child once and say what came back. A new child is the caller's |
| 262 | 271 | * to settle (`settleChild`); a failure here is kept without an alarm. |
| 263 | 272 | */ |
| 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> { | |
| 265 | 274 | if (off(app)) return undefined; |
| 266 | 275 | const child = (await listChildren(app)).find((c) => c.name === name); |
| 267 | 276 | 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 |
||
| 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); | |
| 269 | 289 | } |
| 270 | 290 | |
| 271 | 291 | /** |
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 | ||
| 141 | 141 | expect(lines.map((l) => l.line)).toContainEqual(expect.stringContaining("kid is running, not reachable")); |
| 142 | 142 | expect(lines.find((l) => l.line.startsWith("kid"))?.link).toBe("/forges"); |
| 143 | 143 | }); |
| 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 | }); |
new above
offline
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.