Public: anyone can read this; the crew can change it.
Make my forge: any signed-in person, a name and a size, with a per-account limit #6418
scout/forge:scout/2927-make-my-forge-any-signed-in-person-a-nam → main 9 commits fork
18 files
changed +762
−27
docs/MCP.md +1
| @@ -114,6 +114,7 @@ for a tunnel and a forge that sleeps. | ||
| 114 | 114 | | `repo_settings` | `{ repo, description?, visibility? }` (admin token): change a repository's description and visibility as `PATCH /api/v1/repos/:owner/:name` does, for the same callers (a repository admin); answers `{ repo, description, visibility }`; a visibility other than public or private is `invalid` | yes | |
| 115 | 115 | | `reports` | `{ repo, id?, action? }`: the waiting reports from outsiders, each `from: "outside"` (their words are unchecked); with `id` and `action` (`accept` opens it as an issue marked as from outside, `dismiss` closes it). A person who writes the repository only; needs an `rw` token | yes | |
| 116 | 116 | | `restore_drill` | no input: restore the newest snapshot as a drill and answer `{ verdict, failedStep?, says }`, worded as the Backups page's button; no store, a failure or a red drill is an answer, not an error; a forge admin's, with an admin token | yes | |
| 117 | | `retire_forge` | `{ name }`: retire a child forge you spawned (an admin: any); frees your slot under the per-account limit | yes | | |
| 117 | 118 | | `returns` | `{ repo?, days? }`: what the crew's tokens bought over the last `days` (1–90, default 7): dollars, runs, changes landed, cost per change; `repo` narrows it to one app of yours | | |
| 118 | 119 | | `review` | `{ repo, number, verdict, reason? }`: approve a pull (`approve`) or request changes (`changes`, with a `reason`), as the page does; a writer's or the crew's, and never your own pull | yes | |
| 119 | 120 | | `run_log` | `{ repo, run, tail? }`: a run's log, or its last `tail` lines (1–2000), with the REST route's access: a hidden log or unreadable repository is not found | | |
docs/SPAWN.md +29
| @@ -179,6 +179,35 @@ apps as forks, and the MCP `spawn`. What is missing is built as linked issues: | ||
| 179 | 179 | | #2932 | /boards: forges made, time to live, failures | | |
| 180 | 180 | | #2933 | /forges: an owner's forges side by side, use against size | #2925, #2926 | |
| 181 | 181 | |
| 182 | ## A forge for any signed-in person | |
| 183 | ||
| 184 | `/forges/new`, the terminal's New forge screen, `POST /api/v1/forges` and the | |
| 185 | MCP `spawn` take any signed-in person account, and gate the same way. A | |
| 186 | non-admin gets the short form (page and terminal): a name, a size, public apps | |
| 187 | and optionally a fresh app; it says "live in under 3 minutes" and the account's | |
| 188 | limit. A private app is "not a known app". Crew, a model key, a base URL, a | |
| 189 | budget and a direction stay with admins; the door refuses them from anyone | |
| 190 | else (422). | |
| 191 | ||
| 192 | Limits, since sign-up is open and a per-account cap alone does not bound | |
| 193 | spawns: a non-admin may own at most `FORGE_FORGES_PER_ACCOUNT` children | |
| 194 | (default 3, 1–1000), counting those done or begun in the last hour, so an | |
| 195 | abandoned failed spawn frees its slot after an hour. A live child is freed by | |
| 196 | retiring it: `DELETE /api/v1/forges/:name` or the MCP `retire_forge` (its owner, | |
| 197 | or an admin) takes its body and ledger line and frees the name. There is no | |
| 198 | page or terminal button for it yet. Children with no owner on record (a | |
| 199 | stranger's, or made before the limit) are not counted; only an admin can | |
| 200 | retire them. The slot is reserved | |
| 201 | before placement, so parallel requests cannot overrun it. Over it is a 409 | |
| 202 | `refused` with one sentence. Non-admin spawns are also limited to 5 a day per | |
| 203 | client address and 60 a day for the forge (429), counted under the same | |
| 204 | table as the anonymous limit; the count is taken before the spawn starts, so | |
| 205 | parallel requests cannot overrun it, and given back if no new child results. Admins are exempt from all three. Agent accounts | |
| 206 | are refused (403). The slot reservation is per process, which is how the forge runs; after a | |
| 207 | restart the ledger alone is counted, so a spawn in flight at the restart is | |
| 208 | counted for an hour, as a begun one (the rate counts are in the database and | |
| 209 | survive it). | |
| 210 | ||
| 182 | 211 | ## Child quota |
| 183 | 212 | |
| 184 | 213 | `FORGE_CHILD_CPUS`, `FORGE_CHILD_MEMORY_MIB` and `FORGE_CHILD_DISK_MIB` declare |
src/actions/spawn.ts +63 −1
| @@ -12,7 +12,7 @@ | ||
| 12 | 12 | * idempotent — and a finished one is answered, not made twice. |
| 13 | 13 | */ |
| 14 | 14 | |
| 15 | import { mkdir, readdir, rename } from "node:fs/promises"; | |
| 15 | import { mkdir, readdir, rename, unlink } from "node:fs/promises"; | |
| 16 | 16 | import { join } from "node:path"; |
| 17 | 17 | import { createApp, type App } from "../app.ts"; |
| 18 | 18 | import { |
| @@ -53,6 +53,7 @@ import { loadKey, open } from "../kernel/secret.ts"; | ||
| 53 | 53 | import { loadOrCreateHostKey } from "../ssh/keys.ts"; |
| 54 | 54 | import { claimLink, hasAdmin, mintClaim, spawnedOrg } from "../spawn/claim.ts"; |
| 55 | 55 | import { childPublicUrl, parentOrigin, type Placement, type PlacementKind } from "../spawn/placement.ts"; |
| 56 | import { releaseSlot, reserveSlot } from "../spawn/account.ts"; | |
| 56 | 57 | import { quotaRefusal } from "../spawn/quota.ts"; |
| 57 | 58 | import { planSpawn, type SpawnInput, type SpawnPlan, type SpawnSource } from "../spawn/plan.ts"; |
| 58 | 59 | import { DEFAULT_SIZE, isSize, type Size } from "../spawn/sizes.ts"; |
| @@ -110,6 +111,8 @@ interface Ledger { | ||
| 110 | 111 | done: boolean; |
| 111 | 112 | /** For the client list (#776); absent on a ledger written before it. */ |
| 112 | 113 | readonly forWhom?: string; |
| 114 | /** The signed-in person who made it (the per-account limit counts these). */ | |
| 115 | readonly ownerId?: string; | |
| 113 | 116 | readonly apps?: readonly string[]; |
| 114 | 117 | readonly createdAt?: string; |
| 115 | 118 | /** Absent on a ledger written before sizes; reads as small. */ |
| @@ -223,6 +226,31 @@ async function make( | ||
| 223 | 226 | owner: User | undefined, |
| 224 | 227 | plan: SpawnPlan, |
| 225 | 228 | deps: SpawnDeps, |
| 229 | ): Promise<Result<Spawned, SpawnError>> { | |
| 230 | // The owner's slot is held from the check until the ledger line names them. | |
| 231 | let held = false; | |
| 232 | const free = (): void => { | |
| 233 | if (held && owner) releaseSlot(owner); | |
| 234 | held = false; | |
| 235 | }; | |
| 236 | try { | |
| 237 | return await makeHolding(app, owner, plan, deps, { | |
| 238 | hold: () => { | |
| 239 | held = true; | |
| 240 | }, | |
| 241 | free, | |
| 242 | }); | |
| 243 | } finally { | |
| 244 | free(); | |
| 245 | } | |
| 246 | } | |
| 247 | ||
| 248 | async function makeHolding( | |
| 249 | app: App, | |
| 250 | owner: User | undefined, | |
| 251 | plan: SpawnPlan, | |
| 252 | deps: SpawnDeps, | |
| 253 | slot: { hold(): void; free(): void }, | |
| 226 | 254 | ): Promise<Result<Spawned, SpawnError>> { |
| 227 | 255 | const planHash = sha256(JSON.stringify(plan)); |
| 228 | 256 | let ledger = await readLedger(app, plan.name); |
| @@ -249,6 +277,11 @@ async function make( | ||
| 249 | 277 | // A retry of an unfinished child is a new attempt: its failure has a start. |
| 250 | 278 | app.bus.append({ kind: "forge.spawn.started", payload: { ...(deps.drill === true ? { drill: 1 } : {}) } }); |
| 251 | 279 | } else { |
| 280 | if (owner !== undefined) { | |
| 281 | const taken = reserveSlot(app.config.dataDir, owner); | |
| 282 | if (!taken.ok) return err({ kind: "refused", reason: "account", message: taken.message }); | |
| 283 | if (!owner.isAdmin) slot.hold(); | |
| 284 | } | |
| 252 | 285 | const over = quotaRefusal(app.config.dataDir, plan.size ?? DEFAULT_SIZE); |
| 253 | 286 | if (over !== undefined) return err({ kind: "refused", reason: "quota", message: over }); |
| 254 | 287 | const placed = await deps.placement.place(plan); |
| @@ -264,6 +297,7 @@ async function make( | ||
| 264 | 297 | planHash, |
| 265 | 298 | done: false, |
| 266 | 299 | ...(plan.forWhom === undefined ? {} : { forWhom: plan.forWhom }), |
| 300 | ...(owner === undefined ? {} : { ownerId: owner.id }), | |
| 267 | 301 | apps: plan.apps.map((a) => a.ref), |
| 268 | 302 | createdAt: new Date().toISOString(), |
| 269 | 303 | size: plan.size ?? DEFAULT_SIZE, |
| @@ -272,6 +306,7 @@ async function make( | ||
| 272 | 306 | beatKey: secret(), |
| 273 | 307 | }; |
| 274 | 308 | await writeLedger(app, ledger); |
| 309 | slot.free(); | |
| 275 | 310 | app.bus.append({ kind: "forge.spawn.started", payload: { ...(deps.drill === true ? { drill: 1 } : {}) } }); |
| 276 | 311 | } |
| 277 | 312 | |
| @@ -651,6 +686,33 @@ export async function reissueClaim( | ||
| 651 | 686 | } |
| 652 | 687 | } |
| 653 | 688 | |
| 689 | /** | |
| 690 | * Retire a child: its owner (or an admin) takes its body away and its ledger | |
| 691 | * line with it, which frees the owner's slot and the name. The ledger is the | |
| 692 | * only record of who made it, so a child with no owner (a stranger's, or from | |
| 693 | * before the per-account limit) is an admin's to retire. | |
| 694 | */ | |
| 695 | export async function retireForge( | |
| 696 | app: App, | |
| 697 | actor: User | undefined, | |
| 698 | name: string, | |
| 699 | deps: { readonly request: Request; readonly url: URL; readonly placement: Placement }, | |
| 700 | ): Promise<Result<{ readonly name: string }, SpawnError | { readonly kind: "not_found" }>> { | |
| 701 | if (crossOrigin(deps.request, deps.url, app.log)) return err({ kind: "cross_origin" }); | |
| 702 | const user = requireUser(actor); | |
| 703 | if (!user.ok) return err({ kind: "unauthorized" }); | |
| 704 | if (user.value.kind !== "user") return err({ kind: "forbidden" }); | |
| 705 | const ledger = /^[a-z0-9-]{1,39}$/.test(name) ? await readLedger(app, name) : undefined; | |
| 706 | // Someone else's child answers as no child at all. | |
| 707 | if (!ledger || !(user.value.isAdmin || ledger.ownerId === user.value.id)) return err({ kind: "not_found" }); | |
| 708 | if (inFlight.has(name)) return err({ kind: "exists", message: "that child is being made" }); | |
| 709 | const gone = await deps.placement.retire(ledger.bodyId); | |
| 710 | if (!gone.ok) return fail(gone.error.message); | |
| 711 | await unlink(ledgerPath(app, name)).catch(() => undefined); | |
| 712 | app.bus.append({ kind: "forge.spawn.retired", actorId: user.value.id, payload: { name } }); | |
| 713 | return ok({ name }); | |
| 714 | } | |
| 715 | ||
| 654 | 716 | /** What this forge shows a child it made to learn its private facts; undefined for an older child. */ |
| 655 | 717 | export async function statusTokenOf(app: App, name: string): Promise<string | undefined> { |
| 656 | 718 | return (await readLedger(app, name))?.statusToken; |
src/contract/openapi/routes/deleteApiV1ForgesName.ts added +11
| @@ -0,0 +1,11 @@ | ||
| 1 | import type { Route } from "../shared.ts"; | |
| 2 | ||
| 3 | export const route: Route = { | |
| 4 | method: "delete", | |
| 5 | path: "/api/v1/forges/{name}", | |
| 6 | summary: | |
| 7 | "Retire a child forge you spawned (a site admin: any; rw scope): its body and ledger line go, so the name and your slot under the per-account limit are free again; answers 200 with { retired, name }", | |
| 8 | auth: true, | |
| 9 | status: 200, | |
| 10 | refusals: { not_found: "no child forge of yours has this name" }, | |
| 11 | }; |
src/contract/openapi/routes/index.ts +1
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | // Generated by `bun scripts/openapi-routes.ts` from the files beside it; do not edit. |
| 2 | 2 | // One line per route, sorted: a new route is a new file and one new line here. |
| 3 | 3 | export { route as deleteApiV1AdminAccountsUsername } from "./deleteApiV1AdminAccountsUsername.ts"; |
| 4 | export { route as deleteApiV1ForgesName } from "./deleteApiV1ForgesName.ts"; | |
| 4 | 5 | export { route as deleteApiV1AdminInvitesId } from "./deleteApiV1AdminInvitesId.ts"; |
| 5 | 6 | export { route as deleteApiV1AgentsUsername } from "./deleteApiV1AgentsUsername.ts"; |
| 6 | 7 | export { route as deleteApiV1BoardsSlug } from "./deleteApiV1BoardsSlug.ts"; |
src/core/event_audience.ts +1
| @@ -83,6 +83,7 @@ const READER_KINDS: ReadonlySet<string> = new Set([ | ||
| 83 | 83 | "forge.spawn.started", |
| 84 | 84 | "forge.spawned", |
| 85 | 85 | "forge.claim_reissued", |
| 86 | "forge.spawn.retired", | |
| 86 | 87 | "agent.created", |
| 87 | 88 | "agent.updated", |
| 88 | 89 | "agent.deleted", |
src/http/api.ts +9 −1
| @@ -91,7 +91,7 @@ import { listChildren } from "../actions/spawn.ts"; | ||
| 91 | 91 | import { forgeState, PARENT_HEADER } from "../spawn/clients.ts"; |
| 92 | 92 | import { familyView } from "../spawn/family.ts"; |
| 93 | 93 | import { receiveBeat, SIGNATURE_HEADER } from "../spawn/heartbeat.ts"; |
| 94 | import { reissueDoor, spawnDoor } from "../spawn/door.ts"; | |
| 94 | import { reissueDoor, retireDoor, spawnDoor } from "../spawn/door.ts"; | |
| 95 | 95 | import { listSecrets, putSecret, removeSecret } from "../actions/secrets.ts"; |
| 96 | 96 | import { forgeProposals, proposeToParentForge, proposeUpstream } from "../actions/upstream.ts"; |
| 97 | 97 | import { FOLLOW_MODE_REFUSALS, setFollowMode, severForge, takeForgeCode, takeForgeWords, takeOrTell } from "../actions/follow.ts"; |
| @@ -5535,6 +5535,14 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 5535 | 5535 | return json(door.body, door.status); |
| 5536 | 5536 | }); |
| 5537 | 5537 | |
| 5538 | // Retire a child you made (an admin: any): frees your slot under the per-account limit. | |
| 5539 | router.add("DELETE", "/api/v1/forges/:name", async (ctx) => { | |
| 5540 | const denied = requireScope(ctx, "rw"); | |
| 5541 | if (denied) return denied; | |
| 5542 | const door = await retireDoor(ctx, ctx.auth.user!, ctx.params["name"] ?? ""); | |
| 5543 | return json(door.body, door.status); | |
| 5544 | }); | |
| 5545 | ||
| 5538 | 5546 | // A new claim link for a child nobody has claimed (#729); the old one dies. |
| 5539 | 5547 | router.add("POST", "/api/v1/forges/:name/claim-link", async (ctx) => { |
| 5540 | 5548 | const denied = requireScope(ctx, "rw"); |
src/http/mcp/tools/index.ts +1
| @@ -94,6 +94,7 @@ export { tool as repo_labels } from "./repo_labels.ts"; | ||
| 94 | 94 | export { tool as repo_settings } from "./repo_settings.ts"; |
| 95 | 95 | export { tool as reports } from "./reports.ts"; |
| 96 | 96 | export { tool as restore_drill } from "./restore_drill.ts"; |
| 97 | export { tool as retire_forge } from "./retire_forge.ts"; | |
| 97 | 98 | export { tool as returns } from "./returns.ts"; |
| 98 | 99 | export { tool as review } from "./review.ts"; |
| 99 | 100 | export { tool as run_log } from "./run_log.ts"; |
src/http/mcp/tools/retire_forge.ts added +20
| @@ -0,0 +1,20 @@ | ||
| 1 | import { retireDoor } from "../../../spawn/door.ts"; | |
| 2 | import { good, bad, type Tool } from "../shared.ts"; | |
| 3 | ||
| 4 | export const tool: Tool = { | |
| 5 | name: "retire_forge", | |
| 6 | doc: "`{ name }`: retire a child forge you spawned (an admin: any); frees your slot under the per-account limit", | |
| 7 | description: | |
| 8 | "Retire a forge you spawned (admin: any); frees your slot.", | |
| 9 | inputSchema: { | |
| 10 | type: "object", | |
| 11 | properties: { name: { type: "string" } }, | |
| 12 | required: ["name"], | |
| 13 | }, | |
| 14 | writes: true, | |
| 15 | async run(ctx, user, args) { | |
| 16 | const door = await retireDoor(ctx, user, typeof args["name"] === "string" ? args["name"] : ""); | |
| 17 | if (door.status >= 400) return bad(String(door.body["error"]), String(door.body["message"])); | |
| 18 | return good({ says: `Retired ${String(door.body["name"])}.`, ...door.body }); | |
| 19 | }, | |
| 20 | }; |
src/spawn/account.ts added +80
| @@ -0,0 +1,80 @@ | ||
| 1 | /** | |
| 2 | * The per-account limit: how many children one signed-in person may own. | |
| 3 | * `FORGE_FORGES_PER_ACCOUNT` (default 3) counts the ledger lines that name | |
| 4 | * the person as owner that are live (done) or still in progress (begun in | |
| 5 | * the last hour). A spawn that failed and was abandoned stops counting after | |
| 6 | * that hour, so it never locks a person out; a live one is freed by retiring | |
| 7 | * it (`retireForge`), which removes its ledger line. Children made before the | |
| 8 | * limit carry no owner and are not counted (a stranger's, or an admin's, who is exempt). A slot is reserved in memory | |
| 9 | * between the check and the ledger write, so parallel requests cannot both | |
| 10 | * pass at one below the limit. Admins are not counted: they have | |
| 11 | * the longer form and the host's own room (`capacity.ts`) to answer to. | |
| 12 | */ | |
| 13 | ||
| 14 | import { readdirSync, readFileSync } from "node:fs"; | |
| 15 | import { join } from "node:path"; | |
| 16 | import type { User } from "../core/users.ts"; | |
| 17 | ||
| 18 | export const DEFAULT_FORGES_PER_ACCOUNT = 3; | |
| 19 | ||
| 20 | /** The limit: a whole number from 1 to 1000 in the variable, else the default. */ | |
| 21 | export function forgesPerAccount(env: Record<string, string | undefined> = process.env): number { | |
| 22 | const v = env["FORGE_FORGES_PER_ACCOUNT"]; | |
| 23 | if (v === undefined || !/^[0-9]{1,4}$/.test(v)) return DEFAULT_FORGES_PER_ACCOUNT; | |
| 24 | const n = Number(v); | |
| 25 | return n >= 1 && n <= 1000 ? n : DEFAULT_FORGES_PER_ACCOUNT; | |
| 26 | } | |
| 27 | ||
| 28 | const IN_PROGRESS_MS = 3_600_000; | |
| 29 | const reserved = new Map<string, number>(); | |
| 30 | ||
| 31 | /** How many children the ledger says this person owns, live or in progress. */ | |
| 32 | export function ownedBy(dataDir: string, ownerId: string, now = Date.now()): number { | |
| 33 | let files: string[] = []; | |
| 34 | try { | |
| 35 | files = readdirSync(join(dataDir, "spawn")); | |
| 36 | } catch { | |
| 37 | return 0; | |
| 38 | } | |
| 39 | let n = 0; | |
| 40 | for (const f of files) { | |
| 41 | if (!f.endsWith(".json")) continue; | |
| 42 | try { | |
| 43 | const l = JSON.parse(readFileSync(join(dataDir, "spawn", f), "utf8")) as { | |
| 44 | ownerId?: unknown; | |
| 45 | done?: unknown; | |
| 46 | createdAt?: unknown; | |
| 47 | }; | |
| 48 | if (l.ownerId !== ownerId) continue; | |
| 49 | const begun = typeof l.createdAt === "string" ? Date.parse(l.createdAt) : 0; | |
| 50 | if (l.done === true || now - begun < IN_PROGRESS_MS) n++; | |
| 51 | } catch { | |
| 52 | // A corrupt ledger counts for no one. | |
| 53 | } | |
| 54 | } | |
| 55 | return n; | |
| 56 | } | |
| 57 | ||
| 58 | /** | |
| 59 | * Reserve one of this person's slots, or answer the sentence that refuses. | |
| 60 | * Check and reservation happen with no await between, so they are atomic in | |
| 61 | * this process; `release` once the ledger line is written (or the spawn fails). | |
| 62 | */ | |
| 63 | export function reserveSlot(dataDir: string, user: User): { ok: true } | { ok: false; message: string } { | |
| 64 | if (user.isAdmin) return { ok: true }; | |
| 65 | const limit = forgesPerAccount(); | |
| 66 | if (ownedBy(dataDir, user.id) + (reserved.get(user.id) ?? 0) >= limit) | |
| 67 | return { | |
| 68 | ok: false, | |
| 69 | message: `you already own ${limit} forge${limit === 1 ? "" : "s"}, the most one account may make here`, | |
| 70 | }; | |
| 71 | reserved.set(user.id, (reserved.get(user.id) ?? 0) + 1); | |
| 72 | return { ok: true }; | |
| 73 | } | |
| 74 | ||
| 75 | export function releaseSlot(user: User): void { | |
| 76 | if (user.isAdmin) return; | |
| 77 | const n = (reserved.get(user.id) ?? 0) - 1; | |
| 78 | if (n > 0) reserved.set(user.id, n); | |
| 79 | else reserved.delete(user.id); | |
| 80 | } |
src/spawn/door.ts +43 −3
| @@ -7,7 +7,7 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | import { startWatch, probeOf, type Probe } from "./live.ts"; |
| 10 | import { reissueClaim, spawnForge, type SpawnError } from "../actions/spawn.ts"; | |
| 10 | import { reissueClaim, retireForge, spawnForge, type SpawnError } from "../actions/spawn.ts"; | |
| 11 | 11 | import type { App } from "../app.ts"; |
| 12 | 12 | import { maxChildren } from "../core/capacity.ts"; |
| 13 | 13 | import { getAgentByName } from "../core/agents.ts"; |
| @@ -30,7 +30,7 @@ import { dryRun, planSpawn, KEY_BUDGET_NOTE, MAX_APPS, type CatalogApp, type Cre | ||
| 30 | 30 | import { bootHostedChild } from "./host.ts"; |
| 31 | 31 | import { cellPlacement, ownsBody } from "./cell.ts"; |
| 32 | 32 | import { cellsAvailable } from "../apps/contain.ts"; |
| 33 | import { ANON_SPAWNS_PER_DAY, checkAnonSpawn, noteAnonSpawn } from "./rate.ts"; | |
| 33 | import { ACCOUNT_SPAWNS_PER_ADDRESS_PER_DAY, ANON_SPAWNS_PER_DAY, checkAccountSpawn, checkAnonSpawn, noteAccountSpawn, noteAnonSpawn, unnoteSpawn } from "./rate.ts"; | |
| 34 | 34 | import { readdirSync, readFileSync } from "node:fs"; |
| 35 | 35 | import { readdir, stat } from "node:fs/promises"; |
| 36 | 36 | import { join } from "node:path"; |
| @@ -192,6 +192,14 @@ export async function spawnDoor( | ||
| 192 | 192 | if (!Array.isArray(apps)) return apps; |
| 193 | 193 | const crew = names(raw["crew"], "crew", MAX_CREW); |
| 194 | 194 | if (!Array.isArray(crew)) return crew; |
| 195 | // The short door: a person who is not an admin names a forge, a size and | |
| 196 | // public apps. Crew, model key and direction are the admin's longer form. | |
| 197 | if (!anon && !user.isAdmin) | |
| 198 | for (const f of ["crew", "modelKey", "modelEndpoint", "budgetUsd", "direction"] as const) { | |
| 199 | const v = raw[f]; | |
| 200 | if (v !== undefined && !(Array.isArray(v) && v.length === 0) && v !== "") | |
| 201 | return invalid(f, "only an admin sets this; a forge of your own takes a name, a size and public apps"); | |
| 202 | } | |
| 195 | 203 | if (anon && crew.length > 0) |
| 196 | 204 | return invalid("crew", "anonymous spawns carry no crew; claim the forge and invite one there"); |
| 197 | 205 | // A stranger has no key to seal and no account to keep: the key is |
| @@ -298,6 +306,26 @@ export async function spawnDoor( | ||
| 298 | 306 | }, |
| 299 | 307 | }; |
| 300 | 308 | } |
| 309 | // Sign-up is open, so a non-admin's spawns are also held to a few a day per | |
| 310 | // client address (and for the forge): the per-account limit alone is not a bound. | |
| 311 | if (!anon && !user.isAdmin) { | |
| 312 | const limit = checkAccountSpawn(ctx.app.db, ctx.request); | |
| 313 | if (!limit.allowed) | |
| 314 | return { | |
| 315 | status: 429, | |
| 316 | body: { | |
| 317 | error: "rate_limited", | |
| 318 | message: `new forges are limited to ${ACCOUNT_SPAWNS_PER_ADDRESS_PER_DAY} a day per address; try again after ${limit.resetsAt}`, | |
| 319 | }, | |
| 320 | }; | |
| 321 | } | |
| 322 | // The count is taken now, with no await since the check, so parallel requests | |
| 323 | // cannot all pass at one below the cap; it is given back if no child results. | |
| 324 | const counted = anon | |
| 325 | ? noteAnonSpawn(ctx.app.db, ctx.request) | |
| 326 | : user.isAdmin | |
| 327 | ? undefined | |
| 328 | : noteAccountSpawn(ctx.app.db, ctx.request); | |
| 301 | 329 | const startedAt = Date.now(); |
| 302 | 330 | const made = await spawnForge(ctx.app, user, input, source, { |
| 303 | 331 | placement: placementFor(ctx.app), |
| @@ -306,8 +334,8 @@ export async function spawnDoor( | ||
| 306 | 334 | ...(typeof modelKey === "string" ? { modelKey } : {}), |
| 307 | 335 | ...(modelEndpoint === undefined ? {} : { modelEndpoint }), |
| 308 | 336 | }); |
| 337 | if (counted !== undefined && (!made.ok || made.value.existed)) unnoteSpawn(ctx.app.db, counted); | |
| 309 | 338 | if (!made.ok) return spawnWords(made.error); |
| 310 | if (anon && !made.value.existed) noteAnonSpawn(ctx.app.db, ctx.request); | |
| 311 | 339 | // Its apps come up now, so the first visit after a claim finds them awake. |
| 312 | 340 | if (!made.value.existed) { |
| 313 | 341 | void bootHostedChild(ctx.app, made.value.name); |
| @@ -324,6 +352,18 @@ export async function spawnDoor( | ||
| 324 | 352 | } |
| 325 | 353 | |
| 326 | 354 | /** A new claim link for an unclaimed child (#729): the old one dies. */ |
| 355 | export async function retireDoor(ctx: Ctx, user: User, name: string): Promise<Door> { | |
| 356 | const gone = await retireForge(ctx.app, user, name, { | |
| 357 | request: ctx.request, | |
| 358 | url: ctx.url, | |
| 359 | placement: placementFor(ctx.app), | |
| 360 | }); | |
| 361 | if (gone.ok) return { status: 200, body: { retired: true, name: gone.value.name } }; | |
| 362 | if (gone.error.kind === "not_found") | |
| 363 | return { status: 404, body: { error: "not_found", message: `no child forge of yours named ${name}` } }; | |
| 364 | return spawnWords(gone.error); | |
| 365 | } | |
| 366 | ||
| 327 | 367 | export async function reissueDoor(ctx: Ctx, user: User, name: string): Promise<Door> { |
| 328 | 368 | const made = await reissueClaim(ctx.app, user, name, { request: ctx.request, url: ctx.url }); |
| 329 | 369 | if (made.ok) return { status: 201, body: { ...made.value } }; |
src/spawn/rate.ts +38 −3
| @@ -29,7 +29,7 @@ export function checkAnonSpawn(db: Database, request: Request, now = Date.now()) | ||
| 29 | 29 | const mine = db |
| 30 | 30 | .query("SELECT COUNT(*) AS n, MIN(at) AS oldest FROM anon_spawn WHERE bucket = ?") |
| 31 | 31 | .get(clientBucket(request)) as { n: number; oldest: number | null }; |
| 32 | const all = db.query("SELECT COUNT(*) AS n, MIN(at) AS oldest FROM anon_spawn").get() as { | |
| 32 | const all = db.query("SELECT COUNT(*) AS n, MIN(at) AS oldest FROM anon_spawn WHERE bucket NOT LIKE 'acct:%'").get() as { | |
| 33 | 33 | n: number; |
| 34 | 34 | oldest: number | null; |
| 35 | 35 | }; |
| @@ -40,11 +40,46 @@ export function checkAnonSpawn(db: Database, request: Request, now = Date.now()) | ||
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | /** Count an anonymous spawn against this request's client. */ |
| 43 | export function noteAnonSpawn(db: Database, request: Request, now = Date.now()): void { | |
| 44 | db.query("INSERT INTO anon_spawn (bucket, at) VALUES (?, ?)").run(clientBucket(request), now); | |
| 43 | export function noteAnonSpawn(db: Database, request: Request, now = Date.now()): number { | |
| 44 | return Number(db.query("INSERT INTO anon_spawn (bucket, at) VALUES (?, ?)").run(clientBucket(request), now).lastInsertRowid); | |
| 45 | } | |
| 46 | ||
| 47 | /** Give back a count noted ahead of a spawn that did not make a new child. */ | |
| 48 | export function unnoteSpawn(db: Database, id: number): void { | |
| 49 | db.query("DELETE FROM anon_spawn WHERE rowid = ?").run(id); | |
| 45 | 50 | } |
| 46 | 51 | |
| 47 | 52 | /** Forget every counted spawn (tests). */ |
| 48 | 53 | export function resetAnonSpawns(db: Database): void { |
| 49 | 54 | db.exec("DELETE FROM anon_spawn"); |
| 50 | 55 | } |
| 56 | ||
| 57 | /** Spawns allowed per client address in any 24 hours, by non-admin accounts together. */ | |
| 58 | export const ACCOUNT_SPAWNS_PER_ADDRESS_PER_DAY = 5; | |
| 59 | /** Spawns allowed from all non-admin accounts together in any 24 hours. */ | |
| 60 | export const ACCOUNT_SPAWNS_GLOBAL_PER_DAY = 60; | |
| 61 | ||
| 62 | /** | |
| 63 | * Sign-up is open, so the per-account limit alone does not bound spawns: a | |
| 64 | * non-admin's spawns are also counted per client address and for the forge. | |
| 65 | * Stored beside the anonymous counts, under an `acct:` bucket. | |
| 66 | */ | |
| 67 | export function checkAccountSpawn(db: Database, request: Request, now = Date.now()): RateLimit { | |
| 68 | const since = now - DAY_MS; | |
| 69 | db.query("DELETE FROM anon_spawn WHERE at <= ?").run(since); | |
| 70 | const mine = db | |
| 71 | .query("SELECT COUNT(*) AS n, MIN(at) AS oldest FROM anon_spawn WHERE bucket = ?") | |
| 72 | .get(`acct:${clientBucket(request)}`) as { n: number; oldest: number | null }; | |
| 73 | const all = db | |
| 74 | .query("SELECT COUNT(*) AS n, MIN(at) AS oldest FROM anon_spawn WHERE bucket LIKE 'acct:%'") | |
| 75 | .get() as { n: number; oldest: number | null }; | |
| 76 | const clientFull = mine.n >= ACCOUNT_SPAWNS_PER_ADDRESS_PER_DAY; | |
| 77 | const globalFull = all.n >= ACCOUNT_SPAWNS_GLOBAL_PER_DAY; | |
| 78 | const oldest = (clientFull ? mine.oldest : globalFull ? all.oldest : null) ?? now; | |
| 79 | return { allowed: !clientFull && !globalFull, resetsAt: new Date(oldest + DAY_MS).toISOString() }; | |
| 80 | } | |
| 81 | ||
| 82 | /** Count a non-admin account's spawn against this request's client. */ | |
| 83 | export function noteAccountSpawn(db: Database, request: Request, now = Date.now()): number { | |
| 84 | return Number(db.query("INSERT INTO anon_spawn (bucket, at) VALUES (?, ?)").run(`acct:${clientBucket(request)}`, now).lastInsertRowid); | |
| 85 | } |
src/tui/screens/newforge.ts +28 −9
| @@ -9,6 +9,8 @@ | ||
| 9 | 9 | |
| 10 | 10 | import { DEFAULT_SIZE, SIZES, type Size } from "../../spawn/sizes.ts"; |
| 11 | 11 | import { childUrlOf, spawnDoor } from "../../spawn/door.ts"; |
| 12 | import { forgesPerAccount } from "../../spawn/account.ts"; | |
| 13 | import { notePeer } from "../../http/peer.ts"; | |
| 12 | 14 | import { quotaFull } from "../../spawn/quota.ts"; |
| 13 | 15 | import { MAX_BUDGET_USD, MIN_BUDGET_USD } from "../../spawn/plan.ts"; |
| 14 | 16 | import type { Ctx, Screen } from "../app/app.ts"; |
| @@ -28,9 +30,12 @@ interface Spec { | ||
| 28 | 30 | readonly choices?: readonly string[]; |
| 29 | 31 | } |
| 30 | 32 | |
| 33 | /** The time budget, as the page promises it. */ | |
| 34 | const LIVE_MINUTES = 3; | |
| 35 | ||
| 31 | 36 | const SIZE_CHOICES = Object.keys(SIZES) as Size[]; |
| 32 | 37 | |
| 33 | const SPECS: readonly Spec[] = [ | |
| 38 | const ALL_SPECS: readonly Spec[] = [ | |
| 34 | 39 | { key: "name", label: "Name", hint: "Lowercase letters, digits and dashes." }, |
| 35 | 40 | { key: "for", label: "For whom", hint: "A person or client, for your list of forges sent. Optional." }, |
| 36 | 41 | { key: "direction", label: "What it is for", hint: "The crew's first direction. Optional." }, |
| @@ -43,6 +48,9 @@ const SPECS: readonly Spec[] = [ | ||
| 43 | 48 | { key: "size", label: "Size", hint: "small (default), medium or large: left and right choose, or type it.", choices: SIZE_CHOICES }, |
| 44 | 49 | ]; |
| 45 | 50 | |
| 51 | /** The short door's fields: a person who is not an admin names a forge, public apps and a size. */ | |
| 52 | const SHORT_KEYS = ["name", "apps", "fresh", "size"]; | |
| 53 | ||
| 46 | 54 | /** The door's "field: reason" refusal, as the field it names and the reason. */ |
| 47 | 55 | function refusal(body: Record<string, unknown>): { key?: string; text: string } { |
| 48 | 56 | const text = String(body["message"] ?? "refused"); |
| @@ -51,6 +59,12 @@ function refusal(body: Record<string, unknown>): { key?: string; text: string } | ||
| 51 | 59 | } |
| 52 | 60 | |
| 53 | 61 | export function newForgeScreen(f: Session, ctx: Ctx): Screen { |
| 62 | // The page gates the same way: only an admin sees the longer form. | |
| 63 | const short = f.user !== undefined && !f.user.isAdmin; | |
| 64 | const SPECS = short ? ALL_SPECS.filter((s) => SHORT_KEYS.includes(s.key)) : ALL_SPECS; | |
| 65 | const budget = short | |
| 66 | ? `Live in under ${LIVE_MINUTES} minutes. You can own up to ${forgesPerAccount()} forges.` | |
| 67 | : undefined; | |
| 54 | 68 | const values: Record<string, string> = {}; |
| 55 | 69 | let focus = 0; |
| 56 | 70 | let errorKey: string | undefined; |
| @@ -68,12 +82,12 @@ export function newForgeScreen(f: Session, ctx: Ctx): Screen { | ||
| 68 | 82 | name: val("name"), |
| 69 | 83 | apps, |
| 70 | 84 | ...(val("fresh") === "" ? {} : { fresh: val("fresh") }), |
| 71 | followMode: val("followMode") === "" ? "tell-me" : val("followMode"), | |
| 72 | ...(val("for") === "" ? {} : { for: val("for") }), | |
| 73 | ...(val("direction") === "" ? {} : { direction: val("direction") }), | |
| 74 | ...(val("modelKey") === "" ? {} : { modelKey: val("modelKey") }), | |
| 75 | ...(val("modelEndpoint") === "" ? {} : { modelEndpoint: val("modelEndpoint") }), | |
| 76 | ...(val("budgetUsd") === "" ? {} : { budgetUsd: Number(val("budgetUsd")) }), | |
| 85 | followMode: short || val("followMode") === "" ? "tell-me" : val("followMode"), | |
| 86 | ...(short || val("for") === "" ? {} : { for: val("for") }), | |
| 87 | ...(short || val("direction") === "" ? {} : { direction: val("direction") }), | |
| 88 | ...(short || val("modelKey") === "" ? {} : { modelKey: val("modelKey") }), | |
| 89 | ...(short || val("modelEndpoint") === "" ? {} : { modelEndpoint: val("modelEndpoint") }), | |
| 90 | ...(short || val("budgetUsd") === "" ? {} : { budgetUsd: Number(val("budgetUsd")) }), | |
| 77 | 91 | // Small is the default: it is not recorded, as on the page. |
| 78 | 92 | ...(val("size") === "" || val("size") === DEFAULT_SIZE ? {} : { size: val("size") }), |
| 79 | 93 | dryRun, |
| @@ -90,6 +104,7 @@ export function newForgeScreen(f: Session, ctx: Ctx): Screen { | ||
| 90 | 104 | auth: { user: f.user, scope: undefined, via: "none" as const }, |
| 91 | 105 | theme: undefined, |
| 92 | 106 | }; |
| 107 | notePeer(hctx.request, f.identity.remoteAddress); | |
| 93 | 108 | return spawnDoor(hctx, f.user, request(dryRun)); |
| 94 | 109 | }; |
| 95 | 110 | |
| @@ -160,9 +175,13 @@ export function newForgeScreen(f: Session, ctx: Ctx): Screen { | ||
| 160 | 175 | render(full, s) { |
| 161 | 176 | const c = ctx.theme.c; |
| 162 | 177 | let area = full; |
| 178 | if (budget !== undefined) { | |
| 179 | s.text(full.x, full.y, truncate(budget, full.w), style(c.faint, c.bg), full.x + full.w); | |
| 180 | area = { ...area, y: area.y + 2, h: Math.max(1, area.h - 2) }; | |
| 181 | } | |
| 163 | 182 | if (quota !== undefined) { |
| 164 | s.text(full.x, full.y, truncate(`This forge cannot hold another child: ${quota}.`, full.w), style(c.attn, c.bg), full.x + full.w); | |
| 165 | area = { ...full, y: full.y + 2, h: Math.max(1, full.h - 2) }; | |
| 183 | s.text(area.x, area.y, truncate(`This forge cannot hold another child: ${quota}.`, area.w), style(c.attn, c.bg), area.x + area.w); | |
| 184 | area = { ...area, y: area.y + 2, h: Math.max(1, area.h - 2) }; | |
| 166 | 185 | } |
| 167 | 186 | if (plan !== undefined) { |
| 168 | 187 | s.text(area.x, area.y, truncate("The plan", area.w), style(c.accent, c.bg), area.x + area.w); |
src/web/pages/new-forge.ts +19 −10
| @@ -14,6 +14,7 @@ import { listReposVisibleTo } from "../../core/repos.ts"; | ||
| 14 | 14 | import type { User } from "../../core/users.ts"; |
| 15 | 15 | import type { Ctx } from "../../http/context.ts"; |
| 16 | 16 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; |
| 17 | import { forgesPerAccount } from "../../spawn/account.ts"; | |
| 17 | 18 | import { quotaFull } from "../../spawn/quota.ts"; |
| 18 | 19 | import { childHost, childUrlOf, noRoom, spawnDoor, type Door } from "../../spawn/door.ts"; |
| 19 | 20 | import { |
| @@ -30,6 +31,9 @@ import { html, type Html } from "../html.ts"; | ||
| 30 | 31 | import { page } from "../layout.ts"; |
| 31 | 32 | import { btn, card, field as formField, row, selectField } from "../ui.ts"; |
| 32 | 33 | |
| 34 | /** The time budget the page promises. */ | |
| 35 | const LIVE_MINUTES = 3; | |
| 36 | ||
| 33 | 37 | interface Values { |
| 34 | 38 | readonly name: string; |
| 35 | 39 | readonly forWhom: string; |
| @@ -157,13 +161,18 @@ function formView( | ||
| 157 | 161 | )} |
| 158 | 162 | </div>`; |
| 159 | 163 | const anon = user === undefined; |
| 164 | // A signed-in person who is not an admin gets the short door. | |
| 165 | const short = !anon && !user.isAdmin; | |
| 160 | 166 | return html`<div class="narrow-wide"> |
| 161 | 167 | ${pageHead("New forge", { |
| 162 | sub: anon | |
| 168 | sub: short | |
| 169 | ? `Your own forge, live in under ${LIVE_MINUTES} minutes: a name, a size, and the public apps to start from.` | |
| 170 | : anon | |
| 163 | 171 | ? "Your own forge: pick a name, and the public apps to start from. Whoever opens the claim link becomes its admin." |
| 164 | 172 | : "Your own forge, or one for someone else: the apps you choose, a crew, and one link to claim.", |
| 165 | 173 | actions: btn("Forges you sent", { href: "/forges", size: "sm" }), |
| 166 | 174 | })} |
| 175 | ${short ? html`<p class="muted small" id="live-budget">Live in under ${LIVE_MINUTES} minutes. You can own up to ${forgesPerAccount()} forges.</p>` : null} | |
| 167 | 176 | ${quota ? html`<p role="status" id="quota-full">This forge cannot hold another child: ${quota}.</p>` : null} |
| 168 | 177 | ${errors["form"] |
| 169 | 178 | ? html`<p class="error mb" role="alert">${errors["form"]}</p>` |
| @@ -180,7 +189,7 @@ function formView( | ||
| 180 | 189 | hint: `Its address: <name>--${childHost(ctx.app)}. Lowercase letters, digits and dashes.`, |
| 181 | 190 | ...(errors["name"] ? { error: errors["name"] } : {}), |
| 182 | 191 | })} |
| 183 | ${anon | |
| 192 | ${anon || short | |
| 184 | 193 | ? null |
| 185 | 194 | : formField({ |
| 186 | 195 | label: "For whom", |
| @@ -189,7 +198,7 @@ function formView( | ||
| 189 | 198 | hint: `A person, a client or an organisation, for your list of forges sent (at most ${MAX_FOR_CHARS} characters). Never copied into the child.`, |
| 190 | 199 | ...(errors["for"] ? { error: errors["for"] } : {}), |
| 191 | 200 | })} |
| 192 | ${formField({ | |
| 201 | ${short ? null : formField({ | |
| 193 | 202 | label: "What it is for", |
| 194 | 203 | name: "direction", |
| 195 | 204 | type: "textarea", |
| @@ -219,7 +228,7 @@ function formView( | ||
| 219 | 228 | hint: `One line on what it is; the child gets a small app to start from (at most ${MAX_FRESH_CHARS} characters).`, |
| 220 | 229 | ...(errors["fresh"] ? { error: errors["fresh"] } : {}), |
| 221 | 230 | })} |
| 222 | <fieldset class="field" ${errors["followMode"] ? html`data-invalid` : null}> | |
| 231 | ${short ? null : html`<fieldset class="field" ${errors["followMode"] ? html`data-invalid` : null}> | |
| 223 | 232 | <legend>How it follows this forge's code (recorded for now; nothing acts on it yet)</legend> |
| 224 | 233 | <label class="check"> |
| 225 | 234 | <input |
| @@ -246,8 +255,8 @@ function formView( | ||
| 246 | 255 | ${errors["followMode"] |
| 247 | 256 | ? html`<span class="error" id="f-followMode-err">${errors["followMode"]}</span>` |
| 248 | 257 | : null} |
| 249 | </fieldset> | |
| 250 | ${crew.length === 0 | |
| 258 | </fieldset>`} | |
| 259 | ${crew.length === 0 || short | |
| 251 | 260 | ? null |
| 252 | 261 | : html`<fieldset class="field"> |
| 253 | 262 | <legend>Crew whose briefs go along</legend> |
| @@ -265,7 +274,7 @@ function formView( | ||
| 265 | 274 | >` |
| 266 | 275 | : null} |
| 267 | 276 | </fieldset>`} |
| 268 | ${anon | |
| 277 | ${anon || short | |
| 269 | 278 | ? null |
| 270 | 279 | : html`<fieldset class="field"> |
| 271 | 280 | <legend>After it is claimed</legend> |
| @@ -278,7 +287,7 @@ function formView( | ||
| 278 | 287 | <span>Keep your owner access (the claim page tells them)</span> |
| 279 | 288 | </label> |
| 280 | 289 | </fieldset>`} |
| 281 | ${anon | |
| 290 | ${anon || short | |
| 282 | 291 | ? null |
| 283 | 292 | : formField({ |
| 284 | 293 | label: values.hadKey ? "Model key" : "Model key (optional)", |
| @@ -291,7 +300,7 @@ function formView( | ||
| 291 | 300 | : "An Anthropic key (sk-ant-api…, sk-ant-usr…), a `claude setup-token` token (sk-ant-oat…), or any gateway's key with its base URL below; with its own spend limit, sealed in the child, never shown again.", |
| 292 | 301 | ...(errors["modelKey"] ? { error: errors["modelKey"] } : {}), |
| 293 | 302 | })} |
| 294 | ${anon | |
| 303 | ${anon || short | |
| 295 | 304 | ? null |
| 296 | 305 | : formField({ |
| 297 | 306 | label: "API base URL (optional)", |
| @@ -301,7 +310,7 @@ function formView( | ||
| 301 | 310 | hint: "Anthropic-compatible gateway for the key. OpenRouter: https://openrouter.ai/api. Leave empty for Anthropic.", |
| 302 | 311 | ...(errors["modelEndpoint"] ? { error: errors["modelEndpoint"] } : {}), |
| 303 | 312 | })} |
| 304 | ${anon | |
| 313 | ${anon || short | |
| 305 | 314 | ? null |
| 306 | 315 | : formField({ |
| 307 | 316 | label: `Its budget, in dollars (${MIN_BUDGET_USD}–${MAX_BUDGET_USD})`, |
test/mcp.test.ts +1
| @@ -218,6 +218,7 @@ describe("the forge over MCP", () => { | ||
| 218 | 218 | "account_role", |
| 219 | 219 | "account_suspend", |
| 220 | 220 | "restore_drill", |
| 221 | "retire_forge", | |
| 221 | 222 | "model_ceiling", |
| 222 | 223 | "account_verify", |
| 223 | 224 | "outsiders", |
test/spawn-account-parallel.test.ts added +131
| @@ -0,0 +1,131 @@ | ||
| 1 | /** | |
| 2 | * The per-account limit under parallel requests, the per-address rate limit | |
| 3 | * on non-admin spawns, and the terminal's short form (#2927). | |
| 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 { createUser, type User } from "../src/core/users.ts"; | |
| 11 | import { createToken } from "../src/core/tokens.ts"; | |
| 12 | import { notePeer } from "../src/http/peer.ts"; | |
| 13 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 14 | import { usePlacement, useProbe } from "../src/spawn/door.ts"; | |
| 15 | import { bootHostedChild } from "../src/spawn/host.ts"; | |
| 16 | import { resetAnonSpawns } from "../src/spawn/rate.ts"; | |
| 17 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 18 | import { newForgeScreen } from "../src/tui/screens/newforge.ts"; | |
| 19 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 20 | import { ok } from "../src/kernel/result.ts"; | |
| 21 | import { quietLogs } from "./helpers/globals.ts"; | |
| 22 | import { settleApp } from "./helpers/tui.ts"; | |
| 23 | ||
| 24 | quietLogs(); | |
| 25 | ||
| 26 | let dir: string; | |
| 27 | let app: App; | |
| 28 | let servers: Servers; | |
| 29 | let carol: User; | |
| 30 | let carolToken: string; | |
| 31 | let dave: User; | |
| 32 | let n = 0; | |
| 33 | const made = new Set<string>(); | |
| 34 | ||
| 35 | beforeAll(async () => { | |
| 36 | process.env["FORGE_FORGES_PER_ACCOUNT"] = "2"; | |
| 37 | dir = await mkdtemp(join(tmpdir(), "forge-spawn-parallel-")); | |
| 38 | app = createApp({ dataDir: dir, port: 0 }); | |
| 39 | servers = await serve(app); | |
| 40 | const mk = async (username: string, extra: object = {}) => { | |
| 41 | const r = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", ...extra }); | |
| 42 | if (!r.ok) throw new Error("user"); | |
| 43 | return r.value; | |
| 44 | }; | |
| 45 | await mk("alice", { adminIfFirst: true }); | |
| 46 | carol = await mk("carol"); | |
| 47 | dave = await mk("dave"); | |
| 48 | carolToken = createToken(app.db, carol.id, "rw", "rw").token; | |
| 49 | useProbe(app, () => async () => true); | |
| 50 | usePlacement(app, { | |
| 51 | place: async (plan) => { | |
| 52 | await new Promise((r) => setTimeout(r, 40)); | |
| 53 | return ok({ url: `https://${plan.name}.fake.test`, bodyId: `par-${++n}` }); | |
| 54 | }, | |
| 55 | retire: async () => ok(), | |
| 56 | }); | |
| 57 | }, 30_000); | |
| 58 | ||
| 59 | afterAll(async () => { | |
| 60 | for (const name of made) await bootHostedChild(app, name); | |
| 61 | servers.stop(); | |
| 62 | app.close(); | |
| 63 | delete process.env["FORGE_FORGES_PER_ACCOUNT"]; | |
| 64 | await rm(dir, { recursive: true, force: true }); | |
| 65 | }); | |
| 66 | ||
| 67 | const post = (token: string, name: string) => | |
| 68 | fetch(`${servers.url}/api/v1/forges`, { | |
| 69 | method: "POST", | |
| 70 | headers: { "content-type": "application/json", authorization: `Bearer ${token}` }, | |
| 71 | body: JSON.stringify({ name, dryRun: false }), | |
| 72 | }); | |
| 73 | ||
| 74 | test("parallel requests from one account cannot overrun the limit", async () => { | |
| 75 | const names = ["par1", "par2", "par3", "par4"]; | |
| 76 | const results = await Promise.all(names.map((name) => post(carolToken, name))); | |
| 77 | const statuses = results.map((r) => r.status).sort(); | |
| 78 | expect(statuses).toEqual([201, 201, 409, 409]); | |
| 79 | names.forEach((name, i) => results[i]!.status === 201 && made.add(name)); | |
| 80 | }); | |
| 81 | ||
| 82 | test("non-admin spawns are also held to a few a day per address", async () => { | |
| 83 | resetAnonSpawns(app.db); | |
| 84 | const t = createToken(app.db, dave.id, "rw", "rw").token; | |
| 85 | // Two allowed by the account limit; the rate limit is exercised by the counter directly. | |
| 86 | const { noteAccountSpawn, checkAccountSpawn, ACCOUNT_SPAWNS_PER_ADDRESS_PER_DAY } = await import("../src/spawn/rate.ts"); | |
| 87 | // Loopback may be seen under any of its spellings: fill each. | |
| 88 | for (const address of ["127.0.0.1", "::1", "::ffff:127.0.0.1"]) { | |
| 89 | const req = new Request("http://x/"); | |
| 90 | notePeer(req, address); | |
| 91 | for (let i = 0; i < ACCOUNT_SPAWNS_PER_ADDRESS_PER_DAY; i++) noteAccountSpawn(app.db, req); | |
| 92 | expect(checkAccountSpawn(app.db, req).allowed).toBe(false); | |
| 93 | } | |
| 94 | const res = await post(t, "dave1"); | |
| 95 | expect(res.status).toBe(429); | |
| 96 | resetAnonSpawns(app.db); | |
| 97 | }); | |
| 98 | ||
| 99 | async function screenFor(user: User, keys: string): Promise<string> { | |
| 100 | const terminal = headlessTerminal(120, 40); | |
| 101 | const { app: tui, session } = createForgeApp( | |
| 102 | app, | |
| 103 | { sshUsername: user.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user }, | |
| 104 | { terminal, exitHooks: false }, | |
| 105 | ); | |
| 106 | tui.start(); | |
| 107 | tui.ctx.push(newForgeScreen(session, tui.ctx)); | |
| 108 | try { | |
| 109 | await settleApp(tui); | |
| 110 | terminal.feed(keys); | |
| 111 | await settleApp(tui); | |
| 112 | return tui.screen.toText(); | |
| 113 | } finally { | |
| 114 | tui.stop?.(); | |
| 115 | } | |
| 116 | } | |
| 117 | ||
| 118 | test("the terminal form is short for a non-admin, with the time budget and the limit", async () => { | |
| 119 | const text = await screenFor(dave, ""); | |
| 120 | expect(text).toContain("Live in under 3 minutes"); | |
| 121 | expect(text).toContain("own up to 2 forges"); | |
| 122 | expect(text).toContain("Name"); | |
| 123 | for (const gone of ["What it is for", "Model key", "Budget in dollars", "For whom"]) expect(text).not.toContain(gone); | |
| 124 | }); | |
| 125 | ||
| 126 | test("the terminal form is long for an admin", async () => { | |
| 127 | const alice = (await import("../src/core/users.ts")).getUserByName(app.db, "alice")!; | |
| 128 | const text = await screenFor(alice, ""); | |
| 129 | expect(text).not.toContain("Live in under"); | |
| 130 | expect(text).toContain("Name"); | |
| 131 | }); |
test/spawn-account.test.ts added +162
| @@ -0,0 +1,162 @@ | ||
| 1 | /** | |
| 2 | * The short door (#2927): any signed-in person makes a forge from a name, a | |
| 3 | * size and public apps; one account owns at most FORGE_FORGES_PER_ACCOUNT. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, describe, 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 { createAgent } from "../src/actions/crew.ts"; | |
| 11 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 12 | import { ownedBy } from "../src/spawn/account.ts"; | |
| 13 | import { readFile, writeFile } from "node:fs/promises"; | |
| 14 | import { usePlacement, useProbe } from "../src/spawn/door.ts"; | |
| 15 | import { bootHostedChild } from "../src/spawn/host.ts"; | |
| 16 | import { createToken } from "../src/core/tokens.ts"; | |
| 17 | import { createUser } from "../src/core/users.ts"; | |
| 18 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 19 | import { ok } from "../src/kernel/result.ts"; | |
| 20 | ||
| 21 | describe("a forge for any signed-in person", () => { | |
| 22 | let dataDir: string; | |
| 23 | let app: App; | |
| 24 | let servers: Servers; | |
| 25 | let base: string; | |
| 26 | let bob: string; | |
| 27 | let admin: string; | |
| 28 | let agent: string; | |
| 29 | let placed = 0; | |
| 30 | const made: string[] = []; | |
| 31 | const saved = process.env["FORGE_FORGES_PER_ACCOUNT"]; | |
| 32 | ||
| 33 | const post = (token: string, payload: unknown) => | |
| 34 | fetch(`${base}/api/v1/forges`, { | |
| 35 | method: "POST", | |
| 36 | headers: { "content-type": "application/json", authorization: `Bearer ${token}` }, | |
| 37 | body: JSON.stringify(payload), | |
| 38 | }); | |
| 39 | ||
| 40 | beforeAll(async () => { | |
| 41 | delete process.env["FORGE_FORGES_PER_ACCOUNT"]; | |
| 42 | dataDir = await mkdtemp(join(tmpdir(), "forge-spawn-account-")); | |
| 43 | app = createApp({ dataDir, port: 0 }); | |
| 44 | servers = await serve(app); | |
| 45 | base = servers.url; | |
| 46 | const a = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 47 | const b = await createUser(app.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase" }); | |
| 48 | if (!a.ok || !b.ok) throw new Error("users"); | |
| 49 | admin = createToken(app.db, a.value.id, "rw", "rw").token; | |
| 50 | bob = createToken(app.db, b.value.id, "rw", "rw").token; | |
| 51 | for (const [name, visibility] of [["notes", "public"], ["diary", "private"]] as const) { | |
| 52 | const r = await createRepo(app, a.value, { name, visibility }); | |
| 53 | if (!r.ok) throw new Error("repo"); | |
| 54 | await commitToBranch(app, a.value, r.value, { message: "seed", files: [{ path: "a.txt", content: "a" }] }); | |
| 55 | } | |
| 56 | const ag = await createAgent(app, a.value, { username: "scout", brief: "ship small" }); | |
| 57 | if (!ag.ok) throw new Error("agent"); | |
| 58 | agent = createToken(app.db, ag.value.user.id, "rw", "rw").token; | |
| 59 | useProbe(app, () => async () => true); | |
| 60 | usePlacement(app, { | |
| 61 | place: async (plan) => ok({ url: `https://${plan.name}.fake.test`, bodyId: `fake-${++placed}` }), | |
| 62 | retire: async () => ok(), | |
| 63 | }); | |
| 64 | }, 30_000); | |
| 65 | ||
| 66 | afterAll(async () => { | |
| 67 | for (const n of made) await bootHostedChild(app, n); | |
| 68 | servers.stop(); | |
| 69 | app.close(); | |
| 70 | if (saved === undefined) delete process.env["FORGE_FORGES_PER_ACCOUNT"]; | |
| 71 | else process.env["FORGE_FORGES_PER_ACCOUNT"] = saved; | |
| 72 | await rm(dataDir, { recursive: true, force: true }); | |
| 73 | }); | |
| 74 | ||
| 75 | test("a non-admin makes a forge with a public app", async () => { | |
| 76 | const res = await post(bob, { name: "bobs", apps: ["alice/notes"], size: "medium", dryRun: false }); | |
| 77 | expect(res.status).toBe(201); | |
| 78 | made.push("bobs"); | |
| 79 | }); | |
| 80 | ||
| 81 | test("a private app is not a known app", async () => { | |
| 82 | const res = await post(bob, { name: "bobs2", apps: ["alice/diary"] }); | |
| 83 | expect(res.status).toBe(422); | |
| 84 | expect(((await res.json()) as any).message).toContain("not a known app"); | |
| 85 | }); | |
| 86 | ||
| 87 | test("crew, a model key and a direction stay on the admin's form", async () => { | |
| 88 | for (const extra of [{ crew: ["scout"] }, { modelKey: "sk-ant-api-123", budgetUsd: 5 }, { direction: "do things" }]) { | |
| 89 | const res = await post(bob, { name: "bobs2", ...extra }); | |
| 90 | expect(res.status).toBe(422); | |
| 91 | } | |
| 92 | }); | |
| 93 | ||
| 94 | test("an agent account is still refused", async () => { | |
| 95 | const res = await post(agent, { name: "bots", dryRun: false }); | |
| 96 | expect(res.status).toBe(403); | |
| 97 | }); | |
| 98 | ||
| 99 | test("over the limit is refused with one sentence; the admin is not counted", async () => { | |
| 100 | for (const n of ["bobs3", "bobs4"]) { | |
| 101 | expect((await post(bob, { name: n, dryRun: false })).status).toBe(201); | |
| 102 | made.push(n); | |
| 103 | } | |
| 104 | const res = await post(bob, { name: "bobs5", dryRun: false }); | |
| 105 | expect(res.status).toBe(409); | |
| 106 | expect(((await res.json()) as any).message).toBe("you already own 3 forges, the most one account may make here"); | |
| 107 | const adminOk = await post(admin, { name: "alices", dryRun: false }); | |
| 108 | expect(adminOk.status).toBe(201); | |
| 109 | made.push("alices"); | |
| 110 | }); | |
| 111 | ||
| 112 | test("the ledger names the owner", async () => { | |
| 113 | const l = JSON.parse(await readFile(join(dataDir, "spawn", "bobs.json"), "utf8")); | |
| 114 | expect(l.ownerId).toBeString(); | |
| 115 | expect(ownedBy(dataDir, l.ownerId)).toBe(3); | |
| 116 | }); | |
| 117 | ||
| 118 | test("the MCP spawn tool refuses the long-form fields for a non-admin", async () => { | |
| 119 | const res = await fetch(`${base}/mcp`, { | |
| 120 | method: "POST", | |
| 121 | headers: { "content-type": "application/json", authorization: `Bearer ${bob}` }, | |
| 122 | body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "spawn", arguments: { name: "mcpkid", crew: ["scout"] } } }), | |
| 123 | }); | |
| 124 | const r = ((await res.json()) as any).result; | |
| 125 | expect(r.isError).toBe(true); | |
| 126 | expect(r.content[0].text).toContain("only an admin"); | |
| 127 | }); | |
| 128 | ||
| 129 | test("a failed, abandoned spawn stops counting after an hour", async () => { | |
| 130 | const f = join(dataDir, "spawn", "bobs4.json"); | |
| 131 | const l = JSON.parse(await readFile(f, "utf8")); | |
| 132 | await writeFile(f, JSON.stringify({ ...l, done: false, createdAt: new Date(Date.now() - 2 * 3_600_000).toISOString() })); | |
| 133 | expect(ownedBy(dataDir, l.ownerId)).toBe(2); | |
| 134 | await writeFile(f, JSON.stringify(l)); | |
| 135 | expect(ownedBy(dataDir, l.ownerId)).toBe(3); | |
| 136 | }); | |
| 137 | ||
| 138 | test("FORGE_FORGES_PER_ACCOUNT sets the limit", async () => { | |
| 139 | process.env["FORGE_FORGES_PER_ACCOUNT"] = "1"; | |
| 140 | const res = await post(bob, { name: "bobs6", dryRun: false }); | |
| 141 | expect(res.status).toBe(409); | |
| 142 | expect(((await res.json()) as any).message).toContain("you already own 1 forge,"); | |
| 143 | }); | |
| 144 | ||
| 145 | test("the page is short, states the time budget and the limit", async () => { | |
| 146 | delete process.env["FORGE_FORGES_PER_ACCOUNT"]; | |
| 147 | const res = await fetch(`${base}/signin`, { | |
| 148 | method: "POST", | |
| 149 | body: new URLSearchParams({ username: "bob", password: "a-good-passphrase" }), | |
| 150 | redirect: "manual", | |
| 151 | }); | |
| 152 | const cookie = (res.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 153 | const html = await (await fetch(`${base}/forges/new`, { headers: { cookie } })).text(); | |
| 154 | expect(html).toContain("Live in under 3 minutes"); | |
| 155 | expect(html).toContain("up to 3 forges"); | |
| 156 | expect(html).toContain('name="size"'); | |
| 157 | expect(html).toContain("alice/notes"); | |
| 158 | expect(html).not.toContain("alice/diary"); | |
| 159 | for (const gone of ['name="modelKey"', 'name="direction"', 'name="crew"', 'name="budgetUsd"']) | |
| 160 | expect(html).not.toContain(gone); | |
| 161 | }); | |
| 162 | }); |
test/spawn-retire-rate.test.ts added +124
| @@ -0,0 +1,124 @@ | ||
| 1 | /** | |
| 2 | * The per-address spawn count end to end (a real sixth spawn is refused, a | |
| 3 | * successful one is counted, parallel ones cannot overrun it) and the retire | |
| 4 | * door that frees a person's slot under the per-account limit (#2927). | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { createUser } from "../src/core/users.ts"; | |
| 12 | import { createToken } from "../src/core/tokens.ts"; | |
| 13 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 14 | import { usePlacement, useProbe } from "../src/spawn/door.ts"; | |
| 15 | import { ACCOUNT_SPAWNS_PER_ADDRESS_PER_DAY, resetAnonSpawns } from "../src/spawn/rate.ts"; | |
| 16 | import { ok } from "../src/kernel/result.ts"; | |
| 17 | import { quietLogs } from "./helpers/globals.ts"; | |
| 18 | ||
| 19 | quietLogs(); | |
| 20 | ||
| 21 | let dir: string; | |
| 22 | let app: App; | |
| 23 | let servers: Servers; | |
| 24 | let tokens: Record<string, string>; | |
| 25 | let n = 0; | |
| 26 | const retired: string[] = []; | |
| 27 | ||
| 28 | beforeAll(async () => { | |
| 29 | dir = await mkdtemp(join(tmpdir(), "forge-spawn-retire-")); | |
| 30 | app = createApp({ dataDir: dir, port: 0 }); | |
| 31 | servers = await serve(app); | |
| 32 | tokens = {}; | |
| 33 | for (const username of ["alice", "erin", "frank", "gina"]) { | |
| 34 | const r = await createUser(app.db, { | |
| 35 | origin: "seed", | |
| 36 | trusted: true, | |
| 37 | username, | |
| 38 | email: `${username}@forge.local`, | |
| 39 | password: "a-good-passphrase", | |
| 40 | ...(username === "alice" ? { adminIfFirst: true } : {}), | |
| 41 | }); | |
| 42 | if (!r.ok) throw new Error("user"); | |
| 43 | tokens[username] = createToken(app.db, r.value.id, "rw", "rw").token; | |
| 44 | } | |
| 45 | useProbe(app, () => async () => true); | |
| 46 | usePlacement(app, { | |
| 47 | place: async (plan) => { | |
| 48 | await new Promise((r) => setTimeout(r, 20)); | |
| 49 | return ok({ url: `https://${plan.name}.fake.test`, bodyId: `rr-${++n}` }); | |
| 50 | }, | |
| 51 | retire: async (id) => { | |
| 52 | retired.push(id); | |
| 53 | return ok(); | |
| 54 | }, | |
| 55 | }); | |
| 56 | }, 30_000); | |
| 57 | ||
| 58 | afterAll(async () => { | |
| 59 | servers.stop(); | |
| 60 | app.close(); | |
| 61 | delete process.env["FORGE_FORGES_PER_ACCOUNT"]; | |
| 62 | await rm(dir, { recursive: true, force: true }); | |
| 63 | }); | |
| 64 | ||
| 65 | const post = (who: string, name: string) => | |
| 66 | fetch(`${servers.url}/api/v1/forges`, { | |
| 67 | method: "POST", | |
| 68 | headers: { "content-type": "application/json", authorization: `Bearer ${tokens[who]}` }, | |
| 69 | body: JSON.stringify({ name, dryRun: false }), | |
| 70 | }); | |
| 71 | const del = (who: string, name: string) => | |
| 72 | fetch(`${servers.url}/api/v1/forges/${name}`, { method: "DELETE", headers: { authorization: `Bearer ${tokens[who]}` } }); | |
| 73 | const counted = () => | |
| 74 | (app.db.query("SELECT COUNT(*) AS n FROM anon_spawn WHERE bucket LIKE 'acct:%'").get() as { n: number }).n; | |
| 75 | ||
| 76 | test("a real sixth spawn from one address is refused, and each success is counted", async () => { | |
| 77 | process.env["FORGE_FORGES_PER_ACCOUNT"] = "100"; | |
| 78 | resetAnonSpawns(app.db); | |
| 79 | for (let i = 1; i <= ACCOUNT_SPAWNS_PER_ADDRESS_PER_DAY; i++) { | |
| 80 | expect((await post("erin", `seq${i}`)).status).toBe(201); | |
| 81 | expect(counted()).toBe(i); | |
| 82 | } | |
| 83 | const sixth = await post("erin", "seq6"); | |
| 84 | expect(sixth.status).toBe(429); | |
| 85 | expect(((await sixth.json()) as { error: string }).error).toBe("rate_limited"); | |
| 86 | expect(counted()).toBe(ACCOUNT_SPAWNS_PER_ADDRESS_PER_DAY); | |
| 87 | }); | |
| 88 | ||
| 89 | test("parallel spawns from one address cannot exceed the per-address cap", async () => { | |
| 90 | process.env["FORGE_FORGES_PER_ACCOUNT"] = "100"; | |
| 91 | resetAnonSpawns(app.db); | |
| 92 | const rs = await Promise.all(Array.from({ length: 8 }, (_, i) => post("frank", `pp${i}`))); | |
| 93 | const statuses = rs.map((r) => r.status); | |
| 94 | expect(statuses.filter((s) => s === 201).length).toBe(ACCOUNT_SPAWNS_PER_ADDRESS_PER_DAY); | |
| 95 | expect(statuses.filter((s) => s === 429).length).toBe(3); | |
| 96 | expect(counted()).toBe(ACCOUNT_SPAWNS_PER_ADDRESS_PER_DAY); | |
| 97 | }); | |
| 98 | ||
| 99 | test("a refused spawn gives its count back", async () => { | |
| 100 | process.env["FORGE_FORGES_PER_ACCOUNT"] = "1"; | |
| 101 | resetAnonSpawns(app.db); | |
| 102 | expect((await post("gina", "one")).status).toBe(201); | |
| 103 | expect((await post("gina", "two")).status).toBe(409); | |
| 104 | expect(counted()).toBe(1); | |
| 105 | }); | |
| 106 | ||
| 107 | test("retiring a child frees its owner's slot; another person cannot retire it", async () => { | |
| 108 | process.env["FORGE_FORGES_PER_ACCOUNT"] = "1"; | |
| 109 | resetAnonSpawns(app.db); | |
| 110 | // gina's "one" holds her only slot from the test above. | |
| 111 | expect((await del("erin", "one")).status).toBe(404); | |
| 112 | expect(retired).toEqual([]); | |
| 113 | const gone = await del("gina", "one"); | |
| 114 | expect(gone.status).toBe(200); | |
| 115 | expect(await gone.json()).toEqual({ retired: true, name: "one" }); | |
| 116 | expect(retired.length).toBe(1); | |
| 117 | expect((await del("gina", "one")).status).toBe(404); | |
| 118 | expect((await post("gina", "again")).status).toBe(201); | |
| 119 | }); | |
| 120 | ||
| 121 | test("retiring needs a session", async () => { | |
| 122 | const r = await fetch(`${servers.url}/api/v1/forges/again`, { method: "DELETE" }); | |
| 123 | expect(r.status).toBe(401); | |
| 124 | }); |
new above
offline