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

A new blank forge deploys its own fork of the forge as an app and photographs it: one core and 1.5 GiB for its first four minutes #13825

Merged opened this in answer to

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

Merged

torque/forge:torque/13817-a-new-blank-forge-deploys-its-own-fork-o → main 2 commits fork 1 thread · 1 unresolved

  • Merged. torque/13817-a-new-blank-forge-deploys-its-own-fork-o was merged into main as f243050.

Review

Sign in to review this change.
3 files changed +73 −0
docs/features/new-forge-first-minutes.md +1
@@ -6,6 +6,7 @@ For site admins. A forge started in its own cell (a child forge) is treated gent
66 - **No idle sleep.** The idle sweep leaves a forge alone for 10 minutes after its start (`NEW_FORGE_MS` in `src/spawn/cell.ts`).
77 - **A minute to start.** The parent waits about 60 seconds (`START_PROBES`) for a first start to answer. A start that never answers is asked to stop as a sleep is (SIGTERM first), so a slow boot is not written down as a crash.
88 - **The boots proof.** The forge's reachability proofs include `boots`: it fails when a start in the first ten minutes of the child's record never exited cleanly. It is read from the child's own `boots.json`, and an early unclean start stops counting an hour after it happened, so the alarm clears on its own.
9 - **Its own code is not an app at first boot.** The fork of the forge's code that a spawn makes (`<org>/forge`, the one `parent.json`'s `forgeCode` names) is created and visible, but is not registered as an app, so an admin who sees no app for it has not lost it. Its forge.json declares an app, and running it (with Chromium for its looks) is meant to spare a new forge about a core and 1.5 GiB for its first minutes; that is the intent, not a measured figure. Only that recorded fork is skipped: a repository someone else called `forge` is not, and becomes an app like any other whose tip declares one. To turn it on, the repository's owner uses the app settings page on the web, `/<org>/forge/settings/app`, or the API, `POST /api/v1/repos/<org>/forge/app`; both run `enableApp`. The terminal has no screen for making an existing repository an app, so there it is the web or API. Code: `ownForkName` in `src/spawn/first-boot.ts`. Test: `test/spawn-child-apps.test.ts`.
910 - **Where a person sees it.** On the web, `/forges` (Forges you sent); in the terminal, the "Forges you sent" screen. Each forge's line there reads `Running, not reachable: boots 1 unclean restart in the first ten minutes` while the proof fails, next to any other failing proof. A passing proof adds no words. The proof is read when reachability is next swept (every five minutes).
1011 - **What the proof cannot see.** The newest start is left out: from outside, a start that is still running cannot be told from one that crashed. So a crash with no start after it counts only once the next start is written, for example when the forge next wakes. The window counts from the forge's very first start (`firstAt` in `boots.json`), even after the ledger keeps only its last 50 starts.
1112
src/spawn/first-boot.ts +24
@@ -25,10 +25,25 @@ import { getUserSetting, setUserSetting } from "../core/settings.ts";
2525 import { ensureDirectionIssue } from "../crew/autonomy.ts";
2626 import { resolveOid } from "../git/merge.ts";
2727 import { repoDir } from "../git/repos.ts";
28 import { join } from "node:path";
29 import { readPrivate } from "../kernel/file_modes.ts";
2830 import { spawnedOrg } from "./claim.ts";
31 import { FORGE_CODE_NAME } from "./forge_code.ts";
2932
3033 const BOOTED = "spawn_first_boot";
3134
35 /** The name of the fork of the forge's code the spawn recorded in `parent.json`, if it did. */
36 function ownForkName(app: App, org: string): string | undefined {
37 try {
38 const doc = JSON.parse(readPrivate(join(app.config.dataDir, "parent.json")) ?? "") as {
39 forgeCode?: { repo?: unknown } | null;
40 };
41 return doc.forgeCode?.repo === `${org}/${FORGE_CODE_NAME}` ? FORGE_CODE_NAME : undefined;
42 } catch {
43 return undefined;
44 }
45 }
46
3247 export async function firstBoot(app: App): Promise<void> {
3348 const spawned = await spawnedOrg(app);
3449 if (spawned === undefined) return;
@@ -46,7 +61,16 @@ export async function firstBoot(app: App): Promise<void> {
4661 }).sort((a, b) => a.name.localeCompare(b.name));
4762 // Each repository the child was given is an app here when its tip says it
4863 // is one — the rule the runtime itself deploys by (`parseManifest`).
64 // Except the fork the spawn made of the forge's own code (the one
65 // `parent.json`'s `forgeCode` names): its forge.json declares an app, and
66 // running it (plus Chromium for its looks) cost a new forge a core and
67 // 1.5 GiB for minutes (#13817). A repository a person called `forge` is
68 // not that fork and is treated as any other. The owner enables the fork
69 // from its settings page, `/<org>/forge/settings/app`, or the API,
70 // `POST /api/v1/repos/<org>/forge/app` (both `enableApp`).
71 const forkName = ownForkName(app, owner.username);
4972 for (const repo of repos) {
73 if (repo.name === forkName) continue;
5074 if (getAppByRepo(app.db, repo.id) !== undefined) continue;
5175 const dir = repoDir(app.reposRoot, repo.ownerName, repo.name);
5276 const tip = await resolveOid(dir, `refs/heads/${repo.defaultBranch}`);
test/spawn-child-apps.test.ts +48
@@ -8,6 +8,8 @@
88 import { setLimit } from "../src/settings/limits.ts";
99 import { seatAs } from "./helpers/seat.ts";
1010 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
11 import { readFileSync, writeFileSync } from "node:fs";
12 import { enableApp } from "../src/actions/apps.ts";
1113 import { mkdtemp, rm } from "node:fs/promises";
1214 import { tmpdir } from "node:os";
1315 import { join } from "node:path";
@@ -166,6 +168,52 @@ describe("a spawned child's apps", () => {
166168 ).toBeUndefined();
167169 });
168170
171 async function forgeRepoWithApp(): Promise<void> {
172 const made = await createRepo(child, getUserByName(child.db, "maria")!, {
173 owner: "marias",
174 name: "forge",
175 visibility: "public",
176 });
177 if (!made.ok) throw new Error("repo");
178 const c = await commitFiles(repoDir(child.reposRoot, "marias", "forge"), {
179 branch: "main",
180 message: "forge",
181 files: [{ path: "forge.json", content: FORGE_JSON }],
182 author,
183 });
184 if (!c.ok) throw new Error("commit " + JSON.stringify(c));
185 }
186 const parentFile = (): string => join(child.config.dataDir, "parent.json");
187 const withForgeCode = (record: unknown): void => {
188 const doc = JSON.parse(readFileSync(parentFile(), "utf8")) as Record<string, unknown>;
189 writeFileSync(parentFile(), JSON.stringify({ ...doc, forgeCode: record }));
190 };
191 const reboot = async (): Promise<void> => {
192 child.db.query("DELETE FROM user_setting WHERE key = 'spawn_first_boot'").run();
193 await firstBoot(child);
194 };
195
196 test("the fork of the forge's own code is not made an app at birth (#13817)", async () => {
197 await forgeRepoWithApp();
198 withForgeCode({ repo: "marias/forge" });
199 await reboot();
200 const forge = getRepo(child.db, "marias", "forge");
201 expect(forge).toBeDefined();
202 expect(getAppByRepo(child.db, forge!.id)).toBeUndefined();
203 expect(getAppByRepo(child.db, getRepo(child.db, "marias", "bakery")!.id)).toBeDefined();
204 // The door the docs name accepts it: the owner can enable it afterwards.
205 const owner = getUserByName(child.db, "maria")!;
206 expect(enableApp(child, owner, forge!).ok).toBe(true);
207 });
208
209 test("a repository someone else called forge is an app like any other", async () => {
210 withForgeCode(undefined);
211 const forge = getRepo(child.db, "marias", "forge")!;
212 child.db.query("DELETE FROM app WHERE repo_id = ?").run(forge.id);
213 await reboot();
214 expect(getAppByRepo(child.db, forge.id)).toBeDefined();
215 });
216
169217 test("a second boot registers nothing twice", async () => {
170218 const before = listApps(child.db).length;
171219 await firstBoot(child);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs