Skip to content

refactor: pass db.session explicitly in 8 services (#37403)#37793

Open
EvanYao826 wants to merge 4 commits into
langgenius:mainfrom
EvanYao826:refactor/db-session-parameter-batch2
Open

refactor: pass db.session explicitly in 8 services (#37403)#37793
EvanYao826 wants to merge 4 commits into
langgenius:mainfrom
EvanYao826:refactor/db-session-parameter-batch2

Conversation

@EvanYao826

Copy link
Copy Markdown
Contributor

Summary

Migrate db.session usage to explicit session parameter in 8 service files, following the pattern established in #37402.

Service files changed:

  • api/services/clear_free_plan_tenant_expired_logs.py
  • api/services/knowledge_retrieval_inner_service.py
  • api/services/account_service.py
  • api/services/credit_pool_service.py
  • api/services/hit_testing_service.py
  • api/services/webapp_auth_service.py
  • api/services/workspace_service.py
  • api/services/agent/skill_tool_inference_service.py

Controllers updated:

  • api/controllers/console/app/agent.py
  • api/controllers/console/datasets/external.py
  • api/controllers/console/datasets/hit_testing_base.py
  • api/controllers/console/workspace/workspace.py
  • api/controllers/inner_api/knowledge/retrieval.py

Tests updated:

  • api/tests/unit_tests/services/agent/test_skill_tool_inference_service.py

Pattern

# Before
def is_tenant_owner_or_admin(user):
    account = db.session.scalar(select(Account)...)

# After  
def is_tenant_owner_or_admin(session: Session, user):
    account = session.scalar(select(Account)...)

All callers (controllers) updated to pass db.session explicitly.

Part of #37403 (incremental; not using Fixes since the umbrella issue spans many services).

…#37403)

Migrate db.session to explicit session parameter in 8 service files,
following the pattern established in langgenius#37402.

### Service files:
- clear_free_plan_tenant_expired_logs.py
- knowledge_retrieval_inner_service.py
- account_service.py
- credit_pool_service.py
- hit_testing_service.py
- webapp_auth_service.py
- workspace_service.py
- agent/skill_tool_inference_service.py

### Controllers:
- console/app/agent.py
- console/datasets/external.py
- console/datasets/hit_testing_base.py
- console/workspace/workspace.py
- inner_api/knowledge/retrieval.py

### Tests:
- services/agent/test_skill_tool_inference_service.py

Closes langgenius#37403 (partial)
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. refactor labels Jun 23, 2026
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Jun 23, 2026
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jun 23, 2026
@github-actions

github-actions Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-06-24 06:04:09.880950948 +0000
+++ /tmp/pyrefly_pr.txt	2026-06-24 06:03:55.163762163 +0000
@@ -1,3 +1,7 @@
+ERROR Parse error: Duplicate keyword argument "session" [parse-error]
+   --> controllers/console/datasets/external.py:400:17
+ERROR Multiple values for argument `session` in function `services.hit_testing_service.HitTestingService.external_retrieve` [bad-keyword-argument]
+   --> controllers/console/datasets/external.py:400:17
 ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset` with type `Dataset` in function `dify_vdb_alibabacloud_mysql.alibabacloud_mysql_vector.AlibabaCloudMySQLVectorFactory.init_vector` [bad-argument-type]
   --> providers/vdb/vdb-alibabacloud-mysql/tests/unit_tests/test_alibabacloud_mysql_factory.py:42:38
 ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset` with type `Dataset` in function `dify_vdb_alibabacloud_mysql.alibabacloud_mysql_vector.AlibabaCloudMySQLVectorFactory.init_vector` [bad-argument-type]
@@ -460,6 +464,14 @@
    --> providers/vdb/vdb-weaviate/tests/unit_tests/test_weaviate_vector.py:887:42
 ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset` with type `Dataset` in function `dify_vdb_weaviate.weaviate_vector.WeaviateVectorFactory.init_vector` [bad-argument-type]
    --> providers/vdb/vdb-weaviate/tests/unit_tests/test_weaviate_vector.py:916:42
+ERROR Duplicate parameter "session" [invalid-syntax]
+   --> services/hit_testing_service.py:118:9
+ERROR Duplicate parameter "session" [invalid-syntax]
+   --> services/hit_testing_service.py:203:9
+ERROR Could not find name `db` [unknown-name]
+  --> services/webapp_auth_service.py:38:74
+ERROR Could not find name `db` [unknown-name]
+  --> services/webapp_auth_service.py:58:74
 ERROR Object of class `FromClause` has no attribute `metadata` [missing-attribute]
   --> tests/helpers/legacy_model_type_migration.py:64:16
 ERROR Object of class `FromClause` has no attribute `metadata` [missing-attribute]
@@ -1146,6 +1158,8 @@
     --> tests/test_containers_integration_tests/services/test_app_service.py:1571:41
 ERROR Argument `Literal['owner']` is not assignable to parameter `role` with type `SQLCoreOperations[TenantAccountRole] | TenantAccountRole` in function `models.account.TenantAccountJoin.__init__` [bad-argument-type]
   --> tests/test_containers_integration_tests/services/test_conversation_service_variables.py:47:18
+ERROR Missing argument `session` in function `services.credit_pool_service.CreditPoolService.create_default_pool` [missing-argument]
+  --> tests/test_containers_integration_tests/services/test_credit_pool_service.py:42:53
 ERROR Argument `Literal['active']` is not assignable to parameter `status` with type `AccountStatus | SQLCoreOperations[AccountStatus]` in function `models.account.Account.__init__` [bad-argument-type]
   --> tests/test_containers_integration_tests/services/test_dataset_permission_service.py:39:20
 ERROR Argument `Literal['normal']` is not assignable to parameter `status` with type `SQLCoreOperations[TenantStatus] | TenantStatus` in function `models.account.Tenant.__init__` [bad-argument-type]
@@ -1394,8 +1408,14 @@
    --> tests/test_containers_integration_tests/services/test_webapp_auth_service.py:142:20
 ERROR Argument `Literal['active']` is not assignable to parameter `status` with type `AccountStatus | SQLCoreOperations[AccountStatus]` in function `models.account.Account.__init__` [bad-argument-type]
    --> tests/test_containers_integration_tests/services/test_webapp_auth_service.py:345:20
+ERROR Missing argument `session` in function `services.webapp_auth_service.WebAppAuthService.create_end_user` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_webapp_auth_service.py:662:51
+ERROR Missing argument `session` in function `services.webapp_auth_service.WebAppAuthService.create_end_user` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_webapp_auth_service.py:697:46
 ERROR Argument `Literal['normal']` is not assignable to parameter `status` with type `SQLCoreOperations[TenantStatus] | TenantStatus` in function `models.account.Tenant.__init__` [bad-argument-type]
    --> tests/test_containers_integration_tests/services/test_webapp_auth_service.py:715:20
+ERROR Missing argument `session` in function `services.webapp_auth_service.WebAppAuthService.create_end_user` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_webapp_auth_service.py:735:46
 ERROR Argument `dict[str, dict[str, list[dict[str, bool | str]] | str]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.webhook_service.WebhookService.extract_and_validate_webhook_data` [bad-argument-type]
    --> tests/test_containers_integration_tests/services/test_webhook_service.py:289:88
 ERROR Argument `dict[str, dict[str, str]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.webhook_service.WebhookService.extract_and_validate_webhook_data` [bad-argument-type]
@@ -1472,20 +1492,32 @@
   --> tests/test_containers_integration_tests/services/test_workspace_service.py:53:20
 ERROR Argument `Literal['normal']` is not assignable to parameter `status` with type `SQLCoreOperations[TenantStatus] | TenantStatus` in function `models.account.Tenant.__init__` [bad-argument-type]
   --> tests/test_containers_integration_tests/services/test_workspace_service.py:62:20
+ERROR Missing argument `session` in function `services.workspace_service.WorkspaceService.get_tenant_info` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_workspace_service.py:107:54
 ERROR Cannot index into `object` [bad-index]
    --> tests/test_containers_integration_tests/services/test_workspace_service.py:121:20
 ERROR `in` is not supported between `Literal['replace_webapp_logo']` and `object` [not-iterable]
    --> tests/test_containers_integration_tests/services/test_workspace_service.py:122:20
+ERROR Missing argument `session` in function `services.workspace_service.WorkspaceService.get_tenant_info` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_workspace_service.py:154:54
 ERROR Object of class `NoneType` has no attribute `role` [missing-attribute]
    --> tests/test_containers_integration_tests/services/test_workspace_service.py:199:9
+ERROR Missing argument `session` in function `services.workspace_service.WorkspaceService.get_tenant_info` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_workspace_service.py:209:54
 ERROR Object of class `NoneType` has no attribute `role` [missing-attribute]
    --> tests/test_containers_integration_tests/services/test_workspace_service.py:253:9
+ERROR Missing argument `session` in function `services.workspace_service.WorkspaceService.get_tenant_info` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_workspace_service.py:264:54
 ERROR Cannot index into `object` [bad-index]
    --> tests/test_containers_integration_tests/services/test_workspace_service.py:272:20
 ERROR `in` is not supported between `Literal['replace_webapp_logo']` and `object` [not-iterable]
    --> tests/test_containers_integration_tests/services/test_workspace_service.py:273:20
+ERROR Missing argument `session` in function `services.workspace_service.WorkspaceService.get_tenant_info` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_workspace_service.py:294:50
 ERROR Argument `None` is not assignable to parameter `tenant` with type `Tenant` in function `services.workspace_service.WorkspaceService.get_tenant_info` [bad-argument-type]
    --> tests/test_containers_integration_tests/services/test_workspace_service.py:294:51
+ERROR Missing argument `session` in function `services.workspace_service.WorkspaceService.get_tenant_info` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_workspace_service.py:344:58
 ERROR `in` is not supported between `Literal['replace_webapp_logo']` and `object` [not-iterable]
    --> tests/test_containers_integration_tests/services/test_workspace_service.py:351:28
 ERROR Cannot index into `object` [bad-index]
@@ -1496,8 +1528,14 @@
    --> tests/test_containers_integration_tests/services/test_workspace_service.py:358:24
 ERROR Object of class `NoneType` has no attribute `role` [missing-attribute]
    --> tests/test_containers_integration_tests/services/test_workspace_service.py:389:9
+ERROR Missing argument `session` in function `services.workspace_service.WorkspaceService.get_tenant_info` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_workspace_service.py:401:54
 ERROR Object of class `NoneType` has no attribute `role` [missing-attribute]
    --> tests/test_containers_integration_tests/services/test_workspace_service.py:439:9
+ERROR Missing argument `session` in function `services.workspace_service.WorkspaceService.get_tenant_info` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_workspace_service.py:451:54
+ERROR Missing argument `session` in function `services.workspace_service.WorkspaceService.get_tenant_info` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_workspace_service.py:516:58
 ERROR `in` is not supported between `Literal['replace_webapp_logo']` and `object` [not-iterable]
    --> tests/test_containers_integration_tests/services/test_workspace_service.py:524:28
 ERROR Cannot index into `object` [bad-index]
@@ -1512,10 +1550,32 @@
    --> tests/test_containers_integration_tests/services/test_workspace_service.py:545:100
 ERROR Argument `Literal['normal']` is not assignable to parameter `status` with type `SQLCoreOperations[TenantStatus] | TenantStatus` in function `models.account.Tenant.__init__` [bad-argument-type]
    --> tests/test_containers_integration_tests/services/test_workspace_service.py:549:53
+ERROR Missing argument `session` in function `services.workspace_service.WorkspaceService.get_tenant_info` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_workspace_service.py:556:49
+ERROR Missing argument `session` in function `services.workspace_service.WorkspaceService.get_tenant_info` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_workspace_service.py:575:54
 ERROR Cannot index into `object` [bad-index]
    --> tests/test_containers_integration_tests/services/test_workspace_service.py:578:16
+ERROR Missing argument `session` in function `services.workspace_service.WorkspaceService.get_tenant_info` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_workspace_service.py:599:54
 ERROR Cannot index into `object` [bad-index]
    --> tests/test_containers_integration_tests/services/test_workspace_service.py:602:16
+ERROR Missing argument `session` in function `services.workspace_service.WorkspaceService.get_tenant_info` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_workspace_service.py:618:54
+ERROR Missing argument `session` in function `services.workspace_service.WorkspaceService.get_tenant_info` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_workspace_service.py:645:54
+ERROR Missing argument `session` in function `services.workspace_service.WorkspaceService.get_tenant_info` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_workspace_service.py:672:54
+ERROR Missing argument `session` in function `services.workspace_service.WorkspaceService.get_tenant_info` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_workspace_service.py:700:54
+ERROR Missing argument `session` in function `services.workspace_service.WorkspaceService.get_tenant_info` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_workspace_service.py:729:54
+ERROR Missing argument `session` in function `services.workspace_service.WorkspaceService.get_tenant_info` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_workspace_service.py:757:54
+ERROR Missing argument `session` in function `services.workspace_service.WorkspaceService.get_tenant_info` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_workspace_service.py:788:54
+ERROR Missing argument `session` in function `services.workspace_service.WorkspaceService.get_tenant_info` [missing-argument]
+   --> tests/test_containers_integration_tests/services/test_workspace_service.py:814:54
 ERROR Argument `Literal['active']` is not assignable to parameter `status` with type `AccountStatus | SQLCoreOperations[AccountStatus]` in function `models.account.Account.__init__` [bad-argument-type]
   --> tests/test_containers_integration_tests/services/tools/test_api_tools_manage_service.py:60:20
 ERROR Argument `Literal['normal']` is not assignable to parameter `status` with type `SQLCoreOperations[TenantStatus] | TenantStatus` in function `models.account.Tenant.__init__` [bad-argument-type]
@@ -6872,6 +6932,8 @@
    --> tests/unit_tests/services/test_clear_free_plan_expired_workflow_run_logs.py:126:49
 ERROR Class member `FailingRepo.delete_runs_with_related_by_ids` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
    --> tests/unit_tests/services/test_clear_free_plan_expired_workflow_run_logs.py:394:13
+ERROR Missing argument `session` in function `services.clear_free_plan_tenant_expired_logs.ClearFreePlanTenantExpiredLogs.process_tenant` [missing-argument]
+   --> tests/unit_tests/services/test_clear_free_plan_tenant_expired_logs.py:328:50
 ERROR Class member `FixedDateTime.now` overrides parent class `datetime` in an inconsistent manner [bad-override]
    --> tests/unit_tests/services/test_clear_free_plan_tenant_expired_logs.py:403:13
 ERROR Class member `FixedDateTime.now` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
@@ -6880,6 +6942,8 @@
    --> tests/unit_tests/services/test_clear_free_plan_tenant_expired_logs.py:478:13
 ERROR Class member `FixedDateTime.now` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
    --> tests/unit_tests/services/test_clear_free_plan_tenant_expired_logs.py:478:13
+ERROR Missing argument `session` in function `services.clear_free_plan_tenant_expired_logs.ClearFreePlanTenantExpiredLogs.process_tenant` [missing-argument]
+   --> tests/unit_tests/services/test_clear_free_plan_tenant_expired_logs.py:586:50
 ERROR Argument `SimpleNamespace` is not assignable to parameter `user` with type `Account` in function `services.credential_permission_service.CredentialPermissionService.apply_visibility_filter` [bad-argument-type]
   --> tests/unit_tests/services/test_credential_permission_service.py:80:18
 ERROR Argument `SimpleNamespace` is not assignable to parameter `user` with type `Account` in function `services.credential_permission_service.CredentialPermissionService.apply_visibility_filter` [bad-argument-type]

@github-actions

github-actions Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 50.92% 50.93% +0.01%
Strict coverage 50.43% 50.44% +0.01%
Typed symbols 30,211 30,218 +7
Untyped symbols 29,400 29,400 0
Modules 2928 2928 0

Comment thread api/controllers/console/datasets/hit_testing_base.py Outdated
@asukaminato0721

Copy link
Copy Markdown
Contributor

fix ci

@asukaminato0721 asukaminato0721 self-assigned this Jun 23, 2026
@EvanYao826

Copy link
Copy Markdown
Contributor Author

Checking issue-level comments...

@EvanYao826

Copy link
Copy Markdown
Contributor Author

Hi @asukaminato0721, thanks for the review! I noticed you left an empty suggestion on hit_testing_base.py. Since retrieve() now requires session: scoped_session (no default), the caller must pass session=db.session explicitly. Is there a specific change you'd like me to make here, or is this good to go?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer 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.

2 participants