diet-guard/diet_guard/_cli_gate.py
Krzysztof kuhy Rudnicki be94b4e147 Sync before locking the gate + manual "Fetch from sync" button
The PC gate forced a re-log when a meal was already logged on the phone
but the 15-min sync had not yet propagated it. Two ways to close that gap:

- The gate now pulls the shared log right before it would lock -- and only
  when a lock is otherwise due, so the network cost is paid on the rare
  tick that matters. A meal already pushed by the phone then unlocks
  without re-entry. Fails closed: any sync error (no token, network, disk
  write-back) falls through to the local lock decision, never skips it.
- A "Fetch from sync" button on the lock screen runs the same pull on
  manual request: it auto-unlocks when the pull satisfies every pending
  slot, advances to the next meal on a partial pull, or reports no change.
  Disabled in --demo so the sandbox never triggers a real sync/push.
- Both paths share one fail-closed wrapper, _sync.pull_shared_log(), which
  also keeps the single broad-except in one place.
- _write_log is now atomic (temp file + Path.replace): the gate and the
  15-min sync timer can now both write the log, so a torn read must be
  impossible.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FtSmzACzJSGyEatgK9exkd
2026-07-11 17:54:38 +02:00

80 lines
3.2 KiB
Python

"""CLI handler for the ``gate`` subcommand.
Split out from :mod:`diet_guard._cli` to keep that module under the repo's
500-line cap (see ``CLAUDE.md``'s "feat: split oversized modules" history).
The gate's actual window logic already lives in ``_gatelock*.py``; this is
just the thin CLI glue, same as ``_cli_sync.py`` is for ``sync``.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from diet_guard._gate import gate_is_due
from diet_guard._gatelock import MealGate, acquire_gate_lock, release_gate_lock
from diet_guard._gatelock_support import wait_for_display
from diet_guard._sync import pull_shared_log
if TYPE_CHECKING:
from collections.abc import Callable
def _is_due(emit: Callable[[str], None]) -> bool:
"""Return the gate decision, refreshing from sync only when a lock is due.
The cheap local check runs first; the network pull happens solely on the
rare tick a lock would otherwise fire (via :func:`pull_shared_log`, which
fails closed), then the decision is re-read from the freshly written log.
"""
if not gate_is_due():
return False
reason = pull_shared_log() # only pay the network cost when about to lock
if reason is not None:
emit(f"{reason}; using local log.")
return gate_is_due()
def cmd_gate(emit: Callable[[str], None], *, check: bool, demo: bool) -> int:
"""Run the log-to-unlock gate.
Three modes: ``--check`` is a headless decision (no window) whose exit code
a timer reads; ``--demo`` always shows a safe demo window; bare ``gate``
shows the real lock only when one is due. Both real modes route through
:func:`_is_due`, which -- only when a lock is otherwise due -- pulls the
shared log first (see :func:`_refresh_from_sync`) so a meal just logged on
the phone unlocks without a manual re-entry. A flock guard stops a second
window from stacking on top of the first, and a window-opening mode first
waits for the X display so a session-start launch never crashes unshown.
Args:
emit: A one-line output sink (``_cli._emit``, passed in rather than
imported -- see ``_cli_sync.cmd_sync`` for why).
check: Headless mode -- print and return an exit code, open no window.
demo: Use safe demo mode (local grab + close button) for the window.
Returns:
For ``--check``: 0 if not due, 1 if a lock is due. Otherwise 0.
"""
if check:
due = _is_due(emit)
emit("due (a lock is warranted)" if due else "ok (no lock needed)")
return 1 if due else 0
if not demo and not _is_due(emit):
emit("ok - no lock needed right now.")
return 0
handle = acquire_gate_lock()
if handle is None:
emit("the gate is already running.")
return 0
try:
# At session start the timer can fire before the X display/auth cookie
# is ready; wait it out so the window opens instead of crashing on a
# "couldn't connect to display" TclError (see _gatelock.wait_for_display).
if not wait_for_display():
emit("display not ready yet; will retry on the next timer tick.")
return 0
MealGate(demo_mode=demo).run()
finally:
release_gate_lock(handle)
return 0