mirror of
https://github.com/kuhyx/screen-locker.git
synced 2026-11-10 20:07:16 +01:00
The workout log stored ONE workout per calendar day, so a second same-day
workout was silently dropped and could never earn credit.
Multiple workouts per day:
- workout_log.json: {date: entry} -> {date: [entry, ...]}. Legacy entries are
wrapped on read (_log_io.load_workout_log) so their HMACs stay valid — no
re-signing; every write normalizes the file, migrating it progressively.
- New write chokepoint (_log_mixin.write_signed_entry) APPENDS and dedups by a
stable workout_id, returning RecordResult so callers can tell a new workout
from a duplicate. Re-verifying the same workout is a no-op.
- count_weekly_workouts is anti-gaming: each VERIFIED workout counts
individually; self-reported manual entries count at most once per day. Four
real workouts in one day satisfy the week; four manual entries count as one.
- Reward: the day's first counted workout gets the base +2h; an additional
same-day VERIFIED workout gets +1h; an additional manual gets nothing. The
old "+1h when weekly_count > 4" gate is replaced — weekly volume is the
banked bonus's job and shouldn't be paid twice.
- Fixed: the unlock screen labelled the +1h "#{weekly_count - 4}", which can
render negative under the new rule. Removed the dead runnerup_manual type.
- Flutter: the manual budget counts distinct DAYS, matching the PC.
PC->phone sync (it had never worked once):
- push_pc_manuals pushed from a side-store written at log-time, so anything
logged before that feature existed could never publish. _manual_push now
derives the crdt log directly from workout_log.json — the single source of
truth — covering manual AND verified runs. The record id is the workout's own
workout_id (derived for legacy entries to match the phone's format) and the
HLC comes from the entry's own timestamp, so a re-push is byte-identical and
the repo never churns.
- The phone reads every kind (readMergedWorkoutPayloads) and lists the PC's
workouts under "SYNCED FROM PC". The manual budget still sees manual-only, so
a verified run can't consume a self-report slot.
- Settings claimed "Connected." whenever a token STRING existed. It now
verifies against the API on open, so a revoked token reports 401 instead of
showing a green lie.
Never fail silently:
- 30 swallowed exceptions across 19 files now log WARNING with the concrete
cause AND consequence — plus the token guards in _workout_sync that returned
[] in total silence, the exact bug that hid the dead sync for weeks.
- New pre-commit hook (scripts/check_silent_failures.py) fails any except that
neither re-raises nor logs loudly. ruff's S110 only catches `except: pass`;
the real pattern here was `except: log.info(); return []`. Documented in
CLAUDE.md.
Verified on real data: the Jul 13 run (pulled from its actual TCX over ADB) now
counts alongside that day's manual squash, and all three workouts are published
to kuhyx/syncs.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Fv8ULVt3Q67GwCFbM7doHB
163 lines
5.3 KiB
Python
163 lines
5.3 KiB
Python
"""Non-locking status view: workout count, bonuses, RunnerUp scan trigger."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import date, datetime, timedelta, timezone
|
|
import json
|
|
import logging
|
|
import sys
|
|
from typing import TYPE_CHECKING
|
|
|
|
from screen_locker._constants import EXTRA_BENEFITS_FILE
|
|
from screen_locker._extra_benefits import (
|
|
current_streak,
|
|
has_extended_early_bird,
|
|
weekly_shutdown_bonus_hours,
|
|
)
|
|
from screen_locker._log_io import load_workout_log
|
|
from screen_locker._sick_tracker import load_history
|
|
from screen_locker._weekly_check import (
|
|
COUNTED_WORKOUT_TYPES,
|
|
WEEKLY_WORKOUT_MINIMUM,
|
|
count_weekly_workouts,
|
|
)
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
from screen_locker.screen_lock import ScreenLocker
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
def _load_extra_benefits() -> dict:
|
|
"""Load extra_benefits_state.json, returning {} on any error."""
|
|
if not EXTRA_BENEFITS_FILE.exists():
|
|
return {}
|
|
try:
|
|
return json.loads(EXTRA_BENEFITS_FILE.read_text())
|
|
except (OSError, ValueError) as exc:
|
|
_logger.warning(
|
|
"Could not read extra-benefits state from %s: %s — the status view "
|
|
"will show a 0 streak and no bonus hours, which may be wrong",
|
|
EXTRA_BENEFITS_FILE,
|
|
exc,
|
|
)
|
|
return {}
|
|
|
|
|
|
def _print_day_line(d: date, entries: list[dict], sick_days: set[str]) -> bool:
|
|
"""Print one day's status line(s). Returns True if any workout counted.
|
|
|
|
A day may hold several workouts — each is printed on its own line.
|
|
"""
|
|
label = d.strftime("%a %b %d")
|
|
if not entries:
|
|
if d.isoformat() in sick_days:
|
|
print(f" {label} ✗ sick_day")
|
|
else:
|
|
print(f" {label} — no entry")
|
|
return False
|
|
any_counted = False
|
|
for entry in entries:
|
|
wtype = entry.get("workout_data", {}).get("type", "?")
|
|
src = entry.get("workout_data", {}).get("source", "")
|
|
counted = wtype in COUNTED_WORKOUT_TYPES
|
|
any_counted = any_counted or counted
|
|
src_str = f" ({src[:45]})" if src else ""
|
|
mark = "✓" if counted else "✗"
|
|
print(f" {label} {mark} {wtype}{src_str}")
|
|
return any_counted
|
|
|
|
|
|
def run_status(locker: ScreenLocker) -> None:
|
|
"""Print weekly workout status, run RunnerUp scan, apply bonus, then exit."""
|
|
today = datetime.now(tz=timezone.utc).astimezone().date()
|
|
monday = today - timedelta(days=today.weekday())
|
|
log_file: Path = locker.log_file
|
|
log_data = load_workout_log(log_file)
|
|
sick_days = set(load_history().sick_days)
|
|
|
|
print("=== Weekly Workout Status ===")
|
|
|
|
# Per-day breakdown
|
|
before_count = 0
|
|
for i in range(7):
|
|
d = monday + timedelta(days=i)
|
|
if d > today:
|
|
break
|
|
if _print_day_line(d, log_data.get(d.isoformat(), []), sick_days):
|
|
before_count += 1
|
|
|
|
print()
|
|
|
|
# RunnerUp scan
|
|
n_filled = locker._scan_and_fill_week_runnerup(log_file)
|
|
if n_filled > 0:
|
|
print(f" Auto-filled {n_filled} workout(s) from RunnerUp exports.")
|
|
after_count = count_weekly_workouts(log_file)
|
|
bonus = max(0, after_count - max(WEEKLY_WORKOUT_MINIMUM, before_count))
|
|
if bonus > 0:
|
|
ok = locker._adjust_shutdown_time_by(bonus)
|
|
if ok:
|
|
print(f" +{bonus}h shutdown bonus applied.")
|
|
else:
|
|
print(f" +{bonus}h shutdown bonus pending (config write failed).")
|
|
else:
|
|
print(" No new workouts found via RunnerUp scan.")
|
|
after_count = before_count
|
|
|
|
print()
|
|
|
|
# Extra benefits summary
|
|
bonus_hours = weekly_shutdown_bonus_hours(EXTRA_BENEFITS_FILE)
|
|
streak = current_streak(EXTRA_BENEFITS_FILE)
|
|
eb_ext = has_extended_early_bird(EXTRA_BENEFITS_FILE)
|
|
eb_str = "Yes — until 09:00" if eb_ext else "No"
|
|
|
|
# Heat skips this month
|
|
this_month = datetime.now(tz=timezone.utc).astimezone().date().strftime("%Y-%m")
|
|
heat_entries = [
|
|
(d, entry)
|
|
for d, entries in log_data.items()
|
|
if d.startswith(this_month)
|
|
for entry in entries
|
|
if isinstance(entry, dict)
|
|
and entry.get("workout_data", {}).get("type") == "heat_skip"
|
|
]
|
|
if heat_entries:
|
|
last_date, last_e = max(heat_entries, key=lambda de: de[0])
|
|
last_temp = last_e.get("workout_data", {}).get("temperature_celsius", "?")
|
|
heat_str = f"{len(heat_entries)} (last: {last_date}, {last_temp}°C)"
|
|
else:
|
|
heat_str = "0"
|
|
|
|
print(f" Shutdown bonus (this wk): {bonus_hours}h")
|
|
print(f" Streak (5+ wks) : {streak}")
|
|
print(f" Early-bird extended : {eb_str}")
|
|
print(f" Heat skips (month) : {heat_str}")
|
|
print()
|
|
|
|
remaining = max(0, WEEKLY_WORKOUT_MINIMUM - after_count)
|
|
extra = max(0, after_count - WEEKLY_WORKOUT_MINIMUM)
|
|
|
|
if remaining > 0:
|
|
print(
|
|
f" Need {remaining} more to reach the minimum ({WEEKLY_WORKOUT_MINIMUM})."
|
|
)
|
|
elif extra > 0:
|
|
print(f" {after_count}/{WEEKLY_WORKOUT_MINIMUM} — {extra} above minimum!")
|
|
else:
|
|
print(
|
|
f" Weekly minimum met exactly"
|
|
f" ({WEEKLY_WORKOUT_MINIMUM}/{WEEKLY_WORKOUT_MINIMUM})."
|
|
)
|
|
|
|
# Shutdown config
|
|
cfg = locker._read_shutdown_config()
|
|
if cfg:
|
|
_mw, _ts, _morning = cfg
|
|
print(f" Shutdown tonight : {_mw:02d}:00")
|
|
|
|
sys.exit(0)
|