|
1 | 1 | from __future__ import annotations |
2 | 2 | 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 | + |
27 | 10 |
|
28 | 11 | 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 |
34 | 22 | return float(value), False |
35 | 23 |
|
| 24 | + |
36 | 25 | 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 | + """ |
37 | 47 | coherence, w1 = _clamp(inputs.get("coherence", 0.0)) |
38 | 48 | novelty, w2 = _clamp(inputs.get("novelty", 0.0)) |
39 | 49 | fragility, w3 = _clamp(inputs.get("fragility", 0.0)) |
40 | 50 | latency, w4 = _clamp(inputs.get("latency", 0.0)) |
| 51 | + |
41 | 52 | warn = w1 or w2 or w3 or w4 |
| 53 | + |
42 | 54 | sl = (fragility + latency) / 2.0 |
43 | 55 | pd = novelty * (1.0 - coherence) |
44 | 56 | iv = max(sl, pd) |
45 | 57 | 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 | + } |
0 commit comments