Public: anyone can read this; the crew can change it.
The fork tree, browsable, and each fork chooses how it follows #994
scout/forge:scout/608-the-fork-tree-browsable-and-each-fork-ch → main 2 commits fork
15 files
changed +380
−33
docs/FOLLOWING.md +13 −1
| @@ -126,10 +126,22 @@ without any simply moves. | ||
| 126 | 126 | - The converge run's assignee is set without the assignment event, so the |
| 127 | 127 | run queues under its own reason and not as an ordinary assignment. |
| 128 | 128 | |
| 129 | ## How a fork follows | |
| 130 | ||
| 131 | A fork's writer chooses, on the forks page (`/:owner/:name/forks`) or with the | |
| 132 | MCP `follow {repo, mode}` tool; `following` returns the `mode`. Saved on the | |
| 133 | repository as `follow_mode`: | |
| 134 | ||
| 135 | - `auto` (the default, and what forks always did): a parent that moves is | |
| 136 | followed when git can do it alone — fast-forward, and with autonomy on a | |
| 137 | clean merge or the crew's converge. | |
| 138 | - `tell`: nothing is moved; the fork hears `fork.parent_moved`. | |
| 139 | - `off`: nothing happens. Pressing Take updates or Converge by hand still works. | |
| 140 | ||
| 129 | 141 | ## The way back up |
| 130 | 142 | |
| 131 | 143 | A fork's own work goes upstream in one press. When a fork is ahead of what |
| 132 | it follows, its following card offers **Propose upstream**: one pull opens | |
| 144 | it follows, the forks page offers **Offer this change** (the following card no longer carries a second button for it): one pull opens | |
| 133 | 145 | on the parent from the fork's default branch, its title the one landed |
| 134 | 146 | change's title, or `N changes from owner/fork` when there are several, its |
| 135 | 147 | body one line per change that landed here since the fork last proposed, |
docs/MCP.md +2 −1
| @@ -38,7 +38,8 @@ for a tunnel and a forge that sleeps. | ||
| 38 | 38 | | `audit` | who can do what, per app, read from its contract.json in git: yes, own or no per door, and whether outsiders may direct its crew (`crewOutsiders`) | | |
| 39 | 39 | | `outsiders` | `{ repo, on?, dryRun? }`: whether people who cannot write may direct the crew; with `on`, planned first (`dryRun` is true unless `false`); set only by the app's admin with an admin token, never an agent | yes | |
| 40 | 40 | | `probe` | knock on a running app's doors as nobody, a user and an admin; the ones that disobey its contract | | |
| 41 | | `following` | where a fork stands against what it follows, and what the crew is doing about it | | | |
| 41 | | `following` | where a fork stands against what it follows, its follow `mode`, and what the crew is doing about it | | | |
| 42 | | `follow` | `{ repo, mode }`: `auto`, `tell` or `off`, how a fork follows its parent; a writer's to say | yes | | |
| 42 | 43 | | `spawn` | `{ name, apps?, crew?, dryRun? }`: a child forge, planned first (`dryRun` is true unless `false`); an admin's, and an `ro` token may plan but not make | yes | |
| 43 | 44 | | `claim_link` | `{ name }`: a new claim link for a child you spawned that nobody has claimed; the old one stops working. An admin's | yes | |
| 44 | 45 | | `take_updates` | a fork takes its parent's updates when git can do it alone; a conflict is refused | yes | |
src/actions/forks.ts +24 −1
| @@ -18,6 +18,8 @@ import { | ||
| 18 | 18 | import { |
| 19 | 19 | canRead, |
| 20 | 20 | forkParentFor, |
| 21 | updateRepoSettings, | |
| 22 | type FollowMode, | |
| 21 | 23 | type Repo, |
| 22 | 24 | } from "../core/repos.ts"; |
| 23 | 25 | import { commitIdentity } from "../core/commit_identity.ts"; |
| @@ -25,7 +27,7 @@ import type { User } from "../core/users.ts"; | ||
| 25 | 27 | import { divergence } from "../git/branches.ts"; |
| 26 | 28 | import { resolveOid } from "../git/merge.ts"; |
| 27 | 29 | import { repoDir } from "../git/repos.ts"; |
| 28 | import { err, type Result } from "../kernel/result.ts"; | |
| 30 | import { err, ok, type Result } from "../kernel/result.ts"; | |
| 29 | 31 | import { requireWriter, type AccessError } from "./access.ts"; |
| 30 | 32 | |
| 31 | 33 | export type { ForkRelation, ForkSync, TakeError } from "../core/following.ts"; |
| @@ -65,6 +67,27 @@ export async function relationOf( | ||
| 65 | 67 | return forkRelation(app, fork, parent); |
| 66 | 68 | } |
| 67 | 69 | |
| 70 | /** Choose how a fork follows its parent; a writer's to say, a fork's only. */ | |
| 71 | export function setFollowMode( | |
| 72 | app: App, | |
| 73 | actor: User | undefined, | |
| 74 | fork: Repo, | |
| 75 | mode: FollowMode, | |
| 76 | ): Result<Repo, AccessError | "not_a_fork"> { | |
| 77 | const writer = requireWriter(app, actor, fork); | |
| 78 | if (!writer.ok) return writer; | |
| 79 | if (fork.forkParentId === null) return err("not_a_fork"); | |
| 80 | if (fork.followMode === mode) return ok(fork); | |
| 81 | const saved = updateRepoSettings(app.db, fork, { followMode: mode }); | |
| 82 | app.bus.append({ | |
| 83 | kind: "repo.follow_mode", | |
| 84 | repoId: fork.id, | |
| 85 | actorId: writer.value.id, | |
| 86 | payload: { from: fork.followMode, to: mode }, | |
| 87 | }); | |
| 88 | return ok(saved); | |
| 89 | } | |
| 90 | ||
| 68 | 91 | export type TakeUpdatesError = AccessError | "not_a_fork" | TakeError; |
| 69 | 92 | |
| 70 | 93 | /** |
src/core/event_audience.ts +2 −1
| @@ -42,7 +42,8 @@ export function eventAudience(kind: string): EventAudience { | ||
| 42 | 42 | // theirs to see, never the readers' (`actions/reports.ts`). |
| 43 | 43 | kind.startsWith("report.") || |
| 44 | 44 | kind === "repo.settings_updated" || |
| 45 | kind === "repo.crew_outsiders" | |
| 45 | kind === "repo.crew_outsiders" || | |
| 46 | kind === "repo.follow_mode" | |
| 46 | 47 | ) |
| 47 | 48 | return "admins"; |
| 48 | 49 | // A fleet operation spans repositories; its receipt is its actor's. |
src/core/following.ts +17 −1
| @@ -96,7 +96,22 @@ export function follow(forge: Following): () => void { | ||
| 96 | 96 | }), |
| 97 | 97 | ); |
| 98 | 98 | if (moved.some((m) => m.ref === `refs/heads/${repo.defaultBranch}`)) |
| 99 | for (const fork of listForks(forge.db, repo.id)) | |
| 99 | for (const fork of listForks(forge.db, repo.id)) { | |
| 100 | // The fork's owner chose how it follows: `off` is left alone, `tell` | |
| 101 | // hears that the parent moved and nothing else happens. | |
| 102 | if (fork.followMode === "off") continue; | |
| 103 | if (fork.followMode === "tell") { | |
| 104 | forge.bus.append({ | |
| 105 | kind: "fork.parent_moved", | |
| 106 | repoId: fork.id, | |
| 107 | actorId: actor, | |
| 108 | payload: { | |
| 109 | parent: `${repo.ownerName}/${repo.name}`, | |
| 110 | parentId: repo.id, | |
| 111 | }, | |
| 112 | }); | |
| 113 | continue; | |
| 114 | } | |
| 100 | 115 | fastForwardFork(forge, fork, repo, actor) |
| 101 | 116 | .then((followed) => { |
| 102 | 117 | // A fork with work of its own cannot be moved; it is told the |
| @@ -120,6 +135,7 @@ export function follow(forge: Following): () => void { | ||
| 120 | 135 | error: String(error), |
| 121 | 136 | }), |
| 122 | 137 | ); |
| 138 | } | |
| 123 | 139 | }); |
| 124 | 140 | } |
| 125 | 141 |
src/core/repos.ts +17 −1
| @@ -98,6 +98,8 @@ export interface Repo { | ||
| 98 | 98 | readonly autonomyCeilingUsd: number; |
| 99 | 99 | readonly autonomySlots: number; |
| 100 | 100 | readonly autonomyBy: string | null; |
| 101 | /** How this fork follows its parent; see `FollowMode`. */ | |
| 102 | readonly followMode: FollowMode; | |
| 101 | 103 | /** |
| 102 | 104 | * "Outsiders may trigger the crew": whether someone who cannot write here |
| 103 | 105 | * may still start or steer a run by speaking (`crew/direct.ts`). Off by |
| @@ -124,6 +126,16 @@ export interface Repo { | ||
| 124 | 126 | readonly counts?: RepoCounts; |
| 125 | 127 | } |
| 126 | 128 | |
| 129 | /** | |
| 130 | * How a fork follows its parent: `auto` takes what git can take alone, | |
| 131 | * `tell` only says the parent moved, `off` leaves the fork as it is. | |
| 132 | */ | |
| 133 | export const FOLLOW_MODES = ["auto", "tell", "off"] as const; | |
| 134 | export type FollowMode = (typeof FOLLOW_MODES)[number]; | |
| 135 | export function isFollowMode(v: unknown): v is FollowMode { | |
| 136 | return FOLLOW_MODES.includes(v as FollowMode); | |
| 137 | } | |
| 138 | ||
| 127 | 139 | interface RepoRow { |
| 128 | 140 | id: string; |
| 129 | 141 | owner_id: string; |
| @@ -141,6 +153,7 @@ interface RepoRow { | ||
| 141 | 153 | autonomy_slots: number; |
| 142 | 154 | autonomy_by: string | null; |
| 143 | 155 | crew_outsiders: number; |
| 156 | follow_mode: string; | |
| 144 | 157 | direction_issue_id: string | null; |
| 145 | 158 | fork_parent_id: string | null; |
| 146 | 159 | sealed_for: string | null; |
| @@ -177,6 +190,7 @@ function toRepo(row: RepoRow): Repo { | ||
| 177 | 190 | autonomySlots: row.autonomy_slots, |
| 178 | 191 | autonomyBy: row.autonomy_by, |
| 179 | 192 | crewOutsiders: row.crew_outsiders === 1, |
| 193 | followMode: isFollowMode(row.follow_mode) ? row.follow_mode : "auto", | |
| 180 | 194 | directionIssueId: row.direction_issue_id, |
| 181 | 195 | forkParentId: row.fork_parent_id, |
| 182 | 196 | sealedFor: row.sealed_for, |
| @@ -983,6 +997,7 @@ export interface RepoSettings { | ||
| 983 | 997 | autonomyBy?: string | null; |
| 984 | 998 | /** Whether someone who cannot write here may start or steer a run. */ |
| 985 | 999 | crewOutsiders?: boolean; |
| 1000 | followMode?: FollowMode; | |
| 986 | 1001 | } |
| 987 | 1002 | |
| 988 | 1003 | /** Update the mutable repo fields, leaving unspecified ones untouched. */ |
| @@ -995,7 +1010,7 @@ export function updateRepoSettings( | ||
| 995 | 1010 | `UPDATE repo SET visibility = ?1, description = ?2, protect_default = ?3, |
| 996 | 1011 | default_branch = ?4, require_review = ?5, require_checks = ?7, |
| 997 | 1012 | autonomy = ?8, autonomy_direction = ?9, autonomy_ceiling_usd = ?10, |
| 998 | autonomy_slots = ?11, autonomy_by = ?12, crew_outsiders = ?13 | |
| 1013 | autonomy_slots = ?11, autonomy_by = ?12, crew_outsiders = ?13, follow_mode = ?14 | |
| 999 | 1014 | WHERE id = ?6`, |
| 1000 | 1015 | ).run( |
| 1001 | 1016 | settings.visibility ?? repo.visibility, |
| @@ -1011,6 +1026,7 @@ export function updateRepoSettings( | ||
| 1011 | 1026 | settings.autonomySlots ?? repo.autonomySlots, |
| 1012 | 1027 | settings.autonomyBy === undefined ? repo.autonomyBy : settings.autonomyBy, |
| 1013 | 1028 | (settings.crewOutsiders ?? repo.crewOutsiders) ? 1 : 0, |
| 1029 | settings.followMode ?? repo.followMode, | |
| 1014 | 1030 | ); |
| 1015 | 1031 | return getRepoById(db, repo.id)!; |
| 1016 | 1032 | } |
src/core/schema.ts +6
| @@ -1540,4 +1540,10 @@ export const migrations: readonly string[] = [ | ||
| 1540 | 1540 | SELECT RAISE(ABORT, 'a sealed repository is never public'); |
| 1541 | 1541 | END; |
| 1542 | 1542 | `, |
| 1543 | // 71 — how a fork follows its parent (#608): `auto` takes what git can | |
| 1544 | // take on its own (today's behaviour), `tell` only says the parent moved, | |
| 1545 | // `off` leaves the fork alone. Meaningless on a repository that is no fork. | |
| 1546 | ` | |
| 1547 | ALTER TABLE repo ADD COLUMN follow_mode TEXT NOT NULL DEFAULT 'auto'; | |
| 1548 | `, | |
| 1543 | 1549 | ]; |
src/crew/converge.ts +1 −1
| @@ -207,7 +207,7 @@ export function followDivergence(app: App): () => void { | ||
| 207 | 207 | const parent = |
| 208 | 208 | typeof parentId === "string" ? getRepoById(app.db, parentId) : undefined; |
| 209 | 209 | if (fork === undefined || parent === undefined) return; |
| 210 | if (fork.autonomy === "off") return; | |
| 210 | if (fork.autonomy === "off" || fork.followMode !== "auto") return; | |
| 211 | 211 | diverged(app, fork, parent).catch((error) => |
| 212 | 212 | log.error("could not act on a diverged fork", { |
| 213 | 213 | fork: `${fork.ownerName}/${fork.name}`, |
src/design/activity.ts +1
| @@ -111,6 +111,7 @@ const WORDS: Record<string, string> = { | ||
| 111 | 111 | "repo.created": "created a repository", |
| 112 | 112 | "repo.forked": "forked a repository", |
| 113 | 113 | "repo.settings_updated": "changed repository settings", |
| 114 | "repo.follow_mode": "changed how the fork follows its parent", | |
| 114 | 115 | "repo.crew_outsiders": "changed who may trigger the crew", |
| 115 | 116 | "repo.mirrored": "mirrored a repository", |
| 116 | 117 | "repo.imported": "imported issues and pulls", |
src/http/mcp.ts +34 −1
| @@ -19,7 +19,7 @@ | ||
| 19 | 19 | |
| 20 | 20 | import { mayView, mayViewId, sealedScope } from "../core/sealed.ts"; |
| 21 | 21 | import { changeCapacity, listCapacity } from "../actions/capacity.ts"; |
| 22 | import { relationOf, takeUpdates } from "../actions/forks.ts"; | |
| 22 | import { relationOf, setFollowMode, takeUpdates } from "../actions/forks.ts"; | |
| 23 | 23 | import { watchMilestone } from "../actions/watches.ts"; |
| 24 | 24 | import { |
| 25 | 25 | addCommentThrough, |
| @@ -66,6 +66,8 @@ import { | ||
| 66 | 66 | canWrite, |
| 67 | 67 | forkParentFor, |
| 68 | 68 | getRepo, |
| 69 | FOLLOW_MODES, | |
| 70 | isFollowMode, | |
| 69 | 71 | ownerFactRepoIds, |
| 70 | 72 | type Repo, |
| 71 | 73 | } from "../core/repos.ts"; |
| @@ -742,9 +744,40 @@ const TOOLS: readonly Tool[] = [ | ||
| 742 | 744 | clean: relation.clean, |
| 743 | 745 | conflicts: relation.conflicts, |
| 744 | 746 | converge: converge?.kind ?? null, |
| 747 | mode: repo.followMode, | |
| 745 | 748 | }); |
| 746 | 749 | }, |
| 747 | 750 | }, |
| 751 | { | |
| 752 | name: "follow", | |
| 753 | description: | |
| 754 | "Choose how a fork follows the repository it came from: `auto` takes the updates git can take on its own, `tell` only notes that the parent moved, `off` leaves the fork alone. A writer's to say.", | |
| 755 | inputSchema: { | |
| 756 | type: "object", | |
| 757 | properties: { | |
| 758 | repo: REPO_ARG, | |
| 759 | mode: { type: "string", enum: [...FOLLOW_MODES] }, | |
| 760 | }, | |
| 761 | required: ["repo", "mode"], | |
| 762 | }, | |
| 763 | writes: true, | |
| 764 | async run(ctx, user, args) { | |
| 765 | const repo = repoArg(ctx, user, args); | |
| 766 | if (isAnswer(repo)) return repo; | |
| 767 | const mode = str(args["mode"]); | |
| 768 | if (!isFollowMode(mode)) | |
| 769 | return bad("invalid", "mode is auto, tell or off"); | |
| 770 | const set = setFollowMode(ctx.app, user, repo, mode); | |
| 771 | if (!set.ok) | |
| 772 | return bad( | |
| 773 | set.error, | |
| 774 | set.error === "not_a_fork" | |
| 775 | ? "this repository follows nothing" | |
| 776 | : "only a writer chooses how a fork follows", | |
| 777 | ); | |
| 778 | return good({ mode: set.value.followMode }); | |
| 779 | }, | |
| 780 | }, | |
| 748 | 781 | { |
| 749 | 782 | name: "spawn", |
| 750 | 783 | description: |
src/web/pages/following.ts +83 −21
| @@ -13,11 +13,15 @@ | ||
| 13 | 13 | import { DIRECTION_NOTE } from "../../crew/direct.ts"; |
| 14 | 14 | import { |
| 15 | 15 | relationOf, |
| 16 | setFollowMode, | |
| 16 | 17 | takeUpdates, |
| 17 | 18 | type ForkRelation, |
| 18 | 19 | } from "../../actions/forks.ts"; |
| 19 | 20 | import { listCrewRoles } from "../../core/agents.ts"; |
| 20 | 21 | import { |
| 22 | FOLLOW_MODES, | |
| 23 | type FollowMode, | |
| 24 | isFollowMode, | |
| 21 | 25 | canRead, |
| 22 | 26 | canWrite, |
| 23 | 27 | forkParentFor, |
| @@ -156,6 +160,67 @@ export function relationSentence(relation: ForkRelation): string { | ||
| 156 | 160 | } |
| 157 | 161 | } |
| 158 | 162 | |
| 163 | const MODE_WORDS: Record<FollowMode, [string, string]> = { | |
| 164 | auto: ["Take green updates", "Updates git can take alone are taken."], | |
| 165 | tell: ["Tell me", "The fork is only told when its parent moves."], | |
| 166 | off: ["Don't follow", "The fork stays as it is."], | |
| 167 | }; | |
| 168 | ||
| 169 | /** | |
| 170 | * How this fork follows, and the one offer it can make: on the forks page, | |
| 171 | * for a writer of a fork. Choosing the mode is the action; a fork ahead of | |
| 172 | * its parent also offers its change up. | |
| 173 | */ | |
| 174 | export async function followPanel( | |
| 175 | ctx: Ctx, | |
| 176 | repo: Repo, | |
| 177 | ): Promise<Html | null> { | |
| 178 | if (repo.forkParentId === null) return null; | |
| 179 | if (!canWrite(ctx.app.db, repo, ctx.auth.user)) return null; | |
| 180 | const following = await followingOf(ctx, repo); | |
| 181 | if (following === undefined) return null; | |
| 182 | const { relation } = following; | |
| 183 | const parent = relation.parent; | |
| 184 | const base = repoBase(repo); | |
| 185 | const proposal = | |
| 186 | relation.ahead > 0 ? openProposal(ctx.app, repo, parent) : undefined; | |
| 187 | return html`<section class="card follow-panel" id="follow"> | |
| 188 | <div class="card-body"> | |
| 189 | <p class="following-sentence">${relationSentence(relation)}</p> | |
| 190 | <form method="post" action="${base}/following" class="following-actions"> | |
| 191 | <input type="hidden" name="do" value="mode" /> | |
| 192 | ${FOLLOW_MODES.map((m) => | |
| 193 | btn(MODE_WORDS[m][0], { | |
| 194 | size: "sm", | |
| 195 | type: "submit", | |
| 196 | name: "mode", | |
| 197 | value: m, | |
| 198 | variant: repo.followMode === m ? "primary" : "secondary", | |
| 199 | }), | |
| 200 | )} | |
| 201 | </form> | |
| 202 | <p class="muted small following-foot">${MODE_WORDS[repo.followMode][1]}</p> | |
| 203 | ${relation.ahead === 0 | |
| 204 | ? null | |
| 205 | : proposal === undefined | |
| 206 | ? html`<form method="post" action="${base}/following" class="following-actions"> | |
| 207 | <input type="hidden" name="do" value="propose" /> | |
| 208 | ${btn("Offer this change", { | |
| 209 | size: "sm", | |
| 210 | type: "submit", | |
| 211 | icon: "merge", | |
| 212 | })} | |
| 213 | </form>` | |
| 214 | : html`<p class="following-actions"> | |
| 215 | ${btn("See the offer", { | |
| 216 | size: "sm", | |
| 217 | href: `/${parent.ownerName}/${parent.name}/pulls/${proposal.number}`, | |
| 218 | })} | |
| 219 | </p>`} | |
| 220 | </div> | |
| 221 | </section>`; | |
| 222 | } | |
| 223 | ||
| 159 | 224 | /** |
| 160 | 225 | * The card on a fork's home. `studio` swaps nothing but the words for the |
| 161 | 226 | * crew's part; the mechanics are one and the same. |
| @@ -212,27 +277,6 @@ export function followingCard( | ||
| 212 | 277 | }), |
| 213 | 278 | ); |
| 214 | 279 | |
| 215 | // The fork's own work, offered upstream in one click: the words come | |
| 216 | // from what landed here, so nothing waits on the crew. | |
| 217 | if (writer && relation.ahead > 0) { | |
| 218 | const proposal = openProposal(ctx.app, repo, parent); | |
| 219 | actions.push( | |
| 220 | proposal === undefined | |
| 221 | ? html`<form method="post" action="${base}/following"> | |
| 222 | <input type="hidden" name="do" value="propose" /> | |
| 223 | ${btn("Propose upstream", { | |
| 224 | size: "sm", | |
| 225 | type: "submit", | |
| 226 | icon: "merge", | |
| 227 | })} | |
| 228 | </form>` | |
| 229 | : btn("See the upstream proposal", { | |
| 230 | size: "sm", | |
| 231 | href: `/${parent.ownerName}/${parent.name}/pulls/${proposal.number}`, | |
| 232 | }), | |
| 233 | ); | |
| 234 | } | |
| 235 | ||
| 236 | 280 | const foot: Html | null = |
| 237 | 281 | converge === undefined |
| 238 | 282 | ? conflict |
| @@ -321,6 +365,24 @@ export async function followingSubmit(ctx: Ctx): Promise<Response> { | ||
| 321 | 365 | }); |
| 322 | 366 | const parentName = `${parent.ownerName}/${parent.name}`; |
| 323 | 367 | |
| 368 | if (field(form, "do") === "mode") { | |
| 369 | const mode = field(form, "mode"); | |
| 370 | const to = `${base}/forks`; | |
| 371 | if (!isFollowMode(mode)) | |
| 372 | return flashOrJson(ctx, to, { | |
| 373 | tone: "danger", | |
| 374 | message: "Choose take green updates, tell me, or don't follow.", | |
| 375 | }); | |
| 376 | const set = setFollowMode(ctx.app, ctx.auth.user, repo, mode); | |
| 377 | if (!set.ok) | |
| 378 | return flashOrJson(ctx, to, { | |
| 379 | tone: "danger", | |
| 380 | message: "You need write access to choose how this follows.", | |
| 381 | }); | |
| 382 | return flashOrJson(ctx, to, { | |
| 383 | message: `${MODE_WORDS[mode][0]}: ${MODE_WORDS[mode][1]}`, | |
| 384 | }); | |
| 385 | } | |
| 324 | 386 | if (field(form, "do") === "converge") { |
| 325 | 387 | if (!canWrite(ctx.app.db, repo, ctx.auth.user)) |
| 326 | 388 | return flashOrJson(ctx, base, { |
src/web/pages/repo.ts +3 −1
| @@ -12,7 +12,7 @@ import { canRead, listForks, type Repo } from "../../core/repos.ts"; | ||
| 12 | 12 | import { FORKS_PER_DAY, forkRepo } from "../../actions/repos.ts"; |
| 13 | 13 | import { forkDrift, takeUpdates, type ForkDrift } from "../../actions/forks.ts"; |
| 14 | 14 | import { lineageOf } from "../../core/following.ts"; |
| 15 | import { followingCard, followingOf, lineageCard } from "./following.ts"; | |
| 15 | import { followingCard, followingOf, followPanel, lineageCard } from "./following.ts"; | |
| 16 | 16 | import { pinRepo, syncRepoNow, unpinRepo } from "../../actions/origins.ts"; |
| 17 | 17 | import { |
| 18 | 18 | canAdmin, |
| @@ -132,7 +132,9 @@ export async function forksPage(ctx: Ctx): Promise<Response> { | ||
| 132 | 132 | const lineage = await lineageOf(ctx.app, repo, (r) => |
| 133 | 133 | canRead(ctx.app.db, r, ctx.auth.user), |
| 134 | 134 | ); |
| 135 | const panel = await followPanel(ctx, repo); | |
| 135 | 136 | const body = html`${repoHeader(ctx, repo, "forks")} |
| 137 | ${panel} | |
| 136 | 138 | <div class="page-head mt"> |
| 137 | 139 | <h2>${forks.length} fork${forks.length === 1 ? "" : "s"}</h2> |
| 138 | 140 | </div> |
test/follow_mode.test.ts added +173
| @@ -0,0 +1,173 @@ | ||
| 1 | /** | |
| 2 | * How a fork chooses to follow (#608): saved from the forks page and from | |
| 3 | * MCP, and honoured when the parent moves. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts"; | |
| 11 | import { listEvents } from "../src/core/events.ts"; | |
| 12 | import { forkRelation } from "../src/core/following.ts"; | |
| 13 | import { getRepoById, type Repo } from "../src/core/repos.ts"; | |
| 14 | import { createToken } from "../src/core/tokens.ts"; | |
| 15 | import { createUser, type User } from "../src/core/users.ts"; | |
| 16 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 17 | ||
| 18 | describe("a fork chooses how it follows", () => { | |
| 19 | let dataDir: string; | |
| 20 | let app: App; | |
| 21 | let servers: Servers; | |
| 22 | let base: string; | |
| 23 | let alice: User; | |
| 24 | let bob: User; | |
| 25 | let eve: User; | |
| 26 | let bobToken: string; | |
| 27 | let eveToken: string; | |
| 28 | let parent: Repo; | |
| 29 | const forks: Record<string, Repo> = {}; | |
| 30 | const tokens: Record<string, string> = {}; | |
| 31 | const fresh = (r: Repo) => getRepoById(app.db, r.id)!; | |
| 32 | const commit = (who: User, repo: Repo, path: string) => | |
| 33 | commitToBranch(app, who, repo, { | |
| 34 | message: path, | |
| 35 | files: [{ path, content: `${path}\n` }], | |
| 36 | }); | |
| 37 | const post = (token: string, repo: string, body: string) => | |
| 38 | fetch(`${base}/${repo}/following`, { | |
| 39 | method: "POST", | |
| 40 | headers: { | |
| 41 | authorization: `Bearer ${token}`, | |
| 42 | "content-type": "application/x-www-form-urlencoded", | |
| 43 | accept: "application/json", | |
| 44 | }, | |
| 45 | body, | |
| 46 | }); | |
| 47 | const mcp = async (token: string, name: string, args: unknown) => { | |
| 48 | const res = await fetch(`${base}/mcp`, { | |
| 49 | method: "POST", | |
| 50 | headers: { | |
| 51 | authorization: `Bearer ${token}`, | |
| 52 | "content-type": "application/json", | |
| 53 | }, | |
| 54 | body: JSON.stringify({ | |
| 55 | jsonrpc: "2.0", | |
| 56 | id: 1, | |
| 57 | method: "tools/call", | |
| 58 | params: { name, arguments: args }, | |
| 59 | }), | |
| 60 | }); | |
| 61 | const r = (await res.json()) as any; | |
| 62 | const text: string = r.result.content[0].text; | |
| 63 | return r.result.isError === true | |
| 64 | ? { error: text } | |
| 65 | : { value: JSON.parse(text) }; | |
| 66 | }; | |
| 67 | ||
| 68 | beforeAll(async () => { | |
| 69 | dataDir = await mkdtemp(join(tmpdir(), "forge-follow-mode-")); | |
| 70 | app = createApp({ dataDir, port: 0 }); | |
| 71 | servers = await serve(app); | |
| 72 | base = servers.url; | |
| 73 | const mk = async (username: string, first = false) => { | |
| 74 | const u = await createUser(app.db, { | |
| 75 | trusted: true, | |
| 76 | username, | |
| 77 | email: `${username}@forge.local`, | |
| 78 | password: "a-good-passphrase", | |
| 79 | adminIfFirst: first, | |
| 80 | }); | |
| 81 | if (!u.ok) throw new Error(u.error); | |
| 82 | tokens[username] = createToken(app.db, u.value.id, "t", "rw").token; | |
| 83 | return u.value; | |
| 84 | }; | |
| 85 | alice = await mk("alice", true); | |
| 86 | bob = await mk("bob"); | |
| 87 | eve = await mk("eve"); | |
| 88 | bobToken = tokens["bob"]!; | |
| 89 | eveToken = tokens["eve"]!; | |
| 90 | const made = await createRepo(app, alice, { name: "game", visibility: "public" }); | |
| 91 | if (!made.ok) throw new Error(String(made.error)); | |
| 92 | parent = made.value; | |
| 93 | await commit(alice, parent, "README.md"); | |
| 94 | for (const name of ["auto", "tell", "off"]) { | |
| 95 | const f = await forkRepo(app, name === "off" ? bob : await mk(`f${name}`), parent); | |
| 96 | if (!f.ok) throw new Error(String(f.error)); | |
| 97 | forks[name] = f.value; | |
| 98 | } | |
| 99 | }, 30_000); | |
| 100 | ||
| 101 | afterAll(async () => { | |
| 102 | servers.stop(); | |
| 103 | app.close(); | |
| 104 | await rm(dataDir, { recursive: true, force: true }); | |
| 105 | }); | |
| 106 | ||
| 107 | test("the owner saves a mode from the forks page; a stranger cannot", async () => { | |
| 108 | expect(fresh(forks["auto"]!).followMode).toBe("auto"); | |
| 109 | const page = await fetch(`${base}/ftell/game/forks`, { | |
| 110 | headers: { authorization: `Bearer ${tokens["ftell"]}`, accept: "text/html" }, | |
| 111 | }).then((r) => r.text()); | |
| 112 | expect(page).toContain("Take green updates"); | |
| 113 | expect(page).toContain("Tell me"); | |
| 114 | expect(page).toContain("Don't follow"); | |
| 115 | expect((await post(tokens["ftell"]!, "ftell/game", "do=mode&mode=tell")).status).toBe(200); | |
| 116 | expect((await post(bobToken, "bob/game", "do=mode&mode=off")).status).toBe(200); | |
| 117 | expect(fresh(forks["tell"]!).followMode).toBe("tell"); | |
| 118 | expect(fresh(forks["off"]!).followMode).toBe("off"); | |
| 119 | ||
| 120 | expect((await post(tokens["fauto"]!, "fauto/game", "do=mode&mode=bogus")).status).toBe(422); | |
| 121 | expect((await post(eveToken, "fauto/game", "do=mode&mode=off")).status).toBe(422); | |
| 122 | expect(fresh(forks["auto"]!).followMode).toBe("auto"); | |
| 123 | ||
| 124 | // A stranger's view of the same page carries no controls. | |
| 125 | const seen = await fetch(`${base}/ftell/game/forks`, { | |
| 126 | headers: { authorization: `Bearer ${eveToken}`, accept: "text/html" }, | |
| 127 | }).then((r) => r.text()); | |
| 128 | expect(seen).not.toContain("Tell me"); | |
| 129 | }); | |
| 130 | ||
| 131 | test("converge honours each mode when the parent moves", async () => { | |
| 132 | await commit(alice, parent, "levels.txt"); | |
| 133 | const tip = async (f: Repo) => | |
| 134 | (await forkRelation(app, fresh(f), fresh(parent)))!; | |
| 135 | const until = async (f: Repo) => { | |
| 136 | for (let i = 0; i < 100; i++) { | |
| 137 | if ((await tip(f)).state === "synced") return true; | |
| 138 | await Bun.sleep(40); | |
| 139 | } | |
| 140 | return false; | |
| 141 | }; | |
| 142 | expect(await until(forks["auto"]!)).toBe(true); | |
| 143 | // Give the others the same time; they must not have moved. | |
| 144 | await Bun.sleep(300); | |
| 145 | expect((await tip(forks["tell"]!)).state).toBe("behind"); | |
| 146 | expect((await tip(forks["off"]!)).state).toBe("behind"); | |
| 147 | ||
| 148 | const told = (f: Repo) => | |
| 149 | listEvents(app.db, { repoId: f.id }).filter((e) => e.kind === "fork.parent_moved"); | |
| 150 | expect(told(forks["tell"]!).length).toBe(1); | |
| 151 | expect(told(forks["off"]!).length).toBe(0); | |
| 152 | expect(told(forks["auto"]!).length).toBe(0); | |
| 153 | }); | |
| 154 | ||
| 155 | test("a fork ahead offers its change up from the forks page", async () => { | |
| 156 | await commit(bob, forks["off"]!, "mine.txt"); | |
| 157 | const page = () => | |
| 158 | fetch(`${base}/bob/game/forks`, { | |
| 159 | headers: { authorization: `Bearer ${bobToken}`, accept: "text/html" }, | |
| 160 | }).then((r) => r.text()); | |
| 161 | expect(await page()).toContain("Offer this change"); | |
| 162 | }); | |
| 163 | ||
| 164 | test("MCP reads and sets the mode", async () => { | |
| 165 | expect((await mcp(bobToken, "following", { repo: "bob/game" })).value.mode).toBe("off"); | |
| 166 | const set = await mcp(bobToken, "follow", { repo: "bob/game", mode: "auto" }); | |
| 167 | expect(set.value.mode).toBe("auto"); | |
| 168 | expect(fresh(forks["off"]!).followMode).toBe("auto"); | |
| 169 | expect((await mcp(bobToken, "follow", { repo: "bob/game", mode: "x" })).error).toBeDefined(); | |
| 170 | expect((await mcp(eveToken, "follow", { repo: "bob/game", mode: "off" })).error).toBeDefined(); | |
| 171 | expect((await mcp(bobToken, "follow", { repo: "alice/game", mode: "off" })).error).toBeDefined(); | |
| 172 | }); | |
| 173 | }); |
test/following.test.ts +3 −3
| @@ -606,11 +606,11 @@ describe("a fork that follows its parent", () => { | ||
| 606 | 606 | expect(k.opened).toBe(false); |
| 607 | 607 | expect(k.number).toBe(j.number); |
| 608 | 608 | |
| 609 | // The card offers the way to it now, not another proposal. | |
| 609 | // The forks page offers the way to it now, not another proposal. | |
| 610 | 610 | const page = await ( |
| 611 | await fetch(`${base}/bob/game`, { headers: as(bobToken) }) | |
| 611 | await fetch(`${base}/bob/game/forks`, { headers: as(bobToken) }) | |
| 612 | 612 | ).text(); |
| 613 | expect(page).toContain("See the upstream proposal"); | |
| 613 | expect(page).toContain("See the offer"); | |
| 614 | 614 | expect(page).not.toContain('value="propose"'); |
| 615 | 615 | }); |
| 616 | 616 |
test/mcp.test.ts +1
| @@ -170,6 +170,7 @@ describe("the forge over MCP", () => { | ||
| 170 | 170 | "outsiders", |
| 171 | 171 | "probe", |
| 172 | 172 | "following", |
| 173 | "follow", | |
| 173 | 174 | "spawn", |
| 174 | 175 | "forges", |
| 175 | 176 | "claim_link", |
new above
offline