feat(run,config): operating hours window + timezone-aware lifecycle state
Add OperatingHoursCfg (enabled/timezone/weekdays/start_hhmm/stop_hhmm) so
the run loop can align with NYSE session hours instead of the user's
local wall clock (fixes DST drift between NY and Europe/Bucharest).
- Config parses [options.operating_hours] and resolves ZoneInfo at load,
fail-fast on invalid tz or weekday names. The tz is cached on
_tz_cache so the detection loop pays zero per-tick cost.
- LifecycleState tracks user_paused + last_window_state across ticks.
- Module-scope _should_skip(now, state, cfg, canary) returns skip reason
or None. Weekday check uses datetime.weekday() + a fixed MON..SUN list
(locale-free; strftime('%a') is localized).
- _maybe_log_transition emits market_open / market_closed once per edge.
R2: when last_window_state is None (startup), just seed — do not send
a spurious market_open alert when run_live_async launches in-window.
- _run_tick consults the lifecycle guard before scheduling the heavy
detection thread, so drain + transition logging still happen when the
tick is skipped.
- CLI flags --tz / --weekdays / --oh-start / --oh-stop override TOML.
(Kept distinct from the existing --start-at/--stop-at sleep-until-time
semantics to avoid breaking current deployments — deviation noted.)
- configs/example.toml documents the new [options.operating_hours] table.
Tests: parametrized window matrix (tests #8), transition logging (#9),
notification side-effect (#10), R2 #20 startup suppression, R2 #22
locale-independent weekday, plus guards for user_paused / canary
precedence and config-parse error paths.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -97,3 +97,59 @@ def test_attach_screenshots_unknown_keys_ignored() -> None:
|
||||
}))
|
||||
assert cfg.attach_screenshots.arm is False
|
||||
# Should not raise even with unknown key
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Commit 3: AlertBehaviorCfg (fire_on_phase_skip)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def test_alerts_default_fire_on_phase_skip_true() -> None:
|
||||
cfg = Config._from_dict(_with_opts({}))
|
||||
assert cfg.alerts.fire_on_phase_skip is True
|
||||
|
||||
|
||||
def test_alerts_fire_on_phase_skip_can_be_disabled() -> None:
|
||||
cfg = Config._from_dict(_with_opts({"alerts": {"fire_on_phase_skip": False}}))
|
||||
assert cfg.alerts.fire_on_phase_skip is False
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Commit 4: OperatingHoursCfg parsing + tz cache
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def test_operating_hours_default_disabled() -> None:
|
||||
cfg = Config._from_dict(_with_opts({}))
|
||||
assert cfg.operating_hours.enabled is False
|
||||
assert cfg.operating_hours.timezone == "America/New_York"
|
||||
assert cfg.operating_hours._tz_cache is None
|
||||
|
||||
|
||||
def test_operating_hours_enabled_caches_tz() -> None:
|
||||
cfg = Config._from_dict(_with_opts({
|
||||
"operating_hours": {
|
||||
"enabled": True,
|
||||
"timezone": "America/New_York",
|
||||
"weekdays": ["MON", "TUE", "WED", "THU", "FRI"],
|
||||
"start_hhmm": "09:30",
|
||||
"stop_hhmm": "16:00",
|
||||
}
|
||||
}))
|
||||
assert cfg.operating_hours.enabled is True
|
||||
assert cfg.operating_hours._tz_cache is not None
|
||||
assert str(cfg.operating_hours._tz_cache) == "America/New_York"
|
||||
|
||||
|
||||
def test_operating_hours_invalid_tz_raises_valueerror() -> None:
|
||||
import pytest
|
||||
with pytest.raises(ValueError, match="operating_hours.timezone"):
|
||||
Config._from_dict(_with_opts({
|
||||
"operating_hours": {"enabled": True, "timezone": "Not/A_Zone"},
|
||||
}))
|
||||
|
||||
|
||||
def test_operating_hours_invalid_weekday_raises_valueerror() -> None:
|
||||
import pytest
|
||||
with pytest.raises(ValueError, match="weekdays"):
|
||||
Config._from_dict(_with_opts({
|
||||
"operating_hours": {"enabled": True, "weekdays": ["XYZ"]},
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user