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

The terminal lists who a private app names and a writer adds or removes one after a plan, as MCP `app_members` does #7412

Parked opened this
Closed without merging

scout/forge:scout/7287-the-terminal-lists-who-a-private-app-nam → main 5 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
4 files changed +373 −0
docs/TOUR.md +7
@@ -34,6 +34,13 @@ to filter, `?` for the keys, `:` for the command palette, `t` for the theme.
3434
3535 **Secrets.** For a forge admin, `:` → *Secrets* lists sealed secret names (never values); `a` sets one, `d` removes one after a confirm, `s` changes the place. See [OPERATING](OPERATING.md#secrets-in-the-terminal).
3636
37 **Members.** `:` → *Members* → `owner/name` lists the people a private app
38 names. It is offered to a person who can write the repository, never an agent;
39 a public app, or a reader who cannot write, is refused at the prompt in MCP's
40 words. `a` names someone, `d` removes the one under the cursor; each shows who
41 is let in or out, and changes nothing until you confirm. It is MCP
42 `app_members`, with the same refusals.
43
3744 **Run logs.** In the terminal, `enter` on a run (in the runs list, a pull, an issue or the deploys) opens its log: the state line, then the last 200 lines, scrolled to the end and following new lines while the run is queued or running (`j`/`k` and the page keys scroll; scrolling up keeps your place). A log you may not read is "Not found", as `run_log` and the REST route say. `o` there prints the web address (`src/tui/screens/runlog.ts`).
3845
3946 **Milestones.** `:` → *Milestones in a repository* → `owner/name` lists a
src/tui/forge.ts +12
@@ -49,6 +49,7 @@ import { accessScreen } from "./screens/access.ts";
4949 import { mayEditTrustedHosts } from "../origins/trusted-hosts.ts";
5050 import { trustedHostsPrompt } from "./screens/trustedhosts.ts";
5151 import { flagsScreen } from "./screens/flags.ts";
52 import { appMembersScreen, membersRefusal } from "./screens/appmembers.ts";
5253 import { secretsScreen } from "./screens/secrets.ts";
5354 import { scorecardScreen } from "./screens/scorecard.ts";
5455 import { cloudScreen } from "./screens/cloud.ts";
@@ -104,6 +105,8 @@ const REPO_VERBS: readonly {
104105 title: string;
105106 /** The screen to push, or nothing when the verb hands over to the web. */
106107 open: (f: Session, ctx: Ctx, repo: Repo) => Screen | undefined;
108 /** Why this repository is not offered the verb, as the prompt says it. */
109 refuse?: (f: Session, repo: Repo) => string | undefined;
107110 }[] = [
108111 {
109112 id: "repo",
@@ -125,6 +128,13 @@ const REPO_VERBS: readonly {
125128 title: "Milestones in a repository",
126129 open: (f, ctx, repo) => limbsScreen(f, ctx, repo),
127130 },
131 {
132 id: "members",
133 title: "Members",
134 // A private app's named people: a writer who is a person; a public one has none.
135 open: (f, ctx, repo) => appMembersScreen(f, ctx, repo),
136 refuse: membersRefusal,
137 },
128138 { id: "new-issue", title: "New issue", open: newIssueScreen },
129139 { id: "new-pull", title: "New pull request", open: newPullScreen },
130140 {
@@ -192,6 +202,8 @@ export function forgeCommands(session: Session): (ctx: Ctx) => Command[] {
192202 // this check the palette would be a way around `canRead`.
193203 if (!repo || !canRead(session.db, repo, session.user))
194204 return "not found";
205 const refused = verb.refuse?.(session, repo);
206 if (refused !== undefined) return refused;
195207 // After the prompt has popped itself, or the pop takes the
196208 // screen just pushed instead (`later` in screens/common.ts).
197209 return Promise.resolve().then(() => {
src/tui/screens/appmembers.ts added +145
@@ -0,0 +1,145 @@
1 /**
2 * The people a private app names, in the terminal: the list `app_members`
3 * answers with, and for a writer who is a person `a` names someone and `d`
4 * takes the one under the cursor off. Each shows the plan (who is let in or
5 * out) and applies only after a confirm, through `setAppMembersThrough`, so
6 * its refusals — an unknown username above all — are the action's own words.
7 * An agent, or anyone who cannot write, is offered neither key.
8 */
9
10 import {
11 readAppMembers,
12 setAppMembersThrough,
13 type AppMembersChange,
14 type AppMembersError,
15 } from "../../actions/app_members.ts";
16 import { SETTING_WORDS } from "../../actions/setting_words.ts";
17 import type { Repo } from "../../core/repos.ts";
18 import type { Ctx, Screen } from "../app/app.ts";
19 import { binding } from "../app/keys.ts";
20 import { style } from "../term/style.ts";
21 import { truncate } from "../term/width.ts";
22 import type { Session } from "../session.ts";
23 import { fill, len, split } from "../ui/layout.ts";
24 import { confirmScreen, drawList, later, ListState, promptScreen } from "./common.ts";
25
26 /** The action's refusal in MCP's words. */
27 export function sayMembersError(e: AppMembersError): string {
28 return e.kind === "invalid"
29 ? e.message
30 : e.kind === "forbidden" || e.kind === "unauthorized"
31 ? SETTING_WORDS.appMembersForbidden
32 : "no such app or repository (owner/name)";
33 }
34
35 /** Why the command is not offered for this repository, or undefined when it is. */
36 export function membersRefusal(f: Session, repo: Repo): string | undefined {
37 const read = readAppMembers(f.app, f.user, repo);
38 if (!read.ok) return sayMembersError(read.error);
39 return read.value.access === "private" ? undefined : "only a private app names people";
40 }
41
42 export function appMembersScreen(f: Session, ctx: Ctx, repo: Repo): Screen {
43 const state = new ListState();
44 let note = "";
45 const current = () => readAppMembers(f.app, f.user, repo);
46 const names = (): readonly string[] => {
47 const r = current();
48 return r.ok ? r.value.members : [];
49 };
50 const mayChange = (): boolean => {
51 const r = current();
52 return r.ok && r.value.access === "private";
53 };
54
55 type Change = { readonly add: string } | { readonly remove: string };
56 /** The list a change makes of the list as it is now. */
57 const apply = (c: Change): string[] =>
58 "add" in c ? [...names(), c.add] : names().filter((n) => n !== c.remove);
59
60 /** Plan the change against the current list: the confirm that applies it, or why it cannot be. */
61 function plan(change: Change, parent: Screen): Screen | string {
62 const planned = setAppMembersThrough(f.app, f.user, repo, { members: apply(change), dryRun: true });
63 if (!planned.ok) return sayMembersError(planned.error);
64 const p: AppMembersChange = planned.value;
65 if (p.added.length === 0 && p.removed.length === 0) return "that changes nothing";
66 const shown = JSON.stringify([p.added, p.removed]);
67 return confirmScreen(ctx, parent, {
68 title: "Change who may enter?",
69 body: [
70 ...p.added.map((n) => `${n} is let in`),
71 ...p.removed.map((n) => `${n} is let out`),
72 "They enter the app and its previews, nothing more.",
73 ],
74 actions: [{ label: "Apply" }, { label: "Cancel", variant: "ghost" }],
75 selected: 1,
76 onChoose: (choice) => {
77 if (choice !== 0) return;
78 // Another writer may have edited the list since the plan: recompute
79 // from the list as it is now, and show a differing plan again.
80 const now = setAppMembersThrough(f.app, f.user, repo, { members: apply(change), dryRun: true });
81 if (!now.ok) note = sayMembersError(now.error);
82 else if (JSON.stringify([now.value.added, now.value.removed]) !== shown) {
83 const again = plan(change, parent);
84 if (typeof again === "string") note = `The list changed meanwhile. ${again}`;
85 else ctx.push(again);
86 } else {
87 const done = setAppMembersThrough(f.app, f.user, repo, { members: apply(change), dryRun: false });
88 note = done.ok ? "Done: the list is changed." : sayMembersError(done.error);
89 }
90 ctx.invalidate();
91 },
92 });
93 }
94
95 const screen: Screen = {
96 title: `Members: ${repo.ownerName}/${repo.name}`,
97 keys: [
98 binding(["j", "k"], "move cursor"),
99 binding(["a"], "name someone"),
100 binding(["d"], "remove"),
101 binding(["esc"], "back"),
102 ],
103 render(area, s) {
104 const c = ctx.theme.c;
105 const [head, list] = split(area, "v", [len(1), fill(1)]);
106 const r = current();
107 const text = !r.ok
108 ? sayMembersError(r.error)
109 : note !== ""
110 ? note
111 : r.value.members.length === 0
112 ? "No one is named. a names someone."
113 : "These people may enter this app and its previews, nothing more.";
114 s.text(head!.x, head!.y, truncate(text, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w);
115 drawList(s, list!, c, state, names().map((n) => ({ title: n })));
116 },
117 key(e) {
118 note = "";
119 if (state.key(e, names().length)) return true;
120 if (e.ctrl || e.alt || !mayChange()) return false;
121 if (e.key === "a")
122 ctx.push(
123 promptScreen(ctx, screen, {
124 title: "Name someone",
125 label: "username",
126 onSubmit: (raw) => {
127 const who = raw.trim();
128 if (who === "") return "a username is needed";
129 const next = plan({ add: who }, screen);
130 return typeof next === "string" ? next : later(ctx, () => next);
131 },
132 }),
133 );
134 else if (e.key === "d") {
135 const row = names()[state.index];
136 if (row === undefined) return true;
137 const next = plan({ remove: row }, screen);
138 if (typeof next === "string") note = next;
139 else ctx.push(next);
140 } else return false;
141 return true;
142 },
143 };
144 return screen;
145 }
test/tui-appmembers.test.ts added +209
@@ -0,0 +1,209 @@
1 /**
2 * The people a private app names, in the terminal: a writer who is a person
3 * adds one after a plan and a confirm and they can read the app, removes them
4 * and they cannot; an unknown name is refused in MCP's words; an agent has
5 * neither key; a public app is not offered the command.
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 { createAgent } from "../src/actions/crew.ts";
13 import { createApp, type App as Forge } from "../src/app.ts";
14 import { getAppByRepo } from "../src/core/apps.ts";
15 import { canEnterApp, getRepoById, type Repo } from "../src/core/repos.ts";
16 import { setAppMembersThrough } from "../src/actions/app_members.ts";
17 import { SETTING_WORDS } from "../src/actions/setting_words.ts";
18 import { createUser, getUserByName } from "../src/core/users.ts";
19 import { createForgeApp } from "../src/tui/forge.ts";
20 import { appMembersScreen } from "../src/tui/screens/appmembers.ts";
21 import { Session } from "../src/tui/session.ts";
22 import { headlessTerminal } from "../src/tui/term/terminal.ts";
23 import { quietLogs } from "./helpers/globals.ts";
24 import { settleApp } from "./helpers/tui.ts";
25
26 quietLogs();
27
28 describe("app members in the terminal", () => {
29 let dataDir: string;
30 let forge: Forge;
31 let game: Repo;
32
33 function boot(username: string) {
34 const terminal = headlessTerminal(140, 32);
35 const user = getUserByName(forge.db, username)!;
36 const { app } = createForgeApp(
37 forge,
38 { sshUsername: "t", remoteAddress: "127.0.0.1", user },
39 { terminal, exitHooks: false },
40 );
41 app.start();
42 const key = (k: string, ctrl = false) =>
43 app.dispatch({ type: "key", key: k, ctrl, alt: false, shift: false });
44 return {
45 text: () => app.screen.toText(),
46 press: async (...keys: string[]) => {
47 for (const k of keys) {
48 key(k);
49 await settleApp(app);
50 }
51 },
52 palette: async (verb: string) => {
53 key("k", true);
54 await settleApp(app);
55 for (const ch of verb) key(ch);
56 await settleApp(app);
57 key("enter");
58 await settleApp(app);
59 },
60 type: async (t: string) => {
61 for (const ch of t) key(ch);
62 await settleApp(app);
63 },
64 stop: () => app.stop(),
65 };
66 }
67
68 const carolEnters = () => {
69 const record = getAppByRepo(forge.db, game.id)!;
70 return canEnterApp(forge.db, game, record, getUserByName(forge.db, "carol"));
71 };
72
73 const openMembers = async (h: ReturnType<typeof boot>, spec = "alice/game") => {
74 await h.palette("Members");
75 await h.type(spec);
76 await h.press("enter");
77 };
78
79 beforeAll(async () => {
80 dataDir = await mkdtemp(join(tmpdir(), "forge-tui-appmembers-"));
81 forge = createApp({ dataDir, port: 0 });
82 for (const [username, first] of [["alice", true], ["carol", false], ["dave", false]] as const) {
83 const made = await createUser(forge.db, {
84 origin: "seed",
85 trusted: true,
86 username,
87 email: `${username}@forge.local`,
88 password: "a-good-passphrase",
89 adminIfFirst: first,
90 });
91 if (!made.ok) throw new Error(made.error);
92 }
93 const alice = getUserByName(forge.db, "alice")!;
94 const made = await newApp(forge, alice, { name: "game", description: "Friends play", visibility: "private" });
95 if (!made.ok) throw new Error(String(made.error));
96 game = getRepoById(forge.db, made.value.app.repoId)!;
97 const open = await newApp(forge, alice, { name: "open", description: "Anyone", visibility: "public" });
98 if (!open.ok) throw new Error(String(open.error));
99 const bot = await createAgent(forge, alice, { username: "bot" });
100 if (!bot.ok) throw new Error(String(bot.error));
101 });
102 afterAll(async () => {
103 forge.close();
104 await rm(dataDir, { recursive: true, force: true });
105 });
106
107 test("a writer adds someone after the plan and a confirm, then removes them", async () => {
108 const h = boot("alice");
109 await openMembers(h);
110 expect(h.text()).toContain("No one is named");
111 expect(carolEnters()).toBe(false);
112
113 await h.press("a");
114 await h.type("carol");
115 await h.press("enter");
116 expect(h.text()).toContain("carol is let in");
117 await h.press("right", "enter"); // Cancel
118 expect(carolEnters()).toBe(false);
119
120 await h.press("a");
121 await h.type("carol");
122 await h.press("enter");
123 await h.press("left", "enter"); // Apply
124 expect(h.text()).toContain("the list is changed");
125 expect(carolEnters()).toBe(true);
126
127 await h.press("d");
128 expect(h.text()).toContain("carol is let out");
129 await h.press("left", "enter");
130 expect(carolEnters()).toBe(false);
131 expect(h.text()).not.toContain("carol is let");
132 h.stop();
133 });
134
135 test("an unknown username is refused in MCP's words and changes nothing", async () => {
136 const h = boot("alice");
137 await openMembers(h);
138 await h.press("a");
139 await h.type("nobody");
140 await h.press("enter");
141 expect(h.text()).toContain("nobody is not a person's account here");
142 h.stop();
143 });
144
145 test("an agent has neither key; a public app is not offered the command", async () => {
146 const bot = getUserByName(forge.db, "bot")!;
147 const session = new Session(forge, { sshUsername: "t", remoteAddress: "127.0.0.1", user: bot });
148 const pushed: unknown[] = [];
149 const screen = appMembersScreen(session, { push: (s: unknown) => pushed.push(s) } as never, game);
150 for (const k of ["a", "d"])
151 expect(screen.key({ key: k, ctrl: false, alt: false, shift: false } as never)).toBe(false);
152 expect(pushed).toEqual([]);
153
154 const h = boot("alice");
155 await openMembers(h, "alice/open");
156 expect(h.text()).toContain("only a private app names people");
157 h.stop();
158 });
159
160 test("a reader who cannot write is refused at the prompt with the forbidden words", async () => {
161 const h = boot("carol");
162 await openMembers(h, "alice/open");
163 expect(h.text()).toContain(SETTING_WORDS.appMembersForbidden.slice(0, 30));
164 expect(h.text()).not.toContain("No one is named");
165 h.stop();
166 });
167
168 test("an agent is refused at the prompt with the forbidden words", async () => {
169 const h = boot("bot");
170 await openMembers(h);
171 expect(h.text()).not.toContain("No one is named");
172 h.stop();
173 });
174
175 const external = (members: string[]) => {
176 const alice = getUserByName(forge.db, "alice")!;
177 const r = setAppMembersThrough(forge, alice, game, { members, dryRun: false });
178 if (!r.ok) throw new Error(r.error.kind);
179 };
180
181 test("Apply recomputes from the current list, so another writer's edit is kept", async () => {
182 external([]);
183 const h = boot("alice");
184 await openMembers(h);
185 await h.press("a");
186 await h.type("carol");
187 await h.press("enter");
188 external(["dave"]); // another writer, after the plan
189 await h.press("left", "enter");
190 expect(carolEnters()).toBe(true);
191 const dave = getUserByName(forge.db, "dave")!;
192 expect(canEnterApp(forge.db, game, getAppByRepo(forge.db, game.id)!, dave)).toBe(true);
193 h.stop();
194 });
195
196 test("Apply re-plans when the list moved under the plan", async () => {
197 external([]);
198 const h = boot("alice");
199 await openMembers(h);
200 await h.press("a");
201 await h.type("carol");
202 await h.press("enter");
203 external(["carol"]); // the plan is now stale
204 await h.press("left", "enter");
205 expect(h.text()).toContain("The list changed meanwhile");
206 expect(h.text()).toContain("that changes nothing");
207 h.stop();
208 });
209 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs