import asyncio import uuid from core.database import async_session_factory from models.kb_article import KBArticle from api.v1.kb import _execute_article_indexing_lifecycle from sqlalchemy import select async def verify_kb_autocat(): async with async_session_factory() as db: # 1. Create a test article with NO category test_id = uuid.uuid4() # Get a real workspace and author ws_id = (await db.execute(select(KBArticle.workspace_id).limit(1))).scalar_one() auth_id = (await db.execute(select(KBArticle.author_id).limit(1))).scalar_one() article = KBArticle( id=test_id, title="How to Secure Your API Keys", content="To secure your API keys, use environment variables and never commit them to version control. Rotate them every 90 days.", category=None, # Explicitly missing workspace_id=ws_id, author_id=auth_id, is_published=True ) db.add(article) await db.commit() print(f"CREATED Article {test_id} with category='{article.category}'") # 2. Manually trigger the indexing lifecycle print("TRIGGERING Auto-Categorization Pipeline...") try: await _execute_article_indexing_lifecycle(db, test_id) except Exception as e: print(f"PIPELINE CRASHED: {e}") # 3. Refresh and Inspect # We need to expire the session or re-fetch to see background commits await db.rollback() # Ensure we don't have stale state res = await db.execute(select(KBArticle).where(KBArticle.id == test_id)) fresh_article = res.scalar_one_or_none() print(f"FINAL RESULT: Category for {test_id} is now '{fresh_article.category}'") if fresh_article.category and fresh_article.category not in [None, "N/A", "General"]: print("SUCCESS: Auto-categorization is ACTIVE.") else: print("FAILURE: Multi-Indexing lifecycle did not update the category.") if __name__ == "__main__": asyncio.run(verify_kb_autocat())