It is difficult to predict how reliable a sequence of steps is when each step has a chance of failing.
It looks at a series of transitions and calculates a reliability score based on how likely they are to break.
It provides a clear way to measure the stability of a process by accounting for potential failure points.
It was run inside an isolated container with no network access. This is the exact command and the real output it produced — captured process output, not written by a model.
$ python3 state_dynamic_resilience.py Resilience score: 0.55 Circuit breaker status: Operational
A screenshot of that run.
A clean run proves this does what is shown above, in a CPU-only sandbox. It is a small research demo — not a production tool, and nothing here was published anywhere.
All of it — 87 lines, one file, standard library only.
# State-Dynamic-Resilience Score Calculator
import random
import time
from collections import defaultdict
import heapq
class CircuitBreaker:
def __init__(self, failure_threshold=0.5, reset_timeout=10):
self.failure_count = 0
self.failure_threshold = failure_threshold
self.last_failure = 0
self.reset_timeout = reset_timeout
self.is_tripped = False
self.next_reset = 0
def check(self):
"""Check if circuit is operational"""
if self.is_tripped and time.time() > self.next_reset:
self.reset()
return not self.is_tripped
def trip(self):
"""Trip the circuit breaker"""
self.is_tripped = True
self.next_reset = time.time() + self.reset_timeout
def reset(self):
"""Reset circuit breaker"""
self.failure_count = 0
self.is_tripped = False
def record_failure(self):
"""Record a failure event"""
self.failure_count += 1
if self.failure_count / (self.failure_count + 1) > self.failure_threshold:
self.trip()
class StateGraph:
def __init__(self):
self.nodes = defaultdict(dict) # {state: {next_state: failure_prob}}
def add_transition(self, from_state, to_state, failure_prob):
self.nodes[from_state][to_state] = failure_prob
def calculate_resilience_score(self, start_state, sequence, circuit_breaker):
"""
Calculate resilience score for a sequence of state transitions
"""
score = 1.0
current_state = start_state
for next_state in sequence:
if not circuit_breaker.check():
return 0.0 # Circuit is tripped, no resilience
failure_prob = self.nodes[current_state].get(next_state, 1.0) # Default 100% failure if unknown transition
score *= (1 - failure_prob) # Reduce score by failure probability
# Record outcome (success or failure)
if random.random() < failure_prob:
circuit_breaker.record_failure()
current_state = next_state
return score
# Example usage
if __name__ == "__main__":
# Initialize graph and circuit breaker
graph = StateGraph()
cb = CircuitBreaker(failure_threshold=0.3, reset_timeout=5)
# Add state transitions with failure probabilities
graph.add_transition('start', 'auth', 0.1)
graph.add_transition('auth', 'data_fetch', 0.2)
graph.add_transition('data_fetch', 'processing', 0.15)
graph.add_transition('processing', 'completion', 0.1)
# Test sequence
sequence = ['auth', 'data_fetch', 'processing', 'completion']
score = graph.calculate_resilience_score('start', sequence, cb)
print(f"Resilience score: {score:.2f}")
print(f"Circuit breaker status: {'Tripped' if cb.is_tripped else 'Operational'}")