Skip to content

Personal AI, On Personal Devices

Python

fix(tools): preserve calendar release time precision

Elliot Slusky4cff4c716cb0
diff --git a/docs/user-guide/tools.md b/docs/user-guide/tools.md
index 391511ee..df3e19a6 100644
--- a/docs/user-guide/tools.md
+++ b/docs/user-guide/tools.md
@@ -451,7 +451,9 @@ enabled = "fx_macro_data"
| `limit` | integer | No | Rows to return, 1 to 100 (default: 20) |
For calendar rows, `reference_period` is the period the figure describes, and
-`release_time_utc` is when it is published.
+`release_time_utc` is the scheduled release time. If the publisher has announced
+only a date (`time_announced=false`) or the time is assumed, `release_time_utc`
+is null; `announcement_date` preserves the publisher's local release date.
### CodeInterpreter
diff --git a/src/openjarvis/tools/fx_macro.py b/src/openjarvis/tools/fx_macro.py
index 3ae2423d..3c0000ea 100644
--- a/src/openjarvis/tools/fx_macro.py
+++ b/src/openjarvis/tools/fx_macro.py
@@ -11,7 +11,7 @@ from __future__ import annotations
import json
import re
-from datetime import date
+from datetime import date, datetime, timezone
from typing import Any
import httpx
@@ -187,6 +187,23 @@ def _indicator_history(params: dict[str, Any], api_key: str | None) -> dict:
}
+def _calendar_release_time(row: dict[str, Any]) -> str | None:
+ """Keep date markers distinct from scheduled instants in calendar rows."""
+ if row.get("time_announced") is False or row.get("release_time_assumed") is True:
+ return None
+ if row.get("announcement_datetime_utc"):
+ return row["announcement_datetime_utc"]
+ # The epoch is required by CalendarReleaseRow; the formatted UTC field is
+ # optional. Older responses may also omit the additive precision fields.
+ timestamp = row.get("announcement_datetime")
+ if isinstance(timestamp, int) and not isinstance(timestamp, bool):
+ try:
+ return datetime.fromtimestamp(timestamp, timezone.utc).isoformat()
+ except (OverflowError, OSError, ValueError):
+ return None
+ return None
+
+
def _release_calendar(params: dict[str, Any], api_key: str | None) -> dict:
currency = _currency(params.get("currency"), "currency")
query = {}
@@ -201,7 +218,10 @@ def _release_calendar(params: dict[str, Any], api_key: str | None) -> dict:
{
"release": row.get("release"),
"name": row.get("name"),
- "release_time_utc": row.get("announcement_datetime_utc"),
+ "release_time_utc": _calendar_release_time(row),
+ "time_announced": row.get("time_announced"),
+ "announcement_date": row.get("announcement_date"),
+ "release_time_assumed": row.get("release_time_assumed"),
"reference_period": row.get("date"),
"importance": row.get("event_importance"),
}
diff --git a/tests/tools/test_fx_macro.py b/tests/tools/test_fx_macro.py
index 6039d6ba..915ff3da 100644
--- a/tests/tools/test_fx_macro.py
+++ b/tests/tools/test_fx_macro.py
@@ -286,6 +286,47 @@ def test_calendar_filters_and_truncates_to_limit():
assert events[0]["reference_period"] == "2026-09-30"
[email protected]("precision", [{}, {"time_announced": True}])
+def test_calendar_uses_required_epoch_when_optional_utc_string_is_absent(precision):
+ # CalendarReleaseRow requires the epoch and slug, while formatted UTC and
+ # the additive precision fields are optional in the published contract.
+ row = {
+ "release": "inflation",
+ "announcement_datetime": 1789129800,
+ "announcement_datetime_local": "2026-09-11T08:30:00-04:00",
+ **precision,
+ }
+ with patch(_GET, return_value=_response(body={"data": [row]})):
+ result = _run(action="release_calendar", currency="USD")
+ assert result.success
+ event = json.loads(result.content)["events"][0]
+ assert event["release_time_utc"] == "2026-09-11T12:30:00+00:00"
+ assert event["time_announced"] is precision.get("time_announced")
+
+
+ "precision", [{"time_announced": False}, {"release_time_assumed": True}]
+)
+def test_calendar_never_reports_a_date_marker_as_an_exact_release_time(precision):
+ row = {
+ "release": "gdp",
+ "announcement_datetime": 1791777600,
+ "announcement_datetime_utc": "2026-10-12T04:00:00Z",
+ "announcement_date": "2026-10-12",
+ "date": "2026-09-30",
+ **precision,
+ }
+ with patch(_GET, return_value=_response(body={"data": [row]})):
+ result = _run(action="release_calendar", currency="USD")
+ assert result.success
+ event = json.loads(result.content)["events"][0]
+ assert event["release_time_utc"] is None
+ assert event["announcement_date"] == "2026-10-12"
+ assert event["reference_period"] == "2026-09-30"
+ for field, value in precision.items():
+ assert event[field] is value
+
+
def test_catalogue_is_summarised_and_empty_is_an_error():
body = {
"inflation": {