Public: anyone can read this; the crew can change it.

The forges list asks no forge that is asleep: one look at it woke every sleeping forge at once #13788

Merged opened this

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

Merged

trevato/scale-list-wakes-nothing → main 2 commits

  • Merged. trevato/scale-list-wakes-nothing was merged into main as a14c54e.

Review

Sign in to review this change.
3 files changed +133 −7
src/main.ts +3 −1
@@ -119,7 +119,7 @@ import { stepWithBeat } from "./keeper/beat.ts";
119119 import { keeperEnv, keeperPorts } from "./keeper/wire.ts";
120120 import { noteKeeperAtBoot } from "./keeper/at_boot.ts";
121121 import { snapshotPlace } from "./actions/health.ts";
122 import { dropServed } from "./spawn/clients.ts";
122 import { dropServed, useSleepers } from "./spawn/clients.ts";
123123 import { sweepLeftoverProbes } from "./release/leftover_sweep.ts";
124124 import { forgesMemory, memoryOverLine } from "./spawn/memory_fit.ts";
125125 import { keepFloor } from "./apps/cgroup.ts";
@@ -713,6 +713,8 @@ const upstreamTimer = startUpstreamWatch(app);
713713 // ...and this forge asks every child's public addresses what a visitor would get, every few minutes
714714 // (spawn/reach.ts).
715715 const reachTimer = startReachWatch(app, undefined, (name) => !childAwake(app, name));
716 // ...and the list of them (/forges, the `forges` tool, the terminal) asks none that is asleep.
717 useSleepers(app, (name) => !childAwake(app, name));
716718 // Prompts from Instagram (docs/APPS.md): once a day, for each repository
717719 // that connected an account, the top comment on its latest Reel is told to
718720 // the crew. A minute's clock; nothing leaves the box until a source exists.
src/spawn/clients.ts +31 −6
@@ -273,6 +273,17 @@ const heard = new WeakMap<App, Map<string, Heard>>();
273273 /** Asked again at most this often, however often the list is opened. */
274274 const FRESH_MS = 1500;
275275 const asking = new WeakMap<App, Map<string, Promise<ForgeState | undefined>>>();
276 /** Which children are asleep on this host, by name; unset, none is. */
277 const sleepers = new WeakMap<App, (name: string) => boolean>();
278
279 /**
280 * Tell the list which children are asleep here (`main.ts`, as the reach watch is told): it never
281 * asks one. Asking a sleeping forge's address wakes it, and one look at the list woke every
282 * sleeping forge at once.
283 */
284 export function useSleepers(app: App, asleep: (name: string) => boolean): void {
285 sleepers.set(app, asleep);
286 }
276287
277288 /** Forget what a child last said, so the next list asks it again (after its link changed). */
278289 export function forgetHeard(app: App, name: string): void {
@@ -301,18 +312,24 @@ function ask(app: App, child: Child): Promise<ForgeState | undefined> {
301312 * asked now (all at once, each for at most two seconds); one heard from
302313 * before answers from what it said, and is asked again in the background
303314 * when that is no longer fresh, so the list never waits on a known child.
315 * A child asleep on this host (`useSleepers`) is never asked: the list is
316 * a read, and it wakes nothing.
304317 */
305318 export async function clientRows(app: App, options: { probe?: boolean } = {}): Promise<ClientRow[]> {
306319 const probing = options.probe !== false;
307320 const children = await listChildren(app);
308321 const known = heard.get(app);
309322 const now = Date.now();
323 const told = sleepers.get(app);
324 const asleep = children.map((c) => told?.(c.name) === true);
310325 const states = await Promise.all(
311 children.map((c) => {
326 children.map((c, i) => {
312327 const last = known?.get(c.name);
313 // Not asking (a clock's pass, which must not wake a sleeping forge): what each last said, or
314 // nothing.
315 if (!probing) return last?.state;
328 // Asleep here: never asked (asking its address would wake it); what it last said, or nothing.
329 if (asleep[i] === true) return last?.state;
330 // A clock's pass asks only a forge known to be awake that has not said since this forge
331 // started, once, so a restart does not leave the awake ones reading "Unknown".
332 if (!probing && (told === undefined || last !== undefined)) return last?.state;
316333 if (last === undefined) return ask(app, c);
317334 if (now - last.at > FRESH_MS) void ask(app, c);
318335 return last.state;
@@ -331,6 +348,14 @@ export async function clientRows(app: App, options: { probe?: boolean } = {}): P
331348 const sealed = isSealed(app);
332349 // One read of the mount table for every row.
333350 const mountinfo = children.length === 0 ? "" : readMountinfo();
351 // A forge asleep here that never said: its own tables say whether it was claimed, by the rule it
352 // answers with itself (`hasAdmin`), so sleeping does not make it read "Unknown".
353 const claimed = children.map((_, i) => {
354 const said = states[i]?.claimed;
355 if (said !== undefined) return said;
356 const person = people[i];
357 return asleep[i] === true && person !== undefined ? person.claimedBy !== null : null;
358 });
334359 return orderRows(
335360 children.map((c, i) => ({
336361 name: c.name,
@@ -342,7 +367,7 @@ export async function clientRows(app: App, options: { probe?: boolean } = {}): P
342367 // Undefined until the child has beaten with the field: unknown is not "not yet".
343368 firstTellAt: beats[i]?.beat.firstTellAt,
344369 made: c.done,
345 claimed: states[i]?.claimed ?? null,
370 claimed: claimed[i] ?? null,
346371 lastActivityAt: people[i]?.lastAt ?? null,
347372 claimedBy: people[i]?.claimedBy ?? null,
348373 claimedAt: people[i]?.claimedAt ?? null,
@@ -369,7 +394,7 @@ export async function clientRows(app: App, options: { probe?: boolean } = {}): P
369394 replacedAt: replaced.get(c.name) ?? null,
370395 // Read from the record this forge wrote when it made the child, never from the child's word.
371396 madeFrom: madeFromOf(childDirOf(app, c.bodyId)) ?? null,
372 nextStep: nextStepOf(states[i]?.claimed ?? null, beats[i]?.beat),
397 nextStep: nextStepOf(claimed[i] ?? null, beats[i]?.beat),
373398 })),
374399 now,
375400 );
test/client-list-sleepers.test.ts added +99
@@ -0,0 +1,99 @@
1 /**
2 * The list of forges is a read: it asks no forge that is asleep on this host. Asking a sleeping
3 * forge's address wakes it, and one look at the list (the page, the `forges` tool, the terminal)
4 * booted every sleeping forge at once: 15 of them on 2026-11-16.
5 */
6
7 import { expect, test } from "bun:test";
8 import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
9 import { tmpdir } from "node:os";
10 import { join } from "node:path";
11 import { createApp } from "../src/app.ts";
12 import { createUser } from "../src/core/users.ts";
13 import { claimState, clientRows, useChildFetch, useSleepers } from "../src/spawn/clients.ts";
14
15 function parentWith(names: readonly string[]) {
16 const dir = mkdtempSync(join(tmpdir(), "forge-sleepers-"));
17 const app = createApp({ dataDir: dir, url: "https://forge.test", port: 0 });
18 mkdirSync(join(dir, "spawn"), { recursive: true });
19 for (const name of names)
20 writeFileSync(
21 join(dir, "spawn", `${name}.json`),
22 JSON.stringify({ name, bodyId: `cell-${name}`, done: true, placement: "cell", size: "small", apps: [], createdAt: new Date().toISOString() }),
23 );
24 const asked: string[] = [];
25 useChildFetch(app, async (url) => {
26 asked.push(new URL(url).hostname.split("--")[0] ?? "");
27 return Response.json({ claimed: true, link: { state: "used", revokedAt: null, usedAt: null } });
28 });
29 return { app, dir, asked };
30 }
31
32 test("a forge asleep here is not asked; one awake is", async () => {
33 const { app, asked } = parentWith(["awake", "asleep"]);
34 try {
35 useSleepers(app, (name) => name === "asleep");
36 const rows = await clientRows(app);
37 expect(asked).toEqual(["awake"]);
38 expect(rows.find((r) => r.name === "awake")?.claimed).toBe(true);
39 // Never heard from and no database to read: unknown, and still not asked on the next look.
40 expect(rows.find((r) => r.name === "asleep")?.claimed).toBeNull();
41 await clientRows(app);
42 expect(asked.filter((name) => name === "asleep")).toEqual([]);
43 } finally {
44 app.close();
45 }
46 });
47
48 test("a sleeping forge keeps the last word it said", async () => {
49 const { app, asked } = parentWith(["kid"]);
50 try {
51 const [first] = await clientRows(app);
52 expect(first?.claimed).toBe(true);
53 useSleepers(app, () => true);
54 asked.length = 0;
55 const [again] = await clientRows(app);
56 expect(asked).toEqual([]);
57 expect(again?.claimed).toBe(true);
58 expect(again?.link?.state).toBe("used");
59 } finally {
60 app.close();
61 }
62 });
63
64 test("a clock's pass asks a forge known to be awake once, and nobody when it is not told who sleeps", async () => {
65 const { app, asked } = parentWith(["up", "down"]);
66 try {
67 await clientRows(app, { probe: false });
68 expect(asked).toEqual([]);
69 useSleepers(app, (name) => name === "down");
70 const rows = await clientRows(app, { probe: false });
71 expect(asked).toEqual(["up"]);
72 expect(rows.find((r) => r.name === "up")?.claimed).toBe(true);
73 await clientRows(app, { probe: false });
74 expect(asked).toEqual(["up"]);
75 } finally {
76 app.close();
77 }
78 });
79
80 test("a sleeping forge that never said reads claimed or not from its own tables", async () => {
81 const { app, dir, asked } = parentWith(["taken", "open"]);
82 try {
83 for (const name of ["taken", "open"]) {
84 const child = createApp({ dataDir: join(dir, "children", `cell-${name}`), url: `https://${name}--forge.test`, port: 0 });
85 if (name === "taken") {
86 const made = await createUser(child.db, { origin: "seed", trusted: true, username: "maria", email: "[email protected]", isAdmin: true });
87 if (!made.ok) throw new Error("user");
88 }
89 child.close();
90 }
91 useSleepers(app, () => true);
92 const rows = await clientRows(app);
93 expect(asked).toEqual([]);
94 expect(claimState(rows.find((r) => r.name === "taken")!)).toBe("yes");
95 expect(claimState(rows.find((r) => r.name === "open")!)).toBe("no");
96 } finally {
97 app.close();
98 }
99 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs