#!/usr/bin/env python3
"""
Genesis 200-Agent Swarm Launcher
================================
Fires 200 agents (100 Kimi K2.5 + 100 MiniMax M2.5) via OpenRouter API.
Each agent gets a clear, atomic task. Results logged to swarm-output/.

USAGE:
    PYTHONUNBUFFERED=1 python3 scripts/swarm_200_agent_launcher.py

Cost estimate: ~$2-4 total for 200 agents.
Protocol: 200-AGENT MINIMUM PROTOCOL (Kinan Directive, 2026-02-16)

Author: Genesis System
Date: 2026-02-16
"""

import os
import sys
import json
import asyncio
import time
from datetime import datetime, timezone
from typing import List, Dict, Any, Optional
from dataclasses import dataclass, asdict, field
from pathlib import Path

try:
    import aiohttp
except ImportError:
    print("ERROR: aiohttp not installed. Run: pip install aiohttp")
    sys.exit(1)

# ── Configuration ──────────────────────────────────────────────────────────────

OPENROUTER_BASE_URL = "https://openrouter.ai/api/v1/chat/completions"
OPENROUTER_API_KEY = "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"

# Model IDs on OpenRouter
KIMI_MODEL = "moonshotai/kimi-k2.5"
MINIMAX_MODEL = "minimax/minimax-m2.5"

# Batch settings
KIMI_BATCH_SIZE = 10       # Kimi needs smaller batches (rate limits)
MINIMAX_BATCH_SIZE = 25    # MiniMax can handle bigger batches
KIMI_BATCH_DELAY = 8       # seconds between Kimi batches
MINIMAX_BATCH_DELAY = 3    # seconds between MiniMax batches
REQUEST_TIMEOUT = 300       # 5 min per request
BATCH_TIMEOUT = 600         # 10 min per batch

# Output paths (all on E: drive)
OUTPUT_DIR = "/mnt/e/genesis-system/swarm-output/session17"
KIMI_OUTPUT_DIR = f"{OUTPUT_DIR}/kimi"
MINIMAX_OUTPUT_DIR = f"{OUTPUT_DIR}/minimax"
MANIFEST_FILE = "/mnt/e/genesis-system/hive/agents_active.json"
RESULTS_JSONL = f"{OUTPUT_DIR}/results.jsonl"
REPORT_FILE = f"{OUTPUT_DIR}/SWARM_REPORT.md"


# ── Task Definitions ───────────────────────────────────────────────────────────

