Skip to content

Captcha solving

TextCaptchaSolver is the Capability that reads a text captcha. Solvers report a confidence, and firstConfident chains them from cheap to capable.

Some sources put a text captcha in front of their data: an image with a few distorted letters. A Scraper for such a source requires a TextCaptchaSolver and leaves the choice of solver to whoever provides it.

Like every Capability, this keeps the Scraper honest about what it needs. It also keeps the cost decision out of the Scraper: a free local solver, a hosted model, or both in sequence.

The port ​

ts
export class TextCaptchaSolver extends Context.Service<
  TextCaptchaSolver,
  {
    readonly read: (
      captcha: TextCaptcha
    ) => Effect.Effect<TextCaptchaReadingType, CaptchaUnreadable>;
  }
>()("@scwrap/domain/TextCaptchaSolver") {}

read takes a captcha and returns a reading, or fails with CaptchaUnreadable.

TypeFields
TextCaptchaimage (bytes), mediaType (image/jpeg or image/png), optional length (how many characters), optional alphabet (which characters can occur).
TextCaptchaReadingtext, confidence from 0 to 1, solver (the name of the solver that read it).
CaptchaUnreadablereason, solver.

Tell the solver what you know. length and alphabet are optional, but a solver reads better when it knows a captcha has exactly four capital letters.

Use it in a Scraper ​

A reading is a guess. The site decides whether it was right, so the Scraper submits it and retries with a new image on a miss.

Example (Reading one captcha)

ts
const solver = yield* TextCaptchaSolver;

const reading = yield* solver.read(
  new TextCaptcha({ image, length: 4, mediaType: "image/jpeg" })
);

Wrap one attempt, fetching a new image, reading it and submitting the answer, in an Effect, and retry that Effect. Three rules:

  • Bound the attempts. Effect.retry with times gives the Run a budget. Without one, a broken solver loops forever.
  • Retry only captcha misses. Pass a while predicate that accepts a rejected or unreadable captcha and nothing else. A Blocked must not be retried twelve times.
  • End with a declared error. CaptchaUnreadable is the solver's error, not the Scraper's. Turn an exhausted budget into a Scraper's own error, such as a CaptchaUnsolved with the number of attempts, and declare it in the Definition.

Annotate each attempt's span, for example with Effect.annotateCurrentSpan("captcha.attempt", attempt), so a trace shows how many attempts a Run needed.

Solvers ​

SolverFromCostGood at
Template engine@scwrap/captcha-templatesFree, local, fastFixed-font captchas of one site, after training on its samples.
Decision modelTextCaptchaSolver.fromDecisionModelOne model call per captchaCaptchas the templates are unsure about.

TextCaptchaSolver.fromDecisionModel ​

Builds a solver from any DecisionModel, Effect's port for models that answer fixed questions with a confidence. It asks one question per character: "which character is the first, leftmost one?"

ts
const clefSolver = yield* TextCaptchaSolver.fromDecisionModel({
  prepare: upscaleCaptcha(4),
  solver: "clef",
});
OptionMeaning
solverThe name reported in readings and spans.
prepareOptional. Transforms the captcha before the model sees it, for example upscaling.

It needs a DecisionModel in its requirements and a captcha with a length. The reading's confidence is the lowest confidence among its characters.

firstConfident ​

firstConfident chains solvers. It asks each stage in turn and returns the first reading that reaches the stage's minimumConfidence.

Example (Templates first, a model when they are unsure)

ts
import { glyphModelSolver, upscaleCaptcha } from "@scwrap/captcha-templates";
import { firstConfident, TextCaptchaSolver } from "@scwrap/domain";
import { Effect, Layer } from "effect";

export const templatesThenClef = Layer.effect(
  TextCaptchaSolver,
  Effect.gen(function* templatesThenClef() {
    const templates = yield* glyphModelSolver(siteGlyphModel, "templates");

    const clef = yield* TextCaptchaSolver.fromDecisionModel({
      prepare: upscaleCaptcha(4),
      solver: "clef",
    });

    return firstConfident([
      { minimumConfidence: 0.85, solver: templates },
      { minimumConfidence: 0, solver: clef },
    ]);
  })
).pipe(Layer.provide(Clef));

siteGlyphModel is a glyph model trained for the site (Captcha templates), and Clef is the DecisionModel Layer from Workers AI.

How it decides:

  • A stage that reads with enough confidence wins, and later stages are not asked.
  • A stage that reads below its threshold is remembered as a fallback, and the next stage is asked.
  • A stage that fails is skipped.
  • When no stage is confident, the last reading below a threshold is returned. When every stage failed, the result is CaptchaUnreadable with every stage's reason.

On one site's 49 server-verified four-letter holdout captchas, the template engine read at least 95% and the Clef model 87.8%. Templates go first because they are free, and the model is asked only below a confidence of 0.85.

Tracing ​

Solvers annotate the current span with captcha.solver and captcha.confidence. A trace of a Run shows which solver read each captcha and how sure it was.

Source ​