From f4c82d0010ed6430bef8319663db4f0c616a34bd Mon Sep 17 00:00:00 2001 From: QuantumGhost Date: Thu, 6 Nov 2025 18:30:35 +0800 Subject: [PATCH] fix(api): fix `VariablePool.get` adding unexpected keys to variable_dictionary (#26767) Co-authored-by: -LAN- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- api/core/workflow/runtime/variable_pool.py | 6 ++++- .../workflow/entities/test_variable_pool.py | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/api/core/workflow/runtime/variable_pool.py b/api/core/workflow/runtime/variable_pool.py index d41a20dfd7..7fbaec9e70 100644 --- a/api/core/workflow/runtime/variable_pool.py +++ b/api/core/workflow/runtime/variable_pool.py @@ -153,7 +153,11 @@ class VariablePool(BaseModel): return None node_id, name = self._selector_to_keys(selector) - segment: Segment | None = self.variable_dictionary[node_id].get(name) + node_map = self.variable_dictionary.get(node_id) + if node_map is None: + return None + + segment: Segment | None = node_map.get(name) if segment is None: return None diff --git a/api/tests/unit_tests/core/workflow/entities/test_variable_pool.py b/api/tests/unit_tests/core/workflow/entities/test_variable_pool.py index f9de456b19..18f6753b05 100644 --- a/api/tests/unit_tests/core/workflow/entities/test_variable_pool.py +++ b/api/tests/unit_tests/core/workflow/entities/test_variable_pool.py @@ -111,3 +111,26 @@ class TestVariablePoolGetAndNestedAttribute: assert segment_false is not None assert isinstance(segment_false, BooleanSegment) assert segment_false.value is False + + +class TestVariablePoolGetNotModifyVariableDictionary: + _NODE_ID = "start" + _VAR_NAME = "name" + + def test_convert_to_template_should_not_introduce_extra_keys(self): + pool = VariablePool.empty() + pool.add([self._NODE_ID, self._VAR_NAME], 0) + pool.convert_template("The start.name is {{#start.name#}}") + assert "The start" not in pool.variable_dictionary + + def test_get_should_not_modify_variable_dictionary(self): + pool = VariablePool.empty() + pool.get([self._NODE_ID, self._VAR_NAME]) + assert len(pool.variable_dictionary) == 1 # only contains `sys` node id + assert "start" not in pool.variable_dictionary + + pool = VariablePool.empty() + pool.add([self._NODE_ID, self._VAR_NAME], "Joe") + pool.get([self._NODE_ID, "count"]) + start_subdict = pool.variable_dictionary[self._NODE_ID] + assert "count" not in start_subdict