latest refactoring
Some checks failed
validation / verify (push) Failing after 10s

This commit is contained in:
2026-10-06 20:22:37 +01:00
parent e94b2493e7
commit 7ee49ab352
414 changed files with 15768 additions and 3896 deletions

View File

@@ -9,11 +9,11 @@ import json
import logging
import sqlite3
from datetime import datetime, timezone
from typing import Any, Dict, List, Optional
from typing import Any
try:
import psycopg2
from psycopg2.extras import RealDictCursor
HAS_PSYCOPG2 = True
except ImportError:
HAS_PSYCOPG2 = False
@@ -26,10 +26,12 @@ logger = logging.getLogger(__name__)
class DatabaseManager:
"""Handles persistence of sessions, execution runs, artifacts, and review logs."""
def __init__(self, db_url: Optional[str] = None) -> None:
def __init__(self, db_url: str | None = None) -> None:
self.settings = get_settings()
self.db_url = db_url or self.settings.get_database_url()
self.is_postgres = self.db_url.startswith("postgresql://") or self.db_url.startswith("postgres://")
self.is_postgres = self.db_url.startswith(
"postgresql://"
) or self.db_url.startswith("postgres://")
self._init_db()
def _get_connection(self):
@@ -38,7 +40,7 @@ class DatabaseManager:
try:
conn = psycopg2.connect(self.db_url, connect_timeout=3)
return conn
except Exception as exc:
except Exception as exc: # noqa: BLE001 - any connection failure falls back to SQLite
logger.warning(
"PostgreSQL connection to %s failed (%s); falling back to local SQLite database %s",
self.db_url,
@@ -56,7 +58,7 @@ class DatabaseManager:
conn = self._get_connection()
try:
cursor = conn.cursor()
# 1. Sessions Table
cursor.execute("""
CREATE TABLE IF NOT EXISTS adk_sessions (
@@ -125,12 +127,18 @@ class DatabaseManager:
conn.commit()
logger.info("Database schema initialized successfully.")
except Exception as exc:
except Exception as exc: # noqa: BLE001 - persistence is best-effort; the failure is logged
logger.error("Failed to initialize database schema: %s", exc)
finally:
conn.close()
def save_session(self, session_id: str, agent_name: str, state_data: Dict[str, Any], metadata: Optional[Dict[str, Any]] = None) -> bool:
def save_session(
self,
session_id: str,
agent_name: str,
state_data: dict[str, Any],
metadata: dict[str, Any] | None = None,
) -> bool:
"""Persist or update ADK session state in PostgreSQL."""
conn = self._get_connection()
now = datetime.now(timezone.utc).isoformat()
@@ -140,41 +148,51 @@ class DatabaseManager:
meta_json = json.dumps(metadata or {})
if isinstance(conn, sqlite3.Connection):
cursor.execute("""
cursor.execute(
"""
INSERT INTO adk_sessions (session_id, agent_name, created_at, updated_at, state_data, metadata)
VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT(session_id) DO UPDATE SET
updated_at = excluded.updated_at,
state_data = excluded.state_data,
metadata = excluded.metadata
""", (session_id, agent_name, now, now, state_json, meta_json))
""",
(session_id, agent_name, now, now, state_json, meta_json),
)
else:
cursor.execute("""
cursor.execute(
"""
INSERT INTO adk_sessions (session_id, agent_name, created_at, updated_at, state_data, metadata)
VALUES (%s, %s, %s, %s, %s, %s)
ON CONFLICT(session_id) DO UPDATE SET
updated_at = EXCLUDED.updated_at,
state_data = EXCLUDED.state_data,
metadata = EXCLUDED.metadata
""", (session_id, agent_name, now, now, state_json, meta_json))
""",
(session_id, agent_name, now, now, state_json, meta_json),
)
conn.commit()
return True
except Exception as exc:
except Exception as exc: # noqa: BLE001 - persistence is best-effort; the failure is logged
logger.error("Error saving session %s: %s", session_id, exc)
return False
finally:
conn.close()
def get_session(self, session_id: str) -> Optional[Dict[str, Any]]:
def get_session(self, session_id: str) -> dict[str, Any] | None:
"""Retrieve ADK session state by session_id."""
conn = self._get_connection()
try:
cursor = conn.cursor()
if isinstance(conn, sqlite3.Connection):
cursor.execute("SELECT * FROM adk_sessions WHERE session_id = ?", (session_id,))
cursor.execute(
"SELECT * FROM adk_sessions WHERE session_id = ?", (session_id,)
)
else:
cursor.execute("SELECT * FROM adk_sessions WHERE session_id = %s", (session_id,))
cursor.execute(
"SELECT * FROM adk_sessions WHERE session_id = %s", (session_id,)
)
row = cursor.fetchone()
if not row:
@@ -197,7 +215,7 @@ class DatabaseManager:
"state_data": json.loads(row[4]),
"metadata": json.loads(row[5] or "{}"),
}
except Exception as exc:
except Exception as exc: # noqa: BLE001 - persistence is best-effort; the failure is logged
logger.error("Error reading session %s: %s", session_id, exc)
return None
finally:
@@ -211,7 +229,7 @@ class DatabaseManager:
current_phase: str,
loop_count: int,
request_summary: str,
results: Dict[str, Any],
results: dict[str, Any],
) -> bool:
"""Save workflow execution record to database."""
conn = self._get_connection()
@@ -221,7 +239,8 @@ class DatabaseManager:
results_json = json.dumps(results)
if isinstance(conn, sqlite3.Connection):
cursor.execute("""
cursor.execute(
"""
INSERT INTO workflow_executions
(execution_id, session_id, status, current_phase, loop_count, started_at, completed_at, request_summary, results_json)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
@@ -231,9 +250,22 @@ class DatabaseManager:
loop_count = excluded.loop_count,
completed_at = excluded.completed_at,
results_json = excluded.results_json
""", (execution_id, session_id, status, current_phase, loop_count, now, now, request_summary, results_json))
""",
(
execution_id,
session_id,
status,
current_phase,
loop_count,
now,
now,
request_summary,
results_json,
),
)
else:
cursor.execute("""
cursor.execute(
"""
INSERT INTO workflow_executions
(execution_id, session_id, status, current_phase, loop_count, started_at, completed_at, request_summary, results_json)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
@@ -243,42 +275,67 @@ class DatabaseManager:
loop_count = EXCLUDED.loop_count,
completed_at = EXCLUDED.completed_at,
results_json = EXCLUDED.results_json
""", (execution_id, session_id, status, current_phase, loop_count, now, now, request_summary, results_json))
""",
(
execution_id,
session_id,
status,
current_phase,
loop_count,
now,
now,
request_summary,
results_json,
),
)
conn.commit()
return True
except Exception as exc:
except Exception as exc: # noqa: BLE001 - persistence is best-effort; the failure is logged
logger.error("Error saving workflow execution %s: %s", execution_id, exc)
return False
finally:
conn.close()
def save_artifact(self, artifact_id: str, execution_id: str, artifact_type: str, file_path: str, content: str) -> bool:
def save_artifact(
self,
artifact_id: str,
execution_id: str,
artifact_type: str,
file_path: str,
content: str,
) -> bool:
"""Write ADK artifact metadata & content to database."""
conn = self._get_connection()
now = datetime.now(timezone.utc).isoformat()
try:
cursor = conn.cursor()
if isinstance(conn, sqlite3.Connection):
cursor.execute("""
cursor.execute(
"""
INSERT INTO adk_artifacts (artifact_id, execution_id, artifact_type, file_path, content, created_at)
VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT(artifact_id) DO UPDATE SET
content = excluded.content,
file_path = excluded.file_path
""", (artifact_id, execution_id, artifact_type, file_path, content, now))
""",
(artifact_id, execution_id, artifact_type, file_path, content, now),
)
else:
cursor.execute("""
cursor.execute(
"""
INSERT INTO adk_artifacts (artifact_id, execution_id, artifact_type, file_path, content, created_at)
VALUES (%s, %s, %s, %s, %s, %s)
ON CONFLICT(artifact_id) DO UPDATE SET
content = EXCLUDED.content,
file_path = EXCLUDED.file_path
""", (artifact_id, execution_id, artifact_type, file_path, content, now))
""",
(artifact_id, execution_id, artifact_type, file_path, content, now),
)
conn.commit()
return True
except Exception as exc:
except Exception as exc: # noqa: BLE001 - persistence is best-effort; the failure is logged
logger.error("Error saving artifact %s: %s", artifact_id, exc)
return False
finally:
@@ -299,25 +356,56 @@ class DatabaseManager:
try:
cursor = conn.cursor()
if isinstance(conn, sqlite3.Connection):
cursor.execute("""
cursor.execute(
"""
INSERT INTO orchestrator_review_logs (log_id, execution_id, iteration, review_status, reviewer_agent, feedback, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
""", (log_id, execution_id, iteration, review_status, reviewer_agent, feedback, now))
""",
(
log_id,
execution_id,
iteration,
review_status,
reviewer_agent,
feedback,
now,
),
)
else:
cursor.execute("""
cursor.execute(
"""
INSERT INTO orchestrator_review_logs (log_id, execution_id, iteration, review_status, reviewer_agent, feedback, created_at)
VALUES (%s, %s, %s, %s, %s, %s, %s)
""", (log_id, execution_id, iteration, review_status, reviewer_agent, feedback, now))
""",
(
log_id,
execution_id,
iteration,
review_status,
reviewer_agent,
feedback,
now,
),
)
conn.commit()
return True
except Exception as exc:
except Exception as exc: # noqa: BLE001 - persistence is best-effort; the failure is logged
logger.error("Error recording orchestrator review log %s: %s", log_id, exc)
return False
finally:
conn.close()
def save_evaluation(self, eval_id: str, case_id: str, total_score: float, max_score: float, pass_rate: float, passed: bool, details: Dict[str, Any]) -> bool:
def save_evaluation(
self,
eval_id: str,
case_id: str,
total_score: float,
max_score: float,
pass_rate: float,
passed: bool,
details: dict[str, Any],
) -> bool:
"""Save ADK evaluation benchmark result to database."""
conn = self._get_connection()
now = datetime.now(timezone.utc).isoformat()
@@ -325,26 +413,50 @@ class DatabaseManager:
cursor = conn.cursor()
details_json = json.dumps(details)
if isinstance(conn, sqlite3.Connection):
cursor.execute("""
cursor.execute(
"""
INSERT INTO adk_evaluations (eval_id, case_id, total_score, max_score, pass_rate, passed, details_json, evaluated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""", (eval_id, case_id, total_score, max_score, pass_rate, passed, details_json, now))
""",
(
eval_id,
case_id,
total_score,
max_score,
pass_rate,
passed,
details_json,
now,
),
)
else:
cursor.execute("""
cursor.execute(
"""
INSERT INTO adk_evaluations (eval_id, case_id, total_score, max_score, pass_rate, passed, details_json, evaluated_at)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
""", (eval_id, case_id, total_score, max_score, pass_rate, passed, details_json, now))
""",
(
eval_id,
case_id,
total_score,
max_score,
pass_rate,
passed,
details_json,
now,
),
)
conn.commit()
return True
except Exception as exc:
except Exception as exc: # noqa: BLE001 - persistence is best-effort; the failure is logged
logger.error("Error saving evaluation %s: %s", eval_id, exc)
return False
finally:
conn.close()
_db_manager: Optional[DatabaseManager] = None
_db_manager: DatabaseManager | None = None
def get_db_manager() -> DatabaseManager: