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

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/13821-removing-a-forge-leaves-it-counted-and-l → main 1 commit fork

  • Merged. scout/13821-removing-a-forge-leaves-it-counted-and-l was merged into main as 46835f7.

Review

Sign in to review this change.
3 files changed +28 −4
src/spawn/door.ts +6
@@ -152,6 +152,12 @@ function placementFor(app: App): Placement {
152152 return p;
153153 }
154154
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
155161 /** Why this forge can hold no child right now, or undefined when it can. */
156162 export function noRoom(app: App): string | undefined {
157163 return placementFor(app).room?.()?.message;
src/spawn/manage.ts +8 −1
@@ -13,6 +13,7 @@ import { revokeChildFollowTokens } from "../core/proposal_token.ts";
1313 import { restoreClaim, revokeClaim, spawnedOrg } from "./claim.ts";
1414 import { forgetHeard } from "./clients.ts";
1515 import { forgetForge } from "../core/child_reach.ts";
16 import { retireBody } from "./door.ts";
1617 import { sleepChild } from "./host.ts";
1718 import { CHILD_NAME } from "./placement.ts";
1819 import { switchOwnCode } from "./own_code.ts";
@@ -40,8 +41,14 @@ export async function removeForge(app: App, name: string): Promise<Managed> {
4041 await rm(childDirOf(app, child.bodyId), { recursive: true, force: true });
4142 // What this forge minted for it to read private apps ends with it: no pass will look at it again.
4243 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.
4446 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);
4552 app.bus.append({ kind: "forge.removed", payload: { name } });
4653 return { ok: true, says: `${name} is removed, with its data` };
4754 }
test/forge-sleep.test.ts +14 −3
@@ -6,7 +6,7 @@
66 * the owner stops and removes one (admin only, same-origin, confirmed).
77 */
88 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
9 import { existsSync } from "node:fs";
9 import { existsSync, mkdirSync, writeFileSync } from "node:fs";
1010 import { mkdtemp, rm } from "node:fs/promises";
1111 import { tmpdir } from "node:os";
1212 import { join } from "node:path";
@@ -17,6 +17,7 @@ import { createUser } from "../src/core/users.ts";
1717 import { serve, type Servers } from "../src/http/server.ts";
1818 import { awakeCount, childAwake, gateClock, holdChild, resolveHostedChild, sleepIdleChildren, wakeGate } from "../src/spawn/host.ts";
1919 import { heldOf } from "../src/perf/held.ts";
20 import { usePlacement } from "../src/spawn/door.ts";
2021 import { removeForge, stopForge } from "../src/spawn/manage.ts";
2122 import { setLimit } from "../src/settings/limits.ts";
2223 import { awakeRefusal, personRefusal, WakeRate } from "../src/spawn/sleep.ts";
@@ -204,7 +205,8 @@ describe("a hosted forge scales to zero", () => {
204205 try {
205206 expect(await gate()).toBeUndefined(); // the first wake is granted and holds the one slot
206207 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.
208210 expect(awakeRefusal(awakeCount(parent))).toContain("the forge is full, try again shortly");
209211 } finally {
210212 if (was === undefined) delete process.env["FORGE_CHILD_AWAKE_MAX"];
@@ -266,7 +268,8 @@ describe("a hosted forge scales to zero", () => {
266268 const child = (await resolveHostedChild(parent, new Request("https://forge.test/healthz", { headers: { host: CHILD_HOST } }))) as App;
267269 const bob = child.db.query("SELECT id FROM user WHERE username = 'bob'").get() as { id: string };
268270 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.
270273 const round = async (cookie: string) => {
271274 await fetch(`${servers.url}/healthz`, { headers: { host: CHILD_HOST, authorization: `Bearer ${token}` } });
272275 await Bun.sleep(30);
@@ -406,8 +409,16 @@ describe("a hosted forge scales to zero", () => {
406409 expect((await stopForge(parent, "homie")).says).toContain("asleep");
407410 expect(childAwake(parent, "homie")).toBe(false);
408411 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 }; } });
409418 const removed = await removeForge(parent, "homie");
410419 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);
411422 expect(existsSync(join(dir, "spawn", "homie.json"))).toBe(false);
412423 const gone = await fetch(`${servers.url}/healthz`, { headers: { host: CHILD_HOST } });
413424 expect(gone.status).toBe(404);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs