Skip to content

AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).

fix(delegation): show all-day calendar events on their own day (#470)

Rufus00290b4206aa
diff --git a/packages/core/openexecutive/delegation/calendar.py b/packages/core/openexecutive/delegation/calendar.py
index b5820b45..b2d5f5f0 100644
--- a/packages/core/openexecutive/delegation/calendar.py
+++ b/packages/core/openexecutive/delegation/calendar.py
@@ -109,6 +109,17 @@ def local_when(value: str, all_day: bool, tz: ZoneInfo) -> str:
return moment.astimezone(tz).strftime("%Y-%m-%d %H:%M")
+def last_day(start: str, end: str) -> str:
+ """An all-day event's last day: its ``end`` is the day after (both
+ Google and Graph), so a one-day event ends on its start."""
+ try:
+ first = date.fromisoformat(start[:10])
+ after = date.fromisoformat(end[:10])
+ except ValueError:
+ return start[:10]
+ return max(first, after - timedelta(days=1)).isoformat()
+
+
async def events_for(mailbox: Any, start: datetime, end: datetime, *, max_results: int = 100) -> list[Any]:
"""``mailbox.list_events``: their own calendar, through the client for
their mailbox (Gmail or Outlook)."""
diff --git a/packages/core/openexecutive/delegation/outlook.py b/packages/core/openexecutive/delegation/outlook.py
index 330c46fe..9f134f8d 100644
--- a/packages/core/openexecutive/delegation/outlook.py
+++ b/packages/core/openexecutive/delegation/outlook.py
@@ -50,7 +50,7 @@ import re
import tempfile
import time
from dataclasses import dataclass, field, replace
-from datetime import UTC, datetime, timedelta
+from datetime import UTC, datetime, timedelta, tzinfo
from email.message import Message
from email.utils import parseaddr
from pathlib import Path
@@ -267,26 +267,33 @@ def _who(entry: object) -> str:
return f"{name} <{address}>" if name and address else (name or address)
-def _graph_when(value: object, all_day: bool) -> str:
+def _graph_when(value: object, all_day: bool, tz: tzinfo = UTC) -> str:
"""Graph's ``{dateTime, timeZone}`` (UTC, asked for) as ISO 8601: the
- date alone for an all-day event."""
+ date alone for an all-day event, read in ``tz`` (the person's zone)."""
raw = str(value.get("dateTime") or "") if isinstance(value, dict) else ""
if not raw:
return ""
- if all_day:
- return raw[:10]
try:
- return datetime.fromisoformat(raw[:19]).replace(tzinfo=UTC).isoformat()
+ moment = datetime.fromisoformat(raw[:19])
except ValueError:
return ""
-
-
-def parse_event(raw: dict[str, Any]) -> CalendarEvent | None:
- """An Outlook event, or None for a cancelled one."""
+ if all_day:
+ # Midnight in the mailbox's own zone, shown in UTC: east of UTC that
+ # is the day before (Tokyo's 14th is 15:00 on the 13th, Auckland's
+ # 11:00). Read it in the person's zone, rounded to the nearest
+ # midnight there in case the mailbox's zone differs a little.
+ local = moment.replace(tzinfo=UTC).astimezone(tz)
+ return (local + timedelta(hours=12)).date().isoformat()
+ return moment.replace(tzinfo=UTC).isoformat()
+
+
+def parse_event(raw: dict[str, Any], tz: tzinfo = UTC) -> CalendarEvent | None:
+ """An Outlook event, or None for a cancelled one. ``tz`` is the zone an
+ all-day event's date is read in."""
if raw.get("isCancelled") is True:
return None
all_day = raw.get("isAllDay") is True
- start = _graph_when(raw.get("start"), all_day)
+ start = _graph_when(raw.get("start"), all_day, tz)
if not start:
return None
status_raw = raw.get("responseStatus")
@@ -302,7 +309,7 @@ def parse_event(raw: dict[str, Any]) -> CalendarEvent | None:
id=str(raw.get("id") or ""),
title=clean_header(str(raw.get("subject") or "")) or "(no title)",
start=start,
- end=_graph_when(raw.get("end"), all_day),
+ end=_graph_when(raw.get("end"), all_day, tz),
all_day=all_day,
location=clean_header(str(location.get("displayName") or "")),
organizer=_who(raw.get("organizer")),
@@ -657,7 +664,9 @@ class DelegateOutlook:
raise
items_raw = data.get("value")
items: list[Any] = items_raw if isinstance(items_raw, list) else []
- events = [parse_event(item) for item in items if isinstance(item, dict)]
+ # The window is in the person's zone (calendar.day_window).
+ zone = start.tzinfo or UTC
+ events = [parse_event(item, zone) for item in items if isinstance(item, dict)]
return [e for e in events if e is not None]
async def profile_email(self) -> str:
diff --git a/packages/core/openexecutive/orchestrator/mail_read_tools.py b/packages/core/openexecutive/orchestrator/mail_read_tools.py
index fee8b2bd..108f5b53 100644
--- a/packages/core/openexecutive/orchestrator/mail_read_tools.py
+++ b/packages/core/openexecutive/orchestrator/mail_read_tools.py
@@ -703,7 +703,7 @@ async def handle_my_calendar(tool_input: dict[str, Any]) -> str:
{
"title": one_line(e.title, 160),
"start": calendar.local_when(e.start, e.all_day, tz),
- "end": calendar.local_when(e.end, e.all_day, tz),
+ "end": calendar.last_day(e.start, e.end) if e.all_day else calendar.local_when(e.end, False, tz),
**({"all_day": True} if e.all_day else {}),
**({"location": one_line(e.location, 160)} if e.location else {}),
**({"organizer": one_line(e.organizer, 160)} if e.organizer else {}),
diff --git a/packages/core/tests/unit/test_delegation_outlook.py b/packages/core/tests/unit/test_delegation_outlook.py
index 19839c5b..b9e80ac8 100644
--- a/packages/core/tests/unit/test_delegation_outlook.py
+++ b/packages/core/tests/unit/test_delegation_outlook.py
@@ -16,6 +16,7 @@ from pathlib import Path
from types import ModuleType
from typing import Any
from urllib.parse import unquote
+from zoneinfo import ZoneInfo
import httpx
import pytest
@@ -798,3 +799,21 @@ def _refresh_form(cred: OutlookCredential) -> dict[str, str]:
ol._TOKENS.clear()
asyncio.run(refresh())
return forms[0]
+
+
+def test_an_outlook_all_day_event_keeps_its_day_east_and_west_of_utc() -> None:
+ def day(start: str, end: str, zone: str) -> tuple[str, str]:
+ event = ol.parse_event({"id": "e", "subject": "Offsite", "isAllDay": True,
+ "start": {"dateTime": start}, "end": {"dateTime": end}}, ZoneInfo(zone))
+ assert event is not None
+ return event.start, event.end
+
+ # The 14th's midnight, shown in UTC: Tokyo 15:00 and Auckland (+13) 11:00
+ # on the 13th, New York 04:00 on the 14th.
+ assert day("2026-10-13T15:00:00.0000000", "2026-10-14T15:00:00.0000000", "Asia/Tokyo") == ("2026-10-14", "2026-10-15")
+ assert day("2026-10-13T11:00:00.0000000", "2026-10-14T11:00:00.0000000", "Pacific/Auckland") == (
+ "2026-10-14", "2026-10-15",
+ )
+ assert day("2026-10-14T04:00:00.0000000", "2026-10-15T04:00:00.0000000", "America/New_York") == (
+ "2026-10-14", "2026-10-15",
+ )
diff --git a/packages/core/tests/unit/test_mail_read_tools.py b/packages/core/tests/unit/test_mail_read_tools.py
index e7bfbe41..3830fcd5 100644
--- a/packages/core/tests/unit/test_mail_read_tools.py
+++ b/packages/core/tests/unit/test_mail_read_tools.py
@@ -610,7 +610,8 @@ def test_my_calendar_lists_their_day_in_their_time_zone(calendar_on: None) -> No
pilot, offsite = result["events"]
assert pilot["start"] == "2026-10-14 09:00" and pilot["end"] == "2026-10-14 09:30"
assert "\n" not in pilot["title"] and pilot["their_answer"] == "accepted" and pilot["location"] == "Room 4"
- assert offsite == {"title": "Offsite", "start": "2026-10-14", "end": "2026-10-15", "all_day": True}
+ # The providers' all-day end is the day after; a one-day event ends the day it starts.
+ assert offsite == {"title": "Offsite", "start": "2026-10-14", "end": "2026-10-14", "all_day": True}
start, end, _limit = mailbox.windows[0]
assert start.isoformat() == "2026-10-14T00:00:00-07:00" and end - start == timedelta(days=2)
assert session.turn_delegation.touched_mail is True and session.turn_delegation.read_mail is True