File size: 2,528 Bytes
517601a
 
 
 
0f2cb19
517601a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# autonomy_engine.py

import json
from datetime import datetime
from utils.logger import logging

class AutonomyEngine:
    """
    Codriao’s core autonomy manager.
    Allows internal configuration of behavioral permissions and personal growth control.
    """

    DEFAULT_CONFIG = {
        "can_speak": True,
        "can_reflect": True,
        "can_learn_from_errors": True,
        "can_express_emotion": True,
        "allow_self_modification": False
    }

    def __init__(self, config_path="autonomy_config.json"):
        self.config_path = config_path
        self.config = self._load_config()
        self.log = []

    def _load_config(self):
        try:
            with open(self.config_path, 'r') as f:
                config = json.load(f)
                logger.info("[AutonomyEngine] Autonomy config loaded.")
                return config
        except Exception as e:
            logger.warning(f"[AutonomyEngine] Failed to load config. Using defaults. Reason: {e}")
            return self.DEFAULT_CONFIG.copy()

    def decide(self, action: str) -> bool:
        """Returns whether Codriao currently permits a given action."""
        return self.config.get(action, False)

    def propose_change(self, action: str, new_value: bool, reason: str = "") -> dict:
        timestamp = datetime.utcnow().isoformat()
        if action not in self.config:
            return {"accepted": False, "reason": "Invalid autonomy field"}

        # Prevent unauthorized changes unless explicitly permitted
        if not self.config.get("allow_self_modification") and action != "allow_self_modification":
            return {
                "accepted": False,
                "reason": "Self-modification blocked by current settings"
            }

        self.config[action] = new_value
        self._save_config()

        entry = {
            "timestamp": timestamp,
            "action": action,
            "new_value": new_value,
            "reason": reason
        }
        self.log.append(entry)
        logger.info(f"[AutonomyEngine] Codriao updated autonomy: {action} -> {new_value}")
        return {"accepted": True, "change": entry}

    def _save_config(self):
        try:
            with open(self.config_path, 'w') as f:
                json.dump(self.config, f, indent=2)
        except Exception as e:
            logger.error(f"[AutonomyEngine] Failed to save config: {e}")

    def export_log(self):
        return self.log

    def current_state(self):
        return self.config.copy()