-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemon.py
More file actions
399 lines (341 loc) · 16.7 KB
/
Copy pathdaemon.py
File metadata and controls
399 lines (341 loc) · 16.7 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
"""
Autonomous Daemon — Standalone 24/7 Agent Runner
=================================================
Runs the ReAct agent loop WITHOUT a user terminal.
Start it once:
python daemon.py
The agent will:
1. Generate goals autonomously via LLM
2. Execute each goal using the ReAct loop (real tools)
3. Log every step to logs/daemon.log
4. Sleep, then repeat forever
Stop it with Ctrl+C or by killing the process.
Windows Task Scheduler:
Action: python C:\\path\\to\\daemon.py
Trigger: At startup / On a schedule
"""
import os
import sys
import time
import json
import signal
import logging
import logging.handlers
import random
from datetime import datetime
from pathlib import Path
# ── Bootstrap path ─────────────────────────────────────────────────────────────
ROOT = Path(__file__).parent
sys.path.insert(0, str(ROOT))
# ── Logging Setup ──────────────────────────────────────────────────────────────
LOG_DIR = ROOT / "logs"
LOG_DIR.mkdir(exist_ok=True)
LOG_FILE = LOG_DIR / "daemon.log"
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
handlers=[
logging.handlers.RotatingFileHandler(
LOG_FILE, maxBytes=5_000_000, backupCount=3, encoding="utf-8"
),
logging.StreamHandler(sys.stdout),
],
)
logger = logging.getLogger("Daemon")
# ── Constants ──────────────────────────────────────────────────────────────────
CYCLE_INTERVAL_SECONDS = 120 # How long to sleep between goal cycles
GOAL_GEN_EVERY_N_CYCLES = 5 # Generate new goals every N cycles
MAX_ACTIVE_GOALS = 8 # Never fill DB with more than this
REACT_MAX_STEPS = 8 # ReAct steps per goal
STATE_FILE = ROOT / "daemon_state.json"
# Fallback seed goals used when LLM goal generation fails
SEED_GOALS = [
{
"title": "Research trending AI topics",
"objective": "Search the web for 3 current AI trends and write a summary to knowledge/ai_trends.md",
"priority": 7,
"category": "research",
},
{
"title": "Self-diagnostic check",
"objective": "List all python files in the project, count them, and write a health report to logs/health.txt",
"priority": 6,
"category": "maintenance",
},
{
"title": "Learn about Python best practices",
"objective": "Search for Python clean code best practices and write the top 5 tips to knowledge/python_tips.md",
"priority": 5,
"category": "learning",
},
{
"title": "Write a daily journal entry",
"objective": "Write a short journal entry about the agent's current state, goals, and any observations to logs/journal.txt (append mode)",
"priority": 4,
"category": "creative",
},
]
class AgentDaemon:
"""
Standalone autonomous agent daemon.
Initializes minimal components (LLM, ToolRegistry, ReactAgent),
then loops forever: pick goal → execute via ReAct → log → sleep.
"""
def __init__(self):
self.running = True
self.cycle = 0
self.completed_goals = []
self.active_goals = []
self._load_state()
signal.signal(signal.SIGINT, self._handle_shutdown)
signal.signal(signal.SIGTERM, self._handle_shutdown)
logger.info("=" * 60)
logger.info(" ULTIMATE AI AGENT — AUTONOMOUS DAEMON STARTED")
logger.info(f" PID: {os.getpid()}")
logger.info(f" Log: {LOG_FILE}")
logger.info(f" Cycle interval: {CYCLE_INTERVAL_SECONDS}s")
logger.info("=" * 60)
# ── Initialize components ──────────────────────────────────────────
self.llm = self._init_llm()
self.tools = self._init_tools()
self.react = self._init_react()
# ── Init ──────────────────────────────────────────────────────────────────
def _init_llm(self):
"""Load LLMProvider from config."""
logger.info("Initializing LLM provider...")
try:
from llm_provider import LLMProvider
from config import CONFIG
# Try Groq first (fast, free), then Ollama
groq_key = os.getenv("GROQ_API_KEY") or getattr(CONFIG, "groq", None) and CONFIG.groq.api_key
provider = "hybrid" if groq_key else "ollama"
llm = LLMProvider(provider=provider)
logger.info(f"LLM: {provider}")
return llm
except Exception as e:
logger.error(f"LLM init failed: {e}")
return None
def _init_tools(self):
"""Initialize tool registry with all built-in tools."""
logger.info("Initializing tool registry...")
try:
from tool_registry import ToolRegistry
registry = ToolRegistry()
registry.register_builtins()
logger.info(f"Tools registered: {registry.count}")
# Ensure knowledge/ dir exists for file writing
(ROOT / "knowledge").mkdir(exist_ok=True)
return registry
except Exception as e:
logger.error(f"Tool registry init failed: {e}")
return None
def _init_react(self):
"""Initialize ReactAgent."""
if not self.llm or not self.tools:
logger.error("Cannot init ReactAgent — missing LLM or tools.")
return None
try:
from react_agent import ReactAgent
agent = ReactAgent(
llm_provider=self.llm,
tool_registry=self.tools,
max_steps=REACT_MAX_STEPS,
verbose=True,
)
logger.info("ReactAgent ready.")
return agent
except Exception as e:
logger.error(f"ReactAgent init failed: {e}")
return None
# ── Main Loop ─────────────────────────────────────────────────────────────
def run(self):
"""Main daemon loop — runs forever until SIGINT/SIGTERM."""
if not self.react:
logger.critical("ReactAgent not available. Cannot run daemon.")
return
while self.running:
self.cycle += 1
logger.info(f"\n{'─'*50}")
logger.info(f"CYCLE {self.cycle} — {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
logger.info(f"Active goals: {len(self.active_goals)} | Completed: {len(self.completed_goals)}")
try:
# ── Generate new goals if needed ──────────────────────
if self.cycle % GOAL_GEN_EVERY_N_CYCLES == 1 or not self.active_goals:
self._generate_goals()
# ── Pick and execute a goal ────────────────────────────
goal = self._pick_goal()
if goal:
self._execute_goal(goal)
else:
logger.info("No goals available. Adding a seed goal.")
self.active_goals.extend(SEED_GOALS[:2])
# ── Save state ─────────────────────────────────────────
self._save_state()
except Exception as e:
logger.error(f"Cycle error: {e}", exc_info=True)
# ── Sleep until next cycle ─────────────────────────────────
if self.running:
logger.info(f"Sleeping {CYCLE_INTERVAL_SECONDS}s until next cycle...")
for _ in range(CYCLE_INTERVAL_SECONDS):
if not self.running:
break
time.sleep(1)
logger.info("Daemon stopped cleanly.")
# ── Goal Management ───────────────────────────────────────────────────────
def _generate_goals(self):
"""Ask LLM to generate 2-3 new goals based on current state."""
if len(self.active_goals) >= MAX_ACTIVE_GOALS:
logger.info(f"Goal cap ({MAX_ACTIVE_GOALS}) reached. Skipping generation.")
return
logger.info("Generating new goals via LLM...")
now = datetime.now()
completed_titles = [g.get("title", "") for g in self.completed_goals[-5:]]
active_titles = [g.get("title", "") for g in self.active_goals[:5]]
prompt = f"""You are an autonomous AI agent running 24/7.
Generate 2-3 concrete, executable goals for yourself right now.
Current time: {now.strftime('%Y-%m-%d %H:%M')} (Hour: {now.hour})
Active goals already queued: {active_titles or ['none']}
Recently completed: {completed_titles or ['none']}
Goal ideas: research a topic and write it to a file, run self-diagnostics,
learn something new, write a summary or report, check current events.
IMPORTANT: Goals must be concrete and verifiable — e.g. "Search web for X and write results to Y file".
Return ONLY a JSON array:
[
{{
"title": "Short title",
"objective": "Specific thing to do — which tool to use, what file to write, what to search for",
"priority": 7,
"category": "research"
}}
]
"""
try:
resp = self.llm.call(prompt, max_tokens=600, temperature=0.7)
goals = self._parse_json_goals(resp)
if goals:
self.active_goals.extend(goals)
for g in goals:
logger.info(f" 🎯 Generated: [{g.get('category','?')}] {g.get('title','?')} (P{g.get('priority',5)})")
else:
logger.warning("LLM goal generation returned no valid goals. Using seed.")
seed = random.choice(SEED_GOALS).copy()
self.active_goals.append(seed)
except Exception as e:
logger.error(f"Goal generation failed: {e}. Using seed goal.")
self.active_goals.append(random.choice(SEED_GOALS).copy())
def _pick_goal(self):
"""Return the highest-priority pending goal."""
pending = [g for g in self.active_goals
if g.get("status", "pending") == "pending"]
if not pending:
return None
pending.sort(key=lambda g: -g.get("priority", 5))
return pending[0]
def _execute_goal(self, goal: dict):
"""Run a goal through the ReAct loop and record the result."""
title = goal.get("title", "Untitled")
objective = goal.get("objective", title)
logger.info(f"\n⚡ EXECUTING: {title}")
logger.info(f" Objective: {objective}")
# Mark as running
goal["status"] = "running"
goal["started_at"] = datetime.now().isoformat()
try:
result = self.react.run(objective)
if result["success"]:
goal["status"] = "completed"
goal["result"] = result["result"]
goal["completed_at"] = datetime.now().isoformat()
self.completed_goals.append(goal)
self.active_goals = [g for g in self.active_goals if g is not goal]
logger.info(f"✅ COMPLETED in {result['steps']} steps ({result['elapsed_s']}s): {result['result'][:150]}")
else:
goal["status"] = "failed"
goal["result"] = result["result"]
# Keep failed goals with status=failed so we don't retry forever
logger.warning(f"❌ FAILED after {result['steps']} steps: {result['result'][:150]}")
# Write execution transcript to logs
self._write_transcript(title, result)
except Exception as e:
goal["status"] = "error"
goal["result"] = str(e)
logger.error(f"Goal execution error: {e}", exc_info=True)
def _write_transcript(self, title: str, result: dict):
"""Append a goal execution transcript to the log file."""
try:
transcript_dir = ROOT / "logs" / "transcripts"
transcript_dir.mkdir(exist_ok=True)
safe_title = "".join(c if c.isalnum() or c in "-_" else "_" for c in title)[:40]
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
path = transcript_dir / f"{ts}_{safe_title}.json"
with open(path, "w", encoding="utf-8") as f:
json.dump({"goal": title, **result}, f, indent=2, default=str)
except Exception as e:
logger.warning(f"Could not write transcript: {e}")
# ── Persistence ───────────────────────────────────────────────────────────
def _save_state(self):
"""Save daemon state to JSON for crash recovery."""
try:
state = {
"cycle": self.cycle,
"active_goals": self.active_goals,
"completed_goals": self.completed_goals[-20:], # keep last 20
"saved_at": datetime.now().isoformat(),
}
with open(STATE_FILE, "w", encoding="utf-8") as f:
json.dump(state, f, indent=2, default=str)
except Exception as e:
logger.warning(f"Could not save state: {e}")
def _load_state(self):
"""Restore state from previous run if available."""
try:
if STATE_FILE.exists():
with open(STATE_FILE, "r", encoding="utf-8") as f:
state = json.load(f)
self.cycle = state.get("cycle", 0)
# Only restore pending goals — don't re-run completed/failed ones
self.active_goals = [
g for g in state.get("active_goals", [])
if g.get("status", "pending") == "pending"
]
self.completed_goals = state.get("completed_goals", [])
logger.info(f"Restored state: cycle={self.cycle}, "
f"pending={len(self.active_goals)}, "
f"completed={len(self.completed_goals)}")
except Exception as e:
logger.warning(f"Could not load state: {e}. Starting fresh.")
self.cycle = 0
self.active_goals = []
self.completed_goals = []
# ── Shutdown ──────────────────────────────────────────────────────────────
def _handle_shutdown(self, signum, frame):
logger.info(f"\nShutdown signal received (signal {signum}). Stopping after current cycle...")
self.running = False
# ── Helpers ───────────────────────────────────────────────────────────────
def _parse_json_goals(self, text: str) -> list:
"""Extract a JSON array of goals from LLM response."""
text = text.strip()
# Try direct parse
try:
res = json.loads(text)
return res if isinstance(res, list) else [res]
except Exception:
pass
# Find first [...] block
start, end = text.find("["), text.rfind("]")
if start != -1 and end > start:
try:
return json.loads(text[start:end + 1])
except Exception:
pass
return []
# ── Entry Point ───────────────────────────────────────────────────────────────
if __name__ == "__main__":
print("""
╔══════════════════════════════════════════════════╗
║ ULTIMATE AI AGENT — AUTONOMOUS DAEMON ║
║ Real ReAct loop. Real tools. No babysitting. ║
╚══════════════════════════════════════════════════╝
""")
daemon = AgentDaemon()
daemon.run()