fix(config): validate the timezone key so a bad IANA name is reported, not silently ignored

hermes_time reads `timezone` from config.yaml (or HERMES_TIMEZONE) and, when
ZoneInfo cannot load the name, logs one warning and falls back to
server-local time. Nothing else ever looks at the value: not doctor, not
validate_config_structure. So a typo ("Asia/Tokio", "EST5", "Tokyo") puts
the agent clock and every cron schedule on the server's zone, with the
only evidence a single line in the gateway log.

validate_config_structure now checks the key: a non-string is an error, a
non-empty string that ZoneInfo cannot load is an error, and blank/missing
stays silent (server-local is the documented default). Because the check
runs at startup too, it is skipped when the interpreter has no tz database
at all (bare Windows without tzdata) — there is nothing to judge against,
and flagging every valid name there would be worse than the bug. The hint
names the IANA form and points out that HERMES_TIMEZONE overrides the key.
This commit is contained in:
John Paul Soliva
2026-09-02 22:41:12 +09:00
committed by Teknium
parent 536a07bec2
commit 7af3515c57
2 changed files with 89 additions and 0 deletions
+34
View File
@@ -1138,6 +1138,39 @@ def _validate_voice(config: Dict[str, Any], issues: List[ConfigIssue]) -> None:
"Set voice.submit_mode to direct (submit immediately) or draft (edit before sending)")
def _validate_timezone(config: Dict[str, Any], issues: List[ConfigIssue]) -> None:
"""``timezone`` must be an IANA name the runtime can load.
``hermes_time._get_zoneinfo()`` swallows an invalid name behind a single WARNING in the
gateway log, then runs the agent clock AND every cron schedule on server-local time.
Surface it here, where doctor and the startup check both look. Silent when the
interpreter has no tz database at all (bare Windows without ``tzdata``) — nothing can be
judged there.
"""
if "timezone" not in config:
return
tz = config.get("timezone")
hint = ("Use an IANA zone name such as America/New_York or Asia/Tokyo (see "
"`timedatectl list-timezones`). With an invalid value the agent clock and cron "
"schedules silently fall back to server-local time. HERMES_TIMEZONE overrides "
"this key when set.")
if tz is not None and not isinstance(tz, str):
_issue(issues, "error", f"timezone must be an IANA zone name string, got {tz!r}", hint)
return
if not (isinstance(tz, str) and tz.strip()):
return
name = tz.strip()
try:
import zoneinfo
zoneinfo.ZoneInfo("UTC") # is a tz database available at all?
except Exception:
return
try:
zoneinfo.ZoneInfo(name)
except Exception:
_issue(issues, "error", f"timezone {name!r} is not a valid IANA zone name", hint)
def _validate_entry_list(
entries: list, label: str, issues: List[ConfigIssue], fields, *, non_dict: Tuple[str, str, str],
) -> None:
@@ -1220,6 +1253,7 @@ def validate_config_structure(config: Optional[Dict[str, Any]] = None) -> List["
issues: List[ConfigIssue] = []
_validate_voice(config, issues)
_validate_timezone(config, issues)
cp = config.get("custom_providers")
fb = config.get("fallback_model")
for value, validator in ((cp, _validate_custom_providers), (fb, _validate_fallback_model)):
@@ -1,6 +1,8 @@
"""Tests for config.yaml structure validation (validate_config_structure)."""
import pytest
from hermes_cli.config import (
DEFAULT_CONFIG,
_EXTRA_KNOWN_ROOT_KEYS,
@@ -111,6 +113,59 @@ class TestVoiceSubmitModeValidation:
)
def _tz_issues(config):
return [i for i in validate_config_structure(config) if "timezone" in i.message]
def _has_tz_database() -> bool:
try:
import zoneinfo
zoneinfo.ZoneInfo("UTC")
return True
except Exception:
return False
class TestTimezoneValidation:
"""An invalid ``timezone`` silently puts the agent clock and every cron
schedule on server-local time (hermes_time._get_zoneinfo falls back with
one log warning). validate_config_structure must report it."""
def test_valid_zone_passes(self):
assert _tz_issues({"timezone": "Asia/Tokyo", "model": {"provider": "nous"}}) == []
def test_missing_or_blank_is_not_reported(self):
assert _tz_issues({"model": {"provider": "nous"}}) == []
assert _tz_issues({"timezone": "", "model": {"provider": "nous"}}) == []
assert _tz_issues({"timezone": " ", "model": {"provider": "nous"}}) == []
assert _tz_issues({"timezone": None, "model": {"provider": "nous"}}) == []
@pytest.mark.skipif(not _has_tz_database(), reason="no tz database in this interpreter")
def test_invalid_zone_is_reported(self):
[issue] = _tz_issues({"timezone": "Asia/Tokio", "model": {"provider": "nous"}})
assert issue.severity == "error"
assert "Asia/Tokio" in issue.message
assert "IANA" in issue.hint
assert "HERMES_TIMEZONE" in issue.hint
def test_non_string_is_reported(self):
[issue] = _tz_issues({"timezone": 9, "model": {"provider": "nous"}})
assert issue.severity == "error"
assert "string" in issue.message
def test_silent_without_a_tz_database(self, monkeypatch):
# Bare Windows without tzdata: ZoneInfo cannot load anything, including
# UTC. A valid name must not be flagged just because it cannot be checked.
import zoneinfo
def no_db(_key):
raise zoneinfo.ZoneInfoNotFoundError("no tz database")
monkeypatch.setattr(zoneinfo, "ZoneInfo", no_db)
assert _tz_issues({"timezone": "Asia/Tokyo", "model": {"provider": "nous"}}) == []
assert _tz_issues({"timezone": "Asia/Tokio", "model": {"provider": "nous"}}) == []
class TestUnknownTopLevelKeys:
"""Arbitrary top-level keys must NOT warn — they are bridged to os.environ.