LightRAG/logs/2024-12-05-02-00-beastmode-pipeline-filter-log.md
2025-12-05 14:31:13 +08:00

2.6 KiB

Task Log: Pipeline Screen Tenant Filtering Fix

Date: 2024-12-05 02:00 Mode: beastmode-chatmode Task: Fix pipeline screen not being filtered by tenant and KB

Summary

Implemented multi-tenant support for document routes to ensure the pipeline screen filters documents by the current tenant and knowledge base (KB) context.

Actions Performed

  1. Updated document_routes.py:

    • Added imports for TenantRAGManager, TenantContext, get_tenant_context_optional
    • Modified create_document_routes() signature to accept optional rag_manager parameter
    • Created get_tenant_rag dependency that returns tenant-specific RAG instance when context is available
    • Updated /pipeline_status endpoint to use tenant_rag dependency for workspace-isolated pipeline status
    • Updated /paginated endpoint to use tenant_rag dependency for tenant-filtered document listing
    • Updated /status_counts endpoint to use tenant_rag dependency for tenant-filtered status counts
  2. Restructured lightrag_server.py:

    • Moved multi-tenant component initialization (TenantRAGManager) before document routes registration
    • Modified create_document_routes() call to pass rag_manager=rag_manager parameter
    • Separated tenant routes registration from multi-tenant initialization

Key Decisions

  • Used FastAPI's Depends() injection to get tenant-specific RAG instance
  • Pattern: tenant_rag: LightRAG = Depends(get_tenant_rag) for tenant-aware endpoints
  • Fallback to global rag when tenant context is not available (single-tenant mode compatibility)
  • workspace = tenant_rag.workspace contains the composite {tenant_id}:{kb_id} pattern for storage isolation

Files Modified

  • lightrag/api/routers/document_routes.py
  • lightrag/api/lightrag_server.py

Next Steps

  • Consider updating graph routes (graph_routes.py) for tenant-aware graph operations
  • Consider updating query routes (query_routes.py) for tenant-aware queries
  • Write/upload operations (upload, delete, etc.) may need similar tenant-aware treatment

Lessons/Insights

  • The document routes were using the global rag instance, which always used the default workspace
  • The fix pattern is: replace rag.workspace with tenant_rag.workspace where tenant_rag comes from the dependency
  • TenantRAGManager must be initialized before registering routes that depend on it
  • The get_tenant_rag dependency gracefully falls back to global RAG for backward compatibility

Testing

  • Ran ruff check on modified files - all checks passed
  • No TypeScript/Python errors detected in modified files