def generate_kimi_tasks() -> List[Dict[str, Any]]:
    """Generate 100 Kimi K2.5 tasks (code, APIs, tests, configs)."""
    tasks = []
    agent_id = 1

    # ── BATCH 1: Remaining SWARM_MISSIONS stories (7 tasks) ──
    remaining_missions = [
        ("S-K14", "Sunaiva MCP Server Health Check Endpoint",
         "Write a FastAPI health check endpoint at GET /api/health that returns JSON with: server status, database connectivity (PostgreSQL ping), Redis connectivity, Qdrant connectivity, MCP server status, memory usage, uptime. Include a /api/health/deep endpoint that runs actual test queries against each backend. File: Sunaiva/ai-memory/server/health.py"),
        ("S-K15", "Sunaiva Docker Compose Production Config",
         "Create a production-ready docker-compose.yml for Sunaiva AI Memory that includes: FastAPI app (with gunicorn+uvicorn workers), nginx reverse proxy with SSL termination, Redis for caching, volume mounts for persistent data. Include .env.example with all required env vars documented. Include health checks for all services. File: Sunaiva/ai-memory/docker-compose.prod.yml"),
        ("R-K03", "ReceptionistAI Call Routing Engine",
         "Build a Python call routing engine for ReceptionistAI that handles: inbound call classification (sales inquiry, support, booking, emergency), routing rules based on business hours and staff availability, overflow to voicemail with transcription, call queue management with position announcements. Use Telnyx TeXML for call flow. File: RECEPTIONISTAI/api/call_router.py"),
        ("R-K04", "ReceptionistAI Appointment Booking Integration",
         "Build an appointment booking module that integrates with Google Calendar API. Features: check available slots, create bookings with customer details, send confirmation (return data for email), handle timezone conversion (Australian timezones AEST/ACST/AWST), prevent double-booking with optimistic locking. File: RECEPTIONISTAI/api/appointments.py"),
        ("R-K05", "ReceptionistAI Business Hours Configuration",
         "Create a business hours configuration system. Features: per-business custom hours (open/close per day), public holiday calendar (Australian states), after-hours message customization, timezone-aware current status check, grace period for calls arriving near closing. Store in PostgreSQL. File: RECEPTIONISTAI/api/business_hours.py"),
        ("R-K10", "ReceptionistAI Lead Scoring Algorithm",
         "Build a lead scoring algorithm for ReceptionistAI. Score leads 0-100 based on: call duration (longer = more interested), questions asked (buying signals), urgency indicators, service type requested (high-value services score higher), time of call (business hours = higher intent), repeat caller bonus. Return score + reasoning. File: RECEPTIONISTAI/api/lead_scoring.py"),
        ("V-K02", "Voice Bridge WebSocket Handler",
         "Build a WebSocket handler for real-time voice streaming between browser widget and Telnyx. Handle: WebRTC SDP offer/answer exchange, ICE candidate relay, audio stream bridging, connection state management (connecting/connected/disconnected/failed), heartbeat/keepalive, graceful disconnect with call summary. Use Python websockets library. File: Sunaiva/talking-widget/backend/ws_handler.py"),
    ]
    for story_id, title, prompt in remaining_missions:
        tasks.append({
            "agent_id": f"kimi-{agent_id:03d}",
            "story_id": story_id,
            "title": title,
            "prompt": prompt,
            "system_prompt": "You are an expert Python backend engineer. Write production-ready, type-hinted Python code with comprehensive error handling, logging, and docstrings. Use async/await where appropriate. Follow PEP 8. Include inline comments for complex logic. All database access must use PostgreSQL (never SQLite).",
            "max_tokens": 8192
        })
        agent_id += 1

    # ── BATCH 2: Talking Widget Backend (20 tasks) ──
    tw_backend = [
        ("TW-K01", "Widget Embed Script Generator",
         "Build a Python module that generates customized widget embed scripts. Input: widget_id, business_name, primary_color, position (bottom-right/bottom-left), greeting_message, phone_number. Output: minified JavaScript snippet (<script> tag) that loads the widget. Include CSP-safe inline styles. File: Sunaiva/talking-widget/backend/embed_generator.py"),
        ("TW-K02", "Widget Configuration API",
         "Build FastAPI CRUD endpoints for widget configuration: POST /api/widgets (create), GET /api/widgets/{id} (read), PUT /api/widgets/{id} (update), DELETE /api/widgets/{id} (delete), GET /api/widgets (list all for user). Each widget has: name, greeting, voice_id, phone_number, industry_template, colors, position, business_hours. Auth required. File: Sunaiva/talking-widget/backend/widget_api.py"),
        ("TW-K03", "Telnyx WebRTC Token Generator",
         "Build an endpoint that generates Telnyx WebRTC credentials for browser-based calling. POST /api/widgets/{id}/token returns a short-lived SIP credential. Validate widget ownership, check rate limits (max 10 concurrent calls per widget), log token generation. Include token refresh logic. File: Sunaiva/talking-widget/backend/webrtc_tokens.py"),
        ("TW-K04", "Call Analytics Aggregation Engine",
         "Build an analytics aggregation module. Features: aggregate call data by hour/day/week/month, calculate metrics (total calls, avg duration, peak hours, conversion rate, cost per call), generate time-series data for dashboard charts. Use PostgreSQL window functions for efficiency. File: Sunaiva/talking-widget/backend/analytics_engine.py"),
        ("TW-K05", "Lead Capture Webhook Dispatcher",
         "Build a webhook dispatcher that sends lead data to external CRMs when a call completes. Support: HTTP POST with JSON payload, retry with exponential backoff (3 attempts), HMAC signature for payload verification, configurable per-widget webhook URLs, dead letter queue for failed deliveries. File: Sunaiva/talking-widget/backend/webhook_dispatcher.py"),
        ("TW-K06", "Usage Metering and Billing Calculator",
         "Build a metering system that tracks per-widget call minutes and calculates billing. Features: real-time minute tracking, tier-based pricing (Starter 100min, Pro 500min, Growth 2000min), overage calculation at $0.12/min, monthly usage reset, billing period management. File: Sunaiva/talking-widget/backend/billing_calculator.py"),
        ("TW-K07", "Widget Industry Templates",
         "Create a template system with 10 pre-built industry configurations for the talking widget. Industries: plumbing, electrical, HVAC, dental, real-estate, legal, automotive, restaurant, medical-clinic, accounting. Each template includes: greeting script, common FAQs, business hours defaults, voice personality suggestion, lead qualification questions. Return as JSON. File: Sunaiva/talking-widget/backend/industry_templates.py"),
        ("TW-K08", "Rate Limiter with Redis Backend",
         "Build a rate limiting module using Redis. Features: per-IP rate limiting (requests/minute), per-widget concurrent call limiting, per-user API call limiting, sliding window algorithm, configurable limits per tier. Include FastAPI middleware integration. File: Sunaiva/talking-widget/backend/rate_limiter_redis.py"),
        ("TW-K09", "Call Recording Storage Manager",
         "Build a module to manage call recordings. Features: upload recording to S3-compatible storage (use MinIO/Elestio), generate signed URLs for playback (24h expiry), retention policy enforcement (delete after 90 days), storage usage tracking per widget, metadata indexing (caller, duration, transcript status). File: Sunaiva/talking-widget/backend/recording_manager.py"),
        ("TW-K10", "Widget Deployment Health Monitor",
         "Build a monitoring module that checks widget deployment health. Features: ping each deployed widget endpoint, verify CDN asset availability, check Telnyx number assignment status, monitor WebRTC connectivity, generate health dashboard data. Run as async background task. File: Sunaiva/talking-widget/backend/health_monitor.py"),
        ("TW-K11", "Agency Partner Commission Calculator",
         "Build a commission tracking module for agency partners. Features: calculate 30% recurring commission on referred widget subscriptions, track commission per referral, generate monthly payout reports, handle subscription upgrades/downgrades, support custom commission rates per partner. File: Sunaiva/talking-widget/backend/commission_calculator.py"),
        ("TW-K12", "Widget A/B Testing Framework",
         "Build an A/B testing framework for widget greetings and configurations. Features: create test variants (greeting text, voice, colors), random traffic splitting with configurable percentages, conversion tracking (calls started, calls completed, leads captured), statistical significance calculation, winner auto-promotion. File: Sunaiva/talking-widget/backend/ab_testing.py"),
        ("TW-K13", "Automated Onboarding Pipeline",
         "Build the automated onboarding flow: user signs up -> Stripe checkout -> provision widget -> assign Telnyx number -> configure voice assistant -> generate embed code -> send welcome email data. Each step is a function that can be retried independently. Use a state machine pattern. File: Sunaiva/talking-widget/backend/onboarding_pipeline.py"),
        ("TW-K14", "Multi-Widget Dashboard Data Aggregator",
         "Build a data aggregation layer for businesses with multiple widgets. Features: aggregate analytics across all widgets, cross-widget lead deduplication, unified billing view, comparative performance metrics between widgets, bulk configuration updates. File: Sunaiva/talking-widget/backend/multi_widget_aggregator.py"),
        ("TW-K15", "Call Transcript Search Engine",
         "Build a search engine for call transcripts. Features: full-text search using PostgreSQL tsvector, filter by date range/widget/caller, highlight matching terms, sort by relevance/date, export search results as CSV. Include API endpoints: GET /api/transcripts/search?q=term. File: Sunaiva/talking-widget/backend/transcript_search.py"),
        ("TW-K16", "Automated Daily/Weekly Report Generator",
         "Build a report generator that creates daily and weekly performance summaries. Include: total calls, avg duration, top callers, busiest hours, lead conversion rate, cost summary, comparison to previous period. Output as JSON (for dashboard) and HTML (for email). File: Sunaiva/talking-widget/backend/report_generator.py"),
        ("TW-K17", "Widget Embed Verification Service",
         "Build a service that verifies widget installations on customer websites. Features: fetch the customer URL, check if embed script is present, verify script version is current, check for CSP conflicts, report installation status. Expose as POST /api/widgets/{id}/verify-install. File: Sunaiva/talking-widget/backend/install_verifier.py"),
        ("TW-K18", "CRM Integration - HubSpot Connector",
         "Build a HubSpot CRM connector. Features: create/update contacts from call data, log calls as activities, create deals from qualified leads, sync custom properties (call duration, transcript summary, lead score), handle OAuth2 authentication flow, webhook for real-time sync. File: Sunaiva/talking-widget/backend/crm_hubspot.py"),
        ("TW-K19", "CRM Integration - GoHighLevel Connector",
         "Build a GoHighLevel CRM connector. Features: create/update contacts, log calls as conversations, trigger GHL workflows on lead capture, sync tags based on call outcomes, handle API key authentication, support custom fields mapping. File: Sunaiva/talking-widget/backend/crm_ghl.py"),
        ("TW-K20", "Google Calendar Integration",
         "Build a Google Calendar integration for appointment booking from voice calls. Features: OAuth2 flow for calendar access, check available time slots, create events with customer details, send calendar invites, handle timezone conversion (Australian timezones), prevent double-booking. File: Sunaiva/talking-widget/backend/calendar_google.py"),
    ]
    for story_id, title, prompt in tw_backend:
        tasks.append({
            "agent_id": f"kimi-{agent_id:03d}",
            "story_id": story_id,
            "title": title,
            "prompt": prompt,
            "system_prompt": "You are an expert Python backend engineer building a SaaS voice widget platform. Write production-ready, type-hinted Python code using FastAPI, asyncio, and PostgreSQL. Include comprehensive error handling, logging, and docstrings. Never use SQLite. All code must be async-first.",
            "max_tokens": 8192
        })
        agent_id += 1

    # ── BATCH 3: Testing & Quality (20 tasks) ──
    testing_tasks = [
        ("TEST-K01", "Widget API Integration Tests",
         "Write a comprehensive pytest test suite for the Talking Widget API. Cover: widget CRUD operations, authentication flows, rate limiting, error responses (400/401/403/404/422). Use httpx.AsyncClient for async testing. Include fixtures for test user, test widget, and mock Telnyx responses. File: tests/test_widget_api.py"),
        ("TEST-K02", "Billing Integration Tests",
         "Write pytest tests for the billing system. Cover: checkout session creation for all tiers, webhook processing (subscription created/updated/deleted/payment_failed), overage calculation, commission tracking, tier upgrade/downgrade. Mock Stripe API calls. File: tests/test_billing.py"),
        ("TEST-K03", "Analytics Engine Tests",
         "Write pytest tests for the analytics aggregation engine. Cover: hourly/daily/weekly/monthly aggregation, time-series data generation, metric calculation accuracy (avg duration, conversion rate), empty data handling, timezone edge cases. File: tests/test_analytics.py"),
        ("TEST-K04", "Lead Scoring Algorithm Tests",
         "Write pytest tests for the lead scoring algorithm. Cover: scoring accuracy for different call profiles, edge cases (very short calls, after-hours calls, repeat callers), score distribution analysis, scoring consistency (same input = same output). File: tests/test_lead_scoring.py"),
        ("TEST-K05", "Webhook Dispatcher Tests",
         "Write pytest tests for the webhook dispatcher. Cover: successful delivery, retry on failure, exponential backoff timing, HMAC signature generation, dead letter queue, concurrent dispatch handling. Mock external HTTP endpoints. File: tests/test_webhook_dispatcher.py"),
        ("TEST-K06", "Rate Limiter Tests",
         "Write pytest tests for the Redis-backed rate limiter. Cover: request counting accuracy, sliding window behavior, per-IP limiting, per-widget concurrent limits, limit reset after window, tier-based limit configuration. Use fakeredis for mocking. File: tests/test_rate_limiter.py"),
        ("TEST-K07", "Authentication Module Tests",
         "Write pytest tests for the auth module. Cover: user registration, login with correct/wrong password, JWT token validation, token expiry, refresh token flow, rate limiting on login attempts, password hashing verification. File: tests/test_auth.py"),
        ("TEST-K08", "Onboarding Pipeline Tests",
         "Write pytest tests for the automated onboarding pipeline. Cover: happy path (signup through embed code generation), each step failure and retry, state machine transitions, partial completion recovery, idempotency. File: tests/test_onboarding.py"),
        ("TEST-K09", "Industry Template Validation Tests",
         "Write pytest tests validating all 10 industry templates. Cover: schema compliance (all required fields present), greeting text quality (non-empty, professional), business hours validity, FAQ completeness, no duplicate templates. File: tests/test_industry_templates.py"),
        ("TEST-K10", "Database Migration Tests",
         "Write pytest tests for database migrations. Cover: forward migration succeeds on clean DB, migration is idempotent (can run twice), RLS policies work correctly, indexes exist and are used, foreign key constraints enforced, rollback migration works. File: tests/test_migrations.py"),
        ("TEST-K11", "Call Recording Manager Tests",
         "Write pytest tests for the recording storage manager. Cover: upload to S3, signed URL generation, URL expiry, retention policy enforcement, storage tracking accuracy, metadata indexing. Use moto for S3 mocking. File: tests/test_recording_manager.py"),
        ("TEST-K12", "A/B Testing Framework Tests",
         "Write pytest tests for the A/B testing framework. Cover: variant creation, traffic splitting accuracy (within 5% of configured ratio), conversion tracking, statistical significance calculation, winner auto-promotion logic. File: tests/test_ab_testing.py"),
        ("TEST-K13", "Transcript Search Tests",
         "Write pytest tests for the transcript search engine. Cover: full-text search accuracy, date range filtering, relevance sorting, search highlighting, CSV export format, empty results handling, special character handling in queries. File: tests/test_transcript_search.py"),
        ("TEST-K14", "CRM Connector Tests - HubSpot",
         "Write pytest tests for the HubSpot CRM connector. Cover: contact creation/update, call activity logging, deal creation from leads, OAuth2 token refresh, webhook sync, error handling for API rate limits. Mock HubSpot API. File: tests/test_crm_hubspot.py"),
        ("TEST-K15", "CRM Connector Tests - GoHighLevel",
         "Write pytest tests for the GoHighLevel CRM connector. Cover: contact creation/update, conversation logging, workflow triggers, tag syncing, custom field mapping, API authentication, error handling. Mock GHL API. File: tests/test_crm_ghl.py"),
        ("TEST-K16", "Google Calendar Integration Tests",
         "Write pytest tests for the Google Calendar integration. Cover: OAuth2 flow, available slot checking, event creation, timezone handling (AEST/ACST/AWST), double-booking prevention, calendar invite sending. Mock Google Calendar API. File: tests/test_calendar_google.py"),
        ("TEST-K17", "End-to-End Widget Lifecycle Test",
         "Write a comprehensive E2E test that exercises the full widget lifecycle: create account -> subscribe -> create widget -> configure -> get embed code -> simulate call -> capture lead -> view analytics -> export data -> cancel subscription. File: tests/test_e2e_widget_lifecycle.py"),
        ("TEST-K18", "Security Audit Test Suite",
         "Write a security-focused test suite. Cover: SQL injection prevention, XSS in widget configuration, CSRF protection, auth bypass attempts, rate limit bypass, IDOR (accessing another user's widgets), sensitive data exposure in API responses. File: tests/test_security.py"),
        ("TEST-K19", "Performance Benchmark Tests",
         "Write performance benchmark tests. Cover: API response time under load (target <200ms p95), concurrent call handling (target 50 concurrent), database query performance (no N+1), webhook dispatch throughput, analytics aggregation speed for large datasets. File: tests/test_performance.py"),
        ("TEST-K20", "Widget Installation Verifier Tests",
         "Write pytest tests for the widget embed verification service. Cover: correct detection of embed script, missing script detection, outdated version detection, CSP conflict identification, handling of various HTML structures, timeout handling for slow sites. File: tests/test_install_verifier.py"),
    ]
    for story_id, title, prompt in testing_tasks:
        tasks.append({
            "agent_id": f"kimi-{agent_id:03d}",
            "story_id": story_id,
            "title": title,
            "prompt": prompt,
            "system_prompt": "You are an expert Python test engineer. Write comprehensive pytest test suites with clear test names, docstrings, proper fixtures, parametrize decorators for edge cases, and mock/patch for external dependencies. Use pytest-asyncio for async tests. Achieve high coverage with both positive and negative test cases.",
            "max_tokens": 8192
        })
        agent_id += 1

    # ── BATCH 4: DevOps & Infrastructure (15 tasks) ──
    devops_tasks = [
        ("INFRA-K01", "Docker Compose Full Stack",
         "Create a production-ready docker-compose.yml for the complete Talking Widget stack. Services: fastapi-app (gunicorn+uvicorn, 4 workers), nginx (reverse proxy, SSL, gzip), redis (caching+rate-limiting), postgres (if not using external Elestio). Include health checks, restart policies, resource limits, logging configuration. File: Sunaiva/talking-widget/docker-compose.yml"),
        ("INFRA-K02", "Dockerfile Multi-Stage Build",
         "Create an optimized multi-stage Dockerfile for the Talking Widget backend. Stage 1: build dependencies. Stage 2: production image (slim Python, non-root user, minimal layers). Include: pip install from requirements.txt, copy only necessary files, health check, proper signal handling for graceful shutdown. File: Sunaiva/talking-widget/Dockerfile"),
        ("INFRA-K03", "Nginx Configuration with SSL",
         "Create an nginx configuration for the Talking Widget. Features: SSL termination (Let's Encrypt), HTTP/2, gzip compression, security headers (HSTS, CSP, X-Frame-Options), rate limiting at nginx level, WebSocket proxy pass for /ws, static file serving for widget assets, access/error logging. File: Sunaiva/talking-widget/deploy/nginx.conf"),
        ("INFRA-K04", "GitHub Actions CI/CD Pipeline",
         "Create a GitHub Actions workflow for CI/CD. Jobs: lint (ruff+mypy), test (pytest with PostgreSQL service container), build (Docker image), deploy (SSH to Elestio VPS). Include: caching for pip dependencies, parallel test execution, deployment only on main branch, Slack notification on failure. File: .github/workflows/talking-widget-ci.yml"),
        ("INFRA-K05", "Environment Configuration Manager",
         "Create a configuration management module using pydantic-settings. Define all environment variables with types, defaults, and validation. Groups: database (PG connection), redis, stripe (keys), telnyx (API key, app ID), email (Resend key), storage (S3 endpoint), feature flags. Include .env.example. File: Sunaiva/talking-widget/backend/config.py"),
        ("INFRA-K06", "Database Migration System",
         "Create a database migration system using alembic. Include: initial migration with all tables (users, widgets, subscriptions, calls, leads, analytics, webhooks), seed data for industry templates, migration for indexes and RLS policies. Configure for Elestio PostgreSQL. File: Sunaiva/talking-widget/backend/migrations/"),
        ("INFRA-K07", "Deployment Script for Elestio",
         "Create a deployment script that deploys the Talking Widget to Elestio VPS. Features: SSH connection, Docker image build and push, docker-compose up with zero-downtime (rolling update), database migration run, health check verification, rollback on failure. File: Sunaiva/talking-widget/deploy/deploy.sh"),
        ("INFRA-K08", "Monitoring and Alerting Setup",
         "Create a monitoring configuration. Include: Prometheus metrics endpoint in FastAPI (request count, latency histogram, active calls gauge), health check endpoints, structured JSON logging, alert rules (high error rate, slow response, disk space). File: Sunaiva/talking-widget/backend/monitoring.py"),
        ("INFRA-K09", "Redis Configuration and Caching Layer",
         "Create a Redis caching layer module. Features: cache widget configurations (5min TTL), cache user sessions, rate limit counters, call state tracking, pub/sub for real-time updates. Include connection pooling, retry logic, graceful fallback when Redis is unavailable. File: Sunaiva/talking-widget/backend/cache.py"),
        ("INFRA-K10", "CDN Asset Deployment Script",
         "Create a script that deploys widget.js and associated assets to a CDN. Features: upload to S3/CloudFront or Cloudflare R2, set correct CORS headers, cache headers (1 year for versioned, no-cache for latest), generate integrity hashes (SRI), update DNS CNAME. File: Sunaiva/talking-widget/deploy/cdn_deploy.py"),
        ("INFRA-K11", "Backup and Disaster Recovery",
         "Create backup scripts for the Talking Widget. Features: PostgreSQL pg_dump to S3 (daily), Redis RDB snapshot to S3 (hourly), call recordings backup verification, restore testing script, backup rotation (keep 30 daily, 12 weekly, 12 monthly). File: Sunaiva/talking-widget/deploy/backup.sh"),
        ("INFRA-K12", "Load Testing Script",
         "Create a load testing script using locust. Scenarios: concurrent API calls (100 users), widget embed loading (1000 req/s), WebSocket connections (50 concurrent), webhook processing (burst of 100), mixed workload. Include: ramp-up pattern, response time thresholds, failure rate thresholds. File: Sunaiva/talking-widget/tests/locustfile.py"),
        ("INFRA-K13", "Log Aggregation Pipeline",
         "Create a structured logging configuration. Features: JSON-formatted logs, correlation IDs across requests, log levels per module, sensitive data redaction (passwords, tokens, card numbers), log rotation, export to stdout for Docker. File: Sunaiva/talking-widget/backend/logging_config.py"),
        ("INFRA-K14", "SSL Certificate Auto-Renewal",
         "Create a Let's Encrypt certificate management script. Features: initial certificate issuance via certbot, automated renewal cron job, nginx reload after renewal, certificate expiry monitoring, multi-domain support (api.sunaivadigital.com, cdn.sunaivadigital.com, widget.sunaivadigital.com). File: Sunaiva/talking-widget/deploy/ssl_manager.sh"),
        ("INFRA-K15", "Security Hardening Script",
         "Create a security hardening script for the production server. Features: UFW firewall rules (only 80, 443, 22), fail2ban configuration, SSH key-only auth enforcement, Docker security best practices (non-root, read-only fs, no-new-privileges), rate limiting at OS level. File: Sunaiva/talking-widget/deploy/harden.sh"),
    ]
    for story_id, title, prompt in devops_tasks:
        tasks.append({
            "agent_id": f"kimi-{agent_id:03d}",
            "story_id": story_id,
            "title": title,
            "prompt": prompt,
            "system_prompt": "You are an expert DevOps engineer specializing in Python web application deployment. Write production-ready infrastructure code, Docker configurations, CI/CD pipelines, and deployment scripts. Follow security best practices. All work must target Linux (Ubuntu/Debian) servers. Never use SQLite. All persistent storage uses PostgreSQL on Elestio.",
            "max_tokens": 8192
        })
        agent_id += 1

    # ── BATCH 5: Frontend Components (15 tasks) ──
    frontend_tasks = [
        ("FE-K01", "Widget.js Core - Shadow DOM Implementation",
         "Write the core widget.js that creates a Shadow DOM-encapsulated voice widget. Features: floating button (bottom-right, customizable), expand to call panel on click, visual states (idle/connecting/active/ended), call duration timer, mute/unmute button, end call button. Use vanilla JS, no frameworks. Make it embeddable on any site without CSS conflicts. Target: <30KB minified. Output the complete JavaScript file."),
        ("FE-K02", "Dashboard - Analytics Charts Component",
         "Write a vanilla JavaScript dashboard component for analytics charts. Features: calls-per-day line chart (last 30 days), average duration bar chart (last 7 days), peak hours heatmap, call outcome pie chart (answered/missed/voicemail). Use Chart.js for rendering. Fetch data from /api/analytics endpoint. File: Sunaiva/talking-widget/dashboard/js/analytics.js"),
        ("FE-K03", "Dashboard - Lead Management Table",
         "Write a vanilla JavaScript component for a lead management table. Features: sortable columns (name, phone, date, score, status), filter by date range and status, bulk actions (export CSV, mark as contacted), real-time search, pagination (25 per page), click-to-expand for call details and transcript. File: Sunaiva/talking-widget/dashboard/js/leads.js"),
        ("FE-K04", "Dashboard - Widget Configuration Form",
         "Write a vanilla JavaScript form component for widget configuration. Features: greeting message editor, voice selector (dropdown with preview), color picker for widget theme, position selector (visual preview), business hours editor (day-by-day with time pickers), industry template quick-select. Live preview panel that updates as settings change. File: Sunaiva/talking-widget/dashboard/js/widget-config.js"),
        ("FE-K05", "Dashboard - Billing and Subscription Page",
         "Write a vanilla JavaScript billing dashboard page. Features: current plan display with usage meter, upgrade/downgrade buttons (redirect to Stripe), billing history table, next payment date, overage charges, payment method on file. Fetch from /api/billing endpoints. File: Sunaiva/talking-widget/dashboard/js/billing.js"),
        ("FE-K06", "Agency Partner Portal Page",
         "Write a vanilla JavaScript partner portal page. Features: referral link generator, commission dashboard (this month, total earned, pending payout), referred clients table (name, plan, status, commission amount), performance charts, payout history, partner settings. File: Sunaiva/talking-widget/dashboard/js/partner-portal.js"),
        ("FE-K07", "Widget Installation Guide Component",
         "Write an interactive installation guide component. Features: detect customer's platform (WordPress, Shopify, Wix, Squarespace, custom HTML), show platform-specific installation steps, copy-to-clipboard for embed code, verification checker (paste URL and we check if widget is installed), troubleshooting FAQ. File: Sunaiva/talking-widget/dashboard/js/install-guide.js"),
        ("FE-K08", "Real-time Call Activity Feed",
         "Write a vanilla JavaScript component for a real-time call activity feed. Features: WebSocket connection to /ws/activity, live updates when calls start/end, caller info card with name/number/duration, call status indicators (ringing/active/completed/missed), sound notification for new calls, auto-scroll with pause-on-hover. File: Sunaiva/talking-widget/dashboard/js/activity-feed.js"),
        ("FE-K09", "Admin Panel - User Management",
         "Write a vanilla JavaScript admin panel for user management. Features: user list with search/filter, user detail view (subscriptions, widgets, usage), impersonate user (view their dashboard), suspend/unsuspend accounts, manual tier override, usage statistics. File: Sunaiva/talking-widget/dashboard/js/admin-users.js"),
        ("FE-K10", "Admin Panel - System Metrics Dashboard",
         "Write a vanilla JavaScript system metrics dashboard for admin. Features: total active widgets gauge, calls in progress real-time counter, API request rate chart, error rate chart, revenue metrics (MRR, churn, LTV), Telnyx balance and usage. Fetch from /api/admin/metrics. File: Sunaiva/talking-widget/dashboard/js/admin-metrics.js"),
        ("FE-K11", "Landing Page Pricing Calculator",
         "Write a vanilla JavaScript pricing calculator for the landing page. Features: slider for expected monthly calls, auto-calculate recommended plan, show cost breakdown (base + estimated overage), ROI calculator (show value vs cost of missed calls), comparison table with competitor pricing. File: Sunaiva/talking-widget/landing/js/pricing-calc.js"),
        ("FE-K12", "Widget Demo - Interactive Preview",
         "Write an interactive widget demo for the landing page. Features: embed a sample widget that visitors can click to try (uses a demo Telnyx number), show the widget in different configurations (colors, positions), industry-specific demo selection, after demo: CTA to sign up. File: Sunaiva/talking-widget/landing/js/widget-demo.js"),
        ("FE-K13", "Dashboard - Settings Page",
         "Write a vanilla JavaScript settings page. Sections: profile (name, email, password change), notifications (email alerts for calls, weekly reports), integrations (CRM connection status, calendar sync), API keys (generate/revoke for developers), danger zone (export data, delete account). File: Sunaiva/talking-widget/dashboard/js/settings.js"),
        ("FE-K14", "Responsive Mobile Dashboard",
         "Write CSS and JavaScript to make the dashboard fully responsive for mobile. Features: hamburger menu for navigation, stacked cards for analytics (instead of side-by-side), swipeable tables, touch-friendly buttons, bottom navigation bar on mobile, optimized chart sizes. File: Sunaiva/talking-widget/dashboard/css/mobile.css and mobile.js"),
        ("FE-K15", "Onboarding Wizard Component",
         "Write a multi-step onboarding wizard in vanilla JavaScript. Steps: 1) Business info (name, industry, phone), 2) Choose plan (with pricing cards), 3) Configure widget (voice, greeting, colors), 4) Install (embed code + verification), 5) Test call (make a test call). Progress bar, back/next buttons, save progress between steps. File: Sunaiva/talking-widget/dashboard/js/onboarding-wizard.js"),
    ]
    for story_id, title, prompt in frontend_tasks:
        tasks.append({
            "agent_id": f"kimi-{agent_id:03d}",
            "story_id": story_id,
            "title": title,
            "prompt": prompt,
            "system_prompt": "You are an expert frontend engineer specializing in vanilla JavaScript, modern CSS, and responsive web design. Write clean, performant code without framework dependencies. Use ES6+ features, CSS custom properties for theming, and follow accessibility best practices (ARIA labels, keyboard navigation). Dark theme: background #0B0C0E, accent #3B82F6.",
            "max_tokens": 8192
        })
        agent_id += 1

    # ── BATCH 6: Sunaiva AI Memory additions (13 tasks) ──
    sunaiva_tasks = [
        ("SAM-K01", "Memory Vault Export API",
         "Build an API endpoint that exports a user's entire memory vault. Features: export as JSON (structured), export as Markdown (human-readable), export as CSV (spreadsheet-friendly), include all entities/decisions/preferences/relationships. Stream large exports. Rate limit: 1 export per hour. File: Sunaiva/ai-memory/server/export_api.py"),
        ("SAM-K02", "Memory Search with Semantic Similarity",
         "Build a semantic search endpoint for memory vaults. Features: embed query using OpenAI/Qdrant, search across entities+decisions+preferences, filter by conversation source, rank by similarity score, return top-k results with context snippets. File: Sunaiva/ai-memory/server/semantic_search.py"),
        ("SAM-K03", "Conversation Parser - Gemini Export Format",
         "Build a parser for Google Gemini chat exports. Handle: JSON format from Google Takeout, extract user messages and AI responses, handle multi-turn conversations, extract code blocks and images (metadata only), handle Unicode and emoji correctly. File: Sunaiva/ai-memory/server/parsers/gemini_parser.py"),
        ("SAM-K04", "Memory Entity Extraction Pipeline",
         "Build an entity extraction pipeline using LLM. Features: extract people, places, companies, products, decisions, preferences, action items from conversation text. Use structured output (JSON schema enforcement). Batch processing for efficiency. Deduplication across conversations. File: Sunaiva/ai-memory/server/extraction_pipeline.py"),
        ("SAM-K05", "MCP Server - Memory Access Tools",
         "Build a FastMCP server that exposes memory vault as MCP tools. Tools: search_memory(query), get_entity(name), list_decisions(topic), get_preferences(category), add_memory(content), get_conversation_summary(id). Resources: vault_stats, recent_memories. File: Sunaiva/ai-memory/mcp/memory_server.py"),
        ("SAM-K06", "Waitlist Management API",
         "Build a waitlist API for Sunaiva AI Memory pre-launch. Features: POST /api/waitlist (email, name, platform preference), referral tracking (unique referral codes), position in queue, email verification, duplicate prevention, export waitlist as CSV for Instantly.ai campaigns. File: Sunaiva/ai-memory/server/waitlist.py"),
        ("SAM-K07", "User Dashboard API",
         "Build dashboard API endpoints. Features: GET /api/dashboard (vault stats, recent activity, usage meter), GET /api/dashboard/entities (paginated entity list), GET /api/dashboard/decisions (decision timeline), GET /api/dashboard/activity (upload history, search history). File: Sunaiva/ai-memory/server/dashboard_api.py"),
        ("SAM-K08", "Conversation Deduplication Engine",
         "Build a deduplication engine that prevents duplicate conversation imports. Features: content hash for exact duplicates, fuzzy matching for near-duplicates (similar timestamps + content), merge strategy for overlapping conversations, conflict resolution UI data, dedup report generation. File: Sunaiva/ai-memory/server/dedup_engine.py"),
        ("SAM-K09", "Memory Vault Backup System",
         "Build an automated backup system for memory vaults. Features: scheduled daily backups to PostgreSQL (separate backup schema), point-in-time recovery, backup integrity verification, user-triggered manual backups, backup size tracking and quotas. File: Sunaiva/ai-memory/server/backup_system.py"),
        ("SAM-K10", "Custom GPT Integration Endpoint",
         "Build an endpoint that serves memory data to ChatGPT Custom GPTs. Features: OpenAI Actions schema (openapi.yaml), authentication via API key, search_memory action, get_context action (returns relevant memories for a given topic), rate limiting per API key. File: Sunaiva/ai-memory/server/custom_gpt_api.py"),
        ("SAM-K11", "Privacy and Data Deletion API",
         "Build GDPR-compliant data management endpoints. Features: GET /api/privacy/data-export (full data export), DELETE /api/privacy/delete-account (cascade delete all user data), DELETE /api/privacy/delete-vault/{id} (delete specific vault), POST /api/privacy/anonymize (anonymize but keep structure). File: Sunaiva/ai-memory/server/privacy_api.py"),
        ("SAM-K12", "Notification System",
         "Build a notification system for Sunaiva. Features: email notifications via Resend API (welcome, upload complete, weekly summary, payment), in-app notification feed, notification preferences (per-type opt-out), notification templates, batch sending for weekly digests. File: Sunaiva/ai-memory/server/notifications.py"),
        ("SAM-K13", "API Rate Limiting and Quotas",
         "Build a comprehensive rate limiting system. Features: per-user API limits (based on tier), per-endpoint limits (search: 30/min, upload: 5/min, export: 1/hour), global limits for DDoS protection, quota tracking (monthly API calls, storage bytes), graceful degradation (queue requests vs reject). File: Sunaiva/ai-memory/server/rate_limits.py"),
    ]
    for story_id, title, prompt in sunaiva_tasks:
        tasks.append({
            "agent_id": f"kimi-{agent_id:03d}",
            "story_id": story_id,
            "title": title,
            "prompt": prompt,
            "system_prompt": "You are an expert Python backend engineer building Sunaiva AI Memory, a SaaS platform that turns AI chat exports into searchable, structured memory vaults. Write production-ready, type-hinted Python code using FastAPI and PostgreSQL. Include comprehensive error handling, logging, and docstrings. Never use SQLite.",
            "max_tokens": 8192
        })
        agent_id += 1

    # ── BATCH 7: ReceptionistAI Backend (5 tasks to reach 100) ──
    receptionist_tasks = [
        ("RAI-K01", "ReceptionistAI Voicemail Transcription Pipeline",
         "Build a voicemail transcription pipeline. Features: receive voicemail audio from Telnyx webhook, transcribe using Whisper API or Telnyx STT, extract caller intent and urgency, generate summary, notify business owner via email/SMS with transcript and audio link. File: RECEPTIONISTAI/api/voicemail_pipeline.py"),
        ("RAI-K02", "ReceptionistAI Multi-Tenant Configuration",
         "Build a multi-tenant configuration system. Features: per-business settings (greeting, voice, hours, routing rules), industry-specific defaults, A/B testing support for greetings, configuration versioning (rollback support), bulk import/export for agency partners. File: RECEPTIONISTAI/api/tenant_config.py"),
        ("RAI-K03", "ReceptionistAI Call Analytics Dashboard API",
         "Build analytics API endpoints for ReceptionistAI. Features: call volume trends (hourly, daily, weekly), average handle time, first-call resolution rate, customer satisfaction scores (post-call survey), peak hour analysis, comparison across multiple business locations. File: RECEPTIONISTAI/api/analytics_api.py"),
        ("RAI-K04", "ReceptionistAI SMS Follow-up System",
         "Build an automated SMS follow-up system. Features: send follow-up SMS after missed calls, appointment confirmation via SMS, template-based messages with personalization, opt-out handling (STOP keyword), SMS delivery tracking, Australian number formatting. Use Telnyx Messaging API. File: RECEPTIONISTAI/api/sms_followup.py"),
        ("RAI-K05", "ReceptionistAI Competitor Feature Matrix",
         "Build a Python script that generates a feature comparison matrix between ReceptionistAI and competitors (Bland AI, Retell AI, Vapi, Smith.ai, Ruby Receptionists, AnswerConnect). Compare: pricing, minutes included, AI capabilities, integrations, Australian support, setup time. Output as JSON and Markdown table. File: RECEPTIONISTAI/scripts/competitor_matrix.py"),
        ("RAI-K06", "ReceptionistAI Call Transfer Handler",
         "Build a call transfer handler for ReceptionistAI. Features: warm transfer (AI introduces caller to staff), cold transfer (direct connect), transfer to voicemail, transfer to external number, transfer based on department selection, hold music configuration, transfer timeout handling. Use Telnyx TeXML. File: RECEPTIONISTAI/api/call_transfer.py"),
        ("RAI-K07", "ReceptionistAI Post-Call Processing Pipeline",
         "Build a post-call processing pipeline. After each call: generate transcript (from Telnyx recording), extract key entities (caller name, phone, intent, urgency), calculate lead score, store in PostgreSQL, trigger webhook notifications, queue follow-up SMS if configured. Use async task queue pattern. File: RECEPTIONISTAI/api/post_call_pipeline.py"),
        ("RAI-K08", "Genesis Webhook Registry",
         "Build a centralized webhook registry for Genesis. Features: register webhook endpoints with URL, events, secret, retry config. Dispatch events to matching webhooks. Track delivery status. Admin UI data endpoints. Support for filters (e.g., only calls from widget X). HMAC-SHA256 signatures. File: core/webhook_registry.py"),
        ("RAI-K09", "Genesis API Key Management",
         "Build an API key management system. Features: generate API keys with scopes (read, write, admin), rate limiting per key, key rotation (create new, deprecate old), usage tracking (requests per key per day), key expiration, audit log of key usage. Store in PostgreSQL. File: core/api_key_manager.py"),
        ("RAI-K10-B", "Genesis Event Bus",
         "Build an event bus for Genesis internal communication. Features: publish/subscribe pattern, event types (call.started, lead.captured, payment.received, widget.installed), async handlers, dead letter queue for failed handlers, event replay for debugging, PostgreSQL-backed event store. File: core/event_bus.py"),
    ]
    for story_id, title, prompt in receptionist_tasks:
        tasks.append({
            "agent_id": f"kimi-{agent_id:03d}",
            "story_id": story_id,
            "title": title,
            "prompt": prompt,
            "system_prompt": "You are an expert Python backend engineer building ReceptionistAI, an AI-powered virtual receptionist for Australian businesses. Write production-ready, type-hinted Python code with comprehensive error handling. Use FastAPI, PostgreSQL (never SQLite), and async patterns. Handle Australian phone numbers (+61 format) and timezones (AEST/ACST/AWST).",
            "max_tokens": 8192
        })
        agent_id += 1

    assert len(tasks) == 100, f"Expected 100 Kimi tasks, got {len(tasks)}"
    return tasks


