commit 88e2de1e3effb61227e848527dbdeafa17008371
parent 99521657e064ffb74bde06282d604a0ebd74634b
Author: triesap <tyson@radroots.org>
Date: Mon, 21 Sep 2026 13:43:14 +0000
provider: add circuit and readiness state
Diffstat:
2 files changed, 57 insertions(+), 0 deletions(-)
diff --git a/src/hyf_provider/jev_circuit.mojo b/src/hyf_provider/jev_circuit.mojo
@@ -0,0 +1,36 @@
+
+
+@fieldwise_init
+struct CircuitState(Copyable, Movable):
+ var open: Bool
+ var consecutive_failures: Int
+ var threshold: Int
+
+
+def circuit_state(threshold: Int) raises -> CircuitState:
+ if threshold <= 0:
+ raise Error("circuit threshold must be positive")
+ return CircuitState(open=False, consecutive_failures=0, threshold=threshold)
+
+
+def circuit_record_success(state: CircuitState) -> CircuitState:
+ return CircuitState(
+ open=False, consecutive_failures=0, threshold=state.threshold
+ )
+
+
+def circuit_record_failure(state: CircuitState) -> CircuitState:
+ var failures = state.consecutive_failures + 1
+ return CircuitState(
+ open=failures >= state.threshold,
+ consecutive_failures=failures,
+ threshold=state.threshold,
+ )
+
+
+def circuit_allows(state: CircuitState) -> Bool:
+ return not state.open
+
+
+def process_liveness_is_provider_readiness() -> Bool:
+ return False
diff --git a/tests/test_jev.mojo b/tests/test_jev.mojo
@@ -221,3 +221,24 @@ def test_retry_classification_and_bounded_scheduling() raises:
assert_true(not should_retry(policy, 0, 950, True))
with assert_raises():
_ = retry_policy(1, 0, 10, 100)
+
+
+from hyf_provider.jev_circuit import (
+ circuit_allows,
+ circuit_record_failure,
+ circuit_record_success,
+ circuit_state,
+ process_liveness_is_provider_readiness,
+)
+
+
+def test_circuit_opens_and_recovers() raises:
+ var state = circuit_state(2)
+ assert_true(circuit_allows(state))
+ state = circuit_record_failure(state)
+ assert_true(circuit_allows(state))
+ state = circuit_record_failure(state)
+ assert_true(not circuit_allows(state))
+ state = circuit_record_success(state)
+ assert_true(circuit_allows(state))
+ assert_true(not process_liveness_is_provider_readiness())