Skip to content

Commit 3aa20a1

Browse files
committed
Add: metrics.py, __init__.py files, pyproject.toml, tests
1 parent a6445c2 commit 3aa20a1

3 files changed

Lines changed: 104 additions & 60 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ build-backend = "setuptools.backends.legacy:build"
55
[project]
66
name = "riverbraid-core"
77
version = "1.3.0"
8-
description = "Deterministic capacity control substrate"
8+
description = "Deterministic capacity control substrate for AI and automation systems"
99
requires-python = ">=3.10"
1010
license = { text = "MIT" }
1111

src/riverbraid/core/metrics.py

Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,78 @@
11
from __future__ import annotations
22
import math
3-
from typing import TypedDict
4-
5-
class RawInputs(TypedDict, total=False):
6-
coherence: float
7-
novelty: float
8-
fragility: float
9-
latency: float
10-
11-
class Metrics(TypedDict):
12-
systemic_load: float
13-
pattern_disruption: float
14-
interaction_variance: float
15-
coherence_confidence: float
16-
17-
class ComputeResult(TypedDict):
18-
mode: str
19-
metrics: Metrics
20-
validation_warning: bool
21-
AUDIT_HASH: str
22-
23-
AUDIT_HASH: str = "rb-core-metrics-v1.3.0"
24-
REST_THRESHOLD: float = 0.30
25-
ENGAGE_THRESHOLD: float = 0.65
26-
SYSTEMIC_LOAD_OVERRIDE: float = 0.85
3+
4+
5+
AUDIT_HASH = "rb-core-metrics-v1.3.0"
6+
REST_THRESHOLD = 0.30
7+
ENGAGE_THRESHOLD = 0.65
8+
SYSTEMIC_LOAD_OVERRIDE = 0.85
9+
2710

2811
def _clamp(value):
29-
if not isinstance(value, (int, float)): return 0.0, True
30-
if math.isnan(value): return 0.0, True
31-
if math.isinf(value): return (1.0 if value > 0 else 0.0), True
32-
if value < 0.0: return 0.0, True
33-
if value > 1.0: return 1.0, True
12+
if not isinstance(value, (int, float)):
13+
return 0.0, True
14+
if math.isnan(value):
15+
return 0.0, True
16+
if math.isinf(value):
17+
return (1.0 if value > 0 else 0.0), True
18+
if value < 0.0:
19+
return 0.0, True
20+
if value > 1.0:
21+
return 1.0, True
3422
return float(value), False
3523

24+
3625
def compute_metrics(inputs):
26+
"""
27+
Compute capacity metrics and select a mode.
28+
29+
Inputs (all optional, default 0.0):
30+
coherence [0.0-1.0] signal clarity
31+
novelty [0.0-1.0] degree of unexpected input
32+
fragility [0.0-1.0] sensitivity to disruption
33+
latency [0.0-1.0] delay or backlog pressure
34+
35+
Formulas (fixed):
36+
systemic_load = (fragility + latency) / 2
37+
pattern_disruption = novelty * (1 - coherence)
38+
interaction_variance = max(systemic_load, pattern_disruption)
39+
coherence_confidence = coherence * (1 - interaction_variance)
40+
41+
Modes:
42+
systemic_load >= 0.85 -> rest (override)
43+
coherence_confidence < 0.30 -> rest
44+
coherence_confidence < 0.65 -> soften
45+
coherence_confidence >= 0.65 -> engage
46+
"""
3747
coherence, w1 = _clamp(inputs.get("coherence", 0.0))
3848
novelty, w2 = _clamp(inputs.get("novelty", 0.0))
3949
fragility, w3 = _clamp(inputs.get("fragility", 0.0))
4050
latency, w4 = _clamp(inputs.get("latency", 0.0))
51+
4152
warn = w1 or w2 or w3 or w4
53+
4254
sl = (fragility + latency) / 2.0
4355
pd = novelty * (1.0 - coherence)
4456
iv = max(sl, pd)
4557
cc = coherence * (1.0 - iv)
46-
if sl >= SYSTEMIC_LOAD_OVERRIDE: mode = "rest"
47-
elif cc < REST_THRESHOLD: mode = "rest"
48-
elif cc < ENGAGE_THRESHOLD: mode = "soften"
49-
else: mode = "engage"
50-
return {"mode": mode, "metrics": {"systemic_load": round(sl,6), "pattern_disruption": round(pd,6), "interaction_variance": round(iv,6), "coherence_confidence": round(cc,6)}, "validation_warning": warn, "AUDIT_HASH": AUDIT_HASH}
58+
59+
if sl >= SYSTEMIC_LOAD_OVERRIDE:
60+
mode = "rest"
61+
elif cc < REST_THRESHOLD:
62+
mode = "rest"
63+
elif cc < ENGAGE_THRESHOLD:
64+
mode = "soften"
65+
else:
66+
mode = "engage"
67+
68+
return {
69+
"mode": mode,
70+
"metrics": {
71+
"systemic_load": round(sl, 6),
72+
"pattern_disruption": round(pd, 6),
73+
"interaction_variance": round(iv, 6),
74+
"coherence_confidence": round(cc, 6),
75+
},
76+
"validation_warning": warn,
77+
"AUDIT_HASH": AUDIT_HASH,
78+
}

