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

opened this

Check · Review · Land · Live — Changes were asked on this head. Next: builder, changes were asked on this head.

A reviewer asked for changes Review

scout/forge:scout/14500-an-outsider-s-report-or-offer-is-rate-li → main 5 commits fork

Review

Sign in to review this change.
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";
3030 import { issueReceipt } from "../core/offer_receipt.ts";
3131 import { type Pull } from "../core/pulls.ts";
3232 import { plainText } from "../core/reports.ts";
33 import { RateLimiter } from "../core/ratelimit.ts";
33 import type { RateLimiter } from "../core/ratelimit.ts";
3434 import { canRead, type Repo } from "../core/repos.ts";
3535 import { createUser, getUserById, type User } from "../core/users.ts";
3636 import { runGit } from "../git/env.ts";
@@ -41,7 +41,9 @@ import { repoDir } from "../git/repos.ts";
4141 import { commitFiles } from "../git/write.ts";
4242 import { err, ok, type Result } from "../kernel/result.ts";
4343 import { isMirror } from "../origins/write-through.ts";
44 import { limitValue } from "../settings/limits.ts";
4445 import { noEmail } from "./users.ts";
46 import { hourLimiter, minuteLimiter, rateWords, useHour } from "./outside_limits.ts";
4547 import { openPull } from "./pulls.ts";
4648
4749 // 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;
102104 /** The branch prefix every offer lives under; `core/contribute_token.ts` knows its shape. */
103105 const BRANCH_PREFIX = "contrib/offer";
104106
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
105111 export type OfferError =
106112 | { readonly error: "not_found" | "queue_full" | "unavailable" | OfferRefusal }
107113 | { readonly error: "rate_limited"; readonly retryAfter: number };
@@ -121,16 +127,22 @@ export interface Offered {
121127
122128 interface Limits {
123129 readonly source: RateLimiter;
130 /** The same address counted over an hour (`offer.per_source_hour`). */
131 readonly hour: RateLimiter;
124132 readonly repo: RateLimiter;
125133 readonly forge: RateLimiter;
126134 }
127 const limiter = (perMinute: number) => new RateLimiter({ perMinute, adminPerMinute: perMinute, enabled: true });
128135 /** One set per forge, so a test's forge never throttles another's. */
129136 const limits = new WeakMap<App, Limits>();
130137 function limitsOf(app: App): Limits {
131138 let found = limits.get(app);
132139 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 };
134146 limits.set(app, found);
135147 }
136148 return found;
@@ -246,10 +258,23 @@ export async function fileOffer(
246258 if (!takesOffers(app, repo)) return err({ error: "not_found" });
247259 const clean = cleanOffer(raw);
248260 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 ]) {
251273 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 }
253278 }
254279 if (openOffers(app, repo) >= OFFERS_WAITING_MAX) return err({ error: "queue_full" });
255280
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 @@
2323 * crew's brief says whose words an issue so marked holds.
2424 */
2525 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";
2728 import {
2829 OUTSIDE_COLOR,
2930 OUTSIDE_LABEL,
@@ -58,12 +59,12 @@ const PER_FORGE = 60;
5859
5960 interface Limits {
6061 readonly source: RateLimiter;
62 /** The same address counted over an hour (`report.per_source_hour`). */
63 readonly hour: RateLimiter;
6164 readonly repo: RateLimiter;
6265 readonly forge: RateLimiter;
6366 }
6467
65 const limiter = (perMinute: number) =>
66 new RateLimiter({ perMinute, adminPerMinute: perMinute, enabled: true });
6768 const perSourceOf = (app: App) => limitValue(app.db, "report.per_source");
6869
6970 /** One set per forge, so a test's forge never throttles another's. */
@@ -72,15 +73,20 @@ function limitsOf(app: App): Limits {
7273 let found = limits.get(app);
7374 if (found === undefined) {
7475 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),
7880 };
7981 limits.set(app, found);
8082 }
8183 return found;
8284 }
8385
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
8490 export type FileRefusal =
8591 | { readonly error: "not_found" | ReportError | "queue_full" | "closed" }
8692 | { readonly error: "rate_limited"; readonly retryAfter: number };
@@ -102,18 +108,25 @@ export function fileReport(
102108 if (reportsClosed(app.db, repo.id)) return err({ error: "closed" });
103109 const clean = cleanReport(input);
104110 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);
106112 // The per-source number is a setting (`report.per_source`), read on every use.
107113 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 });
109120 for (const verdict of [
110121 () => forge.take("forge", "ro"),
111122 () => perRepo.take(repo.id, "ro"),
112123 () => perSource.take(source, "ro"),
113124 ]) {
114125 const v = verdict();
115 if (!v.allowed)
126 if (!v.allowed) {
127 hour.refund(source);
116128 return err({ error: "rate_limited", retryAfter: v.retryAfter });
129 }
117130 }
118131 if (countWaiting(app.db, repo.id) >= WAITING_MAX)
119132 return err({ error: "queue_full" });
src/http/api/reports.ts +5 −2
@@ -12,6 +12,7 @@ import {
1212 acceptReport,
1313 dismissReport,
1414 fileReport,
15 reportRate,
1516 reportViews,
1617 waitingReports,
1718 } from "../../actions/reports.ts";
@@ -71,8 +72,10 @@ export function reportRoutes(router: Router<Handler>): void {
7172 if (refusal.error === "not_found") return notFound();
7273 if (refusal.error === "closed")
7374 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 }
7679 if (refusal.error === "queue_full")
7780 return tryAgain("queue_full", 429, "this repository has many reports waiting; try again later", REPORT_QUEUE_RETRY);
7881 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";
4040 import { hintForCode, retryAfterOf } from "./respond.ts";
4141 import { reportRefusal } from "../actions/report_words.ts";
4242 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";
4445 import { START_HINT, START_WORDS, startBuilding } from "../actions/start.ts";
4546 import { CONTRIBUTE_TOKEN_HINT, CONTRIBUTE_TOKEN_WORDS, mintContributeToken } from "../actions/contribute_token.ts";
4647 import { BODY_MAX, REQUEST_MAX, TITLE_MAX } from "../core/reports.ts";
@@ -506,7 +507,7 @@ function reportAnswer(ctx: Ctx, id: unknown, args: Args): unknown {
506507 : e.error === "closed"
507508 ? "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."
508509 : 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.`
510511 : `${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.`,
511512 true,
512513 );
src/http/mcp/offer.ts +3 −3
@@ -5,11 +5,11 @@
55 * token is treated the same: this door is for outsiders, and a private repository is
66 * `not_found` whoever asks.
77 */
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";
910 import { getRepo } from "../../core/repos.ts";
1011 import type { Ctx } from "../context.ts";
1112 import { clientBucket } from "../peer.ts";
12 import { retryAfterOf } from "../respond.ts";
1313 import { hintOfText } from "./hints.ts";
1414 import { REPO_ARG, str, type Args } from "./shared.ts";
1515
@@ -56,7 +56,7 @@ export async function offerAnswer(ctx: Ctx, id: unknown, args: Args): Promise<un
5656 const e = filed.error;
5757 if (e.error === "not_found") return notFound();
5858 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);
6060 if (e.error === "queue_full")
6161 return text(`queue_full: this repository has many offers waiting; try again in ${QUEUE_RETRY} seconds. Next: wait, then call \`offer\` again.`, true);
6262 if (e.error === "unavailable")
src/http/offer_routes.ts +5 −2
@@ -10,6 +10,7 @@ import {
1010 OFFER_REQUEST_MAX,
1111 OFFER_RECEIVED,
1212 OFFER_WORDS,
13 offerRate,
1314 type OfferRefusal,
1415 } from "../actions/offer.ts";
1516 import { readReceipt } from "../core/offer_receipt.ts";
@@ -37,8 +38,10 @@ export const offerHandler: Handler = async (ctx) => {
3738 if (!filed.ok) {
3839 const refusal = filed.error;
3940 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 }
4245 if (refusal.error === "queue_full")
4346 return tryAgain("queue_full", 429, "this repository has many offers waiting; try again later", QUEUE_RETRY);
4447 if (refusal.error === "unavailable")
src/settings/limits.ts +18
@@ -61,6 +61,8 @@ export type LimitKey =
6161 | "guest.cooldown_minutes"
6262 | "api.anonymous_per_source"
6363 | "report.per_source"
64 | "report.per_source_hour"
65 | "offer.per_source_hour"
6466 | "model_review.auto"
6567 | "model_review.min_landed"
6668 | "model_review.slack_pct"
@@ -360,6 +362,22 @@ export const LIMITS: Record<LimitKey, LimitDef> = {
360362 max: 1_000,
361363 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.",
362364 },
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 },
363381 "model_review.auto": {
364382 label: "Weekly model review applies a cheaper model on its own",
365383 unit: "0 asks first, 1 applies",
src/tui/screens/offer.ts +6 −4
@@ -7,7 +7,9 @@
77 * instead.
88 */
99
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";
1113 import { canWrite, type Repo } from "../../core/repos.ts";
1214 import type { Ctx, Screen } from "../app/app.ts";
1315 import { binding } from "../app/keys.ts";
@@ -19,12 +21,12 @@ import type { Session } from "../session.ts";
1921 import { runAsync } from "./common.ts";
2022
2123 /** 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 {
2325 switch (e.error) {
2426 case "not_found":
2527 return "This repository does not take offers.";
2628 case "rate_limited":
27 return `Too many offers; try again in ${e.retryAfter} seconds.`;
29 return rateLine(offerRate(app, e.retryAfter));
2830 case "queue_full":
2931 return "This repository has many offers waiting; try again later.";
3032 case "unavailable":
@@ -61,7 +63,7 @@ export function offerScreen(f: Session, ctx: Ctx, repo: Repo): Screen {
6163 runAsync(ctx, async () => {
6264 const filed = await fileOffer(f.app, repo, { title, body: "", patch: patch.value }, source);
6365 if (!filed.ok) {
64 error = offerRefusalText(filed.error);
66 error = offerRefusalText(f.app, filed.error);
6567 ctx.toast(error, "danger");
6668 return;
6769 }
test/offer-limits.test.ts +37
@@ -13,6 +13,7 @@ import { createRepo } from "../src/actions/repos.ts";
1313 import { fileOffer, OFFER_REQUEST_MAX, OFFERS_WAITING_MAX } from "../src/actions/offer.ts";
1414 import { getRepo, type Repo } from "../src/core/repos.ts";
1515 import { createUser, type User } from "../src/core/users.ts";
16 import { setLimit } from "../src/settings/limits.ts";
1617 import { serve, type Servers } from "../src/http/server.ts";
1718
1819 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", () => {
9293 expect(said.content[0].text.startsWith("queue_full: ")).toBe(true);
9394 }, 60_000);
9495
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
95132 test("one repository takes 10 offers a minute from different sources, then 429 over HTTP", async () => {
96133 const repo = await publicRepo("perrepo");
97134 const ten = Array.from({ length: 10 }, (_, i) => i);
test/reports.test.ts +34
@@ -328,6 +328,40 @@ describe("reports from outside", () => {
328328 }
329329 });
330330
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
331365 test("the owner sees reports marked from outside, escaped, and only the owner", async () => {
332366 const anon = await fetch(`${base}/alice/game/reports`, {
333367 redirect: "manual",

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs