screen-locker/screen_locker/_wake_state.py
Krzysztof kuhy Rudnicki 967e3cd322 Key every workout day on the LOCAL calendar day, not UTC
A manual workout logged at 00:02 CEST on 2026-09-13 was filed under
2026-09-12: every day-key derivation used datetime.now(tz=UTC), and at
that instant UTC was still yesterday. The 09:18 lock check looked for
2026-09-13, found nothing, and locked the PC — for a workout that was on
disk. The budget counter (a trailing window) counted it fine, which is
why the status showed 1/2 used while the locker still locked. Any log
between local midnight and 02:00 CEST hit this; it had happened before.

- New screen_locker/_day.py: today_str() is the one definition of "what
  day is it" — the local calendar day the user sees on the clock, which
  is also what the phone app, RunnerUp verification, the status window
  and the freedays pool already used. All 14 UTC sites (log writer, lock
  predicates, explain chain, early-bird, wake state, shutdown/sick state,
  sick budget, manual CLI, phone verification) now go through it, writers
  and readers together so no new midnight window opens between them.
- write_signed_entry dedups by workout_id across ALL days: a workout id is
  globally unique, and an entry moved to its correct day must not let its
  synced twin (still stamped with the old day) re-ingest as a duplicate.
- scripts/rekey_log_entry.py: move a wrongly-filed entry to its real day
  (the HMAC never covered the day key, so it still verifies).
- test_local_day_key.py freezes the clock at the exact failing instant.

Replayed the real timeline (log at 00:02:35, evaluate at 09:18:53, weekly
3/5) against HEAD in a throwaway worktree vs this tree:
  HEAD  -> logged under 2026-09-12, LOCK
  fixed -> logged under 2026-09-13, no lock (already_logged)

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FcU5dCcUNHMAMzUNChdrrJ
2026-09-13 09:50:06 +02:00

65 lines
1.8 KiB
Python

"""Read wake-alarm state to check if a workout skip was earned today.
This module reads the JSON state file written by the companion wake_alarm
service. It does not import wake_alarm directly — the two packages
communicate only through the shared state file on disk.
The state file path defaults to WAKE_STATE_FILE in _constants.py, which
points to the sibling wake_alarm package directory. Override it in tests
by patching ``screen_locker._wake_state.WAKE_STATE_FILE``.
"""
from __future__ import annotations
import json
import logging
from gatelock.log_integrity import verify_entry_hmac
from screen_locker._constants import WAKE_STATE_FILE
from screen_locker._day import today_str
_logger = logging.getLogger(__name__)
def _today_str() -> str:
"""Return today's LOCAL date as YYYY-MM-DD (see ``screen_locker._day``)."""
return today_str()
def load_wake_state() -> dict[str, object] | None:
"""Load and verify today's wake state.
Returns the state dict if it exists, is valid (HMAC OK), and is
for today. Returns None otherwise.
"""
if not WAKE_STATE_FILE.exists():
return None
try:
with WAKE_STATE_FILE.open() as f:
state = json.load(f)
except OSError, json.JSONDecodeError:
_logger.warning("Cannot read wake state file")
return None
if not isinstance(state, dict):
return None
if state.get("date") != _today_str():
return None
if not verify_entry_hmac(state):
_logger.warning("Wake state HMAC verification failed")
return None
return state
def has_workout_skip_today() -> bool:
"""Check if the user earned a workout skip for today."""
state = load_wake_state()
if state is None:
return False
return bool(state.get("skip_workout"))