diet-guard/diet_guard/_gatelock_credit.py
Krzysztof kuhy Rudnicki dfd60d4f7b Close the open gate when a phone meal arrives
A meal logged on the phone while the gate window was up changed nothing
until Fetch was pressed: the new credit watch only fires on a local
write to the food log, and the PC never pulled on its own.

While a non-demo window is open it now pulls peer logs every 30 s, with
the Fetch button's own narrow pull on a worker thread. One pull is in
flight at a time (shared with the button), failures are logged and
retried, and the window stays up. The pull's write trips the watch,
which closes through the usual reconcile path. gatelock moves to 0.14.0
for watch_credit.

refresh_peer_logs also read the local log before the peer downloads and
wrote it after, so a meal logged during the pull was overwritten. It now
merges the peers first and reads the local log just before the write.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWWfb63jncj9Lca2jeX
2026-11-16 09:18:07 +01:00

53 lines
1.8 KiB
Python

"""The open gate window notices a meal that was logged somewhere else.
Until 2026-11-16 the window's only passive watcher was ``watch_hold``; a meal
the phone (or any other process) wrote to the local log while the window was up
changed nothing until the user pressed Fetch. This hands the shared
``gatelock.watch_credit`` three things diet_guard already has:
* the file to watch -- the food log ``log_meal`` and every sync merge write;
* "is it done?" -- :func:`diet_guard._gate.due_slots`, never a second copy of
the rule: done when none of the slots this window still owes is due any more;
* the exit -- the window's own reconcile (``_reconcile_after_fetch``), the same
path the Fetch button takes, so the status line and unlock delay are identical.
The credit was already written by whoever logged it. Closing writes nothing.
Failure mode: closed -- a log that cannot be read keeps the lock up.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from gatelock import watch_credit
from diet_guard import _state
from diet_guard._gate import due_slots
if TYPE_CHECKING:
from collections.abc import Callable
from tkinter import Misc
__all__ = ["arm_credit_watch"]
def arm_credit_watch(
root: Misc,
pending: Callable[[], list[int]],
on_credit: Callable[[], None],
) -> None:
"""Close the window once every slot it still owes is covered by the log.
Args:
root: The gate's Tk root.
pending: Returns the slots the window still owes. A callable, because
the window rebuilds that list as slots are satisfied.
on_credit: The window's reconcile/close path.
"""
def _all_covered() -> bool:
owed = set(pending())
return bool(owed) and owed.isdisjoint(due_slots())
watch_credit(root, (_state.food_log_path(),), _all_covered, on_credit)