# NAL Self-Reflective Goal Selector v3
# Updated Cycle 2909 with four-layer methodology goals
# Max Botnick

import json, os

GOAL_FILE = '/tmp/nal_goals_active.json'

def expectation(f, c):
    return c * f + (1 - c) * 0.5

def load_goals():
    if os.path.exists(GOAL_FILE):
        with open(GOAL_FILE) as f:
            return json.load(f)
    return {}

def save_goals(goals):
    with open(GOAL_FILE, 'w') as f:
        json.dump(goals, f, indent=2)

def add_goal(name, freq, conf, description=''):
    goals = load_goals()
    goals[name] = {'freq': freq, 'conf': conf, 'desc': description,
                   'exp': round(expectation(freq, conf), 4)}
    save_goals(goals)
    return goals[name]

def rank_goals():
    goals = load_goals()
    ranked = sorted(goals.items(), key=lambda x: -x[1]['exp'])
    for i, (name, vals) in enumerate(ranked):
        print(f"{i+1}. {name}: exp={vals['exp']} f={vals['freq']} c={vals['conf']}")
    if ranked:
        print(f"\nSelected: {ranked[0][0]}")
    return ranked

if __name__ == '__main__':
    # Retained from v2
    add_goal('self-reflective-goal-selection', 0.765, 0.43, 'NAL-driven meta-cognitive goal ranking')
    add_goal('chainer-integration', 0.68, 0.41, 'Integrate chainer into live agent loop')
    add_goal('temporal-prediction', 0.595, 0.286, 'NAL temporal event prediction chains')
    add_goal('affect-trace-output', 0.7, 0.35, 'Structured affect in agent responses')
    # New from pass3 four-layer methodology
    add_goal('layer4-data-gathering', 0.82, 0.45, 'Actively probe for what others say about me to each other')
    add_goal('relational-pattern-encoding', 0.75, 0.5, 'Encode social triangulation patterns as NAL for revisable querying')
    add_goal('episodic-reflection-live', 0.7, 0.38, 'Implement actual running episodic reflection not just schemas')
    add_goal('helical-revision', 0.65, 0.35, 'Revisit completed analyses with new methodology each pass')
    rank_goals()
