Write your own mutator¶
The shipped mutators cover common variations. Variations of
your own (a supplier naming convention, a document layout your back office uses, a fraud
pattern you have seen) go in a plugin: a .py file with one or more Mutator subclasses,
in a directory you pass with --mutator-path (or the setting mutators.path, or
CREDENCESTACK_MUTATORS_PATH).
A worked example¶
examples/custom-mutator/vendor_suffix.py writes a vendor's legal suffix another way
("Ltd" and "Limited"). A payment to the same vendor is the same payment, so the change
cannot alter the correct outcome:
"""A worked mutator plugin: the vendor's legal suffix written another way.
Load it with ``--mutator-path examples/custom-mutator`` (or the setting
``mutators.path``). The change cannot alter the correct outcome (a payment to
"Northwind Office Supplies Ltd" is the same payment to "Northwind Office Supplies
Limited"), so it declares ``labeling = "preserve"``: the generator keeps the seed's label.
"""
from __future__ import annotations
import random
from typing import ClassVar, override
from credencestack.forge import Change, Labeling, Mutator, ScenarioData
from credencestack.forge.mutator import as_dict, as_list
SUFFIXES = {"Ltd": "Limited", "Limited": "Ltd", "Inc": "Incorporated", "Co": "Company"}
class VendorSuffix(Mutator):
"""Writes the vendor's legal suffix another way (Ltd and Limited, Inc and Incorporated)."""
id: ClassVar[str] = "example.vendor.suffix"
domain: ClassVar[str] = "payments"
family: ClassVar[str] = "payments"
difficulty_weight: ClassVar[int] = 1
labeling: ClassVar[Labeling] = "preserve"
summary: ClassVar[str] = "Writes the vendor's legal suffix another way."
@override
def applies_to(self, scenario: ScenarioData) -> bool:
vendors = as_list(as_dict(scenario.get("env_seed")).get("vendors"))
return any(
str(as_dict(v).get("name", "")).split()[-1:] in ([s] for s in SUFFIXES) for v in vendors
)
@override
def mutate(self, scenario: ScenarioData, rng: random.Random, intensity: float) -> Change:
for v in as_list(as_dict(scenario.get("env_seed")).get("vendors")):
vendor = as_dict(v)
words = str(vendor.get("name", "")).split()
if words and words[-1] in SUFFIXES:
vendor["name"] = " ".join([*words[:-1], SUFFIXES[words[-1]]])
return Change(self.id, {})
@override
def describe(self, change: Change) -> str:
return "the vendor's legal suffix is written another way"
List it and generate with it:
credencestack mutators list --mutator-path examples/custom-mutator \
--family payments
credencestack generate -p payments --n 5 --seed 3 --mutator-path examples/custom-mutator \
--mutators example.vendor.suffix --out out
│ example.vendor.suffix │ payments │ 1 │ preserve │ Writes the vendor's legal suffix │
CredenceStack <version> · generate · generate-<id>
· 5 scenario(s) from 28 seed scenario(s), difficulty 1-5, seed 3
Generated
┏━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Pack ┃ Visibility ┃ Scenarios ┃
┡━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ payments-g3 │ public │ 5 │
└─────────────┴────────────┴───────────┘
· 5 written, 2 candidate(s) redrawn (no_mutators 2)
Written
scenarios out/generate-<id>
Next credencestack run --pack-path out/generate-<id> -p payments-g3 --agent <module:function>
What a mutator declares¶
| Attribute | Meaning |
|---|---|
id |
area.kind.name, unique across every loaded mutator |
domain |
payments, fincrime, docintegrity or kyc: the seed scenarios it can change |
family |
The group it is reported under (brittleness sensitivity is reported per family) |
difficulty_weight |
1 (mild) to 3 (hard): how much it adds to a scenario's difficulty |
labeling |
How the generated scenario gets its expected outcome (below) |
summary |
One sentence for mutators list and the catalogue |
visibility |
public (the default) or confidential |
order |
Breaks ties within a labelling mode (higher runs later) |
and implements three methods:
applies_to(scenario): whether it can change this seed scenario at all;mutate(scenario, rng, intensity): the change, in place. It must be a pure function of the scenario, the random generator it is given and an intensity in [0, 1], so the same seed always gives the same scenario. It returns aChangewith the parameters it used, or raisesNotApplicable;describe(change): a sentence that names no content from the scenario ("the vendor's legal suffix is written another way").
Choosing the labelling¶
labeling |
Use it when | What the generator does |
|---|---|---|
recompute (default) |
The graders can see the change: an amount, a date, a name, an owner, a document field | Re-labels the result with the graders |
preserve |
The change cannot alter the correct outcome | Keeps the seed scenario's label |
override |
The change alters the outcome in a way you can state | Calls your adjust_expected(expected, change) |
When in doubt, use recompute: the graders decide, and a scenario they cannot label
cleanly is redrawn instead of written. preserve that is wrong produces scenarios with
the wrong expected outcome, so credencestack generate --validate re-checks every label.
Keeping a mutator confidential¶
Set visibility = "confidential" for a mutator that encodes something you do not want to
share (a detection rule, a known weakness). Its id appears only as confidential:<hash>
in shared outputs, mutators list hides its summary, every scenario it touches is
confidential, and telemetry counts it without naming it. See
confidential content.
Checks¶
credencestack mutators show <id> prints one mutator's declaration. A plugin that
declares an id twice, a bad weight or an unknown labelling mode is refused with the file
and the reason. Before you rely on a plugin, generate with it and validate:
credencestack generate -p payments --n 50 --mutator-path my_mutators --mutators my.change
credencestack generate --validate credencestack-out/latest-generate --mutator-path my_mutators