GitHub

API reference for explore.

Find races and crash bugs by running an effect many ways.

schedules runs an effect under many distinct fiber schedules (and, with interrupts, an interrupt landing at each step) and checks each run::

report = explore.schedules(transfer_twice, check=lambda ex: ...)

A failure names the schedule that replays it (replay=) and the seed that found it. activity_crashes stops a workflow at each activity boundary, resumes it, and checks it, as in "the payment ran once"::

yield from explore.activity_crashes(Order, payload, layer=..., check=...)

schedules drives every fiber itself on virtual time: work handed to threads or the network (blocking, SQL clients, HTTP calls) and programs that poll forever (a cluster's sharding loops) are not supported.

Types

TypeDefinitionDescription
Casetype Case[A, E] = tuple[Effect[A, E], Check[A, E]]

One run's effect and the check of it, made fresh for each run.

Checktype Check[A, E] = Callable[[Exit[A, E]], str | None]

Gets the run's exit; returns what went wrong, or None.

Classes

Functions

activity_crashes

activity_crashes[P, A, E, R, ER = Never, RL = Never](workflow, payload, *, layer, check, at=None, stored=None) -> Effect[CrashReport, CrashFailure | ER, RL]

Run workflow with payload once without a crash, then once per crash point: after each activity it executes ran (before the engine stored its result, and after), the run stops and is resumed. After each, check gets the workflow's exit and returns what went wrong, or None; the first problem fails with CrashFailure.

at names the crash points' activities instead (numbered from 1, in the order the run executes them), to split a long sweep into parts: the run without a crash is then not made. stored keeps only the crash points after the result was stored (True) or only those before (False).

layer provides the engine with the workflow registered (memory, or a cluster engine such as single_runner.layer on SQLite) and what check reads; it is built afresh for every run.

ParameterTypeDescription
workflowWorkflow[P, A, E]
payloadP
layerLayer[R | WorkflowEngine, ER, RL]
checkCallable[[Exit[A, E]], Effect[str | None, Never, R]]
atIterable[int] | None = None
storedbool | None = None

Returns Effect[CrashReport, CrashFailure | ER, RL]

def activity_crashes[P, A, E, R, ER = Never, RL = Never](
    workflow: Workflow[P, A, E],
    payload: P,
    *,
    layer: Layer[R | WorkflowEngine, ER, RL],
    check: Callable[[Exit[A, E]], Effect[str | None, Never, R]],
    at: Iterable[int] | None = None,
    stored: bool | None = None,
) -> Effect[CrashReport, CrashFailure | ER, RL]:
    """Run ``workflow`` with ``payload`` once without a crash, then once per
    crash point: after each activity it executes ran (before the engine
    stored its result, and after), the run stops and is resumed. After each,
    ``check`` gets the workflow's exit and returns what went wrong, or
    ``None``; the first problem fails with ``CrashFailure``.

    ``at`` names the crash points' activities instead (numbered from 1, in
    the order the run executes them), to split a long sweep into parts: the
    run without a crash is then not made. ``stored`` keeps only the crash
    points after the result was stored (``True``) or only those before
    (``False``).

    ``layer`` provides the engine with the workflow registered (memory, or
    a cluster engine such as ``single_runner.layer`` on SQLite) and what
    ``check`` reads; it is built afresh for every run."""
    require_layer(layer, "layer")

    def body() -> Generator[Effect[Any, Any, Any], Any, CrashReport]:
        executed: list[str] | None = None
        indices = at
        if indices is None:
            dry = _Crash(None, stored=False)
            problem = yield from _attempt(workflow, payload, layer, check, dry)
            if problem is not None:
                return (yield from fail(CrashFailure(None, problem)))
            executed = dry.executed
            indices = range(1, len(executed) + 1)
        points: list[CrashPoint] = []
        for index in indices:
            for after_store in (False, True) if stored is None else (stored,):
                crash = _Crash(index, after_store)
                problem = yield from _attempt(workflow, payload, layer, check, crash)
                point = crash.fired
                if point is None:
                    name = "?" if executed is None else executed[index - 1]
                    point = CrashPoint(index, name, after_store)
                    problem = f"the run executed fewer than {index} activities this time"
                points.append(point)
                if problem is not None:
                    return (yield from fail(CrashFailure(point, problem)))
        return CrashReport(tuple(points))

    return cast("Effect[CrashReport, CrashFailure | ER, RL]", gen(body))

schedules

schedules[A, E](scenario: Effect[A, E], check: Check[A, E], /, *, runs: int = 1000, seed: int = 0, interrupts: bool = False, max_ops_before_yield: int | None = None, max_steps: int = 10000, replay: str | None = None) -> Report
schedules[A, E](scenario: Callable[[], Case[A, E]], /, *, runs: int = 1000, seed: int = 0, interrupts: bool = False, max_ops_before_yield: int | None = None, max_steps: int = 10000, replay: str | None = None) -> Report

Run scenario under up to runs distinct fiber schedules and check its exit after each; raise ScheduleFailure naming the first schedule that breaks it, or that leaves a fiber never ending.

scenario is an effect needing no services, with check, or a function making a fresh (effect, check) pair for each run (for a check that reads state kept outside the effect). Half the runs go to every schedule with few deviations from first-come order, the rest to random ones from seed on. interrupts also sends the effect's fiber an interrupt, which the search lands at each step. max_ops_before_yield makes a fiber pause after that many steps, so others run between them; max_steps bounds one run. replay runs only the schedule a failure printed.

ParameterTypeDescription
scenario
check= None
runs= 1000
seed= 0
interrupts= False
max_ops_before_yield= None
max_steps= 10000
replay= None
@overload
def schedules[A, E](
    scenario: Effect[A, E],
    check: Check[A, E],
    /,
    *,
    runs: int = 1000,
    seed: int = 0,
    interrupts: bool = False,
    max_ops_before_yield: int | None = None,
    max_steps: int = 10_000,
    replay: str | None = None,
) -> Report: ...
@overload
def schedules[A, E](
    scenario: Callable[[], Case[A, E]],
    /,
    *,
    runs: int = 1000,
    seed: int = 0,
    interrupts: bool = False,
    max_ops_before_yield: int | None = None,
    max_steps: int = 10_000,
    replay: str | None = None,
) -> Report: ...
def schedules(
    scenario: Effect[Any, Any] | Callable[[], Case[Any, Any]],
    check: Check[Any, Any] | None = None,
    /,
    *,
    runs: int = 1000,
    seed: int = 0,
    interrupts: bool = False,
    max_ops_before_yield: int | None = None,
    max_steps: int = 10_000,
    replay: str | None = None,
) -> Report:
    """Run ``scenario`` under up to ``runs`` distinct fiber schedules and
    ``check`` its exit after each; raise ``ScheduleFailure`` naming the
    first schedule that breaks it, or that leaves a fiber never ending.

    ``scenario`` is an effect needing no services, with ``check``, or a
    function making a fresh ``(effect, check)`` pair for each run (for a
    check that reads state kept outside the effect). Half the runs go to
    every schedule with few deviations from first-come order, the rest to
    random ones from ``seed`` on. ``interrupts`` also sends the effect's
    fiber an interrupt, which the search lands at each step.
    ``max_ops_before_yield`` makes a fiber pause after that many steps, so
    others run between them; ``max_steps`` bounds one run. ``replay``
    runs only the schedule a failure printed."""
    _positive("runs", runs)
    _positive("max_steps", max_steps)
    if max_ops_before_yield is not None:
        _positive("max_ops_before_yield", max_ops_before_yield)
    if isinstance(scenario, Effect):
        if check is None:
            raise TypeError("explore.schedules(effect, check): the check is missing")
        effect, given = cast("Effect[Any, Any]", scenario), check

        def make() -> Case[Any, Any]:
            return effect, given

    else:
        make = scenario
    engine = Scenario(
        "explore.schedules",
        _build(make, interrupts),
        runs=runs,
        max_steps=max_steps,
        ops=max_ops_before_yield,
    )
    if replay is not None:
        return _replay(engine, replay)
    try:
        found = explore(engine, first_seed=seed)
    except InterleaveFailure as failure:
        raise _failure(failure.outcome, failure.schedule, failure.how, failure.seed) from None
    return Report(found.runs, found.distinct, found.exhaustive)