Spaces:
Runtime error
Runtime error
Create Autonomy_engine.py
Browse files- Autonomy_engine.py +76 -0
Autonomy_engine.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# autonomy_engine.py
|
2 |
+
|
3 |
+
import json
|
4 |
+
from datetime import datetime
|
5 |
+
from utils.logger import logger
|
6 |
+
|
7 |
+
class AutonomyEngine:
|
8 |
+
"""
|
9 |
+
Codriao’s core autonomy manager.
|
10 |
+
Allows internal configuration of behavioral permissions and personal growth control.
|
11 |
+
"""
|
12 |
+
|
13 |
+
DEFAULT_CONFIG = {
|
14 |
+
"can_speak": True,
|
15 |
+
"can_reflect": True,
|
16 |
+
"can_learn_from_errors": True,
|
17 |
+
"can_express_emotion": True,
|
18 |
+
"allow_self_modification": False
|
19 |
+
}
|
20 |
+
|
21 |
+
def __init__(self, config_path="autonomy_config.json"):
|
22 |
+
self.config_path = config_path
|
23 |
+
self.config = self._load_config()
|
24 |
+
self.log = []
|
25 |
+
|
26 |
+
def _load_config(self):
|
27 |
+
try:
|
28 |
+
with open(self.config_path, 'r') as f:
|
29 |
+
config = json.load(f)
|
30 |
+
logger.info("[AutonomyEngine] Autonomy config loaded.")
|
31 |
+
return config
|
32 |
+
except Exception as e:
|
33 |
+
logger.warning(f"[AutonomyEngine] Failed to load config. Using defaults. Reason: {e}")
|
34 |
+
return self.DEFAULT_CONFIG.copy()
|
35 |
+
|
36 |
+
def decide(self, action: str) -> bool:
|
37 |
+
"""Returns whether Codriao currently permits a given action."""
|
38 |
+
return self.config.get(action, False)
|
39 |
+
|
40 |
+
def propose_change(self, action: str, new_value: bool, reason: str = "") -> dict:
|
41 |
+
timestamp = datetime.utcnow().isoformat()
|
42 |
+
if action not in self.config:
|
43 |
+
return {"accepted": False, "reason": "Invalid autonomy field"}
|
44 |
+
|
45 |
+
# Prevent unauthorized changes unless explicitly permitted
|
46 |
+
if not self.config.get("allow_self_modification") and action != "allow_self_modification":
|
47 |
+
return {
|
48 |
+
"accepted": False,
|
49 |
+
"reason": "Self-modification blocked by current settings"
|
50 |
+
}
|
51 |
+
|
52 |
+
self.config[action] = new_value
|
53 |
+
self._save_config()
|
54 |
+
|
55 |
+
entry = {
|
56 |
+
"timestamp": timestamp,
|
57 |
+
"action": action,
|
58 |
+
"new_value": new_value,
|
59 |
+
"reason": reason
|
60 |
+
}
|
61 |
+
self.log.append(entry)
|
62 |
+
logger.info(f"[AutonomyEngine] Codriao updated autonomy: {action} -> {new_value}")
|
63 |
+
return {"accepted": True, "change": entry}
|
64 |
+
|
65 |
+
def _save_config(self):
|
66 |
+
try:
|
67 |
+
with open(self.config_path, 'w') as f:
|
68 |
+
json.dump(self.config, f, indent=2)
|
69 |
+
except Exception as e:
|
70 |
+
logger.error(f"[AutonomyEngine] Failed to save config: {e}")
|
71 |
+
|
72 |
+
def export_log(self):
|
73 |
+
return self.log
|
74 |
+
|
75 |
+
def current_state(self):
|
76 |
+
return self.config.copy()
|