LordXido commited on
Commit
4dec111
·
verified ·
1 Parent(s): f1fe93a

Create app/logic.py

Browse files
Files changed (1) hide show
  1. app/logic.py +50 -0
app/logic.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import hashlib
2
+ import time
3
+ from app.vault import store_reflex
4
+
5
+ HIGH = ["override", "surge", "breach", "failure"]
6
+ MED = ["delay", "unstable", "degrade"]
7
+
8
+ def classify(score: float) -> str:
9
+ if score >= 0.8:
10
+ return "HALT"
11
+ if score >= 0.4:
12
+ return "MONITOR"
13
+ return "STABLE"
14
+
15
+ def run_reflex(req):
16
+ ts = time.strftime("%Y-%m-%d %H:%M:%S")
17
+ norm = req.scenario.lower().strip()
18
+ h = hashlib.sha256(norm.encode()).hexdigest()
19
+
20
+ if any(k in norm for k in HIGH):
21
+ score = 0.92
22
+ elif any(k in norm for k in MED):
23
+ score = 0.55
24
+ else:
25
+ score = 0.18
26
+
27
+ state = classify(score)
28
+
29
+ action = {
30
+ "HALT": "HALT_SIGNAL",
31
+ "MONITOR": "ESCALATE_MONITORING",
32
+ "STABLE": "CONTINUE_OPERATION"
33
+ }[state]
34
+
35
+ size = store_reflex(
36
+ timestamp=ts,
37
+ scenario_hash=h,
38
+ score=score,
39
+ state=state,
40
+ source=req.source
41
+ )
42
+
43
+ return {
44
+ "timestamp": ts,
45
+ "scenario_hash": h,
46
+ "anomaly_score": score,
47
+ "system_state": state,
48
+ "action": action,
49
+ "vault_size": size
50
+ }