File size: 9,991 Bytes
b6ae7b8 | 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | """
Observer System - Logs and analyzes the AI's problem-solving process.
Tracks reasoning patterns, decisions, and outcomes for learning.
"""
import json
import os
from datetime import datetime
from typing import Dict, List, Optional, Any
from pathlib import Path
from collections import defaultdict
import threading
class ReasoningObserver:
"""Observes and logs reasoning processes for later analysis."""
def __init__(self, log_dir: str = None):
if log_dir is None:
log_dir = os.path.join(os.path.dirname(__file__), 'logs')
self.log_dir = Path(log_dir)
self.log_dir.mkdir(exist_ok=True, parents=True)
self.current_session = None
self.current_task = None
self.reasoning_log = []
self.decisions = []
self._lock = threading.Lock()
# In-memory buffer before flushing to disk
self._buffer_size = 10
self._buffer = []
def start_session(self, session_id: str, context: Dict = None):
"""Start observing a new session."""
with self._lock:
self.current_session = {
'session_id': session_id,
'start_time': datetime.utcnow().isoformat(),
'context': context or {},
'tasks': []
}
self.reasoning_log = []
self.decisions = []
def start_task(self, task_id: str, task_type: str, description: str):
"""Start observing a new task within the session."""
with self._lock:
self.current_task = {
'task_id': task_id,
'task_type': task_type,
'description': description,
'start_time': datetime.utcnow().isoformat(),
'steps': [],
'decisions': [],
'outcomes': []
}
def log_reasoning_step(self, step_type: str, content: str,
metadata: Dict = None):
"""Log a reasoning step."""
with self._lock:
step = {
'timestamp': datetime.utcnow().isoformat(),
'type': step_type,
'content': content,
'metadata': metadata or {}
}
self.reasoning_log.append(step)
if self.current_task:
self.current_task['steps'].append(step)
self._buffer.append(step)
if len(self._buffer) >= self._buffer_size:
self._flush_buffer()
def log_decision(self, decision_type: str, choice: str,
alternatives: List[str] = None, rationale: str = None):
"""Log a decision made during problem-solving."""
with self._lock:
decision = {
'timestamp': datetime.utcnow().isoformat(),
'type': decision_type,
'choice': choice,
'alternatives': alternatives or [],
'rationale': rationale
}
self.decisions.append(decision)
if self.current_task:
self.current_task['decisions'].append(decision)
def log_outcome(self, outcome_type: str, result: Any,
success: bool, details: Dict = None):
"""Log the outcome of a step or task."""
with self._lock:
outcome = {
'timestamp': datetime.utcnow().isoformat(),
'type': outcome_type,
'result': str(result),
'success': success,
'details': details or {}
}
if self.current_task:
self.current_task['outcomes'].append(outcome)
# Flush buffer on outcome to ensure logs are saved
if self._buffer:
self._flush_buffer()
def end_task(self, success: bool, summary: str = None,
lessons: List[str] = None):
"""End observing the current task."""
with self._lock:
if self.current_task and self.current_session:
self.current_task['end_time'] = datetime.utcnow().isoformat()
self.current_task['success'] = success
self.current_task['summary'] = summary
self.current_task['lessons'] = lessons
self.current_session['tasks'].append(self.current_task)
# Save task to disk
self._save_task(self.current_task)
self.current_task = None
def end_session(self) -> Dict:
"""End the current session and return summary."""
with self._lock:
if self.current_session:
self.current_session['end_time'] = datetime.utcnow().isoformat()
# Calculate session stats
tasks = self.current_session['tasks']
completed = sum(1 for t in tasks if t.get('success', False))
failed = sum(1 for t in tasks if not t.get('success', True))
self.current_session['stats'] = {
'total_tasks': len(tasks),
'successful': completed,
'failed': failed,
'success_rate': completed / len(tasks) if tasks else 0
}
# Save session
self._save_session(self.current_session)
session = self.current_session
self.current_session = None
return session
return {}
def _flush_buffer(self):
"""Flush buffered log entries to disk."""
if not self._buffer:
return
log_file = self.log_dir / 'observer_buffer.jsonl'
with open(log_file, 'a') as f:
for entry in self._buffer:
f.write(json.dumps(entry) + '\n')
self._buffer = []
def _save_task(self, task: Dict):
"""Save task log to disk."""
task_file = self.log_dir / f"task_{task['task_id']}.json"
with open(task_file, 'w') as f:
json.dump(task, f, indent=2)
def _save_session(self, session: Dict):
"""Save session log to disk."""
session_file = self.log_dir / f"session_{session['session_id']}.json"
with open(session_file, 'w') as f:
json.dump(session, f, indent=2)
def get_current_session(self) -> Optional[Dict]:
"""Get the current session data."""
return self.current_session
def analyze_reasoning_patterns(self, session_id: str = None) -> Dict:
"""Analyze reasoning patterns from logs."""
analysis = {
'reasoning_types': defaultdict(int),
'decision_patterns': defaultdict(int),
'success_patterns': [],
'failure_patterns': []
}
# Load sessions from disk
if session_id:
session_file = self.log_dir / f"session_{session_id}.json"
if session_file.exists():
with open(session_file) as f:
sessions = [json.load(f)]
else:
sessions = []
else:
# Load all sessions
sessions = []
for f in self.log_dir.glob('session_*.json'):
with open(f) as fp:
sessions.append(json.load(fp))
for session in sessions:
for task in session.get('tasks', []):
# Analyze reasoning types
for step in task.get('steps', []):
analysis['reasoning_types'][step.get('type', 'unknown')] += 1
# Analyze decisions
for decision in task.get('decisions', []):
analysis['decision_patterns'][decision.get('type', 'unknown')] += 1
# Separate success and failure patterns
if task.get('success'):
analysis['success_patterns'].append({
'task_type': task.get('task_type'),
'steps': len(task.get('steps', [])),
'decisions': len(task.get('decisions', []))
})
else:
analysis['failure_patterns'].append({
'task_type': task.get('task_type'),
'steps': len(task.get('steps', [])),
'decisions': len(task.get('decisions', []))
})
return dict(analysis)
class ReasoningTracker:
"""Context manager for easy reasoning tracking."""
def __init__(self, observer: ReasoningObserver, task_id: str,
task_type: str, description: str):
self.observer = observer
self.task_id = task_id
self.task_type = task_type
self.description = description
self.success = False
self.summary = None
self.lessons = []
def __enter__(self):
self.observer.start_task(self.task_id, self.task_type, self.description)
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.success = exc_type is None
if exc_type:
self.summary = str(exc_val)
self.observer.end_task(self.success, self.summary, self.lessons)
def add_lesson(self, lesson: str):
"""Add a lesson learned during the task."""
self.lessons.append(lesson)
# Global instance
_observer_instance = None
_instance_lock = threading.Lock()
def get_observer() -> ReasoningObserver:
"""Get or create the global observer instance."""
global _observer_instance
with _instance_lock:
if _observer_instance is None:
_observer_instance = ReasoningObserver()
return _observer_instance |