mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 10:09:45 +08:00
`_begin_turn_sync` refuses a new turn while the session owns any row in queued/running/waiting_input. That is right while a turn is alive and catastrophic once one is not: a single row left behind by a lost worker, a dropped socket or a restart blocked every later message in that session, with no way out from the UI. Reporters described it precisely — one window, no refresh, a conversation that never accepted another word (#1297, #1359). v1.6.7 closed the paths that manufacture those rows, but said nothing about the ones already there. It shipped with "existing rows still need clearing by hand" as a known issue, because the obvious fix — a periodic sweep — needs an age threshold or it cancels a turn between its INSERT and its first lease renewal, the worst failure a chat product has. Reclaiming at the point of conflict removes that hazard rather than managing it. The only rows ever considered are ones already blocking a real request, so there is no window to get wrong, no timer, and no threshold to guess. On a restart the leases are gone, so the first message the user sends clears the row and goes through — the outcome the sweep was for, without the sweep. Liveness is the lease, never the row's age. A turn parked on an `ask_user` card emits no events, so `updated_at` stops advancing while a person is simply taking their time to answer; a live worker renews its lease every few seconds whether or not it is producing output. So "no lease" is the one signal that separates a zombie from a slow reader, and a turn whose lease is live still gets its conflict — pinned by a test, because that is what keeps this from becoming a way to run two turns on one session. Also names the exception. Both stores raised a bare RuntimeError with the conflict spelled in the message; `ActiveTurnConflict` subclasses it, so every existing handler keeps working, and a caller that can do something about a stale row no longer has to match on text. 6921 passed, 24 skipped.
217 lines
6.5 KiB
Python
217 lines
6.5 KiB
Python
"""
|
|
Structural protocol for session stores.
|
|
|
|
Both SQLiteSessionStore and PocketBaseSessionStore satisfy this protocol,
|
|
allowing the rest of the codebase to be store-agnostic.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, Protocol, runtime_checkable
|
|
|
|
|
|
class ActiveTurnConflict(RuntimeError):
|
|
"""A session already owns a turn that has not reached a terminal state.
|
|
|
|
Named rather than a bare ``RuntimeError`` so a caller can tell "this
|
|
session is busy" from any other runtime failure without matching on the
|
|
message. Subclasses ``RuntimeError`` because every existing handler
|
|
catches that, and the message is unchanged for the same reason.
|
|
|
|
``turn_id`` is the row that blocks the session, when the store knows it;
|
|
the unique-index race reports the session instead, having lost the race
|
|
to read it.
|
|
"""
|
|
|
|
def __init__(self, message: str, *, turn_id: str = "") -> None:
|
|
super().__init__(message)
|
|
self.turn_id = turn_id
|
|
|
|
|
|
@runtime_checkable
|
|
class SessionRepository(Protocol):
|
|
async def migrate_workspace_preferences(self) -> int: ...
|
|
|
|
async def create_session(
|
|
self,
|
|
title: str | None = None,
|
|
session_id: str | None = None,
|
|
) -> dict[str, Any]: ...
|
|
|
|
async def get_session(self, session_id: str) -> dict[str, Any] | None: ...
|
|
|
|
async def ensure_session(self, session_id: str | None = None) -> dict[str, Any]: ...
|
|
|
|
async def import_legacy_session(
|
|
self,
|
|
session_id: str,
|
|
title: str,
|
|
created_at: float,
|
|
updated_at: float,
|
|
preferences: dict[str, Any],
|
|
messages: list[dict[str, Any]],
|
|
) -> dict[str, Any]: ...
|
|
|
|
async def list_sessions(self, limit: int = 50, offset: int = 0) -> list[dict[str, Any]]: ...
|
|
|
|
async def update_session_title(self, session_id: str, title: str) -> bool: ...
|
|
|
|
async def delete_session(self, session_id: str) -> bool: ...
|
|
|
|
|
|
@runtime_checkable
|
|
class TurnRepository(Protocol):
|
|
async def begin_turn(
|
|
self,
|
|
session_id: str,
|
|
capability: str = "",
|
|
*,
|
|
turn_id: str | None = None,
|
|
owner_id: str = "",
|
|
fencing_token: int = 0,
|
|
) -> dict[str, Any]: ...
|
|
|
|
async def get_turn(self, turn_id: str) -> dict[str, Any] | None: ...
|
|
|
|
async def get_active_turn(self, session_id: str) -> dict[str, Any] | None: ...
|
|
|
|
async def list_active_turns(self, session_id: str) -> list[dict[str, Any]]: ...
|
|
|
|
async def list_nonterminal_turns(self) -> list[dict[str, Any]]: ...
|
|
|
|
async def transition_turn(
|
|
self,
|
|
turn_id: str,
|
|
status: str,
|
|
*,
|
|
expected_status: str | None = None,
|
|
fencing_token: int | None = None,
|
|
error: str = "",
|
|
failure_code: str = "",
|
|
retryable: bool = False,
|
|
) -> bool: ...
|
|
|
|
async def append_events(
|
|
self,
|
|
turn_id: str,
|
|
events: list[dict[str, Any]],
|
|
*,
|
|
fencing_token: int | None = None,
|
|
) -> list[dict[str, Any]]: ...
|
|
|
|
async def get_events(self, turn_id: str, after_seq: int = 0) -> list[dict[str, Any]]: ...
|
|
|
|
|
|
@runtime_checkable
|
|
class MessageRepository(Protocol):
|
|
async def add_message(
|
|
self,
|
|
session_id: str,
|
|
role: str,
|
|
content: str,
|
|
capability: str = "",
|
|
events: list[dict[str, Any]] | None = None,
|
|
attachments: list[dict[str, Any]] | None = None,
|
|
metadata: dict[str, Any] | None = None,
|
|
parent_message_id: int | str | None = None,
|
|
) -> int | str: ...
|
|
|
|
async def get_messages(self, session_id: str) -> list[dict[str, Any]]: ...
|
|
|
|
|
|
@runtime_checkable
|
|
class SessionStoreProtocol(SessionRepository, TurnRepository, MessageRepository, Protocol):
|
|
async def migrate_workspace_preferences(self) -> int: ...
|
|
|
|
async def create_session(
|
|
self,
|
|
title: str | None = None,
|
|
session_id: str | None = None,
|
|
) -> dict[str, Any]: ...
|
|
|
|
async def get_session(self, session_id: str) -> dict[str, Any] | None: ...
|
|
|
|
async def ensure_session(
|
|
self,
|
|
session_id: str | None = None,
|
|
) -> dict[str, Any]: ...
|
|
|
|
async def create_turn(self, session_id: str, capability: str = "") -> dict[str, Any]: ...
|
|
|
|
async def get_turn(self, turn_id: str) -> dict[str, Any] | None: ...
|
|
|
|
async def get_active_turn(self, session_id: str) -> dict[str, Any] | None: ...
|
|
|
|
async def list_active_turns(self, session_id: str) -> list[dict[str, Any]]: ...
|
|
|
|
async def update_turn_status(self, turn_id: str, status: str, error: str = "") -> bool: ...
|
|
|
|
async def append_turn_event(self, turn_id: str, event: dict[str, Any]) -> dict[str, Any]: ...
|
|
|
|
async def get_turn_events(self, turn_id: str, after_seq: int = 0) -> list[dict[str, Any]]: ...
|
|
|
|
async def link_turn_message(self, turn_id: str, assistant_message_id: int | str) -> bool: ...
|
|
|
|
async def get_message_trace(
|
|
self,
|
|
session_id: str,
|
|
message_id: int | str,
|
|
after_seq: int = 0,
|
|
limit: int | None = None,
|
|
) -> dict[str, Any] | None: ...
|
|
|
|
async def update_session_title(self, session_id: str, title: str) -> bool: ...
|
|
|
|
async def delete_session(self, session_id: str) -> bool: ...
|
|
|
|
async def add_message(
|
|
self,
|
|
session_id: str,
|
|
role: str,
|
|
content: str,
|
|
capability: str = "",
|
|
events: list[dict[str, Any]] | None = None,
|
|
attachments: list[dict[str, Any]] | None = None,
|
|
metadata: dict[str, Any] | None = None,
|
|
parent_message_id: int | str | None = None,
|
|
) -> int | str: ...
|
|
|
|
async def delete_message(self, message_id: int | str) -> bool: ...
|
|
|
|
async def get_last_message(
|
|
self, session_id: str, role: str | None = None
|
|
) -> dict[str, Any] | None: ...
|
|
|
|
async def get_messages(self, session_id: str) -> list[dict[str, Any]]: ...
|
|
|
|
async def get_messages_for_context(
|
|
self, session_id: str, leaf_message_id: int | None = None
|
|
) -> list[dict[str, Any]]: ...
|
|
|
|
async def list_sessions(
|
|
self,
|
|
limit: int = 50,
|
|
offset: int = 0,
|
|
) -> list[dict[str, Any]]: ...
|
|
|
|
async def get_session_summaries(
|
|
self,
|
|
session_ids: list[str],
|
|
) -> list[dict[str, Any]]: ...
|
|
|
|
async def update_summary(self, session_id: str, summary: str, up_to_msg_id: int) -> bool: ...
|
|
|
|
async def update_session_preferences(
|
|
self, session_id: str, preferences: dict[str, Any]
|
|
) -> bool: ...
|
|
|
|
async def get_session_with_messages(self, session_id: str) -> dict[str, Any] | None: ...
|
|
|
|
|
|
__all__ = [
|
|
"MessageRepository",
|
|
"SessionRepository",
|
|
"SessionStoreProtocol",
|
|
"TurnRepository",
|
|
]
|