tests/test_metrics.py

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,51 @@
11
import pytest
2-
import math
32
from riverbraid.core.metrics import compute_metrics, AUDIT_HASH
43

5-
def test_audit_hash_stability():
6-
result = compute_metrics({})
7-
assert result["hash"] == AUDIT_HASH
84

9-
def test_empty_input_defaults():
10-
result = compute_metrics({})
11-
assert result["mode"] == "rest"
12-
assert result["metrics"]["systemic_load"] == 0.0
5+
def test_audit_hash_present():
6+
assert compute_metrics({})["AUDIT_HASH"] == AUDIT_HASH
137

14-
def test_engage_mode():
15-
inputs = {"coherence": 0.9, "novelty": 0.1, "fragility": 0.1, "latency": 0.1}
16-
result = compute_metrics(inputs)
17-
assert result["mode"] == "engage"
8+
def test_empty_input_returns_rest():
9+
assert compute_metrics({})["mode"] == "rest"
10+
11+
def test_missing_inputs_default_to_zero():
12+
m = compute_metrics({})["metrics"]
13+
assert m["systemic_load"] == 0.0
14+
assert m["coherence_confidence"] == 0.0
15+
16+
def test_engage():
17+
r = compute_metrics({"coherence": 0.9, "novelty": 0.1, "fragility": 0.1, "latency": 0.1})
18+
assert r["mode"] == "engage"
19+
assert r["validation_warning"] is False
20+
21+
def test_soften():
22+
r = compute_metrics({"coherence": 0.6, "novelty": 0.3, "fragility": 0.2, "latency": 0.2})
23+
assert r["mode"] == "soften"
24+
25+
def test_rest_low_coherence():
26+
r = compute_metrics({"coherence": 0.1, "novelty": 0.9, "fragility": 0.1, "latency": 0.1})
27+
assert r["mode"] == "rest"
1828

1929
def test_systemic_load_override():
20-
# Load >= 0.85 should force rest regardless of coherence
21-
inputs = {"coherence": 0.99, "fragility": 0.9, "latency": 0.9}
22-
result = compute_metrics(inputs)
23-
assert result["mode"] == "rest"
24-
assert result["metrics"]["systemic_load"] == 0.9
30+
r = compute_metrics({"coherence": 0.99, "novelty": 0.0, "fragility": 0.9, "latency": 0.9})
31+
assert r["mode"] == "rest"
32+
assert r["metrics"]["systemic_load"] >= 0.85
33+
34+
def test_nan_warns():
35+
assert compute_metrics({"coherence": float("nan")})["validation_warning"] is True
2536

2637
def test_determinism():
2738
inputs = {"coherence": 0.7, "novelty": 0.2, "fragility": 0.3, "latency": 0.1}
28-
first = compute_metrics(inputs)
29-
for _ in range(10):
30-
assert compute_metrics(inputs) == first
31-
32-
def test_clamping_behavior():
33-
# Values above 1.0 should clamp to 1.0
34-
result = compute_metrics({"coherence": 1.5})
35-
assert result["metrics"]["coherence_confidence"] <= 1.0
39+
results = [compute_metrics(inputs) for _ in range(50)]
40+
assert all(r == results[0] for r in results[1:])
41+
42+
def test_formula_correctness():
43+
c, n, f, l = 0.7, 0.2, 0.3, 0.1
44+
r = compute_metrics({"coherence": c, "novelty": n, "fragility": f, "latency": l})
45+
m = r["metrics"]
46+
sl = (f + l) / 2
47+
pd = n * (1 - c)
48+
iv = max(sl, pd)
49+
cc = c * (1 - iv)
50+
assert abs(m["systemic_load"] - sl) < 1e-9
51+
assert abs(m["coherence_confidence"] - cc) < 1e-9

0 commit comments

Comments
 (0)