Files
LLeMbas/src/lembas/db/session.py
T
Jaroslav Beneš 1d3f6c450b Users, groups, permissions, model settings and reasoning display
Four features, plus the schema machinery they needed.

**Schema sync.** The first live instance had data in it, and create_all
only creates missing *tables* -- a new column silently never appeared.
db/migrations.py now diffs the declared models against the database and
ALTER TABLE ... ADD COLUMN for what is missing, deriving a backfill
default from the column type (SQLite refuses a NOT NULL column without
one, and a Python-side `default=dict` cannot be expressed in DDL).
Verified against a copy of the live database: eight changes applied, all
rows preserved, second run a no-op. Renames, drops and retypes are still
manual and say so.

**Permissions.** A flat set of named booleans: an instance baseline
widened by each group the user belongs to. A group grants and never
denies -- with denies, "why can this user not do X" cannot be answered
without simulating every group. Admins bypass entirely, because an admin
can grant it back to themselves in two clicks and pretending otherwise
is theatre. Model *access* is separate: public, or granted to groups.
The picker is not the boundary -- switching a chat to a model you cannot
reach is a 403.

**Model settings.** Ordering, pinned-first, an instance default and a
per-user default, display names, descriptions, capability flags, and
uploaded images. Images are stored and served locally rather than by
URL: a remote URL makes every page render a request to a third party.
Uploads are validated by magic number, not the declared content type,
and stored under a random name. Models with no image get a generated
initial whose hue is derived from the model id, so it is stable.

**Reasoning display.** Streams into its own collapsible block above the
answer, labelled "Thought for 14 seconds", collapsed once finished, and
never replayed as context on the next turn. Two sources: the
reasoning_content delta field, and <think> tags inline in content -- the
latter needs a streaming splitter because the tags arrive split across
chunks. Models emitting no reasoning show nothing, via a :has() rule
rather than JavaScript. Verified against qwen35-9b on llama-swap: 694
reasoning events, 52 answer tokens, cleanly separated.

Two bugs found and fixed while testing:

- A bare `Mapped[list]` relationship is treated by SQLAlchemy as a scalar
  and returns None instead of []. It needs the element type.
- FastAPI substitutes the default for an empty form value, so with
  `x: str | None = Form(None)` a submitted `x=` is indistinguishable from
  an absent field. That silently broke clearing a system prompt or a
  temperature. update_chat now reads the raw form and checks key presence.

143 tests, ruff clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-21 11:49:32 +02:00

104 lines
3.1 KiB
Python

"""Engine, session factory and startup schema creation."""
from __future__ import annotations
import logging
from collections.abc import Iterator
from contextlib import contextmanager
from sqlalchemy import Engine, create_engine, event
from sqlalchemy.orm import Session, sessionmaker
from lembas.config import settings
log = logging.getLogger(__name__)
_engine: Engine | None = None
_SessionFactory: sessionmaker[Session] | None = None
@event.listens_for(Engine, "connect")
def _configure_sqlite(dbapi_connection, connection_record) -> None: # noqa: ANN001
"""Apply the pragmas SQLite needs to behave under a concurrent web server.
- WAL lets readers proceed while a write is in flight, which matters because
a streaming reply holds a write open for the length of the generation.
- foreign_keys is OFF by default in SQLite, so every ondelete= in the models
would be decoration without this.
- busy_timeout makes concurrent writers wait rather than fail instantly.
"""
cursor = dbapi_connection.cursor()
cursor.execute("PRAGMA journal_mode=WAL")
cursor.execute("PRAGMA foreign_keys=ON")
cursor.execute("PRAGMA busy_timeout=5000")
cursor.execute("PRAGMA synchronous=NORMAL")
cursor.close()
def get_engine() -> Engine:
global _engine
if _engine is None:
settings.ensure_dirs()
_engine = create_engine(
f"sqlite:///{settings.db_path}",
# FastAPI runs sync endpoints in a threadpool, so a connection can
# legitimately be used from a thread other than the one that made it.
connect_args={"check_same_thread": False},
echo=False,
future=True,
)
return _engine
def get_session_factory() -> sessionmaker[Session]:
global _SessionFactory
if _SessionFactory is None:
_SessionFactory = sessionmaker(
bind=get_engine(),
autoflush=False,
expire_on_commit=False,
)
return _SessionFactory
def init_db() -> None:
"""Bring the database up to the declared schema.
Creates missing tables and adds missing columns -- see db/migrations.py for
what that does and does not cover. Additive changes need nothing else;
renames, drops and retypes are still a hand job.
"""
from lembas.db.migrations import sync_schema
changes = sync_schema(get_engine())
if changes:
log.info("database schema updated: %s", ", ".join(changes))
log.debug("schema ensured at %s", settings.db_path)
@contextmanager
def session_scope() -> Iterator[Session]:
"""Transactional scope for background work and CLI commands.
Request handlers should use the `db` dependency in lembas.api.deps instead.
"""
factory = get_session_factory()
session = factory()
try:
yield session
session.commit()
except Exception:
session.rollback()
raise
finally:
session.close()
def reset_engine() -> None:
"""Drop cached engine/factory. Used by tests to rebind to a temp database."""
global _engine, _SessionFactory
if _engine is not None:
_engine.dispose()
_engine = None
_SessionFactory = None