refactor: decouple workflow knowledge access via repository injection#32071
refactor: decouple workflow knowledge access via repository injection#32071shuv-amp wants to merge 2 commits intolanggenius:mainfrom
Conversation
Summary of ChangesHello @shuv-amp, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a significant architectural improvement by implementing a repository pattern for knowledge base access within the workflow system. By decoupling data access logic from core workflow components, it enhances modularity, simplifies testing, and provides a more robust and maintainable structure for handling knowledge-related operations across different execution contexts. The changes ensure that database sessions are managed per-call, preventing shared state issues in concurrent environments. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request is a well-executed architectural refactoring that successfully decouples the KnowledgeRetrievalNode from direct database access. The introduction of the KnowledgeRepository protocol and its SQLAlchemy implementation, along with the use of dependency injection to provide repositories to the workflow context, significantly improves maintainability, testability, and separation of concerns. The changes are systematic and clean, and the new unit tests for the KnowledgeRetrievalNode are a great addition. I have one suggestion regarding a potential architectural layering improvement.
There was a problem hiding this comment.
Pull request overview
This pull request decouples the KnowledgeRetrievalNode from direct database access by introducing a repository pattern with dependency injection. The changes improve testability, maintainability, and architectural boundaries within the workflow execution system.
Changes:
- Introduced a KnowledgeRepository protocol and SQLAlchemyKnowledgeRepository implementation with per-method session management to ensure thread safety
- Added a Repositories container and injection mechanism through GraphInitParams to propagate repository instances through workflow execution paths
- Refactored KnowledgeRetrievalNode to use repository methods instead of direct db.session access, and added comprehensive unit tests with mocks
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| api/core/workflow/repositories/knowledge_repository.py | Defines Protocol interfaces for Dataset, Document, and KnowledgeRepository operations |
| api/core/repositories/sqlalchemy_knowledge_repository.py | Implements KnowledgeRepository with SQLAlchemy, using per-method sessions for thread safety |
| api/core/workflow/entities/repositories.py | Introduces Repositories container for dependency injection |
| api/core/workflow/entities/graph_init_params.py | Adds optional repositories field to GraphInitParams for propagation |
| api/core/workflow/nodes/knowledge_retrieval/knowledge_retrieval_node.py | Refactors node to use injected repository instead of direct db access |
| api/core/workflow/workflow_entry.py | Adds repositories parameter to single_step_run method |
| api/core/app/apps/workflow/app_generator.py | Creates and injects Repositories instance in workflow runner |
| api/core/app/apps/workflow/app_runner.py | Accepts and propagates repositories through workflow execution |
| api/core/app/apps/workflow_app_runner.py | Propagates repositories through graph initialization paths |
| api/core/app/apps/pipeline/pipeline_runner.py | Creates and injects repositories for RAG pipeline execution |
| api/core/workflow/nodes/iteration/iteration_node.py | Propagates repositories to nested graph engines |
| api/core/workflow/nodes/loop/loop_node.py | Propagates repositories to nested graph engines |
| api/services/workflow_service.py | Creates repository instances for draft workflow node execution |
| api/services/rag_pipeline/rag_pipeline.py | Creates repository instances for RAG pipeline draft node execution |
| api/tests/unit_tests/core/workflow/nodes/knowledge_retrieval/test_retrieval_node_mock.py | Adds comprehensive unit tests verifying repository usage and error handling |
| api/tests/unit_tests/core/app/apps/test_workflow_app_runner_single_node.py | Updates test to account for new repositories parameter |
| api/core/workflow/repositories/init.py | Refactors imports to use lazy loading pattern to avoid circular dependencies |
| api/.importlinter | Removes obsolete ignore rules for removed direct database dependencies |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # avoid blocking at retrieval | ||
| db.session.close() | ||
| knowledge_repo.close_session() |
There was a problem hiding this comment.
The call to knowledge_repo.close_session() here is redundant. Since the SQLAlchemyKnowledgeRepository implementation uses per-method sessions (each repository method creates and closes its own session with context managers), this explicit call to close_session() does nothing - it's implemented as a no-op that simply returns. This line can be safely removed as it serves no purpose with the new repository design.
|
addressed the review points: moved metadata filter translation into the sqlalchemy knowledge repository to avoid the cross-layer dependency, and removed the redundant knowledge_repo.close_session call in the knowledge retrieval node. reran: uv run --project api pytest api/tests/unit_tests/core/workflow api/tests/unit_tests/core/app/apps -q (625 passed, 3 skipped). |
|
Please link an issue in the description :) |
|
Thanks for the review @crazywoola! I've linked issue #30269 in the description as requested. Appreciate the feedback! |
|
Thanks for the AI review feedback! I went ahead and moved that metadata filter logic into the repository to fix the layering issue. I also removed the redundant session close call. Everything should be sorted now, just waiting on the CI to run. |
Summary
Testing
Related Issue
Part of #30269.
Implements the Repository pattern to decouple the Graph Engine from Flask-SQLAlchemy, setting the stage for the FastAPI migration.