screen-locker/screen_locker/_workday_penalty.py
Krzysztof kuhy Rudnicki 7d8daca111 sick days: one shared sickdays pool, 20:00 shutdown floor, one-tap lock button
A sick day is now a date in the shared sickdays pool (utils sickdays 0.1.0)
and nothing else. Removed: the 7/30/90-day budget, the escalating lockout
countdown, the justification form, the commitment prompt and its double
debt, the workout-debt counter, the -60 min shutdown cut and its
sick_day_state.json, the post-sick-day --verify-workout mode, the sick
auto-upgrade, scripts/revoke_sick_day.py and their tests.

- Lock screen: "I'm sick" on the retry, no-phone and both Tue-Thu screens;
  one tap -> sickdays.add_sick_day(today) -> sick floor -> best-effort
  background sickdays.sync_quietly -> unlock. Refusals (free day,
  unreadable pool) are shown and the lock stays.
- Shutdown: the reset resolves with earned_time.resolve(..., sick=True)
  (max(20:00, ladder), capped 23:00) and saves the sick lift with the grace
  lift, so later credits absorb it instead of stacking on 20:00. A day
  marked after the reset is lifted by the new _sick_floor live pass (every
  5-min tick, --sync-only, --apply-bonuses, and the tap itself).
- Workout lock and grace floor stand down on a sick day; workouts that day
  credit normally through the sync paths.
- wake-alarm workday_penalty.json is void when its issued_date or
  penalty_date is a sick day.
- Status window, i3 line, CLI --status, MCP flags and web reasons read
  sickdays.
- scripts/migrate_sick_history.py imports sick_history.json's dates via
  sickdays.import_legacy, verifies and removes the file; idempotent, run by
  install.sh.
- Pins: earned-time 0.11.0, freedays 0.4.0, sickdays 0.1.0 (tags pending);
  pre-commit 4.7.0, 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 13:30:39 +02:00

73 lines
2.7 KiB
Python

"""Read the workday-stick penalty wake-alarm signed for today.
A missed Tuesday/Wednesday/Thursday wake-alarm ring costs tomorrow's
relaxed-day leniency (:func:`screen_locker._weekly_check.is_relaxed_day`)
instead of just resetting to normal. This module never re-derives that
policy: the file carries a signed ``penalty_date``, and the only decision
here is *HMAC ok, dated today*. Anything else -- missing file, stale date,
bad signature -- is "no penalty", i.e. today's ordinary relaxed-day rule.
The file is void when EITHER its ``issued_date`` (the day of the missed
ring) OR its ``penalty_date`` is a sick day (``sickdays``): a sick day stands
every gate down, and wake-alarm's own retraction of the file can lag its
sync by ~15 min, so it is not relied on here.
"""
from __future__ import annotations
from datetime import date
import json
import logging
from typing import TYPE_CHECKING
from gatelock.log_integrity import verify_entry_hmac
from screen_locker._constants import WORKDAY_PENALTY_FILE
from screen_locker._day import today_str
from screen_locker._sick import is_sick
if TYPE_CHECKING:
from datetime import datetime
_logger = logging.getLogger(__name__)
def _read_verified() -> dict[str, object] | None:
"""The file's entry when it exists, parses and verifies; else None."""
if not WORKDAY_PENALTY_FILE.exists():
_logger.debug("No workday penalty file at %s", WORKDAY_PENALTY_FILE)
return None
try:
entry = json.loads(WORKDAY_PENALTY_FILE.read_text())
except (OSError, json.JSONDecodeError) as exc:
_logger.warning("Cannot read %s: %s", WORKDAY_PENALTY_FILE, exc)
return None
if not isinstance(entry, dict) or not verify_entry_hmac(entry):
_logger.warning("Workday penalty file is not a signed object")
return None
return entry
def workday_penalty_today(now: datetime | None = None) -> bool:
"""Whether today lost its relaxed-day leniency to a missed prior ring."""
entry = _read_verified()
if entry is None or entry.get("penalty_date") != today_str(now):
return False
for field in ("issued_date", "penalty_date"):
if _is_sick_iso(entry.get(field)):
_logger.warning("Workday penalty void: its %s is a sick day", field)
return False
return True
def _is_sick_iso(value: object) -> bool:
"""Whether ``value`` is a ``YYYY-MM-DD`` that the sickdays pool marks sick."""
if value is None:
return False # an older file without issued_date: penalty_date rules
try:
day = date.fromisoformat(str(value))
except ValueError:
_logger.warning("Workday penalty: %r is not a date; not a sick day", value)
return False
return is_sick(day)