flowsemantics.execution package

Submodules

flowsemantics.execution.clock module

flowsemantics.execution.clock - Injected wall and monotonic clocks.

Layer: Domain Module: flowsemantics.execution.clock Since: 0.1.0 Implements: SPEC.md section 6.2 (injected clocks), section 6.3 (default clock) Depends on: stdlib only (datetime, time) Used by: FlowContext, FlowOutcome timestamps, finalization budgets See also: SPEC.md section 6

Notes

  • Every time read in Core routes through a Clock. Ambient clock APIs are banned in Core by audit.py; SystemClock is the single sanctioned reader, permitted by SPEC.md section 6.3 and clearly replaceable via with_clock.

  • SystemClock reads the host clocks through the datetime and time modules. It is the documented default; deterministic replay requires FakeClock.

class flowsemantics.execution.clock.Clock(*args, **kwargs)[source]

Bases: Protocol

Two-concept clock: wall time for timestamps, monotonic for durations.

SPEC.md section 6.2 makes the wall/monotonic distinction normative.

wall() → datetime[source]

Return the current wall-clock time as a timezone-aware datetime.

monotonic() → float[source]

Return the current monotonic time in seconds.

__init__(*args, **kwargs)
class flowsemantics.execution.clock.FakeClock(wall: datetime | None = None, monotonic: float = 0.0)[source]

Bases: object

Deterministic clock for tests, replay, and conformance vectors.

Wall and monotonic values advance only when advance() or advance_wall() is called. Two FakeClocks constructed with the same values behave identically, which is what SPEC.md section 6.1 determinism requires.

__init__(wall: datetime | None = None, monotonic: float = 0.0) → None[source]

Create a FakeClock at the given wall time and monotonic offset.

wall() → datetime[source]

Return the current fake wall-clock time.

monotonic() → float[source]

Return the current fake monotonic time in seconds.

advance(seconds: float) → None[source]

Advance both wall and monotonic time by the same duration.

advance_wall(seconds: float) → None[source]

Advance only the wall clock (monotonic time is unaffected).

set_wall(value: datetime) → None[source]

Set the wall clock to an absolute value.

class flowsemantics.execution.clock.SystemClock[source]

Bases: object

Default clock reading the host’s real clocks (SPEC.md section 6.3).

This is the only Core component permitted to read ambient time. It is replaceable: pass any Clock implementation via Flow.with_clock() or the execute() clock parameter. Deterministic replay and tests MUST use FakeClock instead.

wall() → datetime[source]

Return the host wall-clock time in UTC.

monotonic() → float[source]

Return the host monotonic clock in seconds.

flowsemantics.execution.context module

flowsemantics.execution.context - Per-invocation execution context.

Layer: Domain Module: flowsemantics.execution.context Since: 0.1.0 Implements: SPEC.md section 4.1.3 (isolated execution context), section 6.2 Depends on: flowsemantics.execution.clock Used by: FlowContract.execute / execute_async See also: SPEC.md section 4.2 (qualified thread safety)

Notes

  • A fresh FlowContext is created per invocation. No mutable state is shared between invocations of the same contract (SPEC.md section 4.1.3).

  • The context carries the input, the injected clock, and (from Phase 2.3) the cancellation token. User callbacks receive the input, not the whole context, so they cannot accidentally mutate shared state.

class flowsemantics.execution.context.FlowContext(input: Any, clock: Clock = <factory>, state: dict[str, ~typing.Any]=<factory>)[source]

Bases: object

Isolated per-invocation state for one flow execution.

input

The input value passed to execute().

Type:

Any

clock

The injected clock. Defaults to SystemClock when the caller does not supply one; deterministic replay MUST inject FakeClock.

Type:

flowsemantics.execution.clock.Clock

state

Per-invocation mutable state for engine bookkeeping. Never shared between invocations.

Type:

dict[str, Any]

input: Any
clock: Clock
state: dict[str, Any]
__init__(input: Any, clock: Clock = <factory>, state: dict[str, ~typing.Any]=<factory>) → None

flowsemantics.execution.outcome module

flowsemantics.execution.outcome - Canonical outcome model (SPEC.md section 3).

Layer: Domain Module: flowsemantics.execution.outcome Since: 0.1.0 Implements: SPEC.md sections 3.1-3.8 (corrected v5 outcome model) Depends on: stdlib only (dataclasses, enum, datetime) Used by: execution engine, trace, simulation, conformance runner See also: SPEC.md section 3, section 7

Notes

  • The v5 corrections are encoded here: interruption is separate from recovery (B5), is_clean is finalization metadata not a status (B6), secondary_exceptions includes cleanup exceptions even when the primary succeeded (B7).

  • All dataclasses are frozen: a FlowOutcome MUST be immutable after construction (SPEC.md section 3.6).

class flowsemantics.execution.outcome.DecisionStatus(value)[source]

Bases: StrEnum

What the flow concluded. Exactly three members (SPEC 3.2).

APPROVED = 'approved'
REJECTED = 'rejected'
NO_MATCH = 'no_match'
class flowsemantics.execution.outcome.Effect(name: str, payload: Any = None)[source]

Bases: object

A proposed side effect. Data, not code (SPEC 3.7).

name: str
payload: Any = None
__init__(name: str, payload: Any = None) → None
class flowsemantics.execution.outcome.ExecutionStatus(value)[source]

Bases: StrEnum

What happened while running. Exactly four members (SPEC 3.1).

COMPLETED = 'completed'
FAILED = 'failed'
CANCELLED = 'cancelled'
TIMED_OUT = 'timed_out'
class flowsemantics.execution.outcome.FinalizationInfo(is_clean: bool, cleanup_exceptions: tuple[BaseException, ...] = (), cleanup_budget_used: timedelta = datetime.timedelta(0), cleanup_budget_exceeded: bool = False)[source]

Bases: object

Finalization metadata including is_clean (SPEC 3.5, B6, B8).

is_clean: bool
cleanup_exceptions: tuple[BaseException, ...] = ()
cleanup_budget_used: timedelta = datetime.timedelta(0)
cleanup_budget_exceeded: bool = False
__init__(is_clean: bool, cleanup_exceptions: tuple[BaseException, ...] = (), cleanup_budget_used: timedelta = datetime.timedelta(0), cleanup_budget_exceeded: bool = False) → None
class flowsemantics.execution.outcome.FlowOutcome(execution_status: ExecutionStatus, decision_status: DecisionStatus | None = None, value: Any = None, recovery: RecoveryInfo | None = None, interruption: InterruptionInfo | None = None, finalization: FinalizationInfo | None = None, primary_exception: BaseException | None = None, secondary_exceptions: tuple[BaseException, ...] = (), effects: tuple[Effect, ...] = (), trace: ExecutionTrace | None = None)[source]

Bases: object

The structured result of one execution (SPEC 3.6).

Invariants enforced by construction in the engine:
  • decision_status is absent unless execution_status is COMPLETED.

  • primary_exception is absent unless execution_status is FAILED.

  • interruption is populated only for CANCELLED and TIMED_OUT.

  • recovery is populated only when an exception was recovered.

  • secondary_exceptions includes cleanup exceptions even when the primary execution succeeded (B7).

execution_status: ExecutionStatus
decision_status: DecisionStatus | None = None
value: Any = None
recovery: RecoveryInfo | None = None
interruption: InterruptionInfo | None = None
finalization: FinalizationInfo | None = None
primary_exception: BaseException | None = None
secondary_exceptions: tuple[BaseException, ...] = ()
effects: tuple[Effect, ...] = ()
trace: ExecutionTrace | None = None
__init__(execution_status: ExecutionStatus, decision_status: DecisionStatus | None = None, value: Any = None, recovery: RecoveryInfo | None = None, interruption: InterruptionInfo | None = None, finalization: FinalizationInfo | None = None, primary_exception: BaseException | None = None, secondary_exceptions: tuple[BaseException, ...] = (), effects: tuple[Effect, ...] = (), trace: ExecutionTrace | None = None) → None
class flowsemantics.execution.outcome.InterruptionInfo(kind: Literal['cancelled', 'timed_out'], source: str, timestamp: datetime, deadline: datetime | None = None)[source]

Bases: object

Cancellation/timeout metadata. Never an exception (SPEC 3.3, B5).

kind: Literal['cancelled', 'timed_out']
source: str
timestamp: datetime
deadline: datetime | None = None
__init__(kind: Literal['cancelled', 'timed_out'], source: str, timestamp: datetime, deadline: datetime | None = None) → None
class flowsemantics.execution.outcome.RecoveryInfo(original_exception: BaseException, handler_rule_id: str, recovered_at: datetime)[source]

Bases: object

Exception-recovery metadata only (SPEC 3.4, O1).

original_exception: BaseException
handler_rule_id: str
recovered_at: datetime
__init__(original_exception: BaseException, handler_rule_id: str, recovered_at: datetime) → None

flowsemantics.execution.trace module

flowsemantics.execution.trace - ExecutionTrace, TraceEvent, TraceRecorder.

Layer: Domain Module: flowsemantics.execution.trace Since: 0.3.0 Implements: SPEC.md sections 3.8, 12.1, 12.3 Depends on: stdlib only Used by: FlowContract.try_execute (when tracing enabled), explain() See also: SPEC.md sections 3.8, 12.1-12.4

Notes

  • The event-type vocabulary is closed (SPEC 12.1): exactly the 22 types listed there. TraceRecorder.emit validates against it.

  • reason values are a closed enumeration (SPEC 12.1.1).

  • Tracing is zero-cost when disabled: no recorder is created and no clock reads happen (SPEC 12.3).

  • Trace events MUST NOT contain input values, secrets, or exception messages (SPEC 12.2).

class flowsemantics.execution.trace.ExecutionTrace(contract_version: str, events: tuple[~flowsemantics.execution.trace.TraceEvent, ...]=<factory>)[source]

Bases: object

Ordered trace events plus contract version (SPEC 3.8).

contract_version: str
events: tuple[TraceEvent, ...]
__init__(contract_version: str, events: tuple[~flowsemantics.execution.trace.TraceEvent, ...]=<factory>) → None
class flowsemantics.execution.trace.TraceEvent(type: str, rule_id: str | None = None, timestamp: datetime | None = None, reason: str | None = None, contract_version: str | None = None, diagnostics: dict[str, Any] | None = None)[source]

Bases: object

A single trace event (SPEC 3.8).

type: str
rule_id: str | None = None
timestamp: datetime | None = None
reason: str | None = None
contract_version: str | None = None
diagnostics: dict[str, Any] | None = None
__init__(type: str, rule_id: str | None = None, timestamp: datetime | None = None, reason: str | None = None, contract_version: str | None = None, diagnostics: dict[str, Any] | None = None) → None
class flowsemantics.execution.trace.TraceRecorder(contract_version: str, diagnostic_hook: Callable[[TraceEvent], dict[str, Any] | None] | None = None)[source]

Bases: object

Collects trace events during one execution (SPEC 12.1, 12.3).

Created only when tracing is enabled; when disabled, no recorder exists and evaluation pays zero tracing cost.

__init__(contract_version: str, diagnostic_hook: Callable[[TraceEvent], dict[str, Any] | None] | None = None) → None[source]

Create an empty recorder for the given contract version.

diagnostic_hook is the application-supplied redaction-aware hook (SPEC 12.2): it receives each event and may return a dict of additional diagnostic fields. The library defers entirely to the application’s policy; absent a hook, no diagnostic data is added.

emit(event_type: str, *, rule_id: str | None = None, timestamp: datetime | None = None, reason: str | None = None) → None[source]

Append an event. Validates type and reason against the closed sets.

build() → ExecutionTrace[source]

Produce the immutable ExecutionTrace.

Module contents

flowsemantics.execution - Outcome model, context, clocks, executor.

Layer: Domain Module: flowsemantics.execution Since: 0.0.0 Implements: SPEC.md sections 3, 4.1.3, 6.2, 7 Depends on: flowsemantics.execution.{clock,context,outcome} Used by: parent package; populated from Phase 1.0 See also: SPEC.md sections 3, 6, 7

Notes

  • __all__ grows as features are implemented. Currently exports the canonical outcome types (SPEC.md section 3) and the clock/context primitives (sections 4.1.3, 6.2).

class flowsemantics.execution.Clock(*args, **kwargs)[source]

Bases: Protocol

Two-concept clock: wall time for timestamps, monotonic for durations.

SPEC.md section 6.2 makes the wall/monotonic distinction normative.

wall() → datetime[source]

Return the current wall-clock time as a timezone-aware datetime.

monotonic() → float[source]

Return the current monotonic time in seconds.

__init__(*args, **kwargs)
class flowsemantics.execution.DecisionStatus(value)[source]

Bases: StrEnum

What the flow concluded. Exactly three members (SPEC 3.2).

APPROVED = 'approved'
REJECTED = 'rejected'
NO_MATCH = 'no_match'
class flowsemantics.execution.Effect(name: str, payload: Any = None)[source]

Bases: object

A proposed side effect. Data, not code (SPEC 3.7).

name: str
payload: Any = None
__init__(name: str, payload: Any = None) → None
class flowsemantics.execution.ExecutionStatus(value)[source]

Bases: StrEnum

What happened while running. Exactly four members (SPEC 3.1).

COMPLETED = 'completed'
FAILED = 'failed'
CANCELLED = 'cancelled'
TIMED_OUT = 'timed_out'
class flowsemantics.execution.ExecutionTrace(contract_version: str, events: tuple[~flowsemantics.execution.trace.TraceEvent, ...]=<factory>)[source]

Bases: object

Ordered trace events plus contract version (SPEC 3.8).

contract_version: str
events: tuple[TraceEvent, ...]
__init__(contract_version: str, events: tuple[~flowsemantics.execution.trace.TraceEvent, ...]=<factory>) → None
class flowsemantics.execution.FakeClock(wall: datetime | None = None, monotonic: float = 0.0)[source]

Bases: object

Deterministic clock for tests, replay, and conformance vectors.

Wall and monotonic values advance only when advance() or advance_wall() is called. Two FakeClocks constructed with the same values behave identically, which is what SPEC.md section 6.1 determinism requires.

__init__(wall: datetime | None = None, monotonic: float = 0.0) → None[source]

Create a FakeClock at the given wall time and monotonic offset.

wall() → datetime[source]

Return the current fake wall-clock time.

monotonic() → float[source]

Return the current fake monotonic time in seconds.

advance(seconds: float) → None[source]

Advance both wall and monotonic time by the same duration.

advance_wall(seconds: float) → None[source]

Advance only the wall clock (monotonic time is unaffected).

set_wall(value: datetime) → None[source]

Set the wall clock to an absolute value.

class flowsemantics.execution.FinalizationInfo(is_clean: bool, cleanup_exceptions: tuple[BaseException, ...] = (), cleanup_budget_used: timedelta = datetime.timedelta(0), cleanup_budget_exceeded: bool = False)[source]

Bases: object

Finalization metadata including is_clean (SPEC 3.5, B6, B8).

is_clean: bool
cleanup_exceptions: tuple[BaseException, ...] = ()
cleanup_budget_used: timedelta = datetime.timedelta(0)
cleanup_budget_exceeded: bool = False
__init__(is_clean: bool, cleanup_exceptions: tuple[BaseException, ...] = (), cleanup_budget_used: timedelta = datetime.timedelta(0), cleanup_budget_exceeded: bool = False) → None
class flowsemantics.execution.FlowContext(input: Any, clock: Clock = <factory>, state: dict[str, ~typing.Any]=<factory>)[source]

Bases: object

Isolated per-invocation state for one flow execution.

input

The input value passed to execute().

Type:

Any

clock

The injected clock. Defaults to SystemClock when the caller does not supply one; deterministic replay MUST inject FakeClock.

Type:

flowsemantics.execution.clock.Clock

state

Per-invocation mutable state for engine bookkeeping. Never shared between invocations.

Type:

dict[str, Any]

input: Any
clock: Clock
state: dict[str, Any]
__init__(input: Any, clock: Clock = <factory>, state: dict[str, ~typing.Any]=<factory>) → None
class flowsemantics.execution.FlowOutcome(execution_status: ExecutionStatus, decision_status: DecisionStatus | None = None, value: Any = None, recovery: RecoveryInfo | None = None, interruption: InterruptionInfo | None = None, finalization: FinalizationInfo | None = None, primary_exception: BaseException | None = None, secondary_exceptions: tuple[BaseException, ...] = (), effects: tuple[Effect, ...] = (), trace: ExecutionTrace | None = None)[source]

Bases: object

The structured result of one execution (SPEC 3.6).

Invariants enforced by construction in the engine:
  • decision_status is absent unless execution_status is COMPLETED.

  • primary_exception is absent unless execution_status is FAILED.

  • interruption is populated only for CANCELLED and TIMED_OUT.

  • recovery is populated only when an exception was recovered.

  • secondary_exceptions includes cleanup exceptions even when the primary execution succeeded (B7).

execution_status: ExecutionStatus
decision_status: DecisionStatus | None = None
value: Any = None
recovery: RecoveryInfo | None = None
interruption: InterruptionInfo | None = None
finalization: FinalizationInfo | None = None
primary_exception: BaseException | None = None
secondary_exceptions: tuple[BaseException, ...] = ()
effects: tuple[Effect, ...] = ()
trace: ExecutionTrace | None = None
__init__(execution_status: ExecutionStatus, decision_status: DecisionStatus | None = None, value: Any = None, recovery: RecoveryInfo | None = None, interruption: InterruptionInfo | None = None, finalization: FinalizationInfo | None = None, primary_exception: BaseException | None = None, secondary_exceptions: tuple[BaseException, ...] = (), effects: tuple[Effect, ...] = (), trace: ExecutionTrace | None = None) → None
class flowsemantics.execution.InterruptionInfo(kind: Literal['cancelled', 'timed_out'], source: str, timestamp: datetime, deadline: datetime | None = None)[source]

Bases: object

Cancellation/timeout metadata. Never an exception (SPEC 3.3, B5).

kind: Literal['cancelled', 'timed_out']
source: str
timestamp: datetime
deadline: datetime | None = None
__init__(kind: Literal['cancelled', 'timed_out'], source: str, timestamp: datetime, deadline: datetime | None = None) → None
class flowsemantics.execution.RecoveryInfo(original_exception: BaseException, handler_rule_id: str, recovered_at: datetime)[source]

Bases: object

Exception-recovery metadata only (SPEC 3.4, O1).

original_exception: BaseException
handler_rule_id: str
recovered_at: datetime
__init__(original_exception: BaseException, handler_rule_id: str, recovered_at: datetime) → None
class flowsemantics.execution.SystemClock[source]

Bases: object

Default clock reading the host’s real clocks (SPEC.md section 6.3).

This is the only Core component permitted to read ambient time. It is replaceable: pass any Clock implementation via Flow.with_clock() or the execute() clock parameter. Deterministic replay and tests MUST use FakeClock instead.

wall() → datetime[source]

Return the host wall-clock time in UTC.

monotonic() → float[source]

Return the host monotonic clock in seconds.

class flowsemantics.execution.TraceEvent(type: str, rule_id: str | None = None, timestamp: datetime | None = None, reason: str | None = None, contract_version: str | None = None, diagnostics: dict[str, Any] | None = None)[source]

Bases: object

A single trace event (SPEC 3.8).

type: str
rule_id: str | None = None
timestamp: datetime | None = None
reason: str | None = None
contract_version: str | None = None
diagnostics: dict[str, Any] | None = None
__init__(type: str, rule_id: str | None = None, timestamp: datetime | None = None, reason: str | None = None, contract_version: str | None = None, diagnostics: dict[str, Any] | None = None) → None
class flowsemantics.execution.TraceRecorder(contract_version: str, diagnostic_hook: Callable[[TraceEvent], dict[str, Any] | None] | None = None)[source]

Bases: object

Collects trace events during one execution (SPEC 12.1, 12.3).

Created only when tracing is enabled; when disabled, no recorder exists and evaluation pays zero tracing cost.

__init__(contract_version: str, diagnostic_hook: Callable[[TraceEvent], dict[str, Any] | None] | None = None) → None[source]

Create an empty recorder for the given contract version.

diagnostic_hook is the application-supplied redaction-aware hook (SPEC 12.2): it receives each event and may return a dict of additional diagnostic fields. The library defers entirely to the application’s policy; absent a hook, no diagnostic data is added.

emit(event_type: str, *, rule_id: str | None = None, timestamp: datetime | None = None, reason: str | None = None) → None[source]

Append an event. Validates type and reason against the closed sets.

build() → ExecutionTrace[source]

Produce the immutable ExecutionTrace.