# AgentWatch — Enterprise LLM Gateway
#
#   cp .env.example .env      # then set JWT_SECRET, SUPER_ADMIN_PASSWORD and the
#                             # three encryption keys (see the comments there)
#   docker compose up -d
#
# Minimum host: 4 vCPU, 16 GB RAM, 20 GB SSD, Docker 20.10+ with Compose v2.
# The image is in a PRIVATE registry: run `docker login` with the Docker Hub
# access token Iterate.ai issued to you before the first `up`.
# Full guide: https://docs.iterate.ai/agentwatch/platform/quick-start/
#
# This file is meant to be used UNMODIFIED — every setting lives in .env.

services:
  app:
    image: iterateai/agentwatch:${AGENTWATCH_IMAGE_TAG:-latest}
    container_name: agentwatch
    restart: unless-stopped
    pull_policy: missing

    security_opt:
      - no-new-privileges:true
    cap_drop:
      - ALL
    cap_add:
      - NET_BIND_SERVICE
    tmpfs:
      - /tmp:size=500M,mode=1777,exec

    ports:
      - "8787:8787"

    # Load .env into the container so ALL keys set there take effect (not only
    # the ${...}-referenced ones). Explicit environment: entries still win, so
    # the credential lines below are written as ${VAR:?...} to be driven from
    # .env — this file can be used UNMODIFIED; put your values in .env.
    env_file:
      - .env

    environment:
      - NODE_ENV=production
      - PORT=8787
      # Defaults to the SQLite volume. For PostgreSQL, set
      # DATABASE_URL=postgresql://USER:PASS@HOST:5432/DB in your .env (see
      # .env.example) — no need to edit this file.
      - DATABASE_URL=${DATABASE_URL:-file:/app/data/agentwatch.db}

      # ── REQUIRED: set these in .env (no defaults shipped) ──────────────
      - JWT_SECRET=${JWT_SECRET:?set a strong JWT_SECRET in .env — e.g. openssl rand -hex 32}
      - SUPER_ADMIN_EMAIL=${SUPER_ADMIN_EMAIL:-admin@yourcompany.com}
      - SUPER_ADMIN_PASSWORD=${SUPER_ADMIN_PASSWORD:?set SUPER_ADMIN_PASSWORD in .env}

      # ── STRONGLY RECOMMENDED: encryption at rest (openssl rand -hex 64) ──
      # Unset → PUBLIC built-in defaults (encrypted but not confidential). No
      # re-encryption on change — set BEFORE first boot. See .env.example.
      - ENCRYPTION_KEY=${ENCRYPTION_KEY:-}          # provider keys / tenant secrets
      - CHAT_ENCRYPTION_KEY=${CHAT_ENCRYPTION_KEY:-} # per-conversation chat files

      # ── Routing ──────────────────────────────────────────────────
      - ENABLE_PATH_PREFIX_ROUTING=true
      # Sub-path deploy: uncomment + set to match your reverse-proxy path
      # - BASE_PATH=/agentwatch

      # ── Event pipeline (Kafka off by default) ────────────────────
      - EVENT_PIPELINE_KAFKA_ENABLED=false
      - EVENT_PIPELINE_FILE_ARCHIVER_ENABLED=true
      # Archived bodies are AES-256 encrypted at rest. SET a strong key BEFORE first
      # boot (openssl rand -hex 64); unset falls back to a PUBLIC built-in default and
      # there is no re-encryption on change. See .env.example.
      - BODY_ARCHIVE_KEY_HEX=${BODY_ARCHIVE_KEY_HEX:-}
      - KAFKA_BROKERS=localhost:9092

      # ── Tuning (safe defaults; leave as-is) ──────────────────────
      - AGENTWATCH_FETCH_TIMEOUT_MS=120000   # LLM-sized upstream timeout
      - AGENTWATCH_SQLITE_INTEGRITY=skip     # fastest cold start

    volumes:
      - agentwatch-logs:/app/logs:rw
      - agentwatch-data:/app/data:rw

    networks:
      - agentwatch-net

    healthcheck:
      test: ["CMD-SHELL", "curl -fs \"http://localhost:8787${BASE_PATH:-}/health\" || exit 1"]
      interval: 30s
      timeout: 10s
      start_period: 60s
      retries: 3

    deploy:
      resources:
        limits:
          # 4 = the minimum supported host. Screening is CPU-bound, so a lower
          # cap leaves the host's cores idle. Override with AGENTWATCH_CPUS.
          cpus: '${AGENTWATCH_CPUS:-4}'
          # 8G → 12G (2.0.388, measured 2026-10-22 on a 16 GB / no-swap host):
          # the full verification suite peaked at 8.95 GiB, so an 8G cap OOM-
          # killed the gateway (very large prompts with Advanced NER). 12G leaves
          # ~3 GiB headroom and ~4 GB for the OS on a 16 GB host — the minimum
          # supported size. Override with AGENTWATCH_MEMORY_LIMIT on bigger hosts.
          memory: ${AGENTWATCH_MEMORY_LIMIT:-12G}
        reservations:
          cpus: '0.5'
          memory: 512M

networks:
  agentwatch-net:
    name: agentwatch-net

volumes:
  agentwatch-logs:
    name: agentwatch-logs
  agentwatch-data:
    name: agentwatch-data
