Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend/copilot): plumb user_timezone in run_block ExecutionContext (#13247)

Zamil Majdy092f71a9396d
diff --git a/autogpt_platform/backend/backend/copilot/tools/conftest.py b/autogpt_platform/backend/backend/copilot/tools/conftest.py
index 84d319f8a3..88bccd64a0 100644
--- a/autogpt_platform/backend/backend/copilot/tools/conftest.py
+++ b/autogpt_platform/backend/backend/copilot/tools/conftest.py
@@ -5,6 +5,9 @@ backend/conftest.py so that integration tests in this directory do not trigger
the full SpinTestServer startup (which requires Postgres + RabbitMQ).
"""
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
import pytest_asyncio
@@ -18,3 +21,25 @@ async def server(): # type: ignore[override]
async def graph_cleanup(): # type: ignore[override]
"""No-op graph cleanup stub."""
yield
+
+
[email protected](autouse=True)
+def stub_user_lookup_in_helpers(monkeypatch):
+ """Stub ``user_db.get_user_by_id`` ONLY for the helpers.py-local binding.
+
+ ``prepare_block_for_execution`` reads the user record to plumb
+ ``user_timezone`` into ``ExecutionContext``. The existing tests don't
+ need a real DB for that.
+
+ ⚠️ We must patch the ``user_db`` name on the helpers module itself,
+ NOT ``helpers.user_db.get_user_by_id`` — the latter resolves through
+ the module reference and mutates ``backend.data.user.get_user_by_id``
+ globally, which leaks into unrelated callers (e.g. ``rate_limit``'s
+ ``user_db().get_user_by_id`` in ``run_agent_test``) and clobbers
+ their real-DB test users with our MagicMock.
+ """
+ user = MagicMock()
+ user.timezone = "UTC"
+ stub = MagicMock()
+ stub.get_user_by_id = AsyncMock(return_value=user)
+ monkeypatch.setattr("backend.copilot.tools.helpers.user_db", stub)
diff --git a/autogpt_platform/backend/backend/copilot/tools/helpers.py b/autogpt_platform/backend/backend/copilot/tools/helpers.py
index 6d18578333..31140a72e0 100644
--- a/autogpt_platform/backend/backend/copilot/tools/helpers.py
+++ b/autogpt_platform/backend/backend/copilot/tools/helpers.py
@@ -20,6 +20,7 @@ from backend.copilot.constants import (
)
from backend.copilot.model import ChatSession
from backend.copilot.sdk.file_ref import FileRefExpansionError, expand_file_refs_in_args
+from backend.data import user as user_db
from backend.data.credit import UsageTransactionMetadata
from backend.data.db_accessors import credit_db, review_db, workspace_db
from backend.data.execution import ExecutionContext
@@ -32,6 +33,7 @@ from backend.executor.simulator import simulate_block
from backend.executor.utils import block_usage_cost
from backend.integrations.creds_manager import IntegrationCredentialsManager
from backend.util.exceptions import BlockError, InsufficientBalanceError
+from backend.util.timezone_utils import get_user_timezone_or_utc
from backend.util.type import coerce_inputs_to_schema
from .models import (
@@ -227,6 +229,14 @@ async def execute_block(
try:
workspace = await workspace_db().get_or_create_workspace(user_id)
+ # get_user_by_id raises ValueError on missing user; fall back to UTC
+ # so an orphaned-session block call still runs instead of crashing.
+ try:
+ user_timezone = get_user_timezone_or_utc(
+ (await user_db.get_user_by_id(user_id)).timezone
+ )
+ except ValueError:
+ user_timezone = "UTC"
synthetic_graph_id = f"{COPILOT_SESSION_PREFIX}{session_id}"
synthetic_node_id = f"{COPILOT_NODE_PREFIX}{block_id}"
@@ -241,6 +251,7 @@ async def execute_block(
workspace_id=workspace.id,
session_id=session_id,
sensitive_action_safe_mode=sensitive_action_safe_mode,
+ user_timezone=user_timezone,
)
exec_kwargs: dict[str, Any] = {