ai-powered-conversational-intelligence

03 — Generic Database Adapter Core

Prompt 03 of the 32-prompt Enterprise AI Analytics & Recommendation Platform initiative (00_MASTER_IMPLEMENTATION_CONTRACT.md), building on 02_TARGET_ARCHITECTURE.md’s boundary map, which already labeled db/connection.py::SUPPORTED_DB_TYPES as the “Database Adapter” boundary’s existing reused code. INSPECT/PLAN/IMPLEMENT (additive only) — no existing route, node, or Settings field changed behavior; one existing function (db.execution.execute_readonly_sql) gained a new, optional, default-None parameter that is a provable no-op for both of its two real callers.

1. The honest starting point: the acceptance criterion already holds

Prompt 03’s stated acceptance criterion is “a future PostgreSQL/MySQL/ Oracle adapter can be added without modifying the AI orchestration layer.” A code-verified read of agent/nodes.py shows this is already true today, and was true before this prompt: every call site that touches the database calls a generic function parameterized by whichever engine/db_type the caller resolved —

None of these branches on db_type inside agent/. Adding a 5th DB_TYPE to db.connection.SUPPORTED_DB_TYPES (plus the matching entries in db/execution.py’s _STATEMENT_TIMEOUT_SQL and db/query_cost.py’s _STRATEGIES, and a driver installed) already requires zero changes to agent/nodes.py, agent/graph.py, or agent/orchestrator/. This document says that plainly rather than claiming this prompt invented an abstraction that predates it — see agent/provenance.py’s DataTruthLevel naming convention this initiative already established for not overclaiming.

2. The real, narrow gap this prompt closes

What was genuinely missing: a single place to ask “what can this engine do.” Capability facts were scattered across three independently- maintained dicts in three files, with no single object connecting them:

Fact Lived in Keys today
dialect / driver / default port db/connection.py::SUPPORTED_DB_TYPES postgresql, mysql, mssql, oracle
driver-level statement timeout support db/execution.py::_STATEMENT_TIMEOUT_SQL postgresql, mysql only
cost-estimation strategy db/query_cost.py::_STRATEGIES postgresql, mysql, mssql, oracle
write-privilege check query db/connection.py::_WRITE_PRIVILEGE_QUERIES postgresql, mysql, mssql, oracle
DB version query db/connection.py::_VERSION_QUERIES postgresql, mysql, mssql, oracle

A caller (or a future new engine implementer) had no way to introspect this without reading four separate private dicts across three files. db/adapter.py::DatabaseCapabilities is the single, computed-not- duplicated answer.

3. What was built

db/adapter.py (new)

Two new small accessor functions (not four)

Neither dict changed. Neither function is called anywhere except db/adapter.py and their own tests today.

db/execution.py: one real, additive behavior extension

_execute_with_timeout and the public execute_readonly_sql both gained a new trailing parameter, params: dict[str, object] | None = None (SQLAlchemy’s own Connection.execute(statement, parameters) bind mechanism — already used internally by db/query_cost.py’s Oracle strategy, just never exposed as a parameter of this function before). It is threaded into the one real execute call as:

cursor_result = (
    connection.execute(text(sql), params) if params else connection.execute(text(sql))
)

When params is falsy (its default), this reproduces the exact original call — no second argument at all. Verified: both real callers (agent/nodes.py’s execute_sql_node, api/main.py’s POST /execute) pass exactly 3 positional args + engine= and never pass params — neither call site changed behavior. This was a deliberate choice over having the adapter re-open a connection and duplicate the timeout/ force-close/row-cap logic itself — that logic has its own documented, benchmark-driven fix history (db/query_cost.py::_run_with_timeout’s 2026-09-25 connection-leak fix mirrors _execute_with_timeout’s mechanic because a second copy of it had already drifted once before); extending the one real implementation instead of writing a second one is what keeps “zero reimplementation” true.

4. “Stubbed today, wired later” — who consumes this

Nothing in the live request path calls db/adapter.py in this increment. agent/nodes.py, agent/graph.py, agent/orchestrator/, and every route in api/ are byte-for-byte unchanged in behavior — this mirrors the exact disclosure agent/tools/ (Prompt-series precedent) and recommendation/ (Prompt 02) already made for their own “real, tested, functionally complete, but not yet wired into the live path” increments.

This becomes load-bearing the day a genuinely new, currently-unsupported engine family (DuckDB, Snowflake, SQLite, …) needs to be added: that engine would implement DatabaseAdapter (or extend SqlAlchemyDatabaseAdapter if it’s SQLAlchemy-compatible) and register its capability facts in DatabaseCapabilities.for_db_type once, instead of touching four separate dicts across three files as today’s four engines would require. Until that day, SqlAlchemyDatabaseAdapter is a correct, tested, but optional second way to reach the exact same functions every existing call site already calls directly.

5. Explicitly out of scope (per the approved plan)

6. Testing

Full suite: 2397 tests pass (2343 pre-existing + 54 new), zero regressions. ruff check . / black --check . / mypy . on new/touched files: clean. Whole-repo counts confirmed unchanged from the disclosed pre-existing baseline: 7 ruff / 11 black / 236 mypy findings, none in any file this prompt touched (cross-referenced by file path).