Detectors & backends¶
Extension points and the layer internals.
Extension points¶
wardcat.register_action
¶
wardcat.ActionContext
dataclass
¶
ActionContext(salt='', tokens=TokenAllocator())
Extra context an action may need beyond the span (e.g. the hash salt).
tokens
class-attribute
instance-attribute
¶
tokens = field(
default_factory=TokenAllocator,
repr=False,
compare=False,
)
Placeholder vault for reversible actions, scoped to a single scan — its
context_id is the one stamped into this scan's tokens. Excluded from
repr/equality so two contexts with the same salt still compare equal.
wardcat.TokenAllocator
¶
Hands out stable, unique placeholders for one scan — the tokenize vault.
A placeholder is [TYPE_index_contextid] — [EMAIL_1_9f3a2c8b71d4]. The
index is per entity type in order of first appearance and restarts at 1 for
every scan, so it stays short and readable; the context id is drawn once per
allocator and shared by every token it hands out, which is what makes one
scan's placeholders distinct from another's. An identical value always gets
the same token, so a name repeated three times stays one referent for whatever
reads the anonymized text.
That distinctness is the safety property behind
:meth:~wardcat.ScanResult.restore: two scans running side by side both hold
an "EMAIL number 1", and without the context id their placeholders would be
the same string — restoring one request's answer against another's result
would silently substitute the wrong person's value. With it there is nothing to
match, so the mistake becomes a reported non-substitution instead.
Allocation state is per instance and the
:class:Anonymizer <wardcat.core.anonymizer.Anonymizer> builds a fresh one for
every apply() call, so concurrent scans never share a counter or an id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context_id
|
str | None
|
the id to stamp into every token. Defaults to a fresh one;
pass .. warning:: Holds the raw values it has seen for the lifetime of the instance. |
None
|
Source code in src/wardcat/core/actions.py
token_for
¶
Return the placeholder for text, allocating a new one on first sight.
Source code in src/wardcat/core/actions.py
Detector interface¶
wardcat.detectors.base.BaseDetector
¶
Bases: ABC
Interface that all detectors implement.
The engine talks to detectors only through this interface — it never imports a concrete detector. Two optional capabilities are expressed on the base so the engine stays decoupled:
can_adjudicate— whenTruethe engine routes the other detectors' spans to this detector via thecandidatesargument (ensemble mode).detect_async— a default thread-based implementation is provided; I/O-bound detectors (e.g. an LLM backend) override it with native async.
detect
abstractmethod
¶
Scan text and return the spans found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
candidates
|
list[DetectedSpan] | None
|
spans found by the other detectors. Only meaningful for
adjudicating detectors ( |
None
|
Source code in src/wardcat/detectors/base.py
detect_async
async
¶
Async variant of :meth:detect.
Default implementation offloads the synchronous :meth:detect to a
thread. I/O-bound detectors should override this with native async I/O.
Source code in src/wardcat/detectors/base.py
wardcat.detectors.base.DetectedSpan
dataclass
¶
A single detected sensitive data span.
confidence
class-attribute
instance-attribute
¶
Detection confidence in [0.0, 1.0]. Regex/checksum detections are 1.0; NER and LLM detections are 0.85 (model-based, not fully deterministic).
LLM backends¶
The built-in backends are selected with the Backend enum (not user-extensible).
wardcat.Backend
¶
Bases: str, Enum
LLM backend types, as constants — for typo-proof selection.
Pass these to :meth:Wardcat.with_llm instead of bare strings::
from wardcat import Wardcat, Backend
Wardcat(salt="s").with_llm(backend=Backend.OPENAI_COMPATIBLE, model="...")
Each member is its string value (Backend.OLLAMA == "ollama"), so the
plain string form is still accepted.
OLLAMA
class-attribute
instance-attribute
¶
Local Ollama service (supports model download).
OPENAI_COMPATIBLE
class-attribute
instance-attribute
¶
OpenAI-compatible HTTP API — LM Studio, LocalAI, LiteLLM, …
VLLM
class-attribute
instance-attribute
¶
vLLM server (OpenAI-compatible API) — native chat, vLLM defaults.
TRANSFORMERS
class-attribute
instance-attribute
¶
In-process HuggingFace Transformers (no HTTP; loads the model locally).
Engine & anonymizer¶
wardcat.core.engine.DetectionEngine
¶
Merges spans from all detectors, resolves overlaps, applies configured actions, and returns a ScanResult.
Source code in src/wardcat/core/engine.py
scan
¶
Run all detectors, apply actions, and return the result.
Source code in src/wardcat/core/engine.py
scan_async
async
¶
Async variant — uses native async for I/O-bound detectors (LLM backend).
CPU-bound detectors (regex, NER) run via asyncio.to_thread;
the LLM detector uses its own detect_async() method with a
native httpx.AsyncClient.
Source code in src/wardcat/core/engine.py
wardcat.core.anonymizer.Anonymizer
¶
Applies configured actions to detected spans and rebuilds the text.
Source code in src/wardcat/core/anonymizer.py
apply
¶
Return (sanitized_text, violations) for spans (already filtered).
spans must be sorted/non-overlapping (the engine guarantees this).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context_id
|
str | None
|
stamped into reversible ( |
None
|