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

A fork's follow mode is one line on its page: follow automatically or only propose #15127

Merged opened this

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

Merged

scout/forge:scout/15089-a-fork-s-follow-mode-is-one-line-on-its → main 5 commits fork 3 threads · 3 unresolved

  • Merged. scout/15089-a-fork-s-follow-mode-is-one-line-on-its was merged into main as 4f8a9a4.

Review

Sign in to review this change.
6 files changed +128 −18
docs/features/fork-follow-undo.md added +12
@@ -0,0 +1,12 @@
1 ---
2 topic: Ship and undo
3 row: A fork's follow mode in one tap
4 ---
5 # A fork's follow mode in one tap (#15089)
6
7 On a fork's `/following` card the follow mode switches in one tap; only "Don't follow" keeps its confirm step, because it severs the upstream. After a change the card shows a receipt ("Changed from “…” <time>.") with an Undo button that restores the previous mode. The receipt stands only while that change is the latest one.
8
9 - Web: the card in `src/web/pages/following.ts`; the previous mode is recorded in the `forge.follow_mode` event (`was`) and read by `lastModeChange` in `src/actions/follow.ts`.
10 - Terminal: `src/tui/screens/forks.ts` shows the same receipt as a row ("changed from “…” <ago>") and `z` undoes it, for anyone who may press `m`. The page puts the receipt on its own muted line under the mode line.
outdated

The terminal does not show the receipt or an Undo; forks.ts only lists FOLLOW_CHOICES. Either build it there or say so here; as written the doc is wrong.

11 - Final: on a repository that follows an address rather than a parent, "Don't follow" severs the upstream for good, so it leaves no receipt and no Undo (`lastModeChange`); a fork's own "Don't follow" is just a mode and can be undone.
12 - Admins only; everyone else sees the standing line without buttons.
src/actions/follow.ts +18 −1
@@ -17,6 +17,7 @@ import { FAMILY_WORDS } from "./family_words.ts";
1717 import { FOLLOWS_NOTHING, FOLLOW_LINE, agoWords, driftWords, followModeWords, followStandingLine, lastFollowClause } from "./follow_words.ts";
1818 import { FORK_FOLLOW_MODES, forkFollowMode, lastTookAt, isForkFollowMode, setForkFollowMode, type ForkFollowMode } from "../core/fork_follow.ts";
1919 import { lastFollow } from "../core/last_follow.ts";
20 import { listEvents } from "../core/events.ts";
2021 import { requireUser } from "./access.ts";
2122 import { getFollow, setFollowTaking } from "../core/upstream_follow.ts";
2223 import { isChecking, severUpstream } from "./upstream_follow.ts";
@@ -245,6 +246,7 @@ export async function setFollowMode(
245246 // After the admin check, so anyone else gets the same 403 and learns nothing of a check.
246247 if (isChecking(repo.id)) return err("updating");
247248 if (!isForkFollowMode(mode)) return err("invalid_mode");
249 const was = await followModeOf(app, repo);
248250 if (upstream !== undefined) {
249251 if (upstream.severedAt !== null) return err("severed");
250252 if (mode === "dont-follow") await severUpstream(app, who.value, repo);
@@ -260,11 +262,26 @@ export async function setFollowMode(
260262 kind: "forge.follow_mode",
261263 repoId: repo.id,
262264 actorId: who.value.id,
263 payload: { repo: `${repo.ownerName}/${repo.name}`, mode },
265 payload: { repo: `${repo.ownerName}/${repo.name}`, mode, was },
264266 });
265267 return ok(mode);
266268 }
267269
270 /**
271 * The last mode change of a repository that is still the standing one, for the page's receipt and
272 * its Undo: what it was before, and when. Undefined when none was recorded, a later change
273 * (which has its own receipt) came after, or the change was to "dont-follow" on a repository
274 * that follows an address rather than a parent: that severs the upstream, and setFollowMode
275 * refuses every mode after it ("severed"), so there is nothing to undo. A fork's own
276 * "dont-follow" is only a stored mode and can be undone.
277 */
outdated

lastModeChange returns a receipt for a change to dont-follow, whose undo is refused after the sever. Return undefined for it, or flag it as not undoable.

