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.
- flow_semantics-0.1.3.dist-info/METADATA +325 -0
- flow_semantics-0.1.3.dist-info/RECORD +34 -0
- flow_semantics-0.1.3.dist-info/WHEEL +4 -0
- flow_semantics-0.1.3.dist-info/licenses/LICENSE +202 -0
- flowsemantics/__init__.py +172 -0
- flowsemantics/_spec.py +12 -0
- flowsemantics/_version.py +81 -0
- flowsemantics/abstractions/__init__.py +19 -0
- flowsemantics/configuration/__init__.py +103 -0
- flowsemantics/configuration/catch.py +58 -0
- flowsemantics/configuration/finalization.py +71 -0
- flowsemantics/configuration/flow.py +1952 -0
- flowsemantics/configuration/portable.py +403 -0
- flowsemantics/configuration/rule.py +81 -0
- flowsemantics/configuration/table.py +109 -0
- flowsemantics/errors/__init__.py +54 -0
- flowsemantics/execution/__init__.py +55 -0
- flowsemantics/execution/clock.py +104 -0
- flowsemantics/execution/context.py +46 -0
- flowsemantics/execution/outcome.py +118 -0
- flowsemantics/execution/trace.py +160 -0
- flowsemantics/finalization/__init__.py +18 -0
- flowsemantics/portable/__init__.py +20 -0
- flowsemantics/py.typed +0 -0
- flowsemantics/testing/__init__.py +23 -0
- flowsemantics/testing/conformance/__init__.py +22 -0
- flowsemantics/testing/conformance/fixtures.py +329 -0
- flowsemantics/testing/conformance/runner.py +472 -0
- flowsemantics/testing/py.typed +0 -0
- flowsemantics/trace/__init__.py +18 -0
- flowsemantics/trust/__init__.py +36 -0
- flowsemantics/trust/replay.py +261 -0
- flowsemantics/trust/shadow.py +103 -0
- flowsemantics/validation/__init__.py +18 -0
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
"""flowsemantics.trust.replay - Replay records and four replay operations.
|
|
2
|
+
|
|
3
|
+
Layer: Trust
|
|
4
|
+
Module: flowsemantics.trust.replay
|
|
5
|
+
Since: 0.3.0
|
|
6
|
+
Implements: SPEC.md sections 12.6, 12.10, 9.5
|
|
7
|
+
Depends on: flowsemantics.configuration.flow, flowsemantics.execution.*,
|
|
8
|
+
stdlib (hmac, hashlib, json, datetime)
|
|
9
|
+
Used by: audit, regression testing, decision forensics
|
|
10
|
+
See also: SPEC.md sections 12.6, 9.5, 12.10
|
|
11
|
+
|
|
12
|
+
Notes:
|
|
13
|
+
- Four distinct operations, never conflated (SPEC 12.6):
|
|
14
|
+
SIMULATION (re-evaluate, no effects), HISTORICAL (return recorded
|
|
15
|
+
data), DECISION (re-evaluate + semantic comparison), EFFECTS
|
|
16
|
+
(apply recorded effects via authorized executor; never default).
|
|
17
|
+
- Replay guarantee is SEMANTIC equivalence, not bit-for-bit (A5).
|
|
18
|
+
- Fail closed on spec_version or contract_version mismatch (9.5).
|
|
19
|
+
- Integrity: HMAC-SHA256 over a canonical JSON payload. An unkeyed
|
|
20
|
+
hash alone is NOT sufficient (12.6); integrity is optional on the
|
|
21
|
+
record but verified whenever present.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from __future__ import annotations
|
|
25
|
+
|
|
26
|
+
import datetime
|
|
27
|
+
import hashlib
|
|
28
|
+
import hmac
|
|
29
|
+
import json
|
|
30
|
+
from dataclasses import dataclass
|
|
31
|
+
from enum import StrEnum
|
|
32
|
+
from typing import Any
|
|
33
|
+
|
|
34
|
+
from flowsemantics._spec import SPEC_VERSION
|
|
35
|
+
from flowsemantics.configuration.flow import FlowContract
|
|
36
|
+
from flowsemantics.errors import FlowSemanticsError
|
|
37
|
+
from flowsemantics.execution.clock import FakeClock
|
|
38
|
+
from flowsemantics.execution.outcome import FlowOutcome
|
|
39
|
+
from flowsemantics.execution.trace import ExecutionTrace
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class ReplayError(FlowSemanticsError):
|
|
43
|
+
"""Raised when a replay record cannot be safely replayed."""
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class ReplayMode(StrEnum):
|
|
47
|
+
"""The four replay operations (SPEC 12.6)."""
|
|
48
|
+
|
|
49
|
+
SIMULATION = "simulation"
|
|
50
|
+
HISTORICAL = "historical"
|
|
51
|
+
DECISION = "decision"
|
|
52
|
+
EFFECTS = "effects"
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@dataclass(frozen=True)
|
|
56
|
+
class ReplayRecord:
|
|
57
|
+
"""A recorded decision tuple (SPEC 12.6, 12.10).
|
|
58
|
+
|
|
59
|
+
Attributes:
|
|
60
|
+
spec_version: SPEC.md version that produced the record.
|
|
61
|
+
contract_version: Structural hash of the FlowContract.
|
|
62
|
+
input: The recorded input (application data; handle per 12.2).
|
|
63
|
+
clock_wall: Recorded wall-clock value.
|
|
64
|
+
clock_monotonic: Recorded monotonic value.
|
|
65
|
+
outcome: The recorded FlowOutcome.
|
|
66
|
+
trace: The recorded trace, if collected.
|
|
67
|
+
effects: Recorded proposed effects.
|
|
68
|
+
integrity: Optional HMAC-SHA256 hex digest (see sign()).
|
|
69
|
+
"""
|
|
70
|
+
|
|
71
|
+
spec_version: str
|
|
72
|
+
contract_version: str
|
|
73
|
+
input: Any
|
|
74
|
+
clock_wall: datetime.datetime
|
|
75
|
+
clock_monotonic: float
|
|
76
|
+
outcome: FlowOutcome
|
|
77
|
+
trace: ExecutionTrace | None = None
|
|
78
|
+
effects: tuple[Any, ...] = ()
|
|
79
|
+
integrity: str | None = None
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@dataclass(frozen=True)
|
|
83
|
+
class ReplayResult:
|
|
84
|
+
"""Result of a replay operation."""
|
|
85
|
+
|
|
86
|
+
mode: ReplayMode
|
|
87
|
+
outcome: FlowOutcome | None = None
|
|
88
|
+
equivalent: bool | None = None
|
|
89
|
+
effects_applied: int = 0
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _canonical_payload(record: ReplayRecord) -> str:
|
|
93
|
+
"""Canonical JSON for integrity signing (input via repr fallback)."""
|
|
94
|
+
payload = {
|
|
95
|
+
"spec_version": record.spec_version,
|
|
96
|
+
"contract_version": record.contract_version,
|
|
97
|
+
"input": record.input,
|
|
98
|
+
"clock_wall": record.clock_wall.isoformat(),
|
|
99
|
+
"clock_monotonic": record.clock_monotonic,
|
|
100
|
+
"outcome": {
|
|
101
|
+
"execution_status": str(record.outcome.execution_status),
|
|
102
|
+
"decision_status": (
|
|
103
|
+
str(record.outcome.decision_status)
|
|
104
|
+
if record.outcome.decision_status is not None
|
|
105
|
+
else None
|
|
106
|
+
),
|
|
107
|
+
"value": record.outcome.value,
|
|
108
|
+
"has_recovery": record.outcome.recovery is not None,
|
|
109
|
+
"interruption_kind": (
|
|
110
|
+
record.outcome.interruption.kind
|
|
111
|
+
if record.outcome.interruption is not None
|
|
112
|
+
else None
|
|
113
|
+
),
|
|
114
|
+
},
|
|
115
|
+
"effects": [repr(e) for e in record.effects],
|
|
116
|
+
}
|
|
117
|
+
return json.dumps(payload, sort_keys=True, default=repr)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def sign(record: ReplayRecord, key: bytes) -> ReplayRecord:
|
|
121
|
+
"""Return a copy of the record with an HMAC-SHA256 integrity tag."""
|
|
122
|
+
digest = hmac.new(key, _canonical_payload(record).encode("utf-8"), hashlib.sha256).hexdigest()
|
|
123
|
+
return ReplayRecord(
|
|
124
|
+
spec_version=record.spec_version,
|
|
125
|
+
contract_version=record.contract_version,
|
|
126
|
+
input=record.input,
|
|
127
|
+
clock_wall=record.clock_wall,
|
|
128
|
+
clock_monotonic=record.clock_monotonic,
|
|
129
|
+
outcome=record.outcome,
|
|
130
|
+
trace=record.trace,
|
|
131
|
+
effects=record.effects,
|
|
132
|
+
integrity=digest,
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def record(contract: FlowContract, input: Any, *, clock: Any | None = None) -> ReplayRecord:
|
|
137
|
+
"""Execute with tracing and capture a ReplayRecord (SPEC 12.6)."""
|
|
138
|
+
clk = clock if clock is not None else contract.clock
|
|
139
|
+
outcome = contract.try_execute(input, clock=clk, trace=True)
|
|
140
|
+
return ReplayRecord(
|
|
141
|
+
spec_version=SPEC_VERSION,
|
|
142
|
+
contract_version=contract.contract_version,
|
|
143
|
+
input=input,
|
|
144
|
+
clock_wall=clk.wall(),
|
|
145
|
+
clock_monotonic=clk.monotonic(),
|
|
146
|
+
outcome=outcome,
|
|
147
|
+
trace=outcome.trace,
|
|
148
|
+
effects=outcome.effects,
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def _verify_integrity(record: ReplayRecord, key: bytes | None) -> None:
|
|
153
|
+
if record.integrity is None:
|
|
154
|
+
return
|
|
155
|
+
if key is None:
|
|
156
|
+
raise ReplayError("record carries an integrity tag; an integrity_key is required")
|
|
157
|
+
expected = hmac.new(key, _canonical_payload(record).encode("utf-8"), hashlib.sha256).hexdigest()
|
|
158
|
+
if not hmac.compare_digest(expected, record.integrity):
|
|
159
|
+
raise ReplayError("replay record integrity check failed")
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _check_versions(record: ReplayRecord, contract: FlowContract) -> None:
|
|
163
|
+
if record.spec_version != SPEC_VERSION:
|
|
164
|
+
raise ReplayError(
|
|
165
|
+
f"record spec_version {record.spec_version!r} does not match "
|
|
166
|
+
f"current {SPEC_VERSION!r}; replay fails closed (SPEC 9.5)"
|
|
167
|
+
)
|
|
168
|
+
if record.contract_version != contract.contract_version:
|
|
169
|
+
raise ReplayError(
|
|
170
|
+
"record contract_version does not match the current contract; "
|
|
171
|
+
"replay fails closed (SPEC 12.6)"
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _reconstruct_clock(record: ReplayRecord) -> FakeClock:
|
|
176
|
+
clock = FakeClock()
|
|
177
|
+
clock.set_wall(record.clock_wall)
|
|
178
|
+
clock.advance(record.clock_monotonic)
|
|
179
|
+
return clock
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def _semantically_equivalent(a: FlowOutcome, b: FlowOutcome) -> bool:
|
|
183
|
+
"""Semantic equivalence per SPEC 12.6 (A5).
|
|
184
|
+
|
|
185
|
+
Compares status, decision, value, recovery presence, interruption kind.
|
|
186
|
+
"""
|
|
187
|
+
if a.execution_status is not b.execution_status:
|
|
188
|
+
return False
|
|
189
|
+
if a.decision_status is not b.decision_status:
|
|
190
|
+
return False
|
|
191
|
+
if a.value != b.value:
|
|
192
|
+
return False
|
|
193
|
+
if (a.recovery is None) != (b.recovery is None):
|
|
194
|
+
return False
|
|
195
|
+
if (
|
|
196
|
+
a.recovery is not None
|
|
197
|
+
and b.recovery is not None
|
|
198
|
+
and a.recovery.handler_rule_id != b.recovery.handler_rule_id
|
|
199
|
+
):
|
|
200
|
+
return False
|
|
201
|
+
a_kind = a.interruption.kind if a.interruption else None
|
|
202
|
+
b_kind = b.interruption.kind if b.interruption else None
|
|
203
|
+
return a_kind == b_kind
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def replay(
|
|
207
|
+
record: ReplayRecord,
|
|
208
|
+
contract: FlowContract,
|
|
209
|
+
*,
|
|
210
|
+
mode: ReplayMode = ReplayMode.SIMULATION,
|
|
211
|
+
integrity_key: bytes | None = None,
|
|
212
|
+
effect_executor: Any | None = None,
|
|
213
|
+
authorize_effects: bool = False,
|
|
214
|
+
) -> ReplayResult:
|
|
215
|
+
"""Replay a recorded decision (SPEC 12.6).
|
|
216
|
+
|
|
217
|
+
Args:
|
|
218
|
+
record: The recorded tuple.
|
|
219
|
+
contract: The current FlowContract to replay against.
|
|
220
|
+
mode: One of the four replay operations.
|
|
221
|
+
integrity_key: HMAC key when the record is integrity-tagged.
|
|
222
|
+
effect_executor: Callable(Effect) for EFFECTS mode only.
|
|
223
|
+
authorize_effects: Must be True for EFFECTS mode; never default.
|
|
224
|
+
"""
|
|
225
|
+
_verify_integrity(record, integrity_key)
|
|
226
|
+
_check_versions(record, contract)
|
|
227
|
+
|
|
228
|
+
if mode is ReplayMode.HISTORICAL:
|
|
229
|
+
return ReplayResult(mode=mode, outcome=record.outcome)
|
|
230
|
+
|
|
231
|
+
if mode in (ReplayMode.SIMULATION, ReplayMode.DECISION):
|
|
232
|
+
clock = _reconstruct_clock(record)
|
|
233
|
+
outcome = contract.simulate(record.input, clock=clock)
|
|
234
|
+
equivalent = _semantically_equivalent(outcome, record.outcome)
|
|
235
|
+
return ReplayResult(mode=mode, outcome=outcome, equivalent=equivalent)
|
|
236
|
+
|
|
237
|
+
if mode is ReplayMode.EFFECTS:
|
|
238
|
+
if not authorize_effects:
|
|
239
|
+
raise ReplayError(
|
|
240
|
+
"effect re-execution requires explicit authorization (authorize_effects=True)"
|
|
241
|
+
)
|
|
242
|
+
if effect_executor is None:
|
|
243
|
+
raise ReplayError("EFFECTS mode requires an effect_executor")
|
|
244
|
+
applied = 0
|
|
245
|
+
for effect in record.effects:
|
|
246
|
+
effect_executor(effect)
|
|
247
|
+
applied += 1
|
|
248
|
+
return ReplayResult(mode=mode, effects_applied=applied)
|
|
249
|
+
|
|
250
|
+
raise ReplayError(f"unknown replay mode: {mode!r}")
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
__all__: list[str] = [
|
|
254
|
+
"ReplayError",
|
|
255
|
+
"ReplayMode",
|
|
256
|
+
"ReplayRecord",
|
|
257
|
+
"ReplayResult",
|
|
258
|
+
"record",
|
|
259
|
+
"replay",
|
|
260
|
+
"sign",
|
|
261
|
+
]
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""flowsemantics.trust.shadow - Shadow evaluation (SPEC 12.7).
|
|
2
|
+
|
|
3
|
+
Layer: Trust
|
|
4
|
+
Module: flowsemantics.trust.shadow
|
|
5
|
+
Since: 0.3.0
|
|
6
|
+
Implements: SPEC.md section 12.7
|
|
7
|
+
Depends on: flowsemantics.configuration.flow, flowsemantics.execution.outcome
|
|
8
|
+
Used by: pre-deployment candidate comparison
|
|
9
|
+
See also: SPEC.md section 12.7
|
|
10
|
+
|
|
11
|
+
Notes:
|
|
12
|
+
- Shadow evaluation NEVER applies effects: both flows run via
|
|
13
|
+
simulate(), so hooks and finalizers are suppressed (12.5).
|
|
14
|
+
- Deterministic: same inputs + contracts + clock -> same report.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
from collections.abc import Iterable
|
|
20
|
+
from dataclasses import dataclass
|
|
21
|
+
from typing import Any
|
|
22
|
+
|
|
23
|
+
from flowsemantics.configuration.flow import FlowContract
|
|
24
|
+
from flowsemantics.execution.clock import Clock
|
|
25
|
+
from flowsemantics.execution.outcome import FlowOutcome
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass(frozen=True)
|
|
29
|
+
class ShadowEntry:
|
|
30
|
+
"""One input's current-vs-candidate comparison."""
|
|
31
|
+
|
|
32
|
+
index: int
|
|
33
|
+
input: Any
|
|
34
|
+
current: FlowOutcome
|
|
35
|
+
candidate: FlowOutcome
|
|
36
|
+
differs: bool
|
|
37
|
+
differing_fields: tuple[str, ...]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass(frozen=True)
|
|
41
|
+
class ShadowReport:
|
|
42
|
+
"""Aggregate shadow comparison report (SPEC 12.7)."""
|
|
43
|
+
|
|
44
|
+
entries: tuple[ShadowEntry, ...]
|
|
45
|
+
total: int
|
|
46
|
+
differences: int
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _diff_fields(a: FlowOutcome, b: FlowOutcome) -> tuple[str, ...]:
|
|
50
|
+
fields: list[str] = []
|
|
51
|
+
if a.execution_status is not b.execution_status:
|
|
52
|
+
fields.append("execution_status")
|
|
53
|
+
if a.decision_status is not b.decision_status:
|
|
54
|
+
fields.append("decision_status")
|
|
55
|
+
if a.value != b.value:
|
|
56
|
+
fields.append("value")
|
|
57
|
+
if (a.recovery is None) != (b.recovery is None):
|
|
58
|
+
fields.append("recovery")
|
|
59
|
+
a_kind = a.interruption.kind if a.interruption else None
|
|
60
|
+
b_kind = b.interruption.kind if b.interruption else None
|
|
61
|
+
if a_kind != b_kind:
|
|
62
|
+
fields.append("interruption")
|
|
63
|
+
return tuple(fields)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def shadow(
|
|
67
|
+
current: FlowContract,
|
|
68
|
+
candidate: FlowContract,
|
|
69
|
+
inputs: Iterable[Any],
|
|
70
|
+
*,
|
|
71
|
+
clock: Clock | None = None,
|
|
72
|
+
) -> ShadowReport:
|
|
73
|
+
"""Compare a candidate flow against the current flow (SPEC 12.7).
|
|
74
|
+
|
|
75
|
+
Both flows are simulated (no effects, no hooks, no finalizers).
|
|
76
|
+
"""
|
|
77
|
+
entries: list[ShadowEntry] = []
|
|
78
|
+
for index, inp in enumerate(inputs):
|
|
79
|
+
current_outcome = current.simulate(inp, clock=clock)
|
|
80
|
+
candidate_outcome = candidate.simulate(inp, clock=clock)
|
|
81
|
+
fields = _diff_fields(current_outcome, candidate_outcome)
|
|
82
|
+
entries.append(
|
|
83
|
+
ShadowEntry(
|
|
84
|
+
index=index,
|
|
85
|
+
input=inp,
|
|
86
|
+
current=current_outcome,
|
|
87
|
+
candidate=candidate_outcome,
|
|
88
|
+
differs=bool(fields),
|
|
89
|
+
differing_fields=fields,
|
|
90
|
+
)
|
|
91
|
+
)
|
|
92
|
+
return ShadowReport(
|
|
93
|
+
entries=tuple(entries),
|
|
94
|
+
total=len(entries),
|
|
95
|
+
differences=sum(1 for e in entries if e.differs),
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
__all__: list[str] = [
|
|
100
|
+
"ShadowEntry",
|
|
101
|
+
"ShadowReport",
|
|
102
|
+
"shadow",
|
|
103
|
+
]
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""flowsemantics.validation - validate, ValidationResult (Phase 3.5+).
|
|
2
|
+
|
|
3
|
+
Layer: Domain
|
|
4
|
+
Module: flowsemantics.validation
|
|
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 Phase 3.5
|
|
9
|
+
See also: SPEC.md section 12.5
|
|
10
|
+
|
|
11
|
+
Notes:
|
|
12
|
+
- __all__ is empty in Phase 0.0. It grows as features are implemented.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
__all__: list[str] = []
|
|
18
|
+
|