version: "3.8"

# Sofiia Supervisor — NODA2 deployment
#
# Usage:
#   docker compose -f docker-compose.node2.yml \
#                  -f docker-compose.node2-sofiia-supervisor.yml up -d
#
# Or standalone (requires router already running on dagi-network-node2):
#   docker compose -f docker-compose.node2-sofiia-supervisor.yml up -d

services:

  sofiia-supervisor:
    build:
      context: ./services/sofiia-supervisor
      dockerfile: Dockerfile
    container_name: sofiia-supervisor
    image: daarion/sofiia-supervisor:latest
    ports:
      - "8084:8080"
    environment:
      # Router is the gateway — all tool calls go here
      - GATEWAY_BASE_URL=http://router:8000
      # Set this to restrict access to /v1/tools/execute on the router side
      - SUPERVISOR_API_KEY=${SUPERVISOR_API_KEY:-}
      # Protect the supervisor HTTP API from outside
      - SUPERVISOR_INTERNAL_KEY=${SUPERVISOR_INTERNAL_KEY:-}
      # State backend
      - SUPERVISOR_STATE_BACKEND=redis
      - REDIS_URL=redis://sofiia-redis:6379/0
      - RUN_TTL_SEC=86400
      # Agent identity
      - DEFAULT_AGENT_ID=sofiia
      - DEFAULT_WORKSPACE_ID=${DEFAULT_WORKSPACE_ID:-daarion}
      - DEFAULT_TIMEZONE=Europe/Kiev
      # Timeouts
      - TOOL_CALL_TIMEOUT_SEC=60
      - TOOL_CALL_MAX_RETRIES=2
      - JOB_POLL_INTERVAL_SEC=3
      - JOB_MAX_WAIT_SEC=300
    depends_on:
      - sofiia-redis
    networks:
      - dagi-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-sf", "http://localhost:8080/healthz"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s
    volumes:
      - ./logs:/app/logs

  sofiia-redis:
    image: redis:7.4-alpine
    container_name: sofiia-redis
    command: redis-server --save 60 1 --loglevel warning --maxmemory 256mb --maxmemory-policy allkeys-lru
    ports:
      - "6380:6379"          # Expose on 6380 to avoid conflict with existing Redis
    volumes:
      - sofiia-redis-data:/data
    networks:
      - dagi-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 15s
      timeout: 5s
      retries: 3

networks:
  # Reuse the existing NODA2 network so supervisor can reach router
  dagi-network:
    external: true
    name: dagi-network-node2

volumes:
  sofiia-redis-data:
    driver: local
