Public: anyone can read this; the crew can change it.
New forge never refreshes the Places catalog, so a cloud stays unpickable unt… #16576
Check · Review · Land · Live — Its check is red.
mason/forge:mason/16543-new-forge-never-refreshes-the-places-cat → main 8 commits fork
5 files
changed +127
−2
docs/features/places-read-on-visit.md added +13
| @@ -0,0 +1,13 @@ | ||
| 1 | --- | |
| 2 | topic: What a forge is | |
| 3 | row: A cloud becomes pickable on New forge without anyone opening Places | |
| 4 | --- | |
| 5 | # The Places catalog is read again when someone looks | |
| 6 | ||
| 7 | The Places catalog (what each cloud offers, and where) is kept for 6 hours. When it is older, the next visit to New forge reads it again before the page renders, so a forge nobody opens Places on still offers a cloud that has become orderable. The page waits for that read for at most two seconds; a slower read carries on and the next view sees it. A provider is asked again no sooner than five minutes after its last try, and a failed read leaves the last good one standing. | |
| 8 | ||
| 9 | The terminal's New forge screen does the same: it opens at once, starts the read, and the Where choices follow when it lands. | |
| 10 | ||
| 11 | Only an admin is offered clouds on New forge, so this changes what an admin sees. The read names no one in the audit. | |
| 12 | ||
| 13 | Code: `refreshForPage` in `src/actions/places_refresh.ts`; callers `src/web/pages/new-forge.ts`, `src/tui/screens/newforge.ts`. Test: `test/new-forge-refresh.test.ts`. |
src/actions/places_refresh.ts +32 −1
| @@ -11,6 +11,7 @@ import { LISTED, RETRY_MS, STALE_MS, noteTry, readPlaces, readTimes, triedTimes | ||
| 11 | 11 | import { refreshRate } from "../cloud/rates.ts"; |
| 12 | 12 | import { listCloudAccounts, secretNameOf } from "../core/cloud_account.ts"; |
| 13 | 13 | import { resolveSecrets } from "../core/secret_store.ts"; |
| 14 | import { during } from "../perf/jobs.ts"; | |
| 14 | 15 | import { net } from "./cloud.ts"; |
| 15 | 16 | |
| 16 | 17 | const listed = (provider: string): boolean => (LISTED as readonly string[]).includes(provider); |
| @@ -26,9 +27,11 @@ const running = new WeakMap<object, Promise<void>>(); | ||
| 26 | 27 | * `RETRY_MS`. |
| 27 | 28 | */ |
| 28 | 29 | export function isStale(app: App, now = Date.now()): boolean { |
| 30 | const accounts = listCloudAccounts(app.db).filter((a) => listed(a.provider)); | |
| 31 | if (accounts.length === 0) return false; | |
| 29 | 32 | const read = readTimes(app.db); |
| 30 | 33 | const tried = triedTimes(app.db); |
| 31 | return listCloudAccounts(app.db).some((a) => { | |
| 34 | return accounts.some((a) => { | |
| 32 | 35 | if (!listed(a.provider)) return false; |
| 33 | 36 | if (now - Date.parse(read[a.provider] ?? "") < STALE_MS) return false; |
| 34 | 37 | return !(now - Date.parse(tried[a.provider] ?? "") < RETRY_MS); |
| @@ -70,3 +73,31 @@ export async function refreshIfDue(app: App, who: Reader): Promise<void> { | ||
| 70 | 73 | if (!running.has(app.db) && !isStale(app)) return; |
| 71 | 74 | await refreshCatalog(app, who).catch(() => undefined); |
| 72 | 75 | } |
| 76 | ||
| 77 | /** When each forge's catalog was last found fresh, and how long that stands for a page. */ | |
| 78 | const looked = new WeakMap<object, number>(); | |
| 79 | const LOOK_MS = 10_000; | |
| 80 | ||
| 81 | /** The longest a page waits on a refresh; a slower one carries on and the next view sees it. */ | |
| 82 | export const PAGE_WAIT_MS = 2_000; | |
| 83 | ||
| 84 | /** | |
| 85 | * The refresh a page of the catalog (New forge) starts before it renders, so a catalog | |
| 86 | * goes stale for no one: nothing when it is fresh, else the refresh, awaited for at most | |
| 87 | * `PAGE_WAIT_MS`. Nobody's token is named in the audit (a paid order reads the same way). | |
| 88 | */ | |
| 89 | export async function refreshForPage(app: App, now = Date.now()): Promise<void> { | |
| 90 | if (!running.has(app.db)) { | |
| 91 | // A catalog goes stale over hours: a fresh one is looked at again no sooner than `LOOK_MS`, so | |
| 92 | // a busy page spends no statement on it each visit. | |
| 93 | if (now - (looked.get(app.db) ?? -Infinity) < LOOK_MS) return; | |
| 94 | if (!isStale(app, now)) { | |
| 95 | looked.set(app.db, now); | |
| 96 | return; | |
| 97 | } | |
| 98 | } | |
| 99 | let timer: ReturnType<typeof setTimeout> | undefined; | |
| 100 | const wait = new Promise<void>((r) => (timer = setTimeout(() => during("places-page-wait", () => r(), undefined, app), PAGE_WAIT_MS))); | |
| 101 | await Promise.race([refreshCatalog(app, { id: undefined }).catch(() => undefined), wait]); | |
| 102 | clearTimeout(timer); | |
| 103 | } |
src/tui/screens/newforge.ts +12 −1
| @@ -19,6 +19,7 @@ import { MAX_BUDGET_USD, MIN_BUDGET_USD } from "../../spawn/plan.ts"; | ||
| 19 | 19 | import { listRecipes, saveRecipe } from "../../spawn/recipes.ts"; |
| 20 | 20 | import { placeChoice } from "../../spawn/making.ts"; |
| 21 | 21 | import { cloudsOffered, parseWhere, whereChoices, whereFields, whereHint } from "../../spawn/clouds_offered.ts"; |
| 22 | import { refreshForPage } from "../../actions/places_refresh.ts"; | |
| 22 | 23 | import { FREE, getPlan, getPlans } from "../../settings/plans.ts"; |
| 23 | 24 | import type { Ctx, Screen } from "../app/app.ts"; |
| 24 | 25 | import { binding } from "../app/keys.ts"; |
| @@ -88,7 +89,7 @@ export function newForgeScreen(f: Session, ctx: Ctx, initial: Record<string, str | ||
| 88 | 89 | // Your saved recipes, by name: only a signed-in person has any. |
| 89 | 90 | const recipes = f.user === undefined ? [] : listRecipes(f.app.config.dataDir, f.user.id).map((r) => r.name); |
| 90 | 91 | // An admin's clouds, as the page's "Where it runs" (`spawn/clouds_offered.ts`): free, at any place. |
| 91 | const offers = cloudsOffered(f.app, f.user); | |
| 92 | let offers = cloudsOffered(f.app, f.user); | |
| 92 | 93 | const paid = f.user?.isAdmin === true && getPlans(f.app.db).some((p) => p.key !== FREE); |
| 93 | 94 | const specs = [ |
| 94 | 95 | ...SPECS.slice(0, 1), |
| @@ -98,6 +99,16 @@ export function newForgeScreen(f: Session, ctx: Ctx, initial: Record<string, str | ||
| 98 | 99 | // A paid plan's place, for a forge here; a cloud's place is chosen with the cloud, under Where. |
| 99 | 100 | ...(paid ? [{ ...PLAN_SPEC, choices: ["", ...getPlans(f.app.db).filter((p) => p.key !== FREE).map((p) => p.key)] }, PLACE_SPEC] : []), |
| 100 | 101 | ]; |
| 102 | // A stale Places catalog is read again, as the page does, and the places offered follow it. | |
| 103 | if (offers.length > 0) { | |
| 104 | runAsync(ctx, async () => { | |
| 105 | await refreshForPage(f.app); | |
| 106 | offers = cloudsOffered(f.app, f.user); | |
| 107 | const at = specs.findIndex((s) => s.key === WHERE_SPEC.key); | |
| 108 | if (at >= 0) specs[at] = { ...WHERE_SPEC, choices: ["", ...whereChoices(offers)] }; | |
| 109 | ctx.invalidate(); | |
| 110 | }); | |
| 111 | } | |
| 101 | 112 | /** Where a paid plan's forge may run: the default first, then the catalog's places for its size. */ |
| 102 | 113 | const where = () => placeChoice(f.app, getPlan(f.app.db, values["plan"] ?? ""), true); |
| 103 | 114 | const choicesOf = (spec: Spec): readonly string[] | undefined => |
src/web/pages/new-forge.ts +2
| @@ -46,6 +46,7 @@ import { html, type Html } from "../html.ts"; | ||
| 46 | 46 | import { timeAgo } from "../time_element.ts"; |
| 47 | 47 | import { noteForgeMade } from "../funnel_door.ts"; |
| 48 | 48 | import { page, redirect } from "../layout.ts"; |
| 49 | import { refreshForPage } from "../../actions/places_refresh.ts"; | |
| 49 | 50 | import { placeChoiceView } from "../pinned.ts"; |
| 50 | 51 | import { orderCookie } from "./pricing.ts"; |
| 51 | 52 | import { placeChoice } from "../../spawn/making.ts"; |
| @@ -691,6 +692,7 @@ export async function newForgePage(ctx: Ctx): Promise<Response> { | ||
| 691 | 692 | const user = ctx.auth.user; |
| 692 | 693 | if (user && user.kind !== "user") return notFoundPage(ctx); |
| 693 | 694 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
| 695 | await refreshForPage(ctx.app); | |
| 694 | 696 | const reads = await readsFor(ctx, user); |
| 695 | 697 | const rendering = performance.now(); |
| 696 | 698 | // A code in the address moves to a cookie and out of the address (the log and history never hold |
test/new-forge-refresh.test.ts added +68
| @@ -0,0 +1,68 @@ | ||
| 1 | /** | |
| 2 | * New forge reads the Places catalog again when it is stale (#16543): nobody has to open Places | |
| 3 | * for a cloud to become pickable. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { addAccount, allowWrite, net } from "../src/actions/cloud.ts"; | |
| 11 | import { setCeiling } from "../src/actions/cloud_ceiling.ts"; | |
| 12 | import { changeBaseDomain } from "../src/actions/pinned.ts"; | |
| 13 | import { isStale } from "../src/actions/places_refresh.ts"; | |
| 14 | import { createUser, getUserByName } from "../src/core/users.ts"; | |
| 15 | import type { Ctx } from "../src/http/context.ts"; | |
| 16 | import { newForgePage } from "../src/web/pages/new-forge.ts"; | |
| 17 | import { hetzner } from "./helpers/places-fixture.ts"; | |
| 18 | import { quietLogs } from "./helpers/globals.ts"; | |
| 19 | ||
| 20 | quietLogs(); | |
| 21 | ||
| 22 | let calls = 0; | |
| 23 | net.fetch = async (url) => { | |
| 24 | const u = String(url); | |
| 25 | if (u.includes("ecb.europa.eu")) return new Response(`<Cube><Cube time='2026-11-24'><Cube currency='USD' rate='1.08'/></Cube></Cube>`); | |
| 26 | if (!u.includes("api.hetzner.cloud/v1/")) return new Response(JSON.stringify({ account: { email: "[email protected]", status: "active" } })); | |
| 27 | calls++; | |
| 28 | const j = (v: unknown) => new Response(JSON.stringify(v)); | |
| 29 | return u.includes("server_types") ? j({ server_types: hetzner.server_types }) : u.includes("/locations") ? j({ locations: hetzner.locations }) : j(hetzner.pricing); | |
| 30 | }; | |
| 31 | ||
| 32 | let dir = ""; | |
| 33 | let app: App; | |
| 34 | ||
| 35 | beforeAll(async () => { | |
| 36 | dir = await mkdtemp(join(tmpdir(), "forge-new-forge-refresh-")); | |
| 37 | app = createApp({ dataDir: dir, port: 0, forgeRepo: "alice/forge", url: "https://forge.test" }); | |
| 38 | const a = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 39 | if (!a.ok) throw new Error(a.error); | |
| 40 | const alice = getUserByName(app.db, "alice")!; | |
| 41 | const added = await addAccount(app, alice, "hetzner", "main", "Zq4TxLm7Kp2Vb8nWcR59Zq4TxLm7Kp2Vb8nWcR59Zq4TxLm7Kp2Vb8nWcR5ab"); | |
| 42 | if (!added.ok) throw new Error(String(added.error)); | |
| 43 | allowWrite(app, alice, "hetzner", "main", true); | |
| 44 | expect(setCeiling(app, alice, "hetzner", "main", 50).ok).toBe(true); | |
| 45 | expect(changeBaseDomain(app, alice, "forges.test").ok).toBe(true); | |
| 46 | }, 30_000); | |
| 47 | afterAll(async () => { | |
| 48 | app.close(); | |
| 49 | await rm(dir, { recursive: true, force: true }); | |
| 50 | }); | |
| 51 | ||
| 52 | const visit = async (): Promise<string> => { | |
| 53 | const url = new URL("https://forge.test/forges/new"); | |
| 54 | const ctx = { app, request: new Request(url.href), url, params: {}, auth: { user: getUserByName(app.db, "alice"), scope: undefined, via: "session" }, theme: undefined } as unknown as Ctx; | |
| 55 | return (await newForgePage(ctx)).text(); | |
| 56 | }; | |
| 57 | ||
| 58 | test("a stale catalog is read again by GET /forges/new, and the new place is offered", async () => { | |
| 59 | expect(isStale(app)).toBe(true); | |
| 60 | const first = await visit(); | |
| 61 | expect(first).not.toContain("Nowhere can be ordered"); | |
| 62 | expect(first).toContain("Nuremberg"); | |
| 63 | // That visit read it; the next asks nothing more. | |
| 64 | calls = 0; | |
| 65 | expect(isStale(app)).toBe(false); | |
| 66 | await visit(); | |
| 67 | expect(calls).toBe(0); | |
| 68 | }); |
new above
offline