Mirrowel commited on
Commit
eb3864b
Β·
1 Parent(s): 5a03c26

debugging pass to try to unfuck deployment

Browse files
src/proxy_app/main.py CHANGED
@@ -42,17 +42,31 @@ _start_time = time.time()
42
  from dotenv import load_dotenv
43
  from glob import glob
44
 
 
 
 
 
45
  # Load main .env first
 
 
 
46
  load_dotenv()
47
 
48
  # Load any additional .env files (e.g., antigravity_all_combined.env, gemini_cli_all_combined.env)
49
  _root_dir = Path.cwd()
 
 
 
 
 
50
  for _env_file in sorted(_root_dir.glob("*.env")):
51
  if _env_file.name != ".env": # Skip main .env (already loaded)
 
52
  load_dotenv(_env_file, override=False) # Don't override existing values
53
 
54
  # Get proxy API key for display
55
  proxy_api_key = os.getenv("PROXY_API_KEY")
 
56
  if proxy_api_key:
57
  key_display = f"βœ“ {proxy_api_key}"
58
  else:
@@ -288,12 +302,16 @@ PROXY_API_KEY = os.getenv("PROXY_API_KEY")
288
 
289
  # Discover API keys from environment variables
290
  api_keys = {}
 
291
  for key, value in os.environ.items():
292
  if "_API_KEY" in key and key != "PROXY_API_KEY":
293
  provider = key.split("_API_KEY")[0].lower()
294
  if provider not in api_keys:
295
  api_keys[provider] = []
296
  api_keys[provider].append(value)
 
 
 
297
 
298
  # Load model ignore lists from environment variables
299
  ignore_models = {}
@@ -337,8 +355,15 @@ async def lifespan(app: FastAPI):
337
 
338
  # The CredentialManager now handles all discovery, including .env overrides.
339
  # We pass all environment variables to it for this purpose.
 
 
340
  cred_manager = CredentialManager(os.environ)
341
  oauth_credentials = cred_manager.discover_and_prepare()
 
 
 
 
 
342
 
343
  if not skip_oauth_init and oauth_credentials:
344
  logging.info("Starting OAuth credential validation and deduplication...")
@@ -482,6 +507,9 @@ async def lifespan(app: FastAPI):
482
  }
483
 
484
  # The client now uses the root logger configuration
 
 
 
485
  client = RotatingClient(
486
  api_keys=api_keys,
487
  oauth_credentials=oauth_credentials, # Pass OAuth config
@@ -492,6 +520,9 @@ async def lifespan(app: FastAPI):
492
  enable_request_logging=ENABLE_REQUEST_LOGGING,
493
  max_concurrent_requests_per_key=max_concurrent_requests_per_key
494
  )
 
 
 
495
  client.background_refresher.start() # Start the background task
496
  app.state.rotating_client = client
497
 
 
42
  from dotenv import load_dotenv
43
  from glob import glob
44
 
45
+ # [DEBUG-REMOVE] Diagnostic logging for .env loading
46
+ print(f"[DEBUG-REMOVE] Current working directory: {Path.cwd()}")
47
+ print(f"[DEBUG-REMOVE] __file__ location: {Path(__file__).resolve().parent}")
48
+
49
  # Load main .env first
50
+ _main_env_path = Path.cwd() / ".env"
51
+ print(f"[DEBUG-REMOVE] Looking for main .env at: {_main_env_path}")
52
+ print(f"[DEBUG-REMOVE] Main .env exists: {_main_env_path.exists()}")
53
  load_dotenv()
54
 
55
  # Load any additional .env files (e.g., antigravity_all_combined.env, gemini_cli_all_combined.env)
56
  _root_dir = Path.cwd()
57
+ _env_files_found = list(_root_dir.glob("*.env"))
58
+ print(f"[DEBUG-REMOVE] Found {len(_env_files_found)} .env files in {_root_dir}:")
59
+ for _ef in _env_files_found:
60
+ print(f"[DEBUG-REMOVE] - {_ef.name}")
61
+
62
  for _env_file in sorted(_root_dir.glob("*.env")):
63
  if _env_file.name != ".env": # Skip main .env (already loaded)
64
+ print(f"[DEBUG-REMOVE] Loading additional .env file: {_env_file}")
65
  load_dotenv(_env_file, override=False) # Don't override existing values
66
 
67
  # Get proxy API key for display
