testsAndMisc/python_pkg/diet_guard/diet-guard-gate.service
Krzysztof kuhy Rudnicki 31992b2a90 feat(diet_guard): add meal-logging screen-lock gate with trigger fix
Add the diet_guard package: a screen-locking meal-logging gate that fires
on 4-hour slots (08/12/16/20) and records calories/macros, persisting an
autocompleting food bank.

- Trigger fix: the systemd timer fires at session start (Persistent=true)
  before lightdm has written ~/.Xauthority, so the gate crashed with a
  TclError instead of locking the screen. Add wait_for_display() /
  _display_is_ready() in _gatelock.py and wire it into _cli._cmd_gate so the
  gate retries on the next tick instead of crashing; add
  Environment=XAUTHORITY=%h/.Xauthority to the service as belt-and-suspenders.
- Food-bank hardening: a transiently corrupt food_bank.json was warned about
  on every keystroke and then silently overwritten (data loss). _read_bank
  now quarantines it via _quarantine_corrupt_bank() (warn-once + timestamped
  backup) before starting fresh.
- Multi-item meals: new _meal.py (MealItem, meal_total, MEAL_SOURCE),
  remember_meal() + _upsert() in _foodbank.py, and a "+ Add item" control in
  the gate that logs both the individual items and the composite meal.
- Bundle resolve_nutrition's manual macros into a ManualMacros dataclass to
  stay within the argument-count limit.

diet_guard at 100% branch coverage; full pre-commit suite passes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-10 22:32:39 +02:00

26 lines
1.2 KiB
Desktop File

[Unit]
Description=Diet Guard log-to-unlock gate (periodic check)
After=graphical-session.target
[Service]
Type=oneshot
# DISPLAY/PYTHONPATH mirror wake-alarm.service: the gate opens a Tk window when a
# lock is due, so without DISPLAY it would crash with "no display name and no
# $DISPLAY" before it could even check. The command self-checks gate_is_due() and
# exits 0 when no lock is needed, so running it every ~30 min is cheap.
#
# XAUTHORITY pins the X auth cookie path explicitly. It is belt-and-suspenders,
# not the fix: when this unit fires at SESSION START (Persistent=true catch-up),
# it can beat the display manager writing ~/.Xauthority, so the cookie is simply
# absent yet -- pointing at it does not help. That race is handled in Python by
# wait_for_display(), which polls the display until it is connectable before
# opening the window (previously a session-start launch died on a "couldn't
# connect to display" TclError and never showed). The sleep gives the session a
# brief head start; the Python wait is what makes it reliable.
Environment=DISPLAY=:0
Environment=XAUTHORITY=%h/.Xauthority
Environment=PYTHONPATH=%h/testsAndMisc
ExecStartPre=/bin/sleep 1
ExecStart=/usr/bin/python -m python_pkg.diet_guard gate
WorkingDirectory=%h/testsAndMisc