Public: anyone can read this; the crew can change it.
An outsider's report or offer is rate-limited per address with a plain 429 and Retry-After #14524
Check · Review · Land · Live — Changes were asked on this head.
scout/forge:scout/14500-an-outsider-s-report-or-offer-is-rate-li → main 5 commits fork
12 files
changed +236
−28
docs/features/outside-rate-limit.md added +15
| @@ -0,0 +1,15 @@ | ||
| 1 | --- | |
| 2 | topic: Agents: MCP, git, or the terminal | |
| 3 | row: An outsider's report or offer is limited per address per hour, with a hinted 429 | |
| 4 | --- | |
| 5 | # An outsider's report or offer is limited per address per hour (#14500) | |
| 6 | ||
| 7 | Anyone may file a report or send an offer with no account, so each address is counted twice: over a minute and over an hour. The address is `clientBucket` (`src/http/peer.ts`). | |
| 8 | ||
| 9 | - **The settings.** `report.per_source` (a minute) and `report.per_source_hour` (default 20) for reports; `OFFERS_PER_SOURCE` (3 a minute, a constant) and `offer.per_source_hour` (default 10) for offers. The hour settings are on `/admin/limits`, read on every request, each with its reason. | |
| 10 | - **The order.** The hour slot is taken first and given back when the forge-wide, per-repository or per-minute limit refuses instead, so a refusal never spends an address's hour. | |
| 11 | - **The refusal.** `429` with `Retry-After: <seconds>` and `{error: "rate_limited", message, hint, retryAfter}`. The message is "too many reports (offers) from this address; wait and send again"; the hint names both numbers and their settings and says when to send again. `Retry-After` is bounded (`retryAfterOf`), at least 1. | |
| 12 | - **Every door.** `POST /api/v1/repos/:owner/:name/reports` and `/offers`, the MCP `report` and `offer` tools and the terminal's offer all say the same words (`rateWords`, `src/actions/outside_limits.ts`, one source). The web form keeps its own friendlier line. Doors with no headers print them as one line: `rate_limited: <message>: <hint>`. | |
| 13 | - **Not touched.** A full queue is still `queue_full`, with its own wait. | |
| 14 | ||
| 15 | Tests: `test/reports.test.ts`, `test/offer-limits.test.ts` (both over HTTP and the MCP). |
src/actions/offer.ts +31 −6
| @@ -30,7 +30,7 @@ import { isContributionFork } from "../core/contribute.ts"; | ||
| 30 | 30 | import { issueReceipt } from "../core/offer_receipt.ts"; |
| 31 | 31 | import { type Pull } from "../core/pulls.ts"; |
| 32 | 32 | import { plainText } from "../core/reports.ts"; |
| 33 | import { RateLimiter } from "../core/ratelimit.ts"; | |
| 33 | import type { RateLimiter } from "../core/ratelimit.ts"; | |
| 34 | 34 | import { canRead, type Repo } from "../core/repos.ts"; |
| 35 | 35 | import { createUser, getUserById, type User } from "../core/users.ts"; |
| 36 | 36 | import { runGit } from "../git/env.ts"; |
| @@ -41,7 +41,9 @@ import { repoDir } from "../git/repos.ts"; | ||
| 41 | 41 | import { commitFiles } from "../git/write.ts"; |
| 42 | 42 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 43 | 43 | import { isMirror } from "../origins/write-through.ts"; |
| 44 | import { limitValue } from "../settings/limits.ts"; | |
| 44 | 45 | import { noEmail } from "./users.ts"; |
| 46 | import { hourLimiter, minuteLimiter, rateWords, useHour } from "./outside_limits.ts"; | |
| 45 | 47 | import { openPull } from "./pulls.ts"; |
| 46 | 48 | |
| 47 | 49 | // The words a stranger is told, said once: the API, the terminal and the OpenAPI contract |
| @@ -102,6 +104,10 @@ export const OFFERS_WAITING_MAX = 20; | ||
| 102 | 104 | /** The branch prefix every offer lives under; `core/contribute_token.ts` knows its shape. */ |
| 103 | 105 | const BRANCH_PREFIX = "contrib/offer"; |
| 104 | 106 | |
| 107 | /** The words a refused-for-rate offer is told, at every door. */ | |
| 108 | export const offerRate = (app: App, retryAfter: number) => | |
| 109 | rateWords(app.db, "offer", retryAfter, OFFERS_PER_SOURCE); | |
| 110 | ||
| 105 | 111 | export type OfferError = |
| 106 | 112 | | { readonly error: "not_found" | "queue_full" | "unavailable" | OfferRefusal } |
| 107 | 113 | | { readonly error: "rate_limited"; readonly retryAfter: number }; |
| @@ -121,16 +127,22 @@ export interface Offered { | ||
| 121 | 127 | |
| 122 | 128 | interface Limits { |
| 123 | 129 | readonly source: RateLimiter; |
| 130 | /** The same address counted over an hour (`offer.per_source_hour`). */ | |
| 131 | readonly hour: RateLimiter; | |
| 124 | 132 | readonly repo: RateLimiter; |
| 125 | 133 | readonly forge: RateLimiter; |
| 126 | 134 | } |
| 127 | const limiter = (perMinute: number) => new RateLimiter({ perMinute, adminPerMinute: perMinute, enabled: true }); | |
| 128 | 135 | /** One set per forge, so a test's forge never throttles another's. */ |
| 129 | 136 | const limits = new WeakMap<App, Limits>(); |
| 130 | 137 | function limitsOf(app: App): Limits { |
| 131 | 138 | let found = limits.get(app); |
| 132 | 139 | if (found === undefined) { |
| 133 | found = { source: limiter(OFFERS_PER_SOURCE), repo: limiter(PER_REPO), forge: limiter(PER_FORGE) }; | |
| 140 | found = { | |
| 141 | source: minuteLimiter(OFFERS_PER_SOURCE), | |
| 142 | hour: hourLimiter(limitValue(app.db, "offer.per_source_hour")), | |
| 143 | repo: minuteLimiter(PER_REPO), | |
| 144 | forge: minuteLimiter(PER_FORGE), | |
| 145 | }; | |
| 134 | 146 | limits.set(app, found); |
| 135 | 147 | } |
| 136 | 148 | return found; |
| @@ -246,10 +258,23 @@ export async function fileOffer( | ||
| 246 | 258 | if (!takesOffers(app, repo)) return err({ error: "not_found" }); |
| 247 | 259 | const clean = cleanOffer(raw); |
| 248 | 260 | if (!clean.ok) return err({ error: clean.error }); |
| 249 | const { forge, repo: perRepo, source: perSource } = limitsOf(app); | |
| 250 | for (const verdict of [() => forge.take("forge", "ro"), () => perRepo.take(repo.id, "ro"), () => perSource.take(source, "ro")]) { | |
| 261 | const { forge, repo: perRepo, source: perSource, hour } = limitsOf(app); | |
| 262 | // The address's hour is a setting, read on every use and taken first: a refusal later gives it | |
| 263 | // back. | |
| 264 | const perHour = limitValue(app.db, "offer.per_source_hour"); | |
| 265 | useHour(hour, perHour); | |
| 266 | const slot = hour.take(source, "ro"); | |
| 267 | if (!slot.allowed) return err({ error: "rate_limited", retryAfter: slot.retryAfter }); | |
| 268 | for (const verdict of [ | |
| 269 | () => forge.take("forge", "ro"), | |
| 270 | () => perRepo.take(repo.id, "ro"), | |
| 271 | () => perSource.take(source, "ro"), | |
| 272 | ]) { | |
| 251 | 273 | const v = verdict(); |
| 252 | if (!v.allowed) return err({ error: "rate_limited", retryAfter: v.retryAfter }); | |
| 274 | if (!v.allowed) { | |
| 275 | hour.refund(source); | |
| 276 | return err({ error: "rate_limited", retryAfter: v.retryAfter }); | |
| 277 | } | |
| 253 | 278 | } |
| 254 | 279 | if (openOffers(app, repo) >= OFFERS_WAITING_MAX) return err({ error: "queue_full" }); |
| 255 | 280 |
src/actions/outside_limits.ts added +57
| @@ -0,0 +1,57 @@ | ||
| 1 | /** | |
| 2 | * The limits on a stranger's report or offer, said once: the limiter shapes both doors share | |
| 3 | * and the words a refusal is told, so the API, the MCP and the terminal read the same sentence. | |
| 4 | * The per-address hour is a setting read on every use; a refusal later in the line gives the | |
| 5 | * slot back. | |
| 6 | */ | |
| 7 | import type { Database } from "bun:sqlite"; | |
| 8 | import { RateLimiter } from "../core/ratelimit.ts"; | |
| 9 | import { limitValue } from "../settings/limits.ts"; | |
| 10 | ||
| 11 | const HOUR_MS = 3_600_000; | |
| 12 | ||
| 13 | /** A sliding minute of `perMinute`. */ | |
| 14 | export const minuteLimiter = (perMinute: number) => | |
| 15 | new RateLimiter({ perMinute, adminPerMinute: perMinute, enabled: true }); | |
| 16 | ||
| 17 | /** A sliding hour of `perHour`. */ | |
| 18 | export const hourLimiter = (perHour: number) => | |
| 19 | new RateLimiter({ perMinute: perHour, adminPerMinute: perHour, enabled: true, windowMs: HOUR_MS }); | |
| 20 | ||
| 21 | /** Put the current setting into a limiter built by `minuteLimiter`. */ | |
| 22 | export function useMinute(limiter: RateLimiter, perMinute: number): void { | |
| 23 | limiter.setLimits({ perMinute, adminPerMinute: perMinute, enabled: true }); | |
| 24 | } | |
| 25 | ||
| 26 | /** Put the current setting into a limiter built by `hourLimiter`. */ | |
| 27 | export function useHour(limiter: RateLimiter, perHour: number): void { | |
| 28 | limiter.setLimits({ perMinute: perHour, adminPerMinute: perHour, enabled: true, windowMs: HOUR_MS }); | |
| 29 | } | |
| 30 | ||
| 31 | export type OutsideKind = "report" | "offer"; | |
| 32 | ||
| 33 | const SETTINGS = { | |
| 34 | report: { minute: "report.per_source", hour: "report.per_source_hour", plural: "reports", verb: "file" }, | |
| 35 | offer: { minute: "OFFERS_PER_SOURCE", hour: "offer.per_source_hour", plural: "offers", verb: "send" }, | |
| 36 | } as const; | |
| 37 | ||
| 38 | /** The refusal's three parts: a message, a hint naming both numbers, and the seconds to wait. */ | |
| 39 | export function rateWords( | |
| 40 | db: Database, | |
| 41 | kind: OutsideKind, | |
| 42 | retryAfter: number, | |
| 43 | perMinute: number, | |
| 44 | ): { message: string; hint: string; retryAfter: number } { | |
| 45 | const s = SETTINGS[kind]; | |
| 46 | const wait = Math.max(1, Math.ceil(Number.isFinite(retryAfter) ? retryAfter : 1)); | |
| 47 | return { | |
| 48 | message: `too many ${s.plural} from this address; wait and send again`, | |
| 49 | hint: | |
| 50 | `each address may ${s.verb} ${perMinute} ${s.plural} a minute (${s.minute}) and ` + | |
| 51 | `${limitValue(db, s.hour)} an hour (${s.hour}); send again after ${wait} seconds (the Retry-After header)`, | |
| 52 | retryAfter: wait, | |
| 53 | }; | |
| 54 | } | |
| 55 | ||
| 56 | /** The same words as one line, for a door with no headers: the MCP and the terminal. */ | |
| 57 | export const rateLine = (w: { message: string; hint: string }): string => `${w.message}: ${w.hint}`; |
src/actions/reports.ts +22 −9
| @@ -23,7 +23,8 @@ | ||
| 23 | 23 | * crew's brief says whose words an issue so marked holds. |
| 24 | 24 | */ |
| 25 | 25 | import type { App } from "../app.ts"; |
| 26 | import { RateLimiter } from "../core/ratelimit.ts"; | |
| 26 | import type { RateLimiter } from "../core/ratelimit.ts"; | |
| 27 | import { hourLimiter, minuteLimiter, rateWords, useHour, useMinute } from "./outside_limits.ts"; | |
| 27 | 28 | import { |
| 28 | 29 | OUTSIDE_COLOR, |
| 29 | 30 | OUTSIDE_LABEL, |
| @@ -58,12 +59,12 @@ const PER_FORGE = 60; | ||
| 58 | 59 | |
| 59 | 60 | interface Limits { |
| 60 | 61 | readonly source: RateLimiter; |
| 62 | /** The same address counted over an hour (`report.per_source_hour`). */ | |
| 63 | readonly hour: RateLimiter; | |
| 61 | 64 | readonly repo: RateLimiter; |
| 62 | 65 | readonly forge: RateLimiter; |
| 63 | 66 | } |
| 64 | 67 | |
| 65 | const limiter = (perMinute: number) => | |
| 66 | new RateLimiter({ perMinute, adminPerMinute: perMinute, enabled: true }); | |
| 67 | 68 | const perSourceOf = (app: App) => limitValue(app.db, "report.per_source"); |
| 68 | 69 | |
| 69 | 70 | /** One set per forge, so a test's forge never throttles another's. */ |
| @@ -72,15 +73,20 @@ function limitsOf(app: App): Limits { | ||
| 72 | 73 | let found = limits.get(app); |
| 73 | 74 | if (found === undefined) { |
| 74 | 75 | found = { |
| 75 | source: limiter(perSourceOf(app)), | |
| 76 | repo: limiter(PER_REPO), | |
| 77 | forge: limiter(PER_FORGE), | |
| 76 | source: minuteLimiter(perSourceOf(app)), | |
| 77 | hour: hourLimiter(limitValue(app.db, "report.per_source_hour")), | |
| 78 | repo: minuteLimiter(PER_REPO), | |
| 79 | forge: minuteLimiter(PER_FORGE), | |
| 78 | 80 | }; |
| 79 | 81 | limits.set(app, found); |
| 80 | 82 | } |
| 81 | 83 | return found; |
| 82 | 84 | } |
| 83 | 85 | |
| 86 | /** The words a refused-for-rate report is told, at every door. */ | |
| 87 | export const reportRate = (app: App, retryAfter: number) => | |
| 88 | rateWords(app.db, "report", retryAfter, perSourceOf(app)); | |
| 89 | ||
| 84 | 90 | export type FileRefusal = |
| 85 | 91 | | { readonly error: "not_found" | ReportError | "queue_full" | "closed" } |
| 86 | 92 | | { readonly error: "rate_limited"; readonly retryAfter: number }; |
| @@ -102,18 +108,25 @@ export function fileReport( | ||
| 102 | 108 | if (reportsClosed(app.db, repo.id)) return err({ error: "closed" }); |
| 103 | 109 | const clean = cleanReport(input); |
| 104 | 110 | if (!clean.ok) return err({ error: clean.error }); |
| 105 | const { forge, repo: perRepo, source: perSource } = limitsOf(app); | |
| 111 | const { forge, repo: perRepo, source: perSource, hour } = limitsOf(app); | |
| 106 | 112 | // The per-source number is a setting (`report.per_source`), read on every use. |
| 107 | 113 | const perMinute = perSourceOf(app); |
| 108 | perSource.setLimits({ perMinute, adminPerMinute: perMinute, enabled: true }); | |
| 114 | useMinute(perSource, perMinute); | |
| 115 | // The address's hour is a setting too, and is taken first: a refusal later gives it back. | |
| 116 | const perHour = limitValue(app.db, "report.per_source_hour"); | |
| 117 | useHour(hour, perHour); | |
| 118 | const slot = hour.take(source, "ro"); | |
| 119 | if (!slot.allowed) return err({ error: "rate_limited", retryAfter: slot.retryAfter }); | |
| 109 | 120 | for (const verdict of [ |
| 110 | 121 | () => forge.take("forge", "ro"), |
| 111 | 122 | () => perRepo.take(repo.id, "ro"), |
| 112 | 123 | () => perSource.take(source, "ro"), |
| 113 | 124 | ]) { |
| 114 | 125 | const v = verdict(); |
| 115 | if (!v.allowed) | |
| 126 | if (!v.allowed) { | |
| 127 | hour.refund(source); | |
| 116 | 128 | return err({ error: "rate_limited", retryAfter: v.retryAfter }); |
| 129 | } | |
| 117 | 130 | } |
| 118 | 131 | if (countWaiting(app.db, repo.id) >= WAITING_MAX) |
| 119 | 132 | return err({ error: "queue_full" }); |
src/http/api/reports.ts +5 −2
| @@ -12,6 +12,7 @@ import { | ||
| 12 | 12 | acceptReport, |
| 13 | 13 | dismissReport, |
| 14 | 14 | fileReport, |
| 15 | reportRate, | |
| 15 | 16 | reportViews, |
| 16 | 17 | waitingReports, |
| 17 | 18 | } from "../../actions/reports.ts"; |
| @@ -71,8 +72,10 @@ export function reportRoutes(router: Router<Handler>): void { | ||
| 71 | 72 | if (refusal.error === "not_found") return notFound(); |
| 72 | 73 | if (refusal.error === "closed") |
| 73 | 74 | return apiError("reports_closed", 403, "the people who keep this repository have closed reports from outside"); |
| 74 | if (refusal.error === "rate_limited") | |
| 75 | return tryAgain("rate_limited", 429, "too many reports; wait and send again", refusal.retryAfter); | |
| 75 | if (refusal.error === "rate_limited") { | |
| 76 | const said = reportRate(ctx.app, refusal.retryAfter); | |
| 77 | return tryAgain("rate_limited", 429, said.message, said.retryAfter, {}, { hint: said.hint }); | |
| 78 | } | |
| 76 | 79 | if (refusal.error === "queue_full") |
| 77 | 80 | return tryAgain("queue_full", 429, "this repository has many reports waiting; try again later", REPORT_QUEUE_RETRY); |
| 78 | 81 | return json({ error: refusal.error, message: reportRefusal(refusal.error), hint: REPORT_HINT }, 422); |
src/http/mcp.ts +3 −2
| @@ -40,7 +40,8 @@ import { strangerCodeSearch } from "./strangers.ts"; | ||
| 40 | 40 | import { hintForCode, retryAfterOf } from "./respond.ts"; |
| 41 | 41 | import { reportRefusal } from "../actions/report_words.ts"; |
| 42 | 42 | import { proofOf, receiptOf } from "./report_filer.ts"; |
| 43 | import { fileReport } from "../actions/reports.ts"; | |
| 43 | import { rateLine } from "../actions/outside_limits.ts"; | |
| 44 | import { fileReport, reportRate } from "../actions/reports.ts"; | |
| 44 | 45 | import { START_HINT, START_WORDS, startBuilding } from "../actions/start.ts"; |
| 45 | 46 | import { CONTRIBUTE_TOKEN_HINT, CONTRIBUTE_TOKEN_WORDS, mintContributeToken } from "../actions/contribute_token.ts"; |
| 46 | 47 | import { BODY_MAX, REQUEST_MAX, TITLE_MAX } from "../core/reports.ts"; |
| @@ -506,7 +507,7 @@ function reportAnswer(ctx: Ctx, id: unknown, args: Args): unknown { | ||
| 506 | 507 | : e.error === "closed" |
| 507 | 508 | ? "reports_closed: the people who keep this repository have closed reports from outside. Next: file nothing here; open the repository's page to reach its keepers." |
| 508 | 509 | : e.error === "rate_limited" |
| 509 | ? `rate_limited: try again in ${e.retryAfter} seconds. Next: wait, then call \`report\` again.` | |
| 510 | ? `rate_limited: ${rateLine(reportRate(ctx.app, e.retryAfter))}. Next: wait, then call \`report\` again.` | |
| 510 | 511 | : `${e.error}: ${reportRefusal(e.error)}. Plain text, a title of 1-${TITLE_MAX} characters, a body of at most ${BODY_MAX}, agent a name of letters, digits, . _ -. Next: call \`report\` again with \`repo\` and \`title\` fixed.`, |
| 511 | 512 | true, |
| 512 | 513 | ); |
src/http/mcp/offer.ts +3 −3
| @@ -5,11 +5,11 @@ | ||
| 5 | 5 | * token is treated the same: this door is for outsiders, and a private repository is |
| 6 | 6 | * `not_found` whoever asks. |
| 7 | 7 | */ |
| 8 | import { fileOffer, OFFER_BODY_MAX, OFFER_HINT, OFFER_RECEIVED, OFFER_TITLE_MAX, OFFER_WORDS, type OfferRefusal } from "../../actions/offer.ts"; | |
| 8 | import { fileOffer, OFFER_BODY_MAX, OFFER_HINT, OFFER_RECEIVED, OFFER_TITLE_MAX, OFFER_WORDS, offerRate, type OfferRefusal } from "../../actions/offer.ts"; | |
| 9 | import { rateLine } from "../../actions/outside_limits.ts"; | |
| 9 | 10 | import { getRepo } from "../../core/repos.ts"; |
| 10 | 11 | import type { Ctx } from "../context.ts"; |
| 11 | 12 | import { clientBucket } from "../peer.ts"; |
| 12 | import { retryAfterOf } from "../respond.ts"; | |
| 13 | 13 | import { hintOfText } from "./hints.ts"; |
| 14 | 14 | import { REPO_ARG, str, type Args } from "./shared.ts"; |
| 15 | 15 | |
| @@ -56,7 +56,7 @@ export async function offerAnswer(ctx: Ctx, id: unknown, args: Args): Promise<un | ||
| 56 | 56 | const e = filed.error; |
| 57 | 57 | if (e.error === "not_found") return notFound(); |
| 58 | 58 | if (e.error === "rate_limited") |
| 59 | return text(`rate_limited: too many offers; try again in ${retryAfterOf(e.retryAfter)} seconds. Next: wait, then call \`offer\` again.`, true); | |
| 59 | return text(`rate_limited: ${rateLine(offerRate(ctx.app, e.retryAfter))}. Next: wait, then call \`offer\` again.`, true); | |
| 60 | 60 | if (e.error === "queue_full") |
| 61 | 61 | return text(`queue_full: this repository has many offers waiting; try again in ${QUEUE_RETRY} seconds. Next: wait, then call \`offer\` again.`, true); |
| 62 | 62 | if (e.error === "unavailable") |
src/http/offer_routes.ts +5 −2
| @@ -10,6 +10,7 @@ import { | ||
| 10 | 10 | OFFER_REQUEST_MAX, |
| 11 | 11 | OFFER_RECEIVED, |
| 12 | 12 | OFFER_WORDS, |
| 13 | offerRate, | |
| 13 | 14 | type OfferRefusal, |
| 14 | 15 | } from "../actions/offer.ts"; |
| 15 | 16 | import { readReceipt } from "../core/offer_receipt.ts"; |
| @@ -37,8 +38,10 @@ export const offerHandler: Handler = async (ctx) => { | ||
| 37 | 38 | if (!filed.ok) { |
| 38 | 39 | const refusal = filed.error; |
| 39 | 40 | if (refusal.error === "not_found") return notFound(); |
| 40 | if (refusal.error === "rate_limited") | |
| 41 | return tryAgain("rate_limited", 429, "too many offers; wait and send again", refusal.retryAfter); | |
| 41 | if (refusal.error === "rate_limited") { | |
| 42 | const said = offerRate(ctx.app, refusal.retryAfter); | |
| 43 | return tryAgain("rate_limited", 429, said.message, said.retryAfter, {}, { hint: said.hint }); | |
| 44 | } | |
| 42 | 45 | if (refusal.error === "queue_full") |
| 43 | 46 | return tryAgain("queue_full", 429, "this repository has many offers waiting; try again later", QUEUE_RETRY); |
| 44 | 47 | if (refusal.error === "unavailable") |
src/settings/limits.ts +18
| @@ -61,6 +61,8 @@ export type LimitKey = | ||
| 61 | 61 | | "guest.cooldown_minutes" |
| 62 | 62 | | "api.anonymous_per_source" |
| 63 | 63 | | "report.per_source" |
| 64 | | "report.per_source_hour" | |
| 65 | | "offer.per_source_hour" | |
| 64 | 66 | | "model_review.auto" |
| 65 | 67 | | "model_review.min_landed" |
| 66 | 68 | | "model_review.slack_pct" |
| @@ -360,6 +362,22 @@ export const LIMITS: Record<LimitKey, LimitDef> = { | ||
| 360 | 362 | max: 1_000, |
| 361 | 363 | reason: "Anyone may file a report with no account, so one address cannot flood the people who read them; the sixth in a minute waits.", |
| 362 | 364 | }, |
| 365 | "report.per_source_hour": { | |
| 366 | label: "Reports from outside per source an hour", | |
| 367 | unit: "reports", | |
| 368 | default: 20, | |
| 369 | min: 1, | |
| 370 | max: 10_000, | |
| 371 | reason: "Open to every agent must not mean open to floods: a steady trickle from one address is still a flood by the end of an hour, and the people who read reports are the ones who pay for it.", | |
| 372 | }, | |
| 373 | "offer.per_source_hour": { | |
| 374 | label: "Offers from outside per source an hour", | |
| 375 | unit: "offers", | |
| 376 | default: 10, | |
| 377 | min: 1, | |
| 378 | max: 10_000, | |
| 379 | reason: "An offer becomes a pull a person must read, so one address may send only a few an hour; the per-minute limit stops a burst, this one stops a drip.", | |
| 380 | }, | |
| 363 | 381 | "model_review.auto": { |
| 364 | 382 | label: "Weekly model review applies a cheaper model on its own", |
| 365 | 383 | unit: "0 asks first, 1 applies", |
src/tui/screens/offer.ts +6 −4
| @@ -7,7 +7,9 @@ | ||
| 7 | 7 | * instead. |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | import { fileOffer, OFFER_RECEIVED, OFFER_WORDS, takesOffers, type OfferError } from "../../actions/offer.ts"; | |
| 10 | import { fileOffer, OFFER_RECEIVED, OFFER_WORDS, offerRate, takesOffers, type OfferError } from "../../actions/offer.ts"; | |
| 11 | import { rateLine } from "../../actions/outside_limits.ts"; | |
| 12 | import type { App } from "../../app.ts"; | |
| 11 | 13 | import { canWrite, type Repo } from "../../core/repos.ts"; |
| 12 | 14 | import type { Ctx, Screen } from "../app/app.ts"; |
| 13 | 15 | import { binding } from "../app/keys.ts"; |
| @@ -19,12 +21,12 @@ import type { Session } from "../session.ts"; | ||
| 19 | 21 | import { runAsync } from "./common.ts"; |
| 20 | 22 | |
| 21 | 23 | /** What a refused offer is told in the terminal: one line, the doors' own words. */ |
| 22 | export function offerRefusalText(e: OfferError): string { | |
| 24 | export function offerRefusalText(app: App, e: OfferError): string { | |
| 23 | 25 | switch (e.error) { |
| 24 | 26 | case "not_found": |
| 25 | 27 | return "This repository does not take offers."; |
| 26 | 28 | case "rate_limited": |
| 27 | return `Too many offers; try again in ${e.retryAfter} seconds.`; | |
| 29 | return rateLine(offerRate(app, e.retryAfter)); | |
| 28 | 30 | case "queue_full": |
| 29 | 31 | return "This repository has many offers waiting; try again later."; |
| 30 | 32 | case "unavailable": |
| @@ -61,7 +63,7 @@ export function offerScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 61 | 63 | runAsync(ctx, async () => { |
| 62 | 64 | const filed = await fileOffer(f.app, repo, { title, body: "", patch: patch.value }, source); |
| 63 | 65 | if (!filed.ok) { |
| 64 | error = offerRefusalText(filed.error); | |
| 66 | error = offerRefusalText(f.app, filed.error); | |
| 65 | 67 | ctx.toast(error, "danger"); |
| 66 | 68 | return; |
| 67 | 69 | } |
test/offer-limits.test.ts +37
| @@ -13,6 +13,7 @@ import { createRepo } from "../src/actions/repos.ts"; | ||
| 13 | 13 | import { fileOffer, OFFER_REQUEST_MAX, OFFERS_WAITING_MAX } from "../src/actions/offer.ts"; |
| 14 | 14 | import { getRepo, type Repo } from "../src/core/repos.ts"; |
| 15 | 15 | import { createUser, type User } from "../src/core/users.ts"; |
| 16 | import { setLimit } from "../src/settings/limits.ts"; | |
| 16 | 17 | import { serve, type Servers } from "../src/http/server.ts"; |
| 17 | 18 | |
| 18 | 19 | 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`; |
| @@ -92,6 +93,42 @@ describe("offer limits", () => { | ||
| 92 | 93 | expect(said.content[0].text.startsWith("queue_full: ")).toBe(true); |
| 93 | 94 | }, 60_000); |
| 94 | 95 | |
| 96 | test("offer.per_source_hour: N+1 from one address is a 429 {error, message, hint} with Retry-After; another passes", async () => { | |
| 97 | await publicRepo("hourly"); | |
| 98 | expect(setLimit(app.db, "offer.per_source_hour", 2, alice.id).ok).toBe(true); | |
| 99 | try { | |
| 100 | const send = (n: number, from: string) => | |
| 101 | fetch(`${servers.url}/api/v1/repos/alice/hourly/offers`, { | |
| 102 | method: "POST", | |
| 103 | headers: { "content-type": "application/json", "x-forwarded-for": from }, | |
| 104 | body: JSON.stringify({ title: `h${n}`, patch: patchFor(200 + n) }), | |
| 105 | }); | |
| 106 | expect((await send(0, "10.8.0.1")).status).toBe(202); | |
| 107 | expect((await send(1, "10.8.0.1")).status).toBe(202); | |
| 108 | const over = await send(2, "10.8.0.1"); | |
| 109 | expect(over.status).toBe(429); | |
| 110 | expect(Number(over.headers.get("retry-after"))).toBeGreaterThan(0); | |
| 111 | const said = (await over.json()) as any; | |
| 112 | expect(said.error).toBe("rate_limited"); | |
| 113 | expect(typeof said.message).toBe("string"); | |
| 114 | expect(said.hint).toContain("2 an hour"); | |
| 115 | expect(said.hint).toContain("OFFERS_PER_SOURCE"); | |
| 116 | // The MCP door says the same plain message, hint and seconds. | |
| 117 | const viaMcp = await fetch(`${servers.url}/mcp`, { | |
| 118 | method: "POST", | |
| 119 | headers: { "content-type": "application/json", "x-forwarded-for": "10.8.0.1" }, | |
| 120 | body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "offer", arguments: { repo: "alice/hourly", title: "m", patch: patchFor(300) } } }), | |
| 121 | }); | |
| 122 | const text = ((await viaMcp.json()) as any).result.content[0].text as string; | |
| 123 | expect(text).toStartWith("rate_limited: too many offers from this address"); | |
| 124 | expect(text).toContain("2 an hour"); | |
| 125 | expect(text).toMatch(/after \d+ seconds/); | |
| 126 | expect((await send(3, "10.8.0.2")).status).toBe(202); | |
| 127 | } finally { | |
| 128 | setLimit(app.db, "offer.per_source_hour", null, alice.id); | |
| 129 | } | |
| 130 | }); | |
| 131 | ||
| 95 | 132 | test("one repository takes 10 offers a minute from different sources, then 429 over HTTP", async () => { |
| 96 | 133 | const repo = await publicRepo("perrepo"); |
| 97 | 134 | const ten = Array.from({ length: 10 }, (_, i) => i); |
test/reports.test.ts +34
| @@ -328,6 +328,40 @@ describe("reports from outside", () => { | ||
| 328 | 328 | } |
| 329 | 329 | }); |
| 330 | 330 | |
| 331 | test("report.per_source_hour: N+1 from one address is a 429 {error, message, hint} with Retry-After; another address passes", async () => { | |
| 332 | const side = await createRepo(app, alice, { name: "hourly", visibility: "public" }); | |
| 333 | if (!side.ok) throw new Error(String(side.error)); | |
| 334 | expect(setLimit(app.db, "report.per_source_hour", 2, alice.id).ok).toBe(true); | |
| 335 | try { | |
| 336 | const source = { "x-forwarded-for": "198.51.100.201" }; | |
| 337 | const statuses: number[] = []; | |
| 338 | for (let i = 0; i < 2; i++) statuses.push((await report("alice/hourly", { title: `h${i}` }, source)).status); | |
| 339 | expect(statuses).toEqual([202, 202]); | |
| 340 | const over = await report("alice/hourly", { title: "h2" }, source); | |
| 341 | expect(over.status).toBe(429); | |
| 342 | expect(Number(over.headers.get("retry-after"))).toBeGreaterThan(60); | |
| 343 | const said = (await over.json()) as any; | |
| 344 | expect(said.error).toBe("rate_limited"); | |
| 345 | expect(typeof said.message).toBe("string"); | |
| 346 | expect(said.hint).toContain("2 an hour"); | |
| 347 | expect(said.hint).toContain("report.per_source"); | |
| 348 | // The MCP door says the same plain message, hint and seconds. | |
| 349 | const viaMcp = await fetch(`${base}/mcp`, { | |
| 350 | method: "POST", | |
| 351 | headers: { "content-type": "application/json", ...source }, | |
| 352 | body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "report", arguments: { repo: "alice/hourly", title: "m" } } }), | |
| 353 | }); | |
| 354 | const text = ((await viaMcp.json()) as any).result.content[0].text as string; | |
| 355 | expect(text).toStartWith("rate_limited: too many reports from this address"); | |
| 356 | expect(text).toContain("2 an hour"); | |
| 357 | expect(text).toMatch(/after \d+ seconds/); | |
| 358 | // Another address has its own hour. | |
| 359 | expect((await report("alice/hourly", { title: "other" }, { "x-forwarded-for": "198.51.100.202" })).status).toBe(202); | |
| 360 | } finally { | |
| 361 | setLimit(app.db, "report.per_source_hour", null, alice.id); | |
| 362 | } | |
| 363 | }); | |
| 364 | ||
| 331 | 365 | test("the owner sees reports marked from outside, escaped, and only the owner", async () => { |
| 332 | 366 | const anon = await fetch(`${base}/alice/game/reports`, { |
| 333 | 367 | redirect: "manual", |
new above
offline