mirror of
https://github.com/kuhyx/screen-locker.git
synced 2026-07-04 11:43:09 +02:00
The screen locker skipped enforcement on 2026-07-03 without ever showing a lock: a banked skip credit (earned from a prior 5+/week streak) was consumed automatically with no confirmation and no visible log. Reworked the whole reward mechanic instead of just gating it, since banking a "skip a future workout" credit works against maximizing weekly workouts: - Removed skip credits entirely (has_skip_credit/consume_skip_credit and the confirmation dialog built to gate them). The only same-day skip paths left are heat_skip and sick_day, both requiring a genuine reason. - Extra workouts (5+/week) now bank shutdown-time-later hours for the following week instead — comfort, not reduced enforcement. Reuses the existing _adjust_shutdown_time_by and reset_to_base_if_new_day's previously-discarded return value as the once-per-day gate. - early_bird and sick_day no longer pollute workout_log.json. early_bird is a same-day pending marker now stored in its own self-expiring, HMAC-signed file; sick_day is sourced entirely from sick_history.json (already the real source of truth). Fixes an accidental-safety gap where "already took a sick day today" only halted startup by luck. - Cleaned up 3 stale non-workout entries already in workout_log.json. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QdTccgbK7624kfoaV6CtXS
90 lines
3.4 KiB
Python
90 lines
3.4 KiB
Python
"""Early bird window detection and pending-state helpers for ScreenLocker.
|
|
|
|
The early-bird "still waiting to see if a real workout shows up" flag is a
|
|
same-day pending marker, not a workout — it is intentionally kept out of
|
|
workout_log.json (which is reserved for real outcomes) and instead lives in
|
|
its own self-expiring, HMAC-signed state file, mirroring the pattern used by
|
|
``_wake_state.py`` for the companion wake_alarm service.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timezone
|
|
import json
|
|
import logging
|
|
|
|
from gatelock.log_integrity import compute_entry_hmac, verify_entry_hmac
|
|
|
|
from screen_locker._constants import (
|
|
EARLY_BIRD_END_HOUR,
|
|
EARLY_BIRD_END_MINUTE,
|
|
EARLY_BIRD_PENDING_FILE,
|
|
EARLY_BIRD_START_HOUR,
|
|
EXTRA_BENEFITS_FILE,
|
|
)
|
|
from screen_locker._extra_benefits import has_extended_early_bird
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
def _today_str() -> str:
|
|
"""Return today's date as YYYY-MM-DD in UTC."""
|
|
return datetime.now(tz=timezone.utc).strftime("%Y-%m-%d")
|
|
|
|
|
|
class EarlyBirdMixin:
|
|
"""Mixin providing early-bird time window checks and pending-state helpers."""
|
|
|
|
def _get_local_time_minutes(self) -> int:
|
|
"""Return current local time as minutes from midnight."""
|
|
now = datetime.now(tz=timezone.utc).astimezone()
|
|
return now.hour * 60 + now.minute
|
|
|
|
def _is_early_bird_time(self) -> bool:
|
|
"""Return True if current local time is in the early bird window.
|
|
|
|
Normally the window closes at 08:30. When the current ISO week has an
|
|
extended early-bird reward (earned by 5+ workouts the prior week) the
|
|
window extends to 09:00.
|
|
"""
|
|
minutes = self._get_local_time_minutes()
|
|
start = EARLY_BIRD_START_HOUR * 60
|
|
if has_extended_early_bird(EXTRA_BENEFITS_FILE):
|
|
end = 9 * 60 # 09:00
|
|
else:
|
|
end = EARLY_BIRD_END_HOUR * 60 + EARLY_BIRD_END_MINUTE
|
|
return start <= minutes < end
|
|
|
|
def _is_early_bird_pending(self) -> bool:
|
|
"""Check if today has an unresolved early-bird pending marker."""
|
|
if not EARLY_BIRD_PENDING_FILE.exists():
|
|
return False
|
|
try:
|
|
with EARLY_BIRD_PENDING_FILE.open() as f:
|
|
state = json.load(f)
|
|
except (OSError, json.JSONDecodeError):
|
|
return False
|
|
if not isinstance(state, dict) or state.get("date") != _today_str():
|
|
return False
|
|
if verify_entry_hmac(state):
|
|
return True
|
|
if compute_entry_hmac({"_probe": True}) is None and "hmac" not in state:
|
|
_logger.info("HMAC key unavailable — accepting unsigned pending marker")
|
|
return True
|
|
_logger.warning("HMAC verification failed for early-bird pending marker")
|
|
return False
|
|
|
|
def _save_early_bird_pending(self) -> None:
|
|
"""Save today's early-bird pending marker (self-expires tomorrow)."""
|
|
state: dict[str, object] = {"date": _today_str()}
|
|
signature = compute_entry_hmac(state)
|
|
if signature is not None:
|
|
state["hmac"] = signature
|
|
else:
|
|
_logger.warning("HMAC key unavailable — saving unsigned pending marker")
|
|
try:
|
|
with EARLY_BIRD_PENDING_FILE.open("w") as f:
|
|
json.dump(state, f, indent=2)
|
|
except OSError as exc:
|
|
_logger.warning("Could not save early-bird pending marker: %s", exc)
|