Detectors & backends¶
Extension points and the layer internals.
Extension points¶
wardcat.register_action
¶
wardcat.ActionContext
dataclass
¶
ActionContext(
salt="",
tokens=TokenAllocator(),
locale="en",
surrogates=_default_surrogates(),
)
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.
locale
class-attribute
instance-attribute
¶
Locale the surrogate action draws names and formats from.
surrogates
class-attribute
instance-attribute
¶
Surrogate vault for the surrogate action, scoped to a single scan.
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_many
¶
Scan several texts; the default is one :meth:detect per text.
A detector whose model runs faster over a batch — SpaCy's nlp.pipe,
say — overrides this. The result has one span list per input, in order.
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).
source
class-attribute
instance-attribute
¶
The layer that produced the span. A detector may leave it empty; the
engine then stamps :attr:BaseDetector.layer.
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
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
scan
¶
Run all detectors, apply actions, and return the result.
Source code in src/wardcat/core/engine.py
scan_many
¶
Scan a batch: batched layers in one pass each, the rest in threads.
The result list matches texts in order and equals what :meth:scan
would return for each, apart from context ids. A detector that batches
(:meth:~wardcat.detectors.base.BaseDetector.detect_many, which SpaCy
NER implements with nlp.pipe) sees the whole batch at once; the
others — the LLM layer — run per text in a pool of workers threads.
A layer that fails is skipped for the whole batch and recorded on every
result. A text that cannot be scanned at all (too large) gets a result
with scan_error set and its text unchanged. Adjudication is not
batched: with it on, each text goes through :meth:scan.
Source code in src/wardcat/core/engine.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |
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
|