Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

fix: keep interrupted Langflow streams owned and resumable (#2527) (#2531)

Edwin Jose22e45cb07d54
diff --git a/alembic/versions/0008_session_conversation_id.py b/alembic/versions/0008_session_conversation_id.py
new file mode 100644
index 000000000..a28a6a34d
--- /dev/null
+++ b/alembic/versions/0008_session_conversation_id.py
@@ -0,0 +1,27 @@
+"""Persist canonical conversation IDs for session aliases.
+
+Revision ID: 0008_session_conversation_id
+Revises: 0007_add_knowledge_delete_anonymous
+"""
+
+import sqlalchemy as sa
+
+from alembic import op
+
+revision = "0008_session_conversation_id"
+down_revision = "0007_add_knowledge_delete_anonymous"
+branch_labels = None
+depends_on = None
+
+
+def upgrade() -> None:
+ op.add_column("session_ownership", sa.Column("conversation_id", sa.String(64), nullable=True))
+ op.create_index(
+ "ix_session_ownership_conversation_id", "session_ownership", ["conversation_id"]
+ )
+ op.execute("UPDATE session_ownership SET conversation_id = response_id")
+
+
+def downgrade() -> None:
+ op.drop_index("ix_session_ownership_conversation_id", table_name="session_ownership")
+ op.drop_column("session_ownership", "conversation_id")
diff --git a/docs/docs/core-components/chat.mdx b/docs/docs/core-components/chat.mdx
index 6ee554d9c..91255de7c 100644
--- a/docs/docs/core-components/chat.mdx
+++ b/docs/docs/core-components/chat.mdx
@@ -22,6 +22,26 @@ To add or change the available tools, you must edit the [**OpenRAG OpenSearch Ag
Try chatting, uploading documents, and modifying chat settings in the [quickstart](/quickstart).
:::
+## Interrupted chat responses
+
+If you stop a response or the connection closes while an answer is streaming, OpenRAG retains ownership of response IDs already sent to your client.
+You can send a follow-up in the same conversation without losing access solely because the previous stream was interrupted.
+The backend keeps the partial answer in the active conversation and records the updated conversation metadata when the stream is closed.
+An interrupted answer is not automatically regenerated, and ingestion or other tool calls are not automatically replayed.
+
+For API clients, `conversation_id` identifies the OpenRAG sidebar thread, while `previous_response_id` continues the Langflow session.
+These IDs can differ when Langflow returns a new response ID.
+Both IDs are checked against the authenticated user's session ownership; interruption recovery does not bypass those checks.
+
+Response aliases are durably associated with their canonical conversation, including across backend restarts.
+Deleting a conversation revokes its canonical ID and all recorded aliases and attempts to remove every associated Langflow session.
+A deleted alias cannot be used to resume the conversation.
+
+IDs are sent only after ownership and its conversation association have been persisted.
+In `files` mode this requires a successful file write; in `db` mode it requires a committed database write.
+In `hybrid` mode the database is preferred, with a successful durable JSON write as fallback if the database is unavailable.
+If neither configured destination can persist the claim, the stream returns an error without exposing a new response ID; an in-memory cache entry is not sufficient.
+
## OpenRAG OpenSearch Agent flow {#flow}
When you use the OpenRAG <Icon name="MessageSquare" aria-hidden="true"/> **Chat**, the **OpenRAG OpenSearch Agent** flow runs in the background to retrieve relevant information from your knowledge base and generate a response.
diff --git a/src/agent.py b/src/agent.py
index 4e01c9f8b..8172039af 100644
--- a/src/agent.py
+++ b/src/agent.py
@@ -1,6 +1,10 @@
+import asyncio
import uuid
+from contextlib import aclosing
from typing import Any
+import anyio
+
from config.config_manager import DEFAULT_SYSTEM_PROMPT
from services.conversation_persistence_service import conversation_persistence
from utils.langflow_utils import parse_knowledge_chunks, strip_untrusted_fence_recursive
@@ -91,13 +95,10 @@ async def _persist_error_conversation(
from services.session_ownership_service import session_ownership_service
+ if not await session_ownership_service.claim_session(user_id, store_id):
+ raise RuntimeError("Unable to establish chat session ownership")
conversation_state["last_activity"] = datetime.now()
await store_conversation_thread(user_id, store_id, conversation_state)
- try:
- await session_ownership_service.claim_session(user_id, store_id)
- logger.debug(f"Claimed session {store_id} for user {user_id} after stream error")
- except Exception as e:
- logger.warning(f"Failed to claim session ownership after stream error: {e}")
def _stream_error_chunk(message: str, response_id: str | None = None) -> bytes:
@@ -228,6 +229,7 @@ async def async_response_stream(
):
logger.info("User prompt received", prompt=prompt)
+ response = None
try:
# Build request parameters
request_params = {
@@ -422,6 +424,10 @@ async def async_response_stream(
except Exception:
logger.exception("[AGENT] Streaming failed")
raise
+ finally:
+ if response is not None:
+ with anyio.CancelScope(shield=True):
+ await response.close()
# Generic async response function for non-streaming
@@ -494,15 +500,18 @@ async def async_stream(
previous_response_id: str = None,
log_prefix: str = "response",
):
- async for chunk in async_response_stream(
- client,
- prompt,
- model,
- extra_headers=extra_headers,
- previous_response_id=previous_response_id,
- log_prefix=log_prefix,
- ):
- yield chunk
+ async with aclosing(
+ async_response_stream(
+ client,
+ prompt,
+ model,
+ extra_headers=extra_headers,
+ previous_response_id=previous_response_id,
+ log_prefix=log_prefix,
+ )
+ ) as stream:
+ async for chunk in stream:
+ yield chunk
# Async langflow function (non-streaming only)
@@ -718,6 +727,7 @@ async def async_chat_stream(
await _persist_error_conversation(user_id, store_id, conversation_state)
except Exception as store_error:
logger.error(f"Failed to store error conversation: {store_error}")
+ store_id = None
yield _stream_error_chunk(display_text, store_id)
@@ -850,18 +860,13 @@ async def async_langflow_chat(
# Store the conversation thread with its response_id
if response_id:
+ from services.session_ownership_service import session_ownership_service
+
+ if not await session_ownership_service.claim_session(user_id, response_id):
+ raise RuntimeError("Unable to establish chat session ownership")
conversation_state["last_activity"] = datetime.now()
await store_conversation_thread(user_id, response_id, conversation_state)
- # Claim session ownership for this user
- try:
- from services.session_ownership_service import session_ownership_service
-
- await session_ownership_service.claim_session(user_id, response_id)
- logger.debug(f"Claimed session {response_id} for user {user_id}")
- except Exception as e:
- logger.warning(f"Failed to claim session ownership: {e}")
-
logger.debug(
"Stored langflow conversation thread",
user_id=user_id,
@@ -894,6 +899,16 @@ async def async_langflow_chat_stream(
# conversation_id keeps the OpenRAG sidebar thread; previous_response_id chains
# the Langflow session. After errors the client omits previous_response_id.
thread_id = conversation_id or previous_response_id
+ if not _conversation_thread_in_memory(user_id, thread_id):
+ # Clients may continue using the latest Langflow id without a sidebar id.
+ thread_id = next(
+ (
+ stored_id
+ for stored_id, state in active_conversations.get(user_id, {}).items()
+ if thread_id and thread_id in state.get("response_ids", [])
+ ),
+ thread_id,
+ )
previous_loaded_from_memory = _conversation_thread_in_memory(user_id, thread_id)
conversation_state = get_conversation_thread(user_id, thread_id)
@@ -915,30 +930,74 @@ async def async_langflow_chat_stream(
usage_data = None
collected_chunks = [] # Store all chunks for function call data
error_occurred = False
+ store_id = None
+ claimed_ids: set[str] = set()
+ turn_finished = False
+
+ async def persist_stream_state(*session_ids, mint_if_missing=False):
+ nonlocal store_id
+ from services.session_ownership_service import session_ownership_service
+
+ if store_id is None:
+ store_id = _resolve_conversation_store_id(
+ response_id,
+ thread_id,
+ previous_loaded_from_memory=previous_loaded_from_memory,
+ mint_if_missing=mint_if_missing,
+ )
+ if store_id is None:
+ return
+ for session_id in dict.fromkeys((store_id, *session_ids)):
+ if not session_id or session_id in claimed_ids:
+ continue
+ if not await session_ownership_service.claim_session(
+ user_id, session_id, conversation_id=store_id
+ ):
+ raise RuntimeError("Unable to establish chat session ownership")
+ claimed_ids.add(session_id)
+ response_ids = conversation_state.setdefault("response_ids", [])
+ if session_id not in response_ids:
+ response_ids.append(session_id)
+ conversation_state["last_activity"] = datetime.now()
+ await store_conversation_thread(user_id, store_id, conversation_state)
+
+ upstream = async_stream(
+ langflow_client,
+ prompt,
+ flow_id,
+ extra_headers=extra_headers,
+ previous_response_id=langflow_previous_response_id,
+ log_prefix="langflow",
+ )
try:
- async for chunk in async_stream(
- langflow_client,
- prompt,
- flow_id,
- extra_headers=extra_headers,
- previous_response_id=langflow_previous_response_id,
- log_prefix="langflow",
- ):
+ async for chunk in upstream:
# Extract text content to build full response for history
+ chunk_ids = []
try:
import json
chunk_data = json.loads(chunk.decode("utf-8"))
collected_chunks.append(chunk_data) # Collect all chunk data
+ # Own every response/session ID callers can use for continuation,
+ # not tool item IDs. OpenAI events may nest the ID in response.
+ response_obj = chunk_data.get("response")
+ if not isinstance(response_obj, dict):
+ response_obj = {}
+ chunk_ids = [
+ value
+ for value in (
+ chunk_data.get("id"),
+ chunk_data.get("response_id"),
+ response_obj.get("id"),
+ )
+ if isinstance(value, str) and value
+ ]
+ if chunk_ids:
+ response_id = chunk_ids[0]
if "delta" in chunk_data and "content" in chunk_data["delta"]:
full_response += chunk_data["delta"]["content"]
- # Extract response_id from chunk
- if "id" in chunk_data:
- response_id = chunk_data["id"]
- elif "response_id" in chunk_data:
- response_id = chunk_data["response_id"]
# Check for error status. Do not forward bare Langflow error
# chunks — they often lack a useful message and the client would
@@ -972,6 +1031,10 @@ async def async_langflow_chat_stream(
usage_data = response_obj.get("usage")
except Exception:
pass
+ # This await must stay outside the permissive JSON parser above.
+ # Persist/claim BEFORE yielding the first occurrence of any ID.
+ if any(session_id not in claimed_ids for session_id in chunk_ids):
+ await persist_stream_state(*chunk_ids)
yield chunk
from api.provider_validation import (
@@ -994,16 +1057,8 @@ async def async_langflow_chat_stream(
"error": True,
}
)
- store_id = _resolve_conversation_store_id(
- response_id,
- thread_id,
- previous_loaded_from_memory=previous_loaded_from_memory,
- mint_if_missing=True,
- )
- try:
- await _persist_error_conversation(user_id, store_id, conversation_state)
- except Exception as store_error:
- logger.error(f"Failed to store error conversation: {store_error}")
+ turn_finished = True
+ await persist_stream_state(response_id, mint_if_missing=True)
yield _stream_error_chunk(display_text, store_id)
return
@@ -1020,20 +1075,29 @@ async def async_langflow_chat_stream(
assistant_message["response_data"] = {"usage": usage_data}
conversation_state["messages"].append(assistant_message)
- persist_id = _resolve_conversation_store_id(
- response_id,
- thread_id,
- previous_loaded_from_memory=previous_loaded_from_memory,
- )
- if persist_id:
- conversation_state["last_activity"] = datetime.now()
- await store_conversation_thread(user_id, persist_id, conversation_state)
+ turn_finished = True
+ await persist_stream_state(response_id)
+ except (asyncio.CancelledError, GeneratorExit):
+ # Disconnects do not run the normal tail and are not provider failures.
+ # Keep exactly one partial assistant turn and propagate cancellation;
+ # never replay the prompt or synthesize a terminal event to a dead client.
+ with anyio.CancelScope(shield=True):
+ if not turn_finished and full_response:
+ conversation_state["messages"].append(
+ {
+ "role": "assistant",
+ "content": full_response,
+ "response_id": response_id,
+ "timestamp": datetime.now(),
+ "chunks": collected_chunks,
+ "interrupted": True,
+ }
+ )
try:
- from services.session_ownership_service import session_ownership_service
-
- await session_ownership_service.claim_session(user_id, persist_id)
- except Exception as e:
- logger.warning(f"Failed to claim session ownership: {e}")
+ await persist_stream_state(response_id)
+ except Exception:
+ logger.exception("Failed to store interrupted Langflow turn")
+ raise
except Exception as e:
logger.error(f"Error in langflow chat stream: {e}", exc_info=True)
from api.provider_validation import resolve_chat_stream_error_message_async
@@ -1051,17 +1115,16 @@ async def async_langflow_chat_stream(
"error": True,
}
)
- store_id = _resolve_conversation_store_id(
- response_id,
- thread_id,
- previous_loaded_from_memory=previous_loaded_from_memory,
- mint_if_missing=True,
- )
+ error_store_id = None
try:
- await _persist_error_conversation(user_id, store_id, conversation_state)
+ await persist_stream_state(response_id, mint_if_missing=True)
+ error_store_id = store_id
except Exception as store_error:
logger.error(f"Failed to store error conversation: {store_error}")
- yield _stream_error_chunk(display_text, store_id)
+ yield _stream_error_chunk(display_text, error_store_id)
+ finally:
+ with anyio.CancelScope(shield=True):
+ await upstream.aclose()
async def delete_user_conversation(user_id: str, response_id: str) -> bool:
@@ -1075,31 +1138,18 @@ async def delete_user_conversation(user_id: str, response_id: str) -> bool:
Exception — on unexpected storage errors so callers can distinguish
a confirmed "not found" from a backend failure.
"""
- deleted = False
-
- # Delete from in-memory storage (cannot raise)
- if user_id in active_conversations and response_id in active_conversations[user_id]:
- del active_conversations[user_id][response_id]
- logger.debug(f"Deleted conversation {response_id} from memory for user {user_id}")
- deleted = True
+ from services.session_ownership_service import session_ownership_service
- # Delete from persistent storage — let real errors propagate to the caller
- conversation_deleted = await conversation_persistence.delete_conversation_thread(
- user_id, response_id
- )
- if conversation_deleted:
- logger.debug(
- f"Deleted conversation {response_id} from persistent storage for user {user_id}"
- )
+ session_ids = await session_ownership_service.get_conversation_sessions(user_id, response_id)
+ if not session_ids:
+ return False
+ canonical_id = session_ids[0]
+ # Revoke ALL durable aliases before removing the local index. Failures must
+ # reach the caller, not turn into a successful delete with reusable IDs.
+ await session_ownership_service.release_conversation_sessions(user_id, session_ids)
+ deleted = await conversation_persistence.delete_conversation_thread(user_id, canonical_id)
+ if canonical_id in active_conversations.get(user_id, {}):
+ del active_conversations[user_id][canonical_id]
deleted = True
- # Release session ownership (best-effort; never masks storage errors above)
- try:
- from services.session_ownership_service import session_ownership_service
-
- await session_ownership_service.release_session(user_id, response_id)
- logger.debug(f"Released session ownership for {response_id} for user {user_id}")
- except Exception as e:
- logger.warning(f"Failed to release session ownership: {e}")
-
return deleted
diff --git a/src/db/migrations_runtime.py b/src/db/migrations_runtime.py
index 489601288..659a9b0d2 100644
--- a/src/db/migrations_runtime.py
+++ b/src/db/migrations_runtime.py
@@ -232,6 +232,7 @@ async def migrate_chat_history_json_to_db(session: AsyncSession) -> dict[str, in
user_id=str(uid),
created_at=created,
last_accessed=last,
+ conversation_id=data.get("conversation_id") or str(sid),
)
if inserted:
stats["sessions_inserted"] += 1
diff --git a/src/db/models/session_ownership.py b/src/db/models/session_ownership.py
index eebc6d83a..204fcb960 100644
--- a/src/db/models/session_ownership.py
+++ b/src/db/models/session_ownership.py
@@ -19,5 +19,7 @@ class SessionOwnership(SQLModel, table=True):
response_id: str = Field(primary_key=True, max_length=64)
user_id: str = Field(max_length=64, index=True)
+ # Null legacy rows are their own canonical conversation.
+ conversation_id: str | None = Field(default=None, max_length=64, index=True)
created_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
last_accessed: datetime | None = Field(default=None)
diff --git a/src/db/repositories/permission_repo.py b/src/db/repositories/permission_repo.py
index 2b899140b..1a9e570c5 100644
--- a/src/db/repositories/permission_repo.py
+++ b/src/db/repositories/permission_repo.py
@@ -1,7 +1,6 @@
-from typing import Optional
-
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
+from sqlmodel import col
from db.models import Permission
@@ -16,8 +15,6 @@ class PermissionRepo:
)
return list(result.scalars().all())
- async def get_by_name(self, name: str) -> Optional[Permission]:
- result = await self.session.execute(
- select(Permission).where(Permission.name == name)
- )
+ async def get_by_name(self, name: str) -> Permission | None:
+ result = await self.session.execute(select(Permission).where(col(Permission.name) == name))
return result.scalar_one_or_none()
diff --git a/src/db/repositories/session_ownership_repo.py b/src/db/repositories/session_ownership_repo.py
index 93baef94d..44b6bbd52 100644
--- a/src/db/repositories/session_ownership_repo.py
+++ b/src/db/repositories/session_ownership_repo.py
@@ -1,10 +1,10 @@
"""Async CRUD over the ``session_ownership`` table."""
from datetime import UTC, datetime
-from typing import Optional
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
+from sqlmodel import col
from db.models import SessionOwnership
@@ -13,29 +13,37 @@ class SessionOwnershipRepo:
def __init__(self, session: AsyncSession):
self.session = session
- async def get(self, response_id: str) -> Optional[SessionOwnership]:
+ async def get(self, response_id: str) -> SessionOwnership | None:
return await self.session.get(SessionOwnership, response_id)
async def claim(
- self, response_id: str, user_id: str
+ self, response_id: str, user_id: str, conversation_id: str | None = None
) -> SessionOwnership:
- """Idempotent claim. If the row exists, only update last_accessed
- — never silently re-assign ownership (that would be a security
- bug). The caller is responsible for ensuring the user_id matches
- the existing owner before calling this for an existing row.
- """
+ """Claim without transferring ownership or moving an existing alias."""
existing = await self.get(response_id)
now = datetime.now(UTC)
+ canonical_id = conversation_id or response_id
+ if canonical_id != response_id:
+ canonical = await self.get(canonical_id)
+ if canonical is None or canonical.user_id != user_id:
+ raise PermissionError("Conversation is not owned by this user")
+ if existing is not None and (
+ existing.user_id != user_id
+ or (existing.conversation_id is not None and existing.conversation_id != canonical_id)
+ ):
+ raise PermissionError("Session ownership or conversation conflict")
if existing is None:
row = SessionOwnership(
response_id=response_id,
user_id=user_id,
+ conversation_id=canonical_id,
created_at=now,
last_accessed=now,
)
self.session.add(row)
await self.session.flush()
return row
+ existing.conversation_id = canonical_id
existing.last_accessed = now
self.session.add(existing)
await self.session.flush()
@@ -43,8 +51,18 @@ class SessionOwnershipRepo:
async def list_for_user(self, user_id: str) -> list[str]:
result = await self.session.execute(
- select(SessionOwnership.response_id).where(
- SessionOwnership.user_id == user_id
+ select(col(SessionOwnership.response_id)).where(
+ col(SessionOwnership.user_id) == user_id
+ )
+ )
+ return list(result.scalars().all())
+
+ async def list_for_conversation(self, conversation_id: str, user_id: str) -> list[str]:
+ result = await self.session.execute(
+ select(col(SessionOwnership.response_id)).where(
+ col(SessionOwnership.user_id) == user_id,
+ (col(SessionOwnership.conversation_id) == conversation_id)
+ | (col(SessionOwnership.response_id) == conversation_id),
)
)
return list(result.scalars().all())
@@ -66,8 +84,9 @@ class SessionOwnershipRepo:
self,
response_id: str,
user_id: str,
- created_at: Optional[datetime] = None,
- last_accessed: Optional[datetime] = None,
+ created_at: datetime | None = None,
+ last_accessed: datetime | None = None,
+ conversation_id: str | None = None,
) -> bool:
"""Used by the runtime migration to copy JSON rows verbatim
without overwriting timestamps. Returns True when a row was inserted."""
@@ -77,6 +96,7 @@ class SessionOwnershipRepo:
row = SessionOwnership(
response_id=response_id,
user_id=user_id,
+ conversation_id=conversation_id or response_id,
created_at=created_at or datetime.now(UTC),
last_accessed=last_accessed,
)
diff --git a/src/services/chat_service.py b/src/services/chat_service.py
index dec8501cf..7b139cc48 100644
--- a/src/services/chat_service.py
+++ b/src/services/chat_service.py
@@ -933,8 +933,20 @@ class ChatService:
async def delete_session(self, user_id: str, session_id: str):
"""Delete a session from both local storage and Langflow"""
try:
- # Delete from local conversation storage
+ # Resolve the complete, owned group before removing any associations.
from agent import delete_user_conversation
+ from services.session_ownership_service import session_ownership_service
+
+ session_ids = await session_ownership_service.get_conversation_sessions(
+ user_id, session_id
+ )
+
+ # Attempt every upstream deletion first: local revocation failures
+ # must not erase JSON-only aliases before their cleanup is attempted.
+ upstream_results = [
+ await self._delete_langflow_session(owned_id) for owned_id in session_ids
+ ]
+ langflow_deleted = bool(upstream_results) and all(upstream_results)
local_deleted = await delete_user_conversation(user_id, session_id)
@@ -945,9 +957,6 @@ class ChatService:
"error": "Conversation not found",
}
- # Delete from Langflow using the monitor API (best-effort)
- langflow_deleted = await self._delete_langflow_session(session_id)
-
return {
"success": True,
"local_deleted": local_deleted,
diff --git a/src/services/conversation_persistence_service.py b/src/services/conversation_persistence_service.py
index bc028ec4c..0570df59b 100644
--- a/src/services/conversation_persistence_service.py
+++ b/src/services/conversation_persistence_service.py
@@ -19,8 +19,9 @@ import asyncio
import json
import os
import threading
+from collections.abc import Callable
from datetime import datetime
-from typing import Any, Callable, Dict, Optional
+from typing import Any
from config.paths import get_data_file
from config.storage_mode import (
@@ -38,23 +39,23 @@ class ConversationPersistenceService:
def __init__(
self,
- storage_file: Optional[str] = None,
- session_factory: Optional[Callable] = None,
+ storage_file: str | None = None,
+ session_factory: Callable | None = None,
):
self.storage_file = storage_file or get_data_file("conversations.json")
os.makedirs(os.path.dirname(self.storage_file), exist_ok=True)
self.lock = threading.Lock()
self._session_factory = session_factory
- self._conversations: Dict[str, Dict[str, Any]] = self._load_conversations()
+ self._conversations: dict[str, dict[str, Any]] = self._load_conversations()
# ------------------------------------------------------------------
# JSON helpers
# ------------------------------------------------------------------
- def _load_conversations(self) -> Dict[str, Dict[str, Any]]:
+ def _load_conversations(self) -> dict[str, dict[str, Any]]:
if os.path.exists(self.storage_file):
try:
- with open(self.storage_file, "r", encoding="utf-8") as f:
+ with open(self.storage_file, encoding="utf-8") as f:
return json.load(f)
except Exception as exc: # noqa: BLE001
logger.error(f"Error loading conversations: {exc}")
@@ -88,7 +89,7 @@ class ConversationPersistenceService:
return [self._serialize_datetime(x) for x in obj]
return obj
- def _count_total(self, data: Dict[str, Any]) -> int:
+ def _count_total(self, data: dict[str, Any]) -> int:
total = 0
for user_conv in data.values():
if isinstance(user_conv, dict):
@@ -99,7 +100,7 @@ class ConversationPersistenceService:
# Public async API
# ------------------------------------------------------------------
- async def get_user_conversations(self, user_id: str) -> Dict[str, Any]:
+ async def get_user_conversations(self, user_id: str) -> dict[str, Any]:
mode = get_storage_mode()
if mode == "files":
if user_id not in self._conversations:
@@ -121,7 +122,7 @@ class ConversationPersistenceService:
self,
user_id: str,
response_id: str,
- conversation_state: Dict[str, Any],
+ conversation_state: dict[str, Any],
) -> None:
serialized = self._serialize_datetime(conversation_state)
@@ -134,9 +135,7 @@ class ConversationPersistenceService:
if db_writes_enabled():
await self._db_upsert(user_id, response_id, serialized)
- async def get_conversation_thread(
- self, user_id: str, response_id: str
- ) -> Dict[str, Any]:
+ async def get_conversation_thread(self, user_id: str, response_id: str) -> dict[str, Any]:
mode = get_storage_mode()
if mode != "files":
payload = await self._db_get_one(response_id, user_id)
@@ -147,16 +146,11 @@ class ConversationPersistenceService:
# files or hybrid-with-no-db-row → JSON
return self._conversations.get(user_id, {}).get(response_id, {})
- async def delete_conversation_thread(
- self, user_id: str, response_id: str
- ) -> bool:
+ async def delete_conversation_thread(self, user_id: str, response_id: str) -> bool:
deleted = False
if file_writes_enabled():
- if (
- user_id in self._conversations
- and response_id in self._conversations[user_id]
- ):
+ if user_id in self._conversations and response_id in self._conversations[user_id]:
del self._conversations[user_id][response_id]
await self._save_conversations()
deleted = True
@@ -175,7 +169,7 @@ class ConversationPersistenceService:
if db_writes_enabled():
await self._db_delete_all(user_id)
- async def get_storage_stats(self) -> Dict[str, Any]:
+ async def get_storage_stats(self) -> dict[str, Any]:
# Snapshot — uses whichever storage the mode prioritizes.
mode = get_storage_mode()
if mode == "files":
@@ -187,20 +181,20 @@ class ConversationPersistenceService:
}
# db / hybrid summary from DB
try:
+ from sqlalchemy import func, select
+ from sqlmodel import col
+
from db.models import Conversation
- from sqlalchemy import select, func
sess_factory = self._resolve_session_factory()
if sess_factory is None:
return {"total_users": 0, "total_conversations": 0}
async with sess_factory() as session:
total = (
- await session.execute(select(func.count(Conversation.response_id)))
+ await session.execute(select(func.count(col(Conversation.response_id))))
).scalar_one()
users = (
- await session.execute(
- select(func.count(func.distinct(Conversation.user_id)))
- )
+ await session.execute(select(func.count(func.distinct(Conversation.user_id))))
).scalar_one()
return {
"total_users": int(users or 0),
@@ -221,12 +215,13 @@ class ConversationPersistenceService:
return self._session_factory
try:
from db.engine import SessionLocal
+
return SessionLocal
except Exception: # noqa: BLE001
return None
@staticmethod
- def _payload_from_row(row) -> Dict[str, Any]:
+ def _payload_from_row(row) -> dict[str, Any]:
return {
"response_id": row.response_id,
"title": row.title,
@@ -238,9 +233,7 @@ class ConversationPersistenceService:
"last_activity": row.last_activity.isoformat() if row.last_activity else None,
}
- async def _db_upsert(
- self, user_id: str, response_id: str, payload: Dict[str, Any]
- ) -> None:
+ async def _db_upsert(self, user_id: str, response_id: str, payload: dict[str, Any]) -> None:
from db.repositories import ConversationRepo
sess_factory = self._resolve_session_factory()
@@ -262,7 +255,7 @@ class ConversationPersistenceService:
except Exception as exc: # noqa: BLE001
logger.error("DB store_conversation failed", error=str(exc))
- async def _db_get_for_user(self, user_id: str) -> Dict[str, Dict[str, Any]]:
+ async def _db_get_for_user(self, user_id: str) -> dict[str, dict[str, Any]]:
from db.repositories import ConversationRepo
sess_factory = self._resolve_session_factory()
@@ -276,9 +269,7 @@ class ConversationPersistenceService:
logger.debug("DB get_for_user failed", error=str(exc))
return {}
- async def _db_get_one(
- self, response_id: str, user_id: str
- ) -> Optional[Dict[str, Any]]:
+ async def _db_get_one(self, response_id: str, user_id: str) -> dict[str, Any] | None:
from db.repositories import ConversationRepo
sess_factory = self._resolve_session_factory()
diff --git a/src/services/session_ownership_service.py b/src/services/session_ownership_service.py
index a0dcd25ff..6ecb2cae1 100644
--- a/src/services/session_ownership_service.py
+++ b/src/services/session_ownership_service.py
@@ -16,6 +16,7 @@ from __future__ import annotations
import json
import os
+import tempfile
from collections.abc import Callable
from datetime import UTC, datetime
from typing import Any
@@ -56,32 +57,71 @@ class SessionOwnershipService:
return {}
return {}
- def _save_ownership_data(self) -> None:
+ def _save_ownership_data(self, data: dict[str, dict[str, Any]]) -> None:
+ """Publish a complete file before changing the authorization cache."""
+ temporary = None
try:
- with open(self.ownership_file, "w") as f:
- json.dump(self.ownership_data, f, indent=2)
- except Exception as exc: # noqa: BLE001
- logger.error(f"Error saving session ownership data: {exc}")
+ with tempfile.NamedTemporaryFile(
+ mode="w", dir=os.path.dirname(self.ownership_file), delete=False
+ ) as f:
+ temporary = f.name
+ json.dump(data, f, indent=2)
+ f.flush()
+ os.fsync(f.fileno())
+ os.replace(temporary, self.ownership_file)
+ self.ownership_data = data
+ finally:
+ if temporary is not None and os.path.exists(temporary):
+ os.unlink(temporary)
# ------------------------------------------------------------------
# Public async API
# ------------------------------------------------------------------
- async def claim_session(self, user_id: str, session_id: str) -> None:
+ async def claim_session(
+ self, user_id: str, session_id: str, conversation_id: str | None = None
+ ) -> bool:
+ """True only after an owned claim AND its association are persisted.
+
+ Hybrid prefers the DB, with a successfully written JSON file as fallback.
+ Ownership conflicts are never treated as backend failures or bypassed.
+ An unsuccessful write never adds an authorization entry to the cache.
+ """
+ canonical_id = conversation_id or session_id
+ if file_writes_enabled() and self._file_claim_conflicts(user_id, session_id, canonical_id):
+ return False
+ durable = False
+ if db_writes_enabled():
+ try:
+ durable = await self._db_claim(user_id, session_id, canonical_id)
+ except PermissionError:
+ return False
if file_writes_enabled():
+ # Another claim may have published while the DB write was awaiting.
+ if self._file_claim_conflicts(user_id, session_id, canonical_id):
+ return False
now = datetime.now(UTC).isoformat()
- if session_id not in self.ownership_data:
- self.ownership_data[session_id] = {
+ existing = self.ownership_data.get(session_id, {})
+ if canonical_id != session_id and not durable:
+ canonical = self.ownership_data.get(canonical_id)
+ if not canonical or canonical.get("user_id") != user_id:
+ return False
+ updated = {
+ **self.ownership_data,
+ session_id: {
+ **existing,
"user_id": user_id,
- "created_at": now,
+ "conversation_id": canonical_id,
+ "created_at": existing.get("created_at", now),
"last_accessed": now,
- }
- else:
- self.ownership_data[session_id]["last_accessed"] = now
- self._save_ownership_data()
-
- if db_writes_enabled():
- await self._db_claim(user_id, session_id)
+ },
+ }
+ try:
+ self._save_ownership_data(updated)
+ durable = True
+ except Exception as exc: # noqa: BLE001
+ logger.error("File claim_session failed", error=str(exc))
+ return durable
async def get_session_owner(self, session_id: str) -> str | None:
mode = get_storage_mode()
@@ -116,13 +156,99 @@ class SessionOwnershipService:
owned = set(await self.get_user_sessions(user_id))
return [sid for sid in session_ids if sid in owned]
+ async def get_conversation_sessions(self, user_id: str, session_id: str) -> list[str]:
+ """Return owned canonical ID first, then aliases, including after restart.
+
+ Deletion must not guess after a DB read failure: otherwise it could leave
+ durable aliases authorized while reporting successful removal.
+ """
+ mode = get_storage_mode()
+ files = self.ownership_data if mode != "db" else {}
+ data = files.get(session_id, {})
+ canonical_id = (
+ data.get("conversation_id") or session_id if data.get("user_id") == user_id else None
+ )
+ sessions = []
+ if mode != "files":
+ from db.repositories import SessionOwnershipRepo
+
+ factory = self._resolve_session_factory()
+ if factory is None and mode == "db":
+ raise RuntimeError("Session ownership database unavailable")
+ if factory is not None:
+ async with factory() as session:
+ repo = SessionOwnershipRepo(session)
+ row = await repo.get(session_id)
+ if row is not None:
+ if row.user_id != user_id:
+ return []
+ canonical_id = row.conversation_id or session_id
+ if canonical_id is None:
+ return []
+ canonical = await repo.get(canonical_id)
+ if canonical is not None:
+ if canonical.user_id != user_id:
+ return []
+ elif files.get(canonical_id, {}).get("user_id") != user_id:
+ return []
+ sessions = await repo.list_for_conversation(canonical_id, user_id)
+ for sid, entry in files.items():
+ if (
+ entry.get("user_id") != user_id
+ or (entry.get("conversation_id") or sid) != canonical_id
+ ):
+ continue
+ authoritative = await repo.get(sid)
+ if authoritative is None or (
+ authoritative.user_id == user_id
+ and (authoritative.conversation_id or sid) == canonical_id
+ ):
+ sessions.append(sid)
+ return list(dict.fromkeys([canonical_id, *sessions]))
+ if canonical_id is None or files.get(canonical_id, {}).get("user_id") != user_id:
+ return []
+ sessions = [
+ sid
+ for sid, entry in files.items()
+ if entry.get("user_id") == user_id
+ and (entry.get("conversation_id") or sid) == canonical_id
+ ]
+ return list(dict.fromkeys([canonical_id, *sessions]))
+
+ async def release_conversation_sessions(self, user_id: str, session_ids: list[str]) -> None:
+ """Revoke a complete owned group atomically in each destination.
+
+ The caller must attempt upstream cleanup before revoking associations:
+ hybrid groups can contain JSON-only aliases absent from a DB rollback.
+ """
+ if file_writes_enabled():
+ updated = dict(self.ownership_data)
+ for session_id in session_ids:
+ if updated.get(session_id, {}).get("user_id") == user_id:
+ del updated[session_id]
+ self._save_ownership_data(updated)
+ if db_writes_enabled():
+ from db.repositories import SessionOwnershipRepo
+
+ factory = self._resolve_session_factory()
+ if factory is None:
+ if get_storage_mode() == "db":
+ raise RuntimeError("Session ownership database unavailable")
+ return
+ async with factory() as session:
+ repo = SessionOwnershipRepo(session)
+ for session_id in session_ids:
+ await repo.release(session_id, user_id)
+ await session.commit()
+
async def release_session(self, user_id: str, session_id: str) -> bool:
released = False
if file_writes_enabled() and session_id in self.ownership_data:
if self.ownership_data[session_id].get("user_id") == user_id:
- del self.ownership_data[session_id]
- self._save_ownership_data()
+ updated = dict(self.ownership_data)
+ del updated[session_id]
+ self._save_ownership_data(updated)
released = True
else:
logger.warning(
@@ -186,18 +312,51 @@ class SessionOwnershipService:
except Exception: # noqa: BLE001
return None
- async def _db_claim(self, user_id: str, session_id: str) -> None:
+ def _file_claim_conflicts(self, user_id: str, session_id: str, canonical_id: str) -> bool:
+ existing = self.ownership_data.get(session_id)
+ canonical = self.ownership_data.get(canonical_id)
+ return bool(
+ existing
+ and (
+ existing.get("user_id") != user_id
+ or (existing.get("conversation_id") or canonical_id) != canonical_id
+ )
+ or canonical
+ and canonical.get("user_id") != user_id
+ )
+
+ async def _db_claim(self, user_id: str, session_id: str, conversation_id: str) -> bool:
+ from sqlalchemy.exc import IntegrityError
+
from db.repositories import SessionOwnershipRepo
sess_factory = self._resolve_session_factory()
if sess_factory is None:
- return
+ logger.error("DB claim_session failed: database unavailable")
+ return False
try:
async with sess_factory() as session:
- await SessionOwnershipRepo(session).claim(session_id, user_id)
+ repo = SessionOwnershipRepo(session)
+ canonical = self.ownership_data.get(conversation_id, {})
+ if (
+ file_writes_enabled()
+ and conversation_id != session_id
+ and canonical.get("user_id") == user_id
+ ):
+ # Reconcile a durable JSON-only canonical claim after DB recovery.
+ await repo.claim(conversation_id, user_id, conversation_id)
+ await repo.claim(session_id, user_id, conversation_id)
await session.commit()
+ return True
+ except PermissionError:
+ raise
+ except IntegrityError as exc:
+ # A competing insert may now own the ID. Never turn a uniqueness
+ # conflict into permission to publish a JSON fallback for this user.
+ raise PermissionError("Concurrent session ownership conflict") from exc
except Exception as exc: # noqa: BLE001
logger.error("DB claim_session failed", error=str(exc))
+ return False
async def _db_get_owner(self, session_id: str) -> str | None:
from db.repositories import SessionOwnershipRepo
@@ -231,7 +390,7 @@ class SessionOwnershipService:
sess_factory = self._resolve_session_factory()
if sess_factory is None:
- return False
+ raise RuntimeError("Session ownership database unavailable")
try:
async with sess_factory() as session:
ok = await SessionOwnershipRepo(session).release(session_id, user_id)
@@ -239,7 +398,7 @@ class SessionOwnershipService:
return ok
except Exception as exc: # noqa: BLE001
logger.error("DB release_session failed", error=str(exc))
- return False
+ raise
# Global instance — session_factory plumbed in main.py at startup.
diff --git a/tests/unit/db/migrations/test_chat_history_migration.py b/tests/unit/db/migrations/test_chat_history_migration.py
index bf48dc66d..f1dba92d8 100644
--- a/tests/unit/db/migrations/test_chat_history_migration.py
+++ b/tests/unit/db/migrations/test_chat_history_migration.py
@@ -1,11 +1,15 @@
"""Runtime migration: session_ownership.json + conversations.json → DB."""
+import importlib.util
import json
import sys
from pathlib import Path
import pytest
import pytest_asyncio
+import sqlalchemy as sa
+from alembic.migration import MigrationContext
+from alembic.operations import Operations
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from sqlmodel import SQLModel
@@ -14,9 +18,9 @@ SRC = ROOT / "src"
if str(SRC) not in sys.path:
sys.path.insert(0, str(SRC))
+import db.migrations_runtime as migrations_runtime # noqa: E402
import db.models # noqa: E402,F401
from db.repositories import ConversationRepo, SessionOwnershipRepo # noqa: E402
-from db import migrations_runtime # noqa: E402
@pytest_asyncio.fixture
@@ -38,41 +42,49 @@ def staged_files(tmp_path, monkeypatch):
so_path = tmp_path / "session_ownership.json"
conv_path = tmp_path / "conversations.json"
- so_path.write_text(json.dumps({
- "sess-1": {
- "user_id": "alice",
- "created_at": "2026-04-01T10:00:00",
- "last_accessed": "2026-04-02T11:00:00",
- },
- "sess-2": {
- "user_id": "bob",
- "created_at": "2026-04-03T09:00:00",
- "last_accessed": "2026-04-03T09:00:00",
- },
- }))
-
- conv_path.write_text(json.dumps({
- "alice": {
- "r-1": {
- "title": "Project sync",
- "endpoint": "chat",
- "previous_response_id": None,
- "filter_id": None,
- "total_messages": 4,
- "created_at": "2026-04-01T10:00:00",
- "last_activity": "2026-04-02T11:00:00",
- },
- },
- "bob": {
- "r-2": {
- "title": "Bug triage",
- "endpoint": "langflow",
- "total_messages": 1,
- "created_at": "2026-04-03T09:00:00",
- "last_activity": "2026-04-03T09:00:00",
- },
- },
- }))
+ so_path.write_text(
+ json.dumps(
+ {
+ "sess-1": {
+ "user_id": "alice",
+ "created_at": "2026-04-01T10:00:00",
+ "last_accessed": "2026-04-02T11:00:00",
+ },
+ "sess-2": {
+ "user_id": "bob",
+ "created_at": "2026-04-03T09:00:00",
+ "last_accessed": "2026-04-03T09:00:00",
+ },
+ }
+ )
+ )
+
+ conv_path.write_text(
+ json.dumps(
+ {
+ "alice": {
+ "r-1": {
+ "title": "Project sync",
+ "endpoint": "chat",
+ "previous_response_id": None,
+ "filter_id": None,
+ "total_messages": 4,
+ "created_at": "2026-04-01T10:00:00",
+ "last_activity": "2026-04-02T11:00:00",
+ },
+ },
+ "bob": {
+ "r-2": {
+ "title": "Bug triage",
+ "endpoint": "langflow",
+ "total_messages": 1,
+ "created_at": "2026-04-03T09:00:00",
+ "last_activity": "2026-04-03T09:00:00",
+ },
+ },
+ }
+ )
+ )
def _resolver(name):
return str(tmp_path / name)
@@ -147,3 +159,58 @@ async def test_run_marks_migration_done(staged_files, session_factory):
assert await migrations_runtime._already_done(
session, migrations_runtime.CHAT_HISTORY_JSON_TO_DB_V1
)
+
+
+async def test_json_migration_preserves_alias_association(staged_files, session_factory):
+ ownership_file = staged_files / "session_ownership.json"
+ payload = json.loads(ownership_file.read_text())
+ payload["alias"] = {"user_id": "alice", "conversation_id": "sess-1"}
+ ownership_file.write_text(json.dumps(payload))
+ async with session_factory() as session:
+ await migrations_runtime.migrate_chat_history_json_to_db(session)
+ await session.commit()
+ async with session_factory() as session:
+ repo = SessionOwnershipRepo(session)
+ assert (await repo.get("alias")).conversation_id == "sess-1"
+ assert set(await repo.list_for_conversation("sess-1", "alice")) == {"sess-1", "alias"}
+
+
+def test_session_alias_schema_upgrade_and_downgrade():
+ migration_path = ROOT / "alembic" / "versions" / "0008_session_conversation_id.py"
+ spec = importlib.util.spec_from_file_location("migration_0008", migration_path)
+ migration = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(migration)
+ engine = sa.create_engine("sqlite:///:memory:")
+ with engine.begin() as connection:
+ connection.execute(
+ sa.text(
+ "CREATE TABLE session_ownership (response_id VARCHAR(64) PRIMARY KEY, "
+ "user_id VARCHAR(64) NOT NULL)"
+ )
+ )
+ connection.execute(sa.text("INSERT INTO session_ownership VALUES ('legacy', 'alice')"))
+ migration.op = Operations(MigrationContext.configure(connection))
+ migration.upgrade()
+ assert (
+ connection.execute(
+ sa.text(
+ "SELECT conversation_id FROM session_ownership WHERE response_id = 'legacy'"
+ )
+ ).scalar_one()
+ == "legacy"
+ )
+ assert "ix_session_ownership_conversation_id" in {
+ index["name"] for index in sa.inspect(connection).get_indexes("session_ownership")
+ }
+ migration.downgrade()
+ assert (
+ connection.execute(
+ sa.text("SELECT user_id FROM session_ownership WHERE response_id = 'legacy'")
+ ).scalar_one()
+ == "alice"
+ )
+ assert "conversation_id" not in {
+ column["name"] for column in sa.inspect(connection).get_columns("session_ownership")
+ }
+ engine.dispose()
diff --git a/tests/unit/services/test_session_ownership_service_modes.py b/tests/unit/services/test_session_ownership_service_modes.py
index 62f4ef410..10f1ec2a1 100644
--- a/tests/unit/services/test_session_ownership_service_modes.py
+++ b/tests/unit/services/test_session_ownership_service_modes.py
@@ -1,7 +1,6 @@
"""SessionOwnershipService — hybrid / db / files mode coverage."""
import json
-import os
import sys
from pathlib import Path
@@ -106,9 +105,11 @@ async def test_hybrid_mode_writes_both(monkeypatch, tmp_data_dir, session_factor
async def test_db_mode_ignores_pre_existing_json(monkeypatch, tmp_data_dir, session_factory):
"""Stale JSON on disk must not bleed through in db mode."""
tmp_data_dir.parent.mkdir(parents=True, exist_ok=True)
- tmp_data_dir.write_text(json.dumps({
- "ghost-sess": {"user_id": "ghost-user", "created_at": "x", "last_accessed": "x"}
- }))
+ tmp_data_dir.write_text(
+ json.dumps(
+ {"ghost-sess": {"user_id": "ghost-user", "created_at": "x", "last_accessed": "x"}}
+ )
+ )
monkeypatch.setenv("OPENRAG_STORAGE_MODE", "db")
svc = _svc(session_factory)
@@ -146,3 +147,212 @@ async def test_release_only_owner_can_release(monkeypatch, tmp_data_dir, session
released = await svc.release_session("alice", "sess-1")
assert released is True
assert await svc.get_session_owner("sess-1") is None
+
+
[email protected]("mode", ["db", "hybrid", "files"])
+async def test_claim_rejects_owner_and_alias_reassignment(
+ monkeypatch, tmp_data_dir, session_factory, mode
+):
+ monkeypatch.setenv("OPENRAG_STORAGE_MODE", mode)
+ svc = _svc(session_factory)
+ assert await svc.claim_session("alice", "canonical") is True
+ assert await svc.claim_session("alice", "other") is True
+ assert await svc.claim_session("alice", "alias", conversation_id="canonical") is True
+ assert await svc.claim_session("mallory", "alias", conversation_id="other") is False
+ assert await svc.claim_session("alice", "alias", conversation_id="other") is False
+ assert await svc.claim_session("mallory", "foreign", conversation_id="canonical") is False
+ fresh = _svc(session_factory)
+ assert await fresh.get_session_owner("alias") == "alice"
+ assert await fresh.get_session_owner("foreign") is None
+ assert set(await fresh.get_conversation_sessions("alice", "canonical")) == {
+ "canonical",
+ "alias",
+ }
+ assert await fresh.get_conversation_sessions("mallory", "canonical") == []
+
+
+async def test_hybrid_committed_db_claim_survives_file_failure(
+ monkeypatch, tmp_data_dir, session_factory
+):
+ monkeypatch.setenv("OPENRAG_STORAGE_MODE", "hybrid")
+ svc = _svc(session_factory)
+ tmp_data_dir.mkdir()
+ assert await svc.claim_session("alice", "canonical") is True
+ assert await svc.claim_session("alice", "alias", conversation_id="canonical") is True
+ assert svc.ownership_data == {}
+ fresh = _svc(session_factory)
+ assert await fresh.get_session_owner("alias") == "alice"
+ assert set(await fresh.get_conversation_sessions("alice", "canonical")) == {
+ "canonical",
+ "alias",
+ }
+
+
+async def test_hybrid_file_fallback_survives_failed_db_commit(
+ monkeypatch, tmp_data_dir, session_factory
+):
+ from contextlib import asynccontextmanager
+
+ monkeypatch.setenv("OPENRAG_STORAGE_MODE", "hybrid")
+
+ @asynccontextmanager
+ async def broken_database():
+ async with session_factory() as session:
+
+ async def fail_commit():
+ raise OSError("commit unavailable")
+
+ monkeypatch.setattr(session, "commit", fail_commit)
+ yield session
+
+ svc = _svc(broken_database)
+ assert await svc.claim_session("alice", "canonical") is True
+ assert await svc.claim_session("alice", "alias", conversation_id="canonical") is True
+ fresh = _svc(session_factory)
+ assert await fresh.get_session_owner("alias") == "alice"
+ assert set(await fresh.get_conversation_sessions("alice", "canonical")) == {
+ "canonical",
+ "alias",
+ }
+ async with session_factory() as session:
+ assert await SessionOwnershipRepo(session).get("alias") is None
+
+
+async def test_hybrid_database_conflict_cannot_fall_back_to_stale_file(
+ monkeypatch, tmp_data_dir, session_factory
+):
+ monkeypatch.setenv("OPENRAG_STORAGE_MODE", "db")
+ assert await _svc(session_factory).claim_session("mallory", "session") is True
+ tmp_data_dir.write_text(json.dumps({"session": {"user_id": "alice"}}))
+ monkeypatch.setenv("OPENRAG_STORAGE_MODE", "hybrid")
+ svc = _svc(session_factory)
+ assert await svc.claim_session("alice", "session") is False
+ assert await svc.get_session_owner("session") == "mallory"
+
+
+async def test_hybrid_db_recovery_backfills_json_only_canonical(
+ monkeypatch, tmp_data_dir, session_factory
+):
+ monkeypatch.setenv("OPENRAG_STORAGE_MODE", "hybrid")
+
+ def unavailable_database():
+ raise OSError("database unavailable")
+
+ svc = _svc(unavailable_database)
+ assert await svc.claim_session("alice", "canonical") is True
+ svc._session_factory = session_factory
+ assert await svc.claim_session("alice", "alias", conversation_id="canonical") is True
+ async with session_factory() as session:
+ repo = SessionOwnershipRepo(session)
+ assert (await repo.get("canonical")).user_id == "alice"
+ assert (await repo.get("alias")).conversation_id == "canonical"
+
+
+async def test_hybrid_deletion_group_ignores_foreign_or_reassigned_db_aliases(
+ monkeypatch, tmp_data_dir, session_factory
+):
+ monkeypatch.setenv("OPENRAG_STORAGE_MODE", "db")
+ svc = _svc(session_factory)
+ assert await svc.claim_session("alice", "canonical") is True
+ assert await svc.claim_session("mallory", "foreign") is True
+ assert await svc.claim_session("alice", "other") is True
+ assert await svc.claim_session("alice", "moved", conversation_id="other") is True
+ tmp_data_dir.write_text(
+ json.dumps(
+ {
+ "canonical": {"user_id": "alice", "conversation_id": "canonical"},
+ "foreign": {"user_id": "alice", "conversation_id": "canonical"},
+ "moved": {"user_id": "alice", "conversation_id": "canonical"},
+ }
+ )
+ )
+ monkeypatch.setenv("OPENRAG_STORAGE_MODE", "hybrid")
+ fresh = _svc(session_factory)
+ assert await fresh.get_conversation_sessions("alice", "canonical") == ["canonical"]
+
+
[email protected]("mode", ["db", "hybrid"])
+async def test_failed_group_revocation_preserves_all_ids_for_retry(
+ monkeypatch, tmp_data_dir, session_factory, mode
+):
+ monkeypatch.setenv("OPENRAG_STORAGE_MODE", mode)
+ svc = _svc(session_factory)
+ for response_id in ("canonical", "alias-1", "alias-2"):
+ assert await svc.claim_session("alice", response_id, conversation_id="canonical") is True
+ original = SessionOwnershipRepo.release
+
+ async def fail_partway(repo, response_id, user_id):
+ if response_id == "alias-1":
+ raise OSError("revocation unavailable")
+ return await original(repo, response_id, user_id)
+
+ monkeypatch.setattr(SessionOwnershipRepo, "release", fail_partway)
+ with pytest.raises(OSError, match="revocation unavailable"):
+ await svc.release_conversation_sessions("alice", ["alias-2", "alias-1", "canonical"])
+ fresh = _svc(session_factory)
+ sessions = await fresh.get_conversation_sessions("alice", "canonical")
+ assert set(sessions) == {"canonical", "alias-1", "alias-2"}
+ monkeypatch.setattr(SessionOwnershipRepo, "release", original)
+ await fresh.release_conversation_sessions("alice", sessions)
+ assert await _svc(session_factory).get_user_sessions("alice") == []
+
+
+async def test_hybrid_losing_db_insert_cannot_publish_fallback_owner(
+ monkeypatch, tmp_data_dir, session_factory
+):
+ monkeypatch.setenv("OPENRAG_STORAGE_MODE", "db")
+ winner = _svc(session_factory)
+ assert await winner.claim_session("alice", "contended") is True
+ # Model a competing claimant whose SELECT happened just before the winner's
+ # insert. The ensuing flush hits the real DB primary-key constraint.
+ original = SessionOwnershipRepo.get
+
+ async def stale_get(repo, response_id):
+ if response_id == "contended":
+ return None
+ return await original(repo, response_id)
+
+ monkeypatch.setenv("OPENRAG_STORAGE_MODE", "hybrid")
+ loser = _svc(session_factory)
+ monkeypatch.setattr(SessionOwnershipRepo, "get", stale_get)
+ assert await loser.claim_session("bob", "contended") is False
+ assert loser.ownership_data == {}
+ assert not tmp_data_dir.exists()
+ monkeypatch.setattr(SessionOwnershipRepo, "get", original)
+ assert await _svc(session_factory).get_session_owner("contended") == "alice"
+
+
+async def test_hybrid_rechecks_cache_after_awaiting_database(
+ monkeypatch, tmp_data_dir, session_factory
+):
+ import asyncio
+
+ monkeypatch.setenv("OPENRAG_STORAGE_MODE", "hybrid")
+ svc = _svc(session_factory)
+ waiting = asyncio.Event()
+ winner_published = asyncio.Event()
+ original = svc._db_claim
+
+ async def delayed_claim(user_id, session_id, conversation_id):
+ if user_id == "bob":
+ waiting.set()
+ await winner_published.wait()
+ return False
+ return await original(user_id, session_id, conversation_id)
+
+ monkeypatch.setattr(svc, "_db_claim", delayed_claim)
+ losing_claim = asyncio.create_task(svc.claim_session("bob", "contended"))
+ await waiting.wait()
+ assert await svc.claim_session("alice", "contended") is True
+ winner_published.set()
+ assert await losing_claim is False
+ assert json.loads(tmp_data_dir.read_text())["contended"]["user_id"] == "alice"
diff --git a/tests/unit/test_agent_stream_fence_stripped.py b/tests/unit/test_agent_stream_fence_stripped.py
index 58ae383fd..c0031ded0 100644
--- a/tests/unit/test_agent_stream_fence_stripped.py
+++ b/tests/unit/test_agent_stream_fence_stripped.py
@@ -46,6 +46,9 @@ class AsyncChunkStream:
except StopIteration:
raise StopAsyncIteration from None
+ async def close(self):
+ self._chunks = iter(())
+
class FakeResponses:
def __init__(self, chunks):
diff --git a/tests/unit/test_langflow_stream_provider_errors.py b/tests/unit/test_langflow_stream_provider_errors.py
index 8a862cf61..faa4ea1ba 100644
--- a/tests/unit/test_langflow_stream_provider_errors.py
+++ b/tests/unit/test_langflow_stream_provider_errors.py
@@ -1,18 +1,31 @@
-"""Provider errors must reach the client as a final stream error chunk.
+"""Langflow stream lifecycle and provider-error regressions.
-These tests mock the upstream stream to raise exceptions shaped like real
-provider failures (watsonx rate limit, invalid key, permission). That proves
-the yield path without hardcoding a raise in production code.
+Only the upstream provider is replaced; cancellation regressions use the real
+conversation store and ownership guard, including durable SQLite ownership.
"""
from __future__ import annotations
+import asyncio
import json
from collections.abc import AsyncIterator
+from pathlib import Path
+from types import SimpleNamespace
+import anyio
+import httpx
import pytest
+import pytest_asyncio
+from fastapi import HTTPException
+from openai import AsyncOpenAI
+from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
+from sqlmodel import SQLModel
import agent as agent_module
+from api.chat import ChatBody, _assert_owns, delete_session_endpoint, langflow_endpoint
+from services.chat_service import ChatService
+from services.conversation_persistence_service import ConversationPersistenceService
+from services.session_ownership_service import SessionOwnershipService
def test_format_provider_error_message_extracts_embedded_json():
@@ -99,8 +112,12 @@ def store_in_memory(monkeypatch):
agent_module.active_conversations[user_id][response_id] = conversation_state
class _Ownership:
- async def claim_session(self, user_id, session_id):
+ async def claim_session(self, user_id, session_id, conversation_id=None):
claimed.append((user_id, session_id))
+ return True
+
+ async def get_session_owner(self, session_id):
+ return next((user_id for user_id, sid in claimed if sid == session_id), None)
monkeypatch.setattr(agent_module, "store_conversation_thread", _store)
monkeypatch.setattr(
@@ -377,7 +394,7 @@ async def test_langflow_stream_treats_credential_dump_content_as_error(
error_chunk = next(c for c in chunks if c.get("status") == "failed")
assert "{" not in error_chunk["error"]["message"]
assert "Provided API key is Invalid" in error_chunk["error"]["message"]
- assert store_in_memory["stored"] == [(user_id, "resp_dump")]
+ assert store_in_memory["stored"] == [(user_id, "resp_dump")] * 2
assert store_in_memory["claimed"] == [(user_id, "resp_dump")]
stored = agent_module.active_conversations[user_id]["resp_dump"]
assert stored["messages"][-1]["error"] is True
@@ -417,7 +434,7 @@ async def test_langflow_stream_keeps_partial_text_with_provider_error(monkeypatc
assert len(chunks) == 2
assert chunks[0]["delta"]["content"] == "Here is a partial answer"
assert chunks[1]["error"]["message"] == (f"Here is a partial answer\n\n{provider_message}")
- assert store_in_memory["stored"] == [(user_id, "resp_partial")]
+ assert store_in_memory["stored"] == [(user_id, "resp_partial")] * 2
assert store_in_memory["claimed"] == [(user_id, "resp_partial")]
@@ -451,3 +468,514 @@ async def test_chat_stream_yields_provider_error_chunk(monkeypatch, store_in_mem
assert not stored_id.startswith("error_")
assert chunks[0].get("id") == stored_id
assert stored_id in agent_module.active_conversations.get(user_id, {})
+
+
+@pytest_asyncio.fixture(params=["db", "hybrid", "files"])
+async def durable_stream_stores(request, monkeypatch, tmp_path):
+ monkeypatch.setenv("OPENRAG_STORAGE_MODE", request.param)
+ engine = create_async_engine(f"sqlite+aiosqlite:///{tmp_path / 'chat.db'}")
+ async with engine.begin() as connection:
+ await connection.run_sync(SQLModel.metadata.create_all)
+ factory = async_sessionmaker(engine, expire_on_commit=False)
+ monkeypatch.setattr(
+ "services.session_ownership_service.get_data_file",
+ lambda name: str(tmp_path / name),
+ )
+ ownership = SessionOwnershipService(session_factory=factory)
+ persistence = ConversationPersistenceService(
+ storage_file=str(tmp_path / "conversations.json"), session_factory=factory
+ )
+ monkeypatch.setattr("services.session_ownership_service.session_ownership_service", ownership)
+ monkeypatch.setattr(agent_module, "conversation_persistence", persistence)
+ monkeypatch.setattr(agent_module, "active_conversations", {})
+ yield ownership, persistence, factory
+ await engine.dispose()
+
+
[email protected]("abort", ["close", "cancel"])
+async def test_aborted_langflow_stream_keeps_owned_id_and_partial_turn(
+ monkeypatch, durable_stream_stores, abort
+):
+ ownership, persistence, factory = durable_stream_stores
+ waiting = asyncio.Event()
+ upstream_closed = asyncio.Event()
+ calls = []
+
+ async def upstream(*_args, **kwargs):
+ calls.append(kwargs.get("previous_response_id"))
+ try:
+ yield b'{"id":"resp-first","delta":{"content":"Partial answer"}}\n'
+ waiting.set()
+ await asyncio.Event().wait()
+ finally:
+ upstream_closed.set()
+
+ monkeypatch.setattr(agent_module, "async_stream", upstream)
+ stream = agent_module.async_langflow_chat_stream(object(), "flow", "First question", "alice")
+ chunk = json.loads(await anext(stream))
+ # The ID must already be durable when it first crosses the generator boundary.
+ await _assert_owns(chunk["id"], "alice")
+ assert (await persistence.get_conversation_thread("alice", chunk["id"]))["total_messages"] >= 1
+ if abort == "close":
+ await stream.aclose()
+ else:
+ pending = asyncio.create_task(anext(stream))
+ await waiting.wait()
+ pending.cancel()
+ with pytest.raises(asyncio.CancelledError):
+ await pending
+ assert upstream_closed.is_set()
+ # Reload ownership rather than relying on the live service's cache.
+ reloaded = SessionOwnershipService(session_factory=factory)
+ assert await reloaded.get_session_owner(chunk["id"]) == "alice"
+ with pytest.raises(HTTPException) as forbidden:
+ await _assert_owns(chunk["id"], "mallory")
+ assert forbidden.value.status_code == 403
+ with pytest.raises(HTTPException) as missing:
+ await _assert_owns("unknown-session", "alice")
+ assert missing.value.status_code == 404
+
+ async def continuation(*_args, **kwargs):
+ calls.append(kwargs.get("previous_response_id"))
+ yield b'{"id":"resp-next","response_id":"resp-next-alias","delta":{"content":"Next"}}\n'
+
+ monkeypatch.setattr(agent_module, "async_stream", continuation)
+ await _assert_owns(chunk["id"], "alice")
+ followup = agent_module.async_langflow_chat_stream(
+ object(),
+ "flow",
+ "Follow-up",
+ "alice",
+ previous_response_id=chunk["id"],
+ conversation_id=chunk["id"],
+ )
+ next_chunk = json.loads(await anext(followup))
+ await _assert_owns(next_chunk["id"], "alice")
+ await _assert_owns(next_chunk["response_id"], "alice")
+ assert await _collect_error_chunks(followup) == []
+ assert calls == [None, "resp-first"] # Never replay the interrupted prompt.
+ conversations = await persistence.get_user_conversations("alice")
+ assert list(conversations) == ["resp-first"] # Stable sidebar id, changing response ids.
+ messages = agent_module.active_conversations["alice"]["resp-first"]["messages"]
+ assert [(m["role"], m["content"]) for m in messages if m["role"] != "system"] == [
+ ("user", "First question"),
+ ("assistant", "Partial answer"),
+ ("user", "Follow-up"),
+ ("assistant", "Next"),
+ ]
+ assert conversations["resp-first"]["total_messages"] == 4
+
+ # SDK callers may only carry the changing response ID, not conversation_id.
+ await _collect_error_chunks(
+ agent_module.async_langflow_chat_stream(
+ object(),
+ "flow",
+ "Third question",
+ "alice",
+ previous_response_id=next_chunk["response_id"],
+ )
+ )
+ assert list(await persistence.get_user_conversations("alice")) == ["resp-first"]
+ assert calls == [None, "resp-first", "resp-next-alias"]
+ assert [m["content"] for m in messages if m["role"] == "user"] == [
+ "First question",
+ "Follow-up",
+ "Third question",
+ ]
+
+
[email protected]("claim_failure", ["unavailable", "foreign"])
+async def test_stream_does_not_expose_unowned_response_id(
+ monkeypatch, durable_stream_stores, claim_failure
+):
+ ownership, persistence, _ = durable_stream_stores
+ if claim_failure == "foreign":
+ await ownership.claim_session("mallory", "resp-unclaimable")
+ else:
+
+ async def unavailable(*_args, **_kwargs):
+ return False
+
+ monkeypatch.setattr(ownership, "claim_session", unavailable)
+
+ async def upstream(*_args, **_kwargs):
+ yield b'{"id":"resp-unclaimable","delta":{"content":"Not exposed"}}\n'
+
+ monkeypatch.setattr(agent_module, "async_stream", upstream)
+ chunks = await _collect_error_chunks(
+ agent_module.async_langflow_chat_stream(object(), "flow", "Question", "alice")
+ )
+ assert len(chunks) == 1
+ assert chunks[0]["status"] == "failed"
+ assert "id" not in chunks[0] and "response_id" not in chunks[0]
+ assert await persistence.get_user_conversations("alice") == {}
+ assert await ownership.get_session_owner("resp-unclaimable") == (
+ "mallory" if claim_failure == "foreign" else None
+ )
+
+
+async def test_local_asgi_disconnect_smoke_preserves_followup(monkeypatch, durable_stream_stores):
+ """Real endpoint → generator → OpenAI SSE adapter → durable stores, locally."""
+ _, persistence, factory = durable_stream_stores
+ disconnected = anyio.Event()
+ upstream_closed = anyio.Event()
+ calls = []
+
+ class ProviderStream(httpx.AsyncByteStream):
+ async def __aiter__(self):
+ yield (
+ b"event: response.created\n"
+ b'data: {"type":"response.created","response":{"id":"resp-http"}}\n\n'
+ )
+ yield (
+ b"event: response.output_text.delta\n"
+ b'data: {"type":"response.output_text.delta","id":"resp-http",'
+ b'"delta":{"content":"Partial HTTP answer"}}\n\n'
+ )
+ await anyio.sleep_forever()
+
+ async def aclose(self):
+ upstream_closed.set()
+
+ async def provider(request):
+ payload = json.loads(request.content)
+ calls.append(payload)
+ if len(calls) == 1:
+ return httpx.Response(
+ 200, headers={"content-type": "text/event-stream"}, stream=ProviderStream()
+ )
+ return httpx.Response(
+ 200,
+ headers={"content-type": "text/event-stream"},
+ content=(
+ b"event: response.output_text.delta\n"
+ b'data: {"type":"response.output_text.delta","id":"resp-http-next",'
+ b'"delta":{"content":"Follow-up HTTP answer"}}\n\n'
+ b"event: response.completed\n"
+ b'data: {"type":"response.completed","response":{"id":"resp-http-next",'
+ b'"usage":{"input_tokens":3,"output_tokens":4,"total_tokens":7}}}\n\n'
+ ),
+ )
+
+ async with AsyncOpenAI(
+ api_key="local-test-only",
+ base_url="https://provider.invalid/v1",
+ http_client=httpx.AsyncClient(transport=httpx.MockTransport(provider)),
+ max_retries=0,
+ ) as client:
+
+ class EndpointChatService:
+ async def langflow_chat(self, prompt, _subject, _jwt, **kwargs):
+ return agent_module.async_langflow_chat_stream(
+ client,
+ "flow",
+ prompt,
+ kwargs["storage_user_id"],
+ previous_response_id=kwargs["previous_response_id"],
+ conversation_id=kwargs["conversation_id"],
+ )
+
+ user = SimpleNamespace(
+ user_id="oauth-alice",
+ db_user_id="alice",
+ jwt_token="local-test-only",
+ name="Alice",
+ )
+ service = EndpointChatService()
+ response = await langflow_endpoint(
+ ChatBody(prompt="First HTTP question", stream=True),
+ chat_service=service,
+ session_manager=None,
+ user=user,
+ )
+ emitted = []
+
+ async def send(message):
+ if message["type"] == "http.response.body" and message.get("body"):
+ chunk = json.loads(message["body"])
+ emitted.append(chunk)
+ response_id = chunk.get("id") or (chunk.get("response") or {}).get("id")
+ if response_id:
+ await _assert_owns(response_id, "alice")
+ if chunk.get("delta", {}).get("content"):
+ disconnected.set()
+
+ async def receive():
+ await disconnected.wait()
+ return {"type": "http.disconnect"}
+
+ # Starlette's real disconnect listener cancels the body iterator using an
+ # AnyIO cancel scope, rather than manually throwing into our generator.
+ with anyio.fail_after(5):
+ await response({"type": "http", "asgi": {"spec_version": "2.3"}}, receive, send)
+ assert upstream_closed.is_set()
+ assert len(emitted) == 2
+ metadata = await persistence.get_conversation_thread("alice", "resp-http")
+ assert metadata["total_messages"] == 2
+
+ followup = await langflow_endpoint(
+ ChatBody(
+ prompt="Next HTTP question",
+ stream=True,
+ previous_response_id="resp-http",
+ conversation_id="resp-http",
+ ),
+ chat_service=service,
+ session_manager=None,
+ user=user,
+ )
+ chunks = await _collect_error_chunks(followup.body_iterator)
+ await _assert_owns("resp-http-next", "alice")
+ assert chunks[-1]["response"]["usage"]["total_tokens"] == 7
+ assert calls[1]["previous_response_id"] == "resp-http"
+ assert [call["input"] for call in calls] == ["First HTTP question", "Next HTTP question"]
+ conversations = await persistence.get_user_conversations("alice")
+ assert list(conversations) == ["resp-http"]
+ assert conversations["resp-http"]["total_messages"] == 4
+ messages = agent_module.active_conversations["alice"]["resp-http"]["messages"]
+ assert [m["content"] for m in messages if m["role"] != "system"] == [
+ "First HTTP question",
+ "Partial HTTP answer",
+ "Next HTTP question",
+ "Follow-up HTTP answer",
+ ]
+
+ # Restart before canonical deletion: aliases must come from durable state.
+ reloaded = SessionOwnershipService(session_factory=factory)
+ monkeypatch.setattr(
+ "services.session_ownership_service.session_ownership_service", reloaded
+ )
+ monkeypatch.setattr(agent_module, "active_conversations", {})
+ upstream_deletes = []
+
+ async def monitor_delete(method, path):
+ assert method == "DELETE"
+ upstream_deletes.append(path.rsplit("/", 1)[-1])
+ return httpx.Response(204)
+
+ monkeypatch.setattr("services.chat_service.clients.langflow_request", monitor_delete)
+ deletion = await delete_session_endpoint(
+ session_id="resp-http", chat_service=ChatService(), user=user
+ )
+ assert deletion.status_code == 200
+ assert set(upstream_deletes) == {"resp-http", "resp-http-next"}
+ fresh = SessionOwnershipService(session_factory=factory)
+ monkeypatch.setattr("services.session_ownership_service.session_ownership_service", fresh)
+ with pytest.raises(HTTPException) as missing:
+ await langflow_endpoint(
+ ChatBody(
+ prompt="Cannot resurrect", stream=True, previous_response_id="resp-http-next"
+ ),
+ chat_service=service,
+ session_manager=None,
+ user=user,
+ )
+ assert missing.value.status_code == 404
+ assert len(calls) == 2
+
+ # Actual endpoint + SSE adapter with both persistent destinations broken.
+ ownership_path = Path(fresh.ownership_file)
+ if ownership_path.exists():
+ ownership_path.unlink()
+ ownership_path.mkdir()
+
+ def unavailable_database():
+ raise OSError("local smoke database unavailable")
+
+ monkeypatch.setattr(fresh, "_resolve_session_factory", lambda: unavailable_database)
+ failed = await langflow_endpoint(
+ ChatBody(prompt="Fail closed", stream=True),
+ chat_service=service,
+ session_manager=None,
+ user=user,
+ )
+ failed_chunks = await _collect_error_chunks(failed.body_iterator)
+ assert len(failed_chunks) == 1 and failed_chunks[0]["status"] == "failed"
+ assert "id" not in failed_chunks[0] and "response_id" not in failed_chunks[0]
+ assert (
+ await SessionOwnershipService(session_factory=factory).get_session_owner(
+ "resp-http-next"
+ )
+ is None
+ )
+ print(
+ "LOCAL SMOKE: disconnect/follow-up retained 4 messages; restart + "
+ "canonical deletion removed both upstream sessions and alias authorization; "
+ "persistent failures emitted no client ID"
+ )
+
+
[email protected]("delta", [None, {"content": None}])
+async def test_metadata_only_chunk_claims_id_before_exposure(
+ monkeypatch, durable_stream_stores, delta
+):
+ async def upstream(*_args, **_kwargs):
+ yield (json.dumps({"id": "resp-metadata", "delta": delta}) + "\n").encode()
+
+ monkeypatch.setattr(agent_module, "async_stream", upstream)
+ stream = agent_module.async_langflow_chat_stream(object(), "flow", "Question", "alice")
+ chunk = json.loads(await anext(stream))
+ await _assert_owns(chunk["id"], "alice")
+ await stream.aclose()
+
+
[email protected]("delete_id", ["resp-first", "resp-second"])
+async def test_delete_canonical_revokes_aliases_after_reload(
+ monkeypatch, durable_stream_stores, delete_id
+):
+ ownership, persistence, factory = durable_stream_stores
+
+ async def upstream(*_args, **kwargs):
+ response_id = "resp-second" if kwargs.get("previous_response_id") else "resp-first"
+ yield (json.dumps({"id": response_id, "delta": {"content": "Answer"}}) + "\n").encode()
+
+ monkeypatch.setattr(agent_module, "async_stream", upstream)
+ for previous in (None, "resp-first"):
+ await _collect_error_chunks(
+ agent_module.async_langflow_chat_stream(
+ object(), "flow", "Question", "alice", previous_response_id=previous
+ )
+ )
+ await ownership.claim_session("mallory", "resp-foreign")
+ await ownership.claim_session("alice", "resp-unrelated")
+ assert list(await persistence.get_user_conversations("alice")) == ["resp-first"]
+ assert await agent_module.delete_user_conversation("mallory", "resp-first") is False
+ assert agent_module.active_conversations["alice"]["resp-first"]["messages"]
+ # Restart: neither ownership cache nor the in-memory response_ids can help.
+ reloaded = SessionOwnershipService(session_factory=factory)
+ monkeypatch.setattr("services.session_ownership_service.session_ownership_service", reloaded)
+ monkeypatch.setattr(agent_module, "active_conversations", {})
+ persistence = ConversationPersistenceService(
+ storage_file=persistence.storage_file, session_factory=factory
+ )
+ monkeypatch.setattr(agent_module, "conversation_persistence", persistence)
+ assert await agent_module.delete_user_conversation("alice", delete_id) is True
+ fresh = SessionOwnershipService(session_factory=factory)
+ monkeypatch.setattr("services.session_ownership_service.session_ownership_service", fresh)
+ for response_id in ("resp-first", "resp-second"):
+ with pytest.raises(HTTPException) as missing:
+ await _assert_owns(response_id, "alice")
+ assert missing.value.status_code == 404
+ assert await fresh.get_session_owner("resp-foreign") == "mallory"
+ assert await fresh.get_session_owner("resp-unrelated") == "alice"
+ assert await persistence.get_user_conversations("alice") == {}
+
+
+async def test_persistent_claim_failure_never_exposes_cached_id(monkeypatch, durable_stream_stores):
+ ownership, persistence, factory = durable_stream_stores
+ # A real IO failure, not a stubbed claim: cache echo must not prove durability.
+ Path(ownership.ownership_file).mkdir()
+
+ def unavailable_database():
+ raise OSError("local database unavailable")
+
+ monkeypatch.setattr(ownership, "_resolve_session_factory", lambda: unavailable_database)
+
+ async def upstream(*_args, **_kwargs):
+ yield b'{"id":"resp-file-failure","delta":{"content":"Not exposed"}}\n'
+
+ monkeypatch.setattr(agent_module, "async_stream", upstream)
+ chunks = await _collect_error_chunks(
+ agent_module.async_langflow_chat_stream(object(), "flow", "Question", "alice")
+ )
+ assert len(chunks) == 1
+ assert chunks[0]["status"] == "failed"
+ assert "id" not in chunks[0] and "response_id" not in chunks[0]
+ assert await ownership.get_session_owner("resp-file-failure") is None
+ assert (
+ await SessionOwnershipService(session_factory=factory).get_session_owner(
+ "resp-file-failure"
+ )
+ is None
+ )
+ assert await persistence.get_user_conversations("alice") == {}
+
+
[email protected]("durable_stream_stores", ["hybrid", "files"], indirect=True)
+async def test_failed_file_revocation_is_not_reported_as_success(
+ monkeypatch, durable_stream_stores
+):
+ ownership, _, _ = durable_stream_stores
+
+ async def upstream(*_args, **_kwargs):
+ yield b'{"id":"resp-delete-failure","delta":{"content":"Answer"}}\n'
+
+ monkeypatch.setattr(agent_module, "async_stream", upstream)
+ await _collect_error_chunks(
+ agent_module.async_langflow_chat_stream(object(), "flow", "Question", "alice")
+ )
+ ownership_path = Path(ownership.ownership_file)
+ ownership_path.unlink()
+ ownership_path.mkdir()
+
+ async def monitor_delete(_self, _session_id):
+ return True
+
+ monkeypatch.setattr(ChatService, "_delete_langflow_session", monitor_delete)
+ result = await ChatService().delete_session("alice", "resp-delete-failure")
+ assert result["success"] is False
+ await _assert_owns("resp-delete-failure", "alice")
+ assert "resp-delete-failure" in agent_module.active_conversations["alice"]
+
+
[email protected]("durable_stream_stores", ["hybrid"], indirect=True)
+async def test_json_only_alias_cleanup_precedes_failed_db_revocation(
+ monkeypatch, durable_stream_stores
+):
+ from db.repositories import SessionOwnershipRepo
+
+ ownership, _, factory = durable_stream_stores
+
+ async def upstream(*_args, **kwargs):
+ response_id = "alias" if kwargs.get("previous_response_id") else "canonical"
+ yield (json.dumps({"id": response_id, "delta": {"content": "Answer"}}) + "\n").encode()
+
+ monkeypatch.setattr(agent_module, "async_stream", upstream)
+ await _collect_error_chunks(
+ agent_module.async_langflow_chat_stream(object(), "flow", "First", "alice")
+ )
+
+ def unavailable_database():
+ raise OSError("database temporarily unavailable")
+
+ ownership._session_factory = unavailable_database
+ await _collect_error_chunks(
+ agent_module.async_langflow_chat_stream(
+ object(), "flow", "Next", "alice", previous_response_id="canonical"
+ )
+ )
+ ownership._session_factory = factory
+ async with factory() as session:
+ assert await SessionOwnershipRepo(session).get("alias") is None
+ assert await ownership.get_session_owner("alias") == "alice"
+ original = SessionOwnershipRepo.release
+
+ async def failed_release(*_args):
+ raise OSError("database revocation failed")
+
+ upstream_deletes = []
+
+ async def monitor_delete(_self, session_id):
+ upstream_deletes.append(session_id)
+ return True
+
+ monkeypatch.setattr(ChatService, "_delete_langflow_session", monitor_delete)
+ monkeypatch.setattr(SessionOwnershipRepo, "release", failed_release)
+ result = await ChatService().delete_session("alice", "canonical")
+ assert result["success"] is False
+ assert set(upstream_deletes) == {"canonical", "alias"}
+ # Even though JSON-only ownership was revoked, its upstream cleanup was not
+ # lost. The surviving canonical DB row can be retried after a restart.
+ reloaded = SessionOwnershipService(session_factory=factory)
+ monkeypatch.setattr("services.session_ownership_service.session_ownership_service", reloaded)
+ monkeypatch.setattr(SessionOwnershipRepo, "release", original)
+ assert (await ChatService().delete_session("alice", "canonical"))["success"] is True