Spaces:
Running
Running
deploy: update app/session_store.py
Browse files- app/session_store.py +42 -42
app/session_store.py
CHANGED
|
@@ -1,42 +1,42 @@
|
|
| 1 |
-
"""
|
| 2 |
-
app/session_store.py — Global confidence history store for anti-gaming detection.
|
| 3 |
-
|
| 4 |
-
Moved here (instead of app/main.py) to avoid circular imports between
|
| 5 |
-
app/main.py and app/environment.py.
|
| 6 |
-
"""
|
| 7 |
-
from __future__ import annotations
|
| 8 |
-
|
| 9 |
-
from collections import deque
|
| 10 |
-
from threading import Lock
|
| 11 |
-
|
| 12 |
-
_global_confidence_history: deque = deque(maxlen=500) # last 500 episodes, all sessions
|
| 13 |
-
_confidence_history_lock = Lock()
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def record_episode_confidence(confidence: str) -> list[dict]:
|
| 17 |
-
"""Thread-safe append to global confidence history.
|
| 18 |
-
|
| 19 |
-
Returns a snapshot of the current history for gaming detection.
|
| 20 |
-
This is called from environment.py after every terminal action.
|
| 21 |
-
"""
|
| 22 |
-
with _confidence_history_lock:
|
| 23 |
-
_global_confidence_history.append({"confidence": confidence})
|
| 24 |
-
return list(_global_confidence_history)
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def get_confidence_distribution() -> dict:
|
| 28 |
-
"""Return current confidence distribution across all sessions."""
|
| 29 |
-
with _confidence_history_lock:
|
| 30 |
-
history = list(_global_confidence_history)
|
| 31 |
-
total = len(history)
|
| 32 |
-
if total == 0:
|
| 33 |
-
return {"episodes_recorded": 0, "distribution": {}}
|
| 34 |
-
return {
|
| 35 |
-
"episodes_recorded": total,
|
| 36 |
-
"distribution": {
|
| 37 |
-
"HIGH": round(sum(1 for e in history if e["confidence"] == "HIGH") / total, 3),
|
| 38 |
-
"MED": round(sum(1 for e in history if e["confidence"] == "MED") / total, 3),
|
| 39 |
-
"LOW": round(sum(1 for e in history if e["confidence"] == "LOW") / total, 3),
|
| 40 |
-
},
|
| 41 |
-
"gaming_detection_active": total >= 10,
|
| 42 |
-
}
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
app/session_store.py — Global confidence history store for anti-gaming detection.
|
| 3 |
+
|
| 4 |
+
Moved here (instead of app/main.py) to avoid circular imports between
|
| 5 |
+
app/main.py and app/environment.py.
|
| 6 |
+
"""
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
|
| 9 |
+
from collections import deque
|
| 10 |
+
from threading import Lock
|
| 11 |
+
|
| 12 |
+
_global_confidence_history: deque = deque(maxlen=500) # last 500 episodes, all sessions
|
| 13 |
+
_confidence_history_lock = Lock()
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def record_episode_confidence(confidence: str) -> list[dict]:
|
| 17 |
+
"""Thread-safe append to global confidence history.
|
| 18 |
+
|
| 19 |
+
Returns a snapshot of the current history for gaming detection.
|
| 20 |
+
This is called from environment.py after every terminal action.
|
| 21 |
+
"""
|
| 22 |
+
with _confidence_history_lock:
|
| 23 |
+
_global_confidence_history.append({"confidence": confidence})
|
| 24 |
+
return list(_global_confidence_history)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def get_confidence_distribution() -> dict:
|
| 28 |
+
"""Return current confidence distribution across all sessions."""
|
| 29 |
+
with _confidence_history_lock:
|
| 30 |
+
history = list(_global_confidence_history)
|
| 31 |
+
total = len(history)
|
| 32 |
+
if total == 0:
|
| 33 |
+
return {"episodes_recorded": 0, "distribution": {}}
|
| 34 |
+
return {
|
| 35 |
+
"episodes_recorded": total,
|
| 36 |
+
"distribution": {
|
| 37 |
+
"HIGH": round(sum(1 for e in history if e["confidence"] == "HIGH") / total, 3),
|
| 38 |
+
"MED": round(sum(1 for e in history if e["confidence"] == "MED") / total, 3),
|
| 39 |
+
"LOW": round(sum(1 for e in history if e["confidence"] == "LOW") / total, 3),
|
| 40 |
+
},
|
| 41 |
+
"gaming_detection_active": total >= 10,
|
| 42 |
+
}
|