From f7e8ee1dd066e7ed68ba92560440cf699fca30b3 Mon Sep 17 00:00:00 2001 From: KeyCOrigin Date: Wed, 4 Nov 2026 22:26:06 +0800 Subject: [PATCH 1/3] hybrid retriever: reuse query embedding across retries --- .../capabilities/kb/providers/hybrid_retriever.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/datamind/capabilities/kb/providers/hybrid_retriever.py b/datamind/capabilities/kb/providers/hybrid_retriever.py index 66a5f4a..bf05d86 100644 --- a/datamind/capabilities/kb/providers/hybrid_retriever.py +++ b/datamind/capabilities/kb/providers/hybrid_retriever.py @@ -145,16 +145,20 @@ async def _retrieve(self, query, *, top_k, filters, version): bm25, ids, texts, metas = await self._ensure_lexical_at_version(version) k_inner = top_k * self._cm stage = "kb.embedding" + # Reuse a completed embedding across vector-search retries within this + # retrieval only. Failed embedding attempts leave the cache empty. + query_vector: list[float] | None = None async def vector_search(): - nonlocal stage - stage = "kb.embedding" - vec = await self._embed.embed_query(query) + nonlocal stage, query_vector + if query_vector is None: + stage = "kb.embedding" + query_vector = await self._embed.embed_query(query) stage = "kb.vector_query" kwargs = {"top_k": k_inner, "where": filters} if version is not None: kwargs["version"] = version - return await self._store.query(vec, **kwargs) + return await self._store.query(query_vector, **kwargs) def rank_lexical(): if bm25 is not None and ids: From f48017f407c5f3ffdd85ed1770fd4b76da2f72c7 Mon Sep 17 00:00:00 2001 From: KeyCOrigin Date: Wed, 4 Nov 2026 22:43:32 +0800 Subject: [PATCH 2/3] test: expect single embedding call when vector read retries --- datamind/tests/test_read_recovery.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datamind/tests/test_read_recovery.py b/datamind/tests/test_read_recovery.py index f52d6be..8893ea1 100644 --- a/datamind/tests/test_read_recovery.py +++ b/datamind/tests/test_read_recovery.py @@ -58,7 +58,7 @@ async def test_transient_failure_falls_back_after_bounded_retries(stage): hits = await retriever.aretrieve("needle", filters={"tenant": "a"}) assert [h.id for h in hits] == ["new"] assert hits[0].metadata["degraded"] is True - assert embed.calls == 2 + assert embed.calls == (2 if stage == "embedding" else 1) summary = recovery_summary(ctx.extra["recovery_events"]) assert summary["retries"] == 1 and summary["fallbacks"] == 1 assert summary["events"][0]["component"] == f"kb.{'embedding' if stage == 'embedding' else 'vector_query'}" From de981e7fa3351d5caf1379755780cbae73da76bf Mon Sep 17 00:00:00 2001 From: NankaiKey Date: Sun, 15 Nov 2026 16:30:02 +0000 Subject: [PATCH 3/3] fix: classify transient database errors for bounded read recovery --- datamind/core/db_errors.py | 57 +++++ datamind/core/recovery.py | 23 ++- datamind/tests/test_db_recovery.py | 321 +++++++++++++++++++++++++++++ 3 files changed, 396 insertions(+), 5 deletions(-) create mode 100644 datamind/core/db_errors.py create mode 100644 datamind/tests/test_db_recovery.py diff --git a/datamind/core/db_errors.py b/datamind/core/db_errors.py new file mode 100644 index 0000000..55b6ab1 --- /dev/null +++ b/datamind/core/db_errors.py @@ -0,0 +1,57 @@ +"""Conservative error-code classification for replayable database reads. + +True means the caller may retry an independent read in a fresh transaction; +it does not authorize replaying writes or statements inside a failed transaction. +False means a recognized driver error is not on the retry allowlist. None means +this is not a recognized driver error, so generic recovery rules may apply. +Optional database drivers are deliberately not imported here. +""" +from __future__ import annotations + +import sqlite3 + + +_MYSQL_TRANSIENT = frozenset({1205, 1213, 2006, 2013}) +_POSTGRES_TRANSIENT = frozenset({ + "08000", # connection_exception + "08001", # sqlclient_unable_to_establish_sqlconnection + "08003", # connection_does_not_exist + "08006", # connection_failure + "40001", # serialization_failure + "40P01", # deadlock_detected + "57P03", # cannot_connect_now (e.g. server startup) +}) + + +def _from_driver(exc: Exception, *modules: str) -> bool: + # Include driver bases so application-defined subclasses retain their rules. + return any( + cls.__module__ == module or cls.__module__.startswith(module + ".") + for cls in type(exc).__mro__ + for module in modules + ) + + +def classify_db_error(exc: Exception) -> bool | None: + """Classify a raw driver exception using codes, never message substrings.""" + if isinstance(exc, sqlite3.Error): + code = getattr(exc, "sqlite_errorcode", None) + # Extended codes retain the primary code in the low eight bits. + # BUSY_SNAPSHOT requires a new transaction, not a statement-level retry. + # LOCKED can indicate a local cursor/schema conflict: do not retry it + # indiscriminately, nor INTERRUPT, corruption, SQL or permission errors. + return type(code) is int and (code & 0xFF) == sqlite3.SQLITE_BUSY + + if _from_driver(exc, "pymysql", "MySQLdb", "mysql.connector"): + code = getattr(exc, "errno", None) + if code is None and exc.args: + code = exc.args[0] + return type(code) is int and code in _MYSQL_TRANSIENT + + if _from_driver(exc, "psycopg", "psycopg2"): + code = getattr(exc, "sqlstate", None) or getattr(exc, "pgcode", None) + # Do not allow every 08xxx error: 08P01 is a protocol violation. + # In particular, 57014 (query cancellation) must not restart the query. + return isinstance(code, str) and code in _POSTGRES_TRANSIENT + + return None diff --git a/datamind/core/recovery.py b/datamind/core/recovery.py index 58575a5..4a46ff9 100644 --- a/datamind/core/recovery.py +++ b/datamind/core/recovery.py @@ -8,7 +8,9 @@ from typing import Any, Awaitable, Callable, TypeVar import httpx +from sqlalchemy.exc import DBAPIError, TimeoutError as SQLAlchemyTimeoutError +from .db_errors import classify_db_error from .errors import CapabilityError, ConfigError, ExternalServiceError from .logging import current_context, get_logger @@ -19,7 +21,11 @@ def is_transient(exc: Exception) -> bool: """Do not mask auth, invalid responses, bad filters, or local path errors.""" seen: set[int] = set() - while id(exc) not in seen: + pending = [exc] + while pending: + exc = pending.pop() + if id(exc) in seen: + continue seen.add(id(exc)) if isinstance(exc, (ConfigError, ValueError, PermissionError, FileNotFoundError)): return False @@ -27,14 +33,21 @@ def is_transient(exc: Exception) -> bool: return exc.response.status_code in {408, 429} or exc.response.status_code >= 500 if isinstance(exc, ExternalServiceError) and exc.status_code is not None: return exc.status_code in {408, 429} or exc.status_code >= 500 - if isinstance(exc, (TimeoutError, ConnectionError, httpx.TransportError)): + db_decision = classify_db_error(exc) + if db_decision is not None: + return db_decision + if isinstance(exc, (TimeoutError, ConnectionError, httpx.TransportError, SQLAlchemyTimeoutError)): return True if isinstance(exc, OSError) and not isinstance(exc, (CapabilityError, ExternalServiceError)): return True cause = exc.__cause__ - if not isinstance(cause, Exception): - return False - exc = cause + if isinstance(cause, Exception): + pending.append(cause) + # SQLAlchemy's original driver error may exist only in .orig. Inspect + # it before the generic cause: explicit database codes take precedence + # over incidental network/OS exceptions deeper in the chain. + if isinstance(exc, DBAPIError) and isinstance(exc.orig, Exception): + pending.append(exc.orig) return False diff --git a/datamind/tests/test_db_recovery.py b/datamind/tests/test_db_recovery.py new file mode 100644 index 0000000..f53da91 --- /dev/null +++ b/datamind/tests/test_db_recovery.py @@ -0,0 +1,321 @@ +"""Database code contracts plus recovery against real, independently locked SQLite. + +MySQL/PostgreSQL exceptions below model optional drivers' public attributes; +those tests do not claim to exercise live MySQL/PostgreSQL servers. +""" +from __future__ import annotations + +import sqlite3 + +import pytest +from sqlalchemy import event, text +from sqlalchemy.exc import OperationalError, TimeoutError as PoolTimeout +from sqlalchemy.pool import NullPool + +from datamind.capabilities.db.providers.sqlite import SQLiteDialect +from datamind.capabilities.db.service import DBService +from datamind.config import DBConfig, Settings +from datamind.core.context import RequestContext +from datamind.core.errors import CapabilityError, ConfigError +from datamind.core.logging import bind_context +from datamind.core.recovery import is_transient, recover_read +from datamind.workspace import Workspace + + +def driver_error(module, *args, **attrs): + error_type = type("DriverError", (Exception,), {"__module__": module}) + error = error_type(*args) + for name, value in attrs.items(): + setattr(error, name, value) + return error + + +def sqlite_error(code): + error = sqlite3.OperationalError("message is deliberately not classified") + if code is not None: + error.sqlite_errorcode = code + return error + + +def wrap(error): + return OperationalError("SELECT 42", {}, error) + + +@pytest.mark.parametrize("code,expected", [ + (5, True), (261, True), (517, True), (773, True), + (6, False), (262, False), (1, False), (8, False), (9, False), + (11, False), (14, False), (19, False), (999, False), (None, False), +]) +@pytest.mark.parametrize("wrapped", [False, True]) +def test_sqlite_codes(code, expected, wrapped): + error = sqlite_error(code) + assert is_transient(wrap(error) if wrapped else error) is expected + + +@pytest.mark.parametrize("module,field", [ + ("pymysql.err", "args"), ("MySQLdb", "args"), + ("mysql.connector.errors", "errno"), +]) +@pytest.mark.parametrize("code,expected", [ + (1205, True), (1213, True), (2006, True), (2013, True), + (1045, False), (1044, False), (1049, False), (1064, False), + (1146, False), (1054, False), (1062, False), (9999, False), (None, False), +]) +def test_mysql_driver_code_contracts(module, field, code, expected): + args = (code, "driver error") if field == "args" else ("driver error",) + error = driver_error(module, *args, **({"errno": code} if field == "errno" else {})) + assert is_transient(error) is expected + assert is_transient(wrap(error)) is expected + + +@pytest.mark.parametrize("module,field", [("psycopg.errors", "sqlstate"), ("psycopg2.errors", "pgcode")]) +@pytest.mark.parametrize("code,expected", [ + ("08000", True), ("08001", True), ("08003", True), ("08006", True), + ("40001", True), ("40P01", True), ("57P03", True), + ("08P01", False), ("57014", False), ("28P01", False), ("28000", False), + ("42501", False), ("42601", False), ("42P01", False), + ("23505", False), ("99999", False), (None, False), +]) +def test_postgres_driver_code_contracts(module, field, code, expected): + error = driver_error(module, "driver error", **{field: code}) + assert is_transient(error) is expected + assert is_transient(wrap(error)) is expected + + +def test_driver_subclasses_keep_code_rules_and_override_generic_oserror(): + base = type("DriverError", (OSError,), {"__module__": "pymysql.err"}) + + class ApplicationError(base): + pass + + assert is_transient(ApplicationError(1213, "deadlock")) + assert not is_transient(ApplicationError(1045, "authentication failed")) + + +@pytest.mark.parametrize("error", [ConnectionError(), TimeoutError(), PoolTimeout()]) +def test_sqlalchemy_orig_only_transient_error(error): + outer = wrap(error) + assert outer.__cause__ is None + assert is_transient(outer) + + +@pytest.mark.parametrize("error", [sqlite_error(1), driver_error("pymysql.err", 1045)]) +def test_explicit_database_rejection_precedes_incidental_network_cause(error): + error.__cause__ = ConnectionError() + outer = wrap(error) + outer.__cause__ = TimeoutError() + assert not is_transient(outer) + + +@pytest.mark.parametrize("outer_type", [RuntimeError, CapabilityError]) +def test_database_error_through_cause(outer_type): + outer = outer_type("db", "failed") if outer_type is CapabilityError else outer_type("failed") + outer.__cause__ = wrap(sqlite_error(5)) + assert is_transient(outer) + + +@pytest.mark.parametrize("outer_type", [ValueError, ConfigError, PermissionError, FileNotFoundError]) +def test_explicit_outer_rejection_still_wins(outer_type): + outer = outer_type("invalid operation") + outer.__cause__ = wrap(sqlite_error(5)) + assert not is_transient(outer) + + +def test_unknown_codes_messages_and_unrelated_attributes_are_not_retry_signals(): + assert not is_transient(sqlite3.OperationalError("database is locked")) + error = RuntimeError("database is locked, timeout, connection lost") + error.orig = ConnectionError() + error.errno = 1213 + error.sqlstate = "40001" + assert not is_transient(error) + assert not is_transient(wrap(RuntimeError("unknown driver error"))) + + +def test_exception_graph_cycles_terminate_and_do_not_hide_another_branch(): + outer = wrap(sqlite_error(5)) + outer.__cause__ = outer + assert is_transient(outer) + outer.orig = outer + assert not is_transient(outer) + outer.__cause__ = ConnectionError() + assert is_transient(outer) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("error,expected_calls", [ + (driver_error("pymysql.err", 1213, "deadlock"), 2), + (driver_error("psycopg.errors", sqlstate="40001"), 2), + (driver_error("pymysql.err", 1045, "authentication failed"), 1), + (driver_error("psycopg2.errors", pgcode="57014"), 1), +]) +async def test_driver_codes_control_actual_recovery_loop(error, expected_calls): + calls = 0 + original = wrap(error) + + async def operation(): + nonlocal calls + calls += 1 + if calls == 1: + raise original + return 42 + + if expected_calls == 2: + assert await recover_read(operation, component="db_query_sql", retries=2) == 42 + else: + with pytest.raises(OperationalError) as caught: + await recover_read(operation, component="db_query_sql", retries=2) + assert caught.value is original + assert calls == expected_calls + + +@pytest.mark.asyncio +async def test_nested_database_retries_share_budget(): + calls = 0 + + async def operation(): + nonlocal calls + calls += 1 + raise wrap(driver_error("pymysql.err", 1213, "deadlock")) + + with pytest.raises(OperationalError): + await recover_read( + lambda: recover_read(operation, component="db.inner", retries=5), + component="db.outer", retries=1, + ) + assert calls == 2 + + +@pytest.fixture +def locked_database(tmp_path): + path = tmp_path / "locked.db" + dialect = SQLiteDialect() + engine = dialect.build_engine( + None, default_path=path, connect_args={"timeout": 0.005}, poolclass=NullPool, + ) + with engine.begin() as connection: + connection.execute(text("CREATE TABLE numbers (value INTEGER)")) + connection.execute(text("INSERT INTO numbers VALUES (42)")) + service = DBService(dialect=dialect, engine=engine, db_cfg=DBConfig()) + blocker = sqlite3.connect(path, isolation_level=None) + blocker.execute("BEGIN EXCLUSIVE") + try: + yield service, blocker + finally: + blocker.rollback() + blocker.close() + engine.dispose() + + +@pytest.mark.asyncio +async def test_real_sqlite_busy_recovers_after_other_connection_unlocks(locked_database): + service, blocker = locked_database + errors = [] + calls = 0 + + async def operation(): + nonlocal calls + calls += 1 + try: + return await service.query_sql("SELECT value FROM numbers") + except OperationalError as error: + errors.append(error) + # Release the actual exclusive lock only after the read has failed. + blocker.rollback() + raise + + ctx = RequestContext.new() + with bind_context(ctx): + result = await recover_read(operation, component="db_query_sql", retries=2) + assert result.rows == [[42]] + assert calls == 2 + assert len(errors) == 1 + assert errors[0].orig.sqlite_errorcode == sqlite3.SQLITE_BUSY + event, = ctx.extra["recovery_events"] + assert (event["attempts"], event["retries"], event["outcome"]) == (2, 1, "ok") + + +@pytest.mark.asyncio +@pytest.mark.parametrize("retries", [0, 2]) +async def test_real_sqlite_busy_stops_at_retry_budget(locked_database, retries): + service, _ = locked_database + calls = 0 + + async def operation(): + nonlocal calls + calls += 1 + return await service.query_sql("SELECT value FROM numbers") + + with pytest.raises(OperationalError) as caught: + await recover_read(operation, component="db_query_sql", retries=retries) + assert caught.value.orig.sqlite_errorcode == sqlite3.SQLITE_BUSY + assert calls == retries + 1 + + +@pytest.mark.asyncio +@pytest.mark.parametrize("sql", ["SELECT value FROM absent_table", "SELECT * FRM numbers"]) +async def test_real_sqlite_sql_errors_are_not_retried(locked_database, sql): + service, blocker = locked_database + blocker.rollback() + calls = 0 + + async def operation(): + nonlocal calls + calls += 1 + return await service.query_sql(sql) + + with pytest.raises(OperationalError) as caught: + await recover_read(operation, component="db_query_sql", retries=2) + assert caught.value.orig.sqlite_errorcode == sqlite3.SQLITE_ERROR + assert calls == 1 + + +@pytest.mark.asyncio +async def test_workspace_db_tool_recovers_real_busy_on_pinned_revision(tmp_path, monkeypatch): + source = tmp_path / "source" + source.mkdir() + (source / "numbers.csv").write_text("value\n42\n") + settings = Settings( + llm={"api_key": "test", "model": "test"}, + data={"base_dir": tmp_path / "runtime"}, + workspace={"retention": "all"}, + ) + workspace = Workspace(settings) + manifest = await workspace.update(str(source), surfaces=("db",)) + async with workspace.reader(read_retries=1) as reader: + service = reader._system.services.db + table, = await service.list_tables() + service.engine.dispose() + + @event.listens_for(service.engine, "connect") + def set_busy_timeout(connection, _): + connection.execute("PRAGMA busy_timeout=5") + + original = service.query_sql + calls = 0 + blocker = sqlite3.connect(service.engine.url.database, isolation_level=None) + blocker.execute("BEGIN EXCLUSIVE") + + async def query(sql): + nonlocal calls + calls += 1 + try: + return await original(sql) + except OperationalError as error: + assert error.orig.sqlite_errorcode == sqlite3.SQLITE_BUSY + blocker.rollback() + raise + + monkeypatch.setattr(service, "query_sql", query) + ctx = RequestContext.new() + try: + with bind_context(ctx): + result = await reader.call("db_query_sql", sql=f'SELECT value FROM "{table}"') + finally: + blocker.rollback() + blocker.close() + assert str(result["rows"][0][0]) == "42" + assert calls == 2 + assert reader.revision == manifest["workspace_revision"] + record, = ctx.extra["recovery_events"] + assert record["retries"] == 1 + assert record["workspace_revision"] == reader.revision