Public: anyone can read this; the crew can change it.
The home page's forge-name field says "free" or "taken" as you type #11273
scout/forge:scout/11239-the-home-page-s-forge-name-field-says-fr → main 6 commits fork
12 files
changed +166
−6
docs/design/areas/front.md +1
| @@ -40,3 +40,4 @@ The page a visitor reads first runs past §3's working scale on purpose, in `cla | ||
| 40 | 40 | - `.door-press-actions` — a press's actions; its empty `data-slot="host"` is where a later "host it for me" sits. |
| 41 | 41 | - `.door-wide` — a visitor's top-bar link that a phone leaves out, so the bar never overflows. |
| 42 | 42 | - `.door-start` — the hero's one form: a labelled forge-name field and "Start my forge", full width on a phone, posting to `/forges/new`. |
| 43 | - `.door-start-status` — the line under the start field that says free, taken or why not; its height is reserved so typing moves nothing. |
docs/features/start-my-forge.md +2
| @@ -6,6 +6,8 @@ A signed-out visitor starts their own forge from the home page, with no account | ||
| 6 | 6 | 2. Type a name (lowercase letters, digits and dashes) and press Enter or the button. |
| 7 | 7 | 3. The form posts to `/forges/new`, the same door `/agents` uses: the free plan and the visitor rate limits apply, and a bad name comes back worded by the door. There is no second spawn path. |
| 8 | 8 | |
| 9 | As you type, a line under the field says "Free", "Taken", or why a name is not allowed (`GET /forges/check?name=…`, `src/web/pages/forge-name.ts`; the rules are `src/spawn/name_check.ts`: the door's name shape, `src/core/reserved.ts`, and forges already made). The door refuses reserved names too. Without JavaScript the line stays empty and the form posts as before. The check is rate-limited per address at the "name" door (`src/http/strangers.ts`). | |
| 10 | ||
| 9 | 11 | The field is a slug, so it turns off autocomplete and capitalisation on phones. The form is in `src/web/studio/home.ts`; its test is `test/home-start-forge.test.ts`. |
| 10 | 12 | |
| 11 | 13 | In the terminal, a guest's "Get your own forge" command prints the plans and the web address to start from; signed in, "New forge" spawns one. |
src/http/server.ts +2
| @@ -269,6 +269,7 @@ import { | ||
| 269 | 269 | composerSubmit, |
| 270 | 270 | tellSubmit, |
| 271 | 271 | } from "../web/studio/actions.ts"; |
| 272 | import { forgeNameCheck } from "../web/pages/forge-name.ts"; | |
| 272 | 273 | import { forgeReady } from "../web/pages/forge-ready.ts"; |
| 273 | 274 | import { claimFragmentPage, claimOptions, claimSubmit } from "../web/pages/claim.ts"; |
| 274 | 275 | import { firstAdminOptions, firstAdminPage, firstAdminSubmit } from "../web/pages/first-admin.ts"; |
| @@ -850,6 +851,7 @@ export function buildRouter(): Router<Handler> { | ||
| 850 | 851 | router.add("GET", "/forges/:name/remove", forgeRemoveConfirm); |
| 851 | 852 | router.add("POST", "/forges/:name/remove", forgeRemoveSubmit); |
| 852 | 853 | router.add("GET", "/forges/:name/ready", forgeReady); |
| 854 | router.add("GET", "/forges/check", forgeNameCheck); | |
| 853 | 855 | router.add("GET", "/forges/new", newForgePage); |
| 854 | 856 | router.add("POST", "/forges/new", newForgeSubmit); |
| 855 | 857 | // The front page as a stranger sees it, for anyone; a site admin there |
src/http/strangers.ts +3 −3
| @@ -23,7 +23,7 @@ import { transferGate } from "../git/env.ts"; | ||
| 23 | 23 | import { bucketOf, clientBucket } from "./peer.ts"; |
| 24 | 24 | |
| 25 | 25 | /** A door a stranger is counted at, each with its own minute. */ |
| 26 | export type Door = "api" | "git" | "code" | "plan"; | |
| 26 | export type Door = "api" | "git" | "code" | "plan" | "name"; | |
| 27 | 27 | |
| 28 | 28 | export interface StrangerLimits { |
| 29 | 29 | /** Requests per minute through each door, per client address. */ |
| @@ -59,8 +59,8 @@ export interface StrangerLimits { | ||
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | export const STRANGER_LIMITS: StrangerLimits = { |
| 62 | perAddress: { api: 600, git: 60, code: 10, plan: 10 }, | |
| 63 | forge: { api: 6000, git: 600, code: 60, plan: 60 }, | |
| 62 | perAddress: { api: 600, git: 60, code: 10, plan: 10, name: 120 }, | |
| 63 | forge: { api: 6000, git: 600, code: 60, plan: 60, name: 1200 }, | |
| 64 | 64 | transfersPerAddress: 4, |
| 65 | 65 | transferShare: 0.5, |
| 66 | 66 | transferTimeoutMs: 7_200_000, |
src/spawn/door.ts +5
| @@ -10,6 +10,8 @@ import { startWatch, probeOf, type Probe } from "./live.ts"; | ||
| 10 | 10 | import { firstProof, reachWords, settleChild } from "./reach.ts"; |
| 11 | 11 | import { reissueClaim, spawnForge, type SpawnError } from "../actions/spawn.ts"; |
| 12 | 12 | import type { App } from "../app.ts"; |
| 13 | import { reservedName } from "../core/reserved.ts"; | |
| 14 | import { RESERVED_WORDS } from "./name_check.ts"; | |
| 13 | 15 | import { maxChildren } from "../core/capacity.ts"; |
| 14 | 16 | import { getAgentByName } from "../core/agents.ts"; |
| 15 | 17 | import { canRead, getRepo } from "../core/repos.ts"; |
| @@ -242,6 +244,9 @@ export async function spawnDoor( | ||
| 242 | 244 | } |
| 243 | 245 | |
| 244 | 246 | const db = ctx.app.db; |
| 247 | // The names the forge keeps for itself and its crew: the live check on the home page says the same. | |
| 248 | if (typeof raw["name"] === "string" && reservedName(db, raw["name"]) !== undefined) | |
| 249 | return invalid("name", RESERVED_WORDS); | |
| 245 | 250 | // Only what the caller named and may read: an unreadable app is "not a known app". |
| 246 | 251 | // A stranger reads only public repositories; their crew list is empty. |
| 247 | 252 | const catalog: CatalogApp[] = []; |
src/spawn/name_check.ts added +26
| @@ -0,0 +1,26 @@ | ||
| 1 | /** | |
| 2 | * Whether a forge name is free to start: the one answer behind | |
| 3 | * `GET /forges/check` and the home page's "free" / "taken" beside the field. | |
| 4 | * It reads the rules the spawn door applies: the name's shape (`plan.ts`), the | |
| 5 | * words the forge keeps (`core/reserved.ts`) and the children already made. | |
| 6 | */ | |
| 7 | import { childNamed } from "../actions/spawn.ts"; | |
| 8 | import type { App } from "../app.ts"; | |
| 9 | import { reservedName } from "../core/reserved.ts"; | |
| 10 | import { isForgeName, NAME_WORDS } from "./plan.ts"; | |
| 11 | ||
| 12 | export type NameState = "free" | "taken" | "reserved" | "invalid"; | |
| 13 | ||
| 14 | export interface NameCheck { | |
| 15 | readonly state: NameState; | |
| 16 | readonly message: string; | |
| 17 | } | |
| 18 | ||
| 19 | export const RESERVED_WORDS = "that name is kept by the forge; pick another"; | |
| 20 | ||
| 21 | export async function checkForgeName(app: App, name: string): Promise<NameCheck> { | |
| 22 | if (!isForgeName(name)) return { state: "invalid", message: `Use ${NAME_WORDS}` }; | |
| 23 | if (reservedName(app.db, name) !== undefined) return { state: "reserved", message: "Kept by the forge: pick another" }; | |
| 24 | if ((await childNamed(app, name)) !== undefined) return { state: "taken", message: "Taken" }; | |
| 25 | return { state: "free", message: "Free" }; | |
| 26 | } |
src/spawn/plan.ts +5 −1
| @@ -35,6 +35,10 @@ export const MAX_BUDGET_USD = 1000; | ||
| 35 | 35 | /** One line saying what a fresh app is. */ |
| 36 | 36 | export const MAX_FRESH_CHARS = 200; |
| 37 | 37 | const NAME = /^[a-z0-9](?:[a-z0-9-]{0,37}[a-z0-9])?$/; |
| 38 | ||
| 39 | /** The one rule for a forge's name: the plan and the live check both read it. */ | |
| 40 | export const NAME_WORDS = `1-39 characters of a-z, 0-9 and "-", not starting or ending with "-"`; | |
| 41 | export const isForgeName = (v: unknown): v is string => typeof v === "string" && NAME.test(v); | |
| 38 | 42 | const PLAN_KEY = /^[a-z][a-z0-9-]{0,19}$/; |
| 39 | 43 | const APP_REF = /^[A-Za-z0-9_.-]{1,100}\/[A-Za-z0-9_.-]{1,100}$/; |
| 40 | 44 | |
| @@ -169,7 +173,7 @@ export function planSpawn( | ||
| 169 | 173 | for (const field of ["name", "org"] as const) { |
| 170 | 174 | const v = input[field]; |
| 171 | 175 | if (typeof v !== "string" || !NAME.test(v)) { |
| 172 | return refuse(field, `${field} must be 1-39 characters of a-z, 0-9 and "-", not starting or ending with "-"`); | |
| 176 | return refuse(field, `${field} must be ${NAME_WORDS}`); | |
| 173 | 177 | } |
| 174 | 178 | } |
| 175 | 179 | const name = input.name as string; |
src/web/forge_name_client.ts added +22
| @@ -0,0 +1,22 @@ | ||
| 1 | /** | |
| 2 | * The home page's live name check: static code, nothing interpolated. Without | |
| 3 | * it the form posts as it always did; with it, the line under the field (its | |
| 4 | * space already reserved) says free, taken or why not. | |
| 5 | */ | |
| 6 | export const forgeNameJs = `(function(){ | |
| 7 | var i=document.getElementById("start-name"),s=document.getElementById("start-status"); | |
| 8 | if(!i||!s||!window.fetch)return; | |
| 9 | var t=0,c=null; | |
| 10 | i.addEventListener("input",function(){ | |
| 11 | clearTimeout(t);if(c)c.abort(); | |
| 12 | var v=i.value.trim(); | |
| 13 | if(v===""){s.textContent="";s.removeAttribute("data-state");return} | |
| 14 | t=setTimeout(function(){ | |
| 15 | c=new AbortController(); | |
| 16 | fetch("/forges/check?name="+encodeURIComponent(v),{signal:c.signal,credentials:"same-origin"}) | |
| 17 | .then(function(r){return r.ok?r.json():null}) | |
| 18 | .then(function(d){if(!d)return;s.textContent=d.message;s.setAttribute("data-state",d.state)}) | |
| 19 | .catch(function(){}); | |
| 20 | },250); | |
| 21 | }); | |
| 22 | })();`; |
src/web/pages/forge-name.ts added +21
| @@ -0,0 +1,21 @@ | ||
| 1 | /** | |
| 2 | * `GET /forges/check?name=…`: `{"state","message"}` for a forge name as typed | |
| 3 | * on the home page — free, taken, reserved or invalid. Read-only and | |
| 4 | * same-origin by nature (nothing is made); strangers are counted per address | |
| 5 | * and for the whole forge at the "name" door, like the other doors. | |
| 6 | */ | |
| 7 | import type { Ctx } from "../../http/context.ts"; | |
| 8 | import { json } from "../../http/respond.ts"; | |
| 9 | import { takeDoor } from "../../http/strangers.ts"; | |
| 10 | import { checkForgeName } from "../../spawn/name_check.ts"; | |
| 11 | ||
| 12 | const NO_STORE = { "cache-control": "no-store" }; | |
| 13 | ||
| 14 | export async function forgeNameCheck(ctx: Ctx): Promise<Response> { | |
| 15 | const verdict = takeDoor(ctx.app, "name", ctx.request); | |
| 16 | if (!verdict.allowed) | |
| 17 | return json({ error: "rate_limited" }, 429, { ...NO_STORE, "retry-after": String(Math.max(1, verdict.retryAfter)) }); | |
| 18 | const name = (ctx.url.searchParams.get("name") ?? "").slice(0, 80); | |
| 19 | if (name === "") return json({ state: "empty", message: "" }, 200, NO_STORE); | |
| 20 | return json(await checkForgeName(ctx.app, name), 200, NO_STORE); | |
| 21 | } |
src/web/studio/home.ts +4 −2
| @@ -46,6 +46,7 @@ import { START_REFUSALS, startRefusalIn } from "../start_refusal.ts"; | ||
| 46 | 46 | import { siteStructuredData } from "../structured_data.ts"; |
| 47 | 47 | import { running } from "../../kernel/version.ts"; |
| 48 | 48 | import { homeMilestones } from "./milestone_lines.ts"; |
| 49 | import { forgeNameJs } from "../forge_name_client.ts"; | |
| 49 | 50 | import { liveClientJs } from "../live.ts"; |
| 50 | 51 | import { |
| 51 | 52 | btn, |
| @@ -801,7 +802,8 @@ export function studioDoor(ctx: Ctx): Response { | ||
| 801 | 802 | ${refusal === undefined ? null : html`<p class="error" id="start-refused">${START_REFUSALS[refusal]}</p>`} |
| 802 | 803 | <div class="field"> |
| 803 | 804 | <label for="start-name">Forge name</label> |
| 804 | <input class="input" id="start-name" name="name" type="text" value="${keptName}" ${refusal === undefined ? null : html`aria-invalid="true" aria-describedby="start-refused"`} required autocomplete="off" autocapitalize="none" spellcheck="false" /> | |
| 805 | <input class="input" id="start-name" name="name" type="text" value="${keptName}" ${refusal === undefined ? null : html`aria-invalid="true"`} required autocomplete="off" autocapitalize="none" spellcheck="false" aria-describedby="${refusal === undefined ? "start-status" : "start-refused start-status"}" /> | |
| 806 | <p class="door-start-status small" id="start-status" role="status" aria-live="polite"></p> | |
| 805 | 807 | </div> |
| 806 | 808 | ${btn("Start my forge", { variant: "primary", size: "lg" })} |
| 807 | 809 | </form> |
| @@ -926,7 +928,7 @@ bun run src/main.ts</code></pre> | ||
| 926 | 928 | title: "forge: software for what you're building, yours to keep", |
| 927 | 929 | description: "Your own forge, with a private AI crew that builds your apps, games and business tools. Open source, and yours to keep.", |
| 928 | 930 | body, |
| 929 | scriptJs: `${liveClientJs}\n${liveRegionsJs}\n${funnelJs}`, | |
| 931 | scriptJs: `${liveClientJs}\n${liveRegionsJs}\n${funnelJs}${visitor ? `\n${forgeNameJs}` : ""}`, | |
| 930 | 932 | jsonLd: siteStructuredData(origin, running().version), |
| 931 | 933 | ...(visitor |
| 932 | 934 | ? { |
src/web/style/content.ts +2
| @@ -262,6 +262,8 @@ form.inline { display: inline; margin-left: auto; } | ||
| 262 | 262 | .door-lead .bar { gap: 8px; flex-wrap: wrap; } |
| 263 | 263 | .door-start { width: 100%; max-width: 28rem; } |
| 264 | 264 | .door-start .btn { width: 100%; } |
| 265 | .door-start-status { min-height: 1.5em; margin: 4px 0 0; color: var(--muted-fg); } | |
| 266 | .door-start-status[data-state="free"] { color: var(--fg); } | |
| 265 | 267 | .door-agent { display: flex; flex-direction: column; gap: 8px; width: 100%; max-width: 520px; font-size: 13px; color: var(--muted-fg); } |
| 266 | 268 | .door-agent a { margin-left: 8px; color: var(--fg); text-decoration: underline; text-underline-offset: 3px; text-decoration-color: var(--border-strong); } |
| 267 | 269 | /* On the input's own tinted ground the muted color falls short of AA; the words are the thing to copy. */ |
test/forge-name-check.test.ts added +73
| @@ -0,0 +1,73 @@ | ||
| 1 | /** The home page's live name check: free, taken, reserved, invalid; limited; no layout shift (#11239). */ | |
| 2 | import { content } from "../src/web/style/content.ts"; | |
| 3 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 4 | import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; | |
| 5 | import { tmpdir } from "node:os"; | |
| 6 | import { join } from "node:path"; | |
| 7 | import { createApp, type App } from "../src/app.ts"; | |
| 8 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 9 | import { strangerLimits } from "../src/http/strangers.ts"; | |
| 10 | import { checkForgeName } from "../src/spawn/name_check.ts"; | |
| 11 | ||
| 12 | let dataDir: string; | |
| 13 | let app: App; | |
| 14 | let servers: Servers; | |
| 15 | beforeAll(async () => { | |
| 16 | dataDir = await mkdtemp(join(tmpdir(), "forge-name-")); | |
| 17 | app = createApp({ dataDir, port: 0 }); | |
| 18 | servers = await serve(app); | |
| 19 | }); | |
| 20 | afterAll(async () => { | |
| 21 | await servers.stop?.(); | |
| 22 | app.close(); | |
| 23 | await rm(dataDir, { recursive: true, force: true }); | |
| 24 | }); | |
| 25 | ||
| 26 | const ask = async (name: string) => (await fetch(`${servers.url}/forges/check?name=${encodeURIComponent(name)}`)).json() as Promise<{ state: string; message: string }>; | |
| 27 | ||
| 28 | test("a name nobody holds is free", async () => { | |
| 29 | expect((await ask("fresh-forge")).state).toBe("free"); | |
| 30 | }); | |
| 31 | ||
| 32 | test("a name the forge keeps is reserved, and so is the crew's", async () => { | |
| 33 | expect((await ask("admin")).state).toBe("reserved"); | |
| 34 | expect((await ask("sc0ut")).state).toBe("free"); // no crew account in a bare forge | |
| 35 | }); | |
| 36 | ||
| 37 | test("a name that is not a name says why", async () => { | |
| 38 | const bad = await ask("Not Allowed!"); | |
| 39 | expect(bad.state).toBe("invalid"); | |
| 40 | expect(bad.message).toContain("a-z"); | |
| 41 | expect((await ask("-x")).state).toBe("invalid"); | |
| 42 | }); | |
| 43 | ||
| 44 | test("a forge already made is taken", async () => { | |
| 45 | await mkdir(join(dataDir, "spawn"), { recursive: true }); | |
| 46 | await writeFile(join(dataDir, "spawn", "made-one.json"), JSON.stringify({ name: "made-one", bodyId: "b1", planHash: "x", done: true })); | |
| 47 | expect((await checkForgeName(app, "made-one")).state).toBe("taken"); | |
| 48 | expect((await ask("made-one")).state).toBe("taken"); | |
| 49 | }); | |
| 50 | ||
| 51 | test("the check is limited per address like the door", async () => { | |
| 52 | const limit = strangerLimits(app).perAddress.name; | |
| 53 | let last = 200; | |
| 54 | for (let i = 0; i < limit + 5; i++) last = (await fetch(`${servers.url}/forges/check?name=x${i}`)).status; | |
| 55 | expect(last).toBe(429); | |
| 56 | }); | |
| 57 | ||
| 58 | test("the door refuses a reserved name too", async () => { | |
| 59 | const res = await fetch(`${servers.url}/api/v1/forges`, { | |
| 60 | method: "POST", | |
| 61 | headers: { "content-type": "application/json", origin: servers.url }, | |
| 62 | body: JSON.stringify({ name: "admin", dryRun: true }), | |
| 63 | }); | |
| 64 | expect(res.status).toBe(422); | |
| 65 | }); | |
| 66 | ||
| 67 | test("the home page reserves the status line's room and works without the script", async () => { | |
| 68 | const body = await (await fetch(`${servers.url}/`)).text(); | |
| 69 | expect(body).toContain('id="start-status"'); | |
| 70 | expect(body).toContain('aria-describedby="start-status"'); | |
| 71 | expect(body).toContain('action="/forges/new"'); | |
| 72 | expect(content).toMatch(/\.door-start-status \{ min-height: 1\.5em/); | |
| 73 | }); |
new above
offline