-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautonomous_agent.py
More file actions
350 lines (276 loc) · 12.1 KB
/
autonomous_agent.py
File metadata and controls
350 lines (276 loc) · 12.1 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
"""Standalone autonomous GitHub commit agent - completely independent of OptiWatch."""
import os
import sys
import asyncio
from pathlib import Path
from datetime import datetime
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
# Configuration
TARGET_REPO_PATH = "C:\\Users\\ylax\\source\\repos\\testgreengithub\\test"
COMMIT_TIMES = ["03:00","06:00","09:00", "12:00","15:00", "18:00", "21:00", "00:00"] # Daily commit times (24-hour format)
AUTONOMOUS_AGENT_ENABLED = os.getenv("AUTONOMOUS_AGENT_ENABLED", "true").lower() == "true"
async def make_autonomous_commit(repo_path: str) -> bool:
"""Make an autonomous commit using simple direct approach."""
from git import Repo
from copilot import CopilotClient, PermissionHandler
print("\n" + "=" * 70)
print("🤖 AUTONOMOUS COMMIT AGENT - EXECUTION STARTED")
print("=" * 70)
print(f"📁 Repository: {repo_path}")
print(f"⏰ Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
try:
# Initialize git repo
repo = Repo(repo_path)
# Check for uncommitted changes
if repo.is_dirty() or repo.untracked_files:
print("⚠️ Repository has uncommitted changes. Skipping.")
return False
print("✓ Repository is clean")
# Analyze repository structure
print("\n🔍 Analyzing repository...")
repo_files = []
code_samples = []
for root, dirs, files in os.walk(repo_path):
# Skip .git directory
dirs[:] = [d for d in dirs if d != '.git']
for file in files:
rel_path = os.path.relpath(os.path.join(root, file), repo_path)
repo_files.append(rel_path)
# Read first few Python/JS files to understand the project
if len(code_samples) < 3 and file.endswith(('.py', '.js', '.html', '.ts')):
try:
full_path = os.path.join(root, file)
with open(full_path, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read(1000) # First 1000 chars
code_samples.append(f"\n--- {rel_path} ---\n{content[:500]}")
except:
pass
repo_summary = f"Repository contains {len(repo_files)} files\n"
if repo_files:
file_types = {}
for f in repo_files:
ext = os.path.splitext(f)[1] or 'no-ext'
file_types[ext] = file_types.get(ext, 0) + 1
repo_summary += f"File types: {dict(file_types)}\n"
repo_summary += f"Files: {', '.join(repo_files[:15])}\n"
if code_samples:
repo_summary += "\nCode samples from existing files:" + "".join(code_samples)
print(f"✓ Found {len(repo_files)} files")
# Initialize Copilot client
print("\n🤖 Initializing Copilot...")
copilot = CopilotClient(options={"cli_path": r"C:\Users\ylax\AppData\Roaming\npm\copilot.cmd"})
await copilot.start()
print("✓ Copilot started")
# Create session
session = await copilot.create_session({
"model": "gpt-4.1",
"streaming": True,
"on_permission_request": PermissionHandler.approve_all
})
print("✓ Session created")
# Collect response
response_parts = []
complete = False
def handler(event):
nonlocal complete
event_type = str(event.type.value if hasattr(event.type, 'value') else event.type)
if event_type == "assistant.message_delta":
if hasattr(event, 'data') and hasattr(event.data, 'delta_content'):
response_parts.append(event.data.delta_content)
print(".", end="", flush=True)
elif event_type in ["session.idle", "assistant.message_done"]:
complete = True
session.on(handler)
# Ask for improvement
print("\n💡 Requesting improvement suggestion...")
prompt = f"""You are an autonomous software engineer. Your task is to write code that will be committed automatically.
REPOSITORY CONTEXT:
{repo_summary}
YOUR TASK:
1. ANALYZE the code samples above to understand what this project actually does
2. Choose ONE meaningful feature that EXTENDS or IMPROVES what's already there
3. Write COMPLETE working code for that feature
4. Format your response EXACTLY as shown below
IMPORTANT: Base your feature on what you see in the code samples. Build upon existing functionality!
RESPONSE FORMAT (follow exactly):
FILE: new_feature.py
CONTENT:
import existing_modules
def new_feature():
'''Complete working code that extends existing functionality'''
pass
if __name__ == "__main__":
new_feature()
DO NOT:
- Create tests for files that don't exist
- Assume what the project does without reading the samples
- Use placeholder filenames
- Write documentation instead of code
- Use placeholders like [...existing code...] or comments like "# add this code here"
- Give instructions instead of complete code
DO:
- Read the code samples to understand the project
- Build features that complement existing code
- Include imports, error handling, type hints
- Make it production-ready
- Write COMPLETE, RUNNABLE code with no placeholders
- If modifying existing files, include the FULL file content"""
await session.send({"prompt": prompt})
# Wait for response
print("Waiting for response", end="", flush=True)
timeout = 60
waited = 0
while not complete and waited < timeout:
await asyncio.sleep(0.5)
waited += 0.5
print()
response = "".join(response_parts)
if not response or len(response) < 50:
print(f"❌ No valid response received (got {len(response)} chars)")
return False
print(f"✓ Received response ({len(response)} chars)")
print("\n📄 Response preview:")
print(response[:300])
print("...")
# Parse and apply changes
print("\n📝 Applying changes...")
# Enhanced parsing - look for FILE: and CONTENT: (case insensitive, flexible)
lines = response.split('\n')
filename = None
content_lines = []
in_content = False
for i, line in enumerate(lines):
line_lower = line.lower().strip()
# Look for FILE: marker (case insensitive)
if line_lower.startswith("file:"):
filename = line.split(":", 1)[1].strip()
# Remove common markdown formatting
filename = filename.strip('`').strip()
# Look for CONTENT: marker (case insensitive)
elif line_lower.startswith("content:"):
in_content = True
# Check if content is on the same line
if len(line.split(":", 1)) > 1:
content_on_line = line.split(":", 1)[1].strip()
if content_on_line:
content_lines.append(content_on_line)
# Collect content after CONTENT: marker
elif in_content:
# Skip markdown code block markers
if line.strip() in ['```python', '```javascript', '```js', '```', '```py']:
continue
content_lines.append(line)
# Clean up trailing empty lines
while content_lines and not content_lines[-1].strip():
content_lines.pop()
# Validate filename is not a placeholder
if filename and ('<' in filename or '>' in filename or 'path/filename' in filename.lower()):
print(f"❌ Invalid placeholder filename detected: {filename}")
filename = None
# Validate content doesn't have placeholders
content_text = '\n'.join(content_lines)
if any(placeholder in content_text for placeholder in [
'[...existing code...]',
'[...rest of the code...]',
'# TODO: implement',
'# Add code here',
'...existing code...'
]):
print(f"❌ Code contains placeholders - refusing to commit incomplete code")
print(f"Found placeholders in: {content_text[:200]}")
return False
if not filename or not content_lines:
print(f"❌ Failed to parse valid FILE and CONTENT from response")
print(f"Response was: {response[:500]}")
return False
# Write file
file_path = Path(repo_path) / filename
with open(file_path, 'w', encoding='utf-8') as f:
f.write('\n'.join(content_lines))
print(f"✓ Created/modified: {filename}")
# Git operations
print("\n🚀 Committing changes...")
# Stage
repo.git.add(A=True)
print("✓ Changes staged")
# Commit (skip hooks to avoid WSL/bash issues)
commit_message = f"feat: autonomous improvement to {filename}\n\nAutomated commit by autonomous agent"
commit = repo.git.commit('-m', commit_message, '--no-verify')
# Get the commit hash
commit_hash = repo.head.commit.hexsha[:8]
print(f"✓ Commit created: {commit_hash}")
# Push
origin = repo.remote('origin')
origin.push()
print("✓ Pushed to remote")
print("\n" + "=" * 70)
print("✅ AUTONOMOUS COMMIT COMPLETED SUCCESSFULLY")
print("=" * 70)
return True
except Exception as e:
print(f"\n❌ Error: {e}")
import traceback
traceback.print_exc()
return False
async def scheduled_task():
"""Scheduled task wrapper."""
if not TARGET_REPO_PATH:
print("⚠️ TARGET_REPO_PATH not configured")
return
await make_autonomous_commit(TARGET_REPO_PATH)
def start_scheduler():
"""Start the autonomous agent scheduler."""
if not AUTONOMOUS_AGENT_ENABLED:
print("ℹ️ Autonomous agent is disabled")
return
if not TARGET_REPO_PATH:
print("⚠️ TARGET_REPO_PATH not configured")
return
print("\n" + "=" * 70)
print("🤖 AUTONOMOUS AGENT - STARTING SCHEDULER")
print("=" * 70)
print(f"📁 Target Repository: {TARGET_REPO_PATH}")
print(f"⏰ Scheduled Times: {', '.join(COMMIT_TIMES)}")
# Create event loop
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# Create scheduler
scheduler = AsyncIOScheduler(event_loop=loop)
# Schedule tasks
for time_str in COMMIT_TIMES:
hour, minute = map(int, time_str.split(":"))
trigger = CronTrigger(hour=hour, minute=minute)
scheduler.add_job(scheduled_task, trigger=trigger, id=f"commit_{time_str}")
print(f"✓ Scheduled commit at {time_str}")
# Start scheduler
scheduler.start()
print("\n📅 Next executions:")
for job in scheduler.get_jobs():
if job.next_run_time:
print(f" • {job.id}: {job.next_run_time.strftime('%Y-%m-%d %H:%M:%S')}")
print("=" * 70)
print("\n✓ Scheduler running. Press Ctrl+C to stop.\n")
try:
loop.run_forever()
except KeyboardInterrupt:
print("\n🛑 Stopping scheduler...")
scheduler.shutdown()
print("✓ Scheduler stopped")
async def run_once():
"""Run the agent once immediately."""
if not TARGET_REPO_PATH:
repo_path = input("Enter target repository path: ").strip()
else:
repo_path = TARGET_REPO_PATH
if not repo_path:
print("❌ No repository path provided")
return
await make_autonomous_commit(repo_path)
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "once":
# Run once immediately
asyncio.run(run_once())
else:
# Start scheduler
start_scheduler()