fix: conversation var unexpected reset after HITL node (#32936)

This commit is contained in:
非法操作 2026-03-06 09:57:09 +08:00 committed by GitHub
parent ad81513b6a
commit 7ffa6c1849
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 2 deletions

View File

@ -65,9 +65,15 @@ class VariablePool(BaseModel):
# Add environment variables to the variable pool
for var in self.environment_variables:
self.add((ENVIRONMENT_VARIABLE_NODE_ID, var.name), var)
# Add conversation variables to the variable pool
# Add conversation variables to the variable pool. When restoring from a serialized
# snapshot, `variable_dictionary` already carries the latest runtime values.
# In that case, keep existing entries instead of overwriting them with the
# bootstrap list.
for var in self.conversation_variables:
self.add((CONVERSATION_VARIABLE_NODE_ID, var.name), var)
selector = (CONVERSATION_VARIABLE_NODE_ID, var.name)
if self._has(selector):
continue
self.add(selector, var)
# Add rag pipeline variables to the variable pool
if self.rag_pipeline_variables:
rag_pipeline_variables_map: defaultdict[Any, dict[Any, Any]] = defaultdict(dict)

View File

@ -4,8 +4,10 @@ from unittest.mock import MagicMock, patch
import pytest
from dify_graph.constants import CONVERSATION_VARIABLE_NODE_ID
from dify_graph.model_runtime.entities.llm_entities import LLMUsage
from dify_graph.runtime import GraphRuntimeState, ReadOnlyGraphRuntimeStateWrapper, VariablePool
from dify_graph.variables.variables import StringVariable
class StubCoordinator:
@ -278,3 +280,17 @@ class TestGraphRuntimeState:
assert restored_execution.started is True
assert new_stub.state == "configured"
def test_snapshot_restore_preserves_updated_conversation_variable(self):
variable_pool = VariablePool(
conversation_variables=[StringVariable(name="session_name", value="before")],
)
variable_pool.add((CONVERSATION_VARIABLE_NODE_ID, "session_name"), "after")
state = GraphRuntimeState(variable_pool=variable_pool, start_at=time())
snapshot = state.dumps()
restored = GraphRuntimeState.from_snapshot(snapshot)
restored_value = restored.variable_pool.get((CONVERSATION_VARIABLE_NODE_ID, "session_name"))
assert restored_value is not None
assert restored_value.value == "after"