mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-19 07:14:33 +01:00
python -m diet_guard claude-worker serves the phone's meal requests from RTDB (rtdb_relay RequestServer, one SSE stream, reconnects with backoff): validate, estimate with Claude, log through the signed log with source=claude under the oldest due slot (normal mode: nearest slot of the phone's hint), write the sealed result, delete only the request payload, then publish in the background. diet-guard-claude.service (Restart=on-failure, WantedBy=default.target) is installed by install.sh and started once the worker's own Firebase session exists. Nothing is deployed by this commit. Failure paths: - narrow peer pull fails (sync locked/unreachable): fail closed, no claim, no answer, no model call; the request stays pending, retried every 30 s - e2e key locked, no passphrase, no session: fail closed, nothing read or written; the worker waits and retries every 60 s - bad body, request older than 24 h (or no created): fail closed, an error result, nothing logged - Claude failure/timeout/invalid JSON: fail closed, an error result with retry, nothing logged - crash between log and result, or a duplicate event: idempotent, the entry id is uuid5 of the rid and is looked up (tombstones included) before the model call - request payload delete, old-answer cleanup and the post-answer publish failing: fail open on purpose (logged only); the answer and the local entry are already durable - due mode with nothing due: falls back to slot_for_log, not an error New log helpers log_meal_as / find_entry; log_meal's body is split into _entry_fields / _store_entry with the same output. The gate and CLI import none of the worker's dependencies at load time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WzxdWZHWP4KZ6x5TcXDBFC
42 lines
1.4 KiB
Python
42 lines
1.4 KiB
Python
"""``log_meal(entry_id=...)`` and ``find_entry``: what makes a replay find itself."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from diet_guard._estimator import Nutrition
|
|
from diet_guard._state import log_meal
|
|
from diet_guard._state_sync import find_entry, log_meal_as
|
|
|
|
FOOD = Nutrition(kcal=100, protein_g=1, carbs_g=2, fat_g=3, grams=50, source="claude")
|
|
|
|
|
|
def test_given_entry_id_is_used_and_found() -> None:
|
|
"""The id the caller picks is the stored id, and find_entry returns it."""
|
|
entry = log_meal_as("fixed-id", "owsianka", FOOD, 480)
|
|
assert entry["id"] == "fixed-id"
|
|
found = find_entry("fixed-id")
|
|
assert found is not None
|
|
assert found["desc"] == "owsianka"
|
|
|
|
|
|
def test_default_id_is_still_a_fresh_uuid() -> None:
|
|
"""Without entry_id nothing changes: two meals, two distinct ids."""
|
|
first = log_meal("a", FOOD, 480)
|
|
second = log_meal("a", FOOD, 480)
|
|
assert first["id"] != second["id"]
|
|
|
|
|
|
def test_find_entry_sees_a_tombstoned_entry() -> None:
|
|
"""A deleted entry still counts as logged: a replay must not re-log it."""
|
|
log_meal_as("gone", "a", FOOD, 480)
|
|
from diet_guard._state_sync import undo_last_today
|
|
|
|
assert undo_last_today() is not None
|
|
found = find_entry("gone")
|
|
assert found is not None
|
|
assert found["deleted"]
|
|
|
|
|
|
def test_find_entry_unknown_is_none() -> None:
|
|
"""No such id: None, no exception."""
|
|
assert find_entry("never-logged") is None
|