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): dream crons never register — timezone lookup crashes in Prisma-less processes (#13336)
Nicholas Tindle4d7928908e41
diff --git a/autogpt_platform/backend/backend/copilot/dream/scheduling.py b/autogpt_platform/backend/backend/copilot/dream/scheduling.pyindex 146a06abc5..58d6d750d6 100644--- a/autogpt_platform/backend/backend/copilot/dream/scheduling.py+++ b/autogpt_platform/backend/backend/copilot/dream/scheduling.py@@ -171,15 +171,24 @@ async def _resolve_user_timezone(user_id: str) -> str | None:Single DB call — cached at the helper level for the duration ofone ``ensure_dream_system_scheduled`` invocation so registeringmultiple crons doesn't take multiple round-trips.++ Routes through the ``user_db()`` accessor, NOT ``User.prisma()``:+ this runs in the copilot-executor and scheduler processes, which+ never connect a local Prisma client. A direct Prisma call raises+ ``ClientNotConnectedError`` on every invocation there — a+ *permanent* failure the keep-existing-schedules fallback was never+ designed for, which silently prevented dream crons from ever being+ registered. The accessor falls back to the DatabaseManager RPC in+ Prisma-less processes (same pattern as ``dream/apply.py``)."""try:- from prisma import Prisma # noqa: F401 — ensures registry- from prisma.models import User-+ from backend.data.db_accessors import user_dbfrom backend.data.model import USER_TIMEZONE_NOT_SET- user = await User.prisma().find_unique(where={"id": user_id})- if user is None:+ try:+ user = await user_db().get_user_by_id(user_id)+ except ValueError:+ # Authoritative: the user row doesn't exist.return "UTC"tz = (user.timezone or "").strip()if not tz or tz == USER_TIMEZONE_NOT_SET:diff --git a/autogpt_platform/backend/backend/copilot/dream/scheduling_test.py b/autogpt_platform/backend/backend/copilot/dream/scheduling_test.pyindex 009fd3c3ab..d2a2fba603 100644--- a/autogpt_platform/backend/backend/copilot/dream/scheduling_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/scheduling_test.py@@ -397,11 +397,40 @@ async def test_force_refresh_with_failed_tz_lookup_still_skips_re_registration()client.add_nightly_batch_schedule.assert_not_called()+async def test_resolve_user_timezone_works_without_local_prisma_connection():+ """Dev outage AUTOGPT: the resolver ran in the copilot-executor+ process, which never connects a local Prisma client — a direct+ ``User.prisma()`` call raised ``ClientNotConnectedError`` on every+ invocation, so dream crons were never registered for anyone. The+ lookup must route through the ``user_db()`` accessor, which falls+ back to the DatabaseManager RPC in Prisma-less processes."""+ accessor = MagicMock()+ accessor.get_user_by_id = AsyncMock(+ return_value=MagicMock(timezone="Europe/Madrid")+ )+ with patch("backend.data.db_accessors.user_db", return_value=accessor):+ assert await scheduling._resolve_user_timezone("abc") == "Europe/Madrid"+ accessor.get_user_by_id.assert_awaited_once_with("abc")+++async def test_resolve_user_timezone_missing_user_is_authoritative_utc():+ """get_user_by_id raises ValueError for a missing row — that's an+ authoritative answer (UTC), not a lookup failure (None)."""+ accessor = MagicMock()+ accessor.get_user_by_id = AsyncMock(side_effect=ValueError("User not found"))+ with patch("backend.data.db_accessors.user_db", return_value=accessor):+ assert await scheduling._resolve_user_timezone("abc") == "UTC"++@pytest.mark.asyncioasync def test_resolve_user_timezone_returns_none_when_db_lookup_fails():"""The resolver distinguishes 'lookup failed' (None) from'genuinely unset' (UTC)."""- with patch("prisma.models.User.prisma", side_effect=RuntimeError("db down")):+ accessor = MagicMock()+ accessor.get_user_by_id = AsyncMock(side_effect=ConnectionError("db down"))+ with patch("backend.data.db_accessors.user_db", return_value=accessor):assert await scheduling._resolve_user_timezone("abc") is None@@ -411,11 +440,11 @@ async def test_resolve_user_timezone_unset_value_falls_back_to_utc():UTC — only lookup FAILURES return None."""from backend.data.model import USER_TIMEZONE_NOT_SET- prisma_stub = MagicMock()- prisma_stub.find_unique = AsyncMock(+ accessor = MagicMock()+ accessor.get_user_by_id = AsyncMock(return_value=MagicMock(timezone=USER_TIMEZONE_NOT_SET))- with patch("prisma.models.User.prisma", return_value=prisma_stub):+ with patch("backend.data.db_accessors.user_db", return_value=accessor):assert await scheduling._resolve_user_timezone("abc") == "UTC"