Moderation

c.var.moderator is always there.

Text one person writes and other people see — a handle, a level title, a report reason — gets checked before it is shown. Pithy has one seam for that check, and core puts it on every request, with or without config, like c.var.t. So a route checks text with no null check and no setup, and you change what does the checking in config without touching a call site.

The request context lists every seam beside it, pithy.config.ts is where its moderation block goes, and errors is how a route refuses.

One question, three answers

const verdict = await c.var.moderator.check(text, context);
// { verdict: "allow" }
// { verdict: "reject", reason: "blocklist" }
// { verdict: "review", reason: "moderator_timeout" }

context says what the text is, so a moderator can hold one kind to a stricter bar than another.

contextWhat it is
handleA name people are known by
titleA name given to something they made
free_textAnything longer they wrote

You decide what each verdict means. reject is a refusal. review asks a person to look, and whether the text waits for that look or shows in the meantime is your call. The verdict schema is ModerationVerdict and the interface is TextModerator, both from @pithy-sh/core/src/moderation/moderator.

Checking a title in a route

Your app’s routes load on first use, so the route lives in a module of its own, and the app capability declares it with a loader for that module. Add a route has the shape:

// apps/api/pithy.config.ts
const app = defineCapability({
  name: "app",
  requiredBindings: [],
  routes: {
    declared: [{ method: "POST", path: "/levels" }],
    load: async () => (await import("./src/levels")).mountLevels,
  },
});
// apps/api/src/levels.ts
import { requireAuth } from "@pithy-sh/auth/src/http/middleware";
import { ValidationError } from "@pithy-sh/core/src/error/pithyError";
import type { RouteMount } from "@pithy-sh/core/src/http/lazyRoutes";
import { describeRoute } from "@pithy-sh/core/src/http/routeSpec";
import { validate } from "@pithy-sh/core/src/http/validation";
import { z } from "zod";

const CreateLevel = z
  .object({ title: z.string().min(1).max(60).describe("The level's title, shown in the level browser.") })
  .describe("A level to publish.");

const LevelCreated = z
  .object({ held: z.boolean().describe("Whether the level waits for a person to look before it is listed.") })
  .describe("The level was saved.");

export const mountLevels: RouteMount = (a) => {
  a.post(
    "/levels",
    describeRoute({
      summary: "Publish a level.",
      verification: ["bearer", "session"],
      responses: { 201: LevelCreated },
    }),
    requireAuth(),
    validate("json", CreateLevel),
    async (c) => {
      const { title } = c.req.valid("json");
      const verdict = await c.var.moderator.check(title, "title");
      if (verdict.verdict === "reject") throw new ValidationError({ message: "Pick another title." });
      const held = verdict.verdict === "review";
      // Save the level with `held`, and list only what is not held.
      return c.json({ held } satisfies z.output<typeof LevelCreated>, 201);
    },
  );
};

The refusal names no word. Telling someone which word tripped the list teaches them the next spelling to try.

What the default checks

With no config, the moderator checks a blocklist the kit ships: English slurs, sexual terms and profanity. The words come from the English dataset of obscenity, MIT, with its notice kept in packages/core/src/moderation/shippedList.ts.

Both the text and the list are folded before they are compared, so a spelling trick reads as the word it imitates:

  • Case and width. Upper case, full-width and mathematical letters are one word with the plain one.
  • Accents. Accents and stacked combining marks are dropped.
  • Look-alikes. A character that stands in for a letter folds onto it: $ for s, 1 for i, a Cyrillic ѕ or һ, a Greek τ, small capitals such as ᴛ. A capital folds by its own shape, so a Greek Ν is an N. l, I and 1 are one letter.
  • Numbers stay numbers. A word made only of digits is never read as letters, so Level 455 and dave717 are allowed. A digit stands in for a letter only in a word that has a letter in it.
  • Invisible characters. A zero-width space inside a word is removed, so it hides nothing.

Then each word is matched whole. A listed word inside a longer one is allowed, and so are its letters split across two words — which is why a short listed word does not reject class or as sorted. A name is also read as its parts (BigCrate as big crate, crate99 as crate 99), and letters spelled out one at a time as one word (c r a t e, c.r.4.t.e). Any reading that matches rejects.

The default answers only allow or reject. review is for a classifier that can be unsure, and for the seam when a moderator fails.

Extending or replacing the list

The moderation block of a Worker’s apps/<name>/pithy.config.ts, beside capabilities and app:

const config = {
  capabilities: [ … ],
  app,
  moderation: {
    words: ["griefer", "speed run*", "cheat code"],
  },
};
Key
wordsAdds to the shipped list. A word ending in * matches every word that starts with it. Several words match in order, or run together
includeShippedfalse drops the shipped list, so words is the whole list
moderatorYour own moderator, in place of the default
timeoutMsHow long the moderator has to answer, in milliseconds. Defaults to 3000, at most 30000

The block is parsed when the Worker assembles, so one that does not parse refuses to deploy. So does a moderator beside words or includeShipped, which it would never read.

Your own moderator

Anything with an async check(text, context) that answers a verdict — a Workers AI classifier, a third-party API. To keep the list and add a classifier, put blocklistModerator inside yours:

import { blocklistModerator } from "@pithy-sh/core/src/moderation/blocklist";
import type { TextModerator } from "@pithy-sh/core/src/moderation/moderator";
import { SHIPPED_BLOCKLIST } from "@pithy-sh/core/src/moderation/shippedList";

const list = blocklistModerator({ words: [...SHIPPED_BLOCKLIST, "griefer"] });

const moderator: TextModerator = {
  async check(text, context) {
    const listed = await list.check(text, context);
    if (listed.verdict !== "allow") return listed;
    const score = await classify(text); // your Workers AI or third-party call
    if (score > 0.9) return { verdict: "reject", reason: "classifier" };
    if (score > 0.5 || (context === "handle" && score > 0.3)) return { verdict: "review", reason: "classifier_unsure" };
    return { verdict: "allow" };
  },
};

Then pass it as moderation: { moderator }.

A failure answers review, never allow

The seam never lets text through for a moderator it could not hear from. It wraps whatever moderator you configure:

What happenedThe verdict
It threw, its promise rejected, or it answered something that is not a verdictreview, with moderator_failed
No answer within timeoutMsreview, with moderator_timeout

Each is logged as a warning with the context and never the text, because the text is a person’s own words. A classifier’s error message can quote what it was handed, so only the error’s name is logged.

What it does not do

One kit capability calls it so far: auth’s public handles. Under auth({ handles: true }), every handle a player sets is checked as a handle: reject refuses it with auth/handle_rejected, and review leaves the handle as it was and answers auth/handle_in_review. Public handles has the rest. The other text the kit’s capabilities store is not checked through it today, so for that, and for your own routes, the seam is yours to call.

It checks text, not images. Media describes images and does not judge them.

The shipped list is English, and it is a list. It catches the words it holds and their disguises, not meaning. For anything subtler, plug in a classifier and keep the list inside it.

ESC