diff --git a/backend/routers/issues.py b/backend/routers/issues.py index de27e1a9..28a4d844 100644 --- a/backend/routers/issues.py +++ b/backend/routers/issues.py @@ -186,9 +186,6 @@ async def create_issue( hash_content = f"{description}|{category}|{prev_hash}" integrity_hash = hashlib.sha256(hash_content.encode()).hexdigest() - # Update cache for next report - blockchain_last_hash_cache.set(data=integrity_hash, key="last_hash") - # RAG Retrieval (New) relevant_rule = rag_service.retrieve(description) initial_action_plan = None @@ -212,10 +209,16 @@ async def create_issue( # Offload blocking DB operations to threadpool await run_in_threadpool(save_issue_db, db, new_issue) + + # Update cache only after a successful DB commit + blockchain_last_hash_cache.set(data=integrity_hash, key="last_hash") else: # Don't create new issue, just return deduplication info new_issue = None except Exception as e: + # Invalidate cache so next request re-fetches the true last hash from DB + blockchain_last_hash_cache.invalidate("last_hash") + # Clean up uploaded file if DB save failed if image_path and os.path.exists(image_path): try: