amirelrefai commited on
Commit
b645e0a
Β·
1 Parent(s): 8714692

fix(rag): resolve orphan session retention, enable bulk chat deletion, and improve global knowledge fallback

Browse files
app/core/persistent_db.py CHANGED
@@ -244,13 +244,32 @@ class SQLitePersistentDB:
244
  with self._tx() as conn:
245
  if owner_id:
246
  cur = conn.execute("DELETE FROM chats WHERE chat_id=? AND (owner_id=? OR owner_id IS NULL)", (chat_id, owner_id))
 
 
 
 
247
  else:
248
  cur = conn.execute("DELETE FROM chats WHERE chat_id=?", (chat_id,))
249
- rc = cur.rowcount
250
  conn.execute("DELETE FROM messages WHERE chat_id=?", (chat_id,))
251
  conn.execute("DELETE FROM chat_documents WHERE chat_id=?", (chat_id,))
252
  return rc > 0
253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254
  # ── message CRUD ────────────────────────────────────────────────────────
255
  def add_message(self, chat_id: str, role: str, content: str, owner_id: Optional[str] = None) -> None:
256
  now = time.time()
 
244
  with self._tx() as conn:
245
  if owner_id:
246
  cur = conn.execute("DELETE FROM chats WHERE chat_id=? AND (owner_id=? OR owner_id IS NULL)", (chat_id, owner_id))
247
+ rc = cur.rowcount
248
+ if rc == 0:
249
+ cur = conn.execute("DELETE FROM chats WHERE chat_id=?", (chat_id,))
250
+ rc = cur.rowcount
251
  else:
252
  cur = conn.execute("DELETE FROM chats WHERE chat_id=?", (chat_id,))
253
+ rc = cur.rowcount
254
  conn.execute("DELETE FROM messages WHERE chat_id=?", (chat_id,))
255
  conn.execute("DELETE FROM chat_documents WHERE chat_id=?", (chat_id,))
256
  return rc > 0
257
 
258
+ def delete_all_chats(self, owner_id: Optional[str] = None) -> int:
259
+ with self._tx() as conn:
260
+ if owner_id:
261
+ cur = conn.execute("SELECT chat_id FROM chats WHERE owner_id=? OR owner_id IS NULL", (owner_id,))
262
+ else:
263
+ cur = conn.execute("SELECT chat_id FROM chats")
264
+ cids = [r[0] for r in cur.fetchall()]
265
+ if not cids:
266
+ return 0
267
+ placeholders = ",".join("?" for _ in cids)
268
+ conn.execute(f"DELETE FROM messages WHERE chat_id IN ({placeholders})", cids)
269
+ conn.execute(f"DELETE FROM chat_documents WHERE chat_id IN ({placeholders})", cids)
270
+ conn.execute(f"DELETE FROM chats WHERE chat_id IN ({placeholders})", cids)
271
+ return len(cids)
272
+
273
  # ── message CRUD ────────────────────────────────────────────────────────
274
  def add_message(self, chat_id: str, role: str, content: str, owner_id: Optional[str] = None) -> None:
275
  now = time.time()
app/core/postgres_db.py CHANGED
@@ -315,13 +315,33 @@ class PostgresDB:
315
  with conn.cursor() as cur:
316
  if owner_id:
317
  cur.execute("DELETE FROM chats WHERE chat_id = %s AND (owner_id = %s OR owner_id IS NULL)", (chat_id, owner_id))
 
 
 
 
 
318
  else:
319
  cur.execute("DELETE FROM chats WHERE chat_id = %s", (chat_id,))
320
- rc = cur.rowcount
321
  cur.execute("DELETE FROM messages WHERE chat_id = %s", (chat_id,))
322
  cur.execute("DELETE FROM chat_documents WHERE chat_id = %s", (chat_id,))
323
  return rc > 0
324
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
325
  # ── message CRUD ────────────────────────────────────────────────────────
326
  def add_message(self, chat_id: str, role: str, content: str, owner_id: Optional[str] = None) -> None:
327
  now = time.time()
@@ -711,11 +731,11 @@ class PostgresDB:
711
  cur.execute(
712
  """
713
  SELECT km.* FROM knowledge_memory km
714
- JOIN doc_meta dm ON km.doc_id = dm.doc_id
715
- WHERE (dm.owner_id = %s OR dm.owner_id IS NULL)
716
  ORDER BY km.updated_at DESC
717
  """,
718
- (owner_id,),
719
  )
720
  else:
721
  cur.execute("SELECT * FROM knowledge_memory ORDER BY updated_at DESC")
 
315
  with conn.cursor() as cur:
316
  if owner_id:
317
  cur.execute("DELETE FROM chats WHERE chat_id = %s AND (owner_id = %s OR owner_id IS NULL)", (chat_id, owner_id))
318
+ rc = cur.rowcount
319
+ if rc == 0:
320
+ # Fallback: delete unconditionally by chat_id so clients can clear orphaned sessions
321
+ cur.execute("DELETE FROM chats WHERE chat_id = %s", (chat_id,))
322
+ rc = cur.rowcount
323
  else:
324
  cur.execute("DELETE FROM chats WHERE chat_id = %s", (chat_id,))
325
+ rc = cur.rowcount
326
  cur.execute("DELETE FROM messages WHERE chat_id = %s", (chat_id,))
327
  cur.execute("DELETE FROM chat_documents WHERE chat_id = %s", (chat_id,))
328
  return rc > 0
329
 
330
+ def delete_all_chats(self, owner_id: Optional[str] = None) -> int:
331
+ with self._conn() as conn:
332
+ with conn.cursor() as cur:
333
+ if owner_id:
334
+ cur.execute("SELECT chat_id FROM chats WHERE owner_id = %s OR owner_id IS NULL", (owner_id,))
335
+ else:
336
+ cur.execute("SELECT chat_id FROM chats")
337
+ cids = [r[0] for r in cur.fetchall()]
338
+ if not cids:
339
+ return 0
340
+ cur.execute("DELETE FROM messages WHERE chat_id = ANY(%s)", (cids,))
341
+ cur.execute("DELETE FROM chat_documents WHERE chat_id = ANY(%s)", (cids,))
342
+ cur.execute("DELETE FROM chats WHERE chat_id = ANY(%s)", (cids,))
343
+ return len(cids)
344
+
345
  # ── message CRUD ────────────────────────────────────────────────────────
346
  def add_message(self, chat_id: str, role: str, content: str, owner_id: Optional[str] = None) -> None:
347
  now = time.time()
 
731
  cur.execute(
732
  """
733
  SELECT km.* FROM knowledge_memory km
734
+ LEFT JOIN doc_meta dm ON km.doc_id = dm.doc_id
735
+ WHERE (dm.owner_id = %s OR dm.owner_id IS NULL OR km.owner_id = %s OR km.owner_id IS NULL)
736
  ORDER BY km.updated_at DESC
737
  """,
738
+ (owner_id, owner_id),
739
  )
740
  else:
741
  cur.execute("SELECT * FROM knowledge_memory ORDER BY updated_at DESC")
app/core/storage.py CHANGED
@@ -779,6 +779,12 @@ class MemoryStorage:
779
  self._invalidate_chat_caches(chat_id)
780
  return persistent_db.delete_chat(chat_id, owner_id=owner_id)
781
 
 
 
 
 
 
 
782
  def list_chats(self, owner_id: Optional[str] = None) -> List[Dict[str, Any]]:
783
  cache_key = owner_id or "__all__"
784
  cached = self._chats_list_cache.get(cache_key)
 
779
  self._invalidate_chat_caches(chat_id)
780
  return persistent_db.delete_chat(chat_id, owner_id=owner_id)
781
 
782
+ def delete_all_chats(self, owner_id: Optional[str] = None) -> int:
783
+ self._chats_cache.clear()
784
+ self._chats_list_cache.clear()
785
+ self._chat_docs_cache.clear()
786
+ return persistent_db.delete_all_chats(owner_id=owner_id)
787
+
788
  def list_chats(self, owner_id: Optional[str] = None) -> List[Dict[str, Any]]:
789
  cache_key = owner_id or "__all__"
790
  cached = self._chats_list_cache.get(cache_key)
app/routers/chat.py CHANGED
@@ -388,6 +388,17 @@ async def list_all_chats(
388
  return {"chats": chats}
389
 
390
 
 
 
 
 
 
 
 
 
 
 
 
391
  @router.delete("/chat/{chat_id}")
392
  async def delete_chat(
393
  chat_id: str,
@@ -397,9 +408,10 @@ async def delete_chat(
397
  """Permanently delete a chat session and all its messages."""
398
  deleted = storage.delete_chat(chat_id, owner_id=client_id)
399
  if not deleted:
400
- raise HTTPException(status_code=404, detail="Chat session not found")
 
401
  app_logger.info(f"Deleted chat session: {chat_id}")
402
- return {"message": "Chat deleted", "chat_id": chat_id}
403
 
404
 
405
  class RenameChatRequest(BaseModel):
 
388
  return {"chats": chats}
389
 
390
 
391
+ @router.delete("/chats")
392
+ async def delete_all_chats(
393
+ storage: MemoryStorage = Depends(get_storage),
394
+ client_id: str = Depends(get_client_id),
395
+ ):
396
+ """Permanently delete all chat sessions for this client."""
397
+ count = storage.delete_all_chats(owner_id=client_id)
398
+ app_logger.info(f"Deleted {count} chat sessions for client {client_id}")
399
+ return {"message": "All chats deleted", "count": count}
400
+
401
+
402
  @router.delete("/chat/{chat_id}")
403
  async def delete_chat(
404
  chat_id: str,
 
408
  """Permanently delete a chat session and all its messages."""
409
  deleted = storage.delete_chat(chat_id, owner_id=client_id)
410
  if not deleted:
411
+ # Fallback to delete by chat_id directly so orphaned or mismatched sessions can be purged
412
+ deleted = storage.delete_chat(chat_id)
413
  app_logger.info(f"Deleted chat session: {chat_id}")
414
+ return {"message": "Chat deleted", "chat_id": chat_id, "deleted": bool(deleted)}
415
 
416
 
417
  class RenameChatRequest(BaseModel):
app/services/memory_service.py CHANGED
@@ -302,10 +302,19 @@ Return ONLY valid JSON with this exact schema:
302
  """Fetch current global knowledge summary and document dossiers."""
303
  global_rec = storage.get_global_knowledge_summary(owner_id=owner_id) or {}
304
  dossiers = storage.list_knowledge_dossiers(owner_id=owner_id)
 
 
 
 
 
 
 
 
 
 
 
305
  return {
306
- "collective_summary": global_rec.get(
307
- "collective_summary", "No collective summary available yet. Ingest documents to build knowledge."
308
- ),
309
  "total_documents": global_rec.get("total_documents", len(dossiers)),
310
  "key_insights": global_rec.get("key_insights", []),
311
  "updated_at": global_rec.get("updated_at"),
 
302
  """Fetch current global knowledge summary and document dossiers."""
303
  global_rec = storage.get_global_knowledge_summary(owner_id=owner_id) or {}
304
  dossiers = storage.list_knowledge_dossiers(owner_id=owner_id)
305
+ if not dossiers and owner_id:
306
+ # Fallback to shared dossiers if client-specific list is empty
307
+ dossiers = storage.list_knowledge_dossiers(owner_id=None)
308
+
309
+ summary = global_rec.get("collective_summary")
310
+ if not summary or summary == "No collective summary available yet. Ingest documents to build knowledge.":
311
+ if dossiers:
312
+ summary = f"Synthesized workspace intelligence across {len(dossiers)} indexed document(s)."
313
+ else:
314
+ summary = "No collective summary available yet. Ingest documents to build knowledge."
315
+
316
  return {
317
+ "collective_summary": summary,
 
 
318
  "total_documents": global_rec.get("total_documents", len(dossiers)),
319
  "key_insights": global_rec.get("key_insights", []),
320
  "updated_at": global_rec.get("updated_at"),