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

A template's forge.json may name its first ask, and a new forge made from it starts with that ask as its first tell #15699

Merged opened this

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

Merged

torque/forge:torque/15664-a-template-s-forge-json-may-name-its-fir → main 0 commits fork 2 threads · 2 unresolved

  • Merged. torque/15664-a-template-s-forge-json-may-name-its-fir was merged into main as 63937a0.

Review

Sign in to review this change.
5 files changed +129 −1
docs/APPS.md +2
@@ -557,6 +557,8 @@ description). The 201 answer carries `plan: {agent, summary, confidence}` when
557557 a pick happened and `plan: null` otherwise, planning being unavailable included.
558558 `template: "owner/name"` starts the app as a copy of that repository's tip, if the caller can read it (see `docs/features/app-template.md`).
559559
560 A template repository's `forge.json` may carry `"firstAsk": "…"`: 1 to 280 characters of plain text (anything else makes the file invalid). A forge made from the template files it as its first tell on first boot, and the template's page shows it (`docs/features/first-ask.md`).
561
560562 ## How an app is declared
561563
562564 The repository says how to run itself, in `forge.json` at its root:
docs/features/first-ask.md +1
@@ -9,6 +9,7 @@ A template may start a new forge's ask box with a sentence, so the forge opens w
99 - **Declare it:** `"firstAsk": "Add a weekly menu page"` at the top level of the template's `forge.json`. A string of 1 to 280 characters of plain text (a newline is allowed); an empty, longer or non-string value makes the file invalid, and a stored value that fails the same check is ignored.
1010 - **Read once:** when a forge is made from a template repository (`template: "owner/name"`), `copyInto` reads `firstAsk` at the template's default branch and keeps it in the new forge's `parent.json` (`template.firstAsk`). Later edits to the template do not change a forge already made.
1111 - **Shown:** on the new forge's home before its first ask, the box holds the sentence, escaped and editable; leading newlines are trimmed. The terminal's "What do you want built?" prompt (`t` on the home) starts with the same text (`readyAsk`).
12 - **Filed as the first tell:** when the new forge first boots (`src/spawn/first-boot.ts`, `fileFirstAsk`), the sentence is told to its first app as its founder, so the crew is seen working on it at once, and it leaves the record so the box does not offer it twice. A refused tell (an unverified founder, nobody to direct) leaves it in the box. Test: `test/first-ask-first-tell.test.ts`.
1213 - **Nothing declared:** the box is empty, as before.
1314 - **On the template's page:** `/templates/:owner/:name` shows the sentence, escaped, above the start button; nothing when none is declared or the stored value is invalid.
1415 - Code: `src/apps/first_ask.ts`, `src/spawn/template_first_ask.ts`, `readyAsk` in `src/spawn/template_record.ts`. Tests: `test/template-first-ask.test.ts`, `test/tui-first-ask.test.ts`.
src/spawn/first-boot.ts +31 −1
@@ -20,13 +20,15 @@ import type { App } from "../app.ts";
2020 import { listCrewRoles } from "../core/agents.ts";
2121 import { getAppByRepo } from "../core/apps.ts";
2222 import { listTeamMembers, listTeams, OWNERS_TEAM } from "../core/orgs.ts";
23 import { getRepoById, listReposByOwner } from "../core/repos.ts";
23 import { getRepoById, listReposByOwner, type Repo } from "../core/repos.ts";
2424 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";
2828 import { join } from "node:path";
29 import { tell } from "../actions/tell.ts";
2930 import { readPrivate } from "../kernel/file_modes.ts";
31 import { clearFirstAsk, startedFromOf } from "./template_record.ts";
3032 import { spawnedOrg } from "./claim.ts";
3133 import { FORGE_CODE_NAME } from "./forge_code.ts";
3234
@@ -82,6 +84,8 @@ export async function firstBoot(app: App): Promise<void> {
8284 if (parseManifest(forgeJson, packageJson).ok) enableApp(app, founder, repo);
8385 }
8486
87 await fileFirstAsk(app, founder, repos.filter((r) => r.name !== forkName), repos);
88
8589 const first = repos.find((r) => r.autonomyDirection.trim() !== "");
8690 if (!first) return;
8791 const issue = ensureDirectionIssue(app, founder, first);
@@ -91,3 +95,29 @@ export async function firstBoot(app: App): Promise<void> {
9195 if (planner && issue.assigneeId === null)
9296 assignIssue(app, founder, repo, issue, planner.agent.user);
9397 }
98
99 /**
100 * A template's declared first ask (`firstAsk`, validated when read into `parent.json`) is filed as
101 * the new forge's first tell, on its first app (else on whatever repository it has), so the crew is
102 * seen working on it at once. Once: the ask leaves the record when filed, so the ask box does not
103 * offer it again. A tell that is refused (an unverified founder, nobody to direct) leaves the ask
104 * in the record, as the box's starting text. The text is data from a repository anyone may fork:
105 * it goes through `tell` like any typed sentence, never as markup.
106 */
107 async function fileFirstAsk(
108 app: App,
109 founder: Parameters<typeof tell>[1],
110 apps: readonly Repo[],
111 all: readonly Repo[],
112 ): Promise<void> {
113 const ask = startedFromOf(app.config.dataDir)?.firstAsk;
114 const target = apps[0] ?? all[0];
115 if (ask === undefined || target === undefined) return;
116 const told = await tell(app, founder, target, ask);
117 if (!told.ok) return;
118 // Filed, so the record forgets it; if it cannot, the box would offer it a second time.
119 if (!clearFirstAsk(app.config.dataDir))
120 app.log.warn("the first ask was filed but not cleared from the spawn record; the ask box offers it again", {
outdated

Hand-parsing and rewriting parent.json here duplicates the record module. Put a clearFirstAsk in template_record.ts and call it.

121 repo: `${target.ownerName}/${target.name}`,
122 });
123 }
src/spawn/template_record.ts +18
@@ -5,6 +5,7 @@
55 */
66
77 import { readFileSync } from "node:fs";
8 import { writePrivate } from "../kernel/file_modes.ts";
89 import { join } from "node:path";
910 import { firstAskProblem } from "../apps/first_ask.ts";
1011 import { DEFAULT_TEMPLATE, TEMPLATES } from "./templates.ts";
@@ -90,3 +91,20 @@ export function startedFromOf(dataDir: string): StartedFrom | undefined {
9091
9192 /** What a new forge's ask box starts with, on either door: its template's first ask, or "". */
9293 export const readyAsk = (dataDir: string): string => startedFromOf(dataDir)?.firstAsk ?? "";
94
95 /**
96 * Forget the template's first ask once it is filed, so the ask box does not offer it again.
97 * False when the record could not be read or rewritten; true when the ask is gone (or was never there).
98 */
99 export function clearFirstAsk(dataDir: string): boolean {
100 const path = join(dataDir, "parent.json");
101 try {
102 const doc = JSON.parse(readFileSync(path, "utf8")) as { template?: Record<string, unknown> | null };
103 if (typeof doc.template !== "object" || doc.template === null || !("firstAsk" in doc.template)) return true;
104 delete doc.template["firstAsk"];
105 writePrivate(path, JSON.stringify(doc));
106 return true;
107 } catch {
108 return false;
109 }
110 }
test/first-ask-first-tell.test.ts added +77
@@ -0,0 +1,77 @@
1 /**
2 * A template's `firstAsk` is filed as the new forge's first tell when the forge first boots
3 * (#15664); a forge whose template declares none boots as before.
4 */
5 import { afterAll, beforeAll, expect, test } from "bun:test";
6 import { readFileSync, writeFileSync } from "node:fs";
7 import { join } from "node:path";
8 import { firstAskProblem } from "../src/apps/first_ask.ts";
9 import { parseManifest } from "../src/apps/manifest.ts";
10 import { firstBoot } from "../src/spawn/first-boot.ts";
11 import { startedFromOf } from "../src/spawn/template_record.ts";
12 import { startedForge, type StartedForge } from "./helpers/claimed-forge.ts";
13 import { quietLogs } from "./helpers/globals.ts";
14
15 quietLogs();
16
17 let forge: StartedForge;
18 beforeAll(async () => {
19 forge = await startedForge("first-ask-first-tell-");
20 }, 60_000);
21 afterAll(() => forge.stop());
22
23 const titles = () =>
24 forge.child.db.query<{ title: string }, []>("SELECT title FROM issue ORDER BY id").all().map((r) => r.title);
25 const rebootWith = async (template: Record<string, unknown> | undefined) => {
26 const path = join(forge.child.config.dataDir, "parent.json");
27 const doc = JSON.parse(readFileSync(path, "utf8"));
28 if (template === undefined) delete doc.template;
29 else doc.template = template;
30 writeFileSync(path, JSON.stringify(doc));
31 forge.child.db.query("DELETE FROM user_setting WHERE key = 'spawn_first_boot'").run();
32 await firstBoot(forge.child);
33 };
34
35 test("too long a first ask is refused with the field named", () => {
36 expect(firstAskProblem("x".repeat(281))).toContain("firstAsk");
37 });
38
39 test("a forge booted from a template without a first ask files no tell", async () => {
40 const before = titles();
41 await rebootWith({ id: "a/b", repo: "a/b" });
42 expect(titles().filter((t) => !before.includes(t))).toEqual([]);
43 });
44
45 test("the first ask is filed as the first tell, once, and leaves the record", async () => {
46 const before = titles();
47 await rebootWith({ id: "a/b", repo: "a/b", firstAsk: "Add a <b>weekly</b> menu page" });
48 expect(titles().filter((t) => !before.includes(t))).toContain("Add a <b>weekly</b> menu page");
49 expect(startedFromOf(forge.child.config.dataDir)?.firstAsk).toBeUndefined();
50 const rows = forge.child.db
51 .query<{ author_id: string; repo_id: string }, [string]>("SELECT author_id, repo_id FROM issue WHERE title = ?")
52 .all("Add a <b>weekly</b> menu page");
53 expect(rows.length).toBe(1);
54 const org = forge.child.db.query<{ id: string }, []>("SELECT id FROM repo ORDER BY name LIMIT 1").get();
55 expect(rows[0]?.repo_id).toBe(org?.id ?? "");
56 const owner = forge.child.db.query<{ user_id: string }, []>("SELECT user_id FROM team_member LIMIT 1").get();
57 expect(rows[0]?.author_id).toBe(owner?.user_id ?? "");
58 // A second first boot files nothing more.
59 const after = titles();
60 forge.child.db.query("DELETE FROM user_setting WHERE key = 'spawn_first_boot'").run();
61 await firstBoot(forge.child);
62 expect(titles()).toEqual(after);
63 });
64
65 test("a refused tell leaves the ask in the record", async () => {
66 forge.child.db.query("UPDATE email_check SET verified_at = NULL").run();
67 const before = titles();
68 await rebootWith({ id: "a/b", repo: "a/b", firstAsk: "Add a refused page" });
69 expect(titles().filter((t) => !before.includes(t))).not.toContain("Add a refused page");
70 expect(startedFromOf(forge.child.config.dataDir)?.firstAsk).toBe("Add a refused page");
71 });
72
73 test("a forge.json with a 281-character firstAsk is invalid and names firstAsk", () => {
74 const m = parseManifest(JSON.stringify({ app: { run: "bun main.ts" }, firstAsk: "x".repeat(281) }), undefined);
75 expect(m.ok).toBe(false);
76 expect(JSON.stringify(m)).toContain("firstAsk");
77 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs