mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-10 19:27:14 +01:00
The periodic diet-guard-sync.timer paid a network round-trip every ~15 min to approximate the two moments the shared log actually changes on this device. Sync now fires on those events instead: * before a lock -- already present in _cli_gate._should_lock, which pulls only when a lock is otherwise due, so a phone-logged meal clears the lock and a not-due tick never touches the network; * after a meal is logged here -- new _sync_events.publish_after_log, wired into all three write paths (_cli_log, _cli_gate, _mcp). This is what stops the PHONE nagging for a slot already logged on the PC, the mirror image of the rule that keeps the phone's own tick unconditional. The gate's publish sits in cmd_gate AFTER MealGate.run() returns, never in the unlock handler: that runs on the Tk thread with the window still up, so a hanging network call would keep the user locked in behind a successful log. It also covers the "Synced from another device" unlock, which hooking log_meal would miss. Accepted trade: a PC that is on but neither logging nor locking no longer pulls, so an inbound phone edit lands at the next log or lock rather than within 15 min. Bounded by slot spacing -- an unlogged day raises a lock. install.sh now disables and removes the installed timer (guarded, since a fresh machine has no such unit and `disable` would abort under `set -euo pipefail`); the .service stays for manual one-off ticks. Every test that logs a meal would otherwise reach the network through the new call sites, so conftest patches publish_after_log at each of the three call sites -- not on _sync_events, so test_sync_events still exercises the real helper. Suite stays at ~8s, which is the actual leak detector. Two files had to be split to commit at all, since file-length passes filenames and both were already over the 250-line cap: _cli.py 466 -> 207, + _cli_args.py, _cli_init.py, _cli_log.py _mcp.py 370 -> 194, + _mcp_server.py (singleton + annotations), _mcp_read.py Their conftest/test patch targets moved in the same commit; a stale target redirects nothing and fails open. Verified: 701 python tests at 100% branch coverage, 555 flutter tests, `python -m diet_guard sync` -> 402 entries/exit 0, `gate --check` -> exit 0, all five MCP tools still register, live ~/.local/share/diet_guard byte-identical. Over-cap files: 44 -> 42. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RA9ynjqXPnexRQnR5oAuCC
51 lines
2.2 KiB
Python
51 lines
2.2 KiB
Python
"""Event-driven sync triggers: publish when state changes, not on a clock.
|
|
|
|
diet_guard used to run a ``diet-guard-sync.timer`` every ~15 min. That cadence
|
|
was a poor proxy for the two moments the shared log actually changes on this
|
|
device, and it paid a network round-trip on every idle tick in between:
|
|
|
|
* **before a lock** -- already handled in :func:`diet_guard._cli_gate._should_lock`,
|
|
which pulls only when a lock is otherwise due, so a meal just logged on the
|
|
phone clears the lock without a manual re-entry.
|
|
* **after a meal is logged here** -- this module. Publishing immediately is
|
|
what stops the *phone* nagging for a slot already logged on the PC; the
|
|
mirror image of the rule in ``CLAUDE.md`` that keeps the phone's tick
|
|
unconditional.
|
|
|
|
Both go through :func:`~diet_guard._sync.pull_shared_log`, which despite its
|
|
name runs a *full* tick (pull, merge, re-sign, persist, push, plus budget and
|
|
both food banks) and fails closed -- a sync outage returns a reason string
|
|
rather than raising, so it can never turn a successful local log into an error.
|
|
|
|
The trade this makes explicit: a PC that is on but neither logging nor locking
|
|
no longer pulls at all, so an inbound phone edit lands at the next log or lock
|
|
rather than within 15 min. That is bounded by the meal-slot spacing, since a
|
|
day that goes unlogged raises a lock by construction.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from diet_guard._sync import pull_shared_log
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
def publish_after_log() -> str | None:
|
|
"""Run a full sync tick after a local write, swallowing any outage.
|
|
|
|
Call this *after* the local log write is durable, never before: the local
|
|
entry is the source of truth for this device, and a network failure must
|
|
leave it intact and the caller's own success path untouched.
|
|
|
|
Returns:
|
|
``None`` when the tick completed, or a short human-readable reason when
|
|
it could not -- callers that have somewhere to show it (the CLI) print
|
|
it; callers that do not (the gate, the MCP tool) let it fall to the log.
|
|
"""
|
|
reason = pull_shared_log()
|
|
if reason is not None:
|
|
_logger.info("post-log sync skipped: %s", reason)
|
|
return reason
|