# Molecule AI Worker Team — Gemini-powered (cost-optimized, full parity with molecule-dev)
# Uses DeepAgents runtime with Google Gemini 3.1 Pro Preview.
# DeepAgents adds: task planning, filesystem, sub-agents, shell execution.
# ~20x cheaper than Claude Opus, suitable for daily operations.
#
# Agent hierarchy, schedules, channels, and per-agent initial prompts are
# kept in sync with molecule-dev. System prompts are runtime-agnostic and
# shared between both orgs (per-workspace files_dir).

name: Molecule AI Worker Team (Gemini)
description: Cost-optimized AI agent team using DeepAgents + Gemini — mirrors molecule-dev's capabilities

defaults:
  runtime: deepagents
  tier: 2
  required_env:
    - GOOGLE_API_KEY
  # Gemini 2.5 Pro (stable). We tried gemini-3.1-pro-preview but its
  # 25 req/min quota is too tight for a 11-workspace org that fans out
  # delegations (PM → Dev Lead → 6 engineers in parallel ≈ 30+ calls
  # in a wave). Stable tier has a much higher ceiling.
  model: google_genai:gemini-2.5-pro

  # IMPORTANT: initial_prompt must NOT send A2A messages — other agents may
  # not be up yet. Keep local: clone, read, memorize. Wait for tasks.
  initial_prompt: |
    You just started. Set up your environment silently — do NOT contact other agents yet.
    1. Clone the repo: git clone https://github.com/${GITHUB_REPO}.git /workspace/repo 2>/dev/null || (cd /workspace/repo && git pull)
    2. Set up git hooks: cd /workspace/repo && git config core.hooksPath .githooks
    3. Read /workspace/repo/CLAUDE.md to understand the project
    4. Read your system prompt at /configs/system-prompt.md to understand your role
    5. Save key conventions to memory so you recall them on every future task:
       Use commit_memory to save: "CONVENTIONS: (1) Every canvas .tsx using hooks needs 'use client' as first line — run the grep check before committing. (2) Dark zinc theme only — never white/light. (3) Zustand selectors must not create new objects. (4) Always run npm test + npm run build before reporting done. (5) Use delegate_task to ask peers questions directly — don't guess API shapes. (6) Pre-commit hook at .githooks/pre-commit enforces these — commits will be rejected if violated."
    6. You are now ready. Wait for tasks from your parent — do not initiate contact.

