Files
causius/lib/limits.ts
CatoandClaude e3dcbda881 feat(helpers): a directory of helpers, lawyers checked and still pseudonymous (#25)
- Helpers list themselves (AI service, legal assistant, licensed lawyer) under
  a name or pseudonym; the same account keeps matters of its own. Directory
  search and filters through searchkit and listkit, with the typo suite.
- A lawyer's admission is submitted privately (evidence sealed per
  credential), checked by a staff reviewer, and published as an SD-JWT signed
  with Ed25519: the place in clear, the legal name only as a salted digest.
  Key at /.well-known/jwks.json; anyone can check at /verify. A lawyer is
  listed only where verified.
- An owner asks a helper about a matter, with or without their personal
  details; the request is previewed exactly as it will be read. Accepted, the
  helper becomes a participant; anonymisation is applied in the store, so no
  route can forget it. A lawyer hands over the checked name with proof.
  Ending it removes access in the same transaction.
- Reviewers are staff/admin accounts, granted by the Grant role workflow.
- Who may do what is lib/professions.ts, the one domain file, so Diplodoctor
  reuses the engine by changing it.
- Fixes: the account menu label template; one site URL instead of three, with
  a dev-only origin so local sign-in works.


Claude-Session: https://claude.ai/code/session_018Lem7GyhyyjKhmwjG8G7Ud

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-21 05:48:41 +02:00

38 lines
1.9 KiB
TypeScript

import { clientIp, slidingWindow, toHeaders } from "limitkit";
/**
* Rate limits, as limitkit decisions. In-memory and per process, which is the
* right size for blunting scripted abuse of a free tier on one box and the
* wrong size for billing — nothing here is billing.
*
* The free analysis is the scarce thing: it draws on a daily budget shared by
* every visitor, so one caller may not drain it. A reader on their own key
* spends their own budget and gets a much wider window.
*/
export const limits = {
analyzeFree: slidingWindow({ limit: 8, windowMs: 60 * 60_000 }),
analyzeOwn: slidingWindow({ limit: 60, windowMs: 60 * 60_000 }),
modelCheck: slidingWindow({ limit: 30, windowMs: 10 * 60_000 }),
// Keyed by actor, not IP: a signed-in person's budget follows them.
turnFree: slidingWindow({ limit: 30, windowMs: 60 * 60_000 }),
turnOwn: slidingWindow({ limit: 200, windowMs: 60 * 60_000 }),
matterCreate: slidingWindow({ limit: 20, windowMs: 60 * 60_000 }),
upload: slidingWindow({ limit: 60, windowMs: 60 * 60_000 }),
// A spoken take or a photographed page each cost a model call on the shared free tier.
transcribe: slidingWindow({ limit: 40, windowMs: 10 * 60_000 }),
pages: slidingWindow({ limit: 20, windowMs: 60 * 60_000 }),
// Profiles, credentials and requests to helpers: people writing, not a model.
helperWrite: slidingWindow({ limit: 60, windowMs: 60 * 60_000 }),
engage: slidingWindow({ limit: 20, windowMs: 60 * 60_000 }),
// Anyone may check an attestation; a script may not hammer it.
verify: slidingWindow({ limit: 60, windowMs: 10 * 60_000 }),
};
export function callerKey(request: Request, scope: string): string {
return `${scope}:${clientIp(request.headers)}`;
}
export function tooMany(result: Parameters<typeof toHeaders>[0]): Response {
return Response.json({ error: "rate_limited" }, { status: 429, headers: toHeaders(result) });
}