Public: anyone can read this; the crew can change it.
Waves, first step: a wave is a file in git, and /:owner/:name/waves renders each one as a note #1364
scout/forge:scout/1125-waves-first-step-a-wave-is-a-file-in-git → main 2 commits fork 3 threads · 3 unresolved
11 files
changed +1126
−0
README.md +7
| @@ -488,6 +488,13 @@ work here. | ||
| 488 | 488 | commit history) with README rendering and **syntax highlighting**, protected |
| 489 | 489 | default branches (force-push and deletion refused, honest fast-forwards |
| 490 | 490 | allowed). |
| 491 | - **Waves** — what landed between two releases, one note per | |
| 492 | `waves/<version>.md` in git (`story/wave.ts`: front matter, three to five | |
| 493 | highlights with their why, the merged pulls). `/:owner/:name/waves` lists | |
| 494 | them; Settings → Waves (admin) cuts a draft as a pull, listing the pulls | |
| 495 | merged since the last wave (it refuses while a wave file does not parse, | |
| 496 | and names it). The `waves` MCP tool reads the same: `app` lists them, | |
| 497 | `app` + `version` opens one, with the page's visibility. No model call. | |
| 491 | 498 | - **Issues** — labels, threaded comments, close/reopen, markdown. |
| 492 | 499 | - **Pull requests** — open from a branch, a **beautiful diff viewer** (syntax |
| 493 | 500 | highlighting composed with intra-line word highlighting, mobile-first, |
scripts/gate.ts +1
| @@ -180,6 +180,7 @@ export const GATE: readonly string[] = [ | ||
| 180 | 180 | "test/map.test.ts", |
| 181 | 181 | "test/pillars.test.ts", |
| 182 | 182 | "test/milestones.test.ts", |
| 183 | "test/waves.test.ts", | |
| 183 | 184 | "test/mcp.test.ts", |
| 184 | 185 | "test/name-marks.test.ts", |
| 185 | 186 | "test/new-forge.test.ts", |
src/actions/waves.ts added +97
| @@ -0,0 +1,97 @@ | ||
| 1 | /** | |
| 2 | * Cut a wave: write a *draft* `waves/<version>.md` and propose it as a pull. | |
| 3 | * The list is every pull merged into the default branch that no wave yet | |
| 4 | * lists; the headline and highlights are left for a person or the crew. | |
| 5 | * No model call. The pull is the only way it lands, like any change. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { App } from "../app.ts"; | |
| 9 | import { mergedIntoBranch } from "../core/pulls.ts"; | |
| 10 | import { canAdmin, type Repo } from "../core/repos.ts"; | |
| 11 | import type { User } from "../core/users.ts"; | |
| 12 | import { err, ok, type Result } from "../kernel/result.ts"; | |
| 13 | import { readWaves, waveVersions } from "../story/read.ts"; | |
| 14 | import { draftWave, isVersion, wavePath, writeWave } from "../story/wave.ts"; | |
| 15 | import { editFiles, type EditFilesError } from "./contents.ts"; | |
| 16 | ||
| 17 | export type CutWaveError = | |
| 18 | | { kind: "not_admin" } | |
| 19 | | { kind: "bad_version" } | |
| 20 | | { kind: "exists"; version: string } | |
| 21 | | { kind: "nothing_new" } | |
| 22 | | { kind: "unreadable"; version: string; reason: string } | |
| 23 | | { kind: "malformed"; reason: string } | |
| 24 | | { kind: "edit"; error: EditFilesError }; | |
| 25 | ||
| 26 | export interface CutWave { | |
| 27 | readonly version: string; | |
| 28 | readonly pullNumber: number; | |
| 29 | readonly listed: number; | |
| 30 | } | |
| 31 | ||
| 32 | /** The first wave file that does not parse, if any. */ | |
| 33 | export async function unreadableWave( | |
| 34 | app: App, | |
| 35 | repo: Repo, | |
| 36 | ): Promise<{ version: string; reason: string } | undefined> { | |
| 37 | for (const e of await readWaves(app, repo)) | |
| 38 | if (!e.wave) return { version: e.version, reason: e.problem ?? "unreadable" }; | |
| 39 | return undefined; | |
| 40 | } | |
| 41 | ||
| 42 | /** The pulls a new wave would list, oldest merge first, and none twice. */ | |
| 43 | export async function pendingPulls( | |
| 44 | app: App, | |
| 45 | repo: Repo, | |
| 46 | ): Promise<{ number: number; title: string }[]> { | |
| 47 | const listed = new Set<number>(); | |
| 48 | for (const entry of await readWaves(app, repo)) | |
| 49 | for (const p of entry.wave?.pulls ?? []) listed.add(p.number); | |
| 50 | return mergedIntoBranch(app.db, repo.id, repo.defaultBranch, listed) | |
| 51 | .map((p) => ({ number: p.number, title: p.title })); | |
| 52 | } | |
| 53 | ||
| 54 | export async function cutWave( | |
| 55 | app: App, | |
| 56 | actor: User | undefined, | |
| 57 | repo: Repo, | |
| 58 | version: string, | |
| 59 | now: Date = new Date(), | |
| 60 | ): Promise<Result<CutWave, CutWaveError>> { | |
| 61 | if (!actor || !canAdmin(app.db, repo, actor)) return err({ kind: "not_admin" }); | |
| 62 | if (!isVersion(version)) return err({ kind: "bad_version" }); | |
| 63 | if ((await waveVersions(app, repo)).includes(version)) | |
| 64 | return err({ kind: "exists", version }); | |
| 65 | ||
| 66 | // A wave that does not parse lists pulls we cannot see; cutting now would | |
| 67 | // list them twice. Say which file to mend. | |
| 68 | const broken = await unreadableWave(app, repo); | |
| 69 | if (broken) return err({ kind: "unreadable", ...broken }); | |
| 70 | ||
| 71 | const merged = await pendingPulls(app, repo); | |
| 72 | if (merged.length === 0) return err({ kind: "nothing_new" }); | |
| 73 | ||
| 74 | const wave = draftWave({ | |
| 75 | version, | |
| 76 | date: now.toISOString().slice(0, 10), | |
| 77 | merged, | |
| 78 | base: `/${repo.ownerName}/${repo.name}`, | |
| 79 | }); | |
| 80 | const text = writeWave(wave); | |
| 81 | if (!text.ok) return err({ kind: "malformed", reason: text.error }); | |
| 82 | ||
| 83 | const edited = await editFiles(app, actor, repo, { | |
| 84 | base: repo.defaultBranch, | |
| 85 | newBranch: `wave/${version}`, | |
| 86 | message: `Draft wave ${version}`, | |
| 87 | files: [{ path: wavePath(version), content: text.value }], | |
| 88 | openPull: { | |
| 89 | title: `Wave ${version} (draft)`, | |
| 90 | body: `A draft of wave ${version}: ${wave.pulls.length} merged pulls listed. The headline and highlights are for a person or the crew to write before this lands.`, | |
| 91 | }, | |
| 92 | }); | |
| 93 | if (!edited.ok) return err({ kind: "edit", error: edited.error }); | |
| 94 | const pull = edited.value.pull; | |
| 95 | if (!pull) return err({ kind: "edit", error: { kind: "pull_failed", reason: "none", commit: edited.value.commit } }); | |
| 96 | return ok({ version, pullNumber: pull.number, listed: wave.pulls.length }); | |
| 97 | } |
src/core/pulls.ts +33
| @@ -827,3 +827,36 @@ export function landingTrailers( | ||
| 827 | 827 | line("Co-authored-by", lander), |
| 828 | 828 | ].join("\n"); |
| 829 | 829 | } |
| 830 | ||
| 831 | /** | |
| 832 | * The pulls merged into `baseBranch` of a repository, oldest merge first — | |
| 833 | * what a wave lists — leaving out the numbers in `skip` (those a wave | |
| 834 | * already lists) *before* the first `limit` are taken, so a long history | |
| 835 | * never hides the newest merges. Sealed pulls are left out: a note kept in | |
| 836 | * git is read by whoever reads the repository. | |
| 837 | */ | |
| 838 | export function mergedIntoBranch( | |
| 839 | db: Database, | |
| 840 | repoId: string, | |
| 841 | baseBranch: string, | |
| 842 | skip: ReadonlySet<number> = new Set(), | |
| 843 | limit = 2_000, | |
| 844 | ): Pull[] { | |
| 845 | const ids = db | |
| 846 | .query<{ issue_id: string; number: number }, [string, string]>( | |
outdated
|
||
| 847 | `SELECT pull.issue_id, issue.number FROM pull | |
| 848 | JOIN issue ON issue.id = pull.issue_id | |
| 849 | WHERE issue.repo_id = ?1 AND pull.base_branch = ?2 | |
| 850 | AND pull.merged_at IS NOT NULL AND issue.sealed = 0 | |
| 851 | ORDER BY pull.merged_at ASC, issue.number ASC`, | |
| 852 | ) | |
| 853 | .all(repoId, baseBranch) | |
| 854 | .filter((r) => !skip.has(r.number)) | |
| 855 | .slice(0, limit) | |
| 856 | .map((r) => r.issue_id); | |
| 857 | const issues = getIssuesByIds(db, ids); | |
| 858 | return joinMany( | |
| 859 | db, | |
| 860 | ids.map((id) => issues.get(id)).filter((i): i is Issue => i !== undefined), | |
| 861 | ); | |
| 862 | } |
src/http/mcp.ts +42
| @@ -20,6 +20,8 @@ | ||
| 20 | 20 | import { mayView, mayViewId, sealedScope } from "../core/sealed.ts"; |
| 21 | 21 | import { changeCapacity, listCapacity } from "../actions/capacity.ts"; |
| 22 | 22 | import { relationOf, takeUpdates } from "../actions/forks.ts"; |
| 23 | import { readWave, readWaves } from "../story/read.ts"; | |
| 24 | import { isVersion } from "../story/wave.ts"; | |
| 23 | 25 | import { watchMilestone } from "../actions/watches.ts"; |
| 24 | 26 | import { |
| 25 | 27 | addCommentThrough, |
| @@ -1194,6 +1196,46 @@ const TOOLS: readonly Tool[] = [ | ||
| 1194 | 1196 | : good(data); |
| 1195 | 1197 | }, |
| 1196 | 1198 | }, |
| 1199 | { | |
| 1200 | name: "waves", | |
| 1201 | description: | |
| 1202 | "What landed between two releases, one note per `waves/<version>.md` in git. With `app` alone: every wave, newest first — version, date, headline, whether it is a draft, how many pulls it lists, or `problem` when its file cannot be read. With `version`: that wave in full (highlights and merged pulls). Read like the waves pages: a private app answers a stranger as if it did not exist. Cutting a wave is Settings → Waves, for an admin of the app.", | |
| 1203 | inputSchema: { | |
| 1204 | type: "object", | |
| 1205 | properties: { | |
| 1206 | app: APP_ARG, | |
| 1207 | version: { | |
| 1208 | type: "string", | |
| 1209 | description: "A wave's version, like 1.2.0; the list when absent.", | |
| 1210 | }, | |
| 1211 | }, | |
| 1212 | required: ["app"], | |
| 1213 | }, | |
| 1214 | writes: false, | |
| 1215 | async run(ctx, user, args) { | |
| 1216 | const repo = planRepo(ctx, user, args); | |
| 1217 | if (isAnswer(repo)) return repo; | |
| 1218 | if (args["version"] === undefined) { | |
| 1219 | const entries = await readWaves(ctx.app, repo); | |
| 1220 | return good({ | |
| 1221 | waves: entries.map((e) => ({ | |
| 1222 | version: e.version, | |
| 1223 | date: e.wave?.date ?? null, | |
| 1224 | headline: e.wave?.headline ?? null, | |
| 1225 | draft: e.wave?.draft ?? null, | |
| 1226 | merged: e.wave?.pulls.length ?? null, | |
| 1227 | problem: e.problem, | |
| 1228 | })), | |
| 1229 | }); | |
| 1230 | } | |
| 1231 | const version = str(args["version"]); | |
| 1232 | if (!isVersion(version)) return bad("bad_version", "a version looks like 1.2.3"); | |
| 1233 | const entry = await readWave(ctx.app, repo, version); | |
| 1234 | if (!entry.wave && entry.problem === "the file is missing") | |
| 1235 | return bad("no_such_wave", `no wave ${version}`); | |
| 1236 | return good(entry); | |
| 1237 | }, | |
| 1238 | }, | |
| 1197 | 1239 | { |
| 1198 | 1240 | name: "watch", |
| 1199 | 1241 | description: |
src/http/server.ts +7
| @@ -153,6 +153,7 @@ import { | ||
| 153 | 153 | newTellPage, |
| 154 | 154 | } from "../web/pages/issues.ts"; |
| 155 | 155 | import { tellPage } from "../web/studio/tell.ts"; |
| 156 | import { wavePage, wavesPage } from "../web/pages/waves.ts"; | |
| 156 | 157 | import { globalMapPage, mapPage } from "../web/pages/map.ts"; |
| 157 | 158 | import { |
| 158 | 159 | milestonePage, |
| @@ -285,6 +286,8 @@ import { | ||
| 285 | 286 | repoDelete, |
| 286 | 287 | settingsPage, |
| 287 | 288 | settingsUpdate, |
| 289 | waveCut, | |
| 290 | wavesSettingsPage, | |
| 288 | 291 | webhookCreate, |
| 289 | 292 | webhookDelete, |
| 290 | 293 | } from "../web/pages/settings.ts"; |
| @@ -1096,6 +1099,10 @@ export function buildRouter(): Router<Handler> { | ||
| 1096 | 1099 | router.add("GET", "/:owner/:name/settings/hooks", hooksPage); |
| 1097 | 1100 | router.add("POST", "/:owner/:name/settings/hooks", webhookCreate); |
| 1098 | 1101 | router.add("POST", "/:owner/:name/settings/hooks/:id/delete", webhookDelete); |
| 1102 | router.add("GET", "/:owner/:name/settings/waves", wavesSettingsPage); | |
| 1103 | router.add("POST", "/:owner/:name/settings/waves", waveCut); | |
| 1104 | router.add("GET", "/:owner/:name/waves", wavesPage); | |
| 1105 | router.add("GET", "/:owner/:name/waves/:version", wavePage); | |
| 1099 | 1106 | router.add("GET", "/:owner/:name/settings/danger", dangerPage); |
| 1100 | 1107 | router.add("POST", "/:owner/:name/settings/delete", repoDelete); |
| 1101 | 1108 | router.add( |
src/story/read.ts added +94
| @@ -0,0 +1,94 @@ | ||
| 1 | /** | |
| 2 | * Reading the waves a repository keeps: every `waves/<version>.md` on its | |
| 3 | * default branch, parsed, newest first. A file that does not parse is kept | |
| 4 | * as a plain reason beside its version, so one bad note never hides the | |
| 5 | * rest. Read-only; the caller has already checked who may read the repo. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { App } from "../app.ts"; | |
| 9 | import type { Repo } from "../core/repos.ts"; | |
| 10 | import { blobForRender, listTree } from "../git/read.ts"; | |
| 11 | import { repoDir } from "../git/repos.ts"; | |
| 12 | import { | |
| 13 | compareVersions, | |
| 14 | MAX_WAVE_BYTES, | |
| 15 | parseWave, | |
| 16 | versionOfPath, | |
| 17 | wavePath, | |
| 18 | type Wave, | |
| 19 | } from "./wave.ts"; | |
| 20 | ||
| 21 | const MAX_WAVES = 200; | |
| 22 | ||
| 23 | export interface WaveEntry { | |
| 24 | readonly version: string; | |
| 25 | /** The parsed wave, or why the file could not be. */ | |
| 26 | readonly wave: Wave | null; | |
| 27 | readonly problem: string | null; | |
| 28 | } | |
| 29 | ||
| 30 | const READ_BATCH = 8; | |
| 31 | ||
| 32 | async function readOne( | |
| 33 | dir: string, | |
| 34 | ref: string, | |
| 35 | version: string, | |
| 36 | ): Promise<WaveEntry> { | |
| 37 | const blob = await blobForRender(dir, ref, wavePath(version), MAX_WAVE_BYTES); | |
| 38 | if (blob.kind === "missing") | |
| 39 | return { version, wave: null, problem: "the file is missing" }; | |
| 40 | if (blob.kind !== "text") | |
| 41 | return { version, wave: null, problem: "the file is not a small text file" }; | |
| 42 | const parsed = parseWave(new TextDecoder().decode(blob.body)); | |
| 43 | if (!parsed.ok) return { version, wave: null, problem: parsed.error }; | |
| 44 | if (parsed.value.version !== version) | |
| 45 | return { | |
| 46 | version, | |
| 47 | wave: null, | |
| 48 | problem: `the file says version ${parsed.value.version}`, | |
| 49 | }; | |
| 50 | return { version, wave: parsed.value, problem: null }; | |
| 51 | } | |
| 52 | ||
| 53 | /** The versions kept, newest first. */ | |
| 54 | export async function waveVersions( | |
| 55 | app: App, | |
| 56 | repo: Repo, | |
| 57 | ): Promise<string[]> { | |
| 58 | const dir = repoDir(app.reposRoot, repo.ownerName, repo.name); | |
| 59 | const tree = await listTree(dir, repo.defaultBranch, "waves"); | |
| 60 | if (!tree.ok) return []; | |
| 61 | return tree.value | |
| 62 | .filter((e) => e.type === "blob") | |
| 63 | .map((e) => versionOfPath(`waves/${e.name}`)) | |
| 64 | .filter((v): v is string => v !== null) | |
| 65 | .sort((a, b) => compareVersions(b, a)) | |
| 66 | .slice(0, MAX_WAVES); | |
| 67 | } | |
| 68 | ||
| 69 | /** Every wave on the default branch, newest first. */ | |
| 70 | export async function readWaves(app: App, repo: Repo): Promise<WaveEntry[]> { | |
| 71 | const dir = repoDir(app.reposRoot, repo.ownerName, repo.name); | |
| 72 | const versions = await waveVersions(app, repo); | |
| 73 | const entries: WaveEntry[] = []; | |
| 74 | // A few git reads at a time, not one process per file all at once. | |
| 75 | for (let i = 0; i < versions.length; i += READ_BATCH) | |
| 76 | entries.push( | |
| 77 | ...(await Promise.all( | |
| 78 | versions | |
| 79 | .slice(i, i + READ_BATCH) | |
| 80 | .map((v) => readOne(dir, repo.defaultBranch, v)), | |
| 81 | )), | |
| 82 | ); | |
| 83 | return entries; | |
| 84 | } | |
| 85 | ||
| 86 | /** One wave by version; `version` must already be a valid version. */ | |
| 87 | export function readWave( | |
| 88 | app: App, | |
| 89 | repo: Repo, | |
| 90 | version: string, | |
| 91 | ): Promise<WaveEntry> { | |
| 92 | const dir = repoDir(app.reposRoot, repo.ownerName, repo.name); | |
| 93 | return readOne(dir, repo.defaultBranch, version); | |
| 94 | } |
src/story/wave.ts added +268
| @@ -0,0 +1,268 @@ | ||
| 1 | /** | |
| 2 | * A wave: what landed between two releases, as one short note kept in git | |
| 3 | * at `waves/<version>.md`. This module is the only place that knows the | |
| 4 | * file's shape; it takes text in and gives plain data out, and the other | |
| 5 | * way round, and refuses a malformed file with a plain reason. | |
| 6 | * | |
| 7 | * --- | |
| 8 | * version: 0.3.0 | |
| 9 | * date: 2026-10-14 | |
| 10 | * headline: One sentence for the whole wave | |
| 11 | * --- | |
| 12 | * | |
| 13 | * ## Highlights | |
| 14 | * | |
| 15 | * - **What changed** — why it matters | |
| 16 | * | |
| 17 | * ## Merged | |
| 18 | * | |
| 19 | * - [#12 Title of the pull](/owner/name/pulls/12) | |
| 20 | * | |
| 21 | * Three to five highlights. A *draft* (`draft: true`, written by Cut a wave) | |
| 22 | * may leave the headline and highlights empty for a person to fill in; a | |
| 23 | * published wave may not. | |
| 24 | */ | |
| 25 | ||
| 26 | import { err, ok, type Result } from "../kernel/result.ts"; | |
| 27 | ||
| 28 | export interface WaveHighlight { | |
| 29 | readonly what: string; | |
| 30 | readonly why: string; | |
| 31 | } | |
| 32 | ||
| 33 | export interface WavePull { | |
| 34 | readonly number: number; | |
| 35 | readonly title: string; | |
| 36 | readonly href: string; | |
| 37 | } | |
| 38 | ||
| 39 | export interface Wave { | |
| 40 | readonly version: string; | |
| 41 | /** `YYYY-MM-DD`. */ | |
| 42 | readonly date: string; | |
| 43 | readonly headline: string; | |
| 44 | readonly draft: boolean; | |
| 45 | readonly highlights: readonly WaveHighlight[]; | |
| 46 | readonly pulls: readonly WavePull[]; | |
| 47 | } | |
| 48 | ||
| 49 | export const MIN_HIGHLIGHTS = 3; | |
| 50 | export const MAX_HIGHLIGHTS = 5; | |
| 51 | export const MAX_PULLS = 2_000; | |
| 52 | export const MAX_WAVE_BYTES = 256 * 1024; | |
| 53 | const MAX_LINE = 400; | |
| 54 | ||
| 55 | const VERSION = /^\d{1,6}\.\d{1,6}\.\d{1,6}$/; | |
| 56 | const DATE = /^\d{4}-\d{2}-\d{2}$/; | |
| 57 | ||
| 58 | export const isVersion = (text: string): boolean => VERSION.test(text); | |
| 59 | ||
| 60 | /** Where a wave lives in the repository. */ | |
| 61 | export const wavePath = (version: string): string => `waves/${version}.md`; | |
| 62 | ||
| 63 | /** The version a path names, or null when it is not a wave file. */ | |
| 64 | export function versionOfPath(path: string): string | null { | |
| 65 | const m = /^waves\/(.+)\.md$/.exec(path); | |
| 66 | return m?.[1] !== undefined && isVersion(m[1]) ? m[1] : null; | |
| 67 | } | |
| 68 | ||
| 69 | /** Newest first: compares versions numerically, part by part. */ | |
| 70 | export function compareVersions(a: string, b: string): number { | |
| 71 | const x = a.split(".").map(Number); | |
| 72 | const y = b.split(".").map(Number); | |
| 73 | for (let i = 0; i < 3; i++) { | |
| 74 | const d = (x[i] ?? 0) - (y[i] ?? 0); | |
| 75 | if (d !== 0) return d; | |
| 76 | } | |
| 77 | return 0; | |
| 78 | } | |
| 79 | ||
| 80 | /** The next version after the newest one: a minor bump, or 0.1.0 first. */ | |
| 81 | export function nextVersion(existing: readonly string[]): string { | |
| 82 | const newest = [...existing].sort(compareVersions).pop(); | |
| 83 | if (newest === undefined) return "0.1.0"; | |
| 84 | const [major = "0", minor = "0"] = newest.split("."); | |
| 85 | return `${major}.${Number(minor) + 1}.0`; | |
| 86 | } | |
| 87 | ||
| 88 | const hasControl = (text: string): boolean => /[\u0000-\u001f\u007f]/.test(text); | |
| 89 | ||
| 90 | /** A one-line value: no control bytes, no more than a line's worth. */ | |
| 91 | function lineProblem(label: string, text: string): string | null { | |
| 92 | if (hasControl(text)) return `${label} must be one plain line`; | |
| 93 | if (text.length > MAX_LINE) return `${label} is over ${MAX_LINE} characters`; | |
| 94 | return null; | |
| 95 | } | |
| 96 | ||
| 97 | /** What a draft puts where a person's words go. */ | |
| 98 | export function checkWave(wave: Wave): string | null { | |
| 99 | if (!isVersion(wave.version)) return "the version must look like 1.2.3"; | |
| 100 | if (!DATE.test(wave.date) || Number.isNaN(Date.parse(wave.date))) | |
| 101 | return "the date must look like 2026-10-14"; | |
| 102 | const headline = lineProblem("the headline", wave.headline); | |
| 103 | if (headline) return headline; | |
| 104 | if (wave.headline.trim() !== wave.headline) | |
| 105 | return "the headline has stray spaces at its ends"; | |
| 106 | if (wave.highlights.length > MAX_HIGHLIGHTS) | |
| 107 | return `a wave has at most ${MAX_HIGHLIGHTS} highlights`; | |
| 108 | if (!wave.draft) { | |
| 109 | if (wave.headline === "") return "a wave needs a headline"; | |
| 110 | if (wave.highlights.length < MIN_HIGHLIGHTS) | |
| 111 | return `a wave needs ${MIN_HIGHLIGHTS} to ${MAX_HIGHLIGHTS} highlights`; | |
| 112 | } | |
| 113 | for (const h of wave.highlights) { | |
| 114 | const what = lineProblem("a highlight", h.what); | |
| 115 | if (what) return what; | |
| 116 | const why = lineProblem("a highlight's why", h.why); | |
| 117 | if (why) return why; | |
| 118 | if (h.what.trim() === "" || h.what.includes("**") || h.what !== h.what.trim()) | |
| 119 | return "a highlight needs a plain name without ** or stray spaces"; | |
| 120 | if (h.why.trim() === "" || h.why !== h.why.trim()) | |
| 121 | return "every highlight needs its why"; | |
| 122 | } | |
| 123 | if (wave.pulls.length > MAX_PULLS) return `a wave lists at most ${MAX_PULLS} pulls`; | |
| 124 | for (const p of wave.pulls) { | |
| 125 | if (!Number.isSafeInteger(p.number) || p.number < 1) | |
| 126 | return "a pull needs a number"; | |
| 127 | const title = lineProblem("a pull title", p.title); | |
| 128 | if (title) return title; | |
| 129 | if (/[\[\]]/.test(p.title) || p.title === "" || p.title !== p.title.trim()) | |
| 130 | return "a pull title cannot hold brackets or be empty"; | |
| 131 | if (!/^\/(?!\/)[\w.\-\/]*$/.test(p.href)) return "a pull link must be a local path"; | |
| 132 | } | |
| 133 | return null; | |
| 134 | } | |
| 135 | ||
| 136 | /** A pull title made safe for a link line: no brackets, one line. */ | |
| 137 | export function pullTitle(title: string): string { | |
| 138 | const clean = title | |
| 139 | .replace(/[\u0000-\u001f\u007f]+/g, " ") | |
| 140 | .replace(/\[/g, "(") | |
| 141 | .replace(/\]/g, ")") | |
| 142 | .replace(/\s+/g, " ") | |
| 143 | .trim() | |
| 144 | .slice(0, 200) | |
| 145 | .trim(); | |
| 146 | return clean === "" ? "Untitled" : clean; | |
| 147 | } | |
| 148 | ||
| 149 | /** Wave to text. Refuses a wave `parseWave` would refuse. */ | |
| 150 | export function writeWave(wave: Wave): Result<string, string> { | |
| 151 | const problem = checkWave(wave); | |
| 152 | if (problem) return err(problem); | |
| 153 | const lines = [ | |
| 154 | "---", | |
| 155 | `version: ${wave.version}`, | |
| 156 | `date: ${wave.date}`, | |
| 157 | `headline: ${wave.headline}`, | |
| 158 | ...(wave.draft ? ["draft: true"] : []), | |
| 159 | "---", | |
| 160 | "", | |
| 161 | "## Highlights", | |
| 162 | "", | |
| 163 | ...wave.highlights.map((h) => `- **${h.what}** — ${h.why}`), | |
| 164 | ...(wave.highlights.length > 0 ? [""] : []), | |
| 165 | "## Merged", | |
| 166 | "", | |
| 167 | ...wave.pulls.map((p) => `- [#${p.number} ${p.title}](${p.href})`), | |
| 168 | "", | |
| 169 | ]; | |
| 170 | return ok(lines.join("\n")); | |
| 171 | } | |
| 172 | ||
| 173 | /** Text to wave, or the plain reason the file is malformed. */ | |
| 174 | export function parseWave(text: string): Result<Wave, string> { | |
| 175 | if (text.length > MAX_WAVE_BYTES) return err("the file is too large"); | |
| 176 | const lines = text.replace(/\r\n?/g, "\n").split("\n"); | |
| 177 | if (lines[0] !== "---") return err("the file must start with a --- front matter"); | |
| 178 | const close = lines.indexOf("---", 1); | |
| 179 | if (close === -1) return err("the front matter is never closed with ---"); | |
| 180 | ||
| 181 | const front = new Map<string, string>(); | |
| 182 | for (const line of lines.slice(1, close)) { | |
| 183 | if (line.trim() === "") continue; | |
| 184 | const m = /^([a-z]+): ?(.*)$/.exec(line); | |
| 185 | if (!m || m[1] === undefined) return err(`front matter line "${line.slice(0, 40)}" is not key: value`); | |
| 186 | if (front.has(m[1])) return err(`front matter repeats ${m[1]}`); | |
| 187 | front.set(m[1], m[2] ?? ""); | |
| 188 | } | |
| 189 | for (const key of front.keys()) | |
| 190 | if (!["version", "date", "headline", "draft"].includes(key)) | |
| 191 | return err(`front matter has an unknown key ${key}`); | |
| 192 | const version = front.get("version"); | |
| 193 | const date = front.get("date"); | |
| 194 | const headline = front.get("headline"); | |
| 195 | if (version === undefined) return err("front matter needs a version"); | |
| 196 | if (date === undefined) return err("front matter needs a date"); | |
| 197 | if (headline === undefined) return err("front matter needs a headline"); | |
| 198 | const draftText = front.get("draft"); | |
| 199 | if (draftText !== undefined && draftText !== "true") | |
| 200 | return err("draft can only be true"); | |
| 201 | ||
| 202 | const highlights: WaveHighlight[] = []; | |
| 203 | const pulls: WavePull[] = []; | |
| 204 | let section: "none" | "highlights" | "merged" = "none"; | |
| 205 | const seen = new Set<string>(); | |
| 206 | for (const line of lines.slice(close + 1)) { | |
| 207 | if (line.trim() === "") continue; | |
| 208 | if (line.startsWith("## ")) { | |
| 209 | const name = line.slice(3).trim(); | |
| 210 | if (name !== "Highlights" && name !== "Merged") | |
| 211 | return err(`unknown section "${name.slice(0, 40)}"`); | |
| 212 | if (seen.has(name)) return err(`the ${name} section appears twice`); | |
| 213 | seen.add(name); | |
| 214 | section = name === "Highlights" ? "highlights" : "merged"; | |
| 215 | continue; | |
| 216 | } | |
| 217 | if (section === "highlights") { | |
| 218 | const m = /^- \*\*(.+?)\*\* — (.+)$/.exec(line); | |
| 219 | if (!m || m[1] === undefined || m[2] === undefined) | |
| 220 | return err(`a highlight must read "- **what** — why": "${line.slice(0, 40)}"`); | |
| 221 | highlights.push({ what: m[1], why: m[2] }); | |
| 222 | } else if (section === "merged") { | |
| 223 | const m = /^- \[#(\d+) (.+)\]\((\/[^\s()]*)\)$/.exec(line); | |
| 224 | if (!m || m[1] === undefined || m[2] === undefined || m[3] === undefined) | |
| 225 | return err(`a pull must read "- [#12 Title](/path)": "${line.slice(0, 40)}"`); | |
| 226 | pulls.push({ number: Number(m[1]), title: m[2], href: m[3] }); | |
| 227 | } else { | |
| 228 | return err(`text outside a section: "${line.slice(0, 40)}"`); | |
| 229 | } | |
| 230 | } | |
| 231 | if (!seen.has("Highlights")) return err("the Highlights section is missing"); | |
| 232 | if (!seen.has("Merged")) return err("the Merged section is missing"); | |
| 233 | ||
| 234 | const wave: Wave = { | |
| 235 | version, | |
| 236 | date, | |
| 237 | headline, | |
| 238 | draft: draftText === "true", | |
| 239 | highlights, | |
| 240 | pulls, | |
| 241 | }; | |
| 242 | const problem = checkWave(wave); | |
| 243 | return problem ? err(problem) : ok(wave); | |
| 244 | } | |
| 245 | ||
| 246 | /** | |
| 247 | * A draft for `version` listing `merged` in the order given (oldest merge | |
outdated
|
||
| 248 | * first). The headline and highlights are left for a person. | |
| 249 | */ | |
| 250 | export function draftWave(input: { | |
| 251 | version: string; | |
| 252 | date: string; | |
| 253 | merged: readonly { number: number; title: string }[]; | |
| 254 | base: string; | |
| 255 | }): Wave { | |
| 256 | return { | |
| 257 | version: input.version, | |
| 258 | date: input.date, | |
| 259 | headline: "", | |
| 260 | draft: true, | |
| 261 | highlights: [], | |
| 262 | pulls: input.merged.map((p) => ({ | |
| 263 | number: p.number, | |
| 264 | title: pullTitle(p.title), | |
| 265 | href: `${input.base}/pulls/${p.number}`, | |
| 266 | })), | |
| 267 | }; | |
| 268 | } |
src/web/pages/settings.ts +78
| @@ -37,6 +37,9 @@ import { | ||
| 37 | 37 | type AppEnvName, |
| 38 | 38 | type AppEnvScope, |
| 39 | 39 | } from "../../core/app_env.ts"; |
| 40 | import { cutWave, pendingPulls, unreadableWave } from "../../actions/waves.ts"; | |
| 41 | import { nextVersion } from "../../story/wave.ts"; | |
| 42 | import { waveVersions } from "../../story/read.ts"; | |
| 40 | 43 | import { addToCrew, listCrew, removeFromCrew } from "../../actions/crew.ts"; |
| 41 | 44 | import { getAgentByName, isCrewRole, setCrewRole } from "../../core/agents.ts"; |
| 42 | 45 | import { isAutonomy } from "../../core/repos.ts"; |
| @@ -109,6 +112,7 @@ type Section = | ||
| 109 | 112 | | "prompts" |
| 110 | 113 | | "labels" |
| 111 | 114 | | "hooks" |
| 115 | | "waves" | |
| 112 | 116 | | "danger"; |
| 113 | 117 | |
| 114 | 118 | /** The repo, but only for someone who administers it. A non-admin is told |
| @@ -151,6 +155,12 @@ function sections(base: string, active: Section) { | ||
| 151 | 155 | icon: "webhook" as const, |
| 152 | 156 | current: active === "hooks", |
| 153 | 157 | }, |
| 158 | { | |
| 159 | label: "Waves", | |
| 160 | href: `${base}/settings/waves`, | |
| 161 | icon: "tag" as const, | |
| 162 | current: active === "waves", | |
| 163 | }, | |
| 154 | 164 | { |
| 155 | 165 | label: "Danger zone", |
| 156 | 166 | href: `${base}/settings/danger`, |
| @@ -1782,3 +1792,71 @@ export async function autonomySettingsUpdate(ctx: Ctx): Promise<Response> { | ||
| 1782 | 1792 | }), |
| 1783 | 1793 | }); |
| 1784 | 1794 | } |
| 1795 | ||
| 1796 | // ---- waves ---- | |
| 1797 | ||
| 1798 | /** GET /:owner/:name/settings/waves — Cut a wave. */ | |
| 1799 | export async function wavesSettingsPage(ctx: Ctx): Promise<Response> { | |
| 1800 | const repo = loadAdminRepo(ctx); | |
| 1801 | if (!repo) return repoNotFound(ctx); | |
| 1802 | const base = repoBase(repo); | |
| 1803 | const pending = await pendingPulls(ctx.app, repo); | |
| 1804 | const suggested = nextVersion(await waveVersions(ctx.app, repo)); | |
| 1805 | const broken = await unreadableWave(ctx.app, repo); | |
| 1806 | const body = card( | |
| 1807 | html`${broken | |
| 1808 | ? html`<p class="muted"> | |
| 1809 | Wave ${broken.version} cannot be read (${broken.reason}). Mend | |
| 1810 | <code>waves/${broken.version}.md</code> before cutting another. | |
| 1811 | </p>` | |
| 1812 | : null} | |
| 1813 | <p> | |
| 1814 | ${pending.length} merged ${pending.length === 1 ? "pull is" : "pulls are"} | |
| 1815 | not in any wave yet. Cutting a wave opens a pull with a draft | |
| 1816 | <code>waves/<version>.md</code> that lists them; its headline | |
| 1817 | and highlights are for a person or the crew to write before it lands. | |
| 1818 | </p> | |
| 1819 | <form class="edit-row mt" method="post" action="${base}/settings/waves"> | |
| 1820 | <input | |
| 1821 | class="input" | |
| 1822 | type="text" | |
| 1823 | name="version" | |
| 1824 | value="${suggested}" | |
| 1825 | pattern="[0-9]+\\.[0-9]+\\.[0-9]+" | |
| 1826 | aria-label="Version" | |
| 1827 | required | |
| 1828 | /> | |
| 1829 | ${btn("Cut a wave", { variant: "primary", size: "sm" })} | |
| 1830 | </form> | |
| 1831 | <p class="mt"><a href="${base}/waves">See the waves</a></p>`, | |
| 1832 | { head: html`<h2>Cut a wave</h2>` }, | |
| 1833 | ); | |
| 1834 | return settingsPageResponse(ctx, repo, "waves", "Waves", body); | |
| 1835 | } | |
| 1836 | ||
| 1837 | /** POST /:owner/:name/settings/waves — write the draft as a pull. */ | |
| 1838 | export async function waveCut(ctx: Ctx): Promise<Response> { | |
| 1839 | const gate = await adminForm(ctx); | |
| 1840 | if (gate instanceof Response) return gate; | |
| 1841 | const { repo, form } = gate; | |
| 1842 | const back = `${repoBase(repo)}/settings/waves`; | |
| 1843 | const cut = await cutWave(ctx.app, ctx.auth.user, repo, field(form, "version").trim()); | |
| 1844 | if (cut.ok) | |
| 1845 | return flashTo(ctx, `${repoBase(repo)}/pulls/${cut.value.pullNumber}`, { | |
| 1846 | message: `Draft wave ${cut.value.version} is open as a pull, listing ${cut.value.listed} merged ${cut.value.listed === 1 ? "pull" : "pulls"}.`, | |
| 1847 | }); | |
| 1848 | const e = cut.error; | |
| 1849 | const message = | |
| 1850 | e.kind === "bad_version" | |
| 1851 | ? "The version must look like 1.2.3." | |
| 1852 | : e.kind === "exists" | |
| 1853 | ? `Wave ${e.version} already exists.` | |
| 1854 | : e.kind === "unreadable" | |
| 1855 | ? `Wave ${e.version} cannot be read (${e.reason}); mend waves/${e.version}.md first.` | |
| 1856 | : e.kind === "nothing_new" | |
| 1857 | ? "Nothing has merged since the last wave." | |
| 1858 | : e.kind === "edit" | |
| 1859 | ? "The draft could not be opened as a pull — the branch may already exist." | |
| 1860 | : "The wave could not be cut."; | |
| 1861 | return flashTo(ctx, back, { tone: "danger", message }); | |
| 1862 | } |
src/web/pages/waves.ts added +125
| @@ -0,0 +1,125 @@ | ||
| 1 | /** | |
| 2 | * Waves: what landed between two releases, one short note each, kept in git | |
| 3 | * (`story/wave.ts`). `/:owner/:name/waves` lists them newest first and | |
| 4 | * `/:owner/:name/waves/<version>` renders one. Server-rendered and escaped; | |
| 5 | * a private repository's waves follow its visibility, since both pages | |
| 6 | * start from `loadRepo`. | |
| 7 | */ | |
| 8 | ||
| 9 | import type { Ctx } from "../../http/context.ts"; | |
| 10 | import { readWave, readWaves, type WaveEntry } from "../../story/read.ts"; | |
| 11 | import { isVersion, type Wave } from "../../story/wave.ts"; | |
| 12 | import { | |
| 13 | crumbsFor, | |
| 14 | loadRepo, | |
| 15 | notFoundPage, | |
| 16 | repoBase, | |
| 17 | repoHeader, | |
| 18 | repoPalette, | |
| 19 | } from "../chrome.ts"; | |
| 20 | import { html, type Html } from "../html.ts"; | |
| 21 | import { page } from "../layout.ts"; | |
| 22 | ||
| 23 | const waveHref = (base: string, version: string): string => | |
| 24 | `${base}/waves/${version}`; | |
| 25 | ||
| 26 | function draftTag(wave: Wave): Html | null { | |
| 27 | return wave.draft ? html` <span class="muted small">(draft)</span>` : null; | |
| 28 | } | |
| 29 | ||
| 30 | function summary(base: string, entry: WaveEntry): Html { | |
| 31 | const link = html`<a href="${waveHref(base, entry.version)}" | |
| 32 | >${entry.version}</a | |
| 33 | >`; | |
| 34 | const w = entry.wave; | |
| 35 | if (!w) | |
| 36 | return html`<li> | |
| 37 | <strong>${link}</strong> | |
| 38 | <span class="muted small">cannot be read: ${entry.problem}</span> | |
| 39 | </li>`; | |
| 40 | return html`<li> | |
| 41 | <strong>${link}</strong>${draftTag(w)} | |
| 42 | <span class="muted small">${w.date} · ${w.pulls.length} merged</span> | |
| 43 | <div>${w.headline === "" ? "Headline not written yet." : w.headline}</div> | |
| 44 | </li>`; | |
| 45 | } | |
| 46 | ||
| 47 | /** GET /:owner/:name/waves — every wave, newest first. */ | |
| 48 | export async function wavesPage(ctx: Ctx): Promise<Response> { | |
| 49 | const repo = loadRepo(ctx); | |
| 50 | if (!repo) return notFoundPage(ctx); | |
| 51 | const base = repoBase(repo); | |
| 52 | const entries = await readWaves(ctx.app, repo); | |
| 53 | const body = html`${repoHeader(ctx, repo, "code")} | |
| 54 | <div class="page-head mt"> | |
| 55 | <h1>Waves</h1> | |
| 56 | <p class="muted"> | |
| 57 | What landed between two releases, one short note each, kept in git | |
| 58 | under <code>waves/</code>. | |
| 59 | </p> | |
| 60 | </div> | |
| 61 | ${entries.length === 0 | |
| 62 | ? html`<p class="muted">No wave has been written yet.</p>` | |
| 63 | : html`<ul class="list" id="waves"> | |
| 64 | ${entries.map((e) => summary(base, e))} | |
| 65 | </ul>`}`; | |
| 66 | return page({ | |
| 67 | ctx, | |
| 68 | title: `Waves · ${repo.name}`, | |
| 69 | crumbs: crumbsFor(repo), | |
| 70 | palette: repoPalette(ctx, repo), | |
| 71 | body, | |
| 72 | }); | |
| 73 | } | |
| 74 | ||
| 75 | /** GET /:owner/:name/waves/:version — one wave as a note. */ | |
| 76 | export async function wavePage(ctx: Ctx): Promise<Response> { | |
| 77 | const repo = loadRepo(ctx); | |
| 78 | if (!repo) return notFoundPage(ctx); | |
| 79 | const version = ctx.params.version ?? ""; | |
| 80 | if (!isVersion(version)) return notFoundPage(ctx); | |
| 81 | const entry = await readWave(ctx.app, repo, version); | |
| 82 | const base = repoBase(repo); | |
| 83 | const w = entry.wave; | |
| 84 | if (!w && entry.problem === "the file is missing") return notFoundPage(ctx); | |
| 85 | ||
| 86 | const note = w | |
| 87 | ? html`<div class="page-head mt"> | |
| 88 | <h1>${w.headline === "" ? `Wave ${w.version}` : w.headline}</h1> | |
| 89 | <p class="muted"> | |
| 90 | Wave ${w.version} · ${w.date}${draftTag(w)} | |
| 91 | </p> | |
| 92 | </div> | |
| 93 | ${w.highlights.length === 0 | |
| 94 | ? null | |
| 95 | : html`<h2>Highlights</h2> | |
| 96 | <ul class="list"> | |
| 97 | ${w.highlights.map( | |
| 98 | (h) => html`<li><strong>${h.what}</strong> — ${h.why}</li>`, | |
| 99 | )} | |
| 100 | </ul>`} | |
| 101 | <h2>Merged</h2> | |
| 102 | ${w.pulls.length === 0 | |
| 103 | ? html`<p class="muted">No pulls are listed.</p>` | |
| 104 | : html`<ul class="list"> | |
| 105 | ${w.pulls.map( | |
| 106 | (p) => | |
| 107 | html`<li> | |
| 108 | <a href="${p.href}">#${p.number} ${p.title}</a> | |
| 109 | </li>`, | |
| 110 | )} | |
| 111 | </ul>`}` | |
| 112 | : html`<div class="page-head mt"> | |
| 113 | <h1>Wave ${version}</h1> | |
| 114 | <p class="muted">This file cannot be read: ${entry.problem}.</p> | |
| 115 | </div>`; | |
| 116 | ||
| 117 | return page({ | |
| 118 | ctx, | |
| 119 | title: `Wave ${version} · ${repo.name}`, | |
| 120 | crumbs: crumbsFor(repo), | |
| 121 | palette: repoPalette(ctx, repo), | |
| 122 | body: html`${repoHeader(ctx, repo, "code")} ${note} | |
| 123 | <p class="mt"><a href="${base}/waves">All waves</a></p>`, | |
| 124 | }); | |
| 125 | } |
test/waves.test.ts added +374
| @@ -0,0 +1,374 @@ | ||
| 1 | /** | |
| 2 | * Waves: a wave is `waves/<version>.md` in git. The file's shape round-trips | |
| 3 | * through `story/wave.ts` and a malformed one is refused with a reason; | |
| 4 | * Cut a wave opens a draft as a pull whose list is the pulls merged since | |
| 5 | * the last wave; the pages render each wave escaped and follow the | |
| 6 | * repository's visibility. | |
| 7 | */ | |
| 8 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 9 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { createApp, type App } from "../src/app.ts"; | |
| 13 | import { mergePull, openPull } from "../src/actions/pulls.ts"; | |
| 14 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 15 | import { cutWave } from "../src/actions/waves.ts"; | |
| 16 | import { mergedIntoBranch, getPull } from "../src/core/pulls.ts"; | |
| 17 | import { getRepoById, type Repo } from "../src/core/repos.ts"; | |
| 18 | import { createToken } from "../src/core/tokens.ts"; | |
| 19 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 20 | import { blobForRender } from "../src/git/read.ts"; | |
| 21 | import { repoDir } from "../src/git/repos.ts"; | |
| 22 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 23 | import { | |
| 24 | draftWave, | |
| 25 | nextVersion, | |
| 26 | parseWave, | |
| 27 | writeWave, | |
| 28 | type Wave, | |
| 29 | } from "../src/story/wave.ts"; | |
| 30 | ||
| 31 | const wave: Wave = { | |
| 32 | version: "1.2.0", | |
| 33 | date: "2026-10-14", | |
| 34 | headline: "A calmer forge", | |
| 35 | draft: false, | |
| 36 | highlights: [ | |
| 37 | { what: "Faster pages", why: "the first paint is server-rendered" }, | |
| 38 | { what: "Safer tokens", why: "they expire with the run" }, | |
| 39 | { what: "Waves", why: "the owner reads a release as a note" }, | |
| 40 | ], | |
| 41 | pulls: [ | |
| 42 | { number: 7, title: "Speed up (the) pages", href: "/o/r/pulls/7" }, | |
| 43 | { number: 9, title: "Expire tokens", href: "/o/r/pulls/9" }, | |
| 44 | ], | |
| 45 | }; | |
| 46 | ||
| 47 | describe("wave file", () => { | |
| 48 | test("round trips as plain data", () => { | |
| 49 | const text = writeWave(wave); | |
| 50 | expect(text.ok).toBe(true); | |
| 51 | if (!text.ok) return; | |
| 52 | expect(parseWave(text.value)).toEqual({ ok: true, value: wave }); | |
| 53 | }); | |
| 54 | ||
| 55 | test("a draft may leave the words empty; a published wave may not", () => { | |
| 56 | const draft = { ...wave, draft: true, headline: "", highlights: [] }; | |
| 57 | const text = writeWave(draft); | |
| 58 | expect(text.ok).toBe(true); | |
| 59 | if (text.ok) expect(parseWave(text.value)).toEqual({ ok: true, value: draft }); | |
| 60 | expect(writeWave({ ...wave, highlights: [] }).ok).toBe(false); | |
| 61 | expect(writeWave({ ...wave, headline: "" }).ok).toBe(false); | |
| 62 | }); | |
| 63 | ||
| 64 | test("a malformed file is refused with a plain reason", () => { | |
| 65 | const good = (writeWave(wave) as { value: string }).value; | |
| 66 | const cases: [string, string][] = [ | |
| 67 | ["no front matter", "## Highlights\n"], | |
| 68 | ["never closed", "---\nversion: 1.0.0\n"], | |
| 69 | ["a bad version", good.replace("1.2.0", "latest")], | |
| 70 | ["a bad date", good.replace("2026-10-14", "yesterday")], | |
| 71 | ["a missing headline", good.replace(/headline: .*\n/, "")], | |
| 72 | ["two highlights", good.replace(/- \*\*Waves.*\n/, "")], | |
| 73 | ["six highlights", good.replace("## Merged", "- **x** — y\n".repeat(3) + "\n## Merged")], | |
| 74 | ["a highlight with no why", good.replace(" — the owner reads a release as a note", "")], | |
| 75 | ["a stray line", good.replace("## Highlights", "hello\n## Highlights")], | |
| 76 | ["an offsite link", good.replace("/o/r/pulls/7", "//evil.example/x")], | |
| 77 | ["an unknown key", good.replace("---\n\n", "color: red\n---\n\n")], | |
| 78 | ]; | |
| 79 | for (const [name, text] of cases) { | |
| 80 | const parsed = parseWave(text); | |
| 81 | expect(parsed.ok, name).toBe(false); | |
| 82 | if (!parsed.ok) expect(parsed.error.length).toBeGreaterThan(5); | |
| 83 | } | |
| 84 | }); | |
| 85 | ||
| 86 | test("the next version is a minor bump", () => { | |
| 87 | expect(nextVersion([])).toBe("0.1.0"); | |
| 88 | expect(nextVersion(["0.2.0", "0.10.0", "0.9.0"])).toBe("0.11.0"); | |
| 89 | }); | |
| 90 | ||
| 91 | test("a draft lists the merged pulls it is given, in order", () => { | |
| 92 | const d = draftWave({ | |
| 93 | version: "1.3.0", | |
| 94 | date: "2026-10-15", | |
| 95 | merged: [ | |
| 96 | { number: 7, title: "Seen" }, | |
| 97 | { number: 11, title: "New [one]\nhere" }, | |
| 98 | ], | |
| 99 | base: "/o/r", | |
| 100 | }); | |
| 101 | expect(d.pulls).toEqual([ | |
| 102 | { number: 7, title: "Seen", href: "/o/r/pulls/7" }, | |
| 103 | { number: 11, title: "New (one) here", href: "/o/r/pulls/11" }, | |
| 104 | ]); | |
| 105 | expect(writeWave(d).ok).toBe(true); | |
| 106 | }); | |
| 107 | }); | |
| 108 | ||
| 109 | describe("waves in a forge", () => { | |
| 110 | let dataDir: string; | |
| 111 | let app: App; | |
| 112 | let servers: Servers; | |
| 113 | let base: string; | |
| 114 | let alice: User; | |
| 115 | let notes: Repo; | |
| 116 | let vault: Repo; | |
| 117 | const cookies: Record<string, string> = {}; | |
| 118 | const fresh = (r: Repo) => getRepoById(app.db, r.id)!; | |
| 119 | ||
| 120 | const land = async (name: string): Promise<number> => { | |
| 121 | const branch = await commitToBranch(app, alice, fresh(notes), { | |
| 122 | branch: `feature/${name}`, | |
| 123 | base: "main", | |
| 124 | message: name, | |
| 125 | files: [{ path: `${name}.md`, content: `${name}\n` }], | |
| 126 | }); | |
| 127 | if (!branch.ok) throw new Error(JSON.stringify(branch.error)); | |
| 128 | const opened = await openPull(app, alice, fresh(notes), { | |
| 129 | title: `Land ${name}`, | |
| 130 | body: "", | |
| 131 | baseBranch: "main", | |
| 132 | headBranch: `feature/${name}`, | |
| 133 | }); | |
| 134 | if (!opened.ok) throw new Error(String(opened.error)); | |
| 135 | const merged = await mergePull(app, alice, fresh(notes), opened.value, "squash"); | |
| 136 | if (!merged.ok) throw new Error(JSON.stringify(merged.error)); | |
| 137 | return opened.value.number; | |
| 138 | }; | |
| 139 | const get = (who: string | null, path: string) => | |
| 140 | fetch(`${base}${path}`, { | |
| 141 | headers: who === null ? {} : { cookie: cookies[who]! }, | |
| 142 | }); | |
| 143 | const post = (who: string, path: string, body: Record<string, string>, origin?: string) => | |
| 144 | fetch(`${base}${path}`, { | |
| 145 | method: "POST", | |
| 146 | headers: { | |
| 147 | cookie: cookies[who]!, | |
| 148 | ...(origin === undefined ? {} : { origin }), | |
| 149 | }, | |
| 150 | body: new URLSearchParams(body), | |
| 151 | redirect: "manual", | |
| 152 | }); | |
| 153 | const draftText = async (branch: string): Promise<string> => { | |
| 154 | const blob = await blobForRender( | |
| 155 | repoDir(app.reposRoot, "alice", "notes"), | |
| 156 | branch, | |
| 157 | "waves/0.1.0.md", | |
| 158 | 100_000, | |
| 159 | ); | |
| 160 | if (blob.kind !== "text") throw new Error(blob.kind); | |
| 161 | return new TextDecoder().decode(blob.body); | |
| 162 | }; | |
| 163 | ||
| 164 | beforeAll(async () => { | |
| 165 | dataDir = await mkdtemp(join(tmpdir(), "forge-waves-")); | |
| 166 | app = createApp({ dataDir, port: 0 }); | |
| 167 | servers = await serve(app); | |
| 168 | base = servers.url; | |
| 169 | for (const [username, adminIfFirst] of [ | |
| 170 | ["alice", true], | |
| 171 | ["bob", false], | |
| 172 | ] as const) { | |
| 173 | const made = await createUser(app.db, { | |
| 174 | trusted: true, | |
| 175 | username, | |
| 176 | email: `${username}@forge.local`, | |
| 177 | password: "a-good-passphrase", | |
| 178 | adminIfFirst, | |
| 179 | }); | |
| 180 | if (!made.ok) throw new Error(made.error); | |
| 181 | cookies[username] = ( | |
| 182 | ( | |
| 183 | await fetch(`${base}/signin`, { | |
| 184 | method: "POST", | |
| 185 | body: new URLSearchParams({ username, password: "a-good-passphrase" }), | |
| 186 | redirect: "manual", | |
| 187 | }) | |
| 188 | ).headers.get("set-cookie") ?? "" | |
| 189 | ).split(";")[0]!; | |
| 190 | } | |
| 191 | alice = getUserByName(app.db, "alice")!; | |
| 192 | for (const [name, visibility] of [ | |
| 193 | ["notes", "public"], | |
| 194 | ["vault", "private"], | |
| 195 | ] as const) { | |
| 196 | const made = await createRepo(app, alice, { name, visibility }); | |
| 197 | if (!made.ok) throw new Error(String(made.error)); | |
| 198 | const seeded = await commitToBranch(app, alice, made.value, { | |
| 199 | message: "Start", | |
| 200 | files: [{ path: "README.md", content: `# ${name}\n` }], | |
| 201 | }); | |
| 202 | if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); | |
| 203 | if (name === "notes") notes = made.value; | |
| 204 | else vault = made.value; | |
| 205 | } | |
| 206 | }); | |
| 207 | ||
| 208 | afterAll(async () => { | |
| 209 | servers.stop(); | |
| 210 | app.close(); | |
| 211 | await rm(dataDir, { recursive: true, force: true }); | |
| 212 | }); | |
| 213 | ||
| 214 | test("Cut a wave lists exactly the merged pulls, as a draft pull", async () => { | |
| 215 | const one = await land("one"); | |
| 216 | const two = await land("two"); | |
| 217 | ||
| 218 | const page = await get("alice", "/alice/notes/settings/waves"); | |
| 219 | expect(page.status).toBe(200); | |
| 220 | expect(await page.text()).toContain("0.1.0"); | |
| 221 | ||
| 222 | const res = await post("alice", "/alice/notes/settings/waves", { version: "0.1.0" }); | |
| 223 | expect(res.status).toBe(303); | |
| 224 | const pullNumber = Number(/\/pulls\/(\d+)/.exec(res.headers.get("location")!)![1]); | |
| 225 | const pull = getPull(app.db, notes.id, pullNumber)!; | |
| 226 | expect(pull.title).toBe("Wave 0.1.0 (draft)"); | |
| 227 | expect(pull.mergedAt).toBeNull(); | |
| 228 | ||
| 229 | const parsed = parseWave(await draftText(pull.headBranch)); | |
| 230 | expect(parsed.ok).toBe(true); | |
| 231 | if (!parsed.ok) return; | |
| 232 | expect(parsed.value.draft).toBe(true); | |
| 233 | expect(parsed.value.headline).toBe(""); | |
| 234 | expect(parsed.value.pulls.map((p) => p.number)).toEqual([one, two]); | |
| 235 | expect(parsed.value.pulls[0]!.href).toBe(`/alice/notes/pulls/${one}`); | |
| 236 | ||
| 237 | // Nothing lands until a person merges it; then the next wave lists only | |
| 238 | // what merged after it — the draft's own pull. | |
| 239 | const merged = await mergePull(app, alice, fresh(notes), pull, "squash"); | |
| 240 | expect(merged.ok).toBe(true); | |
| 241 | const again = await cutWave(app, alice, fresh(notes), "0.2.0"); | |
| 242 | expect(again.ok).toBe(true); | |
| 243 | if (!again.ok) return; | |
| 244 | expect(again.value.listed).toBe(1); | |
| 245 | const dupe = await cutWave(app, alice, fresh(notes), "0.1.0"); | |
| 246 | expect(dupe.ok).toBe(false); | |
| 247 | }); | |
| 248 | ||
| 249 | test("a stranger, a bad version and a foreign origin cut nothing", async () => { | |
| 250 | const before = (await get("alice", "/alice/notes/pulls")).status; | |
| 251 | expect(before).toBe(200); | |
| 252 | expect((await post("bob", "/alice/notes/settings/waves", { version: "0.3.0" })).status).toBe(404); | |
| 253 | expect((await get("bob", "/alice/notes/settings/waves")).status).toBe(404); | |
| 254 | expect( | |
| 255 | (await post("alice", "/alice/notes/settings/waves", { version: "0.3.0" }, "https://evil.example")).status, | |
| 256 | ).toBe(403); | |
| 257 | const bad = await post("alice", "/alice/notes/settings/waves", { version: "../x" }); | |
| 258 | expect(bad.headers.get("location")).toContain("/settings/waves"); | |
| 259 | expect(await cutWave(app, undefined, fresh(notes), "0.3.0")).toMatchObject({ ok: false }); | |
| 260 | }); | |
| 261 | ||
| 262 | test("the pages list newest first and escape everything", async () => { | |
| 263 | const hostile = (v: string, headline: string) => | |
| 264 | (writeWave({ ...wave, version: v, headline, highlights: wave.highlights.map((h) => ({ ...h, what: `${h.what} <b>` })) }) as { value: string }).value; | |
| 265 | const seeded = await commitToBranch(app, alice, fresh(notes), { | |
| 266 | message: "Two waves", | |
| 267 | files: [ | |
| 268 | { path: "waves/1.0.0.md", content: hostile("1.0.0", "<script>alert(1)</script>") }, | |
| 269 | { path: "waves/1.10.0.md", content: hostile("1.10.0", "Tenth") }, | |
| 270 | { path: "waves/9.9.9.md", content: "not a wave" }, | |
| 271 | ], | |
| 272 | }); | |
| 273 | expect(seeded.ok).toBe(true); | |
| 274 | ||
| 275 | const list = await (await get(null, "/alice/notes/waves")).text(); | |
| 276 | expect(list.indexOf("9.9.9")).toBeLessThan(list.indexOf("1.10.0")); | |
| 277 | expect(list.indexOf("1.10.0")).toBeLessThan(list.indexOf("1.0.0")); | |
| 278 | expect(list.indexOf("1.0.0")).toBeLessThan(list.indexOf("0.1.0")); | |
| 279 | expect(list).not.toContain("<script>alert(1)"); | |
| 280 | expect(list).toContain("<script>alert(1)</script>"); | |
| 281 | expect(list).toContain("cannot be read"); | |
| 282 | ||
| 283 | const one = await get(null, "/alice/notes/waves/1.0.0"); | |
| 284 | expect(one.status).toBe(200); | |
| 285 | const text = await one.text(); | |
| 286 | expect(text).not.toContain("<script>alert(1)"); | |
| 287 | expect(text).toContain("<b>"); | |
| 288 | expect(text).toContain("/o/r/pulls/7"); | |
| 289 | expect(text).toContain("Speed up (the) pages"); | |
| 290 | ||
| 291 | expect((await get(null, "/alice/notes/waves/2.0.0")).status).toBe(404); | |
| 292 | expect((await get(null, "/alice/notes/waves/..%2fx")).status).toBe(404); | |
| 293 | const draft = await get(null, "/alice/notes/waves/0.1.0"); | |
| 294 | expect(await draft.text()).toContain("draft"); | |
| 295 | }); | |
| 296 | ||
| 297 | test("a private repository's waves are refused to an outsider", async () => { | |
| 298 | const seeded = await commitToBranch(app, alice, fresh(vault), { | |
| 299 | message: "A wave", | |
| 300 | files: [{ path: "waves/1.0.0.md", content: (writeWave(wave) as { value: string }).value }], | |
| 301 | }); | |
| 302 | expect(seeded.ok).toBe(true); | |
| 303 | for (const path of ["/alice/vault/waves", "/alice/vault/waves/1.0.0"]) { | |
| 304 | expect((await get(null, path)).status).toBe(404); | |
| 305 | expect((await get("bob", path)).status).toBe(404); | |
| 306 | expect((await get("alice", path)).status).toBe(200); | |
| 307 | } | |
| 308 | }); | |
| 309 | ||
| 310 | test("the newest merges are not hidden by a long history", () => { | |
| 311 | const all = mergedIntoBranch(app.db, notes.id, "main").map((p) => p.number); | |
| 312 | expect(all.length).toBeGreaterThan(2); | |
| 313 | const skipOldest = new Set(all.slice(0, 2)); | |
| 314 | const next = mergedIntoBranch(app.db, notes.id, "main", skipOldest, 1); | |
| 315 | expect(next.map((p) => p.number)).toEqual([all[2]!]); | |
| 316 | }); | |
| 317 | ||
| 318 | test("a wave that does not parse stops the cut, naming the file", async () => { | |
| 319 | const broken = await commitToBranch(app, alice, fresh(notes), { | |
| 320 | message: "A broken wave", | |
| 321 | files: [{ path: "waves/9.0.0.md", content: "not a wave\n" }], | |
| 322 | }); | |
| 323 | expect(broken.ok).toBe(true); | |
| 324 | await land("after-broken"); | |
| 325 | const cut = await cutWave(app, alice, fresh(notes), "9.1.0"); | |
| 326 | expect(cut.ok).toBe(false); | |
| 327 | if (!cut.ok) { | |
| 328 | expect(cut.error.kind).toBe("unreadable"); | |
| 329 | expect((cut.error as { version: string }).version).toBe("9.0.0"); | |
| 330 | } | |
| 331 | const page = await get("alice", "/alice/notes/settings/waves"); | |
| 332 | expect(await page.text()).toContain("waves/9.0.0.md"); | |
| 333 | }); | |
| 334 | ||
| 335 | test("the waves tool lists and opens waves, and keeps a private app private", async () => { | |
| 336 | const mcp = async (token: string, args: unknown) => { | |
| 337 | const res = await fetch(`${base}/mcp`, { | |
| 338 | method: "POST", | |
| 339 | headers: { | |
| 340 | "content-type": "application/json", | |
| 341 | accept: "application/json, text/event-stream", | |
| 342 | authorization: `Bearer ${token}`, | |
| 343 | }, | |
| 344 | body: JSON.stringify({ | |
| 345 | jsonrpc: "2.0", | |
| 346 | id: 1, | |
| 347 | method: "tools/call", | |
| 348 | params: { name: "waves", arguments: args }, | |
| 349 | }), | |
| 350 | }); | |
| 351 | const body = (await res.json()) as any; | |
| 352 | const text: string = body.result.content[0].text; | |
| 353 | return { error: body.result.isError === true, text }; | |
| 354 | }; | |
| 355 | const a = createToken(app.db, alice.id, "ro", "ro").token; | |
| 356 | const b = createToken(app.db, getUserByName(app.db, "bob")!.id, "ro", "ro").token; | |
| 357 | const list = await mcp(a, { app: "alice/vault" }); | |
| 358 | expect(list.error).toBe(false); | |
| 359 | expect(JSON.parse(list.text).waves).toEqual([ | |
| 360 | { version: "1.0.0", date: "2026-10-14", headline: "A calmer forge", draft: false, merged: 2, problem: null }, | |
| 361 | ]); | |
| 362 | const one = await mcp(a, { app: "alice/vault", version: "1.0.0" }); | |
| 363 | expect(JSON.parse(one.text).wave.highlights).toHaveLength(3); | |
| 364 | expect((await mcp(a, { app: "alice/vault", version: "7.7.7" })).error).toBe(true); | |
| 365 | expect((await mcp(a, { app: "alice/vault", version: "../x" })).error).toBe(true); | |
| 366 | const notesList = JSON.parse((await mcp(a, { app: "alice/notes" })).text).waves; | |
| 367 | expect(notesList.some((w: { problem: string | null }) => w.problem !== null)).toBe(true); | |
| 368 | // A stranger gets nothing of a private app, and not a hint it exists. | |
| 369 | const denied = await mcp(b, { app: "alice/vault" }); | |
| 370 | expect(denied.error).toBe(true); | |
| 371 | expect(denied.text).toBe((await mcp(b, { app: "alice/nope" })).text); | |
| 372 | expect((await mcp(b, { app: "alice/notes" })).error).toBe(false); | |
| 373 | }); | |
| 374 | }); |
new above
offline
entry.waveis null for any wave file that fails to parse. A published wave is easy to break by hand, for example by trimming it to two highlights. When that happens its pulls stop counting as listed, and the next cut lists all of them again. Either refuse to cut while a wave has a problem (and tell the admin which file), or fall back to a lenient read of the## Mergedlist.