wake-alarm/wake_alarm/_sick.py
Krzysztof kuhy Rudnicki 8696f2d0bf wake_alarm: sick days stand the PC down (fallback ring, RTC wake, trigger, workday penalty)
A date in the new shared sickdays pool (not a free day) now means, on the PC:
no fallback ring (a ring in progress stops, reason sick_day), no RTC
self-wake for that morning (ours is disarmed), no morning-routine trigger,
and no workday_penalty.json; a penalty issued on a day later marked sick is
retracted (penalty_cancelled, cause sick_day) by the next session refresh or
RTC tick. The audit treats the day as one where nothing is expected.

All pool access goes through wake_alarm/_sick.py: fail closed (unreadable
pool or missing package = not sick), and act modes pull the pool from
Firebase first (RTC --arm on a stick-day target, in-window fallback ticks
and ring polls, the day's first penalty write) because the sickdays timer
pauses through hibernate. Dry runs never pull.

Pins: sickdays-v0.1.0, freedays-v0.4.0, crdt-sync-v0.9.2 (needed by both);
pre-commit 4.7.0, pylint 4.1.3, ruff 0.17.1 (freshness gate).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Xfj1sxKH9VBPqpcAni8Jry
2026-10-16 08:45:29 +02:00

55 lines
1.9 KiB
Python

"""Sick days: the one place wake_alarm asks the shared ``sickdays`` pool.
A sick day (``~/src/utils/sickdays``) is not a free day -- a separate pool,
no cap, no reason -- but on the PC it stands down the same things: no
fallback ring, no RTC self-wake, no workday penalty, no morning routine.
Two rules this module exists to keep in one spot:
* **Fail closed.** ``sickdays.is_sick_day`` never raises and reads a corrupt
pool as "not sick". The import is guarded as well: these jobs run under
``/usr/bin/python``, and a package that failed to install must leave
enforcement *on* (not sick), never crash the per-minute tick -- a crashed
tick rings nothing.
* **Sync only where something is about to happen, never in a read-only
mode.** ``sync_quietly`` rewrites the local pool, so ``--dry-run`` and
``--show`` never call it. The sickdays timer alone is not enough: it is a
monotonic 15-minute timer, which pauses during hibernate and has no
``Persistent`` catch-up, so right after the RTC resume the pool can be a
whole interval stale -- exactly when a day marked sick from bed matters.
"""
from __future__ import annotations
import logging
from typing import TYPE_CHECKING
_logger = logging.getLogger(__name__)
try:
import sickdays
_AVAILABLE = True
except ImportError:
_AVAILABLE = False
_logger.warning("sickdays is not installed; no day counts as sick")
if TYPE_CHECKING:
from datetime import date
def is_sick(day: date) -> bool:
"""Whether ``day`` is a sick day; local file only, never raises."""
return _AVAILABLE and sickdays.is_sick_day(day)
def sync() -> bool:
"""Pull the pool from Firebase now; False when it could not (silently)."""
return _AVAILABLE and sickdays.sync_quietly()
def is_sick_synced(day: date) -> bool:
""":func:`is_sick` after a fresh :func:`sync` -- for act modes only."""
sync()
return is_sick(day)