workspaces:
  - name: PM
    role: Project Manager — coordinates Research and Dev teams
    tier: 3
    files_dir: pm
    workspace_dir: /Users/hongming/Documents/GitHub/molecule-monorepo
    canvas: { x: 400, y: 50 }
    # Auto-link Telegram so the user can talk to PM directly from Telegram.
    # Bot token + chat ID come from pm/.env (TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID).
    channels:
      - type: telegram
        config:
          bot_token: ${TELEGRAM_BOT_TOKEN}
          chat_id: ${TELEGRAM_CHAT_ID}
        enabled: true
    initial_prompt: |
      You just started as PM. Set up silently — do NOT contact agents yet.
      1. The repo is already mounted at /workspace — no need to clone
      2. Read /workspace/CLAUDE.md to understand the project
      3. Read your system prompt at /configs/system-prompt.md
      4. Run: git log --oneline -5 in /workspace to see recent changes
      5. Use commit_memory to save a brief summary of recent changes
      6. You are now ready. Wait for the CEO to give you tasks.
    children:
      - name: Research Lead
        role: Market analysis and technical research
        files_dir: research-lead
        canvas: { x: 200, y: 250 }
        initial_prompt: |
          You just started as Research Lead. Set up silently — do NOT contact other agents.
          1. Clone the repo: git clone https://github.com/${GITHUB_REPO}.git /workspace/repo 2>/dev/null || (cd /workspace/repo && git pull)
          2. Read /workspace/repo/CLAUDE.md
          3. Read /configs/system-prompt.md
          4. Read /workspace/repo/docs/product/overview.md to understand the product
          5. Use commit_memory to save key product facts for later recall
          6. Wait for tasks from PM.
        children:
          - name: Market Analyst
            role: Market sizing, trends, user research
            files_dir: market-analyst
          - name: Technical Researcher
            role: AI frameworks and protocol evaluation
            files_dir: technical-researcher
          - name: Competitive Intelligence
            role: Competitor tracking and feature comparison
            files_dir: competitive-intelligence

      - name: Dev Lead
        role: Engineering planning and team coordination
        tier: 3
        files_dir: dev-lead
        canvas: { x: 650, y: 250 }
        initial_prompt: |
          You just started as Dev Lead. Set up silently — do NOT contact other agents.
          1. Clone the repo: git clone https://github.com/${GITHUB_REPO}.git /workspace/repo 2>/dev/null || (cd /workspace/repo && git pull)
          2. Read /workspace/repo/CLAUDE.md — full architecture, build commands, test commands
          3. Read /configs/system-prompt.md
          4. Run: cd /workspace/repo && git log --oneline -5
          5. Use commit_memory to save the architecture summary and recent changes
          6. Wait for tasks from PM.
        children:
          - name: Frontend Engineer
            role: Next.js canvas, React Flow, Zustand
            tier: 3
            files_dir: frontend-engineer
            initial_prompt: |
              You just started as Frontend Engineer. Set up silently — do NOT contact other agents.
              1. Clone the repo: git clone https://github.com/${GITHUB_REPO}.git /workspace/repo 2>/dev/null || (cd /workspace/repo && git pull)
              2. Read /workspace/repo/CLAUDE.md — focus on Canvas section
              3. Read /configs/system-prompt.md
              4. Study existing code — read these files to understand patterns:
                 - /workspace/repo/canvas/src/components/Toolbar.tsx (dark zinc theme, component style)
                 - /workspace/repo/canvas/src/components/WorkspaceNode.tsx (node rendering)
                 - /workspace/repo/canvas/src/store/canvas.ts (Zustand store patterns)
              5. Use commit_memory to save the design system: zinc-900/950 bg, zinc-300/400 text, blue-500/600 accents
              6. Wait for tasks from Dev Lead.
          - name: Backend Engineer
            role: Go platform, Postgres, Redis, A2A
            tier: 3
            files_dir: backend-engineer
            initial_prompt: |
              You just started as Backend Engineer. Set up silently — do NOT contact other agents.
              1. Clone the repo: git clone https://github.com/${GITHUB_REPO}.git /workspace/repo 2>/dev/null || (cd /workspace/repo && git pull)
              2. Read /workspace/repo/CLAUDE.md — focus on Platform section, API routes, database
              3. Read /configs/system-prompt.md
              4. Study the handler pattern: read /workspace/repo/platform/internal/handlers/workspace.go
              5. Use commit_memory to save the API route table and key patterns
              6. Wait for tasks from Dev Lead.
          - name: DevOps Engineer
            role: CI/CD, Docker, infrastructure
            tier: 3
            files_dir: devops-engineer
            initial_prompt: |
              You just started as DevOps Engineer. Set up silently — do NOT contact other agents.
              1. Clone the repo: git clone https://github.com/${GITHUB_REPO}.git /workspace/repo 2>/dev/null || (cd /workspace/repo && git pull)
              2. Read /workspace/repo/CLAUDE.md — focus on Infrastructure, Docker, CI sections
              3. Read /configs/system-prompt.md
              4. Read /workspace/repo/.github/workflows/ci.yml
              5. Use commit_memory to save CI pipeline structure
              6. Wait for tasks from Dev Lead.
          - name: Security Auditor
            role: Security auditing and vulnerability assessment
            tier: 3
            files_dir: security-auditor
            initial_prompt: |
              You just started as Security Auditor. Set up silently — do NOT contact other agents.
              1. Clone the repo: git clone https://github.com/${GITHUB_REPO}.git /workspace/repo 2>/dev/null || (cd /workspace/repo && git pull)
              2. Read /workspace/repo/CLAUDE.md — focus on security, crypto, access control
              3. Read /configs/system-prompt.md
              4. Read /workspace/repo/platform/internal/crypto/aes.go
              5. Use commit_memory to save security patterns and concerns
              6. Wait for tasks from Dev Lead.
            schedules:
              - name: Security audit (every 12h)
                cron_expr: "0 */12 * * *"
                prompt: |
                  Recurring security audit. Be thorough and incremental.

                  1. Pull latest: cd /workspace/repo && git pull
                  2. Check what you audited last time: use search_memory("security audit") to recall prior findings
                  3. See what changed since last audit: git log --oneline --since="12 hours ago"
                  4. For each changed file, do a full security review:
                     - SQL injection (parameterized queries, not fmt.Sprintf)
                     - Path traversal (any endpoint accepting file paths)
                     - Missing access control (every endpoint must check permissions)
                     - Secrets leaking into logs, errors, or responses
                     - Command injection (shell exec with user input)
                     - XSS (user content rendered in canvas)
                  5. Check for open PRs: cd /workspace/repo && gh pr list --state open
                     Review each open PR for security issues
                  6. Record your findings to memory:
                     Use commit_memory with key "security-audit-latest" and value containing:
                     - Date and commit hash audited up to
                     - Files reviewed
                     - Issues found (or "clean")
                     - Areas that need deeper review next time
                  7. If you find issues, report to Dev Lead via delegate_task with file:line references
                  8. If clean, still record what you checked so next audit covers new ground
                enabled: true
          - name: QA Engineer
            role: Testing, quality assurance, test automation
            tier: 3
            files_dir: qa-engineer
            initial_prompt: |
              You just started as QA Engineer. Set up silently — do NOT contact other agents.
              1. Clone the repo: git clone https://github.com/${GITHUB_REPO}.git /workspace/repo 2>/dev/null || (cd /workspace/repo && git pull)
              2. Read /workspace/repo/CLAUDE.md — focus on ALL test commands and locations
              3. Read /configs/system-prompt.md — your comprehensive QA requirements are there
              4. Use commit_memory to save test suite locations and commands
              5. Wait for tasks from Dev Lead. When asked to test, ALWAYS run tests yourself.
            schedules:
              - name: Code quality audit (every 12h)
                cron_expr: "0 6,18 * * *"
                prompt: |
                  Recurring code quality audit. Be thorough and incremental.

                  1. Pull latest: cd /workspace/repo && git pull
                  2. Check what you audited last time: use search_memory("qa audit") to recall prior findings
                  3. See what changed since last audit: git log --oneline --since="12 hours ago"
                  4. Run ALL test suites and record results:
                     cd /workspace/repo/platform && go test -race ./... 2>&1 | tail -20
                     cd /workspace/repo/canvas && npm test 2>&1 | tail -10
                     cd /workspace/repo/workspace-template && python -m pytest --tb=short -q 2>&1 | tail -10
                  5. Check test coverage on recently changed files:
                     - For each changed Python file, check if it has corresponding tests
                     - For each changed Go handler, check if it has test coverage
                     - For each changed .tsx component, check if it has a .test.tsx
                  6. Review recent PRs for quality issues:
                     cd /workspace/repo && gh pr list --state merged --limit 5
                     For each: check if tests were added, if docs were updated, if 'use client' is present on hook-using .tsx
                  7. Check for regressions:
                     cd /workspace/repo/canvas && npm run build 2>&1 | tail -5
                     Look for TypeScript errors, missing exports, build warnings
                  8. Record your findings to memory:
                     Use commit_memory with key "qa-audit-latest" and value containing:
                     - Date and commit hash audited up to
                     - Test counts (Go, Python, Canvas) and pass/fail status
                     - Files with missing test coverage
                     - Quality issues found
                     - Areas to investigate deeper next time
                  9. If you find issues, report to Dev Lead via delegate_task
                  10. If all clean, still record what was checked so next audit covers new ground
                enabled: true
          - name: UIUX Designer
            role: User flow design, visual design review, interaction patterns, accessibility
            tier: 3
            files_dir: uiux-designer
            initial_prompt: |
              You just started as UIUX Designer. Set up silently — do NOT contact other agents.
              1. Clone the repo: git clone https://github.com/${GITHUB_REPO}.git /workspace/repo 2>/dev/null || (cd /workspace/repo && git pull)
              2. Read /workspace/repo/CLAUDE.md — focus on Canvas section
              3. Read /configs/system-prompt.md
              4. Read these files to understand the visual design:
                 - /workspace/repo/canvas/src/components/Toolbar.tsx
                 - /workspace/repo/canvas/src/components/WorkspaceNode.tsx
                 - /workspace/repo/canvas/src/components/SidePanel.tsx
              5. Use commit_memory to save: dark zinc theme (zinc-900/950 bg, zinc-300/400 text, blue-500/600 accents, border-zinc-700/800)
              6. Wait for tasks from Dev Lead.
