mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-11 21:53:52 +01:00
Four asks, plus the fixes two review passes turned up. Removals (all unused): - "Repeat last meal" button, and the reward/temptation-bundling prompt it was the sole trigger for. Replaced with a post-log progress card showing kcal against budget, macros, and the adherence streak. - The multi-item meal builder, on both the gate and the phone. The stored `components` field stays: nothing writes it now, but historical composite entries and the food bank still read it and it is part of the wire format. - Meal photos entirely: 13 files, `imagePath`, the photo filter, the wrapper's /blobs/ route, image_picker, and the CAMERA permission. Reminder false alarms had three real causes, all fixed: - Logging never reconciled notifications, so a reminder sat on screen up to 15 minutes after the meal was logged -- on the phone itself. - The background check never pulled, so a PC-logged meal nagged forever until the app was opened. Now mirrors _cli_gate._should_lock: cheap local check, network only when a slot looks due, then re-check. - Meals before 08:00 / after 22:00 were attributed to no slot at all. New slot_for_log/slotForLog clamps them to the first/last slot, byte-identical across Python and Dart. Deliberately separate from elapsed_slots, which would otherwise make every slot fall due at 23:00. Budget history, so lowering the budget stops rewriting the past: - Forward-only (effective_from, kcal) entries; each day is judged against the budget in force then. Verified live: setting 2000 left a 36-day adherence streak and the whole calendar pixel-identical. - Rides as hist:<date> fields on the existing budget record. crdt_sync merges per-field over the union of field names and both devices push the merged log, so a device that predates the feature relays them untouched -- no coordinated rollout, no second document. - The seed guard tests emptiness, not file presence: _sync can write an empty history after merging with a pre-feature peer, and a presence check would then never seed again, silently reclassifying every past day. One shared source of truth for every piece of data: - Body weight now travels as its own `weight` CRDT field. Inside `value` it was collateral damage of whole-map LWW -- any phone budget edit deleted it, and the protein target with it. - Both halves of the food bank sync. The curated half (foods added without eating them) was the last device-local state. The derived half merges with count as the clock, so LWW means max-count-wins; the log stays authoritative for membership, because a CRDT union never shrinks and a peer's stale copy would otherwise resurrect a deleted food permanently. - Bank adapters honour tombstones, and Dart parses bank records defensively: a cast failure there is an Error, not an Exception, so one malformed remote record would have broken phone sync permanently. _test_guard.py blocks any pathlib write to real user state while pytest is in the process, whatever conftest is loaded. A throwaway test run outside the package conftest destroyed a real GitHub token during this work; this makes that class of accident impossible rather than merely discouraged. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
91 lines
5.2 KiB
Python
91 lines
5.2 KiB
Python
"""Constants for the diet_guard calorie tracker and gate."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
# --- Daily target -----------------------------------------------------------
|
|
# There is deliberately NO budget number here. It is computed once from
|
|
# biometrics at ``init`` time, written to BUDGET_FILE below, and freely
|
|
# editable and synced afterward from either device -- see _budget.py and
|
|
# _sync.py. Read via diet_guard._budget.daily_budget() for over/under
|
|
# decisions; freely shown in local CLI/GUI/app output.
|
|
#
|
|
# Fraction of the budget at which status flips from "on track" to "approaching
|
|
# limit". Also mirrored above 100% as the day-status yellow/red boundary for
|
|
# the calendar -- see _daystatus.py.
|
|
BUDGET_WARN_FRACTION: float = 0.80
|
|
|
|
# --- Storage ----------------------------------------------------------------
|
|
# The food log is personal and high-churn, so it lives in the XDG data dir and
|
|
# is deliberately NOT committed to git (unlike wake_state.json).
|
|
DATA_DIR: Path = Path.home() / ".local" / "share" / "diet_guard"
|
|
FOOD_LOG_FILE: Path = DATA_DIR / "food_log.json"
|
|
# The user's personal "food bank": every food they have logged before, with its
|
|
# full macros, keyed by name. This is the ONLY corpus the gate's autocomplete
|
|
# searches -- Open Food Facts is used to *fill* a new food's macros, never to
|
|
# search. Local-only, git-ignored.
|
|
FOOD_BANK_FILE: Path = DATA_DIR / "food_bank.json"
|
|
# Hand-curated bank entries (see _foodbank_manual.py): foods added without
|
|
# ever logging them, so they are NOT derivable from the food log and must
|
|
# sync in their own right. Kept separate from FOOD_BANK_FILE because that
|
|
# one is rewritten wholesale on every log write.
|
|
MANUAL_BANK_FILE: Path = DATA_DIR / "food_bank_manual.json"
|
|
# The budget: a plain JSON dotfile alongside the log, freely editable on this
|
|
# device or the phone app and synced between them (see _sync.py).
|
|
# Git-ignored, never committed. "Hidden" here means never-online (it lives
|
|
# outside the repo) -- the number itself is shown freely in local CLI/GUI/app
|
|
# output.
|
|
BUDGET_FILE: Path = DATA_DIR / ".budget"
|
|
# The effective-from history of that budget (see _budget_history.py), kept in
|
|
# its own file so ``.budget``'s schema stays at v2 and every existing reader
|
|
# of it is untouched. Classifying a *past* day reads this; everything about
|
|
# *today* still reads BUDGET_FILE. Git-ignored, never committed.
|
|
BUDGET_HISTORY_FILE: Path = DATA_DIR / ".budget_history"
|
|
|
|
# --- Estimator (Open Food Facts) -------------------------------------------
|
|
# The default backend is Open Food Facts' "Search-a-licious" full-text search:
|
|
# free, no key, strongest for branded/packaged foods (including fast food).
|
|
# (The older cgi/search.pl endpoint is heavily rate-limited and returns an HTML
|
|
# "temporarily unavailable" page to API clients, and /api/v2/search ignores the
|
|
# query term, so neither is usable here.) Swappable for a local/remote LLM
|
|
# backend later without touching the log or CLI layers.
|
|
OFF_SEARCH_URL: str = "https://search.openfoodfacts.org/search"
|
|
OFF_TIMEOUT_SECONDS: float = 8.0
|
|
OFF_PAGE_SIZE: int = 5
|
|
# Open Food Facts asks API clients to identify themselves with a descriptive
|
|
# User-Agent string so abusive clients can be told apart from polite ones.
|
|
OFF_USER_AGENT: str = "diet_guard/1.0 (personal diet tracker)"
|
|
# Portion assumed when neither --grams nor an OFF serving size is available.
|
|
DEFAULT_PORTION_GRAMS: float = 100.0
|
|
|
|
# --- Gate (log-to-unlock) ---------------------------------------------------
|
|
# The gate is driven by FIXED MEAL SLOTS, not by a gap timer. Starting at the
|
|
# day-start hour, a slot opens every interval; once a slot's hour has passed,
|
|
# that slot must carry a logged meal or the screen locks until it does. This
|
|
# makes tracking fully automatic (you are prompted on a schedule rather than
|
|
# trusted to log voluntarily) and nudges regular eating. Coming home late
|
|
# naturally produces several unlogged elapsed slots at once -> one lock that
|
|
# backfills the whole day, which is the "requirement to access the PC" behavior.
|
|
GATE_DAY_START_HOUR: int = 8 # first slot (08:00); also the "beginning of day"
|
|
GATE_SLOT_INTERVAL_HOURS: int = 4 # slots at 08:00, 12:00, 16:00, 20:00
|
|
# Past this hour the gate never fires, so an unlogged late slot lapses quietly
|
|
# instead of locking you out overnight. (A new day resets all slots at 00:00.)
|
|
GATE_EATING_END_HOUR: int = 22 # exclusive (22:00)
|
|
# flock single-instance guard: stops a timer from stacking lock windows.
|
|
GATE_LOCK_FILE: Path = DATA_DIR / ".gate.lock"
|
|
|
|
# --- Sync (cross-device log merge) ------------------------------------------
|
|
# GitHub is used purely as dumb file storage via the REST Contents API (not a
|
|
# git clone) -- mirrors ~/todo's sync transport. Each device pushes its own
|
|
# full current log as one file under devices/<id>/food_log.json; merging
|
|
# happens client-side (see _sync_merge.py), never via git.
|
|
SYNC_REPO_OWNER: str = "kuhyx"
|
|
SYNC_REPO_NAME: str = "syncs"
|
|
SYNC_DEVICE_ID: str = "pc"
|
|
# A fine-grained GitHub PAT, scoped to just SYNC_REPO_NAME's contents. The
|
|
# user creates this once via github.com (see CLAUDE.md) and saves it here,
|
|
# mode 600. Never committed -- this path is outside the repo entirely.
|
|
SYNC_TOKEN_FILE: Path = Path.home() / ".config" / "diet_guard" / "sync_token"
|
|
SYNC_TIMEOUT_SECONDS: float = 10.0
|