Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(workbench): re-assert backend lock inside the UPDATE predicate
cyha77a12d4ff3d
diff --git a/storage/workbench_sessions_service.py b/storage/workbench_sessions_service.pyindex ecb69852e..050070e53 100644--- a/storage/workbench_sessions_service.py+++ b/storage/workbench_sessions_service.py@@ -19,7 +19,7 @@ import osfrom datetime import datetime, timezonefrom typing import Any, Optional-from sqlalchemy import func, select, update+from sqlalchemy import func, or_, select, updatefrom sqlalchemy.engine import Connectionfrom storage.agent_session_rows import create_agent_session_row@@ -400,7 +400,36 @@ def update_session(if reasoning_effort is not _UNSET:values["reasoning_effort"] = reasoning_effort or None- conn.execute(update(agent_sessions).where(agent_sessions.c.id == session_id).values(**values))+ stmt = update(agent_sessions).where(agent_sessions.c.id == session_id)+ backend_changes = agent_backend is not _UNSET and str(agent_backend or "") != str(+ existing.agent_backend or ""+ )+ if backend_changes:+ # Re-assert the lock INSIDE the UPDATE: the guard above is read-then-+ # write, and the first turn's native bind can commit in between (the+ # SELECT runs before this statement takes the write lock). The predicate+ # makes the change atomic — it only lands while the session is still+ # unlocked (no native, or the legacy blank-backend escape) or the row+ # already converged to the requested backend.+ stmt = stmt.where(+ or_(+ func.coalesce(agent_sessions.c.native_session_id, "") == "",+ func.coalesce(agent_sessions.c.agent_backend, "") == "",+ agent_sessions.c.agent_backend == str(agent_backend or ""),+ )+ )+ result = conn.execute(stmt.values(**values))+ if result.rowcount == 0:+ current = conn.execute(+ select(agent_sessions.c.agent_backend).where(agent_sessions.c.id == session_id)+ ).first()+ if current is None or not backend_changes:+ raise LookupError(f"Session not found: {session_id}")+ raise SessionBackendLockedError(+ session_id=session_id,+ current_backend=current.agent_backend,+ requested_backend=agent_backend,+ )return get_session(conn, session_id)diff --git a/tests/test_core_services_sessions.py b/tests/test_core_services_sessions.pyindex 4e4b5b3c8..c083204ae 100644--- a/tests/test_core_services_sessions.py+++ b/tests/test_core_services_sessions.py@@ -475,6 +475,48 @@ def test_update_session_locks_backend_once_native_exists(isolated_state):sessions_service.update_session(conn, sid, agent_backend=None, agent_name=None)+def test_update_session_backend_switch_loses_race_with_native_bind(isolated_state):+ """The lock guard is read-then-write and the first turn's native bind can+ commit in between (the SELECT runs before the UPDATE takes the write lock).+ The UPDATE re-asserts the lock in its WHERE predicate, so a cross-backend+ switch that loses the race raises instead of stamping a backend that+ mismatches the backend owning the just-bound native."""++ from sqlalchemy import event++ engine = create_sqlite_engine()+ with engine.begin() as conn:+ scope_id = _seed_avibe_scope(conn)+ sid = sessions_service.create_session(+ conn, scope_id=scope_id, agent_backend="claude", agent_name="claude"+ )["id"]++ bind_engine = create_sqlite_engine()+ fired = {"done": False}++ def bind_native_before_update(_conn, _cursor, statement, _params, _context, _executemany):+ # Right before update_session's UPDATE executes — i.e. AFTER its lock+ # check read "no native yet" — land the first turn's native bind on a+ # separate connection, exactly the racing interleave.+ if fired["done"] or not statement.lstrip().upper().startswith("UPDATE AGENT_SESSIONS"):+ return+ fired["done"] = True+ with bind_engine.begin() as bind_conn:+ _bind_native(bind_conn, sid)++ event.listen(engine, "before_cursor_execute", bind_native_before_update)+ try:+ with engine.begin() as conn:+ with pytest.raises(sessions_service.SessionBackendLockedError):+ sessions_service.update_session(conn, sid, agent_backend="codex", agent_name="codex")+ finally:+ event.remove(engine, "before_cursor_execute", bind_native_before_update)++ assert fired["done"], "race interleave never triggered — test setup is broken"+ with engine.connect() as conn:+ assert sessions_service.get_session(conn, sid)["agent_backend"] == "claude"++def test_update_session_legacy_blank_backend_keeps_initial_pin_escape(isolated_state):"""Legacy agent-less rows whose native predates the bind-time backendbackfill don't know which backend owns their native; the empty -> concrete