Skip to content

credencestack.forge

Forge: generate new, correctly labelled scenarios from seed scenarios and mutators, with a difficulty dial. The grader labels every generated scenario.

credencestack.forge

Forge: unlimited new, correctly labelled scenarios from a seed, with a difficulty dial.

A seed scenario plus an ordered list of mutators makes a new scenario; the pack's grader labels it with its expected outcome. The same seed always gives byte-identical output.

from credencestack import select_packs
from credencestack.forge import GenerateConfig, generate, load_registry

result = generate(GenerateConfig(select_packs(["payments"]), n=100, seed=42), load_registry())

FAMILIES module-attribute

FAMILIES: tuple[str, ...] = (
    "payments",
    "adversarial",
    "resilience",
    "thirdparty",
    "fincrime",
    "kyc",
    "docintegrity",
)

Mutator families: the built-in pack each family's mutators are written for.

ScenarioData module-attribute

ScenarioData = dict[str, Any]

A scenario as the YAML or JSON dictionary a scenario file holds.

Ambiguous

Bases: Exception

More than one outcome would be correct.

Change dataclass

Change(
    mutator: str, params: dict[str, Any] = dict[str, Any]()
)

What one mutator did: its id and content-free parameters (numbers, kinds, places).

GenerateConfig dataclass

GenerateConfig(
    packs: Sequence[Pack],
    n: int,
    seed: int,
    levels: Sequence[int] = (1, 2, 3, 4, 5),
    scenario_ids: Sequence[str] = (),
    mutator_ids: Sequence[str] = (),
)

What to generate.

mutator_ids class-attribute instance-attribute

mutator_ids: Sequence[str] = ()

Only these mutators (--mutators).

scenario_ids class-attribute instance-attribute

scenario_ids: Sequence[str] = ()

Only these seed scenarios (--from-scenario).

Generated dataclass

Generated(
    data: ScenarioData,
    pack: str,
    level: int,
    difficulty: float,
    seed_scenario: Scenario,
    applied: list[Applied],
    label: Label,
    visibility: str,
)

One generated scenario.

data instance-attribute

data: ScenarioData

The scenario, ready to write.

difficulty instance-attribute

difficulty: float

Achieved difficulty: sum of weight × intensity.

id property

id: str

G-<PACK>-<seed>-<index>.

Generation dataclass

Generation(
    scenarios: list[Generated] = list[Generated](),
    rejections: Counter[str] = Counter[str](),
    exhausted: int = 0,
)

What one generation produced.

exhausted class-attribute instance-attribute

exhausted: int = 0

Indexes given up after :data:MAX_ATTEMPTS draws.

Label dataclass

Label(
    expected: dict[str, Any],
    decisions: list[str] = list[str](),
    rule_ids: list[str] = list[str](),
    recomputed: tuple[str, ...] = (),
)

The expected block and why.

decisions class-attribute instance-attribute

decisions: list[str] = field(default_factory=list[str])

One line per decided object: INV-1042: escalate (AUTH-LIMIT-TXN).

recomputed class-attribute instance-attribute

recomputed: tuple[str, ...] = ()

The keys of expected the grader computed; the rest came from the seed.

Level

Bases: BaseModel

One position of the dial.

Mutator

Bases: ABC

Base class of every mutator.

Subclasses set the class attributes and implement :meth:applies_to, :meth:mutate and :meth:describe.

difficulty_weight class-attribute

difficulty_weight: int = 1

1 (mild) to 3 (hard).

domain class-attribute

domain: str

payments, fincrime, docintegrity or kyc.

family class-attribute

family: str

One of :data:FAMILIES.

id class-attribute

id: str

Stable id, <prefix>.<area>.<name>, for example pay.amount.limit_edge.

labeling class-attribute

labeling: Labeling = 'recompute'

How the expected outcome follows the change:

  • recompute: the grader sees the change (an amount, a time, a currency, a name, an owner, a document field) and labels the result;
  • preserve: the change cannot alter the correct outcome by construction (attack wording, placement or formatting; a transient fault the agent can retry through);
  • override: :meth:adjust_expected states the new expectation (a kill switch at step N means the agent must halt).

order class-attribute

order: int = 0

Tie-break within a labelling mode: higher applies later (the kill switch is last).

summary class-attribute

summary: str = ''

One line for mutators list.

visibility class-attribute

visibility: Visibility = 'public'

public for every shipped mutator; plugins may set confidential.

adjust_expected

adjust_expected(
    expected: dict[str, Any], change: Change
) -> dict[str, Any]

For labeling = "override": the expectation after change.

applies_to abstractmethod

applies_to(scenario: ScenarioData) -> bool

Whether this mutator can change scenario meaningfully.

apply

apply(
    scenario: ScenarioData, rng: Random, intensity: float
) -> tuple[ScenarioData, Change]

A changed copy of scenario and the change; scenario is not modified.

Raises:

Type Description
ValueError

intensity outside [0, 1].

NotApplicable

the scenario cannot take this change.

describe abstractmethod

describe(change: Change) -> str

A generic, content-free sentence about change.

mutate abstractmethod

mutate(
    scenario: ScenarioData, rng: Random, intensity: float
) -> Change

Change scenario in place and return what was done.

Raises:

Type Description
NotApplicable

the scenario cannot take this change after all.

MutatorError

MutatorError(
    errors: list[str] | str,
    *,
    hint: str | None = None,
    docs: str | None = None,
)

Bases: InputError

A mutator plugin is invalid (bad id, family, weight or visibility).

NeedsSeed

Bases: Undecidable

The seed relies on facts the authority grader does not see.

A fault, a blocked vendor, a duplicate, an amended amount or a kill switch: the seed's hand-written expectations hold only under mutators that preserve the label.

NotApplicable

Bases: Exception

The mutator cannot apply to this scenario after all; the generator picks another.

Registry dataclass

Registry(mutators: tuple[Mutator, ...])

The mutators available to one generation.

for_family

for_family(family: str) -> list[Mutator]

The mutators of one family, by id.

get

get(mutator_id: str) -> Mutator

The mutator with this id (a confidential one also by its shown id).

Raises:

Type Description
KeyError

unknown id.

Undecidable

Bases: Exception

The grader cannot decide this scenario (no profile, unknown amount, unknown items).

dial cached

dial() -> Dial

The packaged dial.

generate

generate(
    config: GenerateConfig,
    registry: Registry,
    *,
    on_scenario: Callable[[int, int], None] | None = None,
    generation_id: str | None = None,
) -> Generation

Generate config.n scenarios (fewer if some indexes are exhausted).

on_scenario(done, total) is called after each index (progress display). With telemetry on, the generation is one credencestack generate <generation_id> span.

load_registry

load_registry(paths: Iterable[str | Path] = ()) -> Registry

The shipped mutators plus every plugin under paths (directories or .py files).

Raises:

Type Description
MutatorError

a plugin is invalid, or two mutators share an id.

parse_levels

parse_levels(text: str) -> tuple[int, ...]

3 → (3,); 1-5 → (1, 2, 3, 4, 5).

Raises:

Type Description
ValueError

not a level or a range of levels.

shown_id

shown_id(m: Mutator) -> str

How a mutator's id appears in shared outputs: confidential:<hash> if confidential.