wake-alarm/wake_alarm/_fallback_observe.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

121 lines
3.9 KiB
Python

"""One look at the world for the fallback alarm, and its bookkeeping.
Split from ``_fallback`` (tick, ring lifecycle, CLI): this is "what is true
right now" -- today's alarm, the latch, the free-day and sick-day pools
and, only when the decision needs it, every device's ``alarm.json``.
"""
from __future__ import annotations
from dataclasses import dataclass, replace
from typing import TYPE_CHECKING
import freedays
from wake_alarm._enforcement_payload import EnforcementView, enforcement_view
from wake_alarm._escapes import note_escapes
from wake_alarm._events import emit_on_change, last_value, remember
from wake_alarm._fallback_latch import Latch, read_latch
from wake_alarm._fallback_policy import (
FINAL_REASONS,
Facts,
alarm_instant,
needs_facts,
)
from wake_alarm._session_payload import newest_session
from wake_alarm._sick import is_sick, is_sick_synced
from wake_alarm._sync import get_alarm
from wake_alarm._sync_transport import fetch_firebase_logs
if TYPE_CHECKING:
from datetime import datetime
SETTLED_SLOT = "fallback_settled"
@dataclass(frozen=True)
class Observation:
"""One look at the world: the facts, and whether Firebase answered."""
facts: Facts
reachable: bool = True
view: EnforcementView | None = None
def settled_reason(day: str, latch: Latch | None) -> str | None:
"""A final reason already seen today, from the latch or the event state."""
if latch is not None and latch.stop_reason in FINAL_REASONS:
return latch.stop_reason
remembered = (last_value(SETTLED_SLOT) or "").partition("|")
return remembered[2] if remembered[0] == day and remembered[2] else None
def _sick_today(now: datetime, *, online: bool, sync: bool) -> bool:
"""Whether today is a sick day; pulls the pool first when acting online.
Synced only on the ticks that already go to the network (the window, or
a ring in progress), so a day marked sick on the phone mid-ring stops
the ring at the next poll, and an off-window tick stays local.
"""
if online and sync:
return is_sick_synced(now.date())
return is_sick(now.date())
def observe(now: datetime, *, sync: bool) -> Observation:
"""Gather the facts; the network only when the decision needs it.
``sync`` is False for ``--dry-run``: pulling the sick-day pool rewrites
its local file, and a dry run writes nothing.
"""
day = now.date().isoformat()
latch = read_latch(day)
alarm_at = alarm_instant(get_alarm(), now.date())
online = needs_facts(now, alarm_at, latch)
facts = Facts(
now=now,
alarm_at=alarm_at,
free_day=freedays.is_free_day(now.date()),
sick_day=_sick_today(now, online=online, sync=sync),
latch=latch,
settled=settled_reason(day, latch),
)
if facts.free_day or facts.sick_day or not online:
return Observation(facts)
published = fetch_firebase_logs()
if published is None:
return Observation(facts, reachable=False)
view = enforcement_view(published)
session = newest_session(published)
return Observation(
replace(
facts,
escaped=view.escape_for(day) is not None,
session_started=session is not None and session.date == day,
ring_started=view.ring_started_on(day),
),
view=view,
)
def record(seen: Observation) -> None:
"""Log what this observation means for the PC's own state."""
day = seen.facts.now.date().isoformat()
if seen.reachable:
remember("firebase_unreachable", None)
else:
emit_on_change(
"firebase_unreachable",
day,
"treating as no ping, no session",
alarm=seen.facts.alarm_at.isoformat(),
)
if seen.view is not None:
note_escapes(seen.view)
def remember_final(day: str, reason: str) -> None:
"""Make a final reason stick for the rest of the day."""
if reason in FINAL_REASONS:
remember(SETTLED_SLOT, f"{day}|{reason}")