Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)
Python
patch: Reuse SQLite connections for Code and messaging (#1939)
Ali Khokhar0c3a1de257ce
diff --git a/pyproject.toml b/pyproject.tomlindex 97226cdd..9b5d4306 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -39,6 +39,7 @@ dependencies = ["grpcio-tools>=1.81.1","nvidia-riva-client>=2.26.0","pyperclip>=1.11.0",+ "sqlalchemy>=2.1.1",][project.scripts]diff --git a/src/free_claude_code/runtime/messaging_import.py b/src/free_claude_code/runtime/messaging_import.pyindex 82d50b43..1ac31106 100644--- a/src/free_claude_code/runtime/messaging_import.py+++ b/src/free_claude_code/runtime/messaging_import.py@@ -201,6 +201,6 @@ def _import_legacy(database: SQLiteDatabase, path: Path) -> str | None:database.execute(lambda connection: connection.execute("UPDATE messaging_legacy_import SET cleanup_pending=0"- )+ ).close())return warningdiff --git a/src/free_claude_code/runtime/sqlite_database.py b/src/free_claude_code/runtime/sqlite_database.pyindex 647b5d8b..4e276a52 100644--- a/src/free_claude_code/runtime/sqlite_database.py+++ b/src/free_claude_code/runtime/sqlite_database.py@@ -1,7 +1,7 @@"""Connection, transaction, and lifetime ownership of FCC's shared database.-The application holds the owner lock until both features close. Each connection-is opened, used, and closed within initialization or one synchronous transaction.+The application holds the owner lock until both features close and pooled+connections are disposed. Each transaction exclusively leases one connection."""import asyncio@@ -10,6 +10,11 @@ import sqlite3from collections.abc import Callablefrom contextlib import closingfrom pathlib import Path+from typing import cast++from sqlalchemy.engine.interfaces import DBAPIConnection+from sqlalchemy.exc import TimeoutError as PoolTimeoutError+from sqlalchemy.pool import QueuePoolfrom free_claude_code.core.async_tasks import run_sync_ownedfrom free_claude_code.core.interprocess_lock import InterprocessFileLock@@ -36,15 +41,22 @@ _MESSAGING_COLUMNS = {}-def _connect(path: Path, *, existing: bool = False) -> sqlite3.Connection:+def _connect(+ path: Path, *, existing: bool = False, check_same_thread: bool = True+) -> sqlite3.Connection:connection = sqlite3.connect(path.as_uri() + ("?mode=rw" if existing else "?mode=rwc"),uri=True,timeout=10,autocommit=True,+ check_same_thread=check_same_thread,)- connection.row_factory = sqlite3.Row- connection.execute("PRAGMA foreign_keys = ON")+ try:+ connection.row_factory = sqlite3.Row+ connection.execute("PRAGMA foreign_keys = ON").close()+ except BaseException:+ connection.close()+ raisereturn connection@@ -191,6 +203,10 @@ class SQLiteDatabase:self._closing = Falseself._startup_error: Exception | None = Noneself._operations: set[asyncio.Task] = set()+ self._writer: QueuePool | None = None+ self._readers: QueuePool | None = None+ self._write_admission = asyncio.Semaphore(1)+ self._read_admission = asyncio.Semaphore(4)async def start(self) -> None:async with self._lifecycle:@@ -204,10 +220,10 @@ class SQLiteDatabase:await run_sync_owned(self._initialize)except Exception as exc:self._startup_error = exc- self._owner.release()+ await run_sync_owned(self._dispose)raiseexcept BaseException:- self._owner.release()+ await run_sync_owned(self._dispose)raiseself._started = True@@ -224,37 +240,114 @@ class SQLiteDatabase:for path in (self.path, Path(f"{self.path}-wal"), Path(f"{self.path}-shm")):if path.exists():path.chmod(0o600)+ self._writer = self._pool(1)+ self._readers = self._pool(4)++ def _pool(self, size: int) -> QueuePool:+ return QueuePool(+ lambda: cast(DBAPIConnection, _connect(self.path, check_same_thread=False)),+ pool_size=size,+ max_overflow=0,+ timeout=10,+ reset_on_return=None,+ )++ def _dispose(self) -> None:+ try:+ if self._writer is not None:+ self._writer.dispose()+ self._writer = None+ finally:+ if self._readers is not None:+ self._readers.dispose()+ self._readers = None+ self._owner.release()def execute[T](self, operation: Callable[[sqlite3.Connection], T], *, write: bool = True) -> T:- """Run one transaction inside an admitted worker, closing its connection.+ """Run a writer-pool transaction inside an already admitted worker.Callbacks consume the connection synchronously and return materialized- results. They must not begin or finish the outer transaction themselves.+ results. They must not control the outer transaction, retain or close+ the connection, or change connection-wide settings.Legacy import uses this primitive for several transactions in one worker."""- with closing(_connect(self.path)) as connection:+ return self._transaction(self._writer, operation, write=write)++ def _transaction[T](+ self,+ pool: QueuePool | None,+ operation: Callable[[sqlite3.Connection], T],+ *,+ write: bool,+ ) -> T:+ if pool is None:+ raise sqlite3.OperationalError("FCC storage is closed")+ try:+ lease = pool.connect()+ except PoolTimeoutError as exc:+ raise sqlite3.OperationalError(+ "Timed out waiting for FCC storage connection capacity"+ ) from exc+ try:+ connection = lease.driver_connection+ if not isinstance(connection, sqlite3.Connection):+ raise sqlite3.OperationalError("Invalid FCC storage connection")+ if connection.in_transaction:+ raise sqlite3.OperationalError("FCC storage connection is not clean")+ except BaseException as exc:+ lease.invalidate(exc)+ lease.close()+ raise+ try:connection.execute("BEGIN IMMEDIATE" if write else "BEGIN")+ result = operation(connection)+ connection.execute("COMMIT")+ return result+ except BaseException as exc:try:- result = operation(connection)- connection.execute("COMMIT")- return result- except BaseException:if connection.in_transaction:connection.execute("ROLLBACK")- raise+ if connection.in_transaction:+ raise sqlite3.OperationalError(+ "FCC storage rollback did not finish"+ )+ except BaseException as cleanup_error:+ raise exc from cleanup_error+ finally:+ # A traceback can retain a SELECT cursor after ROLLBACK. Never+ # let that cursor's snapshot reach the next borrower.+ lease.invalidate(exc)+ raise+ finally:+ lease.close()async def run[T](self, operation: Callable[[sqlite3.Connection], T], *, write: bool = True) -> T:"""Admit and supervise one transaction through committed result delivery."""- return await self.work(lambda: self.execute(operation, write=write))+ if write:+ return await self.work(lambda: self.execute(operation))+ return await self._work(+ lambda: self._transaction(self._readers, operation, write=False),+ self._read_admission,+ )async def work[T](self, operation: Callable[[], T]) -> T:+ return await self._work(operation, self._write_admission)++ async def _work[T](+ self, operation: Callable[[], T], admission: asyncio.Semaphore+ ) -> T:if not self._started or self._closing:raise sqlite3.OperationalError("FCC storage is closed")- task = asyncio.create_task(run_sync_owned(operation))++ async def admitted() -> T:+ async with admission:+ return await run_sync_owned(operation)++ task = asyncio.create_task(admitted())self._operations.add(task)try:# Deliver the result before cancellation can separate SQL commit from@@ -279,4 +372,4 @@ class SQLiteDatabase:except asyncio.CancelledError:continueself._started = False- await run_sync_owned(self._owner.release)+ await run_sync_owned(self._dispose)diff --git a/tests/runtime/test_code_sessions_sqlite.py b/tests/runtime/test_code_sessions_sqlite.pyindex f0f89951..3132f91c 100644--- a/tests/runtime/test_code_sessions_sqlite.py+++ b/tests/runtime/test_code_sessions_sqlite.py@@ -2,6 +2,7 @@ import asyncioimport jsonimport sqlite3import threading+import tracebackimport uuidfrom contextlib import closingfrom typing import Literal@@ -22,6 +23,52 @@ from free_claude_code.runtime.code_sessions_sqlite import SQLiteCodeStorefrom free_claude_code.runtime.sqlite_database import SQLiteDatabase+async def test_failed_history_decode_cannot_leak_snapshot_to_next_reader(store):+ session = await store.create(+ CodeSession(id="s", cwd="/work", model="provider/model", title="before")+ )++ def seed(connection):+ for index in range(2):+ code_store_module._insert(+ connection,+ "code_runs",+ CodeRun(+ id=f"r{index}",+ session_id=session.id,+ text="test",+ model=session.model,+ ordinal=index + 1,+ status="completed",+ ),+ )+ connection.execute(+ "UPDATE code_runs SET error_details='invalid json' WHERE id='r0'"+ ).close()++ await store.database.run(seed)+ with pytest.raises(json.JSONDecodeError) as failure:+ await store.runs(session.id)+ # Keep the traceback, and therefore its unfinished SELECT cursor, alive.+ try:+ await store.database.run(+ lambda connection: connection.execute(+ "UPDATE code_sessions SET title='after' WHERE id=?", (session.id,)+ ).close()+ )+ with closing(sqlite3.connect(store.database.path)) as external:+ assert (+ external.execute(+ "SELECT title FROM code_sessions WHERE id=?", (session.id,)+ ).fetchone()[0]+ == "after"+ )+ assert (await store.get_session(session.id)).title == "after"+ finally:+ traceback.clear_frames(failure.value.__traceback__)++@pytest.mark.asyncioasync def test_cancelled_initialization_drains_thread_before_releasing_owner_lock(database_factory,diff --git a/tests/runtime/test_messaging_sqlite.py b/tests/runtime/test_messaging_sqlite.pyindex 6fc79180..835d0369 100644--- a/tests/runtime/test_messaging_sqlite.py+++ b/tests/runtime/test_messaging_sqlite.py@@ -241,7 +241,7 @@ async def test_failed_import_transaction_keeps_source_and_allows_retry(assert source.exists()assert (await storage.load_conversation_snapshot()).is_emptyassert (- storage.database.execute(+ await storage.database.run(lambda connection: connection.execute("SELECT count(*) FROM messaging_legacy_import").fetchone()[0],@@ -259,8 +259,8 @@ def incoming(node, *, text="prompt"):)-def import_receipt(database):- return database.execute(+async def import_receipt(database):+ return await database.run(lambda connection: dict(connection.execute("SELECT * FROM messaging_legacy_import").fetchone()),@@ -313,7 +313,7 @@ async def test_import_rejects_storage_invalid_tree_without_reserving_references(after.identity: after,}assert source.read_text() == original- assert import_receipt(storage.database) == {+ assert await import_receipt(storage.database) == {"source": "sessions.json","outcome": "partial","trees": 2,@@ -357,7 +357,7 @@ async def test_import_message_storage_validation_and_successful_duplicate_ownersrejected = value == "\ud800" or field == "kind"assert bool(await import_legacy(storage.database, source)) is rejectedassert source.exists() is rejected- rows = storage.database.execute(+ rows = await storage.database.run(lambda connection: [tuple(row)for row in connection.execute(@@ -378,7 +378,7 @@ async def test_import_message_storage_validation_and_successful_duplicate_ownersbad["kind"],)assert len(rows) == (1 if field == "ts" else 2)- receipt = import_receipt(storage.database)+ receipt = await import_receipt(storage.database)assert receipt["messages"] == len(rows)assert receipt["skipped"] == int(rejected)@@ -395,7 +395,7 @@ async def test_import_last_tree_identity_wins_even_when_unrecoverable(assert (await storage.load_conversation_snapshot()).trees == ({} if last_invalid else {last.identity: last})- assert import_receipt(storage.database)["skipped"] == 1 + int(last_invalid)+ assert (await import_receipt(storage.database))["skipped"] == 1 + int(last_invalid)await storage.commit_trees((tree(root="fresh"),))@@ -416,7 +416,7 @@ async def test_partial_import_survives_reopen_and_clear_without_replay())assert await import_legacy(storage.database, source)- receipt = import_receipt(storage.database)+ receipt = await import_receipt(storage.database)await storage.database.close()for restart in range(2):database = SQLiteDatabase(storage.database.path, tmp_path / "code.lock")@@ -424,7 +424,7 @@ async def test_partial_import_survives_reopen_and_clear_without_replay(try:reopened = SQLiteMessagingStore(database)assert await import_legacy(database, source) is None- assert import_receipt(database) == receipt+ assert await import_receipt(database) == receiptassert (await reopened.load_conversation_snapshot()).is_empty is bool(restart)@@ -438,8 +438,6 @@ async def test_partial_import_survives_reopen_and_clear_without_replay(async def test_import_outer_failure_rolls_back_released_units(storage, tmp_path, failure_stage):- from free_claude_code.runtime import sqlite_database-source = tmp_path / "sessions.json"source.write_text(json.dumps(@@ -453,12 +451,10 @@ async def test_import_outer_failure_rolls_back_released_units(}))- connect = sqlite_database._connect+ transaction = storage.database._transactioninserted = []- def faulting_connect(path, *, existing=False):- connection = connect(path, existing=existing)-+ def faulting_transaction(pool, operation, *, write):def authorize(action, arg1, arg2, database_name, trigger):if action == sqlite3.SQLITE_INSERT:inserted.append(arg1)@@ -473,12 +469,26 @@ async def test_import_outer_failure_rolls_back_released_units(return sqlite3.SQLITE_DENYreturn sqlite3.SQLITE_OK- connection.set_authorizer(authorize)- return connection+ observed = []++ def faulting(connection):+ observed.append(connection)+ connection.set_authorizer(authorize)+ return operation(connection)+ result = transaction(pool, faulting, write=write)+ # Failed transactions discard their connection; only successful leases+ # need the injected connection state restored before another checkout.+ for connection in observed:+ connection.set_authorizer(None)+ return result++ await storage.database.run(+ lambda connection: connection.execute("SELECT 1").close()+ )with (- patch.object(sqlite_database, "_connect", faulting_connect),- pytest.raises(sqlite3.DatabaseError),+ patch.object(storage.database, "_transaction", faulting_transaction),+ pytest.raises(sqlite3.DatabaseError, match="not authorized"),):await import_legacy(storage.database, source)assert "messaging_nodes" in inserted and "messaging_managed_messages" in inserted@@ -486,7 +496,7 @@ async def test_import_outer_failure_rolls_back_released_units(assert (await storage.load_conversation_snapshot()).is_emptyassert not await storage.get_tracked_message_ids_for_chat("telegram", "chat")assert (- storage.database.execute(+ await storage.database.run(lambda connection: connection.execute("SELECT count(*) FROM messaging_legacy_import").fetchone()[0],@@ -495,8 +505,8 @@ async def test_import_outer_failure_rolls_back_released_units(== 0)assert await import_legacy(storage.database, source) is None- assert import_receipt(storage.database)["trees"] == 1- assert import_receipt(storage.database)["messages"] == 1+ assert (await import_receipt(storage.database))["trees"] == 1+ assert (await import_receipt(storage.database))["messages"] == 1@pytest.mark.parametrize("platform", ["none", "telegram"])@@ -576,11 +586,11 @@ async def test_startup_partial_import_preserves_links_scopes_and_code("2","3",]- assert import_receipt(storage.database)["skipped"] == 2- assert import_receipt(storage.database)["messages"] == 3+ assert (await import_receipt(storage.database))["skipped"] == 2+ assert (await import_receipt(storage.database))["messages"] == 3assert await code.get_session(session.id) == sessionassert (- storage.database.execute(+ await storage.database.run(lambda connection: connection.execute("PRAGMA foreign_key_check").fetchall(),diff --git a/tests/runtime/test_sqlite_database.py b/tests/runtime/test_sqlite_database.pyindex e98bf2af..a31aa427 100644--- a/tests/runtime/test_sqlite_database.py+++ b/tests/runtime/test_sqlite_database.py@@ -9,6 +9,8 @@ from pathlib import Pathimport httpximport pytest+from anyio import to_thread+from sqlalchemy.exc import TimeoutError as PoolTimeoutErrorfrom free_claude_code.application.code_sessions.models import CodeUnavailableErrorfrom free_claude_code.runtime import sqlite_database@@ -16,6 +18,365 @@ from free_claude_code.runtime.code_sessions_sqlite import SQLiteCodeStorefrom free_claude_code.runtime.sqlite_database import initialize_database+async def test_transactions_reuse_connections_until_database_closes(+ database_factory, tmp_path+):+ database = database_factory(tmp_path / "fcc.db", tmp_path / "fcc.lock")+ await database.start()+ observed = []++ def observe(connection):+ observed.append(connection)+ assert connection.in_transaction+ assert connection.autocommit is True+ assert connection.execute("PRAGMA synchronous").fetchone()[0] == 2+ assert connection.execute("PRAGMA foreign_keys").fetchone()[0] == 1+ assert connection.execute("PRAGMA journal_mode").fetchone()[0] == "wal"+ return connection.execute("SELECT 1 AS value").fetchone()["value"]++ for write in (True, True, False, False):+ assert await database.run(observe, write=write) == 1+ assert observed[0] is observed[1]+ assert observed[2] is observed[3]+ assert observed[0] is not observed[2]+ await database.close()+ for connection in observed:+ with pytest.raises(sqlite3.ProgrammingError, match="closed"):+ connection.execute("SELECT 1")+++async def test_queued_writers_leave_workers_available_and_drain_on_close(+ database_factory, tmp_path+):+ database = database_factory(tmp_path / "fcc.db", tmp_path / "fcc.lock")+ await database.start()+ entered, release = threading.Event(), threading.Event()+ limiter = to_thread.current_default_thread_limiter()+ previous_limit = limiter.total_tokens+ limiter.total_tokens = 2++ def write(connection, value):+ if value == "0":+ entered.set()+ assert release.wait(10)+ connection.execute("INSERT INTO code_deleted VALUES (?)", (value,)).close()+ return value++ writers = [asyncio.create_task(database.run(lambda c: write(c, "0")))]+ readers = []+ closing_task = None+ try:+ assert await asyncio.to_thread(entered.wait, 3)+ writers.extend(+ asyncio.create_task(database.run(lambda c, i=i: write(c, str(i))))+ for i in range(1, 12)+ )+ # Let each accepted call register its admission task before the probes.+ await asyncio.sleep(0)+ await asyncio.sleep(0)+ writers[-1].cancel()+ readers = [+ asyncio.create_task(+ database.run(+ lambda c: c.execute("SELECT count(*) FROM code_deleted").fetchone()[+ 0+ ],+ write=False,+ )+ ),+ asyncio.create_task(to_thread.run_sync(lambda: "available")),+ ]+ done, pending = await asyncio.wait(readers, timeout=3)+ assert not pending+ assert {task.result() for task in done} == {0, "available"}+ closing_task = asyncio.create_task(database.close())+ await asyncio.sleep(0)+ assert not closing_task.done()+ with pytest.raises(sqlite3.OperationalError, match="closed"):+ await database.run(lambda c: None)+ other = database_factory(database.path, tmp_path / "fcc.lock")+ with pytest.raises(sqlite3.OperationalError, match="another FCC"):+ await other.start()+ finally:+ release.set()+ outcomes = await asyncio.gather(*writers, *readers, return_exceptions=True)+ if closing_task is not None:+ await closing_task+ limiter.total_tokens = previous_limit+ assert outcomes[:12] == [str(i) for i in range(12)]+ reopened = database_factory(database.path, tmp_path / "fcc.lock")+ await reopened.start()+ assert (+ await reopened.run(+ lambda c: c.execute("SELECT count(*) FROM code_deleted").fetchone()[0],+ write=False,+ )+ == 12+ )+++async def test_reader_leases_are_bounded_and_can_move_between_threads(+ database_factory, tmp_path+):+ database = database_factory(tmp_path / "fcc.db", tmp_path / "fcc.lock")+ await database.start()+ four_readers, release = threading.Event(), threading.Event()+ observed = []+ guard = threading.Lock()++ def read(connection):+ with guard:+ observed.append(connection)+ if len(observed) == 4:+ four_readers.set()+ assert release.wait(10)+ return connection.execute("SELECT 1").fetchone()[0]++ tasks = [asyncio.create_task(database.run(read, write=False)) for _ in range(10)]+ try:+ assert await asyncio.to_thread(four_readers.wait, 3)+ assert len(observed) == 4+ finally:+ release.set()+ results = await asyncio.gather(*tasks)+ assert results == [1] * 10+ assert len(set(observed)) == 4+ # Force two different worker threads to reuse the single writer connection.+ seen = []++ def cross_threads():+ errors = []+ finished, release_thread = threading.Event(), threading.Event()++ def execute(wait):+ try:+ database.execute(lambda c: seen.append(c))+ except BaseException as exc:+ errors.append(exc)+ finally:+ if wait:+ finished.set()+ assert release_thread.wait(5)++ thread = threading.Thread(target=execute, args=(True,))+ thread.start()+ try:+ assert finished.wait(3)+ execute(False)+ finally:+ release_thread.set()+ thread.join(3)+ assert not thread.is_alive()+ assert not errors++ await database.work(cross_threads)+ assert seen[0] is seen[1]+++ "failure", ["begin", "callback", "commit", "rollback", "closed"]+)+async def test_failed_transactions_discard_the_connection(+ database_factory, tmp_path, failure+):+ database = database_factory(tmp_path / "fcc.db", tmp_path / "fcc.lock")+ await database.start()+ observed = []++ def authorize(action, arg1, arg2, name, trigger):+ denied = {"begin": "BEGIN", "commit": "COMMIT", "rollback": "ROLLBACK"}+ if action == sqlite3.SQLITE_TRANSACTION and arg1 == denied.get(failure):+ return sqlite3.SQLITE_DENY+ return sqlite3.SQLITE_OK++ def configure(connection):+ observed.append(connection)+ connection.set_authorizer(authorize)++ # Configure before BEGIN without wrapping/replacing sqlite3's transaction logic.+ def configure_lease():+ with closing(database._writer.connect()) as lease:+ configure(lease.driver_connection)++ await database.work(configure_lease)++ def fail(connection):+ connection.execute("INSERT INTO code_deleted VALUES ('failed')").close()+ if failure == "closed":+ connection.close()+ if failure in ("callback", "rollback", "closed"):+ raise ValueError("original callback failure")++ error_type = (+ ValueError+ if failure in ("callback", "rollback", "closed")+ else sqlite3.DatabaseError+ )+ with pytest.raises(error_type) as error:+ await database.run(fail)+ if failure in ("rollback", "closed"):+ assert isinstance(error.value.__cause__, sqlite3.Error)+ with pytest.raises(sqlite3.ProgrammingError, match="closed"):+ observed[0].execute("SELECT 1")++ def verify(connection):+ assert connection is not observed[0]+ assert (+ connection.execute("SELECT count(*) FROM code_deleted").fetchone()[0] == 0+ )+ connection.execute("INSERT INTO code_deleted VALUES ('success')").close()++ await database.run(verify)+++async def test_checkout_timeout_uses_sqlite_error_contract(+ database_factory, tmp_path, monkeypatch+):+ database = database_factory(tmp_path / "fcc.db", tmp_path / "fcc.lock")+ await database.start()++ def timeout():+ raise PoolTimeoutError("injected capacity timeout")++ monkeypatch.setattr(database._writer, "connect", timeout)+ with pytest.raises(sqlite3.OperationalError, match="capacity") as error:+ await database.run(lambda c: pytest.fail("callback must not run"))+ assert isinstance(error.value.__cause__, PoolTimeoutError)++[email protected]("cancel", [False, True])+async def test_startup_failure_disposes_connections_before_releasing_owner(+ tmp_path, cancel+):+ entered, release = threading.Event(), threading.Event()+ observed = []++ class GatedDatabase(sqlite_database.SQLiteDatabase):+ def _initialize(self):+ super()._initialize()+ self.execute(lambda c: observed.append(c))+ entered.set()+ assert release.wait(10)+ if not cancel:+ raise sqlite3.OperationalError("injected startup failure")++ database = GatedDatabase(tmp_path / "fcc.db", tmp_path / "fcc.lock")+ starting = asyncio.create_task(database.start())+ try:+ assert await asyncio.to_thread(entered.wait, 3)+ if cancel:+ starting.cancel()+ competing = sqlite_database.SQLiteDatabase(database.path, tmp_path / "fcc.lock")+ with pytest.raises(sqlite3.OperationalError, match="another FCC"):+ await competing.start()+ await competing.close()+ finally:+ release.set()+ try:+ with pytest.raises(+ asyncio.CancelledError if cancel else sqlite3.OperationalError+ ):+ await starting+ with pytest.raises(sqlite3.ProgrammingError, match="closed"):+ observed[0].execute("SELECT 1")+ reopened = sqlite_database.SQLiteDatabase(database.path, tmp_path / "fcc.lock")+ try:+ await reopened.start()+ finally:+ await reopened.close()+ finally:+ await database.close()+++def test_connection_setup_failure_closes_raw_connection(tmp_path, monkeypatch):+ connect = sqlite3.connect+ observed = []++ class FailedSetup(sqlite3.Connection):+ def execute(self, sql, *args):+ if sql.startswith("PRAGMA foreign_keys"):+ raise sqlite3.OperationalError("injected configuration failure")+ return super().execute(sql, *args)++ def create(*args, **kwargs):+ connection = connect(*args, **kwargs, factory=FailedSetup)+ observed.append(connection)+ return connection++ monkeypatch.setattr(sqlite_database.sqlite3, "connect", create)+ with pytest.raises(sqlite3.OperationalError, match="configuration"):+ sqlite_database._connect(tmp_path / "fcc.db")+ with pytest.raises(sqlite3.ProgrammingError, match="closed"):+ observed[0].execute("SELECT 1")+++async def test_partial_pool_creation_cleans_up_writer(tmp_path, monkeypatch):+ database = sqlite_database.SQLiteDatabase(+ tmp_path / "fcc.db", tmp_path / "fcc.lock"+ )+ create_pool = database._pool+ observed = []++ def partial(size):+ if size == 4:+ raise RuntimeError("reader pool creation failed")+ pool = create_pool(size)+ with closing(pool.connect()) as lease:+ observed.append(lease.driver_connection)+ return pool++ monkeypatch.setattr(database, "_pool", partial)+ try:+ with pytest.raises(RuntimeError, match="reader pool"):+ await database.start()+ with pytest.raises(sqlite3.ProgrammingError, match="closed"):+ observed[0].execute("SELECT 1")+ other = sqlite_database.SQLiteDatabase(database.path, tmp_path / "fcc.lock")+ try:+ await other.start()+ finally:+ await other.close()+ finally:+ await database.close()+++async def test_disposal_failure_retains_owner_and_can_be_retried(+ database_factory, tmp_path, monkeypatch+):+ database = database_factory(tmp_path / "fcc.db", tmp_path / "fcc.lock")+ await database.start()+ reader_connections = []+ await database.run(lambda c: reader_connections.append(c), write=False)+ writer = database._writer+ dispose = writer.dispose++ def fail():+ raise RuntimeError("injected disposal failure")++ monkeypatch.setattr(writer, "dispose", fail)+ try:+ with pytest.raises(RuntimeError, match="disposal"):+ await database.close()+ with pytest.raises(sqlite3.ProgrammingError, match="closed"):+ reader_connections[0].execute("SELECT 1")+ competing = database_factory(database.path, tmp_path / "fcc.lock")+ with pytest.raises(sqlite3.OperationalError, match="another FCC"):+ await competing.start()+ finally:+ monkeypatch.setattr(writer, "dispose", dispose)+ await database.close()+ reopened = database_factory(database.path, tmp_path / "fcc.lock")+ await reopened.start()++def historical_database(path: Path, version: int) -> None:path.parent.mkdir(parents=True, exist_ok=True)schema = (Path(__file__).parent / "fixtures/code_schema_v0.sql").read_text()diff --git a/uv.lock b/uv.lockindex 7ccc1a04..794399ea 100644--- a/uv.lock+++ b/uv.lock@@ -626,6 +626,7 @@ dependencies = [{ name = "python-telegram-bot" },{ name = "requests", extra = ["socks"] },{ name = "simplejson" },+ { name = "sqlalchemy" },{ name = "tiktoken" },{ name = "tomlkit" },{ name = "uvicorn" },@@ -680,6 +681,7 @@ requires-dist = [{ name = "python-telegram-bot", specifier = ">=22.8" },{ name = "requests", extras = ["socks"], specifier = ">=2.34.2" },{ name = "simplejson", specifier = ">=4.1.2" },+ { name = "sqlalchemy", specifier = ">=2.1.1" },{ name = "tiktoken", specifier = ">=0.13.0" },{ name = "tomlkit", specifier = ">=0.13.3" },{ name = "torch", marker = "extra == 'voice-local'", specifier = ">=2.14.0", index = "https://download.pytorch.org/whl/cu130" },@@ -2480,6 +2482,38 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/3c/1b/f84a2570a74094e921bbad5450b2a22a85d58585916e131d9b98029c3e69/soxr-1.0.0-cp314-cp314t-win_amd64.whl", hash = "sha256:a39b519acca2364aa726b24a6fd55acf29e4c8909102e0b858c23013c38328e5", size = 184850, upload-time = "2025-09-07T13:22:14.068Z" },]+[[package]]+name = "sqlalchemy"+version = "2.1.1"+source = { registry = "https://pypi.org/simple" }+dependencies = [+ { name = "typing-extensions" },+]+sdist = { url = "https://files.pythonhosted.org/packages/a8/cb/7c68da82239ffae9378eca076afe14c310e17a4f01e3d315112f266e26c7/sqlalchemy-2.1.1.tar.gz", hash = "sha256:fcf3cbb33bb23bad75d2150157c21804618745731e9931ab27e61879a9f6123b", size = 10503203, upload-time = "2026-09-25T14:59:00.117Z" }+wheels = [+ { url = "https://files.pythonhosted.org/packages/19/6e/3739dc61a0bd20db049f1b220cf380d55cc6c6e6e80a9cab382b3fe2d826/sqlalchemy-2.1.1-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:597e1edf9cbe6ce63974813000c02c7a44da3d21f1da47f07174732ba188e8ae", size = 2455544, upload-time = "2026-09-25T15:12:50.464Z" },+ { url = "https://files.pythonhosted.org/packages/51/9b/29879ca69033f466da33486d6fb238ce1f4465e15529959e5bd3a47a8f8d/sqlalchemy-2.1.1-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:e37b1fe2e080bf47f3b693dfbec9aa39d3c3d52bfac3b4fef290a449570bcf3f", size = 4571544, upload-time = "2026-09-25T15:19:00.881Z" },+ { url = "https://files.pythonhosted.org/packages/41/0f/6db5347662a9ed83509a5236e64afc15285947ed4dd71e618912bde83445/sqlalchemy-2.1.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:18148691d761ef20a92cfc7dced305834e2c6ab4f4f3648f66f5d4028e234089", size = 4601803, upload-time = "2026-09-25T15:19:55.751Z" },+ { url = "https://files.pythonhosted.org/packages/77/e4/073eecd01e8a1bb5b90638c15f06b86c897518ef96d70118a70ab37bb673/sqlalchemy-2.1.1-cp314-cp314-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:44dff425e7d88b04497b4317478a6af2a694327a087e6f1670a4266180838269", size = 4295583, upload-time = "2026-09-25T16:17:56.955Z" },+ { url = "https://files.pythonhosted.org/packages/25/71/c4e261d50297457c2496bb39b03f2a2ea0578a298defe4edc5ef514d4423/sqlalchemy-2.1.1-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:6a261e2244fa88e6eded95a078550c0584da55db22d49b0b2db8c075788282fb", size = 4501686, upload-time = "2026-09-25T15:19:02.804Z" },+ { url = "https://files.pythonhosted.org/packages/7c/85/9ec520ba12027bfdbcaa3e6633e977d1984dca5739ab73bc6c9f16cb4900/sqlalchemy-2.1.1-cp314-cp314-musllinux_1_2_riscv64.whl", hash = "sha256:4789593e8979d7c946da1985a560200b6ef6c3dc6dcc5442985d62c6f2908875", size = 4296847, upload-time = "2026-09-25T16:17:59.584Z" },+ { url = "https://files.pythonhosted.org/packages/dd/65/877c7f19fe976dffbffc579148d842bb99665e520af83d28b287a162f843/sqlalchemy-2.1.1-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:1477f2f1f531697753a729dd8f3e4e98f7019086f001fdc4d50350c929871c8c", size = 4560888, upload-time = "2026-09-25T15:19:57.887Z" },+ { url = "https://files.pythonhosted.org/packages/0c/f3/feb8b0803a6a2d095f4330cdc598aacef46cfb1ab6e83f4b396c6f5b89cb/sqlalchemy-2.1.1-cp314-cp314-win32.whl", hash = "sha256:1ef7bc38d140b3cf08cc6e8823b8e76da2e188b5594997e2c16586c9778af472", size = 2373288, upload-time = "2026-09-25T15:20:32.159Z" },+ { url = "https://files.pythonhosted.org/packages/be/b6/eef7d11752b5b52ad3fecfe03fabf7324e676bf1db5a22788cbcb3ba69c6/sqlalchemy-2.1.1-cp314-cp314-win_amd64.whl", hash = "sha256:73b0f7cb41a3df79dc0427377a9e2a099eb15995a327120d651f21309faa662a", size = 2423714, upload-time = "2026-09-25T15:20:34.012Z" },+ { url = "https://files.pythonhosted.org/packages/22/a0/6ce78eaac3a3197847a1d184a24633869cc343b0885f9207aa605e916bf5/sqlalchemy-2.1.1-cp314-cp314-win_arm64.whl", hash = "sha256:a0421f9eedad616b04a097ae3f155c5a0a31380364369861fd6e0f46074c3fce", size = 2388489, upload-time = "2026-09-25T15:25:20.78Z" },+ { url = "https://files.pythonhosted.org/packages/b1/56/db8f82c576dbe4f52db82e0e430debca0a6398fd0bf6a45bde11e7feb126/sqlalchemy-2.1.1-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:2f0bb0502de28baed425278d4bd98658d8256419fd9ad6ca964138710f933aa0", size = 2494990, upload-time = "2026-09-25T15:27:27.737Z" },+ { url = "https://files.pythonhosted.org/packages/d8/e0/cbadd9f851fa376c619f97adb4abd4b0c9d727841e988c4283dc45007c1f/sqlalchemy-2.1.1-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:e20233c7d000c006a03f3734d8e7a6e63aa878fcd3482c1f412c864a0992e5a0", size = 4899710, upload-time = "2026-09-25T15:28:30.224Z" },+ { url = "https://files.pythonhosted.org/packages/e2/5f/8cb1a5c751161cdac50355b2c91617e2a872c237922a54b47ce014f11dd3/sqlalchemy-2.1.1-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:8b0660ded5f9c090f9bf49fba78d52843ff3ca8c10f71624811f42f11655762d", size = 4810110, upload-time = "2026-09-25T15:49:02.468Z" },+ { url = "https://files.pythonhosted.org/packages/6b/4f/a4329f5a006b6cd2e8836b160d179c522c0cd7edfd494971a6d5a504f33e/sqlalchemy-2.1.1-cp314-cp314t-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:63c0291bf0f721543db3719c71f2d5ad6649f12abbcbc8324d42805dac112cbc", size = 4490328, upload-time = "2026-09-25T17:35:02.971Z" },+ { url = "https://files.pythonhosted.org/packages/4c/40/7bfd8136b8038bc1ffad70f283bd51aa93e43808ea522dac5a99be7b26b3/sqlalchemy-2.1.1-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:092a743f28f3a660321e9b093144b6a5b16f08dbb9b2f52466b47a2a128475c2", size = 4757854, upload-time = "2026-09-25T15:28:32.778Z" },+ { url = "https://files.pythonhosted.org/packages/ab/4b/a25d5f0e8493fa8ad2334a7100479302cd1928f788c6200faa952b47ee27/sqlalchemy-2.1.1-cp314-cp314t-musllinux_1_2_riscv64.whl", hash = "sha256:fdef70d59afce463bedef54580ba6a832f9f2313c1d0054e15cbb4ff46f37a97", size = 4490016, upload-time = "2026-09-25T17:35:05.664Z" },+ { url = "https://files.pythonhosted.org/packages/20/74/075ae28f89ba586a2229dc746aa8b4e5b0b2595f228c96bacadf66940bf4/sqlalchemy-2.1.1-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:0f94929be35def6580cf161f20c71930d9364d62f7040b6aed02b13fd3ca09e6", size = 4748267, upload-time = "2026-09-25T15:49:05.376Z" },+ { url = "https://files.pythonhosted.org/packages/57/fd/744a9e6d2788d1d4a077ed711d06cf503762ee8a614f887e284f0869beba/sqlalchemy-2.1.1-cp314-cp314t-win32.whl", hash = "sha256:dabf6a73f2cc1fa2155b29f2a8a85afd82d8613262e6054cc3c932f5dee5d474", size = 2431659, upload-time = "2026-09-25T15:25:37.802Z" },+ { url = "https://files.pythonhosted.org/packages/c7/11/f4a73cb472cf1401ba3f47b02eed7983a0545f0347582afd469d5da2f324/sqlalchemy-2.1.1-cp314-cp314t-win_amd64.whl", hash = "sha256:3353333f8c25fc5e0661f54630238a17b0d6600305e224400cd4642cce4af34f", size = 2499109, upload-time = "2026-09-25T15:25:39.706Z" },+ { url = "https://files.pythonhosted.org/packages/ee/54/21ceb7c4ad51255be2333aa4fb32c973d35fa28b26f9895c6d66767aff4d/sqlalchemy-2.1.1-cp314-cp314t-win_arm64.whl", hash = "sha256:7f15820c84b2377bcf02cf754232690ac2ad90d425978bf1e5ba30cc04f94d55", size = 2417171, upload-time = "2026-09-25T15:20:58.386Z" },+ { url = "https://files.pythonhosted.org/packages/c5/7a/7d1879d78c086737acee5f3250cbff9d285e5bd7cb6bc2c730f24e8d400e/sqlalchemy-2.1.1-py3-none-any.whl", hash = "sha256:4357c1a222e141662251a59d3702f9b124294941171dfa6bd570513e9f4905ee", size = 2047471, upload-time = "2026-09-25T15:20:37.797Z" },+]+[[package]]name = "starlette"version = "0.52.1"