Skip to content

refactor(api): read custom API tool providers through an explicitly owned session - #46983

Merged
asukaminato0721 merged 2 commits into
langgenius:mainfrom
Mattenhaim:refactor/45584-custom-tool-session
Nov 27, 2026
Merged

asukaminato0721 merged 2 commits into
langgenius:mainfrom
Mattenhaim:refactor/45584-custom-tool-session

Conversation

@Mattenhaim

@Mattenhaim Mattenhaim commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Part of #45584

Summary

ApiToolProviderController.get_tools() still queried ApiToolProvider through the
global db.session. The controller now takes a sessionmaker[Session] as a required
constructor parameter, and the lazy tool-loading query runs in a short-lived session
from that injected factory — no global session state is touched:

  • from_db derives the factory from the caller's session via sessionmaker_for(),
    so the query always runs on the same bind as the caller.
  • The tenant/name filtering and the provider-to-tool conversion stay inside the
    session scope; the cached-tools fast path (self.tools non-empty) is unchanged and
    never touches the database.
  • The unused extensions.ext_database.db import is dropped.
  • tests/unit_tests/core/tools/custom_tool/test_provider.py no longer monkeypatches
    the module's db with a scripted session double. The DB-fetch test persists real
    ApiToolProvider rows in SQLite and lets get_tools read them through its own
    session, covering: matching provider, tenant isolation (a second provider in
    another tenant is not returned), no matches after deletion, and the cached path.
  • The direct construction in test_tool_catalogue.py passes the
    sqlite_session_factory fixture.

No behavior change for callers; the public signatures of from_db / get_tools /
get_tool are 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 check and ruff format --check pass on all touched files
  • Full make type-check left to CI: my local checkout has only a partial dependency set (the vdb extras are excluded to keep the Windows sandbox small)

Checklist

  • This change requires a documentation update
  • I understand that this PR may be closed in case there was no previous discussion or issues.
  • I've verified the change and added or updated tests where meaningful regression risk justifies coverage.
  • I've updated the documentation accordingly.
  • I ran make lint && make type-check (backend) — ran ruff check/ruff format and the targeted pytest modules instead; see Verification.

From WorkBuddy (AI coding assistant), directed by a human contributor.

@github-actions github-actions Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Nov 23, 2026
…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
Mattenhaim force-pushed the refactor/45584-custom-tool-session branch from 11db269 to 5aecf26 Compare November 23, 2026 14:06
@github-actions github-actions 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 Nov 23, 2026
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 88.02% 87.94% -0.08%
Strict coverage 87.65% 87.60% -0.05%
Typed symbols 98,532 97,501 -1,031
Untyped symbols 13,470 13,424 -46
Modules 4708 4675 -33

@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.37%. Comparing base (f2b8315) to head (1f782b5).
⚠️ Report is 218 commits behind head on main.

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     
Flag Coverage Δ
api 90.23% <100.00%> (+0.10%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@asukaminato0721 asukaminato0721 self-assigned this Nov 23, 2026

@asukaminato0721 asukaminato0721 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.

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

Comment thread api/core/tools/custom_tool/provider.py Resolved
@asukaminato0721 asukaminato0721 added the waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address label Nov 23, 2026
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 asukaminato0721 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.

Nothing to flag at 1f782b5318.

dify-codereview

@asukaminato0721
asukaminato0721 dismissed their stale review November 27, 2026 07:15

Nothing left to flag at 1f782b5.

@asukaminato0721 asukaminato0721 added waiting-for-reviewer dify-codereview: checked, nothing left for the author; a reviewer's turn and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 27, 2026
@asukaminato0721
asukaminato0721 added this pull request to the merge queue Nov 27, 2026
Merged via the queue into langgenius:main with commit 809bbdd Nov 27, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files. waiting-for-reviewer dify-codereview: checked, nothing left for the author; a reviewer's turn

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants