This commit is contained in:
2026-05-27 16:05:04 +03:00
parent 0d05a0bd6d
commit ce49760e5a
4 changed files with 43 additions and 17 deletions

View File

@@ -8,7 +8,7 @@
from __future__ import annotations
from datetime import UTC, datetime
from datetime import datetime
from typing import Any
from fastapi import Depends, HTTPException, Query
@@ -69,7 +69,7 @@ async def list_conversations(
conv_id = row.conversation_id
if not conv_id:
continue
created_at = row.created_at or datetime.now(UTC)
created_at = row.created_at or datetime.now()
if conv_id not in summary:
summary[conv_id] = {
"conversation_id": conv_id,

View File

@@ -301,7 +301,7 @@ def _resolve_or_create_conversation(
# @PRE db session is available and user_id references current actor scope.
# @POST Messages older than ASSISTANT_MESSAGE_TTL_DAYS are removed from persistence and memory mirrors.
def _cleanup_history_ttl(db: Session, user_id: str):
cutoff = datetime.now(UTC) - timedelta(days=ASSISTANT_MESSAGE_TTL_DAYS)
cutoff = datetime.now() - timedelta(days=ASSISTANT_MESSAGE_TTL_DAYS)
try:
query = db.query(AssistantMessageRecord).filter(
AssistantMessageRecord.user_id == user_id,
@@ -323,8 +323,11 @@ def _cleanup_history_ttl(db: Session, user_id: str):
kept = []
for item in items:
created_at = item.get("created_at")
if isinstance(created_at, datetime) and created_at < cutoff:
continue
if isinstance(created_at, datetime):
# DB stores naive datetimes; in-memory items may be aware (from datetime.now(UTC))
ref = created_at.replace(tzinfo=None) if created_at.tzinfo else created_at
if ref < cutoff:
continue
kept.append(item)
if kept:
CONVERSATIONS[key] = kept
@@ -344,8 +347,10 @@ def _cleanup_history_ttl(db: Session, user_id: str):
def _is_conversation_archived(updated_at: datetime | None) -> bool:
if not updated_at:
return False
cutoff = datetime.now(UTC) - timedelta(days=ASSISTANT_ARCHIVE_AFTER_DAYS)
return updated_at < cutoff
cutoff = datetime.now() - timedelta(days=ASSISTANT_ARCHIVE_AFTER_DAYS)
# Ensure both operands are naive (DB stores naive datetimes)
ref = updated_at.replace(tzinfo=None) if updated_at.tzinfo else updated_at
return ref < cutoff
# #endregion _is_conversation_archived