Baida07 commited on
Commit
8a58142
·
1 Parent(s): 4e1d0f4

sync: 194 file da Baida98/AI@d7f0f6a2 (2026-08-30 07:28 UTC) (#150)

Browse files

- sync: 194 file da Baida98/AI@d7f0f6a2 (2026-08-30 07:28 UTC) (8dde3786510cacdc19504dcffb07c60ac7c8fe75)

api/public_snapshot.py CHANGED
@@ -8,9 +8,49 @@ from __future__ import annotations
8
 
9
  import asyncio
10
  import logging
 
11
  from typing import Any
12
 
13
- from .state import _agent_tasks, _loop_registry, sb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  from .version import RUNTIME_VERSION
15
 
16
  _logger = logging.getLogger("agente_ai.public_snapshot")
@@ -37,7 +77,7 @@ async def write_public_dashboard_snapshot() -> bool:
37
  public status route remains available with its degraded response while the
38
  service continues booting; the exact exception is kept in backend logs.
39
  """
40
- client = sb()
41
  if client is None:
42
  _logger.warning("BOOT: public snapshot skipped — Supabase client unavailable")
43
  return False
 
8
 
9
  import asyncio
10
  import logging
11
+ import os
12
  from typing import Any
13
 
14
+ from .state import _agent_tasks, _loop_registry
15
+
16
+ try:
17
+ from supabase import create_client
18
+ except ImportError: # pragma: no cover - deployment dependency guard
19
+ create_client = None
20
+
21
+ _snapshot_client: Any | None = None
22
+ _snapshot_client_initialized = False
23
+
24
+
25
+ def get_snapshot_client() -> Any | None:
26
+ """Return one stable Supabase client for the canonical snapshot project.
27
+
28
+ Unlike the general round-robin pool, this client never changes project
29
+ between a write and a read. Dedicated variables can be used when the
30
+ snapshot lives in a separate project; otherwise project A is canonical.
31
+ """
32
+ global _snapshot_client, _snapshot_client_initialized
33
+ if _snapshot_client_initialized:
34
+ return _snapshot_client
35
+ _snapshot_client_initialized = True
36
+ if create_client is None:
37
+ _logger.warning("BOOT: public snapshot skipped — supabase package unavailable")
38
+ return None
39
+ url = os.getenv("PUBLIC_SNAPSHOT_SUPABASE_URL") or os.getenv("SUPABASE_URL")
40
+ key = (
41
+ os.getenv("PUBLIC_SNAPSHOT_SUPABASE_SERVICE_ROLE_KEY")
42
+ or os.getenv("SUPABASE_SERVICE_ROLE_KEY")
43
+ or os.getenv("SUPABASE_KEY")
44
+ )
45
+ if not url or not key:
46
+ _logger.warning("BOOT: public snapshot skipped — canonical Supabase URL/key unavailable")
47
+ return None
48
+ try:
49
+ _snapshot_client = create_client(url, key)
50
+ _logger.info("BOOT: canonical Supabase snapshot client initialized")
51
+ except Exception as exc:
52
+ _logger.warning("BOOT: canonical Supabase snapshot client init failed type=%s", type(exc).__name__)
53
+ return _snapshot_client
54
  from .version import RUNTIME_VERSION
55
 
56
  _logger = logging.getLogger("agente_ai.public_snapshot")
 
77
  public status route remains available with its degraded response while the
78
  service continues booting; the exact exception is kept in backend logs.
79
  """
80
+ client = get_snapshot_client()
81
  if client is None:
82
  _logger.warning("BOOT: public snapshot skipped — Supabase client unavailable")
83
  return False
api/public_snapshot_diagnostics.py CHANGED
@@ -12,7 +12,7 @@ from typing import Any, Optional
12
  from fastapi import APIRouter, Depends
13
 
14
  from .auth_guard import AuthRole, require_role
15
- from .state import sb
16
 
17
  router = APIRouter(
18
  prefix="/api/diagnostics/public-snapshot",
@@ -40,7 +40,7 @@ def _safe_error(exc: Exception) -> dict[str, Optional[str]]:
40
  @router.get("")
41
  async def public_snapshot_diagnostics() -> dict[str, Any]:
42
  """Return snapshot presence and a sanitized exact Supabase error, if any."""
43
- client = sb()
44
  if client is None:
45
  return {
46
  "ok": False,
 
12
  from fastapi import APIRouter, Depends
13
 
14
  from .auth_guard import AuthRole, require_role
15
+ from .public_snapshot import get_snapshot_client
16
 
17
  router = APIRouter(
18
  prefix="/api/diagnostics/public-snapshot",
 
40
  @router.get("")
41
  async def public_snapshot_diagnostics() -> dict[str, Any]:
42
  """Return snapshot presence and a sanitized exact Supabase error, if any."""
43
+ client = get_snapshot_client()
44
  if client is None:
45
  return {
46
  "ok": False,