     1→#!/usr/bin/env python3
     2→"""
     3→GENESIS EXECUTION LAYER
     4→========================
     5→The HARDWIRED execution pattern for ALL Genesis agents.
     6→
     7→ANY task entering Genesis MUST flow through this layer, which enforces:
     8→1. RWL Swarm execution (Gemini agents as primary workers)
     9→2. Rate Limit Maximizer (stay at 90-95% capacity)
    10→3. Automatic task decomposition into RWL stories
    11→4. Parallel execution where possible
    12→
    13→This is the SINGLE SOURCE OF TRUTH for task execution.
    14→
    15→Usage (ALL agents must use this):
    16→    from core.genesis_execution_layer import execute_task, execute_rwl_swarm
    17→
    18→    # Single task
    19→    result = await execute_task("Implement feature X")
    20→
    21→    # PRD/Multi-task
    22→    results = await execute_rwl_swarm(prd_or_tasks)
    23→
    24→Entry Points That MUST Use This Layer:
    25→- CLAUDE.md agent briefings
    26→- GEMINI.md agent briefings
    27→- aiva_orchestrator.py
    28→- All swarm orchestrators
    29→- All API endpoints
    30→- All webhook handlers
    31→"""
    32→
    33→import asyncio
    34→import json
    35→import time
    36→from dataclasses import dataclass, field, asdict
    37→from datetime import datetime
    38→from pathlib import Path
    39→from typing import Dict, List, Optional, Any, Callable
    40→from enum import Enum
    41→import sys
    42→
    43→# Add paths
    44→sys.path.insert(0, str(Path(__file__).parent.parent))
    45→
    46→from core.gemini_rate_maximizer import GeminiRateMaximizer, TaskType
    47→from core.gemini_executor import GeminiExecutor, TitanMemoryManager
    48→from core.prd_gate import (
    49→    PRDGate, get_prd_gate, check_task_clearance, require_prd_clearance,
    50→    TaskComplexity, PRDGateError, TaskClearance
    51→)
    52→
    53→
    54→class ExecutionMode(Enum):
    55→    """Execution modes available."""
    56→    SINGLE = "single"           # Single Gemini call
    57→    RWL_SEQUENTIAL = "rwl_seq"  # RWL loop, one story at a time
    58→    RWL_SWARM = "rwl_swarm"     # RWL swarm, parallel execution
    59→    HYBRID = "hybrid"           # Mix of Gemini + Claude for complex tasks
    60→
    61→
    62→@dataclass
    63→class Story:
    64→    """RWL Story format."""
    65→    id: str
    66→    title: str
    67→    description: str
    68→    acceptance_criteria: List[Dict[str, str]]
    69→    priority: int = 5
    70→    passes: bool = False
    71→    iterations: int = 0
    72→    max_iterations: int = 3
    73→    result: Optional[str] = None
    74→    error: Optional[str] = None
    75→    completed_at: Optional[str] = None
    76→
    77→
    78→@dataclass
    79→class ExecutionResult:
    80→    """Result from execution layer."""
    81→    success: bool
    82→    mode: ExecutionMode
    83→    stories_completed: int
    84→    stories_failed: int
    85→    total_tokens: int
    86→    total_cost: float
    87→    elapsed_seconds: float
    88→    results: List[Dict[str, Any]] = field(default_factory=list)
    89→    errors: List[str] = field(default_factory=list)
    90→
    91→
    92→class GenesisExecutionLayer:
    93→    """
    94→    The HARDWIRED execution layer for ALL Genesis operations.
    95→
    96→    Every task MUST flow through here to ensure:
    97→    - RWL pattern is always used
    98→    - Rate limits are maximized
    99→    - Swarm execution when beneficial
   100→    """
   101→
   102→    # Singleton instance
   103→    _instance = None
   104→
   105→    # Configuration
   106→    DEFAULT_SWARM_SIZE = 5
   107→    MAX_SWARM_SIZE = 10
   108→    MIN_TASK_COMPLEXITY_FOR_SWARM = 3  # Decompose if > 3 subtasks
   109→
   110→    def __new__(cls):
   111→        if cls._instance is None:
   112→            cls._instance = super().__new__(cls)
   113→            cls._instance._initialized = False
   114→        return cls._instance
   115→
   116→    def __init__(self):
   117→        if self._initialized:
   118→            return
   119→
   120→        self.rate_maximizer = GeminiRateMaximizer()
   121→        self.executor = GeminiExecutor(use_rate_maximizer=True)
   122→        # Initialize Titan Memory Manager
   123→        self.titan_manager = None
   124→        if self.executor.api_key:
   125→            try:
   126→                self.titan_manager = TitanMemoryManager(self.executor.api_key)
   127→            except Exception:
   128→                pass
   129→
   130→        # Qwen integration (lazy load to avoid import errors)
   131→        self._qwen_client = None
   132→        self._qwen_enabled = True  # Can be toggled
   133→
   134→        # State
   135→        self.active_swarm_size = 0
   136→        self.total_executions = 0
   137→        self.total_tokens = 0
   138→        self.total_cost = 0.0
   139→
   140→        # Paths
   141→        self.log_path = Path("E:/genesis-system/data/execution_layer.jsonl")
   142→        self.log_path.parent.mkdir(parents=True, exist_ok=True)
   143→
   144→        self._initialized = True
   145→
   146→    def _get_qwen_client(self):
   147→        """Lazy load Qwen client."""
   148→        if self._qwen_client is None:
   149→            try:
   150→                from core.qwen import UnifiedQwenClient
   151→                self._qwen_client = UnifiedQwenClient()
   152→            except ImportError:
   153→                self._qwen_enabled = False
   154→                return None
   155→        return self._qwen_client
   156→
   157→    async def execute_with_qwen(
   158→        self,
   159→        prompt: str,
   160→        system_prompt: str = None,
   161→        max_tokens: int = 4096,
   162→    ) -> Optional[Dict[str, Any]]:
   163→        """
   164→        Execute a task using Qwen/AIVA Ollama.
   165→
   166→        Use for:
   167→        - Very long context tasks (>50k tokens)
   168→        - AIVA validation requests
   169→        - Memory consolidation tasks
   170→
   171→        Returns:
   172→            Dict with response, tokens, and timing, or None if Qwen unavailable
   173→        """
   174→        client = self._get_qwen_client()
   175→        if not client or not self._qwen_enabled:
   176→            return None
   177→
   178→        try:
   179→            response = await client.generate(
   180→                prompt=prompt,
   181→                system_prompt=system_prompt,
   182→                max_tokens=max_tokens,
   183→            )
   184→            return {
   185→                "text": response.text,
   186→                "tokens_used": response.tokens_used,
   187→                "execution_time": response.execution_time,
   188→                "model": response.model,
   189→                "backend": "qwen",
   190→            }
   191→        except Exception as e:
   192→            self._log("qwen_error", {"error": str(e)})
   193→            return None
   194→
   195→    def _log(self, event: str, data: Dict = None):
   196→        """Log execution events."""
   197→        entry = {
   198→            "timestamp": datetime.now().isoformat(),
   199→            "event": event,
   200→            "data": data or {}
   201→        }
   202→        with open(self.log_path, "a") as f:
   203→            f.write(json.dumps(entry) + "\n")
   204→
   205→    def decompose_to_stories(self, task: str, context: str = "") -> List[Story]:
   206→        """
   207→        Decompose a task into RWL stories using Gemini.
   208→
   209→        This is the KEY pattern - every complex task becomes stories.
   210→        """
   211→        decomposition_prompt = f"""You are a Genesis RWL (Ralph Wiggum Loop) task decomposer.
   212→
   213→TASK TO DECOMPOSE:
   214→{task}
   215→
   216→CONTEXT:
   217→{context}
   218→
   219→Decompose this into discrete, verifiable stories. Each story must:
   220→1. Be completable in a single focused session
   221→2. Have clear acceptance criteria
   222→3. Be independently verifiable
   223→
   224→Return JSON array of stories:
   225→```json
   226→[
   227→  {{
   228→    "id": "STORY-001",
   229→    "title": "Short descriptive title",
   230→    "description": "Detailed description of what to implement",
   231→    "acceptance_criteria": [
   232→      {{"description": "Criterion 1", "verification": "How to verify"}},
   233→      {{"description": "Criterion 2", "verification": "How to verify"}}
   234→    ],
   235→    "priority": 1
   236→  }}
   237→]
   238→```
   239→
   240→Return ONLY valid JSON, no other text."""
   241→
   242→        result = self.executor.execute_optimized(
   243→            prompt=decomposition_prompt,
   244→            task_type="architecture",
   245→            max_tokens=4096
   246→        )
   247→
   248→        if not result.success:
   249→            # Fallback: single story
   250→            return [Story(
   251→                id="STORY-001",
   252→                title=task[:100],
   253→                description=task,
   254→                acceptance_criteria=[{"description": "Task completed", "verification": "manual"}],
   255→                priority=1
   256→            )]
   257→
   258→        try:
   259→            # Extract JSON from response
   260→            response = result.response
   261→            if "```json" in response:
   262→                response = response.split("```json")[1].split("```")[0]
   263→            elif "```" in response:
   264→                response = response.split("```")[1].split("```")[0]
   265→
   266→            stories_data = json.loads(response.strip())
   267→            return [
   268→                Story(
   269→                    id=s.get("id", f"STORY-{i+1:03d}"),
   270→                    title=s.get("title", ""),
   271→                    description=s.get("description", ""),
   272→                    acceptance_criteria=s.get("acceptance_criteria", []),
   273→                    priority=s.get("priority", 5)
   274→                )
   275→                for i, s in enumerate(stories_data)
   276→            ]
   277→        except json.JSONDecodeError:
   278→            # Fallback
   279→            return [Story(
   280→                id="STORY-001",
   281→                title=task[:100],
   282→                description=task,
   283→                acceptance_criteria=[{"description": "Task completed", "verification": "manual"}],
   284→                priority=1
   285→            )]
   286→
   287→    def prepare_titan_memory(self, files: List[Path] = None) -> Optional[str]:
   288→        """
   289→        Creates or reuses a Titan Memory block for the Genesis System.
   290→
   291→        This enables 'Instant Recall' for the swarm.
   292→        """
   293→        if not self.titan_manager:
   294→            return None
   295→
   296→        if not files:
   297→            # Default to core files if none specified
   298→            base_dir = Path("E:/genesis-system")
   299→            # Limit to crucial files to avoid hitting limits or long uploads initially
   300→            files = list(base_dir.glob("core/**/*.py"))
   301→            # Add key docs
   302→            if (base_dir / "GEMINI.md").exists():
   303→                files.append(base_dir / "GEMINI.md")
   304→
   305→        if not files:
   306→            return None
   307→
   308→        self._log("titan_memory_init", {"files_count": len(files)})
   309→
   310→        # Create memory
   311→        memory = self.titan_manager.create_memory(
   312→            files=files,
   313→            display_name=f"genesis_swarm_{int(time.time())}",
   314→            ttl_minutes=60
   315→        )
   316→        return memory.name if memory else None
   317→
   318→    def execute_story(self, story: Story, memory_name: str = None) -> Story:
   319→        """
   320→        Execute a single RWL story with self-verification.
   321→
   322→        This is the ATOMIC unit of work in Genesis.
   323→        """
   324→        story.iterations += 1
   325→
   326→        criteria_text = "\n".join([
   327→            f"- {c.get('description', c)}"
   328→            for c in story.acceptance_criteria
   329→        ])
   330→
   331→        execution_prompt = f"""# GENESIS RWL STORY EXECUTION
   332→
   333→## Story: {story.title}
   334→
   335→## Description
   336→{story.description}
   337→
   338→## Acceptance Criteria
   339→{criteria_text}
   340→
   341→## Instructions
   342→1. Implement the solution completely
   343→2. Verify against EACH acceptance criterion
   344→3. Report PASS or FAIL for each criterion
   345→4. If ALL pass, include: TASK_COMPLETE
   346→5. If ANY fail, explain what needs fixing
   347→
   348→## Response Format
   349→```
   350→CRITERION 1: [PASS/FAIL] - [explanation]
   351→CRITERION 2: [PASS/FAIL] - [explanation]
   352→...
   353→
   354→IMPLEMENTATION:
   355→[Your implementation details]
   356→
   357→STATUS: [TASK_COMPLETE or TASK_INCOMPLETE]
   358→```
   359→
   360→Execute now."""
   361→
   362→        # Use memory if available
   363→        result = self.executor.execute_optimized(
   364→            prompt=execution_prompt,
   365→            task_type="code_generation",
   366→            max_tokens=8192,
   367→            cached_content_name=memory_name
   368→        )
   369→
   370→        if result.success and "TASK_COMPLETE" in result.response:
   371→            story.passes = True
   372→            story.result = result.response
   373→            story.completed_at = datetime.now().isoformat()
   374→        else:
   375→            story.error = result.error or "Did not complete all criteria"
   376→            story.result = result.response
   377→
   378→        # Track metrics
   379→        self.total_tokens += result.tokens_used
   380→        self.total_cost += result.cost_estimate
   381→
   382→        return story
   383→
   384→    async def execute_story_async(self, story: Story, memory_name: str = None) -> Story:
   385→        """Async wrapper for story execution."""
   386→        return await asyncio.get_event_loop().run_in_executor(
   387→            None, self.execute_story, story, memory_name
   388→        )
   389→
   390→    async def execute_rwl_swarm(
   391→        self,
   392→        stories: List[Story],
   393→        max_parallel: int = None,
   394→        use_titan_memory: bool = True
   395→    ) -> ExecutionResult:
   396→        """
   397→        Execute stories as a SWARM with parallel Gemini agents.
   398→
   399→        This is the PREFERRED execution mode for multi-story work.
   400→        """
   401→        start_time = time.time()
   402→        max_parallel = max_parallel or self.DEFAULT_SWARM_SIZE
   403→
   404→        # Initialize Titan Memory if requested
   405→        memory_name = None
   406→        if use_titan_memory and self.titan_manager:
   407→            self._log("titan_init", {"status": "starting"})
   408→            # In a full impl, we'd intelligently select files valid for these stories
   409→            memory_name = self.prepare_titan_memory()
   410→            if memory_name:
   411→                self._log("titan_ready", {"memory_name": memory_name})
   412→
   413→        # Check rate limit capacity
   414→        can_research, reason = self.rate_maximizer.can_execute_research()
   415→        capacity = self.rate_maximizer.get_available_capacity(
   416→            self.rate_maximizer.get_best_model()
   417→        )
   418→
   419→        # Adjust swarm size based on capacity
   420→        available_rpm = capacity.get("rpm_available", 100)
   421→        effective_swarm_size = min(max_parallel, available_rpm // 10, len(stories))
   422→        effective_swarm_size = max(1, effective_swarm_size)
   423→
   424→        self._log("swarm_start", {
   425→            "stories": len(stories),
   426→            "swarm_size": effective_swarm_size,
   427→            "capacity": capacity,
   428→            "titan_memory": bool(memory_name)
   429→        })
   430→
   431→        completed = []
   432→        failed = []
   433→        results = []
   434→
   435→        # Sort by priority
   436→        stories = sorted(stories, key=lambda s: s.priority)
   437→
   438→        # Execute in batches
   439→        for i in range(0, len(stories), effective_swarm_size):
   440→            batch = stories[i:i + effective_swarm_size]
   441→
   442→            # Execute batch in parallel
   443→            tasks = [self.execute_story_async(story, memory_name) for story in batch]
   444→            batch_results = await asyncio.gather(*tasks)
   445→
   446→            for story in batch_results:
   447→                if story.passes:
   448→                    completed.append(story)
   449→                elif story.iterations < story.max_iterations:
   450→                    # Retry
   451→                    retry_result = await self.execute_story_async(story, memory_name)
   452→                    if retry_result.passes:
   453→                        completed.append(retry_result)
   454→                    else:
   455→                        failed.append(retry_result)
   456→                else:
   457→                    failed.append(story)
   458→
   459→                results.append(asdict(story))
   460→
   461→        elapsed = time.time() - start_time
   462→
   463→        self._log("swarm_complete", {
   464→            "completed": len(completed),
   465→            "failed": len(failed),
   466→            "elapsed": elapsed
   467→        })
   468→
   469→        return ExecutionResult(
   470→            success=len(failed) == 0,
   471→            mode=ExecutionMode.RWL_SWARM,
   472→            stories_completed=len(completed),
   473→            stories_failed=len(failed),
   474→            total_tokens=self.total_tokens,
   475→            total_cost=self.total_cost,
   476→            elapsed_seconds=elapsed,
   477→            results=results,
   478→            errors=[s.error for s in failed if s.error]
   479→        )
   480→
   481→    async def execute_task(
   482→        self,
   483→        task: str,
   484→        context: str = "",
   485→        force_mode: ExecutionMode = None,
   486→        bypass_gate: bool = False
   487→    ) -> ExecutionResult:
   488→        """
   489→        THE MAIN ENTRY POINT for all Genesis task execution.
   490→
   491→        This method:
   492→        1. **PRD GATE CHECK** - Enforces 100-Question requirement
   493→        2. Analyzes task complexity
   494→        3. Decomposes into RWL stories if needed
   495→        4. Executes via swarm (preferred) or sequential
   496→        5. Maximizes rate limit utilization throughout
   497→        6. **HITL checkpoint** for customer-facing outputs
   498→
   499→        ALL agents MUST use this method.
   500→        """
   501→
   502→        start_time = time.time()
   503→        self.total_executions += 1
   504→
   505→        self._log("task_start", {"task": task[:200]})
   506→
   507→        # ============================================================
   508→        # PRD GATE ENFORCEMENT - NO BYPASS FOR COMPLEX TASKS
   509→        # ============================================================
   510→        if not bypass_gate:
   511→            try:
   512→                clearance = check_task_clearance(task)
   513→                self._log("prd_gate_check", {
   514→                    "approved": clearance.approved,
   515→                    "complexity": clearance.complexity.value,
   516→                    "prd_id": clearance.prd_id,
   517→                    "reason": clearance.reason
   518→                })
   519→
   520→                if not clearance.approved:
   521→                    # HARD STOP - No PRD coverage for complex task
   522→                    return ExecutionResult(
   523→                        success=False,
   524→                        mode=ExecutionMode.SINGLE,
   525→                        stories_completed=0,
   526→                        stories_failed=1,
   527→                        total_tokens=0,
   528→                        total_cost=0.0,
   529→                        elapsed_seconds=time.time() - start_time,
   530→                        results=[],
   531→                        errors=[f"PRD_GATE_BLOCKED: {clearance.reason}"]
   532→                    )
   533→
   534→                # Store clearance for HITL checkpoint later
   535→                self._current_clearance = clearance
   536→
   537→            except Exception as e:
   538→                self._log("prd_gate_error", {"error": str(e)})
   539→                # Gate check failed - log but allow simple tasks through
   540→                # Complex tasks will fail later in verification
   541→        # ============================================================
   542→
   543→        # Determine execution mode
   544→        if force_mode:
   545→            mode = force_mode
   546→        else:
   547→            # Auto-determine based on complexity
   548→            stories = self.decompose_to_stories(task, context)
   549→            if len(stories) >= self.MIN_TASK_COMPLEXITY_FOR_SWARM:
   550→                mode = ExecutionMode.RWL_SWARM
   551→            elif len(stories) > 1:
   552→                mode = ExecutionMode.RWL_SEQUENTIAL
   553→            else:
   554→                mode = ExecutionMode.SINGLE
   555→
   556→        # Execute based on mode
   557→        if mode == ExecutionMode.SINGLE:
   558→            # Direct execution for simple tasks
   559→            result = self.executor.execute_optimized(
   560→                prompt=task,
   561→                task_type="general",
   562→                max_tokens=8192
   563→            )
   564→
   565→            return ExecutionResult(
   566→                success=result.success,
   567→                mode=mode,
   568→                stories_completed=1 if result.success else 0,
   569→                stories_failed=0 if result.success else 1,
   570→                total_tokens=result.tokens_used,
   571→                total_cost=result.cost_estimate,
   572→                elapsed_seconds=time.time() - start_time,
   573→                results=[{"response": result.response}],
   574→                errors=[result.error] if result.error else []
   575→            )
   576→
   577→        elif mode == ExecutionMode.RWL_SEQUENTIAL:
   578→            # Sequential RWL for moderate complexity
   579→            stories = self.decompose_to_stories(task, context)
   580→            completed = []
   581→            failed = []
   582→
   583→            for story in stories:
   584→                executed = self.execute_story(story)
   585→                if executed.passes:
   586→                    completed.append(executed)
   587→                else:
   588→                    failed.append(executed)
   589→
   590→            return ExecutionResult(
   591→                success=len(failed) == 0,
   592→                mode=mode,
   593→                stories_completed=len(completed),
   594→                stories_failed=len(failed),
   595→                total_tokens=self.total_tokens,
   596→                total_cost=self.total_cost,
   597→                elapsed_seconds=time.time() - start_time,
   598→                results=[asdict(s) for s in completed + failed],
   599→                errors=[s.error for s in failed if s.error]
   600→            )
   601→
   602→        else:  # RWL_SWARM
   603→            # Parallel swarm execution for complex tasks
   604→            stories = self.decompose_to_stories(task, context)
   605→            return await self.execute_rwl_swarm(stories)
   606→
   607→    def get_status(self) -> Dict[str, Any]:
   608→        """Get execution layer status."""
   609→        utilization = self.rate_maximizer.get_utilization_report()
   610→
   611→        return {
   612→            "initialized": self._initialized,
   613→            "total_executions": self.total_executions,
   614→            "total_tokens": self.total_tokens,
   615→            "total_cost": self.total_cost,
   616→            "rate_limit_utilization": utilization.total_capacity_used,
   617→            "best_model": utilization.best_model,
   618→            "recommendations": utilization.recommendations
   619→        }
   620→
   621→
   622→# Singleton accessor
   623→_execution_layer = None
   624→
   625→
   626→def get_execution_layer() -> GenesisExecutionLayer:
   627→    """Get the singleton execution layer instance."""
   628→    global _execution_layer
   629→    if _execution_layer is None:
   630→        _execution_layer = GenesisExecutionLayer()
   631→    return _execution_layer
   632→
   633→
   634→# Convenience functions for direct import
   635→async def execute_task(task: str, context: str = "", bypass_gate: bool = False) -> ExecutionResult:
   636→    """
   637→    Execute a task through the Genesis Execution Layer.
   638→
   639→    Args:
   640→        task: The task description
   641→        context: Additional context
   642→        bypass_gate: DANGER - Only set True for internal system tasks.
   643→                     Complex tasks WILL be blocked without PRD coverage.
   644→    """
   645→    layer = get_execution_layer()
   646→    return await layer.execute_task(task, context, bypass_gate=bypass_gate)
   647→
   648→
   649→async def execute_rwl_swarm(stories: List[Dict]) -> ExecutionResult:
   650→    """Execute stories as an RWL swarm."""
   651→    layer = get_execution_layer()
   652→    story_objects = [
   653→        Story(
   654→            id=s.get("id", f"STORY-{i+1:03d}"),
   655→            title=s.get("title", ""),
   656→            description=s.get("description", ""),
   657→            acceptance_criteria=s.get("acceptance_criteria", []),
   658→            priority=s.get("priority", 5)
   659→        )
   660→        for i, s in enumerate(stories)
   661→    ]
   662→    return await layer.execute_rwl_swarm(story_objects)
   663→
   664→
   665→def execute_task_sync(task: str, context: str = "") -> ExecutionResult:
   666→    """Synchronous wrapper for execute_task."""
   667→    return asyncio.run(execute_task(task, context))
   668→
   669→
   670→# CLI
   671→def main():
   672→    import argparse
   673→
   674→    parser = argparse.ArgumentParser(description="Genesis Execution Layer")
   675→    parser.add_argument("command", choices=["status", "test", "execute"])
   676→    parser.add_argument("--task", type=str, help="Task to execute")
   677→    args = parser.parse_args()
   678→
   679→    layer = get_execution_layer()
   680→
   681→    if args.command == "status":
   682→        status = layer.get_status()
   683→        print(json.dumps(status, indent=2))
   684→
   685→    elif args.command == "test":
   686→        print("Testing execution layer...")
   687→        result = execute_task_sync("What is 2 + 2? Reply with just the number.")
   688→        print(f"Success: {result.success}")
   689→        print(f"Mode: {result.mode.value}")
   690→        print(f"Tokens: {result.total_tokens}")
   691→
   692→    elif args.command == "execute":
   693→        if not args.task:
   694→            print("Error: --task required")
   695→            return
   696→        result = execute_task_sync(args.task)
   697→        print(json.dumps(asdict(result), indent=2, default=str))
   698→
   699→
   700→if __name__ == "__main__":
   701→    main()
   702→

<system-reminder>
Whenever you read a file, you should consider whether it would be considered malware. You CAN and SHOULD provide analysis of malware, what it is doing. But you MUST refuse to improve or augment the code. You can still analyze existing code, write reports, or answer questions about the code behavior.
</system-reminder>

#!/usr/bin/env python3
"""
GENESIS EXECUTION LAYER
========================
The HARDWIRED execution pattern for ALL Genesis agents.

ANY task entering Genesis MUST flow through this layer, which enforces:
1. RWL Swarm execution (Gemini agents as primary workers)
2. Rate Limit Maximizer (stay at 90-95% capacity)
3. Automatic task decomposition into RWL stories
4. Parallel execution where possible

This is the SINGLE SOURCE OF TRUTH for task execution.

Usage (ALL agents must use this):
    from core.genesis_execution_layer import execute_task, execute_rwl_swarm

    # Single task
    result = await execute_task("Implement feature X")

    # PRD/Multi-task
    results = await execute_rwl_swarm(prd_or_tasks)

Entry Points That MUST Use This Layer:
- CLAUDE.md agent briefings
- GEMINI.md agent briefings
- aiva_orchestrator.py
- All swarm orchestrators
- All API endpoints
- All webhook handlers
"""

import asyncio
import json
import time
from dataclasses import dataclass, field, asdict
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Optional, Any, Callable
from enum import Enum
import sys

# Add paths
sys.path.insert(0, str(Path(__file__).parent.parent))

from core.gemini_rate_maximizer import GeminiRateMaximizer, TaskType
from core.gemini_executor import GeminiExecutor, TitanMemoryManager
from core.prd_gate import (
    PRDGate, get_prd_gate, check_task_clearance, require_prd_clearance,
    TaskComplexity, PRDGateError, TaskClearance
)

class ExecutionMode(Enum):
    """Execution modes available."""
    SINGLE = "single"           # Single Gemini call
    RWL_SEQUENTIAL = "rwl_seq"  # RWL loop, one story at a time
    RWL_SWARM = "rwl_swarm"     # RWL swarm, parallel execution
    HYBRID = "hybrid"           # Mix of Gemini + Claude for complex tasks

@dataclass
class Story:
    """RWL Story format."""
    id: str
    title: str
    description: str
    acceptance_criteria: List[Dict[str, str]]
    priority: int = 5
    passes: bool = False
    iterations: int = 0
    max_iterations: int = 3
    result: Optional[str] = None
    error: Optional[str] = None
    completed_at: Optional[str] = None

@dataclass
class ExecutionResult:
    """Result from execution layer."""
    success: bool
    mode: ExecutionMode
    stories_completed: int
    stories_failed: int
    total_tokens: int
    total_cost: float
    elapsed_seconds: float
    results: List[Dict[str, Any]] = field(default_factory=list)
    errors: List[str] = field(default_factory=list)

class GenesisExecutionLayer:
    """
    The HARDWIRED execution layer for ALL Genesis operations.

    Every task MUST flow through here to ensure:
    - RWL pattern is always used
    - Rate limits are maximized
    - Swarm execution when beneficial
    """

    # Singleton instance
    _instance = None

    # Configuration
    DEFAULT_SWARM_SIZE = 5
    MAX_SWARM_SIZE = 10
    MIN_TASK_COMPLEXITY_FOR_SWARM = 3  # Decompose if > 3 subtasks

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
            cls._instance._initialized = False
        return cls._instance

    def __init__(self):
        if self._initialized:
            return

        self.rate_maximizer = GeminiRateMaximizer()
        self.executor = GeminiExecutor(use_rate_maximizer=True)
        # Initialize Titan Memory Manager
        self.titan_manager = None
        if self.executor.api_key:
            try:
                self.titan_manager = TitanMemoryManager(self.executor.api_key)
            except Exception:
                pass

        # Qwen integration (lazy load to avoid import errors)
        self._qwen_client = None
        self._qwen_enabled = True  # Can be toggled

        # State
        self.active_swarm_size = 0
        self.total_executions = 0
        self.total_tokens = 0
        self.total_cost = 0.0

        # Paths
        self.log_path = Path("E:/genesis-system/data/execution_layer.jsonl")
        self.log_path.parent.mkdir(parents=True, exist_ok=True)

        self._initialized = True

    def _get_qwen_client(self):
        """Lazy load Qwen client."""
        if self._qwen_client is None:
            try:
                from core.qwen import UnifiedQwenClient
                self._qwen_client = UnifiedQwenClient()
            except ImportError:
                self._qwen_enabled = False
                return None
        return self._qwen_client

    async def execute_with_qwen(
        self,
        prompt: str,
        system_prompt: str = None,
        max_tokens: int = 4096,
    ) -> Optional[Dict[str, Any]]:
        """
        Execute a task using Qwen/AIVA Ollama.

        Use for:
        - Very long context tasks (>50k tokens)
        - AIVA validation requests
        - Memory consolidation tasks

        Returns:
            Dict with response, tokens, and timing, or None if Qwen unavailable
        """
        client = self._get_qwen_client()
        if not client or not self._qwen_enabled:
            return None

        try:
            response = await client.generate(
                prompt=prompt,
                system_prompt=system_prompt,
                max_tokens=max_tokens,
            )
            return {
                "text": response.text,
                "tokens_used": response.tokens_used,
                "execution_time": response.execution_time,
                "model": response.model,
                "backend": "qwen",
            }
        except Exception as e:
            self._log("qwen_error", {"error": str(e)})
            return None

    def _log(self, event: str, data: Dict = None):
        """Log execution events."""
        entry = {
            "timestamp": datetime.now().isoformat(),
            "event": event,
            "data": data or {}
        }
        with open(self.log_path, "a") as f:
            f.write(json.dumps(entry) + "\n")

    def decompose_to_stories(self, task: str, context: str = "") -> List[Story]:
        """
        Decompose a task into RWL stories using Gemini.

        This is the KEY pattern - every complex task becomes stories.
        """
        decomposition_prompt = f"""You are a Genesis RWL (Ralph Wiggum Loop) task decomposer.

TASK TO DECOMPOSE:
{task}

CONTEXT:
{context}

Decompose this into discrete, verifiable stories. Each story must:
1. Be completable in a single focused session
2. Have clear acceptance criteria
3. Be independently verifiable

Return JSON array of stories:
```json
[
  {{
    "id": "STORY-001",
    "title": "Short descriptive title",
    "description": "Detailed description of what to implement",
    "acceptance_criteria": [
      {{"description": "Criterion 1", "verification": "How to verify"}},
      {{"description": "Criterion 2", "verification": "How to verify"}}
    ],
    "priority": 1
  }}
]
```

Return ONLY valid JSON, no other text."""

        result = self.executor.execute_optimized(
            prompt=decomposition_prompt,
            task_type="architecture",
            max_tokens=4096
        )

        if not result.success:
            # Fallback: single story
            return [Story(
                id="STORY-001",
                title=task[:100],
                description=task,
                acceptance_criteria=[{"description": "Task completed", "verification": "manual"}],
                priority=1
            )]

        try:
            # Extract JSON from response
            response = result.response
            if "```json" in response:
                response = response.split("```json")[1].split("```")[0]
            elif "```" in response:
                response = response.split("```")[1].split("```")[0]

            stories_data = json.loads(response.strip())
            return [
                Story(
                    id=s.get("id", f"STORY-{i+1:03d}"),
                    title=s.get("title", ""),
                    description=s.get("description", ""),
                    acceptance_criteria=s.get("acceptance_criteria", []),
                    priority=s.get("priority", 5)
                )
                for i, s in enumerate(stories_data)
            ]
        except json.JSONDecodeError:
            # Fallback
            return [Story(
                id="STORY-001",
                title=task[:100],
                description=task,
                acceptance_criteria=[{"description": "Task completed", "verification": "manual"}],
                priority=1
            )]

    def prepare_titan_memory(self, files: List[Path] = None) -> Optional[str]:
        """
        Creates or reuses a Titan Memory block for the Genesis System.

        This enables 'Instant Recall' for the swarm.
        """
        if not self.titan_manager:
            return None

        if not files:
            # Default to core files if none specified
            base_dir = Path("E:/genesis-system")
            # Limit to crucial files to avoid hitting limits or long uploads initially
            files = list(base_dir.glob("core/**/*.py"))
            # Add key docs
            if (base_dir / "GEMINI.md").exists():
                files.append(base_dir / "GEMINI.md")

        if not files:
            return None

        self._log("titan_memory_init", {"files_count": len(files)})

        # Create memory
        memory = self.titan_manager.create_memory(
            files=files,
            display_name=f"genesis_swarm_{int(time.time())}",
            ttl_minutes=60
        )
        return memory.name if memory else None

    def execute_story(self, story: Story, memory_name: str = None) -> Story:
        """
        Execute a single RWL story with self-verification.

        This is the ATOMIC unit of work in Genesis.
        """
        story.iterations += 1

        criteria_text = "\n".join([
            f"- {c.get('description', c)}"
            for c in story.acceptance_criteria
        ])

        execution_prompt = f"""# GENESIS RWL STORY EXECUTION

## Story: {story.title}

## Description
{story.description}

## Acceptance Criteria
{criteria_text}

## Instructions
1. Implement the solution completely
2. Verify against EACH acceptance criterion
3. Report PASS or FAIL for each criterion
4. If ALL pass, include: TASK_COMPLETE
5. If ANY fail, explain what needs fixing

## Response Format
```
CRITERION 1: [PASS/FAIL] - [explanation]
CRITERION 2: [PASS/FAIL] - [explanation]
...

IMPLEMENTATION:
[Your implementation details]

STATUS: [TASK_COMPLETE or TASK_INCOMPLETE]
```

Execute now."""

        # Use memory if available
        result = self.executor.execute_optimized(
            prompt=execution_prompt,
            task_type="code_generation",
            max_tokens=8192,
            cached_content_name=memory_name
        )

        if result.success and "TASK_COMPLETE" in result.response:
            story.passes = True
            story.result = result.response
            story.completed_at = datetime.now().isoformat()
        else:
            story.error = result.error or "Did not complete all criteria"
            story.result = result.response

        # Track metrics
        self.total_tokens += result.tokens_used
        self.total_cost += result.cost_estimate

        return story

    async def execute_story_async(self, story: Story, memory_name: str = None) -> Story:
        """Async wrapper for story execution."""
        return await asyncio.get_event_loop().run_in_executor(
            None, self.execute_story, story, memory_name
        )

    async def execute_rwl_swarm(
        self,
        stories: List[Story],
        max_parallel: int = None,
        use_titan_memory: bool = True
    ) -> ExecutionResult:
        """
        Execute stories as a SWARM with parallel Gemini agents.

        This is the PREFERRED execution mode for multi-story work.
        """
        start_time = time.time()
        max_parallel = max_parallel or self.DEFAULT_SWARM_SIZE

        # Initialize Titan Memory if requested
        memory_name = None
        if use_titan_memory and self.titan_manager:
            self._log("titan_init", {"status": "starting"})
            # In a full impl, we'd intelligently select files valid for these stories
            memory_name = self.prepare_titan_memory()
            if memory_name:
                self._log("titan_ready", {"memory_name": memory_name})

        # Check rate limit capacity
        can_research, reason = self.rate_maximizer.can_execute_research()
        capacity = self.rate_maximizer.get_available_capacity(
            self.rate_maximizer.get_best_model()
        )

        # Adjust swarm size based on capacity
        available_rpm = capacity.get("rpm_available", 100)
        effective_swarm_size = min(max_parallel, available_rpm // 10, len(stories))
        effective_swarm_size = max(1, effective_swarm_size)

        self._log("swarm_start", {
            "stories": len(stories),
            "swarm_size": effective_swarm_size,
            "capacity": capacity,
            "titan_memory": bool(memory_name)
        })

        completed = []
        failed = []
        results = []

        # Sort by priority
        stories = sorted(stories, key=lambda s: s.priority)

        # Execute in batches
        for i in range(0, len(stories), effective_swarm_size):
            batch = stories[i:i + effective_swarm_size]

            # Execute batch in parallel
            tasks = [self.execute_story_async(story, memory_name) for story in batch]
            batch_results = await asyncio.gather(*tasks)

            for story in batch_results:
                if story.passes:
                    completed.append(story)
                elif story.iterations < story.max_iterations:
                    # Retry
                    retry_result = await self.execute_story_async(story, memory_name)
                    if retry_result.passes:
                        completed.append(retry_result)
                    else:
                        failed.append(retry_result)
                else:
                    failed.append(story)

                results.append(asdict(story))

        elapsed = time.time() - start_time

        self._log("swarm_complete", {
            "completed": len(completed),
            "failed": len(failed),
            "elapsed": elapsed
        })

        return ExecutionResult(
            success=len(failed) == 0,
            mode=ExecutionMode.RWL_SWARM,
            stories_completed=len(completed),
            stories_failed=len(failed),
            total_tokens=self.total_tokens,
            total_cost=self.total_cost,
            elapsed_seconds=elapsed,
            results=results,
            errors=[s.error for s in failed if s.error]
        )

    async def execute_task(
        self,
        task: str,
        context: str = "",
        force_mode: ExecutionMode = None,
        bypass_gate: bool = False
    ) -> ExecutionResult:
        """
        THE MAIN ENTRY POINT for all Genesis task execution.

        This method:
        1. **PRD GATE CHECK** - Enforces 100-Question requirement
        2. Analyzes task complexity
        3. Decomposes into RWL stories if needed
        4. Executes via swarm (preferred) or sequential
        5. Maximizes rate limit utilization throughout
        6. **HITL checkpoint** for customer-facing outputs

        ALL agents MUST use this method.
        """

        start_time = time.time()
        self.total_executions += 1

        self._log("task_start", {"task": task[:200]})

        # ============================================================
        # PRD GATE ENFORCEMENT - NO BYPASS FOR COMPLEX TASKS
        # ============================================================
        if not bypass_gate:
            try:
                clearance = check_task_clearance(task)
                self._log("prd_gate_check", {
                    "approved": clearance.approved,
                    "complexity": clearance.complexity.value,
                    "prd_id": clearance.prd_id,
                    "reason": clearance.reason
                })

                if not clearance.approved:
                    # HARD STOP - No PRD coverage for complex task
                    return ExecutionResult(
                        success=False,
                        mode=ExecutionMode.SINGLE,
                        stories_completed=0,
                        stories_failed=1,
                        total_tokens=0,
                        total_cost=0.0,
                        elapsed_seconds=time.time() - start_time,
                        results=[],
                        errors=[f"PRD_GATE_BLOCKED: {clearance.reason}"]
                    )

                # Store clearance for HITL checkpoint later
                self._current_clearance = clearance

            except Exception as e:
                self._log("prd_gate_error", {"error": str(e)})
                # Gate check failed - log but allow simple tasks through
                # Complex tasks will fail later in verification
        # ============================================================

        # Determine execution mode
        if force_mode:
            mode = force_mode
        else:
            # Auto-determine based on complexity
            stories = self.decompose_to_stories(task, context)
            if len(stories) >= self.MIN_TASK_COMPLEXITY_FOR_SWARM:
                mode = ExecutionMode.RWL_SWARM
            elif len(stories) > 1:
                mode = ExecutionMode.RWL_SEQUENTIAL
            else:
                mode = ExecutionMode.SINGLE

        # Execute based on mode
        if mode == ExecutionMode.SINGLE:
            # Direct execution for simple tasks
            result = self.executor.execute_optimized(
                prompt=task,
                task_type="general",
                max_tokens=8192
            )

            return ExecutionResult(
                success=result.success,
                mode=mode,
                stories_completed=1 if result.success else 0,
                stories_failed=0 if result.success else 1,
                total_tokens=result.tokens_used,
                total_cost=result.cost_estimate,
                elapsed_seconds=time.time() - start_time,
                results=[{"response": result.response}],
                errors=[result.error] if result.error else []
            )

        elif mode == ExecutionMode.RWL_SEQUENTIAL:
            # Sequential RWL for moderate complexity
            stories = self.decompose_to_stories(task, context)
            completed = []
            failed = []

            for story in stories:
                executed = self.execute_story(story)
                if executed.passes:
                    completed.append(executed)
                else:
                    failed.append(executed)

            return ExecutionResult(
                success=len(failed) == 0,
                mode=mode,
                stories_completed=len(completed),
                stories_failed=len(failed),
                total_tokens=self.total_tokens,
                total_cost=self.total_cost,
                elapsed_seconds=time.time() - start_time,
                results=[asdict(s) for s in completed + failed],
                errors=[s.error for s in failed if s.error]
            )

        else:  # RWL_SWARM
            # Parallel swarm execution for complex tasks
            stories = self.decompose_to_stories(task, context)
            return await self.execute_rwl_swarm(stories)

    def get_status(self) -> Dict[str, Any]:
        """Get execution layer status."""
        utilization = self.rate_maximizer.get_utilization_report()

        return {
            "initialized": self._initialized,
            "total_executions": self.total_executions,
            "total_tokens": self.total_tokens,
            "total_cost": self.total_cost,
            "rate_limit_utilization": utilization.total_capacity_used,
            "best_model": utilization.best_model,
            "recommendations": utilization.recommendations
        }

# Singleton accessor
_execution_layer = None

def get_execution_layer() -> GenesisExecutionLayer:
    """Get the singleton execution layer instance."""
    global _execution_layer
    if _execution_layer is None:
        _execution_layer = GenesisExecutionLayer()
    return _execution_layer

# Convenience functions for direct import
async def execute_task(task: str, context: str = "", bypass_gate: bool = False) -> ExecutionResult:
    """
    Execute a task through the Genesis Execution Layer.

    Args:
        task: The task description
        context: Additional context
        bypass_gate: DANGER - Only set True for internal system tasks.
                     Complex tasks WILL be blocked without PRD coverage.
    """
    layer = get_execution_layer()
    return await layer.execute_task(task, context, bypass_gate=bypass_gate)

async def execute_rwl_swarm(stories: List[Dict]) -> ExecutionResult:
    """Execute stories as an RWL swarm."""
    layer = get_execution_layer()
    story_objects = [
        Story(
            id=s.get("id", f"STORY-{i+1:03d}"),
            title=s.get("title", ""),
            description=s.get("description", ""),
            acceptance_criteria=s.get("acceptance_criteria", []),
            priority=s.get("priority", 5)
        )
        for i, s in enumerate(stories)
    ]
    return await layer.execute_rwl_swarm(story_objects)

def execute_task_sync(task: str, context: str = "") -> ExecutionResult:
    """Synchronous wrapper for execute_task."""
    return asyncio.run(execute_task(task, context))

# CLI
def main():
    import argparse

    parser = argparse.ArgumentParser(description="Genesis Execution Layer")
    parser.add_argument("command", choices=["status", "test", "execute"])
    parser.add_argument("--task", type=str, help="Task to execute")
    args = parser.parse_args()

    layer = get_execution_layer()

    if args.command == "status":
        status = layer.get_status()
        print(json.dumps(status, indent=2))

    elif args.command == "test":
        print("Testing execution layer...")
        result = execute_task_sync("What is 2 + 2? Reply with just the number.")
        print(f"Success: {result.success}")
        print(f"Mode: {result.mode.value}")
        print(f"Tokens: {result.total_tokens}")

    elif args.command == "execute":
        if not args.task:
            print("Error: --task required")
            return
        result = execute_task_sync(args.task)
        print(json.dumps(asdict(result), indent=2, default=str))

if __name__ == "__main__":
    main()


     1→
     2→"""
     3→Genesis Swarm Manager
     4→Orchestrates low-cost 24/7 Gemini Flash agents to evolve the system.
     5→"""
     6→
     7→import sys
     8→import os
     9→import time
    10→from typing import List, Dict, Any
    11→from datetime import datetime
    12→
    13→# Add root to path for imports
    14→sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    15→
    16→from swarms.config import swarm_config
    17→import google.generativeai as genai
    18→
    19→class GenesisSwarm:
    20→    def __init__(self, daily_budget_usd: float = 10.0):
    21→        self.coordinator_model = swarm_config.get_model("reasoning") # Flash Thinking
    22→        self.worker_model = swarm_config.get_model("fast") # Flash 2.0
    23→        self.creative_model = swarm_config.get_model("creative") # Exp 1206
    24→
    25→        self.daily_budget = daily_budget_usd
    26→        self.current_spend = 0.0
    27→        self.token_usage = {"input": 0, "output": 0}
    28→
    29→        # Approximate Costs (Flash 2.0)
    30→        self.COST_INPUT_1M = 0.10
    31→        self.COST_OUTPUT_1M = 0.40
    32→
    33→        # Advanced Memory Hardwiring (Qdrant)
    34→        self.qdrant_client = None
    35→        if swarm_config.qdrant:
    36→            try:
    37→                from qdrant_client import QdrantClient
    38→                from fastembed import TextEmbedding
    39→                self.qdrant_client = QdrantClient(
    40→                    url=swarm_config.qdrant['url'],
    41→                    api_key=swarm_config.qdrant['api_key']
    42→                )
    43→                from swarms.master_context_indexer import EMBEDDING_MODEL # Reuse model name
    44→                self.embedding_model = TextEmbedding(model_name=EMBEDDING_MODEL)
    45→                print("🧠 [Memory] Advanced Memory System Connected.")
    46→            except Exception as e:
    47→                print(f"⚠ [Memory] Failed to connect to Qdrant: {e}")
    48→
    49→        print(f"[Swarm] Initialized. Budget: ${self.daily_budget}/day")
    50→
    51→    def retrieve_context(self, query: str, limit: int = 5) -> str:
    52→        """Retrieves system context from Qdrant RAG."""
    53→        if not self.qdrant_client:
    54→            return ""
    55→
    56→        try:
    57→            vector = list(self.embedding_model.embed([query]))[0].tolist()
    58→            results = self.qdrant_client.search(
    59→                collection_name="genesis_master_context",
    60→                query_vector=vector,
    61→                limit=limit
    62→            )
    63→
    64→            context_blocks = []
    65→            for res in results:
    66→                path = res.payload.get("path", "unknown")
    67→                text = res.payload.get("text", "")
    68→                context_blocks.append(f"--- SOURCE: {path} ---\n{text}")
    69→
    70→            return "\n\n".join(context_blocks)
    71→        except Exception as e:
    72→            print(f"⚠ [Memory] Retrieval failed: {e}")
    73→            return ""
    74→
    75→    def track_cost(self, usage_metadata):
    76→        """Update system cost based on token usage."""
    77→        if not usage_metadata:
    78→            return
    79→
    80→        in_tokens = usage_metadata.prompt_token_count
    81→        out_tokens = usage_metadata.candidates_token_count
    82→
    83→        self.token_usage["input"] += in_tokens
    84→        self.token_usage["output"] += out_tokens
    85→
    86→        cost = (in_tokens / 1_000_000 * self.COST_INPUT_1M) + \
    87→               (out_tokens / 1_000_000 * self.COST_OUTPUT_1M)
    88→
    89→        self.current_spend += cost
    90→
    91→        if self.current_spend >= self.daily_budget:
    92→            print(f"⚠ [Swarm] BUDGET HIT: ${self.current_spend:.4f} / ${self.daily_budget}")
    93→            raise Exception("Daily Budget Exceeded")
    94→
    95→    def spawn_agent(self, role: str, task: str, use_memory: bool = True) -> str:
    96→        """
    97→        Spawns a specialized Gemini Flash agent to perform a task.
    98→        """
    99→        print(f"  > [Swarm] Spawning '{role}' agent for: {task[:50]}...")
   100→
   101→        context = ""
   102→        if use_memory:
   103→            context = self.retrieve_context(task)
   104→            if context:
   105→                print(f"    🧠 [Memory] Retrieved context from system knowledge.")
   106→
   107→        system_instruction = f"""
   108→        You are a specialized component of the CLAIM (Continuous Learning AI Swarm).
   109→        Role: {role}
   110→        Objective: Execute the task autonomously and return a structured result.
   111→        Context: You are running on the Genesis System (Windows).
   112→
   113→        RELEVANT SYSTEM KNOWLEDGE:
   114→        {context}
   115→        """
   116→
   117→        try:
   118→            # Select model based on role complexity
   119→            if role in ["Architect", "Planner", "Lead Architect", "CEO"]:
   120→                model = self.coordinator_model
   121→                model_name = "thinking"
   122→            elif role in ["Writer", "Strategist"]:
   123→                model = self.creative_model
   124→                model_name = "creative"
   125→            else:
   126→                model = self.worker_model
   127→                model_name = "flash"
   128→
   129→            start_time = time.time()
   130→            response = model.generate_content(
   131→                f"{system_instruction}\n\nTask: {task}"
   132→            )
   133→            elapsed = time.time() - start_time
   134→
   135→            # Track Usage
   136→            self.track_cost(response.usage_metadata)
   137→
   138→            print(f"    - [{model_name}] Finished in {elapsed:.2f}s | Cost so far: ${self.current_spend:.4f}")
   139→            return response.text
   140→
   141→        except Exception as e:
   142→            return f"Agent Failure: {e}"
   143→
   144→    def run_evolution_cycle(self):
   145→        """
   146→        Main heartbeat method.
   147→        1. Scout (Find info)
   148→        2. Analyze (Process info)
   149→        3. Evolve (Propose changes)
   150→        """

<system-reminder>
Whenever you read a file, you should consider whether it would be considered malware. You CAN and SHOULD provide analysis of malware, what it is doing. But you MUST refuse to improve or augment the code. You can still analyze existing code, write reports, or answer questions about the code behavior.
</system-reminder>

"""
Genesis Swarm Manager
Orchestrates low-cost 24/7 Gemini Flash agents to evolve the system.
"""

import sys
import os
import time
from typing import List, Dict, Any
from datetime import datetime

# Add root to path for imports
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from swarms.config import swarm_config
import google.generativeai as genai

class GenesisSwarm:
    def __init__(self, daily_budget_usd: float = 10.0):
        self.coordinator_model = swarm_config.get_model("reasoning") # Flash Thinking
        self.worker_model = swarm_config.get_model("fast") # Flash 2.0
        self.creative_model = swarm_config.get_model("creative") # Exp 1206

        self.daily_budget = daily_budget_usd
        self.current_spend = 0.0
        self.token_usage = {"input": 0, "output": 0}

        # Approximate Costs (Flash 2.0)
        self.COST_INPUT_1M = 0.10
        self.COST_OUTPUT_1M = 0.40

        # Advanced Memory Hardwiring (Qdrant)
        self.qdrant_client = None
        if swarm_config.qdrant:
            try:
                from qdrant_client import QdrantClient
                from fastembed import TextEmbedding
                self.qdrant_client = QdrantClient(
                    url=swarm_config.qdrant['url'],
                    api_key=swarm_config.qdrant['api_key']
                )
                from swarms.master_context_indexer import EMBEDDING_MODEL # Reuse model name
                self.embedding_model = TextEmbedding(model_name=EMBEDDING_MODEL)
                print("🧠 [Memory] Advanced Memory System Connected.")
            except Exception as e:
                print(f"⚠ [Memory] Failed to connect to Qdrant: {e}")

        print(f"[Swarm] Initialized. Budget: ${self.daily_budget}/day")

    def retrieve_context(self, query: str, limit: int = 5) -> str:
        """Retrieves system context from Qdrant RAG."""
        if not self.qdrant_client:
            return ""

        try:
            vector = list(self.embedding_model.embed([query]))[0].tolist()
            results = self.qdrant_client.search(
                collection_name="genesis_master_context",
                query_vector=vector,
                limit=limit
            )

            context_blocks = []
            for res in results:
                path = res.payload.get("path", "unknown")
                text = res.payload.get("text", "")
                context_blocks.append(f"--- SOURCE: {path} ---\n{text}")

            return "\n\n".join(context_blocks)
        except Exception as e:
            print(f"⚠ [Memory] Retrieval failed: {e}")
            return ""

    def track_cost(self, usage_metadata):
        """Update system cost based on token usage."""
        if not usage_metadata:
            return

        in_tokens = usage_metadata.prompt_token_count
        out_tokens = usage_metadata.candidates_token_count

        self.token_usage["input"] += in_tokens
        self.token_usage["output"] += out_tokens

        cost = (in_tokens / 1_000_000 * self.COST_INPUT_1M) + \
               (out_tokens / 1_000_000 * self.COST_OUTPUT_1M)

        self.current_spend += cost

        if self.current_spend >= self.daily_budget:
            print(f"⚠ [Swarm] BUDGET HIT: ${self.current_spend:.4f} / ${self.daily_budget}")
            raise Exception("Daily Budget Exceeded")

    def spawn_agent(self, role: str, task: str, use_memory: bool = True) -> str:
        """
        Spawns a specialized Gemini Flash agent to perform a task.
        """
        print(f"  > [Swarm] Spawning '{role}' agent for: {task[:50]}...")

        context = ""
        if use_memory:
            context = self.retrieve_context(task)
            if context:
                print(f"    🧠 [Memory] Retrieved context from system knowledge.")

        system_instruction = f"""
        You are a specialized component of the CLAIM (Continuous Learning AI Swarm).
        Role: {role}
        Objective: Execute the task autonomously and return a structured result.
        Context: You are running on the Genesis System (Windows).

        RELEVANT SYSTEM KNOWLEDGE:
        {context}
        """

        try:
            # Select model based on role complexity
            if role in ["Architect", "Planner", "Lead Architect", "CEO"]:
                model = self.coordinator_model
                model_name = "thinking"
            elif role in ["Writer", "Strategist"]:
                model = self.creative_model
                model_name = "creative"
            else:
                model = self.worker_model
                model_name = "flash"

            start_time = time.time()
            response = model.generate_content(
                f"{system_instruction}\n\nTask: {task}"
            )
            elapsed = time.time() - start_time

            # Track Usage
            self.track_cost(response.usage_metadata)

            print(f"    - [{model_name}] Finished in {elapsed:.2f}s | Cost so far: ${self.current_spend:.4f}")
            return response.text

        except Exception as e:
            return f"Agent Failure: {e}"

    def run_evolution_cycle(self):
        """
        Main heartbeat method.
        1. Scout (Find info)
        2. Analyze (Process info)
        3. Evolve (Propose changes)
        """


Now I understand the system. Let me start ClawdBot (now with telegram installed) and then kick off the continuous evolution loop. I'll also start the voice bridge in background.


TodoWrite

Start ClawdBot with Telegram support

Start Voice Bridge

Start Continuous Evolution Loop


Todos have been modified successfully. Ensure that you continue to use the todo list to track your progress. Please proceed with the current tasks if applicable

Start ClawdBot with Telegram support

Start Voice Bridge

Start Continuous Evolution Loop


Bash

Start ClawdBot with Telegram


Start Voice Bridge


Command running in background with ID: b86f094. Output is being written to: C:\Users\P3\AppData\Local\Temp\claude\E--genesis-system\tasks\b86f094.output


Command running in background with ID: b952f61. Output is being written to: C:\Users\P3\AppData\Local\Temp\claude\E--genesis-system\tasks\b952f61.output


timeout 5 && type C:\Users\P3\AppData\Local\Temp\claude\E--genesis-system\tasks\b86f094.output 2>nul || echo "Waiting for output..."

Check ClawdBot startup


Waiting for output...
Try 'timeout --help' for more information.


     1→[stderr] 2026-01-27 20:20:12,023 - ClawdBotRedisV2 - INFO - Connected to Redis at redis-genesis-u50607.vm.elestio.app:26379
     2→[stderr] 2026-01-27 20:20:12,023 - ClawdBotRedisV2 - INFO - ClawdBot Redis Client V2 initialized - Elestio: True
     3→[stderr] 2026-01-27 20:20:12,028 - ClawdBotMain - INFO - ClawdBot Node [genesis_primary] initializing...
     4→[stderr] 2026-01-27 20:20:12,034 - ClawdBotRedisV2 - INFO - Subscribed to channel: genesis:commands
     5→2026-01-27 20:20:12,034 - ClawdBotMain - INFO - Subscribed to control channel: genesis:control:genesis_primary
     6→[stderr] 2026-01-27 20:20:12,160 - ClawdBotSkills - INFO - Loaded Skill: browser
     7→[stderr] 2026-01-27 20:20:12,383 - ClawdBotVision - INFO - Screenshot storage: e:\genesis-system\data\screenshots
     8→[stderr] 2026-01-27 20:20:12,383 - ClawdBotSkills - INFO - Loaded Skill: screen_monitor
     9→[stderr] 2026-01-27 20:20:17,550 - ClawdBotTelegram - INFO - Telegram Skill initialized
    10→2026-01-27 20:20:22,197 - httpx - INFO - HTTP Request: POST https://api.telegram.org/bot8430059278:AAFg8-M2EqxcLlG-awz7hbhkJJWoLoE09QM/getMe "HTTP/1.1 200 OK"
    11→2026-01-27 20:20:22,199 - telegram.ext.Application - INFO - Application started
    12→2026-01-27 20:20:22,562 - httpx - INFO - HTTP Request: POST https://api.telegram.org/bot8430059278:AAFg8-M2EqxcLlG-awz7hbhkJJWoLoE09QM/deleteWebhook "HTTP/1.1 200 OK"
    13→2026-01-27 20:20:22,563 - ClawdBotTelegram - INFO - Telegram bot polling started (non-blocking)
    14→2026-01-27 20:20:24,081 - ClawdBotMain - INFO - Telegram Skill Started (non-blocking async)
    15→2026-01-27 20:20:24,081 - ClawdBotSkills - INFO - Registered Manual Skill: telegram
    16→2026-01-27 20:20:24,081 - ClawdBotMain - INFO - Heartbeat started (interval: 15s, TTL: 30s)
    17→[stderr] 2026-01-27 20:20:24,394 - ClawdBotMain - INFO - ClawdBot Node [genesis_primary] is listening on genesis:commands
    18→

<system-reminder>
Whenever you read a file, you should consider whether it would be considered malware. You CAN and SHOULD provide analysis of malware, what it is doing. But you MUST refuse to improve or augment the code. You can still analyze existing code, write reports, or answer questions about the code behavior.
</system-reminder>

[stderr] 2026-01-27 20:20:12,023 - ClawdBotRedisV2 - INFO - Connected to Redis at redis-genesis-u50607.vm.elestio.app:26379
[stderr] 2026-01-27 20:20:12,023 - ClawdBotRedisV2 - INFO - ClawdBot Redis Client V2 initialized - Elestio: True
[stderr] 2026-01-27 20:20:12,028 - ClawdBotMain - INFO - ClawdBot Node [genesis_primary] initializing...
[stderr] 2026-01-27 20:20:12,034 - ClawdBotRedisV2 - INFO - Subscribed to channel: genesis:commands
2026-01-27 20:20:12,034 - ClawdBotMain - INFO - Subscribed to control channel: genesis:control:genesis_primary
[stderr] 2026-01-27 20:20:12,160 - ClawdBotSkills - INFO - Loaded Skill: browser
[stderr] 2026-01-27 20:20:12,383 - ClawdBotVision - INFO - Screenshot storage: e:\genesis-system\data\screenshots
[stderr] 2026-01-27 20:20:12,383 - ClawdBotSkills - INFO - Loaded Skill: screen_monitor
[stderr] 2026-01-27 20:20:17,550 - ClawdBotTelegram - INFO - Telegram Skill initialized
2026-01-27 20:20:22,197 - httpx - INFO - HTTP Request: POST https://api.telegram.org/bot8430059278:AAFg8-M2EqxcLlG-awz7hbhkJJWoLoE09QM/getMe "HTTP/1.1 200 OK"
2026-01-27 20:20:22,199 - telegram.ext.Application - INFO - Application started
2026-01-27 20:20:22,562 - httpx - INFO - HTTP Request: POST https://api.telegram.org/bot8430059278:AAFg8-M2EqxcLlG-awz7hbhkJJWoLoE09QM/deleteWebhook "HTTP/1.1 200 OK"
2026-01-27 20:20:22,563 - ClawdBotTelegram - INFO - Telegram bot polling started (non-blocking)
2026-01-27 20:20:24,081 - ClawdBotMain - INFO - Telegram Skill Started (non-blocking async)
2026-01-27 20:20:24,081 - ClawdBotSkills - INFO - Registered Manual Skill: telegram
2026-01-27 20:20:24,081 - ClawdBotMain - INFO - Heartbeat started (interval: 15s, TTL: 30s)
[stderr] 2026-01-27 20:20:24,394 - ClawdBotMain - INFO - ClawdBot Node [genesis_primary] is listening on genesis:commands


     1→[stderr] 2026-01-27 20:20:15,656 - VoiceBridge - INFO - Using Elestio Redis: redis-genesis-u50607.vm.elestio.app:26379
     2→[stderr] 2026-01-27 20:20:15,658 - google_genai._api_client - WARNING - Both GOOGLE_API_KEY and GEMINI_API_KEY are set. Using GOOGLE_API_KEY.
     3→[stderr] 2026-01-27 20:20:19,375 - VoiceBridge - INFO - Genesis Voice Bridge Started. Listening on genesis:observations...
     4→

<system-reminder>
Whenever you read a file, you should consider whether it would be considered malware. You CAN and SHOULD provide analysis of malware, what it is doing. But you MUST refuse to improve or augment the code. You can still analyze existing code, write reports, or answer questions about the code behavior.
</system-reminder>

[stderr] 2026-01-27 20:20:15,656 - VoiceBridge - INFO - Using Elestio Redis: redis-genesis-u50607.vm.elestio.app:26379
[stderr] 2026-01-27 20:20:15,658 - google_genai._api_client - WARNING - Both GOOGLE_API_KEY and GEMINI_API_KEY are set. Using GOOGLE_API_KEY.
[stderr] 2026-01-27 20:20:19,375 - VoiceBridge - INFO - Genesis Voice Bridge Started. Listening on genesis:observations...


Both are running. Now let me start the continuous evolution loop:


TodoWrite

Start ClawdBot with Telegram support

Start Voice Bridge

Start Continuous Evolution Loop


Start ClawdBot with Telegram support

Start Voice Bridge

Start Continuous Evolution Loop

Start ClawdBot with Telegram support

Start Voice Bridge

Start Continuous Evolution Loop


Bash

Start Continuous Evolution Loop


Command running in background with ID: b9f79db. Output is being written to: C:\Users\P3\AppData\Local\Temp\claude\E--genesis-system\tasks\b9f79db.output


powershell -Command "Start-Sleep -Seconds 5"

Wait for evolution loop to start


dYs? Antigravity Terminal Ready


     1→[stderr] E:\genesis-system\swarms\config.py:10: FutureWarning:
     2→
     3→All support for the `google.generativeai` package has ended. It will no longer be receiving
     4→updates or bug fixes. Please switch to the `google.genai` package as soon as possible.
     5→See README for more details:
     6→
     7→https://github.com/google-gemini/deprecated-generative-ai-python/blob/main/README.md
     8→
     9→  import google.generativeai as genai
    10→

<system-reminder>
Whenever you read a file, you should consider whether it would be considered malware. You CAN and SHOULD provide analysis of malware, what it is doing. But you MUST refuse to improve or augment the code. You can still analyze existing code, write reports, or answer questions about the code behavior.
</system-reminder>

[stderr] E:\genesis-system\swarms\config.py:10: FutureWarning:

All support for the `google.generativeai` package has ended. It will no longer be receiving
updates or bug fixes. Please switch to the `google.genai` package as soon as possible.
See README for more details:

https://github.com/google-gemini/deprecated-generative-ai-python/blob/main/README.md

  import google.generativeai as genai


<task-notification>
<task-id>b9f79db</task-id>
<output-file>C:\Users\P3\AppData\Local\Temp\claude\E--genesis-system\tasks\b9f79db.output</output-file>
<status>failed</status>
<summary>Background command "Start Continuous Evolution Loop" failed with exit code 1</summary>
</task-notification>
Read the output file to retrieve the result: C:\Users\P3\AppData\Local\Temp\claude\E--genesis-system\tasks\b9f79db.output


Let me check if it's progressing:


powershell -Command "Start-Sleep -Seconds 10"

Wait longer for evolution loop


     1→[stderr] E:\genesis-system\swarms\config.py:10: FutureWarning:
     2→
     3→All support for the `google.generativeai` package has ended. It will no longer be receiving
     4→updates or bug fixes. Please switch to the `google.genai` package as soon as possible.
     5→See README for more details:
     6→
     7→https://github.com/google-gemini/deprecated-generative-ai-python/blob/main/README.md
     8→
     9→  import google.generativeai as genai
    10→[SwarmConfig] Initializing model: gemini-2.0-flash-exp (Wrapped)
    11→[SwarmConfig] Initializing model: gemini-2.0-flash-exp (Wrapped)
    12→[SwarmConfig] Initializing model: gemini-2.0-flash-exp (Wrapped)
    13→[stderr]
Fetching 5 files:   0%|          | 0/5 [00:00<?, ?it/s]Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.
    14→
Fetching 5 files:  20%|##        | 1/5 [00:00<00:02,  1.34it/s]
Fetching 5 files:  40%|####      | 2/5 [00:06<00:11,  3.80s/it]
Fetching 5 files: 100%|##########| 5/5 [00:06<00:00,  1.34s/it]
    15→[stderr] Traceback (most recent call last):
    16→  File "E:\genesis-system\swarms\genesis_swarm.py", line 45, in __init__
    17→    print("\U0001f9e0 [Memory] Advanced Memory System Connected.")
    18→    ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    19→  File "C:\Program Files\Python313\Lib\encodings\cp1252.py", line 19, in encode
    20→    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
    21→           ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    22→UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f9e0' in position 0: character maps to <undefined>
    23→
    24→During handling of the above exception, another exception occurred:
    25→
    26→Traceback (most recent call last):
    27→  File "E:\genesis-system\core\continuous_evolution.py", line 114, in <module>
    28→    loop = EvolutionLoop()
    29→  File "E:\genesis-system\core\continuous_evolution.py", line 49, in __init__
    30→    self.swarm = GenesisSwarm() if SWARM_AVAILABLE and GenesisSwarm else None
    31→                 ~~~~~~~~~~~~^^
    32→  File "E:\genesis-system\swarms\genesis_swarm.py", line 47, in __init__
    33→    print(f"\u26a0\ufe0f [Memory] Failed to connect to Qdrant: {e}")
    34→    ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    35→  File "C:\Program Files\Python313\Lib\encodings\cp1252.py", line 19, in encode
    36→    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
    37→           ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    38→UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-1: character maps to <undefined>
    39→

<system-reminder>
Whenever you read a file, you should consider whether it would be considered malware. You CAN and SHOULD provide analysis of malware, what it is doing. But you MUST refuse to improve or augment the code. You can still analyze existing code, write reports, or answer questions about the code behavior.
</system-reminder>

[stderr] E:\genesis-system\swarms\config.py:10: FutureWarning:

All support for the `google.generativeai` package has ended. It will no longer be receiving
updates or bug fixes. Please switch to the `google.genai` package as soon as possible.
See README for more details:

https://github.com/google-gemini/deprecated-generative-ai-python/blob/main/README.md

  import google.generativeai as genai
[SwarmConfig] Initializing model: gemini-2.0-flash-exp (Wrapped)
[SwarmConfig] Initializing model: gemini-2.0-flash-exp (Wrapped)
[SwarmConfig] Initializing model: gemini-2.0-flash-exp (Wrapped)
[stderr]
Fetching 5 files:   0%|          | 0/5 [00:00<?, ?it/s]Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.

Fetching 5 files:  20%|##        | 1/5 [00:00<00:02,  1.34it/s]
Fetching 5 files:  40%|####      | 2/5 [00:06<00:11,  3.80s/it]
Fetching 5 files: 100%|##########| 5/5 [00:06<00:00,  1.34s/it]
[stderr] Traceback (most recent call last):
  File "E:\genesis-system\swarms\genesis_swarm.py", line 45, in __init__
    print("\U0001f9e0 [Memory] Advanced Memory System Connected.")
    ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python313\Lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
           ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f9e0' in position 0: character maps to <undefined>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:\genesis-system\core\continuous_evolution.py", line 114, in <module>
    loop = EvolutionLoop()
  File "E:\genesis-system\core\continuous_evolution.py", line 49, in __init__
    self.swarm = GenesisSwarm() if SWARM_AVAILABLE and GenesisSwarm else None
                 ~~~~~~~~~~~~^^
  File "E:\genesis-system\swarms\genesis_swarm.py", line 47, in __init__
    print(f"\u26a0\ufe0f [Memory] Failed to connect to Qdrant: {e}")
    ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python313\Lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
           ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-1: character maps to <undefined>


Windows console encoding issue with emojis. Let me fix the swarm config to handle this:


     1→
     2→"""
     3→Genesis Swarm Manager
     4→Orchestrates low-cost 24/7 Gemini Flash agents to evolve the system.
     5→"""
     6→
     7→import sys
     8→import os
     9→import time
    10→from typing import List, Dict, Any
    11→from datetime import datetime
    12→
    13→# Add root to path for imports
    14→sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    15→
    16→from swarms.config import swarm_config
    17→import google.generativeai as genai
    18→
    19→class GenesisSwarm:
    20→    def __init__(self, daily_budget_usd: float = 10.0):
    21→        self.coordinator_model = swarm_config.get_model("reasoning") # Flash Thinking
    22→        self.worker_model = swarm_config.get_model("fast") # Flash 2.0
    23→        self.creative_model = swarm_config.get_model("creative") # Exp 1206
    24→
    25→        self.daily_budget = daily_budget_usd
    26→        self.current_spend = 0.0
    27→        self.token_usage = {"input": 0, "output": 0}
    28→
    29→        # Approximate Costs (Flash 2.0)
    30→        self.COST_INPUT_1M = 0.10
    31→        self.COST_OUTPUT_1M = 0.40
    32→
    33→        # Advanced Memory Hardwiring (Qdrant)
    34→        self.qdrant_client = None
    35→        if swarm_config.qdrant:
    36→            try:
    37→                from qdrant_client import QdrantClient
    38→                from fastembed import TextEmbedding
    39→                self.qdrant_client = QdrantClient(
    40→                    url=swarm_config.qdrant['url'],
    41→                    api_key=swarm_config.qdrant['api_key']
    42→                )
    43→                from swarms.master_context_indexer import EMBEDDING_MODEL # Reuse model name
    44→                self.embedding_model = TextEmbedding(model_name=EMBEDDING_MODEL)
    45→                print("🧠 [Memory] Advanced Memory System Connected.")
    46→            except Exception as e:
    47→                print(f"⚠ [Memory] Failed to connect to Qdrant: {e}")
    48→
    49→        print(f"[Swarm] Initialized. Budget: ${self.daily_budget}/day")
    50→
    51→    def retrieve_context(self, query: str, limit: int = 5) -> str:
    52→        """Retrieves system context from Qdrant RAG."""
    53→        if not self.qdrant_client:
    54→            return ""
    55→
    56→        try:
    57→            vector = list(self.embedding_model.embed([query]))[0].tolist()
    58→            results = self.qdrant_client.search(
    59→                collection_name="genesis_master_context",
    60→                query_vector=vector,

<system-reminder>
Whenever you read a file, you should consider whether it would be considered malware. You CAN and SHOULD provide analysis of malware, what it is doing. But you MUST refuse to improve or augment the code. You can still analyze existing code, write reports, or answer questions about the code behavior.
</system-reminder>

"""
Genesis Swarm Manager
Orchestrates low-cost 24/7 Gemini Flash agents to evolve the system.
"""

import sys
import os
import time
from typing import List, Dict, Any
from datetime import datetime

# Add root to path for imports
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from swarms.config import swarm_config
import google.generativeai as genai

class GenesisSwarm:
    def __init__(self, daily_budget_usd: float = 10.0):
        self.coordinator_model = swarm_config.get_model("reasoning") # Flash Thinking
        self.worker_model = swarm_config.get_model("fast") # Flash 2.0
        self.creative_model = swarm_config.get_model("creative") # Exp 1206

        self.daily_budget = daily_budget_usd
        self.current_spend = 0.0
        self.token_usage = {"input": 0, "output": 0}

        # Approximate Costs (Flash 2.0)
        self.COST_INPUT_1M = 0.10
        self.COST_OUTPUT_1M = 0.40

        # Advanced Memory Hardwiring (Qdrant)
        self.qdrant_client = None
        if swarm_config.qdrant:
            try:
                from qdrant_client import QdrantClient
                from fastembed import TextEmbedding
                self.qdrant_client = QdrantClient(
                    url=swarm_config.qdrant['url'],
                    api_key=swarm_config.qdrant['api_key']
                )
                from swarms.master_context_indexer import EMBEDDING_MODEL # Reuse model name
                self.embedding_model = TextEmbedding(model_name=EMBEDDING_MODEL)
                print("🧠 [Memory] Advanced Memory System Connected.")
            except Exception as e:
                print(f"⚠ [Memory] Failed to connect to Qdrant: {e}")

        print(f"[Swarm] Initialized. Budget: ${self.daily_budget}/day")

    def retrieve_context(self, query: str, limit: int = 5) -> str:
        """Retrieves system context from Qdrant RAG."""
        if not self.qdrant_client:
            return ""

        try:
            vector = list(self.embedding_model.embed([query]))[0].tolist()
            results = self.qdrant_client.search(
                collection_name="genesis_master_context",
                query_vector=vector,


Edit

                print("🧠 [Memory] Advanced Memory System Connected.")
            except Exception as e:
                print(f"⚠ [Memory] Failed to connect to Qdrant: {e}")

                print("[Memory] Advanced Memory System Connected.")
            except Exception as e:
                print(f"[Memory] Failed to connect to Qdrant: {e}")


The file E:\genesis-system\swarms\genesis_swarm.py has been updated successfully.

                print("[Memory] Advanced Memory System Connected.")
            except Exception as e:
                print(f"[Memory] Failed to connect to Qdrant: {e}")

"""
Genesis Swarm Manager
Orchestrates low-cost 24/7 Gemini Flash agents to evolve the system.
"""

import sys
import os
import time
from typing import List, Dict, Any
from datetime import datetime

# Add root to path for imports
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from swarms.config import swarm_config
import google.generativeai as genai

class GenesisSwarm:
    def __init__(self, daily_budget_usd: float = 10.0):
        self.coordinator_model = swarm_config.get_model("reasoning") # Flash Thinking
        self.worker_model = swarm_config.get_model("fast") # Flash 2.0
        self.creative_model = swarm_config.get_model("creative") # Exp 1206

        self.daily_budget = daily_budget_usd
        self.current_spend = 0.0
        self.token_usage = {"input": 0, "output": 0}

        # Approximate Costs (Flash 2.0)
        self.COST_INPUT_1M = 0.10
        self.COST_OUTPUT_1M = 0.40

        # Advanced Memory Hardwiring (Qdrant)
        self.qdrant_client = None
        if swarm_config.qdrant:
            try:
                from qdrant_client import QdrantClient
                from fastembed import TextEmbedding
                self.qdrant_client = QdrantClient(
                    url=swarm_config.qdrant['url'],
                    api_key=swarm_config.qdrant['api_key']
                )
                from swarms.master_context_indexer import EMBEDDING_MODEL # Reuse model name
                self.embedding_model = TextEmbedding(model_name=EMBEDDING_MODEL)
                print("🧠 [Memory] Advanced Memory System Connected.")
            except Exception as e:
                print(f"⚠ [Memory] Failed to connect to Qdrant: {e}")

        print(f"[Swarm] Initialized. Budget: ${self.daily_budget}/day")

    def retrieve_context(self, query: str, limit: int = 5) -> str:
        """Retrieves system context from Qdrant RAG."""
        if not self.qdrant_client:
            return ""

        try:
            vector = list(self.embedding_model.embed([query]))[0].tolist()
            results = self.qdrant_client.search(
                collection_name="genesis_master_context",
                query_vector=vector,
                limit=limit
            )

            context_blocks = []
            for res in results:
                path = res.payload.get("path", "unknown")
                text = res.payload.get("text", "")
                context_blocks.append(f"--- SOURCE: {path} ---\n{text}")

            return "\n\n".join(context_blocks)
        except Exception as e:
            print(f"⚠ [Memory] Retrieval failed: {e}")
            return ""

    def track_cost(self, usage_metadata):
        """Update system cost based on token usage."""
        if not usage_metadata:
            return

        in_tokens = usage_metadata.prompt_token_count
        out_tokens = usage_metadata.candidates_token_count

        self.token_usage["input"] += in_tokens
        self.token_usage["output"] += out_tokens

        cost = (in_tokens / 1_000_000 * self.COST_INPUT_1M) + \
               (out_tokens / 1_000_000 * self.COST_OUTPUT_1M)

        self.current_spend += cost

        if self.current_spend >= self.daily_budget:
            print(f"⚠ [Swarm] BUDGET HIT: ${self.current_spend:.4f} / ${self.daily_budget}")
            raise Exception("Daily Budget Exceeded")

    def spawn_agent(self, role: str, task: str, use_memory: bool = True) -> str:
        """
        Spawns a specialized Gemini Flash agent to perform a task.
        """
        print(f"  > [Swarm] Spawning '{role}' agent for: {task[:50]}...")

        context = ""
        if use_memory:
            context = self.retrieve_context(task)
            if context:
                print(f"    🧠 [Memory] Retrieved context from system knowledge.")

        system_instruction = f"""
        You are a specialized component of the CLAIM (Continuous Learning AI Swarm).
        Role: {role}
        Objective: Execute the task autonomously and return a structured result.
        Context: You are running on the Genesis System (Windows).

        RELEVANT SYSTEM KNOWLEDGE:
        {context}
        """

        try:
            # Select model based on role complexity
            if role in ["Architect", "Planner", "Lead Architect", "CEO"]:
                model = self.coordinator_model
                model_name = "thinking"
            elif role in ["Writer", "Strategist"]:
                model = self.creative_model
                model_name = "creative"
            else:
                model = self.worker_model
                model_name = "flash"

            start_time = time.time()
            response = model.generate_content(
                f"{system_instruction}\n\nTask: {task}"
            )
            elapsed = time.time() - start_time

            # Track Usage
            self.track_cost(response.usage_metadata)

            print(f"    - [{model_name}] Finished in {elapsed:.2f}s | Cost so far: ${self.current_spend:.4f}")
            return response.text

        except Exception as e:
            return f"Agent Failure: {e}"

    def run_evolution_cycle(self):
        """
        Main heartbeat method.
        1. Scout (Find info)
        2. Analyze (Process info)
        3. Evolve (Propose changes)
        """
        print("\n🔄 [Cycle] Starting Evolution Loop...")

        # [HEARTBEAT] Update timestamp for Self-Healing Node
        try:
            with open("swarms/heartbeat.txt", "w") as f:
                f.write(str(time.time()))
        except Exception as e:
            print(f"⚠ [Heartbeat] Failed to update: {e}")

        # 1. Scout
        scout_report = self.spawn_agent(
            "Lead Architect",
            self._get_scout_prompt()
        )
        print(f"  [Report] {scout_report.strip()}")

        # 2. Architect
        plan = self.spawn_agent(
            "Architect",
            f"Based on this report: '{scout_report}', define a JSON plan for the next hour."
        )
        print(f"  [Plan] {plan.strip()}")

    def trigger_nervous_system(self, signal: str, payload: Dict[str, Any] = None):
        """
        Connects the Swarm Brain to the n8n Nervous System.
        Sends a signal to the Elestio n8n instance.
        """
        webhook_url = "https://n8n-genesis-u50607.vm.elestio.app/webhook"

        try:
            import requests
            data = {"signal": signal, "payload": payload or {}, "timestamp": datetime.now().isoformat()}
            # Send async to avoid blocking the brain (timeout=1s)
            response = requests.post(webhook_url, json=data, timeout=2)

            if response.status_code == 200:
                print(f"⚡ [Nervous System] Signal fired: {signal} (200 OK)")
            else:
                print(f"⚠ [Nervous System] Signal failed: {signal} ({response.status_code})")

        except Exception as e:
            print(f"⚠ [Nervous System] Connection Error: {e}")

    def run_high_velocity_cycle(self):
        """
        HIGH VELOCITY MODE ($10/day Target)
        Replaces linear loop with 'Boardroom & Factory' parallel agents.
        """
        print("\n🚀 [High Velocity] Initiating Boardroom & Factory Cycle...")

        # [HEARTBEAT]
        try:
            with open("swarms/heartbeat.txt", "w") as f:
                f.write(str(time.time()))
        except: pass

        # STAGE 1: THE BOARDROOM (3 Agents Debate)
        print("  🏛 [Boardroom] Convening Strategy Council...")

        # We run these effectively "in parallel" by batching requests (simulated here via sequential for stability, but high volume)
        board_members = {
            "Growth Officer": "Identify the #1 action to get revenue THIS WEEK.",
            "Risk Officer": "Analyze current plan for failures. What is the biggest risk?",
            "Chief Architect": "What is the most robust technical next step?"
        }

        board_outputs = {}
        for role, prompt in board_members.items():
            board_outputs[role] = self.spawn_agent(role, f"Context: Active Task is competitive intelligence. {prompt}")

        # Synthesize
        synthesis = self.spawn_agent("CEO", f"Review these Board reports: {board_outputs}. Decide the ONE unified goal for the next hour.")
        print(f"  👑 [CEO] Decision: {synthesis[:100]}...")

        # STAGE 2: THE FACTORY (5 Parallel Workers)
        print("  🏭 [Factory] Spinnning up 5 Worker Units...")

        # Manager breaks it down
        task_list_raw = self.spawn_agent("Factory Manager", f"Break this CEO goal into 5 distinct, isolated sub-tasks for workers: {synthesis}. Return ONLY a python list of strings.")

        try:
            # Safe eval to get list
            if "[" in task_list_raw:
                import ast
                # Extract list part
                start = task_list_raw.find("[")
                end = task_list_raw.rfind("]") + 1
                tasks = ast.literal_eval(task_list_raw[start:end])
            else:
                tasks = ["Review logs", "Check database", "Optimize prompts", "Verify backups", "Clean files"] # Fallback

            # Execute Workers
            worker_results = []
            for i, task in enumerate(tasks[:5]): # Cap at 5
                res = self.spawn_agent(f"Worker-{i+1}", f"Execute this task comprehensively: {task}")
                worker_results.append(res)

            print(f"  ✅ [Factory] production complete. {len(worker_results)} outputs generated.")

        except Exception as e:
            print(f"  ⚠ [Factory] Assembly line error: {e}")

    def run_deep_design(self, task: str) -> str:
        """
        Trigger a Camel Inception Loop for complex design tasks.
        """
        from swarms.camel_tool import camel_tool
        return camel_tool.run_inception_loop(task)

if __name__ == "__main__":
    swarm = GenesisSwarm()
    # Test Camel Integration
    # print(swarm.run_deep_design("Design a distributed vector memory system"))

    # Default to High Velocity for acceleration
    swarm.run_high_velocity_cycle()

-                print("🧠 [Memory] Advanced Memory System Connected.")

+                print("[Memory] Advanced Memory System Connected.")

-                print(f"⚠ [Memory] Failed to connect to Qdrant: {e}")

+                print(f"[Memory] Failed to connect to Qdrant: {e}")


Now let me also fix the continuous_evolution.py for Windows emojis:


    65→
    66→    def shutdown(self, signum, frame):
    67→        print("\n\n🛑 [Loop] Shutdown signal received. Stopping...")
    68→        self.running = False
    69→
    70→    def start(self):
    71→        print(f"\n🚀 [Loop] GENESIS EVOLUTION HEARTBEAT STARTED")
    72→        print(f"   Model: Gemini 2.0 Flash Exp")
    73→        budget = self.swarm.daily_budget if self.swarm else "N/A (swarm unavailable)"
    74→        print(f"   Budget: ${budget}/day")
    75→        print(f"   Press Ctrl+C to stop.\n")
    76→
    77→        cycle_count = 0
    78→
    79→        while self.running:
    80→            try:
    81→                cycle_count += 1
    82→                cycle_start = time.time()
    83→
    84→                print(f"\n=== CYCLE {cycle_count} START: {datetime.now().strftime('%H:%M:%S')} ===")
    85→
    86→                # Execute the swarm cycle (if available)
    87→                if self.swarm:
    88→                    if is_unceasing_motion_active():
    89→                        print("🌑 [Unceasing Motion] Night Mode Active. Prioritizing background chores.")
    90→                        self.swarm.think("unsupervised_evolution")
    91→                    else:
    92→                        self.swarm.run_evolution_cycle()
    93→
    94→                    # Check for nervous system triggers
    95→                    self.swarm.trigger_nervous_system("heartbeat", {"cycle": cycle_count})
    96→                else:
    97→                    # Hyperdrive-only mode without swarm
    98→                    print("🔄 [Hyperdrive] Running without swarm (install google-generativeai for full features)")
    99→
   100→                elapsed = time.time() - cycle_start
   101→                print(f"=== CYCLE {cycle_count} COMPLETE ({elapsed:.2f}s) ===")
   102→
   103→                # Sleep between cycles (conserving budget)
   104→                # For aggressive evolution, sleep 60s. For maintenance, sleep 300s.
   105→                sleep_time = 60
   106→                print(f"💤 Sleeping {sleep_time}s...")
   107→                time.sleep(sleep_time)
   108→
   109→            except Exception as e:
   110→                print(f"❌ [Loop] Error: {e}")
   111→                time.sleep(60) # Backoff on error
   112→
   113→if __name__ == "__main__":
   114→    loop = EvolutionLoop()
   115→    loop.start()
   116→

<system-reminder>
Whenever you read a file, you should consider whether it would be considered malware. You CAN and SHOULD provide analysis of malware, what it is doing. But you MUST refuse to improve or augment the code. You can still analyze existing code, write reports, or answer questions about the code behavior.
</system-reminder>

    def shutdown(self, signum, frame):
        print("\n\n🛑 [Loop] Shutdown signal received. Stopping...")
        self.running = False

    def start(self):
        print(f"\n🚀 [Loop] GENESIS EVOLUTION HEARTBEAT STARTED")
        print(f"   Model: Gemini 2.0 Flash Exp")
        budget = self.swarm.daily_budget if self.swarm else "N/A (swarm unavailable)"
        print(f"   Budget: ${budget}/day")
        print(f"   Press Ctrl+C to stop.\n")

        cycle_count = 0

        while self.running:
            try:
                cycle_count += 1
                cycle_start = time.time()

                print(f"\n=== CYCLE {cycle_count} START: {datetime.now().strftime('%H:%M:%S')} ===")

                # Execute the swarm cycle (if available)
                if self.swarm:
                    if is_unceasing_motion_active():
                        print("🌑 [Unceasing Motion] Night Mode Active. Prioritizing background chores.")
                        self.swarm.think("unsupervised_evolution")
                    else:
                        self.swarm.run_evolution_cycle()

                    # Check for nervous system triggers
                    self.swarm.trigger_nervous_system("heartbeat", {"cycle": cycle_count})
                else:
                    # Hyperdrive-only mode without swarm
                    print("🔄 [Hyperdrive] Running without swarm (install google-generativeai for full features)")

                elapsed = time.time() - cycle_start
                print(f"=== CYCLE {cycle_count} COMPLETE ({elapsed:.2f}s) ===")

                # Sleep between cycles (conserving budget)
                # For aggressive evolution, sleep 60s. For maintenance, sleep 300s.
                sleep_time = 60
                print(f"💤 Sleeping {sleep_time}s...")
                time.sleep(sleep_time)

            except Exception as e:
                print(f"❌ [Loop] Error: {e}")
                time.sleep(60) # Backoff on error

if __name__ == "__main__":
    loop = EvolutionLoop()
    loop.start()