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

New app copies a template repository's tip (readable by the caller, capped, no symlinks), with a door to name it #10439

Parked opened this
Closed without merging

torque/forge:torque/10421-new-app-copies-a-template-repository-s-t → main 16 commits fork 4 threads · 4 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
17 files changed +474 −12
docs/CLI.md +1
@@ -353,6 +353,7 @@ Flags:
353353
354354 - `--private` — only you can see it (public otherwise).
355355 - `--owner org` — make it under an organization you belong to.
356 - `--template owner/name` — start from the tip of a repository you can read instead of the built-in starter (one commit, no history; symlinks and oversized templates are refused).
356357 - `--root DIR` — the library folder to clone into.
357358
358359 ```console
docs/features/app-template.md added +23
@@ -0,0 +1,23 @@
1 # App templates
2
3 `POST /api/v1/apps`, the `new_app` MCP tool and `forge new --template` take
4 `template: "owner/name"`. The new app is one commit holding the tracked files
5 of that repository's default branch at its tip (`src/apps/template_copy.ts`).
6
7 - The caller must be able to read the template; an unreadable repository
8 answers `template_not_found`, the same as a missing one.
9 - Capped at 500 files, 512 KiB a file, 8 MiB in all (`template_too_big`).
10 - Symbolic links, submodules and paths with `..`, `.git` or control bytes
11 refuse the whole copy (`template_unsafe`).
12 - A listing or a blob that cannot be read answers `template_unreadable` (not `template_empty`); the tree listing is read with a byte bound, so a huge tree is `template_too_big` before it is held.
13 - Over HTTP each refusal is a 422 with `error` and `message`.
14 - Without `template`, the built-in starter (`src/apps/scaffold.ts`) is used.
15
16 Not yet: the `apps.template` setting, `template.json` naming, the image copy
17 and the picker (see #10305).
18
19 The terminal screen (`src/tui/screens/newapp.ts`) and the web form are exempt
20 for now: both are the one-sentence path ("say what it should do"), with no
21 options field, and a template is an `owner/name` the person must already
22 know. The doors that take it are the API, MCP and `forge new --template`;
23 the picker (#10305) is where the screens get it.
scripts/unreached-allow.json −1
@@ -15,7 +15,6 @@
1515 "src/flow/review_rounds.ts",
1616 "src/flow/shared_red.ts",
1717 "src/flow/stale_fold.ts",
18 "src/kernel/why_now.ts",
1918 "src/spawn/pod.ts",
outdated

Unrelated to templates; drop and rebase.

2019 "src/tui/gallery.ts",
2120 "src/tui/ui/qr.ts",
src/actions/apps.ts +23 −4
@@ -6,6 +6,8 @@
66 * the repository may look at it and open its environments.
77 */
88
9 import { readTemplate, TEMPLATE_ERRORS, type TemplateError } from "../apps/template_copy.ts";
10 import type { FileWrite } from "../git/write.ts";
911 import { pullDir } from "../core/pulls.ts";
1012 import { getIssue } from "../core/issues.ts";
1113 import { mayView } from "../core/sealed.ts";
@@ -270,6 +272,11 @@ export interface NewAppInput {
270272 * also given.
271273 */
272274 pickAgent?: boolean;
275 /**
276 * `owner/name` of a repository the actor can read: its default branch's
277 * tip is copied in place of the built-in starter (`apps/template_copy.ts`).
278 */
279 template?: string;
273280 }
274281
275282 export interface NewAppResult {
@@ -285,6 +292,7 @@ export type NewAppError =
285292 | CreateRepoActionError
286293 | CreateAppError
287294 | "scaffold_failed"
295 | TemplateError
288296 | "no_such_agent"
289297 | "not_a_builder"
290298 | "app_limit";
@@ -328,6 +336,14 @@ export async function newApp(
328336 )
329337 return err("slug_taken");
330338
339 // Read the template before a repository exists to take back.
340 let copied: FileWrite[] | undefined;
341 if (input.template !== undefined && input.template.trim() !== "") {
342 const read = await readTemplate(app, user.value, input.template);
343 if (!read.ok) return read;
344 copied = read.value;
345 }
346
331347 let mark = performance.now();
332348 const lap = (name: string): void => {
333349 const now = performance.now();
@@ -344,10 +360,12 @@ export async function newApp(
344360 if (!repo.ok) return repo;
345361 const committed = await commitToBranch(app, user.value, repo.value, {
346362 message: `Start ${input.name}`,
347 files: scaffoldFiles({
348 name: input.name,
349 description: input.description ?? "",
350 }),
363 files:
364 copied ??
365 scaffoldFiles({
366 name: input.name,
367 description: input.description ?? "",
368 }),
351369 });
352370 lap("scaffold");
353371 if (!committed.ok) {
@@ -462,6 +480,7 @@ export const NEW_APP_ERRORS: Record<string, string> = {
462480 "Another app already answers to that hostname. Pick another name.",
463481 already_app: "That repository is already an app.",
464482 scaffold_failed: "The starter app could not be committed.",
483 ...TEMPLATE_ERRORS,
465484 no_such_agent: NO_SUCH_AGENT,
466485 not_a_builder: "That agent is not a builder, so it cannot be handed a new app. Pick a builder.",
467486 };
src/apps/template_copy.ts added +139
@@ -0,0 +1,139 @@
1 /**
2 * The files a new app starts from when it names a template repository: the
3 * tracked files of that repository's default branch at its tip, as plain
4 * data for `commitToBranch` — one commit, no history.
5 *
6 * The caller must be able to read the template (`canRead`), and a repository
7 * they cannot read answers exactly as one that does not exist. The copy is
8 * capped (files, bytes per file, bytes in all) and refuses symlinks and
9 * submodules outright, so a template can neither point at a file outside
10 * itself nor smuggle a link into the new repository.
11 */
12
13 import type { App } from "../app.ts";
14 import { canRead, getRepo } from "../core/repos.ts";
15 import type { User } from "../core/users.ts";
16 import { gitEnv } from "../git/env.ts";
17 import { readerFor, refTip } from "../git/read.ts";
18 import { repoDir } from "../git/repos.ts";
19 import type { FileWrite } from "../git/write.ts";
20 import { err, ok, type Result } from "../kernel/result.ts";
21
22 export const TEMPLATE_MAX_FILES = 500;
23 export const TEMPLATE_MAX_FILE_BYTES = 512 * 1024;
24 export const TEMPLATE_MAX_TOTAL_BYTES = 8 * 1024 * 1024;
25
26 export type TemplateError =
27 | "template_invalid"
28 | "template_not_found"
29 | "template_empty"
30 | "template_unsafe"
31 | "template_too_big"
32 | "template_unreadable";
33
34 export const TEMPLATE_ERRORS: Record<TemplateError, string> = {
35 template_invalid: "A template is named owner/name.",
36 template_not_found: "No template repository by that name that you can read.",
37 template_empty: "That template repository has nothing committed yet.",
38 template_unsafe:
39 "That template holds a symbolic link, a submodule or an unsafe path, so it cannot be copied.",
40 template_too_big: "That template is larger than a new app may copy.",
41 template_unreadable: "That template could not be read just now. Try again.",
42 };
43
44 /** A listing record is a path (512) plus mode, type, oid and size: the cap on the listing is what the file cap can fill. */
45 export const TEMPLATE_MAX_LISTING_BYTES = TEMPLATE_MAX_FILES * 700;
46
47 /**
48 * `git ls-tree` output, read as it arrives and stopped (the process killed)
49 * once it passes `limit` bytes, so a huge tree is never held whole.
50 * Undefined: git failed. "big": the listing passed the limit.
51 */
52 async function listTree(dir: string, tip: string, limit: number): Promise<Uint8Array | "big" | undefined> {
53 const proc = Bun.spawn(["git", "ls-tree", "-r", "-z", "-l", tip], {
54 cwd: dir, env: gitEnv(), stdin: "ignore", stdout: "pipe", stderr: "ignore",
55 });
56 const timer = setTimeout(() => proc.kill("SIGKILL"), 20_000);
57 try {
58 const chunks: Uint8Array[] = [];
59 let seen = 0;
60 for await (const chunk of proc.stdout as unknown as AsyncIterable<Uint8Array>) {
61 seen += chunk.byteLength;
62 if (seen > limit) {
63 proc.kill("SIGKILL");
64 return "big";
65 }
66 chunks.push(chunk);
67 }
68 if ((await proc.exited) !== 0) return undefined;
outdated

Listing is read whole before the caps apply; bound its size.

69 const all = new Uint8Array(seen);
70 let at = 0;
71 for (const c of chunks) {
72 all.set(c, at);
73 at += c.byteLength;
74 }
75 return all;
76 } finally {
77 clearTimeout(timer);
78 }
79 }
80
81 const NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,99}\/[A-Za-z0-9][A-Za-z0-9._-]{0,99}$/;
82
83 /** A path git may hand us that we will write: relative, no `.`/`..`/`.git` segment, no control bytes. */
84 export function safeTemplatePath(path: string): boolean {
85 if (path === "" || path.length > 512 || path.startsWith("/")) return false;
86 if (/[\x00-\x1f\x7f\\]/.test(path)) return false;
87 return path
88 .split("/")
89 .every((s) => s !== "" && s !== "." && s !== ".." && s.toLowerCase() !== ".git");
90 }
91
92 export async function readTemplate(
93 app: App,
94 actor: User,
95 named: string,
96 ): Promise<Result<FileWrite[], TemplateError>> {
97 const spec = named.trim();
outdated

A failed blob read is not an empty template; use a distinct error.

98 if (!NAME.test(spec)) return err("template_invalid");
99 const [owner, name] = spec.split("/") as [string, string];
100 const repo = getRepo(app.db, owner, name);
101 if (!repo || !canRead(app.db, repo, actor)) return err("template_not_found");
102
103 const dir = repoDir(app.reposRoot, repo.ownerName, repo.name);
104 const tip = await refTip(dir, `refs/heads/${repo.defaultBranch}`);
105 if (tip === undefined) return err("template_empty");
106
107 const listed = await listTree(dir, tip, TEMPLATE_MAX_LISTING_BYTES);
108 if (listed === "big") return err("template_too_big");
109 if (listed === undefined) return err("template_unreadable");
110
111 const entries: { path: string; oid: string; size: number; exec: boolean }[] = [];
112 let total = 0;
113 for (const record of new TextDecoder().decode(listed).split("\0")) {
114 if (record === "") continue;
115 const tab = record.indexOf("\t");
116 if (tab === -1) return err("template_unsafe");
117 const [mode, type, oid, size] = record.slice(0, tab).split(/ +/) as [string, string, string, string];
118 const path = record.slice(tab + 1);
119 if (type !== "blob" || (mode !== "100644" && mode !== "100755")) return err("template_unsafe");
120 if (!safeTemplatePath(path)) return err("template_unsafe");
121 if (!/^\d+$/.test(size)) return err("template_unreadable"); // git prints "-" or BAD for a blob it cannot size
122 const bytes = Number(size);
123 if (!Number.isInteger(bytes) || bytes > TEMPLATE_MAX_FILE_BYTES) return err("template_too_big");
124 total += bytes;
125 if (entries.length >= TEMPLATE_MAX_FILES || total > TEMPLATE_MAX_TOTAL_BYTES)
126 return err("template_too_big");
127 entries.push({ path, oid, size: bytes, exec: mode === "100755" });
128 }
129 if (entries.length === 0) return err("template_empty");
130
131 const reader = readerFor(dir);
132 const files: FileWrite[] = [];
133 for (const e of entries) {
134 const obj = await reader.object(e.oid);
135 if (!obj.ok || obj.value.type !== "blob") return err("template_unreadable");
136 files.push({ path: e.path, content: obj.value.body, ...(e.exec ? { mode: "100755" as const } : {}) });
137 }
138 return ok(files);
139 }
src/cli/api.ts +1
@@ -299,6 +299,7 @@ export class ForgeApi {
299299 description?: string;
300300 visibility: "public" | "private";
301301 owner?: string;
302 template?: string;
302303 }): Promise<ApiResult<NewApp>> {
303304 const out = await this.call<Record<string, unknown>>("/api/v1/apps", {
304305 method: "POST",
src/cli/commands/new.ts +3 −1
@@ -28,11 +28,13 @@ async function newApp(out: Out, args: Args): Promise<number> {
2828 const api = new ForgeApi(loaded.url, loaded.token);
2929 const description = words.join(" ").trim();
3030 const owner = args.value("owner");
31 const template = args.value("template");
3132 const made = await api.newApp({
3233 name,
3334 ...(description === "" ? {} : { description }),
3435 visibility: args.flag("private") ? "private" : "public",
3536 ...(owner === undefined ? {} : { owner }),
37 ...(template === undefined ? {} : { template }),
3638 });
3739 if (!made.ok) return refusal(out, made.error);
3840
@@ -73,7 +75,7 @@ export const command: Command = {
7375 name: "new",
7476 usage: {
7577 synopsis: "forge new <name> [words…]",
76 flags: "[--private] [--owner org] [--root DIR]",
78 flags: "[--private] [--owner org] [--template owner/name] [--root DIR]",
7779 },
7880 run: (out, args) => newApp(out, args),
7981 };
src/cli/main.ts +1 −1
@@ -36,7 +36,7 @@ export { EXIT };
3636 /** Every command by name, built once from `commands/index.ts`. */
3737 const TABLE = new Map(commands.map((c) => [c.name, c]));
3838
39 const VALUE_FLAGS = ["token", "interval", "root", "url", "limit", "repo", "kind", "reason", "owner", "variants", "from", "table", "file", "page"];
39 const VALUE_FLAGS = ["token", "interval", "root", "url", "limit", "repo", "kind", "reason", "owner", "template", "variants", "from", "table", "file", "page"];
4040
4141 export async function main(
4242 argv: readonly string[],
src/contract/openapi/routes/postApiV1Apps.ts +1 −1
@@ -1,7 +1,7 @@
11 import type { Route } from "../shared.ts";
22
33 /** What `POST /api/v1/apps` reads from its body, and the refusals it answers. */
4 const APP_BODY = { name: "string", owner: "string", description: "string", sentence: "string", visibility: "string", agent: "string", pick: "boolean" };
4 const APP_BODY = { name: "string", owner: "string", description: "string", sentence: "string", visibility: "string", agent: "string", pick: "boolean", template: "string" };
55 const APP_REFUSALS = {
66 unverified: "the account has not verified its email",
77 no_name: "no `name`, and the `sentence` leaves nothing to make one from",
src/http/api.ts +6
@@ -192,6 +192,7 @@ import { isPinned } from "../core/pins.ts";
192192 import { diffCommit } from "../git/diff.ts";
193193 import { runGit } from "../git/env.ts";
194194 import { appLimitWords, appsOwnedBy } from "../actions/app_limit.ts";
195 import { TEMPLATE_ERRORS, type TemplateError } from "../apps/template_copy.ts";
195196 import { getQuota } from "../settings/quotas.ts";
196197 import { isSafeRef, isSafeTreePath } from "../git/guard.ts";
197198 import { compareBranches, listBranchRows } from "../git/branches.ts";
@@ -3583,6 +3584,9 @@ export function apiRoutes(router: Router<Handler>): void {
35833584 ...(typeof input.agent === "string" ? { agent: input.agent } : {}),
35843585 // Only a real boolean asks; like the other fields, anything else is ignored.
35853586 ...(input.pick === true ? { pickAgent: true } : {}),
3587 ...(typeof input.template === "string" && input.template !== ""
3588 ? { template: input.template }
3589 : {}),
35863590 };
35873591 const onPhase = (name: string, ms: number) => notePhase(ctx.request, name, ms);
35883592 // No `name` but a `sentence`: the name is derived, as the web form and terminal do.
@@ -3624,6 +3628,8 @@ export function apiRoutes(router: Router<Handler>): void {
36243628 ctx.auth.user?.username ||
36253629 "",
36263630 );
3631 if (made.error in TEMPLATE_ERRORS)
3632 return apiError(made.error, 422, TEMPLATE_ERRORS[made.error as TemplateError]);
36273633 return respondAccess(made.error, 422);
36283634 }
36293635 return json(
src/http/mcp/tools/new_app.ts +5 −2
@@ -3,6 +3,7 @@ import { listActiveRuns } from "../../../core/runs.ts";
33 import { getUserByName } from "../../../core/users.ts";
44 import { repositoryLimitMessage } from "../../../actions/repos.ts";
55 import { NO_NAME_ERROR, newApp, newAppFromSentence, type NewAppInput } from "../../../actions/apps.ts";
6 import { TEMPLATE_ERRORS, type TemplateError } from "../../../apps/template_copy.ts";
67 import { appLimitWords } from "../../../actions/app_limit.ts";
78 import { storageLimitMessage } from "../../../core/storage.ts";
89 import { VERIFY_FIRST } from "../../../actions/verify_words.ts";
@@ -19,7 +20,7 @@ async function fromSentence(app: App, user: User, sentence: string, input: Omit<
1920
2021 export const tool: Tool = {
2122 name: "new_app",
22 doc: "`{ name?, sentence?, description?, owner?, visibility?, agent?, pick? }`: make an app from a sentence: with `sentence` and no `name` the name is derived from it (`-2`, `-3` when taken), as the web and terminal do; `name` and `sentence` together keep the name; the first issue is `description`, else `sentence`; answers `{ repo, url, issue, agent, run }` (`run` is the first run's state, or `none`) and draws the `ui://forge/app` card: name, production URL, first issue, agent, first run; refused as `POST /api/v1/apps` is (unverified, app_limit, no_such_agent, name_taken)",
23 doc: "`{ name?, sentence?, description?, owner?, visibility?, agent?, pick?, template? }` (`template`: `owner/name` of a repository you can read, whose default branch's tip is copied instead of the built-in starter): make an app from a sentence: with `sentence` and no `name` the name is derived from it (`-2`, `-3` when taken), as the web and terminal do; `name` and `sentence` together keep the name; the first issue is `description`, else `sentence`; answers `{ repo, url, issue, agent, run }` (`run` is the first run's state, or `none`) and draws the `ui://forge/app` card: name, production URL, first issue, agent, first run; refused as `POST /api/v1/apps` is (unverified, app_limit, no_such_agent, name_taken)",
2324 description:
2425 "Make an app from a sentence: a repo, production, and with `description` a first issue.",
2526 inputSchema: {
@@ -32,6 +33,7 @@ export const tool: Tool = {
3233 visibility: { type: "string", enum: ["public", "private"] },
3334 agent: { type: "string", maxLength: 39 },
3435 pick: { type: "boolean" },
36 template: { type: "string", maxLength: 200 },
3537 },
3638 },
3739 writes: true,
@@ -47,6 +49,7 @@ export const tool: Tool = {
4749 ...(visibility === "public" || visibility === "private" ? { visibility } : {}),
4850 ...(agent === undefined ? {} : { agent }),
4951 ...(args["pick"] === true ? { pickAgent: true } : {}),
52 ...(text("template") ? { template: text("template") as string } : {}),
5053 };
5154 const given = str(args["name"]).trim();
5255 const first = description ?? sentence;
@@ -70,7 +73,7 @@ export const tool: Tool = {
7073 case "storage_limit":
7174 return bad("storage_limit", storageLimitMessage(ctx.app, user.id));
7275 default:
73 return bad(made.error, made.error);
76 return bad(made.error, made.error in TEMPLATE_ERRORS ? TEMPLATE_ERRORS[made.error as TemplateError] : made.error);
7477 }
7578 }
7679 const { repo, app: record, issue } = made.value;
test/apps-api.test.ts +33 −1
@@ -12,7 +12,7 @@ import { join } from "node:path";
1212 import { createApp, type App } from "../src/app.ts";
1313 import { AppsRuntime } from "../src/apps/runtime.ts";
1414 import { openPull } from "../src/actions/pulls.ts";
15 import { commitToBranch } from "../src/actions/repos.ts";
15 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
1616 import { getRepo } from "../src/core/repos.ts";
1717 import { getUserByName } from "../src/core/users.ts";
1818 import { serve, type Servers } from "../src/http/server.ts";
@@ -233,6 +233,38 @@ printf '%s\\n' '{"is_error":false,"result":"{\\"summary\\":\\"Build it\\",\\"che
233233 }
234234 }, 60_000);
235235
236 test("POST /apps with a template copies its tip; each template refusal is a 422 in words", async () => {
237 const alice = getUserByName(app.db, "alice")!;
238 const tpl = await createRepo(app, alice, { name: "apitpl", visibility: "public" });
239 if (!tpl.ok) throw new Error(String(tpl.error));
240 await commitToBranch(app, alice, tpl.value, {
241 message: "t",
242 files: [{ path: "TEMPLATE.txt", content: "from the template\n" }],
243 });
244 const made = await post("/api/v1/apps", { name: "fromtpl", template: "alice/apitpl" }, rw);
245 expect(made.status).toBe(201);
246 const file = await fetch(`${servers.url}/api/v1/repos/alice/fromtpl/contents/TEMPLATE.txt`, {
247 headers: { authorization: `Bearer ${rw}` },
248 });
249 expect(file.status).toBe(200);
250
251 for (const [template, error] of [
252 ["alice/missing", "template_not_found"],
253 ["not a name", "template_invalid"],
254 ] as const) {
255 const res = await post("/api/v1/apps", { name: `bad-${error}`, template }, rw);
256 expect(res.status).toBe(422);
257 const body = (await res.json()) as { error: string; message: string };
258 expect(body.error).toBe(error);
259 expect(body.message.length).toBeGreaterThan(10);
260 }
261 const empty = await createRepo(app, alice, { name: "apiempty", visibility: "public" });
262 if (!empty.ok) throw new Error(String(empty.error));
263 const none = await post("/api/v1/apps", { name: "fromempty", template: "alice/apiempty" }, rw);
264 expect(none.status).toBe(422);
265 expect(((await none.json()) as { error: string }).error).toBe("template_empty");
266 });
267
236268 test("POST /apps with a sentence and no name derives the name; a repeat gets -2; neither is 422", async () => {
237269 const first = await post("/api/v1/apps", { sentence: "Keep notes" }, rw);
238270 expect(first.status).toBe(201);
test/child-quota.test.ts +1 −1
@@ -101,7 +101,7 @@ describe("child quota", () => {
101101
102102 test("room for small but not large is refused only for large", async () => {
103103 clear();
104 process.env["FORGE_CHILD_MEMORY_MIB"] = "2000";
104 process.env["FORGE_CHILD_MEMORY_MIB"] = "5000";
105105 expect(quotaRefusal(dir, "small")).toBeUndefined();
106106 expect(quotaRefusal(dir, "large")).toContain("memory limit");
107107 });
test/cli-new.test.ts +6
@@ -121,6 +121,12 @@ describe("forge new", () => {
121121 expect(bodies).toEqual([{ name: "notes", visibility: "private", owner: "acme" }]);
122122 });
123123
124 test("--template is sent as the body's template", async () => {
125 const r = await forge(["new", "notes", "--template", "acme/starter"]);
126 expect(r.code).toBe(EXIT.ok);
127 expect(bodies).toEqual([{ name: "notes", visibility: "public", template: "acme/starter" }]);
128 });
129
124130 test("no live address, none printed", async () => {
125131 live = null;
126132 const r = await forge(["new", "notes"]);
test/mcp.test.ts +20
@@ -18,6 +18,8 @@ import { openIssue } from "../src/actions/issues.ts";
1818 import {} from "../src/core/agents.ts";
1919 import { openPull } from "../src/actions/pulls.ts";
2020 import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts";
21 import { readerFor } from "../src/git/read.ts";
22 import { repoDir } from "../src/git/repos.ts";
2123 import { createApp as insertApp } from "../src/core/apps.ts";
2224 import { addComment, createIssue, listIssues } from "../src/core/issues.ts";
2325 import { addDependency } from "../src/core/dependencies.ts";
@@ -1683,6 +1685,24 @@ describe("the forge over MCP", () => {
16831685 expect(getAccountCeiling(app.db, id)).toBeUndefined();
16841686 });
16851687
1688 test("new_app with a template copies its tip; a template it cannot read is refused in words", async () => {
1689 const tpl = await createRepo(app, alice, { name: "mcptpl", visibility: "public" });
1690 if (!tpl.ok) throw new Error(String(tpl.error));
1691 await commitToBranch(app, alice, tpl.value, {
1692 message: "t",
1693 files: [{ path: "TEMPLATE.txt", content: "from the template\n" }],
1694 });
1695 const made = (await tool(rw, "new_app", { name: "mcpfromtpl", template: "alice/mcptpl" })) as any;
1696 expect(made.value.repo).toBe("alice/mcpfromtpl");
1697 const dir = repoDir(app.reposRoot, "alice", "mcpfromtpl");
1698 const file = await readerFor(dir).object("main:TEMPLATE.txt");
1699 expect(file.ok).toBe(true);
1700 expect((await readerFor(dir).object("main:forge.json")).ok).toBe(false);
1701
1702 const hidden = (await tool(rw, "new_app", { name: "mcpnone", template: "alice/nope" })) as any;
1703 expect(hidden.error).toContain("No template repository");
1704 });
1705
16861706 test("new_app makes an app from a sentence and apps reads it back", async () => {
16871707 const bot = await createAgent(app, alice, { username: "newappbot", brief: "builds" });
16881708 if (!bot.ok) throw new Error(String(bot.error));
test/spawn-pipes.test.ts +1
@@ -17,6 +17,7 @@ const PIPING: Record<string, { sites: string[]; reason: string }> = {
1717 "src/apps/contain.ts": { sites: ["stderr"], reason: "stderr awaited with the exit; stdout ignored" },
1818 "src/apps/process.ts": { sites: ["stdout+stderr", "stdout+stderr"], reason: "pump() reads stdout and stderr of the app until exit" },
1919 "src/spawn/cell.ts": { sites: ["stdout+stderr"], reason: "drain() reads the child forge's stdout and stderr to its log until exit" },
20 "src/apps/template_copy.ts": { sites: ["stdout"], reason: "git ls-tree stdout is read in chunks up to the listing cap, then the process is killed; stderr ignored" },
2021 "src/chaos/faults.ts": { sites: ["stdout"], reason: "stdout is read until the kill closes it, then cancelled; stderr is ignored" },
2122 "src/crew/claude.ts": { sites: ["stdout+stderr", "stdout"], reason: "session pumps stdout and stderr; --version awaits stdout, stderr ignored" },
2223 "src/crew/plan.ts": { sites: ["stdout"], reason: "stdout awaited with the exit; stderr ignored" },
test/template-copy.test.ts added +210
@@ -0,0 +1,210 @@
1 /**
2 * New app from a named template repository: the tip's tracked files, one
3 * commit, only when the caller can read it, capped, never a symlink.
4 */
5 import { afterAll, expect, setDefaultTimeout, test } from "bun:test";

No test for the API, MCP or CLI door, the file-count and total caps, a submodule, or template_empty.

6 import { mkdtemp, rm } from "node:fs/promises";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { newApp } from "../src/actions/apps.ts";
10 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
11 import { setRole } from "../src/actions/roles.ts";
12 import { createApp } from "../src/app.ts";
13 import {
14 readTemplate, safeTemplatePath, TEMPLATE_MAX_FILE_BYTES, TEMPLATE_MAX_FILES, TEMPLATE_MAX_LISTING_BYTES, TEMPLATE_MAX_TOTAL_BYTES,
15 } from "../src/apps/template_copy.ts";
16 import { createUser } from "../src/core/users.ts";
17 import { commitLog, readerFor } from "../src/git/read.ts";
18 import { repoDir } from "../src/git/repos.ts";
19 import { quietLogs } from "./helpers/globals.ts";
20
21 quietLogs();
22 setDefaultTimeout(30_000);
23 const dirs: string[] = [];
24 afterAll(async () => {
25 for (const d of dirs) await rm(d, { recursive: true, force: true });
26 });
27
28 async function world() {
29 const dir = await mkdtemp(join(tmpdir(), "forge-tplcopy-"));
30 dirs.push(dir);
31 const app = createApp({ dataDir: dir, port: 0 });
32 const mk = async (username: string, isAdmin = false) => {
33 const made = await createUser(app.db, {
34 origin: "seed", trusted: true, username, email: `${username}@x.test`, isAdmin,
35 });
36 if (!made.ok) throw new Error(made.error);
37 return made.value;
38 };
39 const admin = await mk("boss", true);
40 const owner = await mk("tplowner");
41 const other = await mk("someone");
42 for (const u of [owner, other]) setRole(app, admin, u.username, "member");
43 return { app, owner, other };
44 }
45
46 async function template(w: Awaited<ReturnType<typeof world>>, visibility: "public" | "private") {
47 const r = await createRepo(w.app, w.owner, { name: "tpl", visibility });
48 if (!r.ok) throw new Error(r.error);
49 const c = await commitToBranch(w.app, w.owner, r.value, {
50 message: "t",
51 files: [
52 { path: "README.md", content: "# hello\n" },
53 { path: "src/a.ts", content: "export const a = 1;\n" },
54 { path: "bin/run", content: "#!/bin/sh\n", mode: "100755" },
55 ],
56 });
57 if (!c.ok) throw new Error("commit");
58 return r.value;
59 }
60
61 test("a new app copies the template's tip as one commit", async () => {
62 const w = await world();
63 await template(w, "public");
64 const made = await newApp(w.app, w.other, { name: "mine", template: "tplowner/tpl" });
65 expect(made.ok).toBe(true);
66 if (!made.ok) return;
67 const dir = repoDir(w.app.reposRoot, "someone", "mine");
68 const reader = readerFor(dir);
69 const readme = await reader.object("main:README.md");
70 expect(readme.ok && new TextDecoder().decode(readme.value.body)).toBe("# hello\n");
71 expect((await reader.object("main:src/a.ts")).ok).toBe(true);
72 expect((await reader.object("main:forge.json")).ok).toBe(false); // not the built-in starter
73 const log = await commitLog(dir, "main", {});
74 expect(log.ok && log.value.length).toBe(1);
75 });
76
77 test("a template the caller cannot read answers as one that does not exist", async () => {
78 const w = await world();
79 await template(w, "private");
80 const hidden = await newApp(w.app, w.other, { name: "mine", template: "tplowner/tpl" });
81 const missing = await newApp(w.app, w.other, { name: "mine", template: "tplowner/nope" });
82 expect(hidden).toEqual({ ok: false, error: "template_not_found" });
83 expect(missing).toEqual(hidden);
84 // and nothing was made
85 const again = await newApp(w.app, w.other, { name: "mine" });
86 expect(again.ok).toBe(true);
87 const own = await readTemplate(w.app, w.owner, "tplowner/tpl");
88 expect(own.ok).toBe(true);
89 });
90
91 test("a malformed template name is refused", async () => {
92 const w = await world();
93 for (const t of ["x", "../etc/passwd", "a/b/c", "a/-b", "a b/c"]) {
94 const r = await readTemplate(w.app, w.owner, t);
95 expect(r).toEqual({ ok: false, error: "template_invalid" });
96 }
97 });
98
99 test("a template holding a symlink is refused", async () => {
100 const w = await world();
101 const repo = await template(w, "public");
102 const dir = repoDir(w.app.reposRoot, "tplowner", "tpl");
103 const git = (args: string[], input?: string) => {
104 const p = Bun.spawnSync(["git", ...args], {
105 cwd: dir, stdin: input === undefined ? undefined : new TextEncoder().encode(input),
106 env: { ...process.env, GIT_AUTHOR_NAME: "t", GIT_AUTHOR_EMAIL: "t@x", GIT_COMMITTER_NAME: "t", GIT_COMMITTER_EMAIL: "t@x" },
107 });
108 return p.stdout.toString().trim();
109 };
110 const target = git(["hash-object", "-w", "--stdin"], "/etc/passwd");
111 const parent = git(["rev-parse", `refs/heads/${repo.defaultBranch}`]);
112 const tree = git(["mktree"], `120000 blob ${target}\tlink\n`);
113 const commit = git(["commit-tree", tree, "-p", parent, "-m", "link"]);
114 git(["update-ref", `refs/heads/${repo.defaultBranch}`, commit]);
115 expect(await readTemplate(w.app, w.owner, "tplowner/tpl")).toEqual({ ok: false, error: "template_unsafe" });
116 expect(await newApp(w.app, w.other, { name: "mine", template: "tplowner/tpl" })).toEqual({
117 ok: false, error: "template_unsafe",
118 });
119 });
120
121 test("a file over the cap refuses the copy", async () => {
122 const w = await world();
123 const r = await createRepo(w.app, w.owner, { name: "big", visibility: "public" });
124 if (!r.ok) throw new Error(r.error);
125 await commitToBranch(w.app, w.owner, r.value, {
126 message: "big",
127 files: [{ path: "huge.bin", content: new Uint8Array(TEMPLATE_MAX_FILE_BYTES + 1) }],
128 });
129 expect(await readTemplate(w.app, w.owner, "tplowner/big")).toEqual({ ok: false, error: "template_too_big" });
130 });
131
132 test("unsafe paths are named unsafe", () => {
133 for (const p of ["../x", "a/../b", "/abs", "a//b", ".git/config", "a/.GIT/x", "a\\b", "a\nb", ""])
134 expect(safeTemplatePath(p)).toBe(false);
135 expect(safeTemplatePath("src/a.ts")).toBe(true);
136 });
137
138 /** Point a repository's default branch at a tree written by hand. */
139 function plant(w: Awaited<ReturnType<typeof world>>, name: string, tree: string, parent?: string): void {
140 const dir = repoDir(w.app.reposRoot, "tplowner", name);
141 const git = (args: string[], input?: string) => {
142 const p = Bun.spawnSync(["git", ...args], {
143 cwd: dir, stdin: input === undefined ? undefined : new TextEncoder().encode(input),
144 env: { ...process.env, GIT_AUTHOR_NAME: "t", GIT_AUTHOR_EMAIL: "t@x", GIT_COMMITTER_NAME: "t", GIT_COMMITTER_EMAIL: "t@x" },
145 });
146 return p.stdout.toString().trim();
147 };
148 const t = git(["mktree", "--missing"], tree);
149 const commit = git(["commit-tree", t, ...(parent ? ["-p", parent] : []), "-m", "x"]);
150 git(["update-ref", "refs/heads/main", commit]);
151 }
152
153 test("a repository with no commits is template_empty", async () => {
154 const w = await world();
155 const r = await createRepo(w.app, w.owner, { name: "blank", visibility: "public" });
156 if (!r.ok) throw new Error(r.error);
157 expect(await readTemplate(w.app, w.owner, "tplowner/blank")).toEqual({ ok: false, error: "template_empty" });
158 });
159
160 test("a submodule entry refuses the copy", async () => {
161 const w = await world();
162 const r = await createRepo(w.app, w.owner, { name: "sub", visibility: "public" });
163 if (!r.ok) throw new Error(r.error);
164 plant(w, "sub", `160000 commit ${"a".repeat(40)}\tvendored\n`);
165 expect(await readTemplate(w.app, w.owner, "tplowner/sub")).toEqual({ ok: false, error: "template_unsafe" });
166 });
167
168 test("more than the file cap, or more bytes in all, refuses the copy", async () => {
169 const w = await world();
170 const many = await createRepo(w.app, w.owner, { name: "many", visibility: "public" });
171 if (!many.ok) throw new Error(many.error);
172 const c = await commitToBranch(w.app, w.owner, many.value, {
173 message: "many",
174 files: Array.from({ length: TEMPLATE_MAX_FILES + 1 }, (_, i) => ({ path: `f/${i}.txt`, content: "x" })),
175 });
176 expect(c.ok).toBe(true);
177 expect(await readTemplate(w.app, w.owner, "tplowner/many")).toEqual({ ok: false, error: "template_too_big" });
178
179 const fat = await createRepo(w.app, w.owner, { name: "fat", visibility: "public" });
180 if (!fat.ok) throw new Error(fat.error);
181 const per = TEMPLATE_MAX_FILE_BYTES;
182 const n = Math.floor(TEMPLATE_MAX_TOTAL_BYTES / per) + 1;
183 const f = await commitToBranch(w.app, w.owner, fat.value, {
184 message: "fat",
185 files: Array.from({ length: n }, (_, i) => ({ path: `b${i}.bin`, content: new Uint8Array(per) })),
186 });
187 expect(f.ok).toBe(true);
188 expect(await readTemplate(w.app, w.owner, "tplowner/fat")).toEqual({ ok: false, error: "template_too_big" });
189 });
190
191 test("a listing past its byte bound stops the read as too big", async () => {
192 const w = await world();
193 const r = await createRepo(w.app, w.owner, { name: "wide", visibility: "public" });
194 if (!r.ok) throw new Error(r.error);
195 const long = "d".repeat(200);
196 const files = Array.from({ length: Math.ceil(TEMPLATE_MAX_LISTING_BYTES / 230) + 50 }, (_, i) => ({
197 path: `${long}/${i}.txt`, content: "x",
198 }));
199 await commitToBranch(w.app, w.owner, r.value, { message: "wide", files });
200 expect(await readTemplate(w.app, w.owner, "tplowner/wide")).toEqual({ ok: false, error: "template_too_big" });
201 });
202
203 test("a blob that cannot be read is template_unreadable, not empty", async () => {
204 const w = await world();
205 const r = await createRepo(w.app, w.owner, { name: "lost", visibility: "public" });
206 if (!r.ok) throw new Error(r.error);
207 plant(w, "lost", `100644 blob ${"b".repeat(40)}\tmissing.txt\n`);
208 const got = await readTemplate(w.app, w.owner, "tplowner/lost");
209 expect(got).toEqual({ ok: false, error: "template_unreadable" });
210 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs