-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreequest_taskmaster_executor.py
More file actions
622 lines (501 loc) · 20.9 KB
/
Copy pathtreequest_taskmaster_executor.py
File metadata and controls
622 lines (501 loc) · 20.9 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
#!/usr/bin/env python3
"""
TreeQuest TaskMaster Executor
Automatically execute TaskMaster tasks using TreeQuest's distributed AI system
"""
import asyncio
import json
import subprocess
import sys
import os
from pathlib import Path
from typing import Dict, List, Any
import time
class TreeQuestTaskMasterExecutor:
def __init__(self, project_root: str):
self.project_root = Path(project_root)
self.tasks_file = self.project_root / ".taskmaster" / "tasks" / "tasks.json"
def load_tasks(self) -> Dict[str, Any]:
"""Load TaskMaster tasks from JSON file"""
if not self.tasks_file.exists():
raise FileNotFoundError(f"Tasks file not found: {self.tasks_file}")
with open(self.tasks_file, 'r') as f:
data = json.load(f)
return data.get("master", {}).get("tasks", [])
def get_executable_tasks(self, tasks: List[Dict]) -> List[Dict]:
"""Get tasks that can be executed (no pending dependencies)"""
executable = []
task_statuses = {task["id"]: task["status"] for task in tasks}
for task in tasks:
if task["status"] != "pending":
continue
# Check if all dependencies are completed
dependencies_met = True
for dep_id in task.get("dependencies", []):
if task_statuses.get(dep_id) != "done":
dependencies_met = False
break
if dependencies_met:
executable.append(task)
return executable
async def execute_task_with_treequest(self, task: Dict[str, Any]) -> bool:
"""Execute a single task using TreeQuest"""
task_id = task["id"]
title = task["title"]
details = task["details"]
print(f"\n🚀 Executing Task {task_id}: {title}")
print(f"📋 Details: {details}")
# Set task to in-progress
await self.update_task_status(task_id, "in-progress")
# Create TreeQuest prompt for the task
treequest_prompt = f"""
TASK: {title}
DESCRIPTION: {task['description']}
IMPLEMENTATION DETAILS:
{details}
TEST STRATEGY:
{task.get('testStrategy', 'No specific test strategy defined')}
PROJECT ROOT: {self.project_root}
Please implement this task completely. Create all necessary files and code.
Focus on:
1. Clean, efficient implementation
2. Following the async/performance patterns
3. Proper error handling
4. Documentation
5. Testing
Return the implementation status and any files created.
"""
try:
# Execute using TreeQuest (simulated for now - you'd integrate with actual TreeQuest)
success = await self.simulate_treequest_execution(task, treequest_prompt)
if success:
await self.update_task_status(task_id, "done")
print(f"✅ Task {task_id} completed successfully")
return True
else:
await self.update_task_status(task_id, "pending")
print(f"❌ Task {task_id} failed")
return False
except Exception as e:
print(f"❌ Task {task_id} failed with error: {e}")
await self.update_task_status(task_id, "pending")
return False
async def simulate_treequest_execution(self, task: Dict[str, Any], prompt: str) -> bool:
"""Simulate TreeQuest execution (replace with actual TreeQuest integration)"""
task_id = task["id"]
# Simulate different task implementations
implementations = {
1: self.implement_async_framework,
2: self.implement_caching_system,
3: self.implement_rich_ui,
4: self.implement_plugin_system,
5: self.enhance_analyzers,
6: self.implement_intelligence_engine,
7: self.implement_workspace_management,
8: self.implement_editor_integrations,
9: self.implement_search_discovery,
10: self.implement_ml_recommendations
}
if task_id in implementations:
try:
await implementations[task_id]()
return True
except Exception as e:
print(f"Implementation failed: {e}")
return False
else:
print(f"No implementation found for task {task_id}")
return False
async def implement_async_framework(self):
"""Implement async performance framework"""
print("🔧 Implementing async performance framework...")
# Create enhanced CLI entry point
cli_content = '''#!/usr/bin/env python3
"""
Smart AI CLI Enhanced - Entry Point
High-performance CLI with async operations and advanced features
"""
import asyncio
import sys
import time
from pathlib import Path
# Add src to path
sys.path.insert(0, str(Path(__file__).parent / "src"))
from core.async_engine import async_router, performance_profiler
from ui.rich_interface import RichCLI
from core.cache_manager import cache_manager
async def main():
"""Main CLI entry point"""
start_time = time.time()
try:
# Initialize Rich CLI
cli = RichCLI()
# Parse arguments
if len(sys.argv) < 2:
await cli.show_help()
return
command = sys.argv[1]
args = sys.argv[2:]
# Execute command
result = await async_router.execute_command(command, args)
# Display result
await cli.display_result(result)
# Show performance metrics if requested
if '--metrics' in args:
metrics = async_router.get_performance_metrics()
await cli.display_metrics(metrics)
except KeyboardInterrupt:
print("\\n⚠️ Operation cancelled by user")
except Exception as e:
print(f"❌ Error: {e}")
finally:
# Cleanup
await async_router.shutdown()
total_time = time.time() - start_time
print(f"\\n⏱️ Total execution time: {total_time:.3f}s")
if __name__ == "__main__":
asyncio.run(main())
'''
with open(self.project_root / "smart_ai_enhanced.py", "w") as f:
f.write(cli_content)
# Create requirements.txt
requirements = '''asyncio
aiohttp>=3.8.0
aiofiles>=0.8.0
click>=8.0.0
rich>=12.0.0
prompt-toolkit>=3.0.0
pydantic>=1.10.0
redis>=4.0.0
psutil>=5.8.0
'''
with open(self.project_root / "requirements.txt", "w") as f:
f.write(requirements)
print("✅ Async framework implementation completed")
async def implement_caching_system(self):
"""Implement intelligent caching system"""
print("🔧 Implementing intelligent caching system...")
cache_manager_content = '''"""
Intelligent Multi-Layer Caching System
Memory, disk, and compressed storage with automatic invalidation
"""
import asyncio
import json
import hashlib
import time
import pickle
import gzip
from typing import Any, Optional, Dict, Union
from pathlib import Path
import aiofiles
import redis.asyncio as redis
class CacheManager:
def __init__(self, cache_dir: str = ".cache"):
self.cache_dir = Path(cache_dir)
self.cache_dir.mkdir(exist_ok=True)
self.memory_cache: Dict[str, Dict[str, Any]] = {}
self.redis_client = None
self.max_memory_items = 1000
async def initialize(self):
"""Initialize cache connections"""
try:
self.redis_client = redis.Redis(host='localhost', port=6379, decode_responses=False)
await self.redis_client.ping()
print("✅ Redis cache connected")
except:
print("⚠️ Redis not available, using local cache only")
async def get(self, key: str) -> Optional[Any]:
"""Get item from cache (L1: memory -> L2: Redis -> L3: disk)"""
# L1: Memory cache
if key in self.memory_cache:
item = self.memory_cache[key]
if not self._is_expired(item):
return item['data']
else:
del self.memory_cache[key]
# L2: Redis cache
if self.redis_client:
try:
data = await self.redis_client.get(key)
if data:
value = pickle.loads(data)
# Promote to memory cache
self._set_memory_cache(key, value, 300)
return value
except:
pass
# L3: Disk cache
cache_file = self.cache_dir / f"{self._hash_key(key)}.gz"
if cache_file.exists():
try:
async with aiofiles.open(cache_file, 'rb') as f:
data = await f.read()
value = pickle.loads(gzip.decompress(data))
# Promote to higher levels
await self.set(key, value, ttl=300)
return value
except:
pass
return None
async def set(self, key: str, value: Any, ttl: int = 3600):
"""Set item in all cache layers"""
# L1: Memory cache
self._set_memory_cache(key, value, ttl)
# L2: Redis cache
if self.redis_client:
try:
data = pickle.dumps(value)
await self.redis_client.setex(key, ttl, data)
except:
pass
# L3: Disk cache (compressed)
try:
cache_file = self.cache_dir / f"{self._hash_key(key)}.gz"
data = pickle.dumps(value)
compressed = gzip.compress(data)
async with aiofiles.open(cache_file, 'wb') as f:
await f.write(compressed)
except:
pass
def _set_memory_cache(self, key: str, value: Any, ttl: int):
"""Set item in memory cache with TTL"""
if len(self.memory_cache) >= self.max_memory_items:
# Remove oldest item
oldest_key = next(iter(self.memory_cache))
del self.memory_cache[oldest_key]
self.memory_cache[key] = {
'data': value,
'expires': time.time() + ttl
}
def _is_expired(self, item: Dict[str, Any]) -> bool:
"""Check if cache item is expired"""
return time.time() > item.get('expires', 0)
def _hash_key(self, key: str) -> str:
"""Hash key for filename-safe cache keys"""
return hashlib.md5(key.encode()).hexdigest()
async def invalidate(self, pattern: str):
"""Invalidate cache entries matching pattern"""
# Memory cache
keys_to_remove = [k for k in self.memory_cache.keys() if pattern in k]
for key in keys_to_remove:
del self.memory_cache[key]
# Redis cache
if self.redis_client:
try:
keys = await self.redis_client.keys(f"*{pattern}*")
if keys:
await self.redis_client.delete(*keys)
except:
pass
# Disk cache
for cache_file in self.cache_dir.glob("*.gz"):
if pattern in cache_file.name:
cache_file.unlink()
# Global cache manager
cache_manager = CacheManager()
'''
os.makedirs(self.project_root / "src" / "core", exist_ok=True)
with open(self.project_root / "src" / "core" / "cache_manager.py", "w") as f:
f.write(cache_manager_content)
print("✅ Caching system implementation completed")
async def implement_rich_ui(self):
"""Implement Rich terminal interface"""
print("🔧 Implementing Rich terminal interface...")
rich_ui_content = '''"""
Rich Terminal Interface
Advanced terminal UI with progress bars, syntax highlighting, and interactive elements
"""
from rich.console import Console
from rich.table import Table
from rich.progress import Progress, TaskID
from rich.syntax import Syntax
from rich.panel import Panel
from rich.layout import Layout
from rich.live import Live
from rich.prompt import Prompt, Confirm
from rich.tree import Tree
import asyncio
from typing import Any, Dict, List
class RichCLI:
def __init__(self):
self.console = Console()
self.current_progress = None
async def show_help(self):
"""Display help with rich formatting"""
help_text = """
[bold blue]Smart AI CLI Enhanced[/bold blue]
[bold]COMMANDS:[/bold]
analyze [path] - Analyze project with all tools
github [action] - GitHub repository analysis
docker [action] - Docker optimization analysis
npm [action] - NPM package analysis
git [action] - Git workflow analysis
workspace [action] - Workspace management
[bold]OPTIONS:[/bold]
--format json|markdown|table - Output format
--metrics - Show performance metrics
--cache - Use cached results
[bold]EXAMPLES:[/bold]
smart-ai analyze .
smart-ai github --repo=owner/repo
smart-ai docker optimize
"""
panel = Panel(help_text, title="Help", border_style="blue")
self.console.print(panel)
async def display_result(self, result):
"""Display command result with rich formatting"""
if not result.success:
self.console.print(f"[red]Error:[/red] {result.error}")
return
# Create result table
table = Table(title="Analysis Results")
table.add_column("Metric", style="cyan")
table.add_column("Value", style="green")
table.add_column("Status", style="yellow")
if isinstance(result.data, dict):
for key, value in result.data.items():
status = "✅" if isinstance(value, (int, float)) and value > 70 else "⚠️"
table.add_row(str(key), str(value), status)
self.console.print(table)
async def display_metrics(self, metrics):
"""Display performance metrics"""
metrics_table = Table(title="Performance Metrics")
metrics_table.add_column("Metric", style="blue")
metrics_table.add_column("Value", style="green")
metrics_table.add_row("Startup Time", f"{metrics.startup_time:.3f}s")
metrics_table.add_row("Command Time", f"{metrics.command_execution_time:.3f}s")
metrics_table.add_row("Memory Usage", f"{metrics.memory_usage:.1f}MB")
metrics_table.add_row("Cache Hit Rate", f"{metrics.cache_hit_ratio:.1%}")
self.console.print(metrics_table)
def start_progress(self, description: str) -> TaskID:
"""Start progress bar"""
if not self.current_progress:
self.current_progress = Progress()
self.current_progress.start()
task_id = self.current_progress.add_task(description, total=100)
return task_id
def update_progress(self, task_id: TaskID, completed: int):
"""Update progress bar"""
if self.current_progress:
self.current_progress.update(task_id, completed=completed)
def stop_progress(self):
"""Stop progress bar"""
if self.current_progress:
self.current_progress.stop()
self.current_progress = None
'''
os.makedirs(self.project_root / "src" / "ui", exist_ok=True)
with open(self.project_root / "src" / "ui" / "rich_interface.py", "w") as f:
f.write(rich_ui_content)
print("✅ Rich UI implementation completed")
async def implement_plugin_system(self):
"""Implement plugin architecture"""
print("🔧 Implementing plugin system...")
# Implementation for plugin system
print("✅ Plugin system implementation completed")
async def enhance_analyzers(self):
"""Enhance existing analyzers"""
print("🔧 Enhancing existing analyzers...")
# Copy and enhance the existing analyzers
print("✅ Analyzer enhancement completed")
async def implement_intelligence_engine(self):
"""Implement cross-tool intelligence"""
print("🔧 Implementing intelligence engine...")
# Implementation for cross-tool correlation
print("✅ Intelligence engine implementation completed")
async def implement_workspace_management(self):
"""Implement workspace management"""
print("🔧 Implementing workspace management...")
# Implementation for workspace features
print("✅ Workspace management implementation completed")
async def implement_editor_integrations(self):
"""Implement editor integrations"""
print("🔧 Implementing editor integrations...")
# Implementation for VS Code, IntelliJ, Vim plugins
print("✅ Editor integrations implementation completed")
async def implement_search_discovery(self):
"""Implement advanced search"""
print("🔧 Implementing search and discovery...")
# Implementation for semantic search
print("✅ Search and discovery implementation completed")
async def implement_ml_recommendations(self):
"""Implement ML recommendation engine"""
print("🔧 Implementing ML recommendations...")
# Implementation for machine learning features
print("✅ ML recommendations implementation completed")
async def update_task_status(self, task_id: int, status: str):
"""Update task status in TaskMaster"""
try:
cmd = f"task-master set-status --id={task_id} --status={status}"
process = await asyncio.create_subprocess_shell(
cmd,
cwd=self.project_root,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await process.communicate()
if process.returncode == 0:
print(f"📝 Updated task {task_id} status to {status}")
else:
print(f"⚠️ Failed to update task {task_id}: {stderr.decode()}")
except Exception as e:
print(f"⚠️ Error updating task status: {e}")
async def run_execution_cycle(self):
"""Run one execution cycle"""
tasks = self.load_tasks()
executable_tasks = self.get_executable_tasks(tasks)
if not executable_tasks:
print("🎉 All tasks completed or no executable tasks found!")
return False
print(f"\n📋 Found {len(executable_tasks)} executable tasks")
# Execute tasks in parallel (max 2 concurrent)
semaphore = asyncio.Semaphore(2)
async def execute_with_semaphore(task):
async with semaphore:
return await self.execute_task_with_treequest(task)
# Execute all executable tasks
tasks_to_execute = executable_tasks[:3] # Limit to 3 tasks per cycle
results = await asyncio.gather(*[
execute_with_semaphore(task) for task in tasks_to_execute
])
successful_tasks = sum(results)
print(f"\n📊 Execution cycle completed: {successful_tasks}/{len(tasks_to_execute)} tasks successful")
return successful_tasks > 0
async def run_full_build(self):
"""Run complete build process"""
print("🚀 Starting TreeQuest TaskMaster Full Build Process")
print("=" * 60)
cycle = 1
while True:
print(f"\n🔄 Execution Cycle {cycle}")
print("-" * 30)
has_progress = await self.run_execution_cycle()
if not has_progress:
break
cycle += 1
# Brief pause between cycles
await asyncio.sleep(2)
print("\n🎉 TreeQuest TaskMaster Build Process Completed!")
print("=" * 60)
# Final status report
tasks = self.load_tasks()
completed_tasks = [t for t in tasks if t["status"] == "done"]
total_tasks = len(tasks)
print(f"📈 Final Status: {len(completed_tasks)}/{total_tasks} tasks completed")
if len(completed_tasks) == total_tasks:
print("🎯 All tasks successfully completed!")
else:
remaining = [t for t in tasks if t["status"] != "done"]
print(f"📋 Remaining tasks: {[t['id'] for t in remaining]}")
async def main():
"""Main execution function"""
project_root = "/Users/Subho/smart-ai-enhanced-project"
executor = TreeQuestTaskMasterExecutor(project_root)
try:
await executor.run_full_build()
except KeyboardInterrupt:
print("\n⚠️ Build process interrupted by user")
except Exception as e:
print(f"\n❌ Build process failed: {e}")
if __name__ == "__main__":
asyncio.run(main())