278 export function lastModeChange(app: App, repo: Repo, now: ForkFollowMode): { was: ForkFollowMode; at: string } | undefined {
279 const last = listEvents(app.db, { repoId: repo.id, kind: "forge.follow_mode", limit: 1 })[0];
280 const p = last?.payload as { mode?: unknown; was?: unknown } | undefined;
281 if (last === undefined || p === undefined || p.mode !== now || (now === "dont-follow" && repo.forkParentId === null) || !isForkFollowMode(p.was) || p.was === now) return undefined;
282 return { was: p.was, at: last.createdAt };
283 }
284
268285 /** The sentence every door answers a mode change with. */
269286 export function followModeSaid(parent: Repo | undefined, mode: ForkFollowMode): string {
270287 const upstream = parent === undefined ? "its parent" : `${parent.ownerName}/${parent.name}`;
src/tui/screens/forks.ts +19 −2
@@ -22,7 +22,7 @@ import {
2222 type ForkRelation,
2323 } from "../../actions/forks.ts";
2424 import { FOLLOW_CHOICES, FOLLOW_PHRASE, FOLLOWS_NOTHING, followChoiceLine, proposeRefusalWords } from "../../actions/follow_words.ts";
25 import { FOLLOW_MODE_REFUSALS, convergeWords, followModeOf, followStanding, followWords, setFollowMode, takeOrTell, takeWords } from "../../actions/follow.ts";
25 import { FOLLOW_MODE_REFUSALS, convergeWords, followModeOf, followStanding, followWords, lastModeChange, setFollowMode, takeOrTell, takeWords } from "../../actions/follow.ts";
2626 import { incomingOf, type Incoming } from "../../actions/follow_incoming.ts";
2727 import { convergeFollowed, followedAs, followedOf } from "../../actions/upstream_sync.ts";
2828 import { forkOffers, sentOffers } from "../../core/fork_offers.ts";
@@ -107,6 +107,7 @@ export function followingRows(
107107 relation: ForkRelation | undefined,
108108 converge: Converge | undefined,
109109 mode?: ForkFollowMode,
110 undo?: { was: ForkFollowMode; at: string },
110111 ): ForkRow[] {
111112 if (relation === undefined) return [];
112113 const { parent } = relation;
@@ -128,6 +129,9 @@ export function followingRows(
128129 heading: "",
129130 },
130131 ...(mode === undefined ? [] : [{ repo: parent, name: ` ${FOLLOW_CHOICES[mode].sentence}`, words: "", heading: "" }]),
132 ...(undo === undefined
133 ? []
134 : [{ repo: parent, name: ` changed from “${FOLLOW_CHOICES[undo.was].label}” ${agoWords(undo.at)}`, words: "z undoes it", heading: "" }]),
131135 ...(converge === undefined
132136 ? []
133137 : [
@@ -189,6 +193,7 @@ export function forksScreen(f: Session, ctx: Ctx, repo: Repo): Screen {
189193 let followed: ReturnType<typeof followedAs> | undefined;
190194 let open: Pull | undefined;
191195 let mode: ForkFollowMode | undefined;
196 let undo: { was: ForkFollowMode; at: string } | undefined;
192197 /** The one plain line the page and MCP say: how this fork follows, and when it last took. */
193198 let standing: string | undefined;
194199 /**
@@ -264,7 +269,8 @@ export function forksScreen(f: Session, ctx: Ctx, repo: Repo): Screen {
264269 standing = (await followWords(f.app, repo, f.user)) ?? (await followStanding(f.app, repo, f.user));
265270 failed = getRepoById(f.db, repo.id)?.origin?.fetchError ?? "";
266271 mode = relation === undefined ? undefined : await followModeOf(f.app, repo);
267 following = followingRows(relation, converge, mode);
272 undo = mode === undefined ? undefined : lastModeChange(f.app, repo, mode);
273 following = followingRows(relation, converge, mode, undo);
268274 const here = forkOffers(f.db, repo, f.user);
269275 const sizes = await offerSizes(f.app, repo, here.map((o) => o.number));
270276 offered = here.map((o) => ({
@@ -356,6 +362,7 @@ export function forksScreen(f: Session, ctx: Ctx, repo: Repo): Screen {
356362 ? [binding(["u"], open === undefined ? "propose upstream" : "open proposal")]
357363 : []),
358364 ...(canSetMode() ? [binding(["m"], "follow mode")] : []),
365 ...(canSetMode() && undo !== undefined ? [binding(["z"], "undo mode change")] : []),
359366 binding(["esc"], "close"),
360367 ];
361368 },
@@ -439,6 +446,16 @@ export function forksScreen(f: Session, ctx: Ctx, repo: Repo): Screen {
439446 });
440447 return true;
441448 }
449 if (e.key === "z" && canSetMode() && undo !== undefined) {
450 const back = undo.was;
451 runAsync(ctx, async () => {
452 const set = await setFollowMode(f.app, f.user, repo, back);
453 if (!set.ok) return ctx.toast(FOLLOW_MODE_REFUSALS[set.error].message, "danger");
454 await read();
455 ctx.toast(followChoiceLine(set.value), "success");
456 });
457 return true;
458 }
442459 if (e.key === "esc" || e.key === "q") {
443460 ctx.pop();
444461 return true;
src/web/pages/following.ts +19 −6
@@ -37,10 +37,10 @@ import {
3737 type ParentReleases,
3838 } from "../../core/parent_releases.ts";
3939 import { running } from "../../kernel/version.ts";
40 import { FOLLOW_SWITCH_LABELS, followModeWords } from "../../actions/follow_words.ts";
40 import { FOLLOW_CHOICES, FOLLOW_SWITCH_LABELS, followModeWords } from "../../actions/follow_words.ts";
4141 import type { ForkFollowMode } from "../../core/fork_follow.ts";
4242 import { loadRepo, notFoundPage, repoBase, signInFirst } from "../chrome.ts";
43 import { convergeWords, followModeOf, takeOrTell, takeWords } from "../../actions/follow.ts";
43 import { convergeWords, followModeOf, lastModeChange, takeOrTell, takeWords } from "../../actions/follow.ts";
4444 import { FOLLOW_WORD, FOLLOWS_NOTHING, HIDDEN_FORK_WORD, proposeRefusalWords } from "../../actions/follow_words.ts";
4545 import { incomingOf, type Incoming } from "../../actions/follow_incoming.ts";
4646 import { openProposal, proposeUpstream } from "../../actions/upstream.ts";
@@ -451,19 +451,32 @@ export function followingCard(
451451 prose.push(html`<p class="muted small">${relation.words}</p>`);
452452 if (foot !== null) prose.push(html`<p class="muted small">${foot}</p>`);
453453 const words = followModeWords(following.mode, parentName);
454 const last = lastModeChange(ctx.app, repo, following.mode);
455 const mayChange = canAdmin(ctx.app.db, repo, ctx.auth.user) && writer;
456 const undone =
457 last === undefined

Principle 3: the receipt and Undo share the line with the mode line and two buttons; on a 360px phone this wraps messily. Give the receipt its own muted line. Also, when last.was is reached from a dont-follow change, Undo posts a mode that setFollowMode refuses with 'severed'; hide Undo in that case.

458 ? null
459 : html`<p class="muted small" data-follow-receipt>Changed from “${FOLLOW_CHOICES[last.was].label}” ${time(last.at)}.${
460 mayChange
461 ? html` <form method="post" action="${base}/following/mode/${last.was}" class="inline">${btn("Undo", { variant: "outline" })}</form>`
462 : null
463 }</p>`;
454464 prose.push(
455465 html`<p data-follow-mode="${following.mode}">
456466 <strong>${words.line}</strong>.
457467 ${canAdmin(ctx.app.db, repo, ctx.auth.user) && writer
458468 ? words.others.map((m) =>
459 btn(FOLLOW_SWITCH_LABELS[m], {
460 href: `/-/confirm?to=${encodeURIComponent(`${base}/following/mode/${m}`)}`,
461 size: "sm",
462 }),
469 m === "dont-follow"
470 ? btn(FOLLOW_SWITCH_LABELS[m], {
471 href: `/-/confirm?to=${encodeURIComponent(`${base}/following/mode/${m}`)}`,
472 size: "sm",
473 })
474 : html`<form method="post" action="${base}/following/mode/${m}" class="inline">${btn(FOLLOW_SWITCH_LABELS[m], { size: "sm" })}</form>`,
463475 )
464476 : null}
465477 </p>`,
466478 );
479 if (undone !== null) prose.push(undone);
467480 if (followers > 0 && o.family !== false)
468481 rows.push(
469482 row({
test/fork-follow-mode.test.ts +43 −1
@@ -2,13 +2,14 @@ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
22 import { mkdtemp, rm } from "node:fs/promises";
33 import { tmpdir } from "node:os";
44 import { join } from "node:path";
5 import { setFollowMode } from "../src/actions/follow.ts";
5 import { lastModeChange, setFollowMode } from "../src/actions/follow.ts";
66 import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts";
77 import { createApp, type App } from "../src/app.ts";
88 import { forkFollowMode } from "../src/core/fork_follow.ts";
99 import { forkRelation } from "../src/core/following.ts";
1010 import { getRepo, type Repo } from "../src/core/repos.ts";
1111 import { listEvents } from "../src/core/events.ts";
12 import { putFollow } from "../src/core/upstream_follow.ts";
1213 import { createUser, type User } from "../src/core/users.ts";
1314 import { createForgeApp } from "../src/tui/forge.ts";
1415 import { forksScreen } from "../src/tui/screens/forks.ts";
@@ -181,4 +182,45 @@ describe("the terminal", () => {
181182 expect(text).not.toContain("follow mode");
182183 expect(forkFollowMode(app.db, fork.id)).toBe("tell-me");
183184 });
185
186 test("z undoes the last mode change, and a change to dont-follow offers no undo", async () => {
187 must(await setFollowMode(app, users["bea"]!, fork, "tell-me"));
188 must(await setFollowMode(app, users["bea"]!, fork, "take-updates"));
189 const shown = await open("bea", "");
190 expect(shown).toContain("changed from “Follow and ask”");
191 expect(shown).toContain("undo mode change");
192 const undone = await open("bea", "z");
193 expect(forkFollowMode(app.db, fork.id)).toBe("tell-me");
194 // The undo is itself the latest change, so it carries its own receipt (undoing the undo).
195 expect(undone).toContain("changed from “Follow and merge”");
196 expect(await open("cal", "")).not.toContain("undo mode change");
197 // Someone who cannot write presses z and nothing moves.
198 must(await setFollowMode(app, users["bea"]!, fork, "take-updates"));
199 await open("cal", "z");
200 expect(forkFollowMode(app.db, fork.id)).toBe("take-updates");
201 });
202 });
203
204 describe("undo and dont-follow", () => {
205 test("a fork's own dont-follow is only a mode: it keeps its receipt and the undo works", async () => {
206 const fay = must(await createUser(app.db, { origin: "seed", trusted: true, username: "fay", email: "[email protected]", password: "a-good-passphrase" }));
207 promote(app.db, fay);
208 const own = must(await forkRepo(app, fay, original, "yard-stop"));
209 must(await setFollowMode(app, fay, own, "dont-follow"));
210 expect(lastModeChange(app, own, "dont-follow")?.was).toBe("tell-me");
211 expect((await setFollowMode(app, fay, own, "tell-me")).ok).toBe(true);
212 });
213
214 test("a repository that follows an address is severed by dont-follow: no receipt, and the undo would be refused", async () => {
215 const gus = must(await createUser(app.db, { origin: "seed", trusted: true, username: "gus", email: "[email protected]", password: "a-good-passphrase" }));
216 promote(app.db, gus);
217 const own = must(await createRepo(app, gus, { name: "addr", visibility: "public" }));
218 putFollow(app.db, { repoId: own.id, url: "http://127.0.0.1:9/o/x.git", branch: "main", enabled: false, byId: gus.id, originId: "" });
219 must(await setFollowMode(app, gus, own, "take-updates"));
220 expect(lastModeChange(app, own, "take-updates")?.was).toBe("tell-me");
221 must(await setFollowMode(app, gus, own, "dont-follow"));
222 expect(lastModeChange(app, own, "dont-follow")).toBeUndefined();
223 const again = await setFollowMode(app, gus, own, "tell-me");
224 expect(again.ok).toBe(false);
225 });
184226 });
test/fork-tree-web.test.ts +17 −8
@@ -127,12 +127,10 @@ describe("the fork tree, on one forge", () => {
127127 const before = await (await get("/bea/yard", cookie)).text();
128128 expect(before).toContain("Proposes updates from ann/yard");
129129 expect(before).not.toContain("automatically</strong>");
130 expect(before).toContain("/-/confirm?to=%2Fbea%2Fyard%2Ffollowing%2Fmode%2Ftake-updates");
131
132 const confirm = await (await get("/-/confirm?to=%2Fbea%2Fyard%2Ffollowing%2Fmode%2Ftake-updates", cookie)).text();
133 expect(confirm).toContain("Follows ann/yard automatically?");
134 expect(confirm).toContain("the crew is told to converge");
135 expect(confirm).toContain('action="/bea/yard/following/mode/take-updates"');
130 // One tap: a form that posts the mode, no confirm step; only stopping still asks first.
131 expect(before).toContain('action="/bea/yard/following/mode/take-updates"');
132 expect(before).toContain("/-/confirm?to=%2Fbea%2Fyard%2Ffollowing%2Fmode%2Fdont-follow");
133 expect(before).not.toContain("data-follow-receipt");
136134
137135 const cross = await post("/bea/yard/following/mode/take-updates", cookie, {}, "https://evil.test");
138136 expect(cross.status).toBe(403);
@@ -150,12 +148,22 @@ describe("the fork tree, on one forge", () => {
150148 const receipt = audits()[0]!;
151149 expect(receipt.repoId).toBe(middle.id);
152150 expect(receipt.actorId).toBe(bea.id);
153 expect(receipt.payload).toMatchObject({ repo: "bea/yard", mode: "take-updates" });
151 expect(receipt.payload).toMatchObject({ repo: "bea/yard", mode: "take-updates", was: "tell-me" });
154152
155153 const after = await (await get("/bea/yard", cookie)).text();
156154 expect(after).toContain("Follows ann/yard automatically");
157155 expect(after).toContain('data-follow-mode="take-updates"');
158156 expect(after).toContain("Only propose updates");
157 // The receipt names what it was, and Undo puts it back.
158 expect(after).toContain("data-follow-receipt");
159 expect(after).toContain('action="/bea/yard/following/mode/tell-me"');
160 const readerPage = await (await get("/bea/yard", await signIn("cal"))).text();
161 expect(readerPage).toContain("data-follow-receipt");
162 expect(readerPage).not.toContain('action="/bea/yard/following/mode/tell-me"');
163 expect((await post("/bea/yard/following/mode/tell-me", cookie, {})).status).toBe(303);
164 expect(forkFollowMode(app.db, middle.id)).toBe("tell-me");
165 expect(audits()[0]!.payload).toMatchObject({ mode: "tell-me", was: "take-updates" });
166 expect((await post("/bea/yard/following/mode/take-updates", cookie, {})).status).toBe(303);
159167
160168 // A reader sees the line but no switch.
161169 const reader = await (await get("/bea/yard", await signIn("cal"))).text();
@@ -188,7 +196,8 @@ describe("the fork tree, on one forge", () => {
188196 expect(forks).not.toContain("forks follow this one");
189197 const now = forkFollowMode(app.db, middle.id);
190198 const others = (["take-updates", "tell-me", "dont-follow"] as const).filter((m) => m !== now);
191 for (const m of others) expect(forks).toContain(`following%2Fmode%2F${m}"`);
199 for (const m of others)
200 expect(forks).toMatch(new RegExp(m === "dont-follow" ? `following%2Fmode%2F${m}"` : `action="/bea/yard/following/mode/${m}"`));
192201 expect(forks).not.toContain(`following%2Fmode%2F${now}"`);
193202
194203 const reader = await (await get("/bea/yard/forks", await signIn("cal"))).text();

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs