refactor(models): pass session to tools.py @property accessors - #40910
Open
morning-verlu wants to merge 5 commits into
Open
refactor(models): pass session to tools.py @property accessors#40910morning-verlu wants to merge 5 commits into
morning-verlu wants to merge 5 commits into
Conversation
Contributor
Pyrefly Type Coverage
|
Remove @Property decorators from 6 accessors in api/models/tools.py that internally call db.session, replacing them with plain methods that accept session: Session as an explicit parameter. Updated accessors: - ApiToolProvider.user, ApiToolProvider.tenant - WorkflowToolProvider.user, WorkflowToolProvider.tenant, WorkflowToolProvider.app - MCPToolProvider.load_user Updated all call sites in: - api/core/tools/custom_tool/provider.py - api/services/tools/tools_transform_service.py - api/services/tools/mcp_tools_manage_service.py Follows the pattern established in langgenius#40370.
morning-verlu
force-pushed
the
refactor/tools-session-param
branch
from
August 18, 2026 03:46
4ff083c to
1341dcf
Compare
Contributor
Pyrefly Diffbase → PR--- /tmp/pyrefly_base.txt 2026-08-18 15:20:27.505019422 +0000
+++ /tmp/pyrefly_pr.txt 2026-08-18 15:20:19.877983837 +0000
@@ -5473,11 +5473,11 @@
ERROR `SimpleNamespace` is not assignable to upper bound `AppGenerateEntity` of type variable `AppGenerateEntityT` [bad-specialization]
--> tests/unit_tests/core/app/task_pipeline/test_based_generate_task_pipeline.py:62:41
ERROR Class member `_FakeQueueManager.listen` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
- --> tests/unit_tests/core/app/task_pipeline/test_easy_ui_based_generate_task_pipeline_core.py:111:9
+ --> tests/unit_tests/core/app/task_pipeline/test_easy_ui_based_generate_task_pipeline_core.py:109:9
ERROR Class member `_FakeQueueManager.publish` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
- --> tests/unit_tests/core/app/task_pipeline/test_easy_ui_based_generate_task_pipeline_core.py:114:9
+ --> tests/unit_tests/core/app/task_pipeline/test_easy_ui_based_generate_task_pipeline_core.py:112:9
ERROR Class member `_FakeQueueManager._publish` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
- --> tests/unit_tests/core/app/task_pipeline/test_easy_ui_based_generate_task_pipeline_core.py:117:9
+ --> tests/unit_tests/core/app/task_pipeline/test_easy_ui_based_generate_task_pipeline_core.py:115:9
ERROR Argument `list[FromClause]` is not assignable to parameter `tables` with type `Sequence[Table] | None` in function `sqlalchemy.sql.schema.MetaData.create_all` [bad-argument-type]
--> tests/unit_tests/core/app/task_pipeline/test_message_cycle_manager_optimization.py:37:16
ERROR `None` is not subscriptable [unsupported-operation]
|
Contributor
|
The autofix failed. |
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors tool-provider model accessors in api/models/tools.py to remove implicit db.session access by converting @property accessors into explicit methods that accept session: Session, and updates affected service/controller call sites to pass a session explicitly.
Changes:
- Converted
ApiToolProvider.user/tenantandWorkflowToolProvider.user/tenant/appfrom@propertyto methods requiringsession: Session. - Updated MCP provider user loading to require an explicit session (
MCPToolProvider.load_user(session=...)) and adjusted service call sites. - Updated unit test stubs to match the new callable
user(session=...)interface.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| api/models/tools.py | Replaces implicit DB session usage in provider accessors with explicit Session parameters. |
| api/services/tools/tools_transform_service.py | Passes an explicit session when resolving provider user details during API transforms. |
| api/services/tools/mcp_tools_manage_service.py | Passes service session into MCP provider user loading for API responses. |
| api/core/tools/custom_tool/provider.py | Updates custom tool provider controller construction to fetch user via explicit session. |
| api/tests/unit_tests/core/tools/test_custom_tool_provider.py | Updates test double to provide user(session=...) callable consistent with the new interface. |
Suppressed comments (1)
api/services/tools/mcp_tools_manage_service.py:699
_build_tool_provider_response()loads the user withself._session, butToolTransformService.mcp_tool_to_user_tool()will load the user again (viadb.session()) whenuser_nameis not provided. Reuse the already-loadeduser_nameto avoid an extra query and avoid mixing sessions.
user = db_provider.load_user(session=self._session)
response = provider_entity.to_api_response(
user_name=user.name if user else None,
)
response["tools"] = ToolTransformService.mcp_tool_to_user_tool(db_provider, tools)
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+314
to
318
| if db_provider.user(session=db.session()) is None: | ||
| raise ValueError(f"user is None for api provider {db_provider.id}") | ||
| try: | ||
| user = db_provider.user | ||
| user = db_provider.user(session=db.session()) | ||
| if not user: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Refactor
@propertyaccessors inapi/models/tools.pythat internally calldb.sessioninto plain methods acceptingsession: Sessionas an explicit parameter, following the pattern established in #40370.Changes
ApiToolProvider.userandApiToolProvider.tenant— removed@property, addedsession: SessionparameterWorkflowToolProvider.user,WorkflowToolProvider.tenant, andWorkflowToolProvider.app— same treatmentMCPToolProvider.load_user— addedsession: Sessionparametercustom_tool/provider.py,tools_transform_service.py, andmcp_tools_manage_service.pyto pass session explicitlyChecklist
Fixes #40372 (tools.py portion)
This PR was prepared with AI assistance. All changes have been manually reviewed and verified.