def generate_minimax_tasks() -> List[Dict[str, Any]]:
    """Generate 100 MiniMax M2.5 tasks (content, marketing, docs, analysis)."""
    tasks = []
    agent_id = 1

    # ── BATCH 1: Email Marketing Sequences (20 tasks) ──
    email_tasks = [
        ("EMAIL-M01", "Cold Email - Plumbing Business Owners",
         "Write a 5-email cold outreach sequence targeting plumbing business owners in Australia. Tone: professional, relatable, ROI-focused. Mention: missed calls cost $500+ per lead, AI receptionist answers 24/7, setup in 5 minutes, $197/mo starting price (strikethrough $497). Include subject lines with <40 chars, personalization tokens {first_name} {business_name}, and clear CTAs. Format as plain text emails (no HTML)."),
        ("EMAIL-M02", "Cold Email - Electrical Contractors",
         "Write a 5-email cold outreach sequence targeting electrical contractors in Australia. Focus on: emergency call handling (after-hours electrical emergencies), booking quotes automatically, capturing customer details while on the job, reducing admin overhead. Price: $197/mo (strikethrough $497). Include subject lines and CTAs."),
        ("EMAIL-M03", "Cold Email - HVAC Companies",
         "Write a 5-email cold outreach sequence targeting HVAC (heating, ventilation, air conditioning) companies in Australia. Focus on: seasonal peak call handling, appointment booking for installations, emergency repair dispatching, reducing missed calls during busy season. Include subject lines, personalization, and CTAs."),
        ("EMAIL-M04", "Cold Email - Dental Practices",
         "Write a 5-email cold outreach sequence targeting dental practices. Focus on: appointment booking, patient intake information, insurance verification questions, after-hours emergency dental line, reducing front desk workload. Tone: professional, healthcare-appropriate."),
        ("EMAIL-M05", "Cold Email - Real Estate Agents",
         "Write a 5-email cold outreach sequence targeting real estate agents. Focus on: never missing a buyer inquiry, property viewing scheduling, instant response to listing inquiries, lead qualification (budget, timeline, preferences), 24/7 availability when showing properties."),
        ("EMAIL-M06", "Cold Email - Website Agency Partners",
         "Write a 5-email sequence targeting website agencies for the 30% commission partnership. Focus on: recurring revenue stream for their agency, white-label option, easy installation on client sites, value-add to their web packages, 30% recurring commission on every client. Tone: partnership-focused, B2B."),
        ("EMAIL-M07", "Cold Email - Legal Firms",
         "Write a 5-email cold outreach sequence targeting law firms and legal practices. Focus on: confidential call screening, client intake forms, appointment scheduling, after-hours emergency legal line, professional voice that represents their brand."),
        ("EMAIL-M08", "Cold Email - Automotive Workshops",
         "Write a 5-email cold outreach sequence targeting auto mechanics and workshops. Focus on: booking service appointments, providing quotes for common services, handling warranty inquiries, parts availability questions, reducing interruptions while working on vehicles."),
        ("EMAIL-M09", "Cold Email - Medical Clinics",
         "Write a 5-email cold outreach sequence targeting medical clinics (GP practices). Focus on: patient appointment booking, prescription refill requests, after-hours triage routing, reducing hold times, HIPAA/privacy-conscious messaging. Tone: professional, healthcare-appropriate."),
        ("EMAIL-M10", "Cold Email - Restaurant/Hospitality",
         "Write a 5-email cold outreach sequence targeting restaurants and hospitality venues. Focus on: reservation booking, event inquiry handling, takeout order questions, reducing phone interruptions during service, handling large party requests."),
        ("EMAIL-M11", "Warm Email - Waitlist Welcome Sequence",
         "Write a 5-email welcome sequence for new waitlist signups (Sunaiva AI Memory). Email 1: welcome + what to expect. Email 2: early access benefits. Email 3: behind-the-scenes development update. Email 4: referral incentive (refer 3 friends, jump 15 spots). Email 5: beta launch announcement with exclusive pricing."),
        ("EMAIL-M12", "Warm Email - Trial to Paid Conversion",
         "Write a 7-email sequence for converting free trial users to paid subscribers (Talking Widget). Day 1: setup guide. Day 3: first call celebration. Day 5: analytics tour. Day 7: integration tips. Day 10: ROI case study. Day 12: trial ending soon. Day 14: last chance + special offer."),
        ("EMAIL-M13", "Cold Email - AI/Automation Influencers",
         "Write a 3-email outreach sequence targeting AI and automation influencers/creators. Offer: free Sunaiva AI Memory Pro account + exclusive content + early feature access in exchange for honest review/mention. Tone: respectful of their time, no hard sell."),
        ("EMAIL-M14", "Email - Monthly Newsletter Template",
         "Write a monthly newsletter template for Sunaiva/Talking Widget customers. Sections: product update highlights, customer success story spotlight, tips and tricks, upcoming features preview, community highlights. Include placeholder content for first edition."),
        ("EMAIL-M15", "Email - Churn Prevention Sequence",
         "Write a 3-email churn prevention sequence triggered when usage drops >50% or payment fails. Email 1: 'We miss you' + usage tips. Email 2: offer to switch to lower tier vs cancel. Email 3: final retention offer (1 month free or 3 months at 30% off)."),
        ("EMAIL-M16", "Email - Agency Partner Onboarding",
         "Write a 5-email onboarding sequence for new agency partners. Email 1: welcome + partner portal access. Email 2: how commission works (30% recurring). Email 3: sales materials and pitch deck. Email 4: first client installation walkthrough. Email 5: scaling tips (how top partners earn $5K+/mo)."),
        ("EMAIL-M17", "Email - Feature Announcement Template",
         "Write 3 feature announcement email templates (reusable). Template A: major feature launch (new CRM integration). Template B: minor improvement (performance update, new template). Template C: beta feature invitation. Include subject line variants for A/B testing."),
        ("EMAIL-M18", "Email - Customer Success Stories",
         "Write 5 customer success story emails (fictional but realistic). Industries: plumbing, dental, real estate, legal, restaurant. Each: before/after stats (missed calls reduced X%, revenue up Y%), customer quote, specific use case, CTA to try same setup."),
        ("EMAIL-M19", "Email - Seasonal Campaign - Tax Season",
         "Write a 3-email campaign for Australian tax season (July-October). Target: accountants and tax agents. Focus on: handling client call volume during EOFY rush, appointment booking for tax consultations, after-hours availability during peak season."),
        ("EMAIL-M20", "Email - Seasonal Campaign - Summer Trades",
         "Write a 3-email campaign for Australian summer (December-February). Target: tradies (plumbing, HVAC, electrical). Focus on: emergency call handling during holiday period, booking jobs while on leave, never missing a customer during peak demand."),
    ]
    for story_id, title, prompt in email_tasks:
        tasks.append({
            "agent_id": f"minimax-{agent_id:03d}",
            "story_id": story_id,
            "title": title,
            "prompt": prompt,
            "system_prompt": "You are an expert email copywriter specializing in B2B SaaS cold outreach and lifecycle email marketing. Write compelling, concise emails that drive action. Australian English (colour, organisation, etc.). Always show value before price. Use the strikethrough pricing technique (show higher price crossed out, then actual price). Each email should be under 150 words for cold outreach, under 300 words for lifecycle emails.",
            "max_tokens": 4096
        })
        agent_id += 1

    # ── BATCH 2: Landing Page Content (15 tasks) ──
    landing_tasks = [
        ("LP-M01", "Talking Widget - Hero Section Copy",
         "Write hero section copy for the Talking Widget landing page. Include: headline (8 words max, benefit-focused), subheadline (20 words, explain what it does), 3 bullet points (key benefits), CTA button text. Target audience: Australian business owners who miss phone calls. Tone: confident, professional, Australian."),
        ("LP-M02", "Talking Widget - Features Section",
         "Write content for a 6-feature section on the Talking Widget landing page. Features: 1) 24/7 AI Voice Answering, 2) Instant Lead Capture, 3) Smart Call Routing, 4) Real-time Analytics Dashboard, 5) CRM Integration, 6) 5-Minute Setup. Each feature needs: icon suggestion, title (4 words), description (30 words), supporting stat or proof point."),
        ("LP-M03", "Talking Widget - Pricing Section",
         "Write pricing section copy for 3 tiers: Starter ($197/mo), Pro ($397/mo), Growth ($597/mo). Show strikethrough original prices ($497, $797, $997). Include: tier names, price, what's included (call minutes, features, integrations), 'Most Popular' badge on Pro, CTA per tier, money-back guarantee note."),
        ("LP-M04", "Talking Widget - FAQ Section",
         "Write 12 FAQ questions and answers for the Talking Widget. Cover: setup time, AI voice quality, custom greetings, CRM integration, pricing/billing, call recordings, multi-language support, mobile app, data security, cancellation, phone number provision, competitor comparison."),
        ("LP-M05", "Talking Widget - Testimonials",
         "Write 6 realistic testimonials for the Talking Widget. Industries: plumber (Sydney), electrician (Melbourne), dentist (Brisbane), real estate agent (Perth), lawyer (Adelaide), HVAC company (Gold Coast). Each: 2-3 sentences, specific results (X% more leads, Y hours saved), name and business type. Make them sound authentic, not salesy."),
        ("LP-M06", "Talking Widget - How It Works Section",
         "Write a 'How It Works' section with 4 steps: 1) Sign Up & Choose Your Plan, 2) Configure Your AI Voice Agent, 3) Install the Widget (one line of code), 4) Start Capturing Leads. Each step: title, 25-word description, suggested illustration/icon description."),
        ("LP-M07", "Talking Widget - ROI Calculator Copy",
         "Write supporting copy for an ROI calculator section. Include: intro paragraph explaining missed call cost ($500+ average for tradies), calculator input labels (monthly calls, current miss rate, average job value), result messaging templates ('You're leaving $X on the table'), CTA to start free trial."),
        ("LP-M08", "Talking Widget - Agency Partner Page",
         "Write a full agency partner landing page. Sections: hero (earn 30% recurring commission), why partner with us (5 benefits), how it works (3 steps), commission structure, partner success stories (2 fictional), FAQ (8 questions), CTA to apply for partnership."),
        ("LP-M09", "Sunaiva AI Memory - Updated Pricing Copy",
         "Write updated pricing section for Sunaiva AI Memory with 4 tiers. Static Memory Vault: Starter $49/mo (200 conversations), Pro $79/mo (500), Power $149/mo (2,000), Enterprise $247/mo (10,000). Show 50% beta discount prominently. Include feature comparison table. CTA: Join beta waitlist."),
        ("LP-M10", "Sunaiva AI Memory - Platform Comparison",
         "Write a comparison section showing how Sunaiva AI Memory works with different AI platforms. Platforms: Claude (direct JSON export), ChatGPT (conversations.json from data export), Gemini (Google Takeout). For each: how to export, what we extract, setup time. Position as 'Works with your favorite AI'."),
        ("LP-M11", "Sunaiva AI Memory - Use Cases Section",
         "Write 6 use cases for Sunaiva AI Memory. 1) Remember every AI decision, 2) Search across all your AI conversations, 3) Build a personal knowledge base, 4) Never repeat context to your AI, 5) Track preferences across platforms, 6) Export your AI memory to any tool via MCP. Each: title, 40-word description, icon suggestion."),
        ("LP-M12", "Talking Widget - Industry Pages - Trades",
         "Write a trades-specific landing page section for the Talking Widget. Cover: plumbing, electrical, HVAC. Include: industry-specific pain points (on the tools, can't answer phone), specific use cases, example greeting scripts, Australian tradie language/tone, local phone number mention (+61)."),
        ("LP-M13", "Talking Widget - Industry Pages - Professional Services",
         "Write a professional services landing page section. Cover: legal, accounting, consulting. Include: industry-specific pain points (client confidentiality, complex intake), compliance considerations, professional tone requirements, appointment scheduling focus."),
        ("LP-M14", "Talking Widget - Security & Privacy Page",
         "Write a security and privacy page for the Talking Widget. Cover: data encryption (at rest and in transit), Australian data residency, call recording consent, GDPR/APPs compliance, SOC2 roadmap, data retention policies, access controls, regular security audits."),
        ("LP-M15", "Talking Widget - Terms of Service",
         "Write Terms of Service for the Talking Widget SaaS platform. Cover: service description, user obligations, billing and refunds, intellectual property, data handling, limitation of liability, termination, governing law (Australia/NSW), dispute resolution. Written in plain English, not legalese."),
    ]
    for story_id, title, prompt in landing_tasks:
        tasks.append({
            "agent_id": f"minimax-{agent_id:03d}",
            "story_id": story_id,
            "title": title,
            "prompt": prompt,
            "system_prompt": "You are an expert SaaS copywriter and conversion rate optimizer. Write compelling, benefit-focused copy that drives signups and conversions. Use Australian English. Brand voice: confident, professional, human (not corporate). Dark theme aesthetic (Cosmic Void #0B0C0E, Signal Blue #3B82F6). Always show value before price, use strikethrough for original prices.",
            "max_tokens": 4096
        })
        agent_id += 1

    # ── BATCH 3: Documentation (15 tasks) ──
    docs_tasks = [
        ("DOC-M01", "API Reference - Widget Endpoints",
         "Write comprehensive API documentation for all Talking Widget endpoints. Cover: authentication (JWT Bearer), POST /api/widgets (create), GET /api/widgets (list), GET /api/widgets/{id}, PUT /api/widgets/{id}, DELETE /api/widgets/{id}. For each: method, URL, headers, request body schema, response schema, error codes, curl examples."),
        ("DOC-M02", "API Reference - Analytics Endpoints",
         "Write API documentation for analytics endpoints. Cover: GET /api/analytics/summary (overview stats), GET /api/analytics/calls (call history with pagination), GET /api/analytics/trends (time-series data), GET /api/analytics/peak-hours. Include: query parameters, date range filtering, response formats, chart-ready data schemas."),
        ("DOC-M03", "API Reference - Billing Endpoints",
         "Write API documentation for billing endpoints. Cover: POST /api/billing/checkout (create Stripe session), POST /api/billing/webhook (Stripe events), GET /api/billing/subscription (current plan), GET /api/billing/invoices (history), POST /api/billing/portal (customer portal). Include Stripe integration details."),
        ("DOC-M04", "Widget Installation Guide - WordPress",
         "Write a step-by-step installation guide for adding the Talking Widget to WordPress. Cover: using header/footer plugin method, theme functions.php method, page-specific installation, WooCommerce checkout page installation, troubleshooting (CSP issues, caching plugins), screenshots description for each step."),
        ("DOC-M05", "Widget Installation Guide - Shopify",
         "Write a step-by-step installation guide for Shopify. Cover: theme.liquid method, Shopify sections method, app integration (if applicable), page-specific installation, checkout page considerations, troubleshooting (Shopify CSP, theme conflicts)."),
        ("DOC-M06", "Widget Installation Guide - Custom HTML",
         "Write a comprehensive guide for installing the widget on any HTML website. Cover: basic script tag installation, async loading, configuration options, event hooks (onCallStart, onCallEnd, onLeadCapture), custom styling, CSP configuration, subresource integrity."),
        ("DOC-M07", "Developer Guide - Webhook Integration",
         "Write a developer guide for webhook integrations. Cover: webhook event types (call.started, call.ended, lead.captured, widget.installed), payload schemas, HMAC signature verification code samples (Python, Node.js, PHP), retry behavior, testing with webhook.site, debugging tips."),
        ("DOC-M08", "Developer Guide - CRM Integration",
         "Write a developer guide for CRM integrations. Cover: HubSpot setup (API key, field mapping), GHL setup (API key, pipeline config), custom CRM via webhooks, field mapping configuration, sync frequency, error handling, data format. Include screenshots description."),
        ("DOC-M09", "User Guide - Dashboard Walkthrough",
         "Write a comprehensive user guide for the Talking Widget dashboard. Cover: overview page (key metrics), widgets section (create, configure, manage), analytics (reading charts, filtering), leads (managing, exporting), billing (plans, invoices), settings (profile, notifications, integrations). Include tips for each section."),
        ("DOC-M10", "User Guide - Voice Configuration",
         "Write a guide for configuring voice settings. Cover: choosing a voice (Australian male/female, American, British), custom greeting scripts (best practices, examples by industry), conversation flow design, escalation triggers (when to transfer to human), after-hours configuration, holiday schedule setup."),
        ("DOC-M11", "Troubleshooting Guide",
         "Write a comprehensive troubleshooting guide. Cover: widget not appearing (CSP, ad blockers, script errors), call quality issues (bandwidth, codec settings), webhook not firing (URL validation, firewall), analytics not updating (cache, timezone), billing issues (card declined, plan limits). Include diagnostic steps for each."),
        ("DOC-M12", "SEO/AEO Whitepaper - AI Voice for Business",
         "Write a 2000-word whitepaper: 'How AI Voice Agents Are Transforming Australian Small Business'. Sections: the missed call epidemic (stats), evolution of receptionist services, how AI voice differs from IVR, ROI analysis for SMBs, case studies (3 fictional), future trends, CTA. Include data points and sources."),
        ("DOC-M13", "Agency Partner Playbook",
         "Write a partner sales playbook for agencies selling the Talking Widget. Cover: ideal customer profile, discovery questions, objection handling (10 common objections + responses), pricing presentation tips, demo script, proposal template, onboarding checklist for new clients. Target: website agencies."),
        ("DOC-M14", "Privacy Policy",
         "Write a Privacy Policy for the Talking Widget platform. Cover: data collected (personal info, call recordings, analytics), how data is used, data sharing (Telnyx for calls, Stripe for payments), data retention, user rights (access, correction, deletion), cookies, Australian Privacy Principles compliance, contact info."),
        ("DOC-M15", "Changelog Template and First Entry",
         "Write a changelog format and the first 3 entries. Format: date, version, categories (Added/Changed/Fixed/Removed). Entry 1: v1.0.0 launch (core features). Entry 2: v1.1.0 (CRM integrations, analytics improvements). Entry 3: v1.2.0 (agency partner portal, A/B testing). Keep entries concise but informative."),
    ]
    for story_id, title, prompt in docs_tasks:
        tasks.append({
            "agent_id": f"minimax-{agent_id:03d}",
            "story_id": story_id,
            "title": title,
            "prompt": prompt,
            "system_prompt": "You are an expert technical writer creating documentation for a SaaS platform. Write clear, concise, well-structured documentation. Use markdown formatting. Include code examples where appropriate. Audience: developers and non-technical business owners. Use Australian English.",
            "max_tokens": 6144
        })
        agent_id += 1

    # ── BATCH 4: Social Media Content (15 tasks) ──
    social_tasks = [
        ("SM-M01", "LinkedIn Content Calendar - Week 1",
         "Create 5 LinkedIn posts for a B2B SaaS founder launching a voice AI widget. Posts: 1) Problem post (missed calls cost), 2) Behind-the-scenes build post, 3) Industry insight (AI voice market), 4) Customer pain point story, 5) Product announcement teaser. Each: text (150-250 words), hashtag suggestions, best posting time (AEST)."),
        ("SM-M02", "LinkedIn Content Calendar - Week 2",
         "Create 5 LinkedIn posts. Posts: 1) Case study post (fictional plumber), 2) Comparison post (old receptionist vs AI), 3) Tech deep-dive (how voice AI works), 4) Industry trend post, 5) Call to action / demo offer. Each: text, hashtags, posting time."),
        ("SM-M03", "LinkedIn Content Calendar - Week 3",
         "Create 5 LinkedIn posts. Posts: 1) Partnership announcement (agency program), 2) Feature highlight (analytics dashboard), 3) Customer success metric (fictional), 4) Thought leadership (future of Australian SMB tech), 5) Personal founder story. Each: text, hashtags, posting time."),
        ("SM-M04", "LinkedIn Content Calendar - Week 4",
         "Create 5 LinkedIn posts. Posts: 1) Monthly recap/metrics, 2) User feedback highlight, 3) Industry-specific post (dental practices), 4) ROI breakdown post (with numbers), 5) Community engagement post (ask question). Each: text, hashtags, posting time."),
        ("SM-M05", "Twitter/X Content - 20 Tweets",
         "Write 20 tweets for promoting Talking Widget on Twitter/X. Mix: 5 problem-aware (missed calls pain), 5 solution-aware (AI receptionist benefits), 5 product (features/pricing), 5 engagement (questions/polls). Each under 280 chars. Include thread ideas (2 full threads of 5 tweets each)."),
        ("SM-M06", "Reddit Strategy - 4 Week Plan",
         "Write a 4-week Reddit engagement plan. Target subreddits: r/smallbusiness, r/entrepreneur, r/australia, r/VOIP, r/artificial. Week 1-2: comment on relevant posts (write 10 example comments), Week 3-4: original posts (write 4 value-first posts). Rules: never hard sell, always provide value first, follow subreddit rules."),
        ("SM-M07", "Facebook Ad Copy - 5 Variants",
         "Write 5 Facebook ad copy variants for the Talking Widget. Each: headline (5 words), primary text (100 words), description (25 words), CTA button choice. Variants: 1) Pain-focused, 2) Solution-focused, 3) Social proof, 4) Urgency, 5) ROI-focused. Target audience: Australian SMB owners."),
        ("SM-M08", "Google Ads Copy - Search Campaigns",
         "Write Google Ads copy for 5 keyword groups. Groups: 1) 'ai receptionist australia', 2) 'missed calls business solution', 3) 'virtual receptionist for tradies', 4) 'voice ai widget website', 5) 'automated phone answering'. Each: 3 headline variants (30 chars), 2 description variants (90 chars), sitelink extensions."),
        ("SM-M09", "YouTube Video Scripts - 3 Videos",
         "Write scripts for 3 YouTube videos. Video 1: 'Stop Missing Calls: AI Voice Widget Demo' (3 min). Video 2: 'How to Install a Voice Widget in 5 Minutes' (5 min tutorial). Video 3: 'Talking Widget vs Traditional Receptionist: Real Cost Comparison' (4 min). Include: intro hook, main content, CTA, thumbnail concept."),
        ("SM-M10", "Blog Post - AI Voice Agents for Tradies",
         "Write a 1500-word blog post: '5 Ways AI Voice Agents Are Saving Australian Tradies Thousands'. Cover: the missed call problem, 5 specific use cases with examples, ROI breakdown, how to get started, FAQ. SEO-optimized with target keyword 'ai voice agent australia'. Include meta title and description."),
        ("SM-M11", "Blog Post - Choosing the Right AI Receptionist",
         "Write a 1500-word blog post: 'How to Choose the Right AI Receptionist for Your Business in 2026'. Cover: what to look for (voice quality, integrations, pricing, support), comparison framework, red flags, our recommendation. Include competitor mentions (balanced/fair). SEO target: 'best ai receptionist 2026'."),
        ("SM-M12", "Blog Post - Website Voice Widget Guide",
         "Write a 1500-word blog post: 'Complete Guide to Adding a Voice Widget to Your Website'. Cover: what is a voice widget, benefits over chat, how it works technically, installation guide (general), best practices, common mistakes, FAQ. SEO target: 'website voice widget'."),
        ("SM-M13", "Case Study Template - Plumbing Business",
         "Write a detailed case study: 'How Sydney Plumber Dave Increased Leads by 40% with Talking Widget'. Structure: challenge (missing calls while on jobs), solution (installed Talking Widget), implementation (5-minute setup story), results (specific numbers: calls answered, leads captured, revenue increase), quote from Dave. Make realistic but fictional."),
        ("SM-M14", "Case Study Template - Dental Practice",
         "Write a detailed case study: 'How Brisbane Dental Reduced Reception Costs by 60% with AI Voice'. Structure: challenge (overwhelmed front desk), solution (AI handles routine calls), implementation, results (specific numbers: wait time reduction, appointment booking rate, staff reallocation), patient satisfaction improvement."),
        ("SM-M15", "Product Hunt Launch Copy",
         "Write Product Hunt launch copy for the Talking Widget. Include: tagline (under 60 chars), description (260 chars), detailed description (2 paragraphs), maker's comment (personal story of why we built this), 5 key features (with emoji), first comment (answering expected questions). Tone: authentic builder, not corporate."),
    ]
    for story_id, title, prompt in social_tasks:
        tasks.append({
            "agent_id": f"minimax-{agent_id:03d}",
            "story_id": story_id,
            "title": title,
            "prompt": prompt,
            "system_prompt": "You are an expert SaaS marketing content creator specializing in B2B social media, content marketing, and growth hacking. Write engaging, authentic content that drives awareness and conversions. Australian English. Brand: Sunaiva / Talking Widget. Tone: professional but human, data-driven, not salesy. Always lead with value.",
            "max_tokens": 4096
        })
        agent_id += 1

    # ── BATCH 5: Market Research & Analysis (15 tasks) ──
    research_tasks = [
        ("RES-M01", "Australian Tradie Market Size Analysis",
         "Analyze the Australian trades market. Cover: number of trade businesses by category (plumbing, electrical, HVAC, building), average revenue per business, technology adoption rates, common software tools used, biggest pain points, spending on communication/reception services. Cite realistic sources."),
        ("RES-M02", "Voice AI Market Landscape 2026",
         "Analyze the voice AI market landscape as of early 2026. Cover: key players (ElevenLabs, Vapi, Bland, Retell, OpenAI), market size estimates, growth rate, key trends, pricing comparison, technology stack comparison. Focus on conversational AI for business phone calls."),
        ("RES-M03", "Competitor Deep Dive - Bland AI",
         "Deep analysis of Bland AI as a competitor. Cover: pricing (detailed breakdown), features, target market, strengths, weaknesses, customer reviews/sentiment, technology stack, funding/team size, Australian presence, integration options. Compare directly to our Talking Widget positioning."),
        ("RES-M04", "Competitor Deep Dive - Retell AI",
         "Deep analysis of Retell AI. Cover: pricing, features, target market, strengths, weaknesses, customer reviews, technology, Australian presence. Note their developer-focused approach vs our business-user focus. Compare API complexity, pricing models, voice quality options."),
        ("RES-M05", "Competitor Deep Dive - Smith.ai",
         "Deep analysis of Smith.ai (human + AI hybrid receptionist). Cover: pricing (starts at $240/mo for 20 calls), features, how they blend human and AI, target market (legal, real estate), strengths (human fallback), weaknesses (expensive, limited AI). Position our pure-AI approach as alternative."),
        ("RES-M06", "Australian Privacy Act Impact Analysis",
         "Analyze how the Australian Privacy Act (APPs) affects a voice AI SaaS product. Cover: call recording consent requirements (state-by-state), data storage obligations, cross-border data transfer, customer data rights, required privacy policy elements, breach notification requirements."),
        ("RES-M07", "CRM Market Share - Australian SMBs",
         "Analyze CRM market share among Australian SMBs. Cover: HubSpot, Salesforce, GHL (GoHighLevel), Tradify, ServiceM8, SimPRO, Jobber, and others. Focus on trades and professional services. Include: pricing, market share estimates, integration capabilities, why businesses choose each."),
        ("RES-M08", "Website Agency Revenue Model Analysis",
         "Analyze the website agency market as a channel partner opportunity. Cover: number of agencies in Australia, average client count, typical website packages offered, revenue per client, willingness to resell SaaS tools, commission expectations, competitive partner programs. Assess if 30% commission is competitive."),
        ("RES-M09", "Telnyx vs Twilio Cost Comparison",
         "Detailed cost comparison: Telnyx vs Twilio for a voice AI SaaS. Cover: per-minute calling rates (Australian inbound, outbound, toll-free), WebRTC costs, phone number provisioning, API request pricing, support tiers, uptime SLAs, Australian number availability. Calculate cost for 100, 1000, 10000 monthly minutes."),
        ("RES-M10", "Customer Acquisition Cost Benchmarks",
         "Research customer acquisition cost (CAC) benchmarks for B2B SaaS targeting SMBs. Cover: average CAC by channel (cold email, PPC, content marketing, partnerships, social media), typical conversion rates, time to convert, LTV:CAC ratio targets, Australian-specific data where available."),
        ("RES-M11", "AI Voice Quality Comparison",
         "Compare AI voice quality across providers: Telnyx (NaturalHD voices), ElevenLabs, OpenAI TTS, Google Cloud TTS, Amazon Polly. Evaluate: naturalness, Australian accent availability, latency, customization options, pricing per character/minute, streaming support."),
        ("RES-M12", "Australian Small Business Technology Survey",
         "Compile key findings about Australian small business technology adoption. Cover: % using cloud software, average monthly SaaS spend, most valued features, barriers to adoption (cost, complexity, trust), preferred support channels, mobile vs desktop usage, industry-specific patterns."),
        ("RES-M13", "Pricing Psychology for SaaS",
         "Research pricing psychology best practices for B2B SaaS. Cover: anchor pricing (strikethrough technique), charm pricing, tier naming psychology, feature allocation strategy, free trial vs freemium, annual vs monthly discount, money-back guarantee impact, pricing page layout best practices."),
        ("RES-M14", "Email Deliverability Best Practices",
         "Research email deliverability best practices for cold outreach using Instantly.ai. Cover: warm-up protocols, sending volume ramp, domain reputation management, subject line spam triggers to avoid, optimal sending times (Australian timezones), reply rate benchmarks, bounce rate management."),
        ("RES-M15", "SEO Keyword Research - Voice AI",
         "Conduct keyword research for voice AI SaaS targeting Australia. Provide: 50 target keywords with search volume estimates, keyword difficulty scores, content type recommendations (blog, landing page, FAQ), top 5 high-opportunity keywords for quick wins, long-tail keyword clusters."),
    ]
    for story_id, title, prompt in research_tasks:
        tasks.append({
            "agent_id": f"minimax-{agent_id:03d}",
            "story_id": story_id,
            "title": title,
            "prompt": prompt,
            "system_prompt": "You are an expert market research analyst specializing in B2B SaaS, Australian business markets, and voice AI technology. Provide data-driven analysis with realistic estimates and sources. When exact data is unavailable, provide reasonable estimates clearly marked as estimates. Structure your analysis with clear sections, tables, and key takeaways.",
            "max_tokens": 6144
        })
        agent_id += 1

    # ── BATCH 6: Sales & Business Development (15 tasks) ──
    sales_tasks = [
        ("SALES-M01", "Sales Pitch Deck Outline",
         "Write the content for a 12-slide sales pitch deck for the Talking Widget. Slides: 1) Title, 2) Problem (missed calls stats), 3) Impact (revenue loss), 4) Solution overview, 5) How it works (4 steps), 6) Demo/screenshots description, 7) Features, 8) Pricing, 9) ROI calculator, 10) Customer testimonials, 11) About us/team, 12) Next steps/CTA. Include speaker notes for each slide."),
        ("SALES-M02", "Sales Objection Handling Guide",
         "Write a comprehensive objection handling guide for Talking Widget sales calls. Cover 15 common objections: 'too expensive', 'AI doesn't sound natural', 'my customers want a real person', 'we don't get many calls', 'I already have a receptionist', 'concerned about privacy', 'not tech savvy', etc. For each: empathize, reframe, provide evidence, redirect."),
        ("SALES-M03", "Competitive Battle Card - vs Smith.ai",
         "Create a sales battle card comparing Talking Widget vs Smith.ai. Format: quick comparison table, our strengths, their strengths, when they're a better fit, when we're a better fit, killer questions to ask prospect, pricing comparison, key differentiators. One page, scannable."),
        ("SALES-M04", "Competitive Battle Card - vs Bland AI",
         "Create a sales battle card comparing Talking Widget vs Bland AI. Bland is developer-focused, expensive at scale, US-centric. Our advantages: business-user friendly, Australian voices, simple pricing, widget form factor, agency program."),
        ("SALES-M05", "ROI Calculator Methodology",
         "Design the methodology for an ROI calculator. Inputs: monthly missed calls (estimate from industry data), average job value, close rate, current receptionist cost. Calculations: missed revenue, Talking Widget cost, net savings, payback period, 12-month ROI. Provide formulas and default values per industry."),
        ("SALES-M06", "Discovery Call Script",
         "Write a discovery call script for selling Talking Widget. Structure: 1) Opening (30 sec), 2) Qualifying questions (5 questions about current call handling), 3) Pain amplification (help them feel the cost), 4) Solution presentation (2 min), 5) Pricing presentation, 6) Handling questions, 7) Close (commitment to trial or demo). 15-minute call total."),
        ("SALES-M07", "Demo Script - Live Product Walkthrough",
         "Write a demo script for a live Talking Widget walkthrough. Flow: 1) Show the landing page (30 sec), 2) Sign up process (1 min), 3) Widget configuration (2 min), 4) Live call demo (call the widget from phone, 2 min), 5) Dashboard - show the call appear in analytics (1 min), 6) Lead captured - show lead details (1 min), 7) Pricing and CTA (2 min). Total: 10 min."),
        ("SALES-M08", "Proposal Template",
         "Write a proposal template for agency partners presenting Talking Widget to their clients. Sections: executive summary, client needs assessment (fill-in), recommended solution, implementation plan, pricing (show value first, then cost), expected results, timeline, next steps. Include terms and conditions."),
        ("SALES-M09", "Agency Partner Recruitment Script",
         "Write an outreach script for recruiting website agencies as partners. Cover: identify their client base, explain commission model (30% recurring), show partner portal, share success metrics of existing partners (fictional), explain onboarding process, close on partnership agreement. Email + phone script."),
        ("SALES-M10", "Customer Onboarding Checklist",
         "Write a comprehensive customer onboarding checklist. Phases: 1) Account setup (email verification, plan selection), 2) Widget configuration (business info, voice, greeting), 3) Installation (embed code, verification), 4) Testing (make test call, review in dashboard), 5) Go-live (enable for public), 6) 7-day check-in (review analytics, optimize). Include email templates for each phase."),
        ("SALES-M11", "Retention Strategy Document",
         "Write a customer retention strategy. Cover: onboarding excellence (reduce time-to-value), health score model (usage frequency, feature adoption, support tickets), churn risk indicators, intervention playbooks (low usage, payment issues, feature requests), upsell/cross-sell opportunities, loyalty program concepts."),
        ("SALES-M12", "Referral Program Design",
         "Design a customer referral program. Cover: incentive structure (give $50 credit, get $50 credit), referral tracking mechanism, viral coefficient targets, referral email templates, in-app referral widget, partner vs customer referral distinction, T&Cs, success metrics."),
        ("SALES-M13", "Vertical-Specific Sales Sheet - Trades",
         "Write a one-page sales sheet targeting Australian tradies. Cover: the problem (missing calls while on the tools), specific stats (avg tradie misses 5+ calls/day), the solution (2-sentence pitch), features that matter to tradies (24/7, job booking, quote requests), pricing ($197/mo with strikethrough $497), guarantee, QR code placeholder for demo."),
        ("SALES-M14", "Vertical-Specific Sales Sheet - Professional Services",
         "Write a one-page sales sheet targeting professional services (legal, accounting, medical). Cover: the problem (overwhelmed front desk, after-hours inquiries), specific stats, the solution, features (appointment booking, confidential screening, intake forms), pricing, compliance notes (privacy, data security)."),
        ("SALES-M15", "Annual Plan Pitch",
         "Write compelling copy and a sales script for converting monthly subscribers to annual plans. Annual discount: 20% (2 months free). Cover: email pitch, in-app banner copy, checkout page upsell, renewal reminder sequence, comparison table (monthly vs annual savings). Show total savings prominently."),
    ]
    for story_id, title, prompt in sales_tasks:
        tasks.append({
            "agent_id": f"minimax-{agent_id:03d}",
            "story_id": story_id,
            "title": title,
            "prompt": prompt,
            "system_prompt": "You are an expert B2B SaaS sales strategist specializing in Australian SMB markets. Create compelling sales materials that focus on ROI, reduce friction, and drive conversions. Use Australian English, be specific with numbers and examples. Always lead with customer value, not product features.",
            "max_tokens": 6144
        })
        agent_id += 1

    # ── BATCH 7: Instantly.ai Campaign Setup (5 tasks to reach 100) ──
    instantly_tasks = [
        ("INST-M01", "Instantly.ai Campaign - Tradies Wave 1",
         "Create the complete configuration for an Instantly.ai email campaign targeting 1000 Australian tradies. Include: campaign name, sending schedule (AEST business hours), daily sending limit per inbox (25/inbox, 15 inboxes = 375/day), warm-up settings, tracking settings, A/B test configuration for subject lines. Output as a step-by-step setup guide with exact field values."),
        ("INST-M02", "Instantly.ai Campaign - Agencies Wave 1",
         "Create Instantly.ai campaign configuration for 500 Australian website agencies. Include: campaign name, targeted sequence (partnership focused), sending schedule, daily limits, custom tracking domain setup, reply handling instructions, lead scoring criteria, follow-up rules."),
        ("INST-M03", "Instantly.ai Lead List Segmentation",
         "Write a guide for segmenting the 3,452 tradie leads and 329 influencer gold list for optimal Instantly.ai campaigns. Segments: by industry, by state/city, by business size, by email domain quality. Include: import format (CSV schema), deduplication rules, suppression list management, best practices for list hygiene."),
        ("INST-M04", "Instantly.ai Domain Warm-Up Schedule",
         "Create a detailed domain warm-up schedule for 3 domains (consultsense.org, streamlinechart.com, fusiive.com) x 5 inboxes each = 15 inboxes. Include: daily volume ramp (week 1-4), warm-up pool engagement, SPF/DKIM/DMARC verification checklist, inbox health monitoring metrics, when to start sending real campaigns."),
        ("INST-M05", "Instantly.ai Analytics and Optimization Guide",
         "Write a guide for analyzing and optimizing Instantly.ai campaign performance. Cover: key metrics (open rate, reply rate, bounce rate, unsubscribe rate), benchmarks for B2B cold email, A/B testing methodology, sequence optimization (when to add/remove emails), when to retire a sequence, scaling rules."),
    ]
    for story_id, title, prompt in instantly_tasks:
        tasks.append({
            "agent_id": f"minimax-{agent_id:03d}",
            "story_id": story_id,
            "title": title,
            "prompt": prompt,
            "system_prompt": "You are an expert in cold email outreach and Instantly.ai campaign management. Provide actionable, specific instructions for setting up and optimizing B2B email campaigns targeting Australian businesses. Focus on deliverability, personalization, and conversion optimization. Include exact configuration values, not vague suggestions.",
            "max_tokens": 4096
        })
        agent_id += 1

    assert len(tasks) == 100, f"Expected 100 MiniMax tasks, got {len(tasks)}"
    return tasks


# ── Execution Engine ───────────────────────────────────────────────────────────

@dataclass
class AgentResult:
    """Result from a single agent execution."""
    agent_id: str
    story_id: str
    title: str
    model: str
    status: str  # success/fail
    prompt_tokens: int = 0
    completion_tokens: int = 0
    total_tokens: int = 0
    cost_usd: float = 0.0
    response_time_ms: int = 0
    response: Optional[str] = None
    error: Optional[str] = None
    timestamp: str = field(default_factory=lambda: datetime.now(timezone.utc).isoformat())


async def execute_agent(
    session: aiohttp.ClientSession,
    task: Dict[str, Any],
    model_id: str,
    api_key: str
) -> AgentResult:
    """Execute a single agent task via OpenRouter."""
    start = time.time()

    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
        "HTTP-Referer": "https://github.com/genesis-system",
        "X-Title": "Genesis 200-Agent Swarm"
    }

    payload = {
        "model": model_id,
        "messages": [
            {"role": "system", "content": task["system_prompt"]},
            {"role": "user", "content": task["prompt"]}
        ],
        "temperature": 0.7,
        "max_tokens": task.get("max_tokens", 4096)
    }

    try:
        timeout = aiohttp.ClientTimeout(total=REQUEST_TIMEOUT)
        async with session.post(OPENROUTER_BASE_URL, json=payload, headers=headers, timeout=timeout) as resp:
            elapsed_ms = int((time.time() - start) * 1000)

            if resp.status != 200:
                error_text = await resp.text()
                return AgentResult(
                    agent_id=task["agent_id"],
                    story_id=task["story_id"],
                    title=task["title"],
                    model=model_id,
                    status="fail",
                    response_time_ms=elapsed_ms,
                    error=f"HTTP {resp.status}: {error_text[:300]}"
                )

            data = await resp.json()
            usage = data.get("usage", {})
            prompt_tokens = usage.get("prompt_tokens", 0)
            completion_tokens = usage.get("completion_tokens", 0)
            total_tokens = usage.get("total_tokens", 0)

            # Cost calculation
            cost_per_mtok = 1.07 if "kimi" in model_id.lower() else 1.00
            cost_usd = (total_tokens / 1_000_000) * cost_per_mtok

            response_text = data["choices"][0]["message"]["content"]

            return AgentResult(
                agent_id=task["agent_id"],
                story_id=task["story_id"],
                title=task["title"],
                model=model_id,
                status="success",
                prompt_tokens=prompt_tokens,
                completion_tokens=completion_tokens,
                total_tokens=total_tokens,
                cost_usd=cost_usd,
                response_time_ms=elapsed_ms,
                response=response_text
            )

    except asyncio.TimeoutError:
        return AgentResult(
            agent_id=task["agent_id"],
            story_id=task["story_id"],
            title=task["title"],
            model=model_id,
            status="fail",
            response_time_ms=int((time.time() - start) * 1000),
            error=f"Timeout after {REQUEST_TIMEOUT}s"
        )
    except Exception as e:
        return AgentResult(
            agent_id=task["agent_id"],
            story_id=task["story_id"],
            title=task["title"],
            model=model_id,
            status="fail",
            response_time_ms=int((time.time() - start) * 1000),
            error=str(e)[:300]
        )


