screen-locker/screen_locker/_early_bird.py
Krzysztof kuhy Rudnicki 9ab7c09c79
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (push) Waiting to run
Tests / test (push) Waiting to run
Tests / lock-popup-safety (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
The early-bird window is the wake-alarm carrot, not a clock
05:00-08:30 (09:00 with the extended reward) locked nobody but did hold the
workout lock off on a clock that knew nothing about whether the user was
up, next to the morning-session rung that did. EarlyBirdMixin._is_early_bird_time
is now that rung's verdict: open before the alarm, during the session, and
until 11:00 once it completed in time -- exactly what wake-alarm signs. The
wake_alarm_skip rung banks the pending marker so the carrot's end still tries
a phone/RunnerUp workout before locking; early-bird-workout-check.timer moves
to 09:30:30 and 11:00:30, the two possible ends. The consumer's boot-retry
window starts at 05:00 with the refresher.

Also: the sick screen lists four past justifications, one line each -- six
full-length ones pushed it 7 px past 1366x768, which the screens-fit gate
only sees with a real history on disk. Takes @types/node 26.6.2, eslint 10.11.0.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XTdfXjbGYYrY7Quxcfz6mg
2026-09-20 08:39:44 +02:00

72 lines
2.9 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
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_alarm._state`` in the companion wake-alarm service. It is written when
the wake-alarm carrot defers the lock, so that when the carrot ends the next
run tries a phone/RunnerUp workout before locking.
"""
from __future__ import annotations
import json
import logging
from gatelock.log_integrity import compute_entry_hmac
from screen_locker._compliance_state import is_early_bird_pending
from screen_locker._constants import EARLY_BIRD_PENDING_FILE
from screen_locker._day import today_str
from screen_locker._morning_session import MorningSkip, morning_skip_today
_logger = logging.getLogger(__name__)
def _today_str() -> str:
"""Return today's LOCAL date as YYYY-MM-DD (see ``screen_locker._day``)."""
return today_str()
class EarlyBirdMixin:
"""Mixin providing the early-bird window check and pending-state helpers.
Since 2026-09-20 the window is not a wall clock but the wake-alarm
morning-session carrot: open exactly while wake-alarm's signed
``morning_session.json`` says the morning is being earned (before the
alarm, during the session, and until 11:00 once it completed in time).
The old 05:00-08:30 clock locked someone who got up at 07:00 -- the
opposite of the goal -- and it was a second mechanism next to the one
that actually knows whether the user is up.
"""
_morning_skip: MorningSkip | None = None
def _is_early_bird_time(self) -> bool:
"""Whether the wake-alarm carrot is in force right now.
Waits (bounded) for a freshly booted PC's refresher; the verdict is
kept on the instance so the skip detail can quote its ``exempt_until``.
"""
self._morning_skip = morning_skip_today(wait=True)
return self._morning_skip is not None
def _is_early_bird_pending(self) -> bool:
"""Check if today has an unresolved early-bird pending marker."""
return is_early_bird_pending(EARLY_BIRD_PENDING_FILE)
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)