diet-guard/diet_guard/_gate.py
Krzysztof kuhy Rudnicki 2bdb7925a8 Read the meal schedule from a forward-only history (Python)
Wires the Python side to the configurable schedule. No edit surface yet, so
nothing writes a schedule and current_schedule() returns the default -- the
derived hours are still 08/12/16/20 and the cutoff is still 22:00. Verified by
running it: before any edit the slots are unchanged, after recording 08-20 x5
they become 08:00, 11:00, 14:00, 17:00, 20:00, and a 2020 day still resolves
to the four-meal schedule.

_slots.py now takes the schedule as a *required* argument rather than reading
module constants. Required, not defaulted, on purpose: a default would let a
call site missed in this refactor keep deriving the old fixed hours on one
device, and a slot one device offers while the other does not is a checkpoint
that can never be satisfied. Making it required let mypy enumerate the nine
call sites instead of trusting a grep.

The history is forward-only and seeds the default at the epoch *before*
recording today's value, so switching to five meals leaves past days judged
against the four-meal schedule they actually ran under. Same shape and same
reasoning as _budget_history.py; it will ride the same CRDT record.

Two details worth naming:

- MEAL_SCHEDULE_FILE lands in DATA_DIR, so conftest's _isolate_state redirect
  is patched in this same commit rather than a later one. Confirmed with a
  canary that the redirect is live and that ~/.local/share/diet_guard/ has no
  .meal_schedule after a full run.
- _mcp_read imports _budget.current_schedule already, so the meal-schedule
  lookup is aliased rather than shadowing it.

_gatelock._pending_slots is spelled `is None` rather than `or` now. The two are
in fact equivalent -- if 0 is a slot it is necessarily the first slot, checked
exhaustively -- but the intent should not rest on that coincidence.

761 tests, 100% branch coverage.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0159b4E5hh512jYUeaJVNxfM
2026-08-16 14:28:22 +02:00

74 lines
2.5 KiB
Python

"""Decision logic for the diet_guard slot-based log-to-unlock gate.
This module is GUI-free and side-effect-free so the lock/no-lock decision can
be verified headlessly: the fullscreen window in ``_gatelock.py`` is only a
thin shell around :func:`gate_is_due` and :func:`due_slots`. It composes the
pure slot arithmetic in :mod:`diet_guard._slots` with the logged-slot
state in :mod:`diet_guard._state`; ``now`` is injectable so the
time-of-day rules stay deterministically testable.
The gate fires when any *elapsed* meal slot for today carries no logged meal.
Coming home late therefore surfaces several unlogged slots at once -- a single
lock that backfills the whole day before the PC is usable -- while a normal day
prompts one slot at a time, with no separate weekday code path.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from diet_guard._meal_schedule_store import current_schedule
from diet_guard._slots import missing_slots, slot_label
from diet_guard._state import (
now_local,
)
from diet_guard._state_today import (
logged_slots_today,
)
if TYPE_CHECKING:
from datetime import datetime
def due_slots(now: datetime | None = None) -> tuple[int, ...]:
"""Return today's elapsed-but-unlogged meal slots, ascending.
Args:
now: Reference time (defaults to the current local time); injectable.
Returns:
The slot hours that still need a meal logged (empty == nothing due).
"""
reference = now if now is not None else now_local()
return missing_slots(reference, logged_slots_today(), current_schedule())
def gate_is_due(now: datetime | None = None) -> bool:
"""Return True if the screen should lock until the missing slots are filled.
Args:
now: Reference time (defaults to the current local time); injectable.
Returns:
True if at least one elapsed slot today is unlogged, else False.
"""
return bool(due_slots(now))
def gate_message(now: datetime | None = None) -> str:
"""Return the lock-screen reason line listing the slots to backfill.
Args:
now: Reference time (defaults to the current local time); injectable.
Returns:
A short human-readable explanation of which meals are missing.
"""
missing = due_slots(now)
if not missing:
return "All meals are logged. You're up to date."
labels = ", ".join(slot_label(slot) for slot in missing)
if len(missing) == 1:
return f"Log your {labels} meal to unlock."
return f"Log your meals for {labels} to unlock."