Public: anyone can read this; the crew can change it.
An outsider with no account offers a patch to a public repository, held for a person to read #12461
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/12402-an-outsider-with-no-account-offers-a-pat → main 28 commits fork 4 threads · 4 unresolved
14 files
changed +1001
−0
docs/features/README.md +1
| @@ -343,6 +343,7 @@ Read with the guide page [Agents: MCP, git, or the terminal](../guide-agents.md) | ||
| 343 | 343 | - [](api-root.md) |
| 344 | 344 | - [](conversation-fence.md) |
| 345 | 345 | - [](muse-test.md) |
| 346 | - [](offer.md) | |
| 346 | 347 | - [](report-form.md) |
| 347 | 348 | - [](tokens-harness.md) |
| 348 | 349 |
docs/features/offer.md added +44
| @@ -0,0 +1,44 @@ | ||
| 1 | # Offering a patch with no account (#12402) | |
| 2 | ||
| 3 | On a public repository an outside person or agent can offer a change the way | |
| 4 | they can report a problem: no account, no token, nothing to be taught. | |
| 5 | ||
| 6 | - **API:** `POST /api/v1/repos/{owner}/{name}/offers` with | |
| 7 | `{"title": "…", "body": "…", "patch": "<a unified diff>"}` | |
| 8 | → `202 {"number", "state": "open", "url", "message"}`. | |
| 9 | - **MCP:** the token-free tool `offer` comes next (#12633). | |
| 10 | - **Terminal:** a signed-in reader who cannot write the repository has | |
| 11 | "Offer a patch" in its `:` palette (`src/tui/screens/offer.ts`): a title, a | |
| 12 | pasted diff, ctrl+d sends. The same action, limits and words; a writer pushes | |
| 13 | a branch instead, so is not offered the row. | |
| 14 | - **Try it:** `git diff > fix.patch`, then | |
| 15 | `curl -s -X POST "$FORGE/api/v1/repos/owner/name/offers" -H 'content-type: application/json' -d "$(jq -n --rawfile p fix.patch '{title:"Fix it", patch:$p}')"`. | |
| 16 | ||
| 17 | ### What happens to it | |
| 18 | ||
| 19 | It becomes a stranger's pull (`src/crew/stranger.ts`): committed to a | |
| 20 | `contrib/offer/<segment>/…` branch under one shared account that writes | |
| 21 | nothing, and opened the way an outside agent's pull is. A person who can write | |
| 22 | the repository reads it and approves it; until then no preview and no | |
| 23 | repository secret reaches its code, and it never merges without a person. | |
| 24 | Paths a stranger may not change (secrets, `check` and `release` in | |
| 25 | `forge.json`) stay guarded. | |
| 26 | ||
| 27 | ### What is refused (422 naming why; nothing is stored) | |
| 28 | ||
| 29 | | error | when | | |
| 30 | |---|---| | |
| 31 | | `empty_title`, `title_too_long`, `body_too_long`, `patch_missing` | the words are not 1-200 characters, at most 8,000, and a patch | | |
| 32 | | `patch_too_large` | more than 200 KB | | |
| 33 | | `patch_not_text`, `patch_unsupported` | a NUL byte; binary files, symlinks, submodules | | |
| 34 | | `patch_touches_git` | any header names `.git` as a path part, in any case | | |
| 35 | | `patch_bad_path` | a header path that is absolute, climbs with `..` or is quoted | | |
| 36 | | `patch_does_not_apply` | it does not apply to the default branch's head | | |
| 37 | ||
| 38 | A private, archived, sealed or mirrored repository answers `not_found`. Three | |
| 39 | offers a minute from one source, ten a minute to one repository, thirty to the | |
| 40 | forge (`429`); a repository holds at most 20 open offers (`429 queue_full`). | |
| 41 | ||
| 42 | The patch is applied only to a scratch index (`src/git/patch_files.ts`: the | |
| 43 | text goes on stdin, never in an argument), and its headers are read first | |
| 44 | (`patchHeaderRefusal` in `src/actions/offer.ts`): on any doubt, refuse. |
src/actions/offer.ts added +299
| @@ -0,0 +1,299 @@ | ||
| 1 | /** | |
Both doors: still no terminal screen in src/tui/ calling fileOffer (and no test). Add it; the reverted attempt can be redone smaller. |
||
| 2 | * An offer from outside (#12402): anyone, with no account, sends a patch to | |
| 3 | * a public repository and it becomes a stranger's pull (`crew/stranger.ts`) | |
| 4 | * for a person to read. The riskiest door the forge has, so it refuses by | |
| 5 | * default on any doubt: | |
| 6 | * | |
| 7 | * - **Public originals only.** A private, archived, sealed or mirrored | |
| 8 | * repository answers `not_found`, exactly as a missing one. | |
| 9 | * - **Limited three ways**, as reports are: per source, per repository and | |
| 10 | * for the whole forge, each a sliding minute, and a repository holds at | |
| 11 | * most `OFFERS_WAITING_MAX` open offers. | |
| 12 | * - **The patch is text, read before it is run.** Its headers are scanned | |
| 13 | * for `.git`, `..` and absolute or quoted paths; it is then applied only | |
| 14 | * to a scratch index (`git/patch_files.ts`: the text goes on stdin, never | |
| 15 | * in an argument; no work tree is touched) and read back as whole files | |
| 16 | * that pass the ordinary safe-path rules. Nothing is stored unless every | |
| 17 | * step passed. | |
| 18 | * - **It is a stranger's pull.** Committed to a `contrib/offer/<segment>/…` | |
| 19 | * branch (the shape only an outside hand has, `core/contribute_token.ts`) | |
| 20 | * under one shared account that writes nothing, and opened with the | |
| 21 | * outside agent's path: no preview or secret until a person approves it, | |
| 22 | * no merge without one, and the forge.json and secret paths a stranger | |
| 23 | * may not change stay guarded. | |
| 24 | */ | |
| 25 | import { mkdir } from "node:fs/promises"; | |
| 26 | import { join } from "node:path"; | |
| 27 | import type { App } from "../app.ts"; | |
| 28 | import { commitIdentity } from "../core/commit_identity.ts"; | |
| 29 | import { isContributionFork } from "../core/contribute.ts"; | |
| 30 | import { type Pull } from "../core/pulls.ts"; | |
| 31 | import { plainText } from "../core/reports.ts"; | |
| 32 | import { RateLimiter } from "../core/ratelimit.ts"; | |
| 33 | import { canRead, type Repo } from "../core/repos.ts"; | |
| 34 | import { createUser, getUserById, type User } from "../core/users.ts"; | |
| 35 | import { runGit } from "../git/env.ts"; | |
| 36 | import { isSafeRef } from "../git/guard.ts"; | |
| 37 | import { resolveOid } from "../git/merge.ts"; | |
| 38 | import { patchToFiles } from "../git/patch_files.ts"; | |
| 39 | import { repoDir } from "../git/repos.ts"; | |
| 40 | import { commitFiles } from "../git/write.ts"; | |
| 41 | import { err, ok, type Result } from "../kernel/result.ts"; | |
| 42 | import { isMirror } from "../origins/write-through.ts"; | |
| 43 | import { noEmail } from "./users.ts"; | |
| 44 | import { openPull } from "./pulls.ts"; | |
| 45 | ||
| 46 | // The words a stranger is told, said once: the API, the terminal and the OpenAPI contract | |
| 47 | // read them from here (and the MCP `offer` tool, #12633). | |
| 48 | ||
| 49 | /** The most patch bytes one offer takes (200 KB). */ | |
| 50 | export const OFFER_PATCH_MAX = 200 * 1024; | |
| 51 | /** | |
| 52 | * JSON may spell each byte of a patch as six (`\u00XX`): a request is bounded past the patch, | |
| 53 | * the patch itself exactly. | |
| 54 | */ | |
| 55 | export const OFFER_REQUEST_MAX = OFFER_PATCH_MAX * 6 + 32 * 1024; | |
| 56 | export const OFFER_TITLE_MAX = 200; | |
| 57 | export const OFFER_BODY_MAX = 8_000; | |
| 58 | ||
| 59 | /** The answer to an offer that was stored and now waits for a person. */ | |
| 60 | export const OFFER_RECEIVED = | |
| 61 | "Received as a pull. A person who maintains this repository reads it; nothing runs with the repository's secrets and nothing lands before they approve it."; | |
| 62 | ||
| 63 | /** What a refused offer is told: the shape that would have been taken. */ | |
| 64 | export const OFFER_HINT = | |
| 65 | `send JSON {title, body?, patch}: plain text; a title of 1-${OFFER_TITLE_MAX} characters, ` + | |
| 66 | `a body of at most ${OFFER_BODY_MAX}, a patch that is a unified diff of text files ` + | |
| 67 | `(git diff output) of at most ${OFFER_PATCH_MAX / 1024} KB that applies to the default branch`; | |
| 68 | ||
| 69 | export type OfferRefusal = | |
| 70 | | "empty_title" | |
| 71 | | "title_too_long" | |
| 72 | | "body_too_long" | |
| 73 | | "patch_missing" | |
| 74 | | "patch_too_large" | |
| 75 | | "patch_not_text" | |
| 76 | | "patch_touches_git" | |
| 77 | | "patch_bad_path" | |
| 78 | | "patch_does_not_apply" | |
| 79 | | "patch_unsupported"; | |
| 80 | ||
| 81 | /** Why each refusal is a 422, in words that say what to change. */ | |
| 82 | export const OFFER_WORDS: Readonly<Record<OfferRefusal, string>> = { | |
| 83 | empty_title: "the title is empty: say in one line what the change does", | |
| 84 | title_too_long: `the title is longer than ${OFFER_TITLE_MAX} characters`, | |
| 85 | body_too_long: `the body is longer than ${OFFER_BODY_MAX} characters`, | |
| 86 | patch_missing: "the patch is missing: send a unified diff as text", | |
| 87 | patch_too_large: `the patch is larger than ${OFFER_PATCH_MAX / 1024} KB: offer a smaller change, or fork and send a pull`, | |
| 88 | patch_not_text: "the patch is not plain text: send a unified diff of text files", | |
| 89 | patch_touches_git: "the patch touches .git, which an offer may never change", | |
| 90 | patch_bad_path: "the patch names a path that is absolute, climbs out with .., or is not a safe path", | |
| 91 | patch_does_not_apply: "the patch does not apply to the repository's default branch: make it against its current head", | |
| 92 | patch_unsupported: "the patch changes something an offer may not: binary files, symlinks and submodules are refused", | |
| 93 | }; | |
| 94 | ||
| 95 | /** A few a minute from one source; the other two hold when a source lies. */ | |
| 96 | export const OFFERS_PER_SOURCE = 3; | |
| 97 | const PER_REPO = 10; | |
| 98 | const PER_FORGE = 30; | |
| 99 | /** Open offers one repository holds before it takes no more. */ | |
| 100 | export const OFFERS_WAITING_MAX = 20; | |
| 101 | /** The branch prefix every offer lives under; `core/contribute_token.ts` knows its shape. */ | |
| 102 | const BRANCH_PREFIX = "contrib/offer"; | |
| 103 | ||
| 104 | export type OfferError = | |
| 105 | | { readonly error: "not_found" | "queue_full" | "unavailable" | OfferRefusal } | |
| 106 | | { readonly error: "rate_limited"; readonly retryAfter: number }; | |
| 107 | ||
| 108 | export interface OfferInput { | |
| 109 | readonly title: string; | |
| 110 | readonly body: string; | |
| 111 | readonly patch: string; | |
| 112 | } | |
| 113 | ||
| 114 | export interface Offered { | |
| 115 | readonly number: number; | |
| 116 | readonly pull: Pull; | |
| 117 | } | |
| 118 | ||
| 119 | interface Limits { | |
| 120 | readonly source: RateLimiter; | |
| 121 | readonly repo: RateLimiter; | |
| 122 | readonly forge: RateLimiter; | |
| 123 | } | |
| 124 | const limiter = (perMinute: number) => new RateLimiter({ perMinute, adminPerMinute: perMinute, enabled: true }); | |
| 125 | /** One set per forge, so a test's forge never throttles another's. */ | |
| 126 | const limits = new WeakMap<App, Limits>(); | |
| 127 | function limitsOf(app: App): Limits { | |
| 128 | let found = limits.get(app); | |
| 129 | if (found === undefined) { | |
| 130 | found = { source: limiter(OFFERS_PER_SOURCE), repo: limiter(PER_REPO), forge: limiter(PER_FORGE) }; | |
| 131 | limits.set(app, found); | |
| 132 | } | |
| 133 | return found; | |
| 134 | } | |
| 135 | ||
| 136 | /** Whether `repo` takes offers: a public, unsealed, unarchived original. */ | |
| 137 | export function takesOffers(app: App, repo: Repo): boolean { | |
| 138 | return ( | |
| 139 | canRead(app.db, repo, undefined) && | |
| 140 | repo.visibility === "public" && | |
| 141 | repo.sealedFor === null && | |
| 142 | repo.archivedAt === null && | |
| 143 | !isMirror(app, repo) && | |
| 144 | !isContributionFork(app.db, repo.id) | |
| 145 | ); | |
| 146 | } | |
| 147 | ||
| 148 | /** | |
| 149 | * The paths a patch's header lines name, as written (a name may hold spaces, so a line is split | |
| 150 | * on slashes and spaces). | |
| 151 | */ | |
| 152 | const HEADER = /^(?:diff --git |--- |\+\+\+ |rename (?:from|to) |copy (?:from|to) )(.*)$/; | |
| 153 | ||
| 154 | /** | |
| 155 | * Why a patch's headers are refused, or undefined. Every header line is read, | |
| 156 | * and a content line that merely looks like one is refused too: on any doubt, no. | |
| 157 | */ | |
| 158 | export function patchHeaderRefusal(patch: string): "patch_touches_git" | "patch_bad_path" | undefined { | |
| 159 | for (const line of patch.split("\n")) { | |
| 160 | const named = HEADER.exec(line.replace(/\r$/, "")); | |
| 161 | if (named === null) continue; | |
| 162 | const text = named[1] ?? ""; | |
| 163 | // A quoted name carries C escapes (`\056git`): refuse rather than decode. | |
| 164 | if (text.includes('"') || text.includes("\\")) return "patch_bad_path"; | |
| 165 | for (const word of text.split(/[ \t]+/)) { | |
| 166 | if (word === "" || word === "/dev/null") continue; | |
| 167 | const path = word.replace(/^[ab]\//, ""); | |
| 168 | if (path.startsWith("/") || /[\x00-\x1f\x7f]/.test(word)) return "patch_bad_path"; | |
| 169 | for (const part of path.split("/")) { | |
| 170 | if (part.toLowerCase() === ".git") return "patch_touches_git"; | |
| 171 | if (part === "..") return "patch_bad_path"; | |
| 172 | } | |
| 173 | } | |
| 174 | } | |
| 175 | return undefined; | |
| 176 | } | |
| 177 | ||
| 178 | /** The words as they will be stored, or why they will not be. */ | |
| 179 | export function cleanOffer(input: { title: unknown; body: unknown; patch: unknown }): Result<OfferInput, OfferRefusal> { | |
| 180 | const title = plainText(typeof input.title === "string" ? input.title : "").replace(/\s+/g, " ").trim(); | |
| 181 | if (title === "") return err("empty_title"); | |
| 182 | if (title.length > OFFER_TITLE_MAX) return err("title_too_long"); | |
| 183 | const body = plainText(typeof input.body === "string" ? input.body : "").trim(); | |
| 184 | if (body.length > OFFER_BODY_MAX) return err("body_too_long"); | |
| 185 | if (typeof input.patch !== "string" || input.patch.trim() === "") return err("patch_missing"); | |
| 186 | if (Buffer.byteLength(input.patch, "utf8") > OFFER_PATCH_MAX) return err("patch_too_large"); | |
| 187 | if (input.patch.includes("\0")) return err("patch_not_text"); | |
| 188 | const refused = patchHeaderRefusal(input.patch); | |
| 189 | if (refused !== undefined) return err(refused); | |
| 190 | return ok({ title, body, patch: input.patch }); | |
| 191 | } | |
| 192 | ||
| 193 | /** The one account every offer is committed and opened as: it writes nothing and cannot sign in. */ | |
| 194 | async function offerAccount(app: App): Promise<User | undefined> { | |
| 195 | const email = noEmail("outside-offers"); | |
| 196 | const find = (): User | undefined => { | |
| 197 | const row = app.db.query<{ id: string }, [string]>("SELECT id FROM user WHERE email = ?1").get(email); | |
| 198 | return row === null ? undefined : getUserById(app.db, row.id); | |
| 199 | }; | |
| 200 | const known = find(); | |
| 201 | if (known !== undefined) return known; | |
| 202 | for (const username of ["outside-offers", `outside-offers-${crypto.randomUUID().slice(0, 6)}`]) { | |
| 203 | const made = await createUser(app.db, { | |
| 204 | origin: "start", | |
| 205 | username, | |
| 206 | email, | |
| 207 | displayName: "An outside offer", | |
| 208 | trusted: true, | |
| 209 | }); | |
| 210 | if (made.ok) return made.value; | |
| 211 | // Two first offers raced, or a person holds the name: the other path decides. | |
| 212 | const raced = find(); | |
| 213 | if (raced !== undefined) return raced; | |
| 214 | } | |
| 215 | return undefined; | |
| 216 | } | |
| 217 | ||
| 218 | /** How many open offers `repo` holds, read through the head-branch index. */ | |
| 219 | function openOffers(app: App, repo: Repo): number { | |
| 220 | return ( | |
| 221 | app.db | |
| 222 | .query<{ n: number }, [string, string, string]>( | |
| 223 | `SELECT COUNT(*) AS n FROM pull INDEXED BY pull_head_branch JOIN issue ON issue.id = pull.issue_id | |
| 224 | WHERE pull.head_branch >= ?2 AND pull.head_branch < ?3 AND issue.repo_id = ?1 AND issue.state = 'open'`, | |
| 225 | ) | |
| 226 | .get(repo.id, `${BRANCH_PREFIX}/`, `${BRANCH_PREFIX}0`)?.n ?? 0 | |
| 227 | ); | |
| 228 | } | |
| 229 | ||
| 230 | /** What the pull's description says before the stranger's words: whose they are. */ | |
| 231 | const FROM_OUTSIDE = "Offered from outside, with no account. Everything below is the sender's, unchecked."; | |
| 232 | ||
| 233 | /** | |
| 234 | * File an offer on a public repository; `source` keys the per-source limit. | |
| 235 | * Nothing is stored unless the patch applies to the default branch. | |
| 236 | */ | |
| 237 | export async function fileOffer( | |
| 238 | app: App, | |
| 239 | repo: Repo, | |
| 240 | raw: { title: unknown; body: unknown; patch: unknown }, | |
| 241 | source: string, | |
| 242 | ): Promise<Result<Offered, OfferError>> { | |
| 243 | if (!takesOffers(app, repo)) return err({ error: "not_found" }); | |
| 244 | const clean = cleanOffer(raw); | |
| 245 | if (!clean.ok) return err({ error: clean.error }); | |
| 246 | const { forge, repo: perRepo, source: perSource } = limitsOf(app); | |
| 247 | for (const verdict of [() => forge.take("forge", "ro"), () => perRepo.take(repo.id, "ro"), () => perSource.take(source, "ro")]) { | |
| 248 | const v = verdict(); | |
| 249 | if (!v.allowed) return err({ error: "rate_limited", retryAfter: v.retryAfter }); | |
| 250 | } | |
| 251 | if (openOffers(app, repo) >= OFFERS_WAITING_MAX) return err({ error: "queue_full" }); | |
| 252 | ||
| 253 | const dir = repoDir(app.reposRoot, repo.ownerName, repo.name); | |
| 254 | const base = repo.defaultBranch; | |
| 255 | if (!isSafeRef(base)) return err({ error: "unavailable" }); | |
| 256 | const tip = await resolveOid(dir, `refs/heads/${base}`); | |
| 257 | if (tip === undefined) return err({ error: "patch_does_not_apply" }); | |
| 258 | const scratch = join(app.config.dataDir, "scratch"); | |
| 259 | await mkdir(scratch, { recursive: true }); | |
| 260 | const files = await patchToFiles(dir, tip, clean.value.patch, scratch); | |
| 261 | if (!files.ok) { | |
| 262 | const e = files.error; | |
| 263 | return err({ | |
| 264 | error: | |
| 265 | e === "too_large" ? "patch_too_large" | |
| 266 | : e === "empty" ? "patch_missing" | |
| 267 | : e === "bad_path" ? "patch_bad_path" | |
| 268 | : e === "unsupported" ? "patch_unsupported" | |
| 269 | : "patch_does_not_apply", | |
| 270 | }); | |
| 271 | } | |
| 272 | const account = await offerAccount(app); | |
| 273 | if (account === undefined) return err({ error: "unavailable" }); | |
| 274 | ||
| 275 | const segment = crypto.randomUUID().replace(/-/g, "").slice(0, 8); | |
| 276 | const branch = `${BRANCH_PREFIX}/${segment}/${Date.now().toString(36)}`; | |
| 277 | const committed = await commitFiles(dir, { | |
| 278 | branch, | |
| 279 | startPoint: base, | |
| 280 | message: clean.value.title, | |
| 281 | files: files.value, | |
| 282 | author: commitIdentity(app.config, account), | |
| 283 | }); | |
| 284 | // `.git` as a path component is refused here too, by the writer itself. | |
| 285 | if (!committed.ok) return err({ error: committed.error.kind === "invalid_path" ? "patch_touches_git" : "unavailable" }); | |
| 286 | const opened = await openPull(app, account, repo, { | |
| 287 | title: clean.value.title, | |
| 288 | body: `${FROM_OUTSIDE}\n\n${clean.value.body}`.trim(), | |
| 289 | baseBranch: base, | |
| 290 | headBranch: branch, | |
| 291 | outsideAgent: true, | |
| 292 | }); | |
| 293 | if (!opened.ok) { | |
| 294 | // Nothing is kept of a refused offer: the branch goes with it. | |
| 295 | await runGit(["update-ref", "-d", `refs/heads/${branch}`], { cwd: dir }); | |
| 296 | return err({ error: "unavailable" }); | |
| 297 | } | |
| 298 | return ok({ number: opened.value.number, pull: opened.value }); | |
| 299 | } |
src/contract/openapi/routes/index.ts +1
| @@ -365,6 +365,7 @@ export { route as postApiV1ReposOwnerNameJobsIdAction } from "./postApiV1ReposOw | ||
| 365 | 365 | export { route as postApiV1ReposOwnerNameLabels } from "./postApiV1ReposOwnerNameLabels.ts"; |
| 366 | 366 | export { route as postApiV1ReposOwnerNameMakeItYours } from "./postApiV1ReposOwnerNameMakeItYours.ts"; |
| 367 | 367 | export { route as postApiV1ReposOwnerNameMilestonesNumberLimb } from "./postApiV1ReposOwnerNameMilestonesNumberLimb.ts"; |
| 368 | export { route as postApiV1ReposOwnerNameOffers } from "./postApiV1ReposOwnerNameOffers.ts"; | |
| 368 | 369 | export { route as postApiV1ReposOwnerNameOriginHook } from "./postApiV1ReposOwnerNameOriginHook.ts"; |
| 369 | 370 | export { route as postApiV1ReposOwnerNameOriginReconnect } from "./postApiV1ReposOwnerNameOriginReconnect.ts"; |
| 370 | 371 | export { route as postApiV1ReposOwnerNamePlan } from "./postApiV1ReposOwnerNamePlan.ts"; |
src/contract/openapi/routes/postApiV1ReposOwnerNameOffers.ts added +12
| @@ -0,0 +1,12 @@ | ||
| 1 | import { REPO, type Route } from "../shared.ts"; | |
| 2 | ||
| 3 | export const route: Route = { | |
| 4 | method: "post", | |
| 5 | path: `${REPO}/offers`, | |
| 6 | summary: | |
| 7 | "Offer a change to a public repository as a unified diff (at most 200 KB), without an account; answers 202 { number, state, url, message }: a pull a person reads before anything runs or lands. A patch that does not apply, touches .git or is too large answers 422 naming why, and nothing is stored; a few a minute from one source", | |
| 8 | auth: false, | |
| 9 | role: "anonymous", | |
| 10 | body: { title: "string", body: "string", patch: "string" }, | |
| 11 | status: 202, | |
| 12 | }; |
src/contract/tags.ts +1
| @@ -16,6 +16,7 @@ const ALIAS: Readonly<Record<string, string>> = { | ||
| 16 | 16 | upstream: "forks", |
| 17 | 17 | "make-it-yours": "forks", |
| 18 | 18 | contribute: "forks", |
| 19 | offers: "forks", | |
| 19 | 20 | queue: "gates", |
| 20 | 21 | growth: "issues", |
| 21 | 22 | chapters: "issues", |
src/http/api.ts +2
| @@ -402,6 +402,7 @@ import { buildCartRoutes } from "./api/build_cart.ts"; | ||
| 402 | 402 | import { iterationRoutes } from "./api/iterations.ts"; |
| 403 | 403 | import { appNoteRoutes } from "./api/app_notes.ts"; |
| 404 | 404 | import { reportRoutes } from "./api/reports.ts"; |
| 405 | import { offerHandler } from "./offer_routes.ts"; | |
| 405 | 406 | import { startRoutes } from "./api/start.ts"; |
| 406 | 407 | import { rollbackRoutes } from "./api/rollback.ts"; |
| 407 | 408 | import { takeBackRoutes } from "./api/take_back.ts"; |
| @@ -2207,6 +2208,7 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 2207 | 2208 | chapterRoutes(router); |
| 2208 | 2209 | |
| 2209 | 2210 | reportRoutes(router); |
| 2211 | router.add("POST", "/api/v1/repos/:owner/:name/offers", offerHandler); | |
| 2210 | 2212 | startRoutes(router); |
| 2211 | 2213 | appNoteRoutes(router); |
| 2212 | 2214 | rollbackRoutes(router); |
src/http/offer_routes.ts added +57
| @@ -0,0 +1,57 @@ | ||
| 1 | /** | |
| 2 | * The HTTP door of an offer (#12402): `POST /api/v1/repos/:owner/:name/offers`. | |
| 3 | * A stranger's patch on a public repository goes through `fileOffer` | |
| 4 | * (`actions/offer.ts`), the action and words the terminal's door shares. It | |
| 5 | * becomes a pull a person reads before anything runs. | |
| 6 | */ | |
| 7 | import { | |
| 8 | fileOffer, | |
| 9 | OFFER_HINT, | |
| 10 | OFFER_REQUEST_MAX, | |
| 11 | OFFER_RECEIVED, | |
| 12 | OFFER_WORDS, | |
| 13 | type OfferRefusal, | |
| 14 | } from "../actions/offer.ts"; | |
| 15 | import { canRead, getRepo } from "../core/repos.ts"; | |
| 16 | import type { Handler } from "./context.ts"; | |
| 17 | import { clientBucket } from "./peer.ts"; | |
| 18 | import { apiError, boundedBody, json, notFound, str as bodyStr, tryAgain } from "./respond.ts"; | |
| 19 | ||
| 20 | /** A full queue drains as people read offers; no clock says when, so a minute. */ | |
| 21 | const QUEUE_RETRY = 60; | |
| 22 | ||
| 23 | /** The handler of `POST /api/v1/repos/:owner/:name/offers`, registered in `api.ts`. */ | |
| 24 | export const offerHandler: Handler = async (ctx) => { | |
| 25 | const found = getRepo(ctx.app.db, ctx.params.owner!, ctx.params.name!); | |
| 26 | const repo = found && canRead(ctx.app.db, found, ctx.auth.user) ? found : undefined; | |
| 27 | if (!repo) return notFound(); | |
| 28 | const input = await boundedBody(ctx.request, OFFER_REQUEST_MAX); | |
| 29 | if (input === "too_large") return apiError("patch_too_large", 422, OFFER_WORDS.patch_too_large); | |
| 30 | const filed = await fileOffer( | |
| 31 | ctx.app, | |
| 32 | repo, | |
| 33 | { title: bodyStr(input.title), body: bodyStr(input.body), patch: input.patch }, | |
| 34 | clientBucket(ctx.request), | |
| 35 | ); | |
| 36 | if (!filed.ok) { | |
| 37 | const refusal = filed.error; | |
| 38 | if (refusal.error === "not_found") return notFound(); | |
| 39 | if (refusal.error === "rate_limited") | |
| 40 | return tryAgain("rate_limited", 429, "too many offers; wait and send again", refusal.retryAfter); | |
| 41 | if (refusal.error === "queue_full") | |
| 42 | return tryAgain("queue_full", 429, "this repository has many offers waiting; try again later", QUEUE_RETRY); | |
| 43 | if (refusal.error === "unavailable") | |
| 44 | return tryAgain("unavailable", 503, "offers cannot be taken just now; try again later", QUEUE_RETRY); | |
| 45 | const why: OfferRefusal = refusal.error; | |
| 46 | return json({ error: why, message: `${OFFER_WORDS[why]}; ${OFFER_HINT}` }, 422); | |
| 47 | } | |
| 48 | return json( | |
| 49 | { | |
| 50 | number: filed.value.number, | |
| 51 | state: "open", | |
| 52 | url: `${ctx.url.origin}/${repo.ownerName}/${repo.name}/pulls/${filed.value.number}`, | |
| 53 | message: OFFER_RECEIVED, | |
| 54 | }, | |
| 55 | 202, | |
| 56 | ); | |
| 57 | }; |
src/tui/screens/offer.ts added +135
| @@ -0,0 +1,135 @@ | ||
| 1 | /** | |
| 2 | * Offer a patch, from a repository screen's `:` palette (#12402): the | |
| 3 | * terminal's door beside `POST /api/v1/repos/:owner/:name/offers`. A title | |
| 4 | * and a pasted unified diff go through `fileOffer`, the same action, limits | |
| 5 | * and words as the API; the pull it becomes waits for a person. Offered to a | |
| 6 | * signed-in reader who cannot write the repository: a writer pushes a branch | |
| 7 | * instead. | |
| 8 | */ | |
| 9 | ||
| 10 | import { fileOffer, OFFER_RECEIVED, OFFER_WORDS, takesOffers, type OfferError } from "../../actions/offer.ts"; | |
| 11 | import { canWrite, type Repo } from "../../core/repos.ts"; | |
| 12 | import type { Ctx, Screen } from "../app/app.ts"; | |
| 13 | import { binding } from "../app/keys.ts"; | |
| 14 | import type { Command } from "../app/palette.ts"; | |
| 15 | import { fill, len, split } from "../ui/layout.ts"; | |
| 16 | import { Textarea } from "../ui/textarea.ts"; | |
| 17 | import { card, field } from "../ui/widgets.ts"; | |
| 18 | import type { Session } from "../session.ts"; | |
| 19 | import { runAsync } from "./common.ts"; | |
| 20 | ||
| 21 | /** What a refused offer is told in the terminal: one line, the doors' own words. */ | |
| 22 | export function offerRefusalText(e: OfferError): string { | |
| 23 | switch (e.error) { | |
| 24 | case "not_found": | |
| 25 | return "This repository does not take offers."; | |
| 26 | case "rate_limited": | |
| 27 | return `Too many offers; try again in ${e.retryAfter} seconds.`; | |
| 28 | case "queue_full": | |
| 29 | return "This repository has many offers waiting; try again later."; | |
| 30 | case "unavailable": | |
| 31 | return "Offers cannot be taken just now; try again later."; | |
| 32 | default: | |
| 33 | return `${OFFER_WORDS[e.error]}.`; | |
| 34 | } | |
| 35 | } | |
| 36 | ||
| 37 | /** The palette row for `repo`: a signed-in reader who cannot write it, where offers are taken. */ | |
| 38 | export function offerCommands(f: Session, ctx: Ctx, repo: Repo | undefined): Command[] { | |
| 39 | if (repo === undefined || f.user === undefined) return []; | |
| 40 | if (canWrite(f.app.db, repo, f.user) || !takesOffers(f.app, repo)) return []; | |
| 41 | return [ | |
| 42 | { | |
| 43 | id: "offer", | |
| 44 | title: `${repo.ownerName}/${repo.name} — Offer a patch`, | |
| 45 | hint: "a person reads it first", | |
| 46 | run: () => ctx.push(offerScreen(f, ctx, repo)), | |
| 47 | }, | |
| 48 | ]; | |
| 49 | } | |
| 50 | ||
| 51 | /** A title and a pasted diff; ctrl+d sends. A refusal keeps everything typed. */ | |
| 52 | export function offerScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | |
| 53 | let title = ""; | |
| 54 | const patch = new Textarea(""); | |
| 55 | let focus: "title" | "patch" = "title"; | |
| 56 | let error: string | undefined; | |
| 57 | ||
| 58 | const submit = (): void => { | |
| 59 | // One limit per account: the terminal needs one, so its id is the source. | |
| 60 | const source = `tui:${f.user?.id ?? "nobody"}`; | |
| 61 | runAsync(ctx, async () => { | |
| 62 | const filed = await fileOffer(f.app, repo, { title, body: "", patch: patch.value }, source); | |
| 63 | if (!filed.ok) { | |
| 64 | error = offerRefusalText(filed.error); | |
| 65 | ctx.toast(error, "danger"); | |
| 66 | return; | |
| 67 | } | |
| 68 | ctx.toast(`Pull #${filed.value.number} opened. ${OFFER_RECEIVED}`, "success"); | |
| 69 | ctx.pop(); | |
| 70 | }); | |
| 71 | }; | |
| 72 | ||
| 73 | return { | |
| 74 | title: "Offer a patch", | |
| 75 | keys: [binding(["tab"], "next field"), binding(["ctrl+d"], "send"), binding(["esc"], "discard")], | |
| 76 | render(area, s) { | |
| 77 | const c = ctx.theme.c; | |
| 78 | const [top, rest] = split(area, "v", [len(4), fill(1)]); | |
| 79 | const cursor = field(s, top!, c, { | |
| 80 | label: "Title", | |
| 81 | value: title, | |
| 82 | focused: focus === "title", | |
| 83 | ...(focus === "title" ? { cursor: [...title].length } : {}), | |
| 84 | ...(error === undefined ? { hint: "One line: what the change does." } : { error }), | |
| 85 | }); | |
| 86 | const inner = card(s, rest!, c, { | |
| 87 | title: "Patch (git diff output)", | |
| 88 | footer: "ctrl+d send", | |
| 89 | focused: focus === "patch", | |
| 90 | }); | |
| 91 | const patchCursor = patch.render(s, inner, c, focus === "patch"); | |
| 92 | ctx.setCursor(focus === "title" ? cursor : patchCursor); | |
| 93 | }, | |
| 94 | paste(text) { | |
| 95 | if (focus === "patch") patch.paste(text); | |
| 96 | else title += text.replace(/\s+/g, " "); | |
| 97 | error = undefined; | |
| 98 | return true; | |
| 99 | }, | |
| 100 | key(e) { | |
| 101 | if (e.ctrl && e.key === "d") { | |
| 102 | submit(); | |
| 103 | return true; | |
| 104 | } | |
| 105 | if (e.key === "tab") { | |
| 106 | focus = focus === "title" ? "patch" : "title"; | |
| 107 | return true; | |
| 108 | } | |
| 109 | if (focus === "patch") { | |
| 110 | const out = patch.key(e); | |
| 111 | if (out === "submit") { | |
| 112 | submit(); | |
| 113 | return true; | |
| 114 | } | |
| 115 | if (out === "cancel") return false; | |
| 116 | return out !== "ignored"; | |
| 117 | } | |
| 118 | if (e.key === "enter") { | |
| 119 | focus = "patch"; | |
| 120 | return true; | |
| 121 | } | |
| 122 | if (e.key === "backspace") { | |
| 123 | title = [...title].slice(0, -1).join(""); | |
| 124 | error = undefined; | |
| 125 | return true; | |
| 126 | } | |
| 127 | if (!e.ctrl && !e.alt && [...e.key].length === 1) { | |
| 128 | title += e.key; | |
| 129 | error = undefined; | |
| 130 | return true; | |
| 131 | } | |
| 132 | return false; | |
| 133 | }, | |
| 134 | }; | |
| 135 | } |
src/tui/screens/workspace.ts +2
| @@ -152,6 +152,7 @@ import { | ||
| 152 | 152 | } from "./common.ts"; |
| 153 | 153 | import { noteCommands } from "./notes.ts"; |
| 154 | 154 | import { reportCommands } from "./report.ts"; |
| 155 | import { offerCommands } from "./offer.ts"; | |
| 155 | 156 | import { reportsCommands } from "./reports.ts"; |
| 156 | 157 | import { tellCommands } from "./tell.ts"; |
| 157 | 158 | import { workingCommands } from "./working.ts"; |
| @@ -1988,6 +1989,7 @@ export function workspaceScreen( | ||
| 1988 | 1989 | ...workingCommands(f, ctx, repo), |
| 1989 | 1990 | ...tellCommands(f, ctx, () => screen, repo), |
| 1990 | 1991 | ...reportCommands(f, ctx, () => screen, repo), |
| 1992 | ...offerCommands(f, ctx, repo), | |
| 1991 | 1993 | ...reportsCommands(f, ctx, repo), |
| 1992 | 1994 | ...noteCommands(f, ctx, () => screen, repo), |
| 1993 | 1995 | ...yoursCommands(f, ctx, repo), |
test/offer-limits.test.ts added +169
| @@ -0,0 +1,169 @@ | ||
| 1 | /** | |
| 2 | * The limits and refusals of an offer from outside (#12402) that the main test does not reach: | |
| 3 | * a full queue, the per-repository and forge limits, a symlink, a request body too big by the | |
| 4 | * bytes actually read, and the terminal door, which goes through the same action. | |
| 5 | */ | |
| 6 | import { afterAll, afterEach, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { editFiles } from "../src/actions/contents.ts"; | |
| 12 | import { createRepo } from "../src/actions/repos.ts"; | |
| 13 | import { fileOffer, OFFER_REQUEST_MAX, OFFERS_WAITING_MAX } from "../src/actions/offer.ts"; | |
| 14 | import { getRepo, type Repo } from "../src/core/repos.ts"; | |
| 15 | import { createUser, type User } from "../src/core/users.ts"; | |
| 16 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 17 | ||
| 18 | const patchFor = (n: number) => `diff --git a/f${n}.txt b/f${n}.txt\nnew file mode 100644\n--- /dev/null\n+++ b/f${n}.txt\n@@ -0,0 +1 @@\n+${n}\n`; | |
| 19 | ||
| 20 | const realNow = Date.now; | |
| 21 | let clock = realNow(); | |
| 22 | afterEach(() => { | |
| 23 | Date.now = realNow; | |
| 24 | }); | |
| 25 | ||
| 26 | describe("offer limits", () => { | |
| 27 | let dataDir: string; | |
| 28 | let app: App; | |
| 29 | let servers: Servers; | |
| 30 | let alice: User; | |
| 31 | const repos: Repo[] = []; | |
| 32 | ||
| 33 | async function publicRepo(name: string): Promise<Repo> { | |
| 34 | const made = await createRepo(app, alice, { name, visibility: "public" }); | |
| 35 | if (!made.ok) throw new Error(String(made.error)); | |
| 36 | const seeded = await editFiles(app, alice, made.value, { files: [{ path: "hello.txt", content: "hello\n" }], message: "first" }); | |
| 37 | if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); | |
| 38 | const repo = getRepo(app.db, "alice", name)!; | |
| 39 | repos.push(repo); | |
| 40 | return repo; | |
| 41 | } | |
| 42 | ||
| 43 | beforeAll(async () => { | |
| 44 | dataDir = await mkdtemp(join(tmpdir(), "forge-offer-limits-")); | |
| 45 | app = createApp({ dataDir, port: 0 }); | |
| 46 | servers = await serve(app); | |
| 47 | const a = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 48 | if (!a.ok) throw new Error(a.error); | |
| 49 | alice = a.value; | |
| 50 | }, 30_000); | |
| 51 | ||
| 52 | afterAll(async () => { | |
| 53 | servers.stop(); | |
| 54 | app.close(); | |
| 55 | await rm(dataDir, { recursive: true, force: true }); | |
| 56 | }); | |
| 57 | ||
| 58 | test("a repository holding 20 open offers takes no more (queue_full), and 10 a minute reach it first", async () => { | |
| 59 | const repo = await publicRepo("queue"); | |
| 60 | Date.now = () => clock; | |
| 61 | let n = 0; | |
| 62 | // The per-repository limit is ten a minute: wait a minute between tens, and the queue fills. | |
| 63 | // The ten of a minute are filed together, as ten strangers at once would: each on its own | |
| 64 | // branch. | |
| 65 | for (let minute = 0; minute < 2; minute++) { | |
| 66 | const tens = Array.from({ length: 10 }, (_, i) => n + i); | |
| 67 | const done = await Promise.all(tens.map((k) => fileOffer(app, repo, { title: `t${k}`, body: "", patch: patchFor(k) }, `src-${k}`))); | |
| 68 | expect(done.map((d) => d.ok)).toEqual(tens.map(() => true)); | |
| 69 | n += tens.length; | |
| 70 | const limited = await fileOffer(app, repo, { title: "over", body: "", patch: patchFor(99) }, "src-over"); | |
| 71 | expect(!limited.ok && limited.error).toEqual({ error: "rate_limited", retryAfter: expect.any(Number) }); | |
| 72 | clock += 61_000; | |
| 73 | } | |
| 74 | expect(n).toBe(OFFERS_WAITING_MAX); | |
| 75 | const full = await fileOffer(app, repo, { title: "late", body: "", patch: patchFor(100) }, "src-late"); | |
| 76 | expect(!full.ok && full.error).toEqual({ error: "queue_full" }); | |
| 77 | const res = await fetch(`${servers.url}/api/v1/repos/alice/queue/offers`, { | |
| 78 | method: "POST", | |
| 79 | headers: { "content-type": "application/json", "x-forwarded-for": "10.9.0.1" }, | |
| 80 | body: JSON.stringify({ title: "late", patch: patchFor(101) }), | |
| 81 | }); | |
| 82 | expect(res.status).toBe(429); | |
| 83 | expect(((await res.json()) as any).error).toBe("queue_full"); | |
| 84 | }, 60_000); | |
| 85 | ||
| 86 | test("one repository takes 10 offers a minute from different sources, then 429 over HTTP", async () => { | |
| 87 | const repo = await publicRepo("perrepo"); | |
| 88 | const ten = Array.from({ length: 10 }, (_, i) => i); | |
| 89 | const done = await Promise.all(ten.map((i) => fileOffer(app, repo, { title: `t${i}`, body: "", patch: patchFor(i) }, `r-${i}`))); | |
| 90 | expect(done.map((d) => d.ok)).toEqual(ten.map(() => true)); | |
| 91 | const res = await fetch(`${servers.url}/api/v1/repos/alice/perrepo/offers`, { | |
| 92 | method: "POST", | |
| 93 | headers: { "content-type": "application/json", "x-forwarded-for": "10.9.1.1" }, | |
| 94 | body: JSON.stringify({ title: "x", patch: patchFor(50) }), | |
| 95 | }); | |
| 96 | expect(res.status).toBe(429); | |
| 97 | expect(((await res.json()) as any).error).toBe("rate_limited"); | |
| 98 | expect(res.headers.get("retry-after")).not.toBeNull(); | |
| 99 | }, 60_000); | |
| 100 | ||
| 101 | test("the forge takes 30 offers a minute across repositories, then refuses even a repository with room", async () => { | |
| 102 | // A fresh forge: its own limits, and the clock stands still inside one minute. The limits are | |
| 103 | // taken before any git work, so an offer to an empty repository (patch_does_not_apply, cheap) | |
| 104 | // spends the forge's minute as a landed one would; the last, to a repository with room and a | |
| 105 | // patch that applies, is refused by the forge's limit alone. | |
| 106 | const dir = await mkdtemp(join(tmpdir(), "forge-offer-forge-")); | |
| 107 | const other = createApp({ dataDir: dir, port: 0 }); | |
| 108 | try { | |
| 109 | const u = await createUser(other.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 110 | if (!u.ok) throw new Error(u.error); | |
| 111 | const made: Repo[] = []; | |
| 112 | for (const name of ["a", "b", "c", "d"]) { | |
| 113 | const r = await createRepo(other, u.value, { name, visibility: "public" }); | |
| 114 | if (!r.ok) throw new Error(String(r.error)); | |
| 115 | made.push(getRepo(other.db, "alice", name)!); | |
| 116 | } | |
| 117 | const roomy = made[3]!; | |
| 118 | await editFiles(other, u.value, roomy, { files: [{ path: "hello.txt", content: "hello\n" }], message: "first" }); | |
| 119 | let n = 0; | |
| 120 | for (const repo of made.slice(0, 3)) { | |
| 121 | for (let i = 0; i < 10; i++) { | |
| 122 | const done = await fileOffer(other, repo, { title: `t${n}`, body: "", patch: patchFor(n) }, `f-${n}`); | |
| 123 | expect(!done.ok && done.error.error).toBe("patch_does_not_apply"); | |
| 124 | n++; | |
| 125 | } | |
| 126 | } | |
| 127 | const refused = await fileOffer(other, getRepo(other.db, "alice", "d")!, { title: "x", body: "", patch: patchFor(77) }, "f-last"); | |
| 128 | expect(!refused.ok && refused.error.error).toBe("rate_limited"); | |
| 129 | } finally { | |
| 130 | other.close(); | |
| 131 | await rm(dir, { recursive: true, force: true }); | |
| 132 | } | |
| 133 | }, 120_000); | |
| 134 | ||
| 135 | test("a symlink or a submodule in the patch is patch_unsupported, and nothing is stored", async () => { | |
| 136 | const repo = await publicRepo("links"); | |
| 137 | const link = "diff --git a/ln b/ln\nnew file mode 120000\n--- /dev/null\n+++ b/ln\n@@ -0,0 +1 @@\n+hello.txt\n\\ No newline at end of file\n"; | |
| 138 | const sub = "diff --git a/mod b/mod\nnew file mode 160000\n--- /dev/null\n+++ b/mod\n@@ -0,0 +1 @@\n+Subproject commit 1111111111111111111111111111111111111111\n"; | |
| 139 | for (const [i, patch] of [link, sub].entries()) { | |
| 140 | const res = await fetch(`${servers.url}/api/v1/repos/alice/links/offers`, { | |
| 141 | method: "POST", | |
| 142 | headers: { "content-type": "application/json", "x-forwarded-for": `10.9.2.${i}` }, | |
| 143 | body: JSON.stringify({ title: "x", patch }), | |
| 144 | }); | |
| 145 | expect(res.status).toBe(422); | |
| 146 | expect(((await res.json()) as any).error).toBe("patch_unsupported"); | |
| 147 | } | |
| 148 | expect(repo.id).toBeTruthy(); | |
| 149 | }); | |
| 150 | ||
| 151 | test("a request body over the bound is a 422 on the bytes read, though no patch field is that large", async () => { | |
| 152 | await publicRepo("bigbody"); | |
| 153 | // A small patch, with padding in another field that no declared length could hide. | |
| 154 | const body = JSON.stringify({ title: "x", patch: patchFor(1), pad: "y".repeat(OFFER_REQUEST_MAX) }); | |
| 155 | const res = await fetch(`${servers.url}/api/v1/repos/alice/bigbody/offers`, { | |
| 156 | method: "POST", | |
| 157 | headers: { "content-type": "application/json", "x-forwarded-for": "10.9.3.1" }, | |
| 158 | body: new ReadableStream({ | |
| 159 | start(c) { | |
| 160 | c.enqueue(new TextEncoder().encode(body)); | |
| 161 | c.close(); | |
| 162 | }, | |
| 163 | }), | |
| 164 | duplex: "half", | |
| 165 | } as RequestInit); | |
| 166 | expect(res.status).toBe(422); | |
| 167 | expect(((await res.json()) as any).error).toBe("patch_too_large"); | |
| 168 | }); | |
| 169 | }); |
test/offer-stranger.test.ts added +161
| @@ -0,0 +1,161 @@ | ||
| 1 | /** | |
| 2 | * An outsider with no account offers a patch to a public repository, held as a stranger's pull | |
| 3 | * for a person (#12402). | |
| 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 { editFiles } from "../src/actions/contents.ts"; | |
| 11 | import { createRepo } from "../src/actions/repos.ts"; | |
| 12 | import { OFFER_PATCH_MAX } from "../src/actions/offer.ts"; | |
| 13 | import { getPull } from "../src/core/pulls.ts"; | |
| 14 | import { getRepo, type Repo } from "../src/core/repos.ts"; | |
| 15 | import { createUser, type User } from "../src/core/users.ts"; | |
| 16 | import { isStrangerPull } from "../src/crew/stranger.ts"; | |
| 17 | import { runGit } from "../src/git/env.ts"; | |
| 18 | import { repoDir } from "../src/git/repos.ts"; | |
| 19 | import { guestWaits } from "../src/origins/guest.ts"; | |
| 20 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 21 | ||
| 22 | const PATCH = `diff --git a/hello.txt b/hello.txt | |
| 23 | --- a/hello.txt | |
| 24 | +++ b/hello.txt | |
| 25 | @@ -1,2 +1,2 @@ | |
| 26 | hello | |
| 27 | -world | |
| 28 | +there | |
| 29 | `; | |
| 30 | ||
| 31 | describe("offers from outside", () => { | |
| 32 | let dataDir: string; | |
| 33 | let app: App; | |
| 34 | let servers: Servers; | |
| 35 | let alice: User; | |
| 36 | let game: Repo; | |
| 37 | let n = 1; | |
| 38 | ||
| 39 | /** Each call is its own source, so one test's limit never meets another's. */ | |
| 40 | const offer = (body: unknown, path = "/api/v1/repos/alice/game/offers", source = `10.1.0.${n++}`) => | |
| 41 | fetch(`${servers.url}${path}`, { | |
| 42 | method: "POST", | |
| 43 | headers: { "content-type": "application/json", "x-forwarded-for": source }, | |
| 44 | body: JSON.stringify(body), | |
| 45 | }); | |
| 46 | ||
| 47 | const branches = async (): Promise<string[]> => { | |
| 48 | const out = await runGit(["for-each-ref", "--format=%(refname:short)", "refs/heads"], { | |
| 49 | cwd: repoDir(app.reposRoot, "alice", "game"), | |
| 50 | }); | |
| 51 | return out.ok ? out.value.split("\n").filter(Boolean) : []; | |
| 52 | }; | |
| 53 | const pullCount = (): number => | |
| 54 | app.db.query<{ n: number }, [string]>("SELECT COUNT(*) AS n FROM pull JOIN issue ON issue.id = pull.issue_id WHERE issue.repo_id = ?1").get(game.id)!.n; | |
| 55 | ||
| 56 | beforeAll(async () => { | |
| 57 | dataDir = await mkdtemp(join(tmpdir(), "forge-offer-")); | |
| 58 | app = createApp({ dataDir, port: 0 }); | |
| 59 | servers = await serve(app); | |
| 60 | const a = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 61 | if (!a.ok) throw new Error(a.error); | |
| 62 | alice = a.value; | |
| 63 | const made = await createRepo(app, alice, { name: "game", visibility: "public" }); | |
| 64 | if (!made.ok) throw new Error(String(made.error)); | |
| 65 | game = made.value; | |
| 66 | const seeded = await editFiles(app, alice, game, { files: [{ path: "hello.txt", content: "hello\nworld\n" }], message: "first" }); | |
| 67 | if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); | |
| 68 | const priv = await createRepo(app, alice, { name: "secret", visibility: "private" }); | |
| 69 | if (!priv.ok) throw new Error(String(priv.error)); | |
| 70 | const s = getRepo(app.db, "alice", "secret")!; | |
| 71 | await editFiles(app, alice, s, { files: [{ path: "hello.txt", content: "hello\nworld\n" }], message: "first" }); | |
| 72 | }, 30_000); | |
| 73 | ||
| 74 | afterAll(async () => { | |
| 75 | servers.stop(); | |
| 76 | app.close(); | |
| 77 | await rm(dataDir, { recursive: true, force: true }); | |
| 78 | }); | |
| 79 | ||
| 80 | test("a patch with no token becomes a stranger's pull that waits for a person", async () => { | |
| 81 | const res = await offer({ title: "Say there", body: "friendlier", patch: PATCH }); | |
| 82 | expect(res.status).toBe(202); | |
| 83 | const done: any = await res.json(); | |
| 84 | expect(done.state).toBe("open"); | |
| 85 | expect(done.url).toBe(`${servers.url}/alice/game/pulls/${done.number}`); | |
| 86 | const pull = getPull(app.db, game.id, done.number)!; | |
| 87 | expect(pull.title).toBe("Say there"); | |
| 88 | expect(isStrangerPull(app.db, game, pull)).toBe(true); | |
| 89 | // No secrets or preview and no crew until a person approves it. | |
| 90 | expect(guestWaits(app.db, pull)).toBe(true); | |
| 91 | const shown = await runGit(["show", `${pull.headBranch}:hello.txt`], { cwd: repoDir(app.reposRoot, "alice", "game") }); | |
| 92 | expect(shown.ok && shown.value).toBe("hello\nthere"); | |
| 93 | // The default branch is untouched. | |
| 94 | const main = await runGit(["show", `${game.defaultBranch}:hello.txt`], { cwd: repoDir(app.reposRoot, "alice", "game") }); | |
| 95 | expect(main.ok && main.value).toBe("hello\nworld"); | |
| 96 | }); | |
| 97 | ||
| 98 | test("a patch that does not apply is a 422 naming why, and nothing is stored", async () => { | |
| 99 | const before = { pulls: pullCount(), branches: await branches() }; | |
| 100 | const stale = PATCH.replace("-world", "-earth"); | |
| 101 | const res = await offer({ title: "Stale", patch: stale }); | |
| 102 | expect(res.status).toBe(422); | |
| 103 | const refused: any = await res.json(); | |
| 104 | expect(refused.error).toBe("patch_does_not_apply"); | |
| 105 | expect(refused.message).toContain("does not apply"); | |
| 106 | expect(pullCount()).toBe(before.pulls); | |
| 107 | expect(await branches()).toEqual(before.branches); | |
| 108 | }); | |
| 109 | ||
| 110 | test("a patch that touches .git is a 422, in any spelling, and nothing is stored", async () => { | |
| 111 | const before = { pulls: pullCount(), branches: await branches() }; | |
| 112 | const into = (path: string) => `diff --git a/${path} b/${path}\nnew file mode 100644\n--- /dev/null\n+++ b/${path}\n@@ -0,0 +1 @@\n+x\n`; | |
| 113 | for (const path of [".git/hooks/pre-receive", ".GIT/config", "sub/.git/config", ".Git"]) { | |
| 114 | const res = await offer({ title: "Hook", patch: into(path) }); | |
| 115 | expect({ path, status: res.status }).toEqual({ path, status: 422 }); | |
| 116 | expect(((await res.json()) as any).error).toBe("patch_touches_git"); | |
| 117 | } | |
| 118 | expect(pullCount()).toBe(before.pulls); | |
| 119 | expect(await branches()).toEqual(before.branches); | |
| 120 | }); | |
| 121 | ||
| 122 | test("a path that climbs out, is absolute or is quoted is a 422", async () => { | |
| 123 | const into = (path: string) => `diff --git a/${path} b/${path}\nnew file mode 100644\n--- /dev/null\n+++ b/${path}\n@@ -0,0 +1 @@\n+x\n`; | |
| 124 | for (const path of ["../outside.txt", "a/../../b.txt", "//etc/passwd", '"\\056git/config"']) { | |
| 125 | const res = await offer({ title: "Out", patch: into(path) }); | |
| 126 | expect({ path, status: res.status }).toEqual({ path, status: 422 }); | |
| 127 | expect(((await res.json()) as any).error).toBe("patch_bad_path"); | |
| 128 | } | |
| 129 | }); | |
| 130 | ||
| 131 | test("a patch over 200 KB is a 422, however it arrives", async () => { | |
| 132 | const big = `diff --git a/big.txt b/big.txt\nnew file mode 100644\n--- /dev/null\n+++ b/big.txt\n@@ -0,0 +1 @@\n+${"x".repeat(OFFER_PATCH_MAX)}\n`; | |
| 133 | const res = await offer({ title: "Big", patch: big }); | |
| 134 | expect(res.status).toBe(422); | |
| 135 | expect(((await res.json()) as any).error).toBe("patch_too_large"); | |
| 136 | }); | |
| 137 | ||
| 138 | test("a missing title or patch, or a binary patch, is a 422", async () => { | |
| 139 | expect(((await (await offer({ title: " ", patch: PATCH })).json()) as any).error).toBe("empty_title"); | |
| 140 | expect(((await (await offer({ title: "x" })).json()) as any).error).toBe("patch_missing"); | |
| 141 | expect(((await (await offer({ title: "x", patch: 7 })).json()) as any).error).toBe("patch_missing"); | |
| 142 | const binary = "diff --git a/p.bin b/p.bin\nnew file mode 100644\nindex 0000000..e69de29\nBinary files /dev/null and b/p.bin differ\n"; | |
| 143 | const res = await offer({ title: "x", patch: binary }); | |
| 144 | expect(res.status).toBe(422); | |
| 145 | }); | |
| 146 | ||
| 147 | test("a private or missing repository answers not_found, and a few a minute from one source", async () => { | |
| 148 | expect((await offer({ title: "x", patch: PATCH }, "/api/v1/repos/alice/secret/offers")).status).toBe(404); | |
| 149 | expect((await offer({ title: "x", patch: PATCH }, "/api/v1/repos/alice/none/offers")).status).toBe(404); | |
| 150 | const source = "10.9.9.9"; | |
| 151 | const stale = PATCH.replace("-world", "-earth"); | |
| 152 | const seen: number[] = []; | |
| 153 | for (let i = 0; i < 4; i++) seen.push((await offer({ title: "x", patch: stale }, undefined, source)).status); | |
| 154 | expect(seen).toEqual([422, 422, 422, 429]); | |
| 155 | }); | |
| 156 | ||
| 157 | test("the OpenAPI contract lists it", async () => { | |
| 158 | const spec: any = await (await fetch(`${servers.url}/contract/openapi.json`)).json(); | |
| 159 | expect(Object.keys(spec.paths).some((p) => p.endsWith("/offers"))).toBe(true); | |
| 160 | }); | |
| 161 | }); |
test/signup-gate-parent.test.ts +1
| @@ -113,6 +113,7 @@ test("every caller of createUser says where its account comes from", async () => | ||
| 113 | 113 | "src/actions/contribute_token.ts": ["start"], |
| 114 | 114 | "src/actions/crew.ts": ["agent"], |
| 115 | 115 | "src/actions/first_admin.ts": ["first-admin"], |
| 116 | "src/actions/offer.ts": ["start"], | |
| 116 | 117 | "src/actions/spawn.ts": ["spawn", "spawn"], |
| 117 | 118 | "src/actions/start.ts": ["start", "start"], |
| 118 | 119 | "src/actions/users.ts": ["signup", "passkey"], |
test/tui-offer.test.ts added +116
| @@ -0,0 +1,116 @@ | ||
| 1 | /** | |
| 2 | * Offer a patch from a repository screen's palette (#12402): the terminal's | |
| 3 | * door beside the API. A title and a pasted diff become a | |
| 4 | * stranger's pull through `fileOffer`; a writer is not offered the row. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { rm } from "node:fs/promises"; | |
| 8 | import { editFiles } from "../src/actions/contents.ts"; | |
| 9 | import { createRepo } from "../src/actions/repos.ts"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import type { Repo } from "../src/core/repos.ts"; | |
| 12 | import { createUser, type User } from "../src/core/users.ts"; | |
| 13 | import { isStrangerPull } from "../src/crew/stranger.ts"; | |
| 14 | import { getPull } from "../src/core/pulls.ts"; | |
| 15 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 16 | import { repoScreen } from "../src/tui/screens/repo.ts"; | |
| 17 | import { local } from "../src/tui/screens/where.ts"; | |
| 18 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 19 | import { quietLogs } from "./helpers/globals.ts"; | |
| 20 | import { scratchDir } from "./helpers/scratch.ts"; | |
| 21 | import { settleApp } from "./helpers/tui.ts"; | |
| 22 | import { proveAll } from "./helpers/verified.ts"; | |
| 23 | ||
| 24 | quietLogs(); | |
| 25 | ||
| 26 | const PATCH = `diff --git a/hello.txt b/hello.txt | |
| 27 | --- a/hello.txt | |
| 28 | +++ b/hello.txt | |
| 29 | @@ -1,2 +1,2 @@ | |
| 30 | hello | |
| 31 | -world | |
| 32 | +there | |
| 33 | `; | |
| 34 | const paste = (text: string): string => `\x1b[200~${text}\x1b[201~`; | |
| 35 | ||
| 36 | let dir: string; | |
| 37 | let app: App; | |
| 38 | let alice: User; | |
| 39 | let bob: User; | |
| 40 | let repo: Repo; | |
| 41 | ||
| 42 | async function palette(user: User, keys: string[]): Promise<string> { | |
| 43 | const terminal = headlessTerminal(120, 40); | |
| 44 | const { app: tui, session } = createForgeApp( | |
| 45 | app, | |
| 46 | { sshUsername: user.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user }, | |
| 47 | { terminal, exitHooks: false }, | |
| 48 | ); | |
| 49 | tui.start(); | |
| 50 | tui.ctx.push(repoScreen(session, tui.ctx, local(session, repo))); | |
| 51 | await settleApp(tui); | |
| 52 | for (const k of keys) { | |
| 53 | terminal.feed(k); | |
| 54 | await settleApp(tui); | |
| 55 | } | |
| 56 | const text = tui.screen.toText(); | |
| 57 | tui.stop?.(); | |
| 58 | return text; | |
| 59 | } | |
| 60 | ||
| 61 | const pulls = (): number => | |
| 62 | app.db | |
| 63 | .query<{ n: number }, [string]>("SELECT COUNT(*) AS n FROM pull JOIN issue ON issue.id = pull.issue_id WHERE issue.repo_id = ?1") | |
| 64 | .get(repo.id)!.n; | |
| 65 | ||
| 66 | beforeAll(async () => { | |
| 67 | dir = scratchDir("forge-tui-offer-"); | |
| 68 | app = createApp({ dataDir: dir, port: 0 }); | |
| 69 | const mk = async (username: string, first = false) => { | |
| 70 | const r = await createUser(app.db, { | |
| 71 | origin: "seed", | |
| 72 | trusted: true, username, email: `${username}@forge.local`, | |
| 73 | password: "a-good-passphrase", adminIfFirst: first, | |
| 74 | }); | |
| 75 | if (!r.ok) throw new Error(r.error); | |
| 76 | return r.value; | |
| 77 | }; | |
| 78 | alice = await mk("alice", true); | |
| 79 | bob = await mk("bob"); | |
| 80 | proveAll(app); | |
| 81 | const made = await createRepo(app, alice, { name: "game", visibility: "public" }); | |
| 82 | if (!made.ok) throw new Error(String(made.error)); | |
| 83 | repo = made.value; | |
| 84 | const seeded = await editFiles(app, alice, repo, { files: [{ path: "hello.txt", content: "hello\nworld\n" }], message: "first" }); | |
| 85 | if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); | |
| 86 | }, 30_000); | |
| 87 | afterAll(async () => { | |
| 88 | app.db.close(); | |
| 89 | await rm(dir, { recursive: true, force: true }); | |
| 90 | }); | |
| 91 | ||
| 92 | describe("offer a patch in the terminal", () => { | |
| 93 | test("a reader who cannot write has the row; the owner does not", async () => { | |
| 94 | expect(await palette(bob, [":", "Offer a"])).toContain("alice/game — Offer a patch"); | |
| 95 | expect(await palette(alice, [":", "Offer a"])).not.toContain("alice/game — Offer a patch"); | |
| 96 | }); | |
| 97 | ||
| 98 | test("a title and a pasted diff open a stranger's pull that waits for a person", async () => { | |
| 99 | const before = pulls(); | |
| 100 | const text = await palette(bob, [":", "Offer a", "\r", ..."Say there".split(""), "\r", paste(PATCH), "\x04"]); | |
| 101 | expect(text).toContain("opened"); | |
| 102 | expect(pulls()).toBe(before + 1); | |
| 103 | const n = app.db | |
| 104 | .query<{ number: number }, [string]>("SELECT issue.number AS number FROM pull JOIN issue ON issue.id = pull.issue_id WHERE issue.repo_id = ?1 AND issue.title = 'Say there'") | |
| 105 | .get(repo.id)!.number; | |
| 106 | const pull = getPull(app.db, repo.id, n)!; | |
| 107 | expect(isStrangerPull(app.db, repo, pull)).toBe(true); | |
| 108 | }); | |
| 109 | ||
| 110 | test("a patch that does not apply says so in the doors' words and opens nothing", async () => { | |
| 111 | const before = pulls(); | |
| 112 | const text = await palette(bob, [":", "Offer a", "\r", ..."Stale".split(""), "\r", paste(PATCH.replace("-world", "-earth")), "\x04"]); | |
| 113 | expect(text).toContain("does not apply"); | |
| 114 | expect(pulls()).toBe(before); | |
| 115 | }); | |
| 116 | }); |
new above
offline
Both doors: there is no terminal screen in src/tui/ for this action. Add one that calls fileOffer, with its entry where AGENTS.md says, plus a test. The 'No door:' empty commit does not satisfy this.