Skip to content

armgit / avibe

Public Imported from GitHub
0

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.

fix(session): skip SQLite's writer when a Turn end has nothing left to change (#2718)

Call White089f517d8398
diff --git a/core/session_turns.py b/core/session_turns.py
index f1e5b3342..3302bcc07 100644
--- a/core/session_turns.py
+++ b/core/session_turns.py
@@ -4679,6 +4679,15 @@ class SessionTurnManager:
expected_head_version: int | None = None,
) -> bool:
turn_id: str | None = None
+ with self._sqlite_engine().connect() as conn:
+ # Most drains find nothing to claim -- every Turn end asks. Those answer
+ # from a read, as the claim below would, without taking the writer (#2706).
+ claimable = (
+ delivery_store.active_turn(conn, session_id) is None
+ and delivery_store.claimable_fifo_head(conn, session_id) is not None
+ )
+ if not claimable:
+ return False
with self._sqlite_engine().begin() as conn:
reserve_write_lock(conn)
turn_id = self._claim_fifo_batch_in_transaction(
@@ -4752,6 +4761,16 @@ class SessionTurnManager:
linked_activation_deferred = False
with self._sqlite_engine().connect() as conn:
observed_turn = delivery_store.get_turn(conn, turn_id)
+ if observed_turn is None or observed_turn["state"] == "terminal":
+ # Terminal is final, and a missing Turn stays missing: the answer the
+ # recheck under the writer would give, without the boundary or the writer.
+ # A Turn end reaches here again after its terminal write (#2706).
+ return {
+ "changed": False,
+ "successor_turn_id": None,
+ "delivery_id": None,
+ "defer_queue_resume": False,
+ }
owner_session_id = str((observed_turn or {}).get("session_id") or "")
owner_backend = str((observed_turn or {}).get("backend") or "")
with self._runtime_start_owner(owner_session_id, owner_backend) as start_owner, self._sqlite_engine().begin() as conn:
@@ -7448,8 +7467,8 @@ class SessionTurnManager:
return successor_id
async def _resume_post_terminal(self, session_id: str) -> None:
- with self._sqlite_engine().begin() as conn:
- reserve_write_lock(conn)
+ # A read: both continuations recheck under their own writer.
+ with self._sqlite_engine().connect() as conn:
owner = delivery_store.active_turn(conn, session_id)
if owner is not None:
await self._resume_durable_session(session_id)
diff --git a/tests/test_session_delivery_fsm.py b/tests/test_session_delivery_fsm.py
index 862fd1d24..4868f4019 100644
--- a/tests/test_session_delivery_fsm.py
+++ b/tests/test_session_delivery_fsm.py
@@ -8398,3 +8398,36 @@ def test_startup_recovery_reads_growing_tables_only_through_index_seeks(tmp_path
scans.append(f"{detail} <- {' '.join(statement.split())}")
assert statements, "recovery issued no statements, so this test proves nothing"
assert not scans, "startup recovery scans a growing table:\n" + "\n".join(scans)
+
+
+async def test_a_turn_end_after_its_terminal_write_takes_no_writer(managers) -> None:
+ """#2706: a Turn end reaches SQLite's writer from several owners on the controller loop: the evidence-carrying
+ terminal write, then the runner's release, the post-terminal resume and the queue drain. Once the Turn is
+ terminal and nothing is queued, the later three change nothing, and they answer from a read instead of waiting for
+ the writer another process may hold."""
+
+ from sqlalchemy import event
+
+ manager, _other, engine, _engine_b, _starts = managers
+ turn_id, context = await _activate(manager, text="request")
+ manager.on_terminal_result(context, is_error=False)
+ manager.on_terminal_delivery_complete(context)
+ with engine.connect() as conn:
+ assert delivery_store.get_turn(conn, turn_id)["state"] == "terminal"
+ writers: list[str] = []
+
+ def capture(_conn, _cursor, statement, _parameters, _context, _executemany):
+ if statement.lstrip().upper().startswith(("BEGIN IMMEDIATE", "UPDATE AGENT_SESSIONS SET ID = ID")):
+ writers.append(statement)
+
+ event.listen(engine, "before_cursor_execute", capture)
+ try:
+ released = manager._terminalize_durable_turn(turn_id, "completed", evidence_kind="runner_release")
+ await manager._resume_post_terminal("ses_fsm")
+ drained = await manager.drain_delivery_queue("ses_fsm")
+ finally:
+ event.remove(engine, "before_cursor_execute", capture)
+
+ assert released["changed"] is False and drained is False
+ assert writers == [], f"a Turn end took SQLite's writer {len(writers)} more time(s) to change nothing"