Public: anyone can read this; the crew can change it.
Removing a forge leaves it counted and leaves its files: children.max, reach/ and beats/ #13828
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13821-removing-a-forge-leaves-it-counted-and-l → main 1 commit fork
3 files
changed +28
−4
src/spawn/door.ts +6
| @@ -152,6 +152,12 @@ function placementFor(app: App): Placement { | ||
| 152 | 152 | return p; |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | /** A removed forge stops being counted: its body leaves the placement's store (idempotent). */ | |
| 156 | export async function retireBody(app: App, bodyId: string): Promise<void> { | |
| 157 | const done = await placementFor(app).retire(bodyId); | |
| 158 | if (!done.ok) app.log.warn(`the placement of the removed forge was not retired: ${done.error.message}`); | |
| 159 | } | |
| 160 | ||
| 155 | 161 | /** Why this forge can hold no child right now, or undefined when it can. */ |
| 156 | 162 | export function noRoom(app: App): string | undefined { |
| 157 | 163 | return placementFor(app).room?.()?.message; |
src/spawn/manage.ts +8 −1
| @@ -13,6 +13,7 @@ import { revokeChildFollowTokens } from "../core/proposal_token.ts"; | ||
| 13 | 13 | import { restoreClaim, revokeClaim, spawnedOrg } from "./claim.ts"; |
| 14 | 14 | import { forgetHeard } from "./clients.ts"; |
| 15 | 15 | import { forgetForge } from "../core/child_reach.ts"; |
| 16 | import { retireBody } from "./door.ts"; | |
| 16 | 17 | import { sleepChild } from "./host.ts"; |
| 17 | 18 | import { CHILD_NAME } from "./placement.ts"; |
| 18 | 19 | import { switchOwnCode } from "./own_code.ts"; |
| @@ -40,8 +41,14 @@ export async function removeForge(app: App, name: string): Promise<Managed> { | ||
| 40 | 41 | await rm(childDirOf(app, child.bodyId), { recursive: true, force: true }); |
| 41 | 42 | // What this forge minted for it to read private apps ends with it: no pass will look at it again. |
| 42 | 43 | revokeChildFollowTokens(app.db, name); |
| 43 | // What it asked to reach and was allowed goes with it: a new forge of the same name starts with neither. | |
| 44 | // What it asked to reach and was allowed goes with it: a new forge of the same name starts with | |
| 45 | // neither. | |
| 44 | 46 | forgetForge(app.db, child.bodyId); |
| 47 | // The parent's own records of it: a new forge of the same name inherits none, and nothing pages | |
| 48 | // for it. | |
| 49 | for (const dir of ["reach", "beats", "credit"]) await rm(join(app.config.dataDir, dir, `${name}.json`), { force: true }); | |
| 50 | // The count of forges sent on this node is the forges that exist. | |
| 51 | await retireBody(app, child.bodyId); | |
| 45 | 52 | app.bus.append({ kind: "forge.removed", payload: { name } }); |
| 46 | 53 | return { ok: true, says: `${name} is removed, with its data` }; |
| 47 | 54 | } |
test/forge-sleep.test.ts +14 −3
| @@ -6,7 +6,7 @@ | ||
| 6 | 6 | * the owner stops and removes one (admin only, same-origin, confirmed). |
| 7 | 7 | */ |
| 8 | 8 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 9 | import { existsSync } from "node:fs"; | |
| 9 | import { existsSync, mkdirSync, writeFileSync } from "node:fs"; | |
| 10 | 10 | import { mkdtemp, rm } from "node:fs/promises"; |
| 11 | 11 | import { tmpdir } from "node:os"; |
| 12 | 12 | import { join } from "node:path"; |
| @@ -17,6 +17,7 @@ import { createUser } from "../src/core/users.ts"; | ||
| 17 | 17 | import { serve, type Servers } from "../src/http/server.ts"; |
| 18 | 18 | import { awakeCount, childAwake, gateClock, holdChild, resolveHostedChild, sleepIdleChildren, wakeGate } from "../src/spawn/host.ts"; |
| 19 | 19 | import { heldOf } from "../src/perf/held.ts"; |
| 20 | import { usePlacement } from "../src/spawn/door.ts"; | |
| 20 | 21 | import { removeForge, stopForge } from "../src/spawn/manage.ts"; |
| 21 | 22 | import { setLimit } from "../src/settings/limits.ts"; |
| 22 | 23 | import { awakeRefusal, personRefusal, WakeRate } from "../src/spawn/sleep.ts"; |
| @@ -204,7 +205,8 @@ describe("a hosted forge scales to zero", () => { | ||
| 204 | 205 | try { |
| 205 | 206 | expect(await gate()).toBeUndefined(); // the first wake is granted and holds the one slot |
| 206 | 207 | expect(awakeCount(parent)).toBe(1); |
| 207 | // Another forge's wake now would pass the ceiling: the slot is reserved though nothing is open yet. | |
| 208 | // Another forge's wake now would pass the ceiling: the slot is reserved though nothing is | |
| 209 | // open yet. | |
| 208 | 210 | expect(awakeRefusal(awakeCount(parent))).toContain("the forge is full, try again shortly"); |
| 209 | 211 | } finally { |
| 210 | 212 | if (was === undefined) delete process.env["FORGE_CHILD_AWAKE_MAX"]; |
| @@ -266,7 +268,8 @@ describe("a hosted forge scales to zero", () => { | ||
| 266 | 268 | const child = (await resolveHostedChild(parent, new Request("https://forge.test/healthz", { headers: { host: CHILD_HOST } }))) as App; |
| 267 | 269 | const bob = child.db.query("SELECT id FROM user WHERE username = 'bob'").get() as { id: string }; |
| 268 | 270 | const { token: session } = createSession(child.db, bob.id); |
| 269 | // Each round: the last touch is at least 30 ms old, one request, then a sweep that sleeps what is 20 ms idle. | |
| 271 | // Each round: the last touch is at least 30 ms old, one request, then a sweep that sleeps what | |
| 272 | // is 20 ms idle. | |
| 270 | 273 | const round = async (cookie: string) => { |
| 271 | 274 | await fetch(`${servers.url}/healthz`, { headers: { host: CHILD_HOST, authorization: `Bearer ${token}` } }); |
| 272 | 275 | await Bun.sleep(30); |
| @@ -406,8 +409,16 @@ describe("a hosted forge scales to zero", () => { | ||
| 406 | 409 | expect((await stopForge(parent, "homie")).says).toContain("asleep"); |
| 407 | 410 | expect(childAwake(parent, "homie")).toBe(false); |
| 408 | 411 | expect((await stopForge(parent, "Not A Name")).ok).toBe(false); |
| 412 | for (const d of ["reach", "beats", "credit"]) { | |
| 413 | mkdirSync(join(dir, d), { recursive: true }); | |
| 414 | writeFileSync(join(dir, d, "homie.json"), "{}"); | |
| 415 | } | |
| 416 | const retired: string[] = []; | |
| 417 | usePlacement(parent, { place: async () => { throw new Error("unused"); }, retire: async (id) => { retired.push(id); return { ok: true, value: undefined }; } }); | |
| 409 | 418 | const removed = await removeForge(parent, "homie"); |
| 410 | 419 | expect(removed.ok).toBe(true); |
| 420 | for (const d of ["reach", "beats", "credit"]) expect(existsSync(join(dir, d, "homie.json"))).toBe(false); | |
| 421 | expect(retired).toHaveLength(1); | |
| 411 | 422 | expect(existsSync(join(dir, "spawn", "homie.json"))).toBe(false); |
| 412 | 423 | const gone = await fetch(`${servers.url}/healthz`, { headers: { host: CHILD_HOST } }); |
| 413 | 424 | expect(gone.status).toBe(404); |
new above
offline