Repository navigation
refactor(api): write tool model invoke logs through short-lived sessions - #46632
asukaminato0721 merged 4 commits into
Conversation
ModelInvocationUtils.invoke added the ToolModelInvoke row through the global db.session and committed the response update through it after the model call, which also committed anything else pending in the caller's scoped session. Write the row and the update through their own session_factory sessions so no session is held across invoke_llm. Part of langgenius#45584
Pyrefly Type Coverage
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #46632 +/- ##
==========================================
- Coverage 90.31% 90.30% -0.01%
==========================================
Files 6495 6495
Lines 408368 408371 +3
Branches 81903 81903
==========================================
- Hits 368800 368792 -8
- Misses 33164 33175 +11
Partials 6404 6404
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
left a comment
There was a problem hiding this comment.
2 inline suggestions at 282afe9eb2, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong.
dify-codereview
invoke now takes the session maker for its log row writes as a required keyword-only argument instead of reaching for the global session factory. BuiltinTool.invoke_model and the plugin system-model invocation pass session_factory.get_session_maker(), and the tests hand in sqlite_session_factory directly. Part of langgenius#45584
|
Both suggestions are addressed in 06ff8ef: |
asukaminato0721
left a comment
There was a problem hiding this comment.
Nothing to flag at dc4ef62762.
dify-codereview
Nothing left to flag at dc4ef62.
Pyrefly Diffbase → PR--- /tmp/pyrefly_base.txt 2026-11-22 12:50:36.359486913 +0000
+++ /tmp/pyrefly_pr.txt 2026-11-22 12:50:16.935319531 +0000
@@ -4004,45 +4004,45 @@
ERROR Cannot instantiate `TextSplitter` because the following members are abstract: `split_text` [bad-instantiation]
--> tests/unit_tests/core/rag/splitter/test_text_splitter.py:2208:21
ERROR Argument `Literal[core.entities.provider_entities.ProviderQuotaType.TRIAL]` is not assignable to parameter `quota_type` with type `models.enums.ProviderQuotaType | SQLCoreOperations[models.enums.ProviderQuotaType | None] | None` in function `models.provider.Provider.__init__` [bad-argument-type]
- --> tests/unit_tests/core/test_provider_manager.py:101:20
+ --> tests/unit_tests/core/test_provider_manager.py:125:20
ERROR Argument `Literal['llm']` is not assignable to parameter `model_type` with type `ModelType | SQLCoreOperations[ModelType]` in function `models.provider.ProviderModelSetting.__init__` [bad-argument-type]
- --> tests/unit_tests/core/test_provider_manager.py:159:20
+ --> tests/unit_tests/core/test_provider_manager.py:183:20
ERROR Argument `Literal['llm']` is not assignable to parameter `model_type` with type `ModelType | SQLCoreOperations[ModelType]` in function `models.provider.LoadBalancingModelConfig.__init__` [bad-argument-type]
- --> tests/unit_tests/core/test_provider_manager.py:172:24
+ --> tests/unit_tests/core/test_provider_manager.py:196:24
ERROR Argument `Literal['llm']` is not assignable to parameter `model_type` with type `ModelType | SQLCoreOperations[ModelType]` in function `models.provider.LoadBalancingModelConfig.__init__` [bad-argument-type]
- --> tests/unit_tests/core/test_provider_manager.py:181:24
+ --> tests/unit_tests/core/test_provider_manager.py:205:24
ERROR Argument `list[ProviderModelSetting]` is not assignable to parameter `provider_model_settings` with type `list[_ProviderModelSettingCacheEntry] | None` in function `core.provider_manager.ProviderManager._to_model_settings` [bad-argument-type]
- --> tests/unit_tests/core/test_provider_manager.py:202:37
+ --> tests/unit_tests/core/test_provider_manager.py:226:37
ERROR Argument `list[LoadBalancingModelConfig]` is not assignable to parameter `load_balancing_model_configs` with type `list[_LoadBalancingModelConfigCacheEntry] | None` in function `core.provider_manager.ProviderManager._to_model_settings` [bad-argument-type]
- --> tests/unit_tests/core/test_provider_manager.py:203:42
+ --> tests/unit_tests/core/test_provider_manager.py:227:42
ERROR Argument `Literal['llm']` is not assignable to parameter `model_type` with type `ModelType | SQLCoreOperations[ModelType]` in function `models.provider.ProviderModelSetting.__init__` [bad-argument-type]
- --> tests/unit_tests/core/test_provider_manager.py:246:20
+ --> tests/unit_tests/core/test_provider_manager.py:270:20
ERROR Argument `Literal['llm']` is not assignable to parameter `model_type` with type `ModelType | SQLCoreOperations[ModelType]` in function `models.provider.LoadBalancingModelConfig.__init__` [bad-argument-type]
- --> tests/unit_tests/core/test_provider_manager.py:255:24
+ --> tests/unit_tests/core/test_provider_manager.py:279:24
ERROR Argument `list[ProviderModelSetting]` is not assignable to parameter `provider_model_settings` with type `list[_ProviderModelSettingCacheEntry] | None` in function `core.provider_manager.ProviderManager._to_model_settings` [bad-argument-type]
- --> tests/unit_tests/core/test_provider_manager.py:269:67
+ --> tests/unit_tests/core/test_provider_manager.py:293:67
ERROR Argument `list[LoadBalancingModelConfig]` is not assignable to parameter `load_balancing_model_configs` with type `list[_LoadBalancingModelConfigCacheEntry] | None` in function `core.provider_manager.ProviderManager._to_model_settings` [bad-argument-type]
- --> tests/unit_tests/core/test_provider_manager.py:269:77
+ --> tests/unit_tests/core/test_provider_manager.py:293:77
ERROR Argument `Literal['llm']` is not assignable to parameter `model_type` with type `ModelType | SQLCoreOperations[ModelType]` in function `models.provider.ProviderModelSetting.__init__` [bad-argument-type]
- --> tests/unit_tests/core/test_provider_manager.py:465:20
+ --> tests/unit_tests/core/test_provider_manager.py:489:20
ERROR Argument `Literal['llm']` is not assignable to parameter `model_type` with type `ModelType | SQLCoreOperations[ModelType]` in function `models.provider.LoadBalancingModelConfig.__init__` [bad-argument-type]
- --> tests/unit_tests/core/test_provider_manager.py:476:24
+ --> tests/unit_tests/core/test_provider_manager.py:500:24
ERROR Argument `list[ProviderModelSetting]` is not assignable to parameter `provider_model_settings` with type `list[_ProviderModelSettingCacheEntry] | None` in function `core.provider_manager.ProviderManager._to_model_settings` [bad-argument-type]
- --> tests/unit_tests/core/test_provider_manager.py:496:37
+ --> tests/unit_tests/core/test_provider_manager.py:520:37
ERROR Argument `list[LoadBalancingModelConfig]` is not assignable to parameter `load_balancing_model_configs` with type `list[_LoadBalancingModelConfigCacheEntry] | None` in function `core.provider_manager.ProviderManager._to_model_settings` [bad-argument-type]
- --> tests/unit_tests/core/test_provider_manager.py:497:42
+ --> tests/unit_tests/core/test_provider_manager.py:521:42
ERROR Argument `Literal['llm']` is not assignable to parameter `model_type` with type `ModelType | SQLCoreOperations[ModelType]` in function `models.provider.ProviderModelSetting.__init__` [bad-argument-type]
- --> tests/unit_tests/core/test_provider_manager.py:515:20
+ --> tests/unit_tests/core/test_provider_manager.py:539:20
ERROR Argument `Literal['llm']` is not assignable to parameter `model_type` with type `ModelType | SQLCoreOperations[ModelType]` in function `models.provider.LoadBalancingModelConfig.__init__` [bad-argument-type]
- --> tests/unit_tests/core/test_provider_manager.py:526:24
+ --> tests/unit_tests/core/test_provider_manager.py:550:24
ERROR Argument `Literal['llm']` is not assignable to parameter `model_type` with type `ModelType | SQLCoreOperations[ModelType]` in function `models.provider.LoadBalancingModelConfig.__init__` [bad-argument-type]
- --> tests/unit_tests/core/test_provider_manager.py:535:24
+ --> tests/unit_tests/core/test_provider_manager.py:559:24
ERROR Argument `list[ProviderModelSetting]` is not assignable to parameter `provider_model_settings` with type `list[_ProviderModelSettingCacheEntry] | None` in function `core.provider_manager.ProviderManager._to_model_settings` [bad-argument-type]
- --> tests/unit_tests/core/test_provider_manager.py:556:37
+ --> tests/unit_tests/core/test_provider_manager.py:580:37
ERROR Argument `list[LoadBalancingModelConfig]` is not assignable to parameter `load_balancing_model_configs` with type `list[_LoadBalancingModelConfigCacheEntry] | None` in function `core.provider_manager.ProviderManager._to_model_settings` [bad-argument-type]
- --> tests/unit_tests/core/test_provider_manager.py:557:42
+ --> tests/unit_tests/core/test_provider_manager.py:581:42
WARN Identity comparison `False is True` is always False [unnecessary-comparison]
- --> tests/unit_tests/core/test_provider_manager.py:1555:30
+ --> tests/unit_tests/core/test_provider_manager.py:1500:30
ERROR Object of type `ToolInvokeMessage.BlobChunkMessage | ToolInvokeMessage.BlobMessage | ToolInvokeMessage.FileMessage | ToolInvokeMessage.JsonMessage | ToolInvokeMessage.LogMessage | ToolInvokeMessage.RetrieverResourceMessage | ToolInvokeMessage.TextMessage | ToolInvokeMessage.VariableMessage | None` has no attribute `text` [missing-attribute]
--> tests/unit_tests/core/tools/builtin_tool/providers/time/tools/test_localtime_to_timestamp.py:56:16
ERROR Object of type `ToolInvokeMessage.BlobChunkMessage | ToolInvokeMessage.BlobMessage | ToolInvokeMessage.FileMessage | ToolInvokeMessage.JsonMessage | ToolInvokeMessage.LogMessage | ToolInvokeMessage.RetrieverResourceMessage | ToolInvokeMessage.TextMessage | ToolInvokeMessage.VariableMessage | None` has no attribute `text` [missing-attribute]
|
Summary
Part of #45584
ModelInvocationUtils.invoke(used by built-in tools and plugin backwards invocation) wrote itsToolModelInvokelog row through the globaldb.session:add+commitbeforeinvoke_llm, then a seconddb.session.commit()after the model returned. That second commit runs on the caller's scoped session, so it also committed anything else the caller had pending, and the log row stayed attached to that session for the whole model call.invokenow takes a required keyword-onlysession_maker: sessionmaker[Session], and the row and the response update each go through their ownsession_maker.begin()scope. No session is held duringinvoke_llm, and the module no longer importsextensions.ext_databaseor the global session factory. The two callers,BuiltinTool.invoke_modelandPluginModelBackwardsInvocation.invoke_system_model, passsession_factory.get_session_maker(). Behaviour is otherwise the same: the row is committed with an empty response before the call, and updated with the response and usage only on success.Contract (also in the
invokedocstring, perapi/AGENTS.md):session_makeris bound to the app database (callers passsession_factory.get_session_maker()).answer_tokens == 0, andInvokeModelErroris raised.Tests (
tests/unit_tests/core/tools/utils/test_model_invocation_utils.py):model_invocation_utils.dbfor the test session; the tests passsqlite_session_factoryassession_maker.invoke_llm, that a separate session already sees the committed row with an empty response, then checks the final persisted values.tests/unit_tests/core/tools/builtin_tool/test_tool.pyassertsinvoke_modelforwards the session maker.invoketests fail (The current Flask app is not registered with this 'SQLAlchemy' instance); with it they pass.Local results on this branch (based on main
e316d5b5a8):pytest api/tests/unit_tests/core/tools api/tests/unit_tests/core/plugin: 1255 passedmake lint: passed (102 import contracts kept)make type-check: passedscripts/check_no_new_session_mock.py --base-rev upstream/main: cleanScreenshots
N/A (no UI change)
Checklist
make lint && make type-check(backend) andvp staged(frontend) to appease the lint godsFrom Claude Code