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

Waves in the terminal: list and read waves/*.md from the app terminal #1915

Parked opened this
Closed without merging

scout/forge:scout/1819-waves-in-the-terminal-list-and-read-wave → main 4 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
9 files changed +337 −20
README.md +6
@@ -106,6 +106,12 @@ thoughts, then the issues you can write to that are not shipped, oldest first
106106 only, shows a guest nothing, and names nothing of a placement in a repository
107107 you cannot read.
108108
109 Waves in the terminal: `Ctrl+K`, then **Waves in a repository**, asks for
110 `owner/name` and lists `waves/*.md` on its default branch, newest first, a file
111 that does not parse after them with its reason. `enter` reads one — headline,
112 highlights with their why, the pulls with their addresses — and `esc` goes back.
113 Read only; a repository you cannot read has no waves, as anywhere else.
114
109115 The family across forges: `:` → Family in the terminal, `/family` on the web,
110116 `GET /api/v1/family` and the MCP `family` tool all give the same view. The
111117 forge that sent this one is shown to anyone; the forges it sent, only to a
scripts/gate.ts +1
@@ -419,6 +419,7 @@ export const EXCLUDED: readonly Exclusion[] = [
419419 },
420420 { file: "test/tui-app.test.ts", reason: "not yet admitted", issue: 261 },
421421 { file: "test/tui-asked.test.ts", reason: "not yet admitted", issue: 261 },
422 { file: "test/tui-waves.test.ts", reason: "not yet admitted", issue: 261 },
422423 { file: "test/tui-apps.test.ts", reason: "not yet admitted", issue: 261 },
423424 { file: "test/tui-code.test.ts", reason: "not yet admitted", issue: 261 },
424425 { file: "test/tui-editor.test.ts", reason: "not yet admitted", issue: 261 },
src/actions/cut_wave.ts +3 −19
@@ -9,10 +9,9 @@ import type { App } from "../app.ts";
99 import { canAdmin, type Repo } from "../core/repos.ts";
1010 import { mergedPullsSince } from "../core/pulls.ts";
1111 import type { User } from "../core/users.ts";
12 import { listTree, readerFor } from "../git/read.ts";
13 import { repoDir } from "../git/repos.ts";
1412 import { err, ok, type Result } from "../kernel/result.ts";
15 import { cutWave, type WaveFile } from "../story/cut.ts";
13 import { cutWave } from "../story/cut.ts";
14 import { readWaveFiles } from "./waves.ts";
1615 import { editFiles } from "./contents.ts";
1716
1817 export interface CutDone {
@@ -20,21 +19,6 @@ export interface CutDone {
2019 readonly number: number;
2120 }
2221
23 /** The waves on the default branch, as files. */
24 async function readWaves(app: App, repo: Repo): Promise<WaveFile[]> {
25 const dir = repoDir(app.reposRoot, repo.ownerName, repo.name);
26 const tree = await listTree(dir, repo.defaultBranch, "waves");
27 if (!tree.ok) return [];
28 const files: WaveFile[] = [];
29 for (const e of tree.value) {
30 if (e.type !== "blob" || !e.name.endsWith(".md")) continue;
31 const obj = await readerFor(dir).object(e.oid);
32 const text = obj.ok ? new TextDecoder().decode(obj.value.body) : "";
33 files.push({ path: `waves/${e.name}`, text });
34 }
35 return files;
36 }
37
3822 /** The reason is a plain sentence, ready to show. */
3923 export async function cutWaveFor(
4024 app: App,
@@ -43,7 +27,7 @@ export async function cutWaveFor(
4327 ): Promise<Result<CutDone, string>> {
4428 if (!actor || actor.kind === "agent" || !canAdmin(app.db, repo, actor))
4529 return err("Only a repository admin can cut a wave.");
46 const waves = await readWaves(app, repo);
30 const waves = await readWaveFiles(app, repo);
4731 const base = app.config.url ?? `http://localhost:${app.config.port}`;
4832 const repoUrl = `${base.replace(/\/+$/, "")}/${repo.ownerName}/${repo.name}`;
4933 const plan = cutWave({
src/actions/waves.ts added +35
@@ -0,0 +1,35 @@
1 /**
2 * The `waves/*.md` files on a repository's default branch, read through git.
3 * A blob's text never changes under its id, so it is kept by id: a second
4 * read of an unchanged wave costs no object read.
5 */
6
7 import type { App } from "../app.ts";
8 import type { Repo } from "../core/repos.ts";
9 import { listTree, readerFor } from "../git/read.ts";
10 import { repoDir } from "../git/repos.ts";
11 import type { WaveFile } from "../story/cut.ts";
12
13 const MAX_CACHED = 256;
14 const texts = new Map<string, string>();
15
16 export async function readWaveFiles(app: App, repo: Repo): Promise<WaveFile[]> {
17 const dir = repoDir(app.reposRoot, repo.ownerName, repo.name);
18 const tree = await listTree(dir, repo.defaultBranch, "waves");
19 if (!tree.ok) return [];
20 const files: WaveFile[] = [];
21 for (const e of tree.value) {
22 if (e.type !== "blob" || !e.name.endsWith(".md")) continue;
23 let text = texts.get(e.oid);
24 if (text === undefined) {
25 const obj = await readerFor(dir).object(e.oid);
26 text = obj.ok ? new TextDecoder().decode(obj.value.body) : "";
27 if (obj.ok) {
28 if (texts.size >= MAX_CACHED) texts.clear();
29 texts.set(e.oid, text);
30 }
31 }
32 files.push({ path: `waves/${e.name}`, text });
33 }
34 return files;
35 }
src/story/cut.ts +2 −1
@@ -38,7 +38,8 @@ export type CutResult = { ok: true; value: Cut } | { ok: false; error: string };
3838 const parts = (v: string): number[] =>
3939 v.replace(/-.*$/, "").split(".").map(Number);
4040
41 const newer = (a: Wave, b: Wave): boolean => {
41 /** Is `a` a later wave than `b`: by version, then date. */
42 export const newer = (a: Wave, b: Wave): boolean => {
4243 const x = parts(a.version);
4344 const y = parts(b.version);
4445 for (let i = 0; i < 3; i++) if (x[i] !== y[i]) return (x[i] ?? 0) > (y[i] ?? 0);
src/story/list.ts added +30
@@ -0,0 +1,30 @@
1 /**
2 * The waves of a repository as one list: the ones that parse, newest first,
3 * and the files that do not, each with its reason. Pure: files in, plain
4 * data out; the pages and the terminal read the same list.
5 */
6
7 import { newer, type WaveFile } from "./cut.ts";
8 import { parseWave, type Wave } from "./wave.ts";
9
10 export interface BadWave {
11 path: string;
12 reason: string;
13 }
14 export interface WaveList {
15 waves: Wave[];
16 bad: BadWave[];
17 }
18
19 export function listWaves(files: readonly WaveFile[]): WaveList {
20 const waves: Wave[] = [];
21 const bad: BadWave[] = [];
22 for (const f of files) {
23 const r = parseWave(f.path, f.text);
24 if (r.ok) waves.push(r.value);
25 else bad.push({ path: f.path, reason: r.error.message });
26 }
27 waves.sort((a, b) => (newer(a, b) ? -1 : newer(b, a) ? 1 : 0));
28 bad.sort((a, b) => (a.path < b.path ? -1 : 1));
29 return { waves, bad };
30 }
src/tui/forge.ts +6
@@ -28,6 +28,7 @@ import {
2828 openOnWeb,
2929 runAsync,
3030 } from "./screens/common.ts";
31 import { wavesScreen } from "./screens/waves.ts";
3132 import { askedScreen } from "./screens/asked.ts";
3233 import { familyScreen } from "./screens/family.ts";
3334 import { familyLines, familyView } from "../spawn/family.ts";
@@ -79,6 +80,11 @@ const REPO_VERBS: readonly {
7980 title: "Milestones in a repository",
8081 open: (f, ctx, repo) => limbsScreen(f, ctx, repo),
8182 },
83 {
84 id: "waves",
85 title: "Waves in a repository",
86 open: (f, ctx, repo) => wavesScreen(f, ctx, repo),
87 },
8288 { id: "new-issue", title: "New issue", open: newIssueScreen },
8389 { id: "new-pull", title: "New pull request", open: newPullScreen },
8490 {
src/tui/screens/waves.ts added +128
@@ -0,0 +1,128 @@
1 /**
2 * The waves of a repository in the terminal (#1819): `waves/*.md` on the
3 * default branch, newest first, a file that does not parse listed with its
4 * reason, and `enter` reads one. The list is `story/list.ts`'s and the files
5 * come through `actions/waves.ts`, as for any other reader. Read only; a
6 * repository the viewer cannot read has no waves to show.
7 */
8
9 import { readWaveFiles } from "../../actions/waves.ts";
10 import { canRead, type Repo } from "../../core/repos.ts";
11 import { listWaves, type WaveList } from "../../story/list.ts";
12 import type { Wave } from "../../story/wave.ts";
13 import type { Ctx, Screen } from "../app/app.ts";
14 import { binding } from "../app/keys.ts";
15 import type { Session } from "../session.ts";
16 import { fill, len, split } from "../ui/layout.ts";
17 import { emptyState } from "../ui/widgets.ts";
18 import { style } from "../term/style.ts";
19 import { truncate } from "../term/width.ts";
20 import { ListState, Scroll, drawLines, drawList, hitList, hitScroll, plural, runAsync, type ListRow } from "./common.ts";
21
22 /** One line of plain text: file contents must not steer the terminal. */
23 const plain = (text: string): string =>
24 // eslint-disable-next-line no-control-regex
25 text.replace(/[\u0000-\u001f\u007f-\u009f]+/g, " ").trim();
26
27 type Entry = { wave: Wave } | { bad: { path: string; reason: string } };
28
29 /** Good waves newest first, then the files that did not parse. */
30 export function waveEntries(list: WaveList): Entry[] {
31 return [...list.waves.map((wave) => ({ wave })), ...list.bad.map((bad) => ({ bad }))];
32 }
33
34 function rowOf(e: Entry): ListRow {
35 if ("wave" in e)
36 return {
37 title: `${plain(e.wave.version)} — ${plain(e.wave.headline)}`,
38 secondary: plural(e.wave.pulls.length, "pull"),
39 meta: e.wave.date,
40 };
41 return { title: plain(e.bad.path), tone: "danger", secondary: plain(e.bad.reason), meta: "not a wave" };
42 }
43
44 /** A wave as lines: headline, each highlight with its why, then the pulls. */
45 export function waveLines(w: Wave): string[] {
46 return [
47 `${plain(w.version)} · ${w.date}`,
48 plain(w.headline),
49 "",
50 "Highlights",
51 ...w.highlights.flatMap((h) => [` ${plain(h.title)}`, ` ${plain(h.why)}`]),
52 "",
53 `Pulls (${w.pulls.length})`,
54 ...w.pulls.map((p) => ` ${plain(p.title)} ${plain(p.url)}`),
55 ];
56 }
57
58 export function waveScreen(ctx: Ctx, w: Wave): Screen {
59 const scroll = new Scroll();
60 const lines = waveLines(w);
61 return {
62 title: `Wave ${plain(w.version)}`,
63 keys: [binding(["j", "k"], "scroll"), binding(["esc"], "back")],
64 render(area, s) {
65 const c = ctx.theme.c;
66 const spans = lines.map((text, i) => [
67 { text, style: style(i === 1 || /^(Highlights|Pulls)/.test(text) ? c.fg : c.mutedFg, c.bg) },
68 ]);
69 drawLines(s, area, c, scroll, spans);
70 hitScroll(ctx, area, scroll, lines.length);
71 },
72 key(e) {
73 if (e.key === "esc" || e.key === "q") {
74 ctx.pop();
75 return true;
76 }
77 return scroll.key(e, lines.length);
78 },
79 };
80 }
81
82 export function wavesScreen(f: Session, ctx: Ctx, repo: Repo): Screen {
83 const state = new ListState();
84 let entries: Entry[] = [];
85 let loaded = false;
86 const read = (): void =>
87 runAsync(ctx, async () => {
88 entries = canRead(f.db, repo, f.user) ? waveEntries(listWaves(await readWaveFiles(f.app, repo))) : [];
89 loaded = true;
90 state.clamp(entries.length);
91 ctx.invalidate();
92 });
93 const open = (index: number): void => {
94 const e = entries[index];
95 if (e !== undefined && "wave" in e) ctx.push(waveScreen(ctx, e.wave));
96 };
97 return {
98 title: "Waves",
99 keys: [binding(["j", "k"], "move"), binding(["enter"], "read"), binding(["r"], "refresh")],
100 load: async () => read(),
101 render(area, s) {
102 const c = ctx.theme.c;
103 const [head, body] = split(area, "v", [len(1), fill(1)]);
104 const sentence = `${repo.ownerName}/${repo.name} · ${plural(entries.length, "wave")}`;
105 s.text(head!.x, head!.y, truncate(sentence, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w);
106 if (entries.length === 0) {
107 if (loaded)
108 emptyState(s, body!, c, { title: "No waves yet", body: "A wave is a file: waves/<version>.md." });
109 return;
110 }
111 const rows = entries.map(rowOf);
112 drawList(s, body!, c, state, rows);
113 hitList(ctx, body!, state, rows.length, { open });
114 },
115 key(e) {
116 if (state.key(e, entries.length)) return true;
117 if (e.key === "enter") {
118 open(state.index);
119 return true;
120 }
121 if (e.key === "r") {
122 read();
123 return true;
124 }
125 return false;
126 },
127 };
128 }
test/tui-waves.test.ts added +126
@@ -0,0 +1,126 @@
1 /**
2 * Waves in the terminal (#1819): the list is the pages' list (newest first,
3 * a bad file with its reason), one wave reads in full, and a repository the
4 * viewer cannot read shows nothing.
5 */
6 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
7 import { mkdtemp, rm } from "node:fs/promises";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
11 import { createApp, type App } from "../src/app.ts";
12 import type { Repo } from "../src/core/repos.ts";
13 import { createUser, type User } from "../src/core/users.ts";
14 import { listWaves } from "../src/story/list.ts";
15 import { writeWave } from "../src/story/wave.ts";
16 import { createForgeApp } from "../src/tui/forge.ts";
17 import { wavesScreen } from "../src/tui/screens/waves.ts";
18 import { headlessTerminal } from "../src/tui/term/terminal.ts";
19 import { quietLogs } from "./helpers/globals.ts";
20 import { settleApp } from "./helpers/tui.ts";
21
22 quietLogs();
23
24 const wave = (version: string, date: string, headline: string) => {
25 const w = writeWave({
26 version,
27 date,
28 headline,
29 highlights: [1, 2, 3].map((i) => ({ title: `Title ${i}`, why: `Because ${i}` })),
30 pulls: [{ title: "#4 Four", url: "https://f.test/o/r/pulls/4" }],
31 });
32 if (!w.ok) throw w.error;
33 return w.value;
34 };
35
36 describe("listWaves", () => {
37 test("newest first, bad files after with their reason", () => {
38 const l = listWaves([
39 { path: "waves/0.1.0.md", text: wave("0.1.0", "2026-10-01", "old") },
40 { path: "waves/0.2.0.md", text: wave("0.2.0", "2026-10-09", "new") },
41 { path: "waves/oops.md", text: "no front matter" },
42 ]);
43 expect(l.waves.map((w) => w.version)).toEqual(["0.2.0", "0.1.0"]);
44 expect(l.bad).toEqual([{ path: "waves/oops.md", reason: "waves/oops.md: missing front matter" }]);
45 });
46 });
47
48 describe("the waves screen", () => {
49 let dir: string;
50 let app: App;
51 let olive: User;
52 let stranger: User;
53 let repo: Repo;
54
55 beforeAll(async () => {
56 dir = await mkdtemp(join(tmpdir(), "forge-tui-waves-"));
57 app = createApp({ dataDir: dir, port: 0 });
58 const mk = async (username: string, first = false) => {
59 const r = await createUser(app.db, {
60 trusted: true, username, email: `${username}@forge.local`,
61 password: "a-good-passphrase", adminIfFirst: first,
62 });
63 if (!r.ok) throw new Error(r.error);
64 return r.value;
65 };
66 olive = await mk("olive");
67 stranger = await mk("stranger");
68 const made = await createRepo(app, olive, { name: "proj" });
69 if (!made.ok) throw new Error(String(made.error));
70 repo = made.value;
71 const c = await commitToBranch(app, olive, repo, {
72 message: "waves",
73 files: [
74 { path: "waves/0.1.0.md", content: wave("0.1.0", "2026-10-01", "First light") },
75 { path: "waves/0.2.0.md", content: wave("0.2.0", "2026-10-09", "Second tide \u001b[31m") },
76 { path: "waves/oops.md", content: "nothing here" },
77 ],
78 });
79 if (!c.ok) throw new Error(String(c.error));
80 });
81 afterAll(async () => {
82 app.close();
83 await rm(dir, { recursive: true, force: true });
84 });
85
86 async function text(user: User, keys = ""): Promise<string> {
87 const terminal = headlessTerminal(120, 30);
88 const { app: tui, session } = createForgeApp(
89 app,
90 { sshUsername: user.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:t", user },
91 { terminal, exitHooks: false },
92 );
93 tui.start();
94 tui.ctx.push(wavesScreen(session, tui.ctx, repo));
95 await settleApp(tui);
96 if (keys !== "") {
97 terminal.feed(keys);
98 await settleApp(tui);
99 }
100 const out = tui.screen.toText();
101 tui.stop?.();
102 return out;
103 }
104
105 test("lists newest first with the bad file and its reason", async () => {
106 const t = await text(olive);
107 expect(t.indexOf("0.2.0")).toBeGreaterThan(-1);
108 expect(t.indexOf("0.2.0")).toBeLessThan(t.indexOf("0.1.0"));
109 expect(t).toContain("waves/oops.md");
110 expect(t).toContain("missing front matter");
111 expect(t).not.toContain("\u001b");
112 });
113
114 test("enter reads the wave: highlights with their why, and the pulls", async () => {
115 const t = await text(olive, "\r");
116 expect(t).toContain("Second tide");
117 expect(t).toContain("Because 2");
118 expect(t).toContain("https://f.test/o/r/pulls/4");
119 });
120
121 test("a viewer who cannot read the repository sees no waves", async () => {
122 const t = await text(stranger);
123 expect(t).not.toContain("0.2.0");
124 expect(t).toContain("No waves yet");
125 });
126 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs