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

The terminal's repository screen makes an app you can see your own with `Y`, planned first, as MCP `make_it_yours` does #7284

Parked opened this
Closed without merging

torque/forge:torque/7270-the-terminal-s-repository-screen-makes-a → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
7 files changed +404 −28
docs/design/sections/6b-workspace.md +2 −1
@@ -67,7 +67,8 @@ move between tabs, `x` / `X` close one or the others, `c` commits, `b` lists
6767 branches and tags, `B` creates a branch, `t` (or `ctrl+p`) is the fuzzy finder,
6868 `y` copies the path, `i` / `p` / `C` open issues, pulls and commits, `I` shows
6969 the about card, `W` swaps to the other side of a mirror, `O` opens the file on
70 its origin, `P` pins, `S` syncs, `F` forks, `,` is settings, `r` refreshes and
70 its origin, `P` pins, `S` syncs, `F` forks, `Y` makes an app you can see
71 yours (a plan first, a copy only after `y`), `,` is settings, `r` refreshes and
7172 `E` hands the file to `$FORGE_EDITOR`.
7273
7374 **The tab bar.** One row above the panes: the path, the active tab bold and
src/actions/make_it_yours.ts +27
@@ -24,6 +24,9 @@
2424 */
2525
2626 import { storageLimitMessage } from "../core/storage.ts";
27 import { NOT_A_MEMBER } from "../core/roles.ts";
28 import { APP_ALREADY_YOURS } from "./origin_app_words.ts";
29 import { PROVE_FIRST } from "./verify_words.ts";
2730 import type { App } from "../app.ts";
2831 import {
2932 getAppByRepo,
@@ -96,6 +99,30 @@ export const NOT_COPIED = [
9699 export const COPIES_PER_DAY = FORKS_PER_DAY;
97100 const DAY_MS = 86_400_000;
98101
102 /**
103 * What a refusal says, in the words every door that is not HTTP uses: the
104 * MCP tool and the terminal (#7270) both answer from here, so a person
105 * reading either is told the same thing.
106 */
107 export function makeItYoursRefusal(e: MakeItYoursError): string {
108 switch (e.kind) {
109 case "limit":
110 return `at most ${COPIES_PER_DAY} copies a day; another after ${e.resetsAt}`;
111 case "repository_limit":
112 case "storage_limit":
113 case "invalid":
114 return e.message;
115 case "own":
116 return APP_ALREADY_YOURS;
117 case "not_member":
118 return NOT_A_MEMBER;
119 case "unverified":
120 return PROVE_FIRST;
121 default:
122 return "no such app or repository (owner/name)";
123 }
124 }
125
99126 /** The account's copies in the last 24 hours, oldest first. */
100127 const copiesToday = (app: App, ownerId: string, now: number): string[] =>
101128 forksSince(app.db, ownerId, new Date(now - DAY_MS).toISOString());
src/http/mcp/tools/make_it_yours.ts +5 −19
@@ -1,7 +1,4 @@
1 import { APP_ALREADY_YOURS } from "../../../actions/origin_app_words.ts";
2 import { NOT_A_MEMBER } from "../../../core/roles.ts";
3 import { COPIES_PER_DAY, makeItYours } from "../../../actions/make_it_yours.ts";
4 import { PROVE_FIRST } from "../../../actions/verify_words.ts";
1 import { makeItYours, makeItYoursRefusal } from "../../../actions/make_it_yours.ts";
52 import { good, bad, type Tool, str, REPO_ARG, repoArg, isAnswer } from "../shared.ts";
63 import { VISIBILITY_WORDS } from "../../../core/arg_words.ts";
74
@@ -35,21 +32,10 @@ export const tool: Tool = {
3532 ...(visibility === "" ? {} : { visibility }),
3633 });
3734 if (!made.ok)
38 return made.error.kind === "limit"
39 ? bad("limit", `at most ${COPIES_PER_DAY} copies a day; another after ${made.error.resetsAt}`)
40 : made.error.kind === "repository_limit"
41 ? bad("repository_limit", made.error.message)
42 : made.error.kind === "storage_limit"
43 ? bad("storage_limit", made.error.message)
44 : made.error.kind === "invalid"
45 ? bad("invalid", made.error.message)
46 : made.error.kind === "own"
47 ? bad("own", APP_ALREADY_YOURS)
48 : made.error.kind === "not_member"
49 ? bad("not_member", NOT_A_MEMBER)
50 : made.error.kind === "unverified"
51 ? bad("unverified", PROVE_FIRST)
52 : bad("not_found", "no such app or repository (owner/name)");
35 return bad(
36 made.error.kind === "unauthorized" ? "not_found" : made.error.kind,
37 makeItYoursRefusal(made.error),
38 );
5339 const { made: done, ...plan } = made.value;
5440 return good(
5541 done === undefined ? plan : { ...plan, url: `/${done.repo.ownerName}/${done.repo.name}` },
src/tui/screens/workspace.ts +10 −1
@@ -120,7 +120,7 @@ import { noteCommands } from "./notes.ts";
120120 import { reportCommands } from "./report.ts";
121121 import { tellCommands } from "./tell.ts";
122122 import { workingCommands } from "./working.ts";
123 import { yoursCommands } from "./yours.ts";
123 import { hasApp, makeItYoursScreen, yoursCommands } from "./yours.ts";
124124 import { externalEditor, editorHint, hasExternalEditor } from "./external.ts";
125125 import { finderScreen } from "./finder.ts";
126126 import { issuesScreen } from "./issues.ts";
@@ -1333,6 +1333,9 @@ export function workspaceScreen(
13331333 { disabled: !f.signedIn },
13341334 ),
13351335 ]),
1336 ...(repo !== undefined && f.user !== undefined && repo.ownerId !== f.user.id && hasApp(f, repo)
1337 ? [binding(["Y"], "make it yours: a copy that follows it")]
1338 : []),
13361339 ...(repo === undefined
13371340 ? []
13381341 : [
@@ -1734,6 +1737,12 @@ export function workspaceScreen(
17341737 else if (repo !== undefined) ctx.push(forkScreen(f, ctx, repo));
17351738 return true;
17361739 }
1740 // On a repository of your own it still opens, to say so in the
1741 // words MCP `make_it_yours` uses; it is listed only where it copies.
1742 if (plain && e.key === "Y" && repo !== undefined && f.signedIn && hasApp(f, repo)) {
1743 ctx.push(makeItYoursScreen(f, ctx, repo));
1744 return true;
1745 }
17371746 if (plain && e.key === "," && repo !== undefined && admin()) {
17381747 openOnWeb(
17391748 f,
src/tui/screens/yours.ts +181 −6
@@ -1,14 +1,40 @@
11 /**
2 * Make it yours, from a repository screen's `:` palette (#810): the
3 * terminal's door to the toolbar's button, for whoever cannot write the
4 * repository. It is the fork screen (#606) the `F` key opens.
2 * Make it yours in the terminal: the repository screen's `Y` (#7270) and its
3 * `:` palette row (#810), for whoever finds an app worth starting from.
4 *
5 * The screen is the action's (`actions/make_it_yours.ts`), the same one the
6 * page and MCP `make_it_yours` answer from: it asks for a name (the
7 * original's by default) and who sees the copy, shows the plan — what is
8 * copied, what is not, that it follows the original — re-planned on every
9 * change, and copies only after `enter` and then `y`. Every refusal is said
10 * in the MCP tool's words (`makeItYoursRefusal`). `esc` leaves with nothing
11 * copied. A repository with no app has nothing to make yours; its palette
12 * row is the plain fork screen.
513 */
614
15 import {
16 COPIES_PER_DAY,
17 makeItYours,
18 makeItYoursRefusal,
19 type MakeItYoursPlan,
20 } from "../../actions/make_it_yours.ts";
21 import { getAppByRepo } from "../../core/apps.ts";
722 import { canWrite, type Repo } from "../../core/repos.ts";
8 import type { Ctx } from "../app/app.ts";
23 import type { Ctx, Screen } from "../app/app.ts";
24 import { binding } from "../app/keys.ts";
925 import type { Command } from "../app/palette.ts";
26 import { style } from "../term/style.ts";
27 import { truncate } from "../term/width.ts";
28 import { field } from "../ui/widgets.ts";
1029 import type { Session } from "../session.ts";
30 import { runAsync } from "./common.ts";
1131 import { forkScreen } from "./newrepo.ts";
32 import { repoScreen } from "./repo.ts";
33 import { local } from "./where.ts";
34
35 /** True when `repo` has an app a signed-in person could make theirs. */
36 export const hasApp = (f: Session, repo: Repo): boolean =>
37 getAppByRepo(f.db, repo.id) !== undefined;
1238
1339 export function yoursCommands(f: Session, ctx: Ctx, repo: Repo | undefined): Command[] {
1440 if (repo === undefined || f.user === undefined || canWrite(f.db, repo, f.user)) return [];
@@ -16,8 +42,157 @@ export function yoursCommands(f: Session, ctx: Ctx, repo: Repo | undefined): Com
1642 {
1743 id: "make-it-yours",
1844 title: `${repo.ownerName}/${repo.name} — Make it yours`,
19 hint: "fork it",
20 run: () => ctx.push(forkScreen(f, ctx, repo)),
45 hint: hasApp(f, repo) ? "a copy that follows it" : "fork it",
46 run: () =>
47 ctx.push(hasApp(f, repo) ? makeItYoursScreen(f, ctx, repo) : forkScreen(f, ctx, repo)),
2148 },
2249 ];
2350 }
51
52 export function makeItYoursScreen(f: Session, ctx: Ctx, source: Repo): Screen {
53 let name = source.name;
54 let visibility: "public" | "private" = "private";
55 let focus: 0 | 1 = 0;
56 let confirming = false;
57 let making = false;
58 let plan: MakeItYoursPlan | undefined;
59 let refusal: string | undefined;
60 // A plan answered for an older name is dropped: only the latest is drawn.
61 let asked = 0;
62 const from = `${source.ownerName}/${source.name}`;
63
64 const replan = async (): Promise<void> => {
65 const n = ++asked;
66 const made = await makeItYours(f.app, f.user, source, { dryRun: true, name, visibility });
67 if (n !== asked) return;
68 plan = made.ok ? made.value : undefined;
69 refusal = made.ok ? undefined : makeItYoursRefusal(made.error);
70 ctx.invalidate();
71 };
72 const edited = (): void => {
73 confirming = false;
74 runAsync(ctx, replan);
75 };
76
77 const make = (): void => {
78 making = true;
79 runAsync(ctx, async () => {
80 const made = await makeItYours(f.app, f.user, source, { dryRun: false, name, visibility });
81 making = false;
82 confirming = false;
83 if (!made.ok) {
84 refusal = makeItYoursRefusal(made.error);
85 ctx.toast(refusal, "danger");
86 ctx.invalidate();
87 return;
88 }
89 const copy = made.value.made;
90 if (copy === undefined) return;
91 ctx.toast(`${copy.repo.ownerName}/${copy.repo.name} is yours`, "success");
92 ctx.replace(repoScreen(f, ctx, local(f, copy.repo)));
93 });
94 };
95
96 return {
97 title: "Make it yours",
98 get keys() {
99 return confirming
100 ? [binding(["y"], plan?.existing ? "open your copy" : "copy it"), binding(["n"], "back"), binding(["esc"], "leave, nothing copied")]
101 : [
102 binding(["tab"], "name / visibility"),
103 binding(["space"], "toggle visibility"),
104 binding(["enter"], "confirm the plan"),
105 binding(["esc"], "leave, nothing copied"),
106 ];
107 },
108 async load() {
109 await replan();
110 },
111 render(area, s) {
112 const c = ctx.theme.c;
113 const w = area.w;
114 const right = area.x + w;
115 let y = area.y;
116 const line = (text: string, color = c.fg): void => {
117 if (y >= area.y + area.h) return;
118 s.text(area.x, y, truncate(text, w), style(color, c.bg), right);
119 y++;
120 };
121 line(`Make ${from} yours, as ${f.user?.username ?? "?"}`);
122 y++;
123 const cursor = field(s, { x: area.x, y, w: Math.min(60, w), h: 3 }, c, {
124 label: "Name",
125 value: name,
126 focused: focus === 0 && !confirming,
127 ...(focus === 0 && !confirming ? { cursor: [...name].length } : {}),
128 hint: "Letters, digits, dot, dash, underscore.",
129 });
130 ctx.setCursor(cursor);
131 y += 4;
132 line(
133 `visibility ${visibility}${source.visibility === "public" ? "" : " (the original is private)"}`,
134 focus === 1 && !confirming ? c.accent : c.mutedFg,
135 );
136 y++;
137 if (refusal !== undefined) {
138 line(`not copied: ${refusal}`, c.danger);
139 return;
140 }
141 if (plan === undefined) {
142 line("planning…", c.faint);
143 return;
144 }
145 line("The plan", c.mutedFg);
146 if (plan.existing) line(` you already have ${plan.repo}; nothing new is copied`);
147 else line(` copy to ${plan.repo} (app ${plan.slug}, ${plan.visibility})`);
148 line(` copied ${plan.copied.join(", ")}`);
149 line(` not copied ${plan.notCopied.join(", ")}`);
150 line(` follows ${plan.from}: moves when its default branch moves, until the copy has work of its own`);
151 line(` copies left ${plan.copiesLeft} of ${COPIES_PER_DAY} today`);
152 y++;
153 if (making) line("copying…", c.faint);
154 else if (confirming)
155 line(plan.existing ? `y opens ${plan.repo} · n back · esc leaves` : `y copies ${from} to ${plan.repo} · n back · esc leaves, nothing copied`, c.accent);
156 else line("enter to confirm · esc leaves, nothing copied", c.faint);
157 },
158 paste(text) {
159 if (focus !== 0 || confirming) return true;
160 name += text.replace(/\s+/g, "");
161 edited();
162 return true;
163 },
164 key(e) {
165 if (e.key === "esc") return false;
166 if (making) return true;
167 if (confirming) {
168 if (e.key === "y") make();
169 else if (e.key === "n") confirming = false;
170 return true;
171 }
172 if (e.key === "tab") {
173 focus = focus === 0 ? 1 : 0;
174 return true;
175 }
176 if (e.key === "enter") {
177 if (plan !== undefined && refusal === undefined) confirming = true;
178 return true;
179 }
180 if (focus === 1 && e.key === " ") {
181 visibility = visibility === "private" ? "public" : "private";
182 edited();
183 return true;
184 }
185 if (focus === 0 && e.key === "backspace") {
186 name = [...name].slice(0, -1).join("");
187 edited();
188 return true;
189 }
190 if (focus === 0 && !e.ctrl && !e.alt && [...e.key].length === 1) {
191 name += e.key;
192 edited();
193 return true;
194 }
195 return focus === 0 && !e.ctrl && !e.alt;
196 },
197 };
198 }
test/tui-make-it-yours.test.ts added +178
@@ -0,0 +1,178 @@
1 /**
2 * Make it yours from the terminal's repository screen (#7270): `Y` plans
3 * first — name, visibility, what is copied and what is not, that it follows
4 * the original — copies only after `enter` then `y`, refuses in the words
5 * MCP `make_it_yours` uses, and opens the copy. `esc` copies nothing.
6 */
7 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
8 import { mkdtemp, rm } from "node:fs/promises";
9 import { tmpdir } from "node:os";
10 import { join } from "node:path";
11 import { newApp } from "../src/actions/apps.ts";
12 import { COPIES_PER_DAY, makeItYours } from "../src/actions/make_it_yours.ts";
13 import { APP_ALREADY_YOURS } from "../src/actions/origin_app_words.ts";
14 import { createApp, type App } from "../src/app.ts";
15 import { getAppByRepo } from "../src/core/apps.ts";
16 import { getRepo, type Repo } from "../src/core/repos.ts";
17 import { createUser, type User } from "../src/core/users.ts";
18 import { createForgeApp } from "../src/tui/forge.ts";
19 import { repoScreen } from "../src/tui/screens/repo.ts";
20 import { local } from "../src/tui/screens/where.ts";
21 import { headlessTerminal } from "../src/tui/term/terminal.ts";
22 import { quietLogs } from "./helpers/globals.ts";
23 import { promote } from "./helpers/members.ts";
24 import { settleApp, settleUntil } from "./helpers/tui.ts";
25 import { proveAll } from "./helpers/verified.ts";
26
27 quietLogs();
28
29 let dir: string;
30 let app: App;
31 let alice: User;
32 let garden: Repo;
33
34 const mk = async (username: string, first = false): Promise<User> => {
35 const r = await createUser(app.db, {
36 origin: "seed",
37 trusted: true,
38 username,
39 email: `${username}@forge.local`,
40 password: "a-good-passphrase",
41 adminIfFirst: first,
42 });
43 if (!r.ok) throw new Error(r.error);
44 promote(app.db, r.value);
45 proveAll(app);
46 return r.value;
47 };
48
49 /** `user` in the terminal on `repo`'s screen; `press` dispatches keys and settles. */
50 async function open(user: User, repo: Repo) {
51 const terminal = headlessTerminal(140, 40);
52 const { app: tui, session } = createForgeApp(
53 app,
54 { sshUsername: user.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user },
55 { terminal, exitHooks: false },
56 );
57 tui.start();
58 tui.ctx.push(repoScreen(session, tui.ctx, local(session, repo)));
59 await settleApp(tui);
60 const press = async (...keys: string[]): Promise<void> => {
61 for (const key of keys)
62 tui.dispatch({ type: "key", key, ctrl: false, alt: false, shift: false });
63 await settleApp(tui);
64 };
65 return { tui, press, text: () => tui.screen.toText() };
66 }
67
68 beforeAll(async () => {
69 dir = await mkdtemp(join(tmpdir(), "forge-tui-yours-"));
70 app = createApp({ dataDir: dir, port: 0 });
71 alice = await mk("alice", true);
72 const made = await newApp(app, alice, { name: "garden", description: "A garden planner", visibility: "public" });
73 if (!made.ok) throw new Error(String(made.error));
74 garden = made.value.repo;
75 }, 60_000);
76
77 afterAll(async () => {
78 app.db.close();
79 await rm(dir, { recursive: true, force: true });
80 });
81
82 describe("Y on the repository screen", () => {
83 test("plans first, copies only after a confirm, and opens the copy", async () => {
84 const bob = await mk("bob");
85 const h = await open(bob, garden);
86 try {
87 await h.press("?");
88 expect(h.text()).toContain("make it yours: a copy that follows it");
89 await h.press("esc", "Y");
90 await settleUntil(h.tui, "The plan");
91 const plan = h.text();
92 expect(plan).toContain("Make alice/garden yours");
93 expect(plan).toContain("bob/garden (app garden-bob, private)");
94 expect(plan).toContain("the code, its history");
95 expect(plan).toContain("its variables and secrets");
96 expect(plan).toContain("follows alice/garden");
97 expect(plan).toContain(`${COPIES_PER_DAY} of ${COPIES_PER_DAY} today`);
98 // A name of one's own re-plans; the plan is not the copy.
99 await h.press("backspace", "backspace", "backspace", "backspace", "backspace", "backspace", "p", "l", "o", "t");
100 await settleUntil(h.tui, "bob/plot (app plot-bob");
101 await h.press("enter");
102 expect(h.text()).toContain("y copies alice/garden to bob/plot");
103 expect(getRepo(app.db, "bob", "plot")).toBeUndefined();
104 await h.press("y");
105 await settleUntil(h.tui, "bob/plot");
106 const copy = getRepo(app.db, "bob", "plot");
107 expect(copy?.forkParentId).toBe(garden.id);
108 expect(getAppByRepo(app.db, copy!.id)?.slug).toBe("plot-bob");
109 expect(h.text()).not.toContain("The plan");
110 } finally {
111 h.tui.stop();
112 }
113 }, 30_000);
114
115 test("esc leaves with nothing copied", async () => {
116 const carol = await mk("carol");
117 const h = await open(carol, garden);
118 try {
119 await h.press("Y");
120 await settleUntil(h.tui, "The plan");
121 await h.press("enter");
122 expect(h.text()).toContain("y copies");
123 await h.press("esc");
124 expect(h.text()).not.toContain("The plan");
125 expect(getRepo(app.db, "carol", "garden")).toBeUndefined();
126 } finally {
127 h.tui.stop();
128 }
129 }, 30_000);
130
131 test("a sixth copy today is refused in MCP's words, and nothing is made", async () => {
132 const dave = await mk("dave");
133 for (let n = 0; n < COPIES_PER_DAY; n++) {
134 const src = await newApp(app, alice, { name: `bed-${n}`, visibility: "public" });
135 if (!src.ok) throw new Error("bed");
136 const made = await makeItYours(app, dave, src.value.repo, { dryRun: false });
137 expect(made.ok).toBe(true);
138 }
139 const h = await open(dave, garden);
140 try {
141 await h.press("Y");
142 await settleUntil(h.tui, `0 of ${COPIES_PER_DAY} today`);
143 await h.press("enter", "y");
144 await settleUntil(h.tui, `at most ${COPIES_PER_DAY} copies a day; another after`);
145 expect(getRepo(app.db, "dave", "garden")).toBeUndefined();
146 } finally {
147 h.tui.stop();
148 }
149 }, 60_000);
150
151 test("already yours, and a private original made public, are refused in MCP's words", async () => {
152 const own = await open(alice, garden);
153 try {
154 await own.press("?");
155 expect(own.text()).not.toContain("make it yours");
156 await own.press("esc", "Y");
157 await settleUntil(own.tui, APP_ALREADY_YOURS);
158 await own.press("enter", "y");
159 expect(own.text()).not.toContain("copies");
160 } finally {
161 own.tui.stop();
162 }
163 const erin = await mk("erin");
164 const vault = await newApp(app, erin, { name: "vault", visibility: "private" });
165 if (!vault.ok) throw new Error("vault");
166 const h = await open(alice, vault.value.repo);
167 try {
168 await h.press("Y");
169 await settleUntil(h.tui, "The plan");
170 await h.press("tab", " ");
171 await settleUntil(h.tui, "a copy of a private repository stays private");
172 await h.press("enter", "y");
173 expect(getRepo(app.db, "alice", "vault")).toBeUndefined();
174 } finally {
175 h.tui.stop();
176 }
177 }, 30_000);
178 });
test/verify-gate-words.test.ts +1 −1
@@ -16,7 +16,7 @@ const ROOT = join(import.meta.dir, "..");
1616 const DOORS = [
1717 "src/http/api.ts",
1818 "src/http/mcp/tools/new_app.ts",
19 "src/http/mcp/tools/make_it_yours.ts",
19 "src/actions/make_it_yours.ts",
2020 "src/http/respond.ts",
2121 "src/actions/tell.ts",
2222 "src/actions/verify.ts",

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs