flow-semantics 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,172 @@
1
+ """FlowSemantics: zero-runtime-dependency, language-neutral execution-semantics library.
2
+
3
+ This package is the Python reference implementation of the FlowSemantics
4
+ specification (SPEC.md v0.1, frozen). The specification is authoritative;
5
+ this package is a conforming implementation, not the definition.
6
+
7
+ Layer: Contracts
8
+ Module: flowsemantics
9
+ Since: 0.0.0
10
+ Implements: n/a (public package surface)
11
+ Depends on: flowsemantics._version (re-exported as __version__)
12
+ Used by: consumers, tests, samples, conformance runner
13
+ See also: SPEC.md, docs/getting-started.md (Phase 1)
14
+
15
+ Notes:
16
+ - __all__ grows as features are implemented in Phases 1-3.
17
+ - _version is re-exported for the smoke test and for pip-show consistency.
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ from flowsemantics._version import __version__
23
+ from flowsemantics.configuration import (
24
+ OPS,
25
+ CatchRule,
26
+ DecisionTable,
27
+ DecisionTableRow,
28
+ EvaluationMode,
29
+ Fallback,
30
+ FinallyCancellationToken,
31
+ FinallyContext,
32
+ FinallyFailurePolicy,
33
+ Flow,
34
+ FlowContract,
35
+ FlowPortability,
36
+ PExpr,
37
+ PortableFlow,
38
+ PortableRule,
39
+ Rule,
40
+ RuleId,
41
+ ValidationIssue,
42
+ eval_expr,
43
+ expr_from_dict,
44
+ expr_to_dict,
45
+ pand,
46
+ pconst,
47
+ peq,
48
+ pfield,
49
+ pge,
50
+ pgt,
51
+ pin,
52
+ pis_not_null,
53
+ pis_null,
54
+ ple,
55
+ plt,
56
+ pne,
57
+ pnot,
58
+ pnot_in,
59
+ por,
60
+ table_to_flow,
61
+ )
62
+ from flowsemantics.errors import (
63
+ AsyncOnlyContractError,
64
+ EvaluationError,
65
+ FinalizationBudgetExceeded,
66
+ FlowSemanticsError,
67
+ PortabilityError,
68
+ StructureError,
69
+ )
70
+ from flowsemantics.execution import (
71
+ EVENT_TYPES,
72
+ REASONS,
73
+ Clock,
74
+ DecisionStatus,
75
+ Effect,
76
+ ExecutionStatus,
77
+ ExecutionTrace,
78
+ FakeClock,
79
+ FinalizationInfo,
80
+ FlowContext,
81
+ FlowOutcome,
82
+ InterruptionInfo,
83
+ RecoveryInfo,
84
+ SystemClock,
85
+ TraceEvent,
86
+ TraceRecorder,
87
+ )
88
+ from flowsemantics.trust import (
89
+ ReplayError,
90
+ ReplayMode,
91
+ ReplayRecord,
92
+ ReplayResult,
93
+ ShadowEntry,
94
+ ShadowReport,
95
+ record,
96
+ replay,
97
+ shadow,
98
+ sign,
99
+ )
100
+
101
+ __all__: list[str] = [
102
+ "EVENT_TYPES",
103
+ "OPS",
104
+ "REASONS",
105
+ "AsyncOnlyContractError",
106
+ "CatchRule",
107
+ "Clock",
108
+ "DecisionStatus",
109
+ "DecisionTable",
110
+ "DecisionTableRow",
111
+ "Effect",
112
+ "EvaluationError",
113
+ "EvaluationMode",
114
+ "ExecutionStatus",
115
+ "ExecutionTrace",
116
+ "FakeClock",
117
+ "Fallback",
118
+ "FinalizationBudgetExceeded",
119
+ "FinalizationInfo",
120
+ "FinallyCancellationToken",
121
+ "FinallyContext",
122
+ "FinallyFailurePolicy",
123
+ "Flow",
124
+ "FlowContext",
125
+ "FlowContract",
126
+ "FlowOutcome",
127
+ "FlowPortability",
128
+ "FlowSemanticsError",
129
+ "InterruptionInfo",
130
+ "PExpr",
131
+ "PortabilityError",
132
+ "PortableFlow",
133
+ "PortableRule",
134
+ "RecoveryInfo",
135
+ "ReplayError",
136
+ "ReplayMode",
137
+ "ReplayRecord",
138
+ "ReplayResult",
139
+ "Rule",
140
+ "RuleId",
141
+ "ShadowEntry",
142
+ "ShadowReport",
143
+ "StructureError",
144
+ "SystemClock",
145
+ "TraceEvent",
146
+ "TraceRecorder",
147
+ "ValidationIssue",
148
+ "__version__",
149
+ "eval_expr",
150
+ "expr_from_dict",
151
+ "expr_to_dict",
152
+ "pand",
153
+ "pconst",
154
+ "peq",
155
+ "pfield",
156
+ "pge",
157
+ "pgt",
158
+ "pin",
159
+ "pis_not_null",
160
+ "pis_null",
161
+ "ple",
162
+ "plt",
163
+ "pne",
164
+ "pnot",
165
+ "pnot_in",
166
+ "por",
167
+ "record",
168
+ "replay",
169
+ "shadow",
170
+ "sign",
171
+ "table_to_flow",
172
+ ]
flowsemantics/_spec.py ADDED
@@ -0,0 +1,12 @@
1
+ """Specification version constant.
2
+
3
+ SPEC.md is versioned independently of the package version (VERSION file).
4
+ Vectors declare the spec_version they target; the conformance runner uses
5
+ this constant to decide compatibility (runner-protocol.md).
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ SPEC_VERSION = "0.1.0"
11
+
12
+ __all__: list[str] = ["SPEC_VERSION"]
@@ -0,0 +1,81 @@
1
+ """Single source of truth for the FlowSemantics package version.
2
+
3
+ Reads VERSION from the repository root when running from a source checkout,
4
+ and falls back to importlib.metadata when installed as a wheel. Both paths
5
+ agree because hatchling bakes the same VERSION content into the wheel
6
+ metadata via [tool.hatch.version] in python/pyproject.toml.
7
+
8
+ Layer: Contracts
9
+ Module: flowsemantics._version
10
+ Since: 0.0.0
11
+ Implements: n/a
12
+ Depends on: stdlib only (pathlib, importlib.metadata)
13
+ Used by: flowsemantics.__init__, test_smoke, pip-consumer-path
14
+ See also: VERSION (repository root), python/pyproject.toml
15
+
16
+ Notes:
17
+ - VERSION is NOT shipped inside the wheel; the fallback path is the
18
+ authoritative path for installed consumers.
19
+ - No f-strings with nested quotes (F-010 lesson).
20
+ """
21
+
22
+ from __future__ import annotations
23
+
24
+ from importlib import metadata
25
+ from pathlib import Path
26
+
27
+ DISTRIBUTION_NAME = "flow-semantics"
28
+
29
+
30
+ def _read_from_source_tree() -> str | None:
31
+ """Walk up from this file to find VERSION at the repository root.
32
+
33
+ Returns:
34
+ The stripped VERSION content, or None if not found.
35
+
36
+ Raises:
37
+ None. Missing VERSION is not an error here; the caller falls back.
38
+ """
39
+ here = Path(__file__).resolve()
40
+ for parent in here.parents:
41
+ candidate = parent / "VERSION"
42
+ if candidate.is_file():
43
+ return candidate.read_text(encoding="utf-8").strip()
44
+ return None
45
+
46
+
47
+ def _read_from_installed_metadata() -> str:
48
+ """Read the version from installed distribution metadata.
49
+
50
+ Returns:
51
+ The version string reported by importlib.metadata.
52
+
53
+ Raises:
54
+ metadata.PackageNotFoundError: If the distribution is not installed.
55
+ """
56
+ return metadata.version(DISTRIBUTION_NAME)
57
+
58
+
59
+ def _resolve_version() -> str:
60
+ """Resolve the version, preferring the source tree, falling back to metadata.
61
+
62
+ Returns:
63
+ The version string.
64
+
65
+ Raises:
66
+ RuntimeError: If neither path produces a version.
67
+ """
68
+ v = _read_from_source_tree()
69
+ if v is not None and v != "":
70
+ return v
71
+ try:
72
+ return _read_from_installed_metadata()
73
+ except metadata.PackageNotFoundError as exc:
74
+ raise RuntimeError(
75
+ "Cannot resolve flowsemantics version: no VERSION in source tree "
76
+ "and distribution not installed"
77
+ ) from exc
78
+
79
+
80
+ __version__: str = _resolve_version()
81
+
@@ -0,0 +1,19 @@
1
+ """flowsemantics.abstractions - Protocols and interfaces (Phase 1+).
2
+
3
+ Layer: Contracts
4
+ Module: flowsemantics.abstractions
5
+ Since: 0.0.0
6
+ Implements: n/a (subpackage marker)
7
+ Depends on: n/a (empty in Phase 0.0)
8
+ Used by: parent package; populated in Phases 1-3
9
+ See also: SPEC.md, docs/adr/ADR-0001-governing-decisions.md
10
+
11
+ Notes:
12
+ - __all__ is empty in Phase 0.0. It grows as features are implemented.
13
+ - No cross-subpackage imports in Phase 0.0 (no feature code yet).
14
+ """
15
+
16
+ from __future__ import annotations
17
+
18
+ __all__: list[str] = []
19
+
@@ -0,0 +1,103 @@
1
+ """flowsemantics.configuration - Flow builder, options, registry.
2
+
3
+ Layer: Composition
4
+ Module: flowsemantics.configuration
5
+ Since: 0.0.0
6
+ Implements: SPEC.md sections 4.1, 10
7
+ Depends on: flowsemantics.configuration.{rule,flow}, flowsemantics.errors,
8
+ flowsemantics.execution
9
+ Used by: parent package; populated from Phase 1.0
10
+ See also: SPEC.md section 10
11
+
12
+ Notes:
13
+ - __all__ grows as features are implemented. Currently exports the
14
+ decision-layer primitives (Flow, FlowContract, Rule, RuleId,
15
+ EvaluationMode, Fallback, FlowPortability).
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ from flowsemantics.configuration.catch import CatchRule
21
+ from flowsemantics.configuration.finalization import (
22
+ FinallyCancellationToken,
23
+ FinallyContext,
24
+ FinallyFailurePolicy,
25
+ )
26
+ from flowsemantics.configuration.flow import (
27
+ EvaluationMode,
28
+ Fallback,
29
+ Flow,
30
+ FlowContract,
31
+ FlowPortability,
32
+ ValidationIssue,
33
+ )
34
+ from flowsemantics.configuration.portable import (
35
+ OPS,
36
+ PExpr,
37
+ PortableFlow,
38
+ PortableRule,
39
+ eval_expr,
40
+ expr_from_dict,
41
+ expr_to_dict,
42
+ pand,
43
+ pconst,
44
+ peq,
45
+ pfield,
46
+ pge,
47
+ pgt,
48
+ pin,
49
+ pis_not_null,
50
+ pis_null,
51
+ ple,
52
+ plt,
53
+ pne,
54
+ pnot,
55
+ pnot_in,
56
+ por,
57
+ )
58
+ from flowsemantics.configuration.rule import Rule, RuleId
59
+ from flowsemantics.configuration.table import (
60
+ DecisionTable,
61
+ DecisionTableRow,
62
+ table_to_flow,
63
+ )
64
+
65
+ __all__: list[str] = [
66
+ "OPS",
67
+ "CatchRule",
68
+ "DecisionTable",
69
+ "DecisionTableRow",
70
+ "EvaluationMode",
71
+ "Fallback",
72
+ "FinallyCancellationToken",
73
+ "FinallyContext",
74
+ "FinallyFailurePolicy",
75
+ "Flow",
76
+ "FlowContract",
77
+ "FlowPortability",
78
+ "PExpr",
79
+ "PortableFlow",
80
+ "PortableRule",
81
+ "Rule",
82
+ "RuleId",
83
+ "ValidationIssue",
84
+ "eval_expr",
85
+ "expr_from_dict",
86
+ "expr_to_dict",
87
+ "pand",
88
+ "pconst",
89
+ "peq",
90
+ "pfield",
91
+ "pge",
92
+ "pgt",
93
+ "pin",
94
+ "pis_not_null",
95
+ "pis_null",
96
+ "ple",
97
+ "plt",
98
+ "pne",
99
+ "pnot",
100
+ "pnot_in",
101
+ "por",
102
+ "table_to_flow",
103
+ ]
@@ -0,0 +1,58 @@
1
+ """flowsemantics.configuration.catch - CatchRule (SPEC.md sections 11.1-11.4).
2
+
3
+ Layer: Composition
4
+ Module: flowsemantics.configuration.catch
5
+ Since: 0.2.0
6
+ Implements: SPEC.md sections 11.1 (catch), 11.2 (catch_when),
7
+ 11.3 (catch_any), 11.4 (rethrow)
8
+ Depends on: stdlib only
9
+ Used by: Flow builder, FlowContract, exception router
10
+ See also: SPEC.md section 11, section 7.3.1
11
+
12
+ Notes:
13
+ - Catch evaluation is ordered: the first matching catch wins.
14
+ - catch_any MUST be registered after all typed catch/catch_when rules;
15
+ compile() enforces this (SPEC 11.3, 4.3).
16
+ - rethrow stops catch evaluation and propagates the current exception.
17
+ """
18
+
19
+ from __future__ import annotations
20
+
21
+ from collections.abc import Callable
22
+ from dataclasses import dataclass
23
+ from typing import Any, Literal
24
+
25
+ CatchKind = Literal["typed", "when", "any", "rethrow"]
26
+
27
+ ExceptionPredicate = Callable[[Any, BaseException], bool]
28
+ ExceptionHandler = Callable[[Any, BaseException], Any]
29
+
30
+
31
+ @dataclass(frozen=True)
32
+ class CatchRule:
33
+ """One exception handler in the catch chain (SPEC 11.1-11.4).
34
+
35
+ Attributes:
36
+ kind: "typed" (isinstance match), "when" (isinstance + predicate),
37
+ "any" (matches any Exception), or "rethrow" (always propagates).
38
+ exc_type: Declared exception type for typed/when kinds.
39
+ predicate: Optional predicate for "when" kind; receives
40
+ (input, exception) and must return bool.
41
+ handler: Receives (input, exception), returns the outcome value.
42
+ Absent for "rethrow".
43
+ id: Optional stable identifier recorded in RecoveryInfo.
44
+ """
45
+
46
+ kind: CatchKind
47
+ exc_type: type[Exception] | None = None
48
+ predicate: ExceptionPredicate | None = None
49
+ handler: ExceptionHandler | None = None
50
+ id: str | None = None
51
+
52
+
53
+ __all__: list[str] = [
54
+ "CatchKind",
55
+ "CatchRule",
56
+ "ExceptionHandler",
57
+ "ExceptionPredicate",
58
+ ]
@@ -0,0 +1,71 @@
1
+ """flowsemantics.configuration.finalization - FinallyContext and policies.
2
+
3
+ Layer: Composition
4
+ Module: flowsemantics.configuration.finalization
5
+ Since: 0.2.0
6
+ Implements: SPEC.md sections 11.8, 16.4, 16.7
7
+ Depends on: flowsemantics.execution.outcome, stdlib only
8
+ Used by: Flow.finally_, FlowContract, finalization engine
9
+ See also: SPEC.md sections 11.8, 16.4, 16.7
10
+
11
+ Notes:
12
+ - Exactly three failure policies exist (SPEC 11.8.3): PreservePrimary
13
+ (default), Attach, ReplacePrimary. No others.
14
+ - is_clean is orthogonal to policy: false iff any finalizer raised,
15
+ was cancelled, or exceeded its budget (SPEC 11.8.3, 3.5).
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ from dataclasses import dataclass
21
+ from enum import StrEnum
22
+ from typing import Any
23
+
24
+ from flowsemantics.execution.outcome import ExecutionStatus, FlowOutcome
25
+
26
+
27
+ class FinallyFailurePolicy(StrEnum):
28
+ """What happens to the primary outcome when cleanup fails (SPEC 16.4)."""
29
+
30
+ PRESERVE_PRIMARY = "PreservePrimary"
31
+ ATTACH = "Attach"
32
+ REPLACE_PRIMARY = "ReplacePrimary"
33
+
34
+
35
+ @dataclass(frozen=True)
36
+ class FinallyCancellationToken:
37
+ """Fresh per-finalizer cancellation token (SPEC 11.8.1, 11.8.5).
38
+
39
+ Bounded by WithFinallyTimeout where configured. One finalizer
40
+ cancelling its token MUST NOT cancel the tokens of the others.
41
+ """
42
+
43
+ cancelled: bool = False
44
+
45
+
46
+ @dataclass(frozen=True)
47
+ class FinallyContext:
48
+ """Context passed to each finalizer (SPEC 11.8.1).
49
+
50
+ Attributes:
51
+ result: The sealed primary outcome.
52
+ status: The ExecutionStatus of the primary outcome.
53
+ value: The outcome value, read-only. Finally MUST NOT change it.
54
+ exception: The primary exception, if any.
55
+ context: The typed input from the flow.
56
+ cancellation_token: A fresh token for this finalizer.
57
+ """
58
+
59
+ result: FlowOutcome
60
+ status: ExecutionStatus
61
+ value: Any
62
+ exception: BaseException | None
63
+ context: Any
64
+ cancellation_token: FinallyCancellationToken
65
+
66
+
67
+ __all__: list[str] = [
68
+ "FinallyCancellationToken",
69
+ "FinallyContext",
70
+ "FinallyFailurePolicy",
71
+ ]