fix: harden assistant SQLite/PostgreSQL dual-backend (singleton, timeouts, warnings)

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
oliver 2026-05-14 10:24:23 +08:00
parent 1490bd5f24
commit 1caf96e694
8 changed files with 129 additions and 15 deletions

View file

@ -3,6 +3,9 @@
from __future__ import annotations
import os
import warnings
_PG_URL_IGNORED_WARNED = False
def assistant_db_backend() -> str:
@ -22,8 +25,31 @@ def assistant_db_backend() -> str:
)
def _warn_if_postgres_url_ignored_for_sqlite() -> None:
"""Emit once if PostgreSQL DSN env vars are set but backend is still SQLite."""
global _PG_URL_IGNORED_WARNED
if _PG_URL_IGNORED_WARNED or assistant_db_backend() != "sqlite":
return
for var in (
"AIA_ASSISTANT_DATABASE_URL",
"OPS_ASSISTANT_DATABASE_URL",
"AIA_ASSISTANT_PG_DSN",
"OPS_ASSISTANT_PG_DSN",
):
if str(os.getenv(var) or "").strip():
_PG_URL_IGNORED_WARNED = True
warnings.warn(
f"{var} is set but assistant DB backend is sqlite; the PostgreSQL URL is ignored. "
"Set AIA_ASSISTANT_DB_BACKEND=postgresql (or pg/postgres) to use PostgreSQL.",
UserWarning,
stacklevel=2,
)
return
def assistant_sqlalchemy_url() -> str:
"""SQLAlchemy URL for the assistant store (sqlite or postgresql+psycopg)."""
_warn_if_postgres_url_ignored_for_sqlite()
if assistant_db_backend() == "postgresql":
raw = assistant_postgres_dsn()
if raw.startswith("postgresql+") or raw.startswith("postgres+"):
@ -54,4 +80,15 @@ def assistant_postgres_dsn() -> str:
return url
__all__ = ["assistant_db_backend", "assistant_postgres_dsn", "assistant_sqlalchemy_url"]
def reset_assistant_db_config_warnings_for_tests() -> None:
"""Reset one-shot warnings (pytest only)."""
global _PG_URL_IGNORED_WARNED
_PG_URL_IGNORED_WARNED = False
__all__ = [
"assistant_db_backend",
"assistant_postgres_dsn",
"assistant_sqlalchemy_url",
"reset_assistant_db_config_warnings_for_tests",
]