Repository navigation
refactor(api): read custom API tool providers through an explicitly owned session - #46983
Merged
asukaminato0721 merged 2 commits intoNov 27, 2026
Merged
asukaminato0721 merged 2 commits into
asukaminato0721 merged 2 commits into
Conversation
…wned session ApiToolProviderController.get_tools queried ApiToolProvider through the global db.session. It now uses a short-lived session from session_factory.create_session(), keeping tenant/name filtering and the provider-to-tool conversion inside the session scope. The cached-tools fast path is unchanged. The unit test no longer monkeypatches the module's db with a scripted session double; it persists real ApiToolProvider rows in SQLite and lets get_tools read them through its own session. Part of langgenius#45584
Mattenhaim
force-pushed
the
refactor/45584-custom-tool-session
branch
from
November 23, 2026 14:06
11db269 to
5aecf26
Compare
Contributor
Pyrefly Type Coverage
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #46983 +/- ##
==========================================
+ Coverage 90.31% 90.37% +0.06%
==========================================
Files 6495 6512 +17
Lines 408426 410154 +1728
Branches 81921 82100 +179
==========================================
+ Hits 368857 370664 +1807
+ Misses 33163 33092 -71
+ Partials 6406 6398 -8
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
asukaminato0721
previously requested changes
Nov 23, 2026
asukaminato0721
left a comment
Contributor
There was a problem hiding this comment.
1 inline suggestion at 5aecf26b6f, from a rule learned from earlier reviews in this repository. Reply on the comment if it is wrong.
dify-codereview
Address review feedback: instead of reaching for the global session_factory, the controller now takes a sessionmaker[Session] as a required constructor parameter. from_db derives it from the caller's session via sessionmaker_for(), so the lazy tool-loading query runs on the same bind without any global state. Tests pass sqlite_session / sqlite_session_factory directly with no monkeypatching.
asukaminato0721
left a comment
Contributor
There was a problem hiding this comment.
Nothing to flag at 1f782b5318.
dify-codereview
asukaminato0721
dismissed
their stale review
November 27, 2026 07:15
Nothing left to flag at 1f782b5.
asukaminato0721
enabled auto-merge
November 27, 2026 13:41
asukaminato0721
approved these changes
Nov 27, 2026
This was referenced Nov 27, 2026
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.
Part of #45584
Summary
ApiToolProviderController.get_tools()still queriedApiToolProviderthrough theglobal
db.session. The controller now takes asessionmaker[Session]as a requiredconstructor parameter, and the lazy tool-loading query runs in a short-lived session
from that injected factory — no global session state is touched:
from_dbderives the factory from the caller's session viasessionmaker_for(),so the query always runs on the same bind as the caller.
session scope; the cached-tools fast path (
self.toolsnon-empty) is unchanged andnever touches the database.
extensions.ext_database.dbimport is dropped.tests/unit_tests/core/tools/custom_tool/test_provider.pyno longer monkeypatchesthe module's
dbwith a scripted session double. The DB-fetch test persists realApiToolProviderrows in SQLite and letsget_toolsread them through its ownsession, covering: matching provider, tenant isolation (a second provider in
another tenant is not returned), no matches after deletion, and the cached path.
test_tool_catalogue.pypasses thesqlite_session_factoryfixture.No behavior change for callers; the public signatures of
from_db/get_tools/get_toolare unchanged.Verification
pytest tests/unit_tests/core/tools/custom_tool/test_provider.py tests/unit_tests/core/workflow/generator/test_tool_catalogue.py— 42 passed locally (Python 3.13, Windows)ruff checkandruff format --checkpass on all touched filesmake type-checkleft to CI: my local checkout has only a partial dependency set (the vdb extras are excluded to keep the Windows sandbox small)Checklist
make lint && make type-check(backend) — ranruff check/ruff formatand the targeted pytest modules instead; see Verification.From WorkBuddy (AI coding assistant), directed by a human contributor.