# Policy for the unified simulation enforcer.
# This file is used by `bin/ytops-client policy-enforcer --live` to manage
# both the authentication and download simulation environments from a single process.

# Policy for the unified simulation enforcer.
# This file is used by `bin/ytops-client policy-enforcer --live` to manage
# both the authentication and download simulation environments from a single process.

simulation_parameters:
  # --- Common Redis settings for all tools ---
  # The enforcer will connect to two different Redis environments (key prefixes)
  # based on these settings, applying the corresponding policies to each.
  env_file: ".env"
  auth_env: "sim_auth"
  download_env: "sim_download"
  
  # How often the enforcer should wake up and apply all policies.
  interval_seconds: 2

# --- Policies for the Authentication Simulation ---
auth_policy_enforcer_config:
  # Ban if 2 failures occur within a 1-minute window.
  #ban_on_failures: 2
  #ban_on_failures_window_minutes: 1
  
  # The standard rest policy is disabled, as rotation is handled by the profile group.
  profile_prefix: "user1"
  
  # New rate limit policy to enforce requests-per-hour limits.
  # For guest sessions, the limit is ~300 videos/hour.
  rate_limit_requests: 280
  rate_limit_window_minutes: 60
  rate_limit_rest_duration_minutes: 5
                        
  rest_after_requests: 0
  rest_duration_minutes: 10

  # NOTE on Rate Limits: With the default yt-dlp settings, the rate limit for guest
  # sessions is ~300 videos/hour (~1000 webpage/player requests per hour).
  # For accounts, it is ~2000 videos/hour (~4000 webpage/player requests per hour).
  # The settings below should be configured to respect these limits.
  
  # A group of profiles that are managed together.
  # The enforcer will ensure that no more than `max_active_profiles` from this
  # group are in the ACTIVE state at any time.
  profile_groups:
    - name: "exclusive_auth_profiles"
      prefix: "user1"
      # Enforce that only 1 profile from this group can be active at a time.
      max_active_profiles: 1
      # After an active profile has been used for this many requests, it will be
      # rotated out and put into a RESTING state.
      rotate_after_requests: 25
      # How long a profile rests after being rotated out.
      rest_duration_minutes_on_rotation: 1

      # If true, no new profile in this group will be activated while another
      # one is in the 'waiting_downloads' state.
      defer_activation_if_any_waiting: true

      # --- New settings for download wait feature ---
      # When a profile is rotated, wait for its generated downloads to finish
      # before it can be used again.
      wait_download_finish_per_profile: true
      # Safety net: max time to wait for downloads before forcing rotation.
      # Should be aligned with info.json URL validity (e.g., 4 hours = 240 mins).
      max_wait_for_downloads_minutes: 240

  # Time-based proxy rules are disabled as they are not needed for this setup.
  proxy_work_minutes: 0
  proxy_rest_duration_minutes: 0

  # Global maximum time a proxy can be active before being rested, regardless of
  # other rules. Acts as a safety net. Set to 0 to disable.
  max_global_proxy_active_minutes: 0
  rest_duration_on_max_active: 10

  # Proxy-level ban on failure burst is disabled.
  proxy_ban_on_failures: 0
  proxy_ban_window_minutes: 2

  # Clean up locks held for more than 16 minutes (960s) to prevent stuck workers.
  # This should be longer than the docker container timeout (15m).
  unlock_stale_locks_after_seconds: 960

  # A short post-task cooldown for auth simulation profiles. When a batch is finished,
  # the profile is put into COOLDOWN briefly. This prevents a worker from immediately
  # re-locking the same profile, giving the policy enforcer a window to perform rotation.
  unlock_cooldown_seconds: 1

# Cross-simulation synchronization
cross_simulation_sync:
  # Link auth profiles to download profiles (by name)
  # Both profiles should exist in their respective environments
  profile_links:
    - auth: "user1"
      download: "user1"
  # Which states to synchronize
  #sync_states:
    # - "RESTING"  # Disabling to prevent deadlock when auth profile is waiting for downloads.
                   # The download profile must remain active to process them.
     #  - "BANNED"
  # Whether to sync rotation (when auth is rotated due to rotate_after_requests)
  #sync_rotation: true
  # Whether download profile should be banned if auth is banned (even if download hasn't violated its own rules)
  #enforce_auth_lead: true
  # Ensures the same profile (e.g., user1_0) is active in both simulations.
  # This will activate the correct download profile and rest any others in its group.
  sync_active_profile: true
  # When an auth profile is waiting for downloads, ensure the matching download profile is active
  sync_waiting_downloads: true

# --- Policies for the Download Simulation ---
download_policy_enforcer_config:
  # Ban if 1 failure occurs within a 1-minute window.
  ban_on_failures: 1
  ban_on_failures_window_minutes: 1

  # Standard rest policy is disabled in favor of group rotation.
  profile_prefix: "user1"
  
  # New rate limit policy to enforce requests-per-hour limits.
  # For guest sessions, the limit is ~300 videos/hour. We set it slightly lower to be safe.
  rate_limit_requests: 280
  rate_limit_window_minutes: 60
  rate_limit_rest_duration_minutes: 5
  #
  rest_after_requests: 0
  rest_duration_minutes: 20

  # NOTE on Rate Limits: With the default yt-dlp settings, the rate limit for guest
  # sessions is ~300 videos/hour (~1000 webpage/player requests per hour).
  # For accounts, it is ~2000 videos/hour (~4000 webpage/player requests per hour).
  # The settings below should be configured to respect these limits.

  # A group of profiles that are mutually exclusive. Only one will be active at a time.
  profile_groups:
    - name: "exclusive_download_profiles"
      prefix: "user1"
      rotate_after_requests: 25
      rest_duration_minutes_on_rotation: 1
      max_active_profiles: 1

  # Time-based proxy rules are disabled.
  proxy_work_minutes: 50
  proxy_rest_duration_minutes: 10
  
  # Global maximum time a proxy can be active before being rested, regardless of
  # other rules. Acts as a safety net. Set to 0 to disable.
  max_global_proxy_active_minutes: 0
  rest_duration_on_max_active: 10

  # Proxy-level ban on failure burst is disabled.
  proxy_ban_on_failures: 3
  proxy_ban_window_minutes: 1

  # Clean up download locks held for more than 16 minutes (960s) to allow for long downloads.
  # This should be longer than the docker container timeout (15m).
  unlock_stale_locks_after_seconds: 960

  # After a profile is used for a download, unlock it but put it in COOLDOWN
  # state for 12-16s. This is enforced by the worker, which reads this config from Redis.
  unlock_cooldown_seconds: [2, 3]
