Skip to content

refactor(models): pass session to tools.py @property accessors - #40910

Open
morning-verlu wants to merge 5 commits into
langgenius:mainfrom
morning-verlu:refactor/tools-session-param
Open

refactor(models): pass session to tools.py @property accessors#40910
morning-verlu wants to merge 5 commits into
langgenius:mainfrom
morning-verlu:refactor/tools-session-param

Conversation

@morning-verlu

Copy link
Copy Markdown

Summary

Refactor @property accessors in api/models/tools.py that internally call db.session into plain methods accepting session: Session as an explicit parameter, following the pattern established in #40370.

Changes

  • ApiToolProvider.user and ApiToolProvider.tenant — removed @property, added session: Session parameter
  • WorkflowToolProvider.user, WorkflowToolProvider.tenant, and WorkflowToolProvider.app — same treatment
  • MCPToolProvider.load_user — added session: Session parameter
  • Updated all call sites in custom_tool/provider.py, tools_transform_service.py, and mcp_tools_manage_service.py to pass session explicitly

Checklist

Fixes #40372 (tools.py portion)


This PR was prepared with AI assistance. All changes have been manually reviewed and verified.

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. refactor labels Aug 18, 2026
@github-actions

github-actions Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 59.69% 59.69% -0.01%
Strict coverage 59.28% 59.27% -0.01%
Typed symbols 39,917 39,904 -13
Untyped symbols 27,142 27,142 0
Modules 3181 3181 0

morning-verlu and others added 3 commits August 18, 2026 11:46
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
morning-verlu force-pushed the refactor/tools-session-param branch from 4ff083c to 1341dcf Compare August 18, 2026 03:46
@github-actions

github-actions Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → 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]

@wylswz

wylswz commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

The autofix failed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/tenant and WorkflowToolProvider.user/tenant/app from @property to methods requiring session: 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 with self._session, but ToolTransformService.mcp_tool_to_user_tool() will load the user again (via db.session()) when user_name is not provided. Reuse the already-loaded user_name to 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:
Comment thread api/tests/unit_tests/core/tools/test_custom_tool_provider.py Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rewrite property to accept session: Session as parameter

4 participants