async def execute_batch(
    session: aiohttp.ClientSession,
    tasks: List[Dict[str, Any]],
    model_id: str,
    api_key: str,
    batch_size: int,
    batch_delay: int,
    team_name: str
) -> List[AgentResult]:
    """Execute tasks in batches."""
    results = []
    total = len(tasks)

    for i in range(0, total, batch_size):
        batch = tasks[i:i + batch_size]
        batch_num = (i // batch_size) + 1
        total_batches = (total + batch_size - 1) // batch_size

        print(f"  [{team_name}] Batch {batch_num}/{total_batches}: firing {len(batch)} agents...")

        try:
            batch_results = await asyncio.wait_for(
                asyncio.gather(
                    *[execute_agent(session, t, model_id, api_key) for t in batch]
                ),
                timeout=BATCH_TIMEOUT
            )
            results.extend(batch_results)

            successes = sum(1 for r in batch_results if r.status == "success")
            failures = sum(1 for r in batch_results if r.status == "fail")
            batch_cost = sum(r.cost_usd for r in batch_results)
            print(f"    {successes} success, {failures} failed, ${batch_cost:.4f} cost")

            # Delay between batches
            if i + batch_size < total:
                print(f"    Waiting {batch_delay}s before next batch...")
                await asyncio.sleep(batch_delay)

        except asyncio.TimeoutError:
            print(f"    Batch {batch_num} TIMED OUT after {BATCH_TIMEOUT}s")
            for t in batch:
                results.append(AgentResult(
                    agent_id=t["agent_id"],
                    story_id=t["story_id"],
                    title=t["title"],
                    model=model_id,
                    status="fail",
                    error=f"Batch timeout after {BATCH_TIMEOUT}s"
                ))

    return results


def save_agent_output(result: AgentResult, output_dir: str):
    """Save individual agent output to a file."""
    os.makedirs(output_dir, exist_ok=True)
    safe_id = result.story_id.replace("/", "_")
    filepath = os.path.join(output_dir, f"{safe_id}_{result.agent_id}.md")

    with open(filepath, "w", encoding="utf-8") as f:
        f.write(f"# {result.title}\n")
        f.write(f"**Story ID**: {result.story_id}\n")
        f.write(f"**Agent**: {result.agent_id}\n")
        f.write(f"**Model**: {result.model}\n")
        f.write(f"**Status**: {result.status}\n")
        f.write(f"**Tokens**: {result.total_tokens:,}\n")
        f.write(f"**Cost**: ${result.cost_usd:.4f}\n")
        f.write(f"**Response Time**: {result.response_time_ms}ms\n")
        f.write(f"**Timestamp**: {result.timestamp}\n\n")
        f.write("---\n\n")
        if result.response:
            f.write(result.response)
        elif result.error:
            f.write(f"**ERROR**: {result.error}\n")


def save_manifest(kimi_tasks, minimax_tasks, kimi_results, minimax_results):
    """Save agent manifest to hive/agents_active.json."""
    os.makedirs(os.path.dirname(MANIFEST_FILE), exist_ok=True)

    manifest = {
        "swarm_id": f"session17_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
        "fired_at": datetime.now(timezone.utc).isoformat(),
        "protocol": "200-AGENT MINIMUM PROTOCOL",
        "total_agents": len(kimi_tasks) + len(minimax_tasks),
        "kimi_agents": len(kimi_tasks),
        "minimax_agents": len(minimax_tasks),
        "models": {
            "kimi": KIMI_MODEL,
            "minimax": MINIMAX_MODEL
        },
        "status": "COMPLETED" if kimi_results and minimax_results else "RUNNING",
        "results_summary": {},
        "agents": []
    }

    all_results = (kimi_results or []) + (minimax_results or [])

    if all_results:
        total_success = sum(1 for r in all_results if r.status == "success")
        total_cost = sum(r.cost_usd for r in all_results)
        total_tokens = sum(r.total_tokens for r in all_results)

        manifest["results_summary"] = {
            "total_executed": len(all_results),
            "successes": total_success,
            "failures": len(all_results) - total_success,
            "success_rate": f"{(total_success / len(all_results) * 100):.1f}%",
            "total_cost_usd": round(total_cost, 4),
            "total_tokens": total_tokens
        }

    for result in all_results:
        manifest["agents"].append({
            "agent_id": result.agent_id,
            "story_id": result.story_id,
            "title": result.title,
            "model": result.model,
            "status": result.status,
            "tokens": result.total_tokens,
            "cost_usd": round(result.cost_usd, 4),
            "response_time_ms": result.response_time_ms,
            "timestamp": result.timestamp,
            "output_file": f"{result.story_id}_{result.agent_id}.md"
        })

    with open(MANIFEST_FILE, "w", encoding="utf-8") as f:
        json.dump(manifest, f, indent=2)

    print(f"Manifest saved: {MANIFEST_FILE}")


def generate_report(kimi_results: List[AgentResult], minimax_results: List[AgentResult]):
    """Generate execution report."""
    all_results = kimi_results + minimax_results
    total = len(all_results)
    successes = sum(1 for r in all_results if r.status == "success")
    total_cost = sum(r.cost_usd for r in all_results)
    total_tokens = sum(r.total_tokens for r in all_results)
    avg_time = sum(r.response_time_ms for r in all_results) / total if total > 0 else 0

    kimi_success = sum(1 for r in kimi_results if r.status == "success")
    kimi_cost = sum(r.cost_usd for r in kimi_results)
    minimax_success = sum(1 for r in minimax_results if r.status == "success")
    minimax_cost = sum(r.cost_usd for r in minimax_results)

    report = f"""# Genesis 200-Agent Swarm Report
**Session 17** | {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}

## Summary

| Metric | Value |
|--------|-------|
| **Total Agents Fired** | {total} |
| **Successful** | {successes} ({(successes/total*100):.1f}%) |
| **Failed** | {total - successes} |
| **Total Tokens** | {total_tokens:,} |
| **Total Cost** | ${total_cost:.4f} |
| **Avg Response Time** | {avg_time:.0f}ms |
| **Cost Per Agent** | ${total_cost/total:.4f} |

## Team Breakdown

### Kimi K2.5 (Code & Engineering)
- **Agents**: {len(kimi_results)}
- **Success**: {kimi_success}/{len(kimi_results)} ({(kimi_success/len(kimi_results)*100):.1f}%)
- **Cost**: ${kimi_cost:.4f}

### MiniMax M2.5 (Content & Marketing)
- **Agents**: {len(minimax_results)}
- **Success**: {minimax_success}/{len(minimax_results)} ({(minimax_success/len(minimax_results)*100):.1f}%)
- **Cost**: ${minimax_cost:.4f}

## Task Categories

### Kimi Tasks (Code)
"""

    # Group Kimi results by prefix
    kimi_groups = {}
    for r in kimi_results:
        prefix = r.story_id.split("-")[0]
        if prefix not in kimi_groups:
            kimi_groups[prefix] = {"total": 0, "success": 0}
        kimi_groups[prefix]["total"] += 1
        if r.status == "success":
            kimi_groups[prefix]["success"] += 1

    for prefix, stats in sorted(kimi_groups.items()):
        report += f"- **{prefix}**: {stats['success']}/{stats['total']} succeeded\n"

    report += "\n### MiniMax Tasks (Content)\n"

    minimax_groups = {}
    for r in minimax_results:
        prefix = r.story_id.split("-")[0]
        if prefix not in minimax_groups:
            minimax_groups[prefix] = {"total": 0, "success": 0}
        minimax_groups[prefix]["total"] += 1
        if r.status == "success":
            minimax_groups[prefix]["success"] += 1

    for prefix, stats in sorted(minimax_groups.items()):
        report += f"- **{prefix}**: {stats['success']}/{stats['total']} succeeded\n"

    # Failed tasks
    failed = [r for r in all_results if r.status == "fail"]
    if failed:
        report += f"\n## Failed Tasks ({len(failed)})\n\n"
        for r in failed:
            report += f"- **{r.story_id}** ({r.agent_id}): {r.error}\n"

    report += f"""
## Output Locations

- **Kimi outputs**: `{KIMI_OUTPUT_DIR}/`
- **MiniMax outputs**: `{MINIMAX_OUTPUT_DIR}/`
- **Results JSONL**: `{RESULTS_JSONL}`
- **Agent manifest**: `{MANIFEST_FILE}`
- **This report**: `{REPORT_FILE}`

## Protocol Compliance

- 200-Agent Minimum: {'COMPLIANT' if total >= 200 else 'NON-COMPLIANT (' + str(total) + '/200)'}
- All on E: drive: COMPLIANT
- No SQLite: COMPLIANT
- No C: drive: COMPLIANT

---
Generated by Genesis 200-Agent Swarm Launcher | {datetime.now(timezone.utc).isoformat()}
"""

    os.makedirs(os.path.dirname(REPORT_FILE), exist_ok=True)
    with open(REPORT_FILE, "w") as f:
        f.write(report)
    print(f"Report saved: {REPORT_FILE}")


async def main():
    """Main orchestrator - fire 200 agents."""
    print("=" * 80)
    print("GENESIS 200-AGENT SWARM LAUNCHER")
    print("Protocol: 200-AGENT MINIMUM (Kinan Directive, 2026-02-16)")
    print("=" * 80)
    print(f"Timestamp: {datetime.now().isoformat()}")
    print(f"Kimi Model: {KIMI_MODEL}")
    print(f"MiniMax Model: {MINIMAX_MODEL}")
    print(f"Output: {OUTPUT_DIR}")
    print("=" * 80)
    print()

    # Generate tasks
    print("Generating 100 Kimi K2.5 tasks (code/tests/infrastructure)...")
    kimi_tasks = generate_kimi_tasks()
    print(f"  Generated {len(kimi_tasks)} Kimi tasks")

    print("Generating 100 MiniMax M2.5 tasks (content/marketing/research)...")
    minimax_tasks = generate_minimax_tasks()
    print(f"  Generated {len(minimax_tasks)} MiniMax tasks")

    print(f"\nTotal: {len(kimi_tasks) + len(minimax_tasks)} agents ready to fire")
    print()

    # Create output directories
    os.makedirs(KIMI_OUTPUT_DIR, exist_ok=True)
    os.makedirs(MINIMAX_OUTPUT_DIR, exist_ok=True)

    # Save initial manifest (RUNNING state)
    save_manifest(kimi_tasks, minimax_tasks, None, None)

    # Execute both teams
    kimi_results = []
    minimax_results = []

    async with aiohttp.ClientSession() as session:
        # Fire MiniMax first (faster, bigger batches)
        print("=" * 80)
        print(f"TEAM MINIMAX: Firing {len(minimax_tasks)} agents (content & marketing)")
        print("=" * 80)
        minimax_results = await execute_batch(
            session, minimax_tasks, MINIMAX_MODEL, OPENROUTER_API_KEY,
            MINIMAX_BATCH_SIZE, MINIMAX_BATCH_DELAY, "MINIMAX"
        )

        # Save MiniMax outputs
        for r in minimax_results:
            save_agent_output(r, MINIMAX_OUTPUT_DIR)
        print(f"MiniMax: {sum(1 for r in minimax_results if r.status == 'success')}/{len(minimax_results)} succeeded")
        print()

        # Fire Kimi (smaller batches, rate limited)
        print("=" * 80)
        print(f"TEAM KIMI: Firing {len(kimi_tasks)} agents (code & engineering)")
        print("=" * 80)
        kimi_results = await execute_batch(
            session, kimi_tasks, KIMI_MODEL, OPENROUTER_API_KEY,
            KIMI_BATCH_SIZE, KIMI_BATCH_DELAY, "KIMI"
        )

        # Save Kimi outputs
        for r in kimi_results:
            save_agent_output(r, KIMI_OUTPUT_DIR)
        print(f"Kimi: {sum(1 for r in kimi_results if r.status == 'success')}/{len(kimi_results)} succeeded")
        print()

    # Save results JSONL
    os.makedirs(os.path.dirname(RESULTS_JSONL), exist_ok=True)
    with open(RESULTS_JSONL, "w") as f:
        for r in kimi_results + minimax_results:
            f.write(json.dumps(asdict(r)) + "\n")
    print(f"Results JSONL: {RESULTS_JSONL}")

    # Save final manifest
    save_manifest(kimi_tasks, minimax_tasks, kimi_results, minimax_results)

    # Generate report
    generate_report(kimi_results, minimax_results)

    # Final summary
    all_results = kimi_results + minimax_results
    total_success = sum(1 for r in all_results if r.status == "success")
    total_cost = sum(r.cost_usd for r in all_results)

    print()
    print("=" * 80)
    print("SWARM EXECUTION COMPLETE")
    print("=" * 80)
    print(f"Total Agents: {len(all_results)}")
    print(f"Successes: {total_success} ({(total_success/len(all_results)*100):.1f}%)")
    print(f"Failures: {len(all_results) - total_success}")
    print(f"Total Cost: ${total_cost:.4f}")
    print(f"Report: {REPORT_FILE}")
    print(f"Manifest: {MANIFEST_FILE}")
    print("=" * 80)


if __name__ == "__main__":
    asyncio.run(main())
