Skip to content

refactor(api): drop global db.session from workflow run trace assembly - #45679

Merged
fatelei merged 3 commits into
langgenius:mainfrom
paulhuiseismic:refactor/45584-workflow-run-session
Nov 6, 2026
Merged

fatelei merged 3 commits into
langgenius:mainfrom
paulhuiseismic:refactor/45584-workflow-run-session

Conversation

@paulhuiseismic

@paulhuiseismic paulhuiseismic commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

Part of #45584. This PR covers services/workflow_run_service.py only. It does not overlap with #45587, which handles api_tools_manage_service.py. tasks/initialize_created_app_rbac_access_task.py will follow in a separate PR.

WorkflowRunService already receives a session_factory at construction, and get_workflow_run_pause_details already uses it. The two node-execution trace methods were the last places in the service that still passed the Flask-SQLAlchemy global db.session() to assemble_workflow_node_execution_traces.

  • get_workflow_run_node_executions and get_workflow_tool_node_executions now resolve creators inside with self._session_factory() as session:. The session closes before the method returns. For tool traces, the internal correlation keys are stripped from process_data after the session has closed.
  • The unused from extensions.ext_database import db import is removed.

Session lifetime: the session is only used for read-only session.get(Account | EndUser, ...) lookups and is never committed. database_client is configured with expire_on_commit=False. The node-execution response models only read the column-backed SimpleAccount (id, name, email) and SimpleEndUser fields. These remain readable on the detached instances after the session closes. A new test serializes the response after the session closes to lock this in.

Tests

  • test_trace_assembly_uses_a_session_from_the_injected_factory, parametrized over both methods: the session passed to the assembler comes from the injected factory, and the factory's context is exited.
  • test_traces_serialize_creator_after_the_assembly_session_closes: stores an Account in SQLite, assembles traces in a session that is then closed, and validates WorkflowRunNodeExecutionListResponse.
  • pytest api/tests/unit_tests/services/test_workflow_run_service.py api/tests/unit_tests/services/test_workflow_node_execution_trace_service.py api/tests/unit_tests/controllers/console/app/test_workflow_run.py: 37 passed.

Screenshots

N/A (no UI change)

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • 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) and vp staged (frontend) to appease the lint gods

🤖 Generated with Claude Code

From Claude Code

WorkflowRunService already receives a session factory at construction.
Use it for the creator lookups in get_workflow_run_node_executions and
get_workflow_tool_node_executions instead of the Flask-SQLAlchemy
global session, and drop the now-unused db import.

The session is closed before the traces are returned; the response
models only read column-backed Account/EndUser fields, which stay
readable on detached instances (expire_on_commit=False, no commit).

Part of langgenius#45584.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Nov 5, 2026
@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-11-05 17:10:12.661451197 +0000
+++ /tmp/pyrefly_pr.txt	2026-11-05 17:09:52.410303029 +0000
@@ -6116,6 +6116,10 @@
    --> tests/unit_tests/services/test_workflow_node_execution_trace_service.py:162:21
 ERROR Argument `dict[str, list[dict[str, object]]]` is not assignable to parameter `full_process_data` with type `dict[str, object] | None` in function `_repository` [bad-argument-type]
    --> tests/unit_tests/services/test_workflow_node_execution_trace_service.py:162:49
+ERROR Argument `dict[str, list[dict[str, object]]]` is not assignable to parameter `process_data` with type `dict[str, object]` in function `_execution` [bad-argument-type]
+   --> tests/unit_tests/services/test_workflow_node_execution_trace_service.py:182:25
+ERROR Argument `dict[str, list[dict[str, object]]]` is not assignable to parameter `full_process_data` with type `dict[str, object] | None` in function `_repository` [bad-argument-type]
+   --> tests/unit_tests/services/test_workflow_node_execution_trace_service.py:182:53
 ERROR Argument `_RepoRecorder | Unknown` is not assignable to parameter `workflow_execution_repository` with type `WorkflowExecutionRepository` in function `services.workflow_persistence.WorkflowPersistenceLayer.__init__` [bad-argument-type]
    --> tests/unit_tests/services/test_workflow_persistence.py:356:39
 ERROR Argument `_RepoRecorder | Unknown` is not assignable to parameter `workflow_node_execution_repository` with type `WorkflowNodeExecutionRepository` in function `services.workflow_persistence.WorkflowPersistenceLayer.__init__` [bad-argument-type]

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 83.55% 83.55% +0.00%
Strict coverage 83.21% 83.21% +0.00%
Typed symbols 87,084 87,090 +6
Untyped symbols 17,215 17,215 0
Modules 4415 4415 0

@codecov

codecov Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.97%. Comparing base (e994c3f) to head (6cc90fb).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #45679   +/-   ##
=======================================
  Coverage   89.97%   89.97%           
=======================================
  Files        6260     6260           
  Lines      391690   391691    +1     
  Branches    78479    78479           
=======================================
+ Hits       352435   352442    +7     
+ Misses      33164    33160    -4     
+ Partials     6091     6089    -2     
Flag Coverage Δ
api 89.76% <100.00%> (-0.01%) ⬇️

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.

paulhuiseismic and others added 2 commits November 6, 2026 00:50
Replace the spec-based session mocks and the MagicMock assembler with a
close-tracking Session subclass and a recording function, so the test
satisfies the no-spec-mock, no-new-session-mock and no-new-mock guards.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@paulhuiseismic

paulhuiseismic commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @asukaminato0721 @QuantumGhost, can you please help to review this PR then?

@fatelei
fatelei added this pull request to the merge queue Nov 6, 2026
Merged via the queue into langgenius:main with commit 5507d46 Nov 6, 2026
51 checks passed
fatelei pushed a commit to fatelei/dify that referenced this pull request Nov 9, 2026
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants