Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(chat): make the chat lane a partial index on PostgreSQL
trohitgfff575838e61
diff --git a/server/core/database.py b/server/core/database.pyindex 2429c896..87cd602e 100644--- a/server/core/database.py+++ b/server/core/database.py@@ -102,6 +102,22 @@ ParameterMutator = Callable[]+def _rebuild_chat_lane_index(sync_connection) -> None:+ """Make PostgreSQL's ``ux_chat_runs_lane`` the partial index the model+ declares. Shared databases created before the model named its PostgreSQL+ condition got a plain ``UNIQUE(session_id)``, which refuses every run+ after a conversation's first."""+ definition = sync_connection.execute(text(+ "SELECT indexdef FROM pg_indexes WHERE schemaname = current_schema() AND indexname = 'ux_chat_runs_lane'"+ )).scalar()+ if definition is not None and " WHERE " in definition:+ return+ index = next(index for index in ChatRun.__table__.indexes if index.name == "ux_chat_runs_lane")+ if definition is not None:+ index.drop(sync_connection)+ index.create(sync_connection)++def _workflow_context_node_ids(data: Any) -> set[str]:if not isinstance(data, dict):return set()@@ -168,6 +184,8 @@ class Database:from models.browser_owners import BrowserProfileOwnerfor index in BrowserProfileOwner.__table__.indexes:await conn.run_sync(lambda sync, index=index: index.create(sync, checkfirst=True))+ if self.engine.dialect.name == "postgresql":+ await conn.run_sync(_rebuild_chat_lane_index)# Existing SQLite upgrades retain their exact backfills. PostgreSQL# starts with the current SQLModel schema; legacy SQLite data isdiff --git a/server/models/chat.py b/server/models/chat.pyindex 22e57771..c18cefec 100644--- a/server/models/chat.py+++ b/server/models/chat.py@@ -66,7 +66,8 @@ class ChatThread(SQLModel, table=True):class ChatRun(SQLModel, table=True):__tablename__ = "chat_runs"- __table_args__ = (Index("ux_chat_runs_lane", "session_id", unique=True, sqlite_where=text(_LANE_WHERE)),)+ __table_args__ = (Index("ux_chat_runs_lane", "session_id", unique=True,+ sqlite_where=text(_LANE_WHERE), postgresql_where=text(_LANE_WHERE)),)run_id: str = Field(primary_key=True, max_length=64)session_id: str = Field(index=True, max_length=255)diff --git a/server/tests/test_chat_lane_index.py b/server/tests/test_chat_lane_index.pynew file mode 100644index 00000000..c0364a17--- /dev/null+++ b/server/tests/test_chat_lane_index.py@@ -0,0 +1,96 @@+"""The chat lane (models/chat.py): at most one live run per session, on both+databases. A shared PostgreSQL database once got the lane as a plain+``UNIQUE(session_id)``, which refused every run after a conversation's+first; startup rebuilds that index as the declared partial one."""++import sys+from datetime import datetime, timezone++import pytest+from sqlalchemy import Index, text+from sqlalchemy.dialects import postgresql+from sqlalchemy.exc import IntegrityError+from sqlalchemy.schema import CreateIndex+from sqlmodel import SQLModel++from models.chat import ChatRun+++def _lane_index() -> Index:+ return next(index for index in ChatRun.__table__.indexes if index.name == "ux_chat_runs_lane")+++def test_lane_index_is_partial_on_postgresql():+ ddl = str(CreateIndex(_lane_index()).compile(dialect=postgresql.dialect()))++ assert "WHERE state IN ('queued', 'pending', 'running', 'stopping')" in ddl+++async def test_every_partial_index_names_its_condition_for_both_databases(real_database):+ for table in SQLModel.metadata.tables.values():+ for index in table.indexes:+ sqlite_where = index.dialect_options["sqlite"].get("where")+ postgresql_where = index.dialect_options["postgresql"].get("where")+ assert (sqlite_where is None) == (postgresql_where is None), index.name+ if sqlite_where is not None:+ assert str(sqlite_where) == str(postgresql_where), index.name+++class _Connection:+ def __init__(self, definition):+ self.definition = definition++ def execute(self, statement):+ return self++ def scalar(self):+ return self.definition+++ ("definition", "expected"),+ [+ ("CREATE UNIQUE INDEX ux_chat_runs_lane ON public.chat_runs USING btree (session_id)", ["drop", "create"]),+ (None, ["create"]),+ ("CREATE UNIQUE INDEX ux_chat_runs_lane ON public.chat_runs USING btree (session_id) WHERE ((state)::text = ANY "+ "((ARRAY['queued'::character varying])::text[]))", []),+ ],+)+async def test_startup_rebuilds_only_a_lane_index_without_its_condition(real_database, monkeypatch, definition, expected):+ rebuild = sys.modules[type(real_database).__module__]._rebuild_chat_lane_index+ calls = []+ monkeypatch.setattr(Index, "drop", lambda index, bind: calls.append("drop"))+ monkeypatch.setattr(Index, "create", lambda index, bind: calls.append("create"))++ rebuild(_Connection(definition))++ assert calls == expected+++async def _add_run(database, run_id: str, state: str) -> None:+ async with database.get_session() as session:+ session.add(ChatRun(run_id=run_id, session_id="session", state=state, created_at=datetime.now(timezone.utc)))+ await session.commit()+++async def test_a_conversation_takes_a_new_run_after_the_last_one_ended(real_database):+ await _add_run(real_database, "r1", "finished")+ await _add_run(real_database, "r2", "pending")++ with pytest.raises(IntegrityError):+ await _add_run(real_database, "r3", "running")+++async def test_postgresql_startup_rebuilds_a_plain_lane_index(real_database):+ if real_database.engine.dialect.name != "postgresql":+ pytest.skip("TEST_POSTGRES_URL is required for the PostgreSQL index rebuild")+ rebuild = sys.modules[type(real_database).__module__]._rebuild_chat_lane_index+ async with real_database.engine.begin() as connection:+ await connection.execute(text("DROP INDEX ux_chat_runs_lane"))+ await connection.execute(text("CREATE UNIQUE INDEX ux_chat_runs_lane ON chat_runs (session_id)"))+ await connection.run_sync(rebuild)+ definition = (await connection.execute(text(+ "SELECT indexdef FROM pg_indexes WHERE indexname = 'ux_chat_runs_lane'"))).scalar()++ assert " WHERE " in definition+ await test_a_conversation_takes_a_new_run_after_the_last_one_ended(real_database)