2026-07-03 15:27:08 +02:00
|
|
|
"""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.
|
|
|
|
|
"""
|
2026-05-22 22:48:28 +02:00
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
|
import json
|
|
|
|
|
import logging
|
|
|
|
|
|
2026-07-03 15:27:08 +02:00
|
|
|
from gatelock.log_integrity import compute_entry_hmac, verify_entry_hmac
|
|
|
|
|
|
2026-05-28 07:43:06 +02:00
|
|
|
from screen_locker._constants import (
|
2026-05-22 22:48:28 +02:00
|
|
|
EARLY_BIRD_END_HOUR,
|
|
|
|
|
EARLY_BIRD_END_MINUTE,
|
2026-07-03 15:27:08 +02:00
|
|
|
EARLY_BIRD_PENDING_FILE,
|
2026-05-22 22:48:28 +02:00
|
|
|
EARLY_BIRD_START_HOUR,
|
Add auto-fill RunnerUp scan, carrot bonuses, and --status interface
- Refactor RunnerUp verification: extract RunnerUpDbMixin (_runnerup_db.py),
split _scan_and_fill_week_runnerup into a helper _try_fill_runnerup_for_date
to keep cyclomatic complexity ≤10
- Generalise TCX lookup to any date in the ISO week (was today-only); all gap
days Mon→today auto-filled on every startup and 08:30 timer firing
- Add _adjust_shutdown_time_by(): +1h per extra workout beyond the 4-workout
minimum, capped at midnight (hour=24)
- Add _shutdown_base.py: daily reset of shutdown config to a stored base so
the bonus doesn't silently accumulate across days
- Add _extra_benefits.py: streak tracking, skip credits (earn (n-4) credits
for 5+ workout weeks), early-bird extension to 09:00 for eligible weeks
- Add --status mode (_status.py): non-locking CLI view showing per-day
breakdown (✓/✗), RunnerUp auto-scan, bonus status, shutdown time, streak,
skip credits, and early-bird status
- Hook carrot into _check_non_verify_exits: bonus applied whenever auto-fill
pushes weekly count above the minimum
- Pass all pre-commit hooks (ruff, mypy, pylint, bandit, shellcheck,
codespell, max-file-length); 508 tests at 100% branch coverage
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017auyHmf2ZwQcDAwXaSo7KX
2026-06-28 08:08:35 +02:00
|
|
|
EXTRA_BENEFITS_FILE,
|
2026-05-22 22:48:28 +02:00
|
|
|
)
|
Add auto-fill RunnerUp scan, carrot bonuses, and --status interface
- Refactor RunnerUp verification: extract RunnerUpDbMixin (_runnerup_db.py),
split _scan_and_fill_week_runnerup into a helper _try_fill_runnerup_for_date
to keep cyclomatic complexity ≤10
- Generalise TCX lookup to any date in the ISO week (was today-only); all gap
days Mon→today auto-filled on every startup and 08:30 timer firing
- Add _adjust_shutdown_time_by(): +1h per extra workout beyond the 4-workout
minimum, capped at midnight (hour=24)
- Add _shutdown_base.py: daily reset of shutdown config to a stored base so
the bonus doesn't silently accumulate across days
- Add _extra_benefits.py: streak tracking, skip credits (earn (n-4) credits
for 5+ workout weeks), early-bird extension to 09:00 for eligible weeks
- Add --status mode (_status.py): non-locking CLI view showing per-day
breakdown (✓/✗), RunnerUp auto-scan, bonus status, shutdown time, streak,
skip credits, and early-bird status
- Hook carrot into _check_non_verify_exits: bonus applied whenever auto-fill
pushes weekly count above the minimum
- Pass all pre-commit hooks (ruff, mypy, pylint, bandit, shellcheck,
codespell, max-file-length); 508 tests at 100% branch coverage
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017auyHmf2ZwQcDAwXaSo7KX
2026-06-28 08:08:35 +02:00
|
|
|
from screen_locker._extra_benefits import has_extended_early_bird
|
2026-05-22 22:48:28 +02:00
|
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
2026-07-03 15:27:08 +02:00
|
|
|
def _today_str() -> str:
|
|
|
|
|
"""Return today's date as YYYY-MM-DD in UTC."""
|
|
|
|
|
return datetime.now(tz=timezone.utc).strftime("%Y-%m-%d")
|
|
|
|
|
|
|
|
|
|
|
2026-05-22 22:48:28 +02:00
|
|
|
class EarlyBirdMixin:
|
2026-07-03 15:27:08 +02:00
|
|
|
"""Mixin providing early-bird time window checks and pending-state helpers."""
|
2026-05-22 22:48:28 +02:00
|
|
|
|
|
|
|
|
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:
|
Add auto-fill RunnerUp scan, carrot bonuses, and --status interface
- Refactor RunnerUp verification: extract RunnerUpDbMixin (_runnerup_db.py),
split _scan_and_fill_week_runnerup into a helper _try_fill_runnerup_for_date
to keep cyclomatic complexity ≤10
- Generalise TCX lookup to any date in the ISO week (was today-only); all gap
days Mon→today auto-filled on every startup and 08:30 timer firing
- Add _adjust_shutdown_time_by(): +1h per extra workout beyond the 4-workout
minimum, capped at midnight (hour=24)
- Add _shutdown_base.py: daily reset of shutdown config to a stored base so
the bonus doesn't silently accumulate across days
- Add _extra_benefits.py: streak tracking, skip credits (earn (n-4) credits
for 5+ workout weeks), early-bird extension to 09:00 for eligible weeks
- Add --status mode (_status.py): non-locking CLI view showing per-day
breakdown (✓/✗), RunnerUp auto-scan, bonus status, shutdown time, streak,
skip credits, and early-bird status
- Hook carrot into _check_non_verify_exits: bonus applied whenever auto-fill
pushes weekly count above the minimum
- Pass all pre-commit hooks (ruff, mypy, pylint, bandit, shellcheck,
codespell, max-file-length); 508 tests at 100% branch coverage
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017auyHmf2ZwQcDAwXaSo7KX
2026-06-28 08:08:35 +02:00
|
|
|
"""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.
|
|
|
|
|
"""
|
2026-05-22 22:48:28 +02:00
|
|
|
minutes = self._get_local_time_minutes()
|
|
|
|
|
start = EARLY_BIRD_START_HOUR * 60
|
Add auto-fill RunnerUp scan, carrot bonuses, and --status interface
- Refactor RunnerUp verification: extract RunnerUpDbMixin (_runnerup_db.py),
split _scan_and_fill_week_runnerup into a helper _try_fill_runnerup_for_date
to keep cyclomatic complexity ≤10
- Generalise TCX lookup to any date in the ISO week (was today-only); all gap
days Mon→today auto-filled on every startup and 08:30 timer firing
- Add _adjust_shutdown_time_by(): +1h per extra workout beyond the 4-workout
minimum, capped at midnight (hour=24)
- Add _shutdown_base.py: daily reset of shutdown config to a stored base so
the bonus doesn't silently accumulate across days
- Add _extra_benefits.py: streak tracking, skip credits (earn (n-4) credits
for 5+ workout weeks), early-bird extension to 09:00 for eligible weeks
- Add --status mode (_status.py): non-locking CLI view showing per-day
breakdown (✓/✗), RunnerUp auto-scan, bonus status, shutdown time, streak,
skip credits, and early-bird status
- Hook carrot into _check_non_verify_exits: bonus applied whenever auto-fill
pushes weekly count above the minimum
- Pass all pre-commit hooks (ruff, mypy, pylint, bandit, shellcheck,
codespell, max-file-length); 508 tests at 100% branch coverage
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017auyHmf2ZwQcDAwXaSo7KX
2026-06-28 08:08:35 +02:00
|
|
|
if has_extended_early_bird(EXTRA_BENEFITS_FILE):
|
|
|
|
|
end = 9 * 60 # 09:00
|
|
|
|
|
else:
|
|
|
|
|
end = EARLY_BIRD_END_HOUR * 60 + EARLY_BIRD_END_MINUTE
|
2026-05-22 22:48:28 +02:00
|
|
|
return start <= minutes < end
|
|
|
|
|
|
2026-07-03 15:27:08 +02:00
|
|
|
def _is_early_bird_pending(self) -> bool:
|
|
|
|
|
"""Check if today has an unresolved early-bird pending marker."""
|
|
|
|
|
if not EARLY_BIRD_PENDING_FILE.exists():
|
2026-05-22 22:48:28 +02:00
|
|
|
return False
|
|
|
|
|
try:
|
2026-07-03 15:27:08 +02:00
|
|
|
with EARLY_BIRD_PENDING_FILE.open() as f:
|
|
|
|
|
state = json.load(f)
|
2026-05-22 22:48:28 +02:00
|
|
|
except (OSError, json.JSONDecodeError):
|
|
|
|
|
return False
|
2026-07-03 15:27:08 +02:00
|
|
|
if not isinstance(state, dict) or state.get("date") != _today_str():
|
2026-05-22 22:48:28 +02:00
|
|
|
return False
|
2026-07-03 15:27:08 +02:00
|
|
|
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
|
2026-05-22 22:48:28 +02:00
|
|
|
|
2026-07-03 15:27:08 +02:00
|
|
|
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)
|