Spaces:
Running
Running
opencode commited on
Commit ·
c822bb0
1
Parent(s): 439d7d7
fix: statement_timeout 5s nas queries PG + timing granular no gap pós-EMOTION UPDATE
Browse files- modules/api.py +7 -0
- modules/database_pg.py +7 -0
modules/api.py
CHANGED
|
@@ -2542,9 +2542,12 @@ class BelmiraAPI:
|
|
| 2542 |
pass
|
| 2543 |
|
| 2544 |
# 🔧 UNIFIED CONTEXT: Build complete context including STM and Reply Context
|
|
|
|
|
|
|
| 2545 |
unified_context = None
|
| 2546 |
if getattr(self, 'unified_builder', None) and conversation_id:
|
| 2547 |
try:
|
|
|
|
| 2548 |
reply_metadata_robust: Dict[str, Any] = dict(reply_metadata) if reply_metadata else {}
|
| 2549 |
if is_reply:
|
| 2550 |
reply_metadata_robust.update({
|
|
@@ -2598,6 +2601,8 @@ class BelmiraAPI:
|
|
| 2598 |
pass
|
| 2599 |
except Exception as e:
|
| 2600 |
self.logger.warning(f"Error building unified context: {e}")
|
|
|
|
|
|
|
| 2601 |
|
| 2602 |
web_content = ""
|
| 2603 |
# 🛡️ ANTI-HALLUCINATION: Não pesquisar se o remetente é um bot conhecido
|
|
@@ -2641,6 +2646,8 @@ class BelmiraAPI:
|
|
| 2641 |
dossie=dossie,
|
| 2642 |
conversation_id=conversation_id
|
| 2643 |
)
|
|
|
|
|
|
|
| 2644 |
|
| 2645 |
# ✅ PREPARAR CONTEXTO LSTM PARA THINKING ENGINE
|
| 2646 |
# unified_context é um dataclass (não dict), por isso buscamos
|
|
|
|
| 2542 |
pass
|
| 2543 |
|
| 2544 |
# 🔧 UNIFIED CONTEXT: Build complete context including STM and Reply Context
|
| 2545 |
+
import time as _tStep
|
| 2546 |
+
_step_t0 = _tStep.time()
|
| 2547 |
unified_context = None
|
| 2548 |
if getattr(self, 'unified_builder', None) and conversation_id:
|
| 2549 |
try:
|
| 2550 |
+
_step_t0b = _tStep.time()
|
| 2551 |
reply_metadata_robust: Dict[str, Any] = dict(reply_metadata) if reply_metadata else {}
|
| 2552 |
if is_reply:
|
| 2553 |
reply_metadata_robust.update({
|
|
|
|
| 2601 |
pass
|
| 2602 |
except Exception as e:
|
| 2603 |
self.logger.warning(f"Error building unified context: {e}")
|
| 2604 |
+
_step_t1 = _tStep.time()
|
| 2605 |
+
self.logger.info(f"⏱️ [STEP-TIMING] build_unified_context: {_step_t1-_step_t0:.2f}s")
|
| 2606 |
|
| 2607 |
web_content = ""
|
| 2608 |
# 🛡️ ANTI-HALLUCINATION: Não pesquisar se o remetente é um bot conhecido
|
|
|
|
| 2646 |
dossie=dossie,
|
| 2647 |
conversation_id=conversation_id
|
| 2648 |
)
|
| 2649 |
+
_step_t2 = _tStep.time()
|
| 2650 |
+
self.logger.info(f"⏱️ [STEP-TIMING] _build_prompt: {_step_t2-_step_t1:.2f}s")
|
| 2651 |
|
| 2652 |
# ✅ PREPARAR CONTEXTO LSTM PARA THINKING ENGINE
|
| 2653 |
# unified_context é um dataclass (não dict), por isso buscamos
|
modules/database_pg.py
CHANGED
|
@@ -165,6 +165,13 @@ class DatabasePG:
|
|
| 165 |
else:
|
| 166 |
conn = psycopg2.connect(**params, cursor_factory=psycopg2.extras.RealDictCursor, connect_timeout=10, **keepalive_opts)
|
| 167 |
conn.autocommit = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
# Success: reset circuit breaker
|
| 170 |
DatabasePG._circuit_breaker_failures = 0
|
|
|
|
| 165 |
else:
|
| 166 |
conn = psycopg2.connect(**params, cursor_factory=psycopg2.extras.RealDictCursor, connect_timeout=10, **keepalive_opts)
|
| 167 |
conn.autocommit = False
|
| 168 |
+
# ⏱️ Statement timeout: evita queries que travam o event loop inteiro
|
| 169 |
+
try:
|
| 170 |
+
cur = conn.cursor()
|
| 171 |
+
cur.execute("SET statement_timeout = '5000'")
|
| 172 |
+
conn.commit()
|
| 173 |
+
except Exception:
|
| 174 |
+
pass
|
| 175 |
|
| 176 |
# Success: reset circuit breaker
|
| 177 |
DatabasePG._circuit_breaker_failures = 0
|