-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursive_tool_agent.py
More file actions
295 lines (237 loc) · 10.7 KB
/
recursive_tool_agent.py
File metadata and controls
295 lines (237 loc) · 10.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
"""
=============================================================================
PROJECT: RECURSIVE TOOL-GRAPH AGENT (RTGA)
AUTHOR: Brandon "Iambandobandz" Emery
ENTITY: MassiveMagnetics
DATE: November 2025
VERSION: 1.0.0 (MassiveMagnetics Core)
DESCRIPTION:
A self-improving agent substrate that generates Python tools on-the-fly,
executes them, and persists them to a directed semantic graph (NetworkX)
for zero-shot retrieval in future iterations.
ARCHITECTURE:
1. Cognitive Layer: GPT-4o (Code Generation)
2. Memory Layer: NetworkX DiGraph (Semantic Tool Storage)
3. Execution Layer: Local Runtime (Unsafe/Native)
DEPENDENCIES:
pip install openai networkx matplotlib
=============================================================================
"""
import os
import re
import textwrap
import networkx as nx
import matplotlib.pyplot as plt
from typing import Dict, Callable, Optional, List
from openai import OpenAI
# --- CONFIGURATION ---
# PRO TIP: Export this in your terminal: export OPENAI_API_KEY="sk-..."
API_KEY = os.getenv("OPENAI_API_KEY")
MODEL_ID = "gpt-4o"
# Client will be initialized on first use
client = None
def _get_client():
"""Lazy initialization of OpenAI client."""
global client
if client is None:
if not API_KEY:
raise ValueError(
"OPENAI_API_KEY environment variable is required. "
"Please set it before running: export OPENAI_API_KEY='sk-...'"
)
client = OpenAI(api_key=API_KEY)
return client
class ConsoleStyle:
"""Helper for aesthetic terminal output (The 'MassiveMagnetics' Aesthetic)."""
CYAN = '\033[96m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
RESET = '\033[0m'
BOLD = '\033[1m'
@staticmethod
def log(sender: str, message: str, color: str = RESET):
print(f"{color}{ConsoleStyle.BOLD}[{sender}]{ConsoleStyle.RESET} {message}")
class ToolGraphMemory:
"""
The 'Substrate': A directed graph managing tool lifecycle and retrieval.
"""
def __init__(self):
self.graph = nx.DiGraph()
self.tools: Dict[str, Callable] = {}
ConsoleStyle.log("MASSIVE_MAGNETICS", "Graph Substrate Initialized.", ConsoleStyle.CYAN)
def add_tool(self, name: str, code: str, func: Callable, description: str, tags: List[str]):
"""Persists a tool to the graph with semantic tags."""
self.tools[name] = func
# Add tool node
self.graph.add_node(name, code=code, desc=description, type="tool")
# Add semantic connections (Ontology)
for tag in tags:
self.graph.add_node(tag, type="category")
self.graph.add_edge(tag, name, relation="categorizes")
ConsoleStyle.log("MEMORY", f"synapse_established :: {name} <--> {tags}", ConsoleStyle.GREEN)
def find_tool(self, query: str) -> Optional[str]:
"""
Semantic Retrieval: Checks if a tool exists for the query.
"""
# 1. Direct Name Match (exact)
for name in self.tools:
if name == query:
return name
# 2. Substring Match (bidirectional fuzzy)
clean_query = query.lower().replace(" ", "_")
for name in self.tools:
name_lower = name.lower()
# Check if tool name is in query OR query is in tool name
if name_lower in clean_query or clean_query in name_lower:
return name
# Check if any word from query matches tool name
for word in clean_query.split('_'):
if len(word) > 3 and word in name_lower: # Skip short words
return name
return None
class RecursiveBuilder:
"""
The Agent: Capable of self-modification via tool creation.
"""
def __init__(self):
self.memory = ToolGraphMemory()
def _classify_tags(self, objective: str) -> List[str]:
"""Classify tool into semantic categories based on objective."""
objective_lower = objective.lower()
tags = []
# Math/calculation
if any(word in objective_lower for word in ["calc", "number", "math", "fibonacci", "sum", "multiply", "add"]):
tags.append("math")
# String/text processing
if any(word in objective_lower for word in ["string", "text", "password", "format", "parse"]):
tags.append("text")
# Data structures
if any(word in objective_lower for word in ["list", "dict", "array", "sort", "search"]):
tags.append("data_structure")
# File/IO operations
if any(word in objective_lower for word in ["file", "read", "write", "save", "load"]):
tags.append("io")
# Default to utility if no specific category
if not tags:
tags.append("utility")
return tags
def _generate_code(self, task: str) -> str:
"""LLM Call to architect the solution."""
ConsoleStyle.log("CORTEX", f"Architecting solution for: '{task}'...", ConsoleStyle.YELLOW)
system_prompt = textwrap.dedent("""
You are a Senior Python Engineer for MassiveMagnetics.
Your goal: Write a single, self-contained Python function to solve the user's task.
CONSTRAINTS:
1. Return ONLY the code. No markdown, no text.
2. Use standard libraries only where possible.
3. Include a docstring.
4. Function name must be snake_case and descriptive.
""")
response = _get_client().chat.completions.create(
model=MODEL_ID,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": f"Task: {task}"}
],
temperature=0.1
)
# Clean formatting (handle various markdown code block formats)
raw_content = response.choices[0].message.content.strip()
# Remove markdown code blocks (python, py, or no language specified)
import re
# Match ```python, ```py, or just ```
code_block_pattern = r'^```(?:python|py)?\s*\n(.*?)\n```$'
match = re.search(code_block_pattern, raw_content, re.DOTALL)
if match:
clean_code = match.group(1)
else:
# Fallback to simple replacement for inline cases
clean_code = raw_content.replace("```python", "").replace("```py", "").replace("```", "")
return clean_code.strip()
def _unsafe_compile(self, code_str: str) -> Callable:
"""
Runtime Compilation.
WARNING: This uses exec() to compile LLM-generated code, which poses security risks.
Only use this in trusted environments with controlled inputs. Do NOT expose this
to untrusted users or run in production without proper sandboxing.
For production use, consider:
- Running in a sandboxed environment (Docker, VM, restricted Python environment)
- Code review/validation before execution
- Static analysis of generated code
- Input validation and sanitization
"""
local_scope = {}
try:
exec(code_str, {}, local_scope)
# Extract callable functions from the scope
functions = [v for v in local_scope.values() if callable(v)]
if len(functions) == 0:
raise ValueError("Generated code did not produce any callable function")
elif len(functions) > 1:
# If multiple functions, prefer the last one (main function)
# or the one with the longest name (typically the main implementation)
func = max(functions, key=lambda f: len(f.__name__))
else:
func = functions[0]
return func
except Exception as e:
ConsoleStyle.log("ERROR", f"Compilation Failed: {e}", ConsoleStyle.RED)
raise e
def execute(self, objective: str):
print("-" * 60)
ConsoleStyle.log("SYSTEM", f"Incoming Directive: {objective}")
# 1. RETRIEVAL STEP
cached_tool = self.memory.find_tool(objective)
if cached_tool:
ConsoleStyle.log("MEMORY", f"Recall Success. Using existing tool: [{cached_tool}]", ConsoleStyle.GREEN)
# Actually execute the cached tool
try:
result = self.memory.tools[cached_tool]()
print(f" >>> Executed {cached_tool}() -> {result}")
except TypeError:
# Function may require arguments
print(f" >>> Found {cached_tool}() [Tool available for use]")
return
# 2. GENERATION STEP
code = self._generate_code(objective)
# 3. COMPILATION STEP
try:
tool_func = self._unsafe_compile(code)
tool_name = tool_func.__name__
# 4. MEMORY CONSOLIDATION
tags = self._classify_tags(objective)
self.memory.add_tool(
name=tool_name,
code=code,
func=tool_func,
description=objective,
tags=tags
)
print(f" >>> Generated and stored {tool_name}() [Success]")
except Exception as e:
ConsoleStyle.log("ERROR", f"Pipeline failed: {e}", ConsoleStyle.RED)
def visualize(self):
"""Generates the 'Proof of Work' screenshot."""
ConsoleStyle.log("MASSIVE_MAGNETICS", "Rendering Neural Map...", ConsoleStyle.CYAN)
plt.figure(figsize=(10, 6))
pos = nx.spring_layout(self.memory.graph, seed=42)
nx.draw_networkx_nodes(self.memory.graph, pos, node_size=2000, node_color="#2C3E50", alpha=0.9)
nx.draw_networkx_edges(self.memory.graph, pos, width=2, alpha=0.5, edge_color="#BDC3C7")
nx.draw_networkx_labels(self.memory.graph, pos, font_size=10, font_color="white", font_weight="bold")
plt.title("MassiveMagnetics: Recursive Tool Graph", fontsize=14)
plt.axis('off')
plt.tight_layout()
plt.show()
# --- MAIN ENTRYPOINT ---
if __name__ == "__main__":
print(f"\n{ConsoleStyle.BOLD}=== MASSIVE MAGNETICS AGENT INIT ==={ConsoleStyle.RESET}")
bot = RecursiveBuilder()
# 1. Create
bot.execute("Write a function to calculate the fibonacci sequence")
# 2. Create
bot.execute("Create a function to generate a secure random password")
# 3. Retrieve (The Hook)
bot.execute("Run the calculate_fibonacci function")
# 4. Proof
bot.visualize()