68
  proxy_api_key = os.getenv("PROXY_API_KEY")
69
+ print(f"[DEBUG-REMOVE] PROXY_API_KEY from environment: {'SET' if proxy_api_key else 'NOT SET'}")
70
  if proxy_api_key:
71
  key_display = f"βœ“ {proxy_api_key}"
72
  else:
 
302
 
303
  # Discover API keys from environment variables
304
  api_keys = {}
305
+ print("[DEBUG-REMOVE] === Discovering API keys from environment ===")
306
  for key, value in os.environ.items():
307
  if "_API_KEY" in key and key != "PROXY_API_KEY":
308
  provider = key.split("_API_KEY")[0].lower()
309
  if provider not in api_keys:
310
  api_keys[provider] = []
311
  api_keys[provider].append(value)
312
+ print(f"[DEBUG-REMOVE] Found API key: {key} for provider '{provider}'")
313
+
314
+ print(f"[DEBUG-REMOVE] Total providers with API keys: {list(api_keys.keys())}")
315
 
316
  # Load model ignore lists from environment variables
317
  ignore_models = {}
 
355
 
356
  # The CredentialManager now handles all discovery, including .env overrides.
357
  # We pass all environment variables to it for this purpose.
358
+ print("[DEBUG-REMOVE] === Creating CredentialManager ===")
359
+ print(f"[DEBUG-REMOVE] Total environment variables: {len(os.environ)}")
360
  cred_manager = CredentialManager(os.environ)
361
  oauth_credentials = cred_manager.discover_and_prepare()
362
+
363
+ print(f"[DEBUG-REMOVE] === OAuth credentials discovered ===")
364
+ print(f"[DEBUG-REMOVE] Providers with OAuth credentials: {list(oauth_credentials.keys())}")
365
+ for provider, paths in oauth_credentials.items():
366
+ print(f"[DEBUG-REMOVE] {provider}: {len(paths)} credential(s) - {paths}")
367
 
368
  if not skip_oauth_init and oauth_credentials:
369
  logging.info("Starting OAuth credential validation and deduplication...")
 
507
  }
508
 
509
  # The client now uses the root logger configuration
510
+ print(f"[DEBUG-REMOVE] === Initializing RotatingClient ===")
511
+ print(f"[DEBUG-REMOVE] API keys providers: {list(api_keys.keys())}")
512
+ print(f"[DEBUG-REMOVE] OAuth providers: {list(oauth_credentials.keys())}")
513
  client = RotatingClient(
514
  api_keys=api_keys,
515
  oauth_credentials=oauth_credentials, # Pass OAuth config
 
520
  enable_request_logging=ENABLE_REQUEST_LOGGING,
521
  max_concurrent_requests_per_key=max_concurrent_requests_per_key
522
  )
523
+ print(f"[DEBUG-REMOVE] RotatingClient.all_credentials keys: {list(client.all_credentials.keys())}")
524
+ for provider, creds in client.all_credentials.items():
525
+ print(f"[DEBUG-REMOVE] {provider}: {len(creds)} credential(s)")
526
  client.background_refresher.start() # Start the background task
527
  app.state.rotating_client = client
528
 
src/rotator_library/credential_manager.py CHANGED
@@ -58,13 +58,25 @@ class CredentialManager:
58
  """
59
  env_credentials: Dict[str, Set[str]] = {}
60
 
 
 
 
 
61
  for provider, env_prefix in ENV_OAUTH_PROVIDERS.items():
62
  found_indices: Set[str] = set()
 
63
 
64
  # Check for numbered credentials (PROVIDER_N_ACCESS_TOKEN pattern)
65
  # Pattern: ANTIGRAVITY_1_ACCESS_TOKEN, ANTIGRAVITY_2_ACCESS_TOKEN, etc.
66
  numbered_pattern = re.compile(rf"^{env_prefix}_(\d+)_ACCESS_TOKEN$")
67
 
 
 
 
 
 
 
 
68
  for key in self.env_vars.keys():
69
  match = numbered_pattern.match(key)
70
  if match:
@@ -73,20 +85,30 @@ class CredentialManager:
73
  refresh_key = f"{env_prefix}_{index}_REFRESH_TOKEN"
74
  if refresh_key in self.env_vars and self.env_vars[refresh_key]:
75
  found_indices.add(index)
 
 
 
76
 
77
  # Check for legacy single credential (PROVIDER_ACCESS_TOKEN pattern)
78
  # Only use this if no numbered credentials exist
79
  if not found_indices:
80
  access_key = f"{env_prefix}_ACCESS_TOKEN"
81
  refresh_key = f"{env_prefix}_REFRESH_TOKEN"
 
82
  if (access_key in self.env_vars and self.env_vars[access_key] and
83
  refresh_key in self.env_vars and self.env_vars[refresh_key]):
84
  # Use "0" as the index for legacy single credential
85
  found_indices.add("0")
 
 
 
86
 
87
  if found_indices:
88
  env_credentials[provider] = found_indices
89
  lib_logger.info(f"Found {len(found_indices)} env-based credential(s) for {provider}")
 
 
 
90
 
91
  # Convert to virtual paths
92
  result: Dict[str, List[str]] = {}
 
58
  """
59
  env_credentials: Dict[str, Set[str]] = {}
60
 
61
+ # [DEBUG-REMOVE] Log all environment variable keys for OAuth providers
62
+ print(f"[DEBUG-REMOVE] === Scanning environment for OAuth credentials ===")
63
+ print(f"[DEBUG-REMOVE] ENV_OAUTH_PROVIDERS: {list(ENV_OAUTH_PROVIDERS.keys())}")
64
+
65
  for provider, env_prefix in ENV_OAUTH_PROVIDERS.items():
66
  found_indices: Set[str] = set()
67
+ print(f"[DEBUG-REMOVE] Scanning for provider '{provider}' with prefix '{env_prefix}'")
68
 
69
  # Check for numbered credentials (PROVIDER_N_ACCESS_TOKEN pattern)
70
  # Pattern: ANTIGRAVITY_1_ACCESS_TOKEN, ANTIGRAVITY_2_ACCESS_TOKEN, etc.
71
  numbered_pattern = re.compile(rf"^{env_prefix}_(\d+)_ACCESS_TOKEN$")
72
 
73
+ # [DEBUG-REMOVE] Show all matching environment variable keys
74
+ matching_keys = [k for k in self.env_vars.keys() if env_prefix in k]
75
+ if matching_keys:
76
+ print(f"[DEBUG-REMOVE] Found {len(matching_keys)} keys with '{env_prefix}': {matching_keys}")
77
+ else:
78
+ print(f"[DEBUG-REMOVE] No keys found with '{env_prefix}' prefix")
79
+
80
  for key in self.env_vars.keys():
81
  match = numbered_pattern.match(key)
82
  if match:
 
85
  refresh_key = f"{env_prefix}_{index}_REFRESH_TOKEN"
86
  if refresh_key in self.env_vars and self.env_vars[refresh_key]:
87
  found_indices.add(index)
88
+ print(f"[DEBUG-REMOVE] βœ“ Found numbered credential {index} for {provider}")
89
+ else:
90
+ print(f"[DEBUG-REMOVE] βœ— Missing REFRESH_TOKEN for {provider} credential {index}")
91
 
92
  # Check for legacy single credential (PROVIDER_ACCESS_TOKEN pattern)
93
  # Only use this if no numbered credentials exist
94
  if not found_indices:
95
  access_key = f"{env_prefix}_ACCESS_TOKEN"
96
  refresh_key = f"{env_prefix}_REFRESH_TOKEN"
97
+ print(f"[DEBUG-REMOVE] Checking legacy format: {access_key}, {refresh_key}")
98
  if (access_key in self.env_vars and self.env_vars[access_key] and
99
  refresh_key in self.env_vars and self.env_vars[refresh_key]):
100
  # Use "0" as the index for legacy single credential
101
  found_indices.add("0")
102
+ print(f"[DEBUG-REMOVE] βœ“ Found legacy single credential for {provider}")
103
+ else:
104
+ print(f"[DEBUG-REMOVE] βœ— No legacy credential found for {provider}")
105
 
106
  if found_indices:
107
  env_credentials[provider] = found_indices
108
  lib_logger.info(f"Found {len(found_indices)} env-based credential(s) for {provider}")
109
+ print(f"[DEBUG-REMOVE] RESULT: {len(found_indices)} credential(s) registered for {provider}")
110
+ else:
111
+ print(f"[DEBUG-REMOVE] RESULT: No credentials found for {provider}")
112
 
113
  # Convert to virtual paths
114
  result: Dict[str, List[str]] = {}