Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(api): read the START node sys.timestamp from the UTC clock (#44847)

Eapp1ebd0367cdfec8
diff --git a/api/services/workflow_service.py b/api/services/workflow_service.py
index 55571cd7a88..65225738a75 100644
--- a/api/services/workflow_service.py
+++ b/api/services/workflow_service.py
@@ -85,7 +85,7 @@ from graphon.variable_loader import load_into_variable_pool
from graphon.variables import VariableBase
from graphon.variables.input_entities import VariableEntityType
from graphon.variables.variables import Variable
-from libs.datetime_utils import naive_utc_now
+from libs.datetime_utils import naive_utc_now, to_utc_timestamp
from models import Account
from models.agent import WorkflowAgentBindingType, WorkflowAgentNodeBinding
from models.human_input import HumanInputFormRecipient, RecipientType
@@ -2079,7 +2079,7 @@ def _setup_variable_pool(
system_variable_values: dict[str, Any] = {
"user_id": user_id,
"app_id": workflow.app_id,
- "timestamp": int(naive_utc_now().timestamp()),
+ "timestamp": to_utc_timestamp(naive_utc_now()),
"workflow_id": workflow.id,
"files": files or [],
"workflow_execution_id": str(uuid.uuid4()),
diff --git a/api/tests/unit_tests/services/test_workflow_service.py b/api/tests/unit_tests/services/test_workflow_service.py
index 048880dfd71..dcd7747699a 100644
--- a/api/tests/unit_tests/services/test_workflow_service.py
+++ b/api/tests/unit_tests/services/test_workflow_service.py
@@ -1,7 +1,9 @@
import json
+import os
+import time
import uuid
from collections.abc import Callable, Generator, Iterator
-from datetime import datetime, timedelta
+from datetime import UTC, datetime, timedelta
from types import SimpleNamespace
from typing import Any, cast
from unittest.mock import ANY, MagicMock, patch, sentinel
@@ -3014,6 +3016,49 @@ class TestWorkflowServiceCases:
inputs={"k": "v"},
)
+ def test_setup_variable_pool_should_read_the_timestamp_from_the_epoch(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ """sys.timestamp is a Unix timestamp, so it must not follow the host timezone."""
+ if not hasattr(time, "tzset"):
+ pytest.skip("tzset is only available on POSIX platforms")
+
+ recorded_at = datetime(2024, 5, 1, tzinfo=UTC)
+ previous_tz = os.environ.get("TZ")
+ monkeypatch.setenv("TZ", "Asia/Shanghai")
+ time.tzset()
+ try:
+ workflow = self._make_workflow()
+ monkeypatch.setattr("services.workflow_service.naive_utc_now", lambda: recorded_at.replace(tzinfo=None))
+
+ with (
+ patch("services.workflow_service.VariablePool"),
+ patch("services.workflow_service.build_system_variables") as mock_build_system_variables,
+ patch("services.workflow_service.build_bootstrap_variables"),
+ patch("services.workflow_service.add_variables_to_pool"),
+ patch("services.workflow_service.add_node_inputs_to_pool"),
+ ):
+ _setup_variable_pool(
+ query="hello",
+ files=[],
+ user_id="u-1",
+ user_inputs={},
+ workflow=workflow,
+ node_id="start-node",
+ node_type=BuiltinNodeTypes.START,
+ conversation_id="conv-1",
+ conversation_variables=[],
+ )
+ finally:
+ if previous_tz is None:
+ monkeypatch.delenv("TZ", raising=False)
+ else:
+ monkeypatch.setenv("TZ", previous_tz)
+ time.tzset()
+
+ values = mock_build_system_variables.call_args.args[0]
+ assert values["timestamp"] == int(recorded_at.timestamp())
+
def test_setup_variable_pool_should_use_default_system_variables_for_non_start_node(
self,
) -> None: