Skip to content

fix: preserve context during child graph init#38140

Open
mturac wants to merge 1 commit into
langgenius:mainfrom
mturac:izon/fix-34511-child-graph-context
Open

fix: preserve context during child graph init#38140
mturac wants to merge 1 commit into
langgenius:mainfrom
mturac:izon/fix-34511-child-graph-context

Conversation

@mturac

@mturac mturac commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #34511.

Child workflow engines inherit the parent GraphRuntimeState.execution_context, but the child Graph.init(...) call creates node instances before the engine starts running. LLM node construction can load model/provider configuration during that init step, which requires the captured Flask app context.

This wraps child graph initialization in the inherited execution context so node factories and subsequent child engine execution use the same context boundary.

Screenshots

Not applicable; backend workflow runtime change.

Validation

  • uv run --project api --dev pytest api/tests/unit_tests/core/workflow/test_workflow_entry.py::TestWorkflowEntry::test_child_engine_enters_execution_context_while_initializing_graph -q
  • uv run --project api --dev pytest api/tests/unit_tests/core/workflow/test_workflow_entry.py -q
  • uv run --project api --dev pytest api/tests/unit_tests/core/workflow/nodes/iteration/test_iteration_child_engine_errors.py -q
  • uv run --project api --dev pytest api/tests/unit_tests/core/workflow/test_workflow_entry_helpers.py::TestWorkflowChildEngineBuilder::test_build_child_engine_constructs_graph_engine_with_quota_layer_only -q
  • uv run --project api --dev pytest api/tests/unit_tests/core/workflow/test_workflow_entry.py api/tests/unit_tests/core/workflow/test_workflow_entry_helpers.py api/tests/unit_tests/core/workflow/nodes/iteration/test_iteration_child_engine_errors.py -q
  • uv run --project api --dev ruff check api/core/workflow/workflow_entry.py api/tests/unit_tests/core/workflow/test_workflow_entry.py api/tests/unit_tests/core/workflow/test_workflow_entry_helpers.py
  • uv run --project api --dev ruff format --check api/core/workflow/workflow_entry.py api/tests/unit_tests/core/workflow/test_workflow_entry.py api/tests/unit_tests/core/workflow/test_workflow_entry_helpers.py
  • ./dev/pyrefly-check-local api/core/workflow/workflow_entry.py
  • git diff --check

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 added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran make lint && make type-check (backend) and cd web && pnpm exec vp staged (frontend) to appease the lint gods

@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jun 29, 2026
@github-actions

github-actions Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-06-29 10:15:24.675149800 +0000
+++ /tmp/pyrefly_pr.txt	2026-06-29 10:15:13.665100222 +0000
@@ -6226,7 +6226,7 @@
 ERROR Object of class `object` has no attribute `tool` [missing-attribute]
    --> tests/unit_tests/core/workflow/test_node_runtime.py:654:12
 ERROR Argument `TestWorkflowEntry.test_single_step_run_injects_code_limits.StubWorkflow` is not assignable to parameter `workflow` with type `Workflow` in function `core.workflow.workflow_entry.WorkflowEntry.single_step_run` [bad-argument-type]
-   --> tests/unit_tests/core/workflow/test_workflow_entry.py:145:22
+   --> tests/unit_tests/core/workflow/test_workflow_entry.py:241:22
 ERROR Object of class `_FakeModelNodeMixin` has no attribute `node_data` [missing-attribute]
   --> tests/unit_tests/core/workflow/test_workflow_entry_helpers.py:73:35
 ERROR Object of class `_FakeModelNodeMixin` has no attribute `node_data` [missing-attribute]

@github-actions

github-actions Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 51.49% 51.49% -0.00%
Strict coverage 51.01% 51.01% -0.00%
Typed symbols 30,846 30,847 +1
Untyped symbols 29,332 29,333 +1
Modules 2931 2931 0

@mturac mturac force-pushed the izon/fix-34511-child-graph-context branch 2 times, most recently from bfd4d70 to 92cf6d1 Compare June 29, 2026 04:07
@mturac mturac force-pushed the izon/fix-34511-child-graph-context branch from 92cf6d1 to 3cec973 Compare June 29, 2026 10:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Working outside of application context" error when executing workflow nodes in Parallel Mode

1 participant