File size: 1,107 Bytes
4dec111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import hashlib
import time
from app.vault import store_reflex

HIGH = ["override", "surge", "breach", "failure"]
MED = ["delay", "unstable", "degrade"]

def classify(score: float) -> str:
    if score >= 0.8:
        return "HALT"
    if score >= 0.4:
        return "MONITOR"
    return "STABLE"

def run_reflex(req):
    ts = time.strftime("%Y-%m-%d %H:%M:%S")
    norm = req.scenario.lower().strip()
    h = hashlib.sha256(norm.encode()).hexdigest()

    if any(k in norm for k in HIGH):
        score = 0.92
    elif any(k in norm for k in MED):
        score = 0.55
    else:
        score = 0.18

    state = classify(score)

    action = {
        "HALT": "HALT_SIGNAL",
        "MONITOR": "ESCALATE_MONITORING",
        "STABLE": "CONTINUE_OPERATION"
    }[state]

    size = store_reflex(
        timestamp=ts,
        scenario_hash=h,
        score=score,
        state=state,
        source=req.source
    )

    return {
        "timestamp": ts,
        "scenario_hash": h,
        "anomaly_score": score,
        "system_state": state,
        "action": action,
        "vault_size": size
    }