mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-20 00:34:22 +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
99 lines
3.9 KiB
Python
99 lines
3.9 KiB
Python
"""Slot choice and the idempotent write of one Claude meal."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime
|
|
from unittest.mock import patch
|
|
|
|
from hypothesis import HealthCheck, given, settings
|
|
from hypothesis import strategies as st
|
|
|
|
from diet_guard._claude_contract import MealRequest, Mode, entry_id_for
|
|
from diet_guard._claude_log import already_logged, log_estimate, resolve_slot
|
|
from diet_guard._claude_schema import Estimate
|
|
from diet_guard._slot_wire import entry_slot_minute
|
|
from diet_guard._state import load_log
|
|
|
|
EST = Estimate("2 jajka", 190.0, 410.0, 17.5, 22.0, 26.0, None)
|
|
NOON = datetime(2026, 11, 17, 12, 30, tzinfo=UTC)
|
|
|
|
|
|
def _request(mode: Mode = "due", slot: int = 480, text: str = "x") -> MealRequest:
|
|
return MealRequest(text, None, slot, mode)
|
|
|
|
|
|
def test_due_mode_takes_the_oldest_due_slot_and_ignores_the_hint() -> None:
|
|
"""Two slots due: the older one, whatever the phone says."""
|
|
with patch("diet_guard._claude_log.due_slots", return_value=(480, 720)):
|
|
assert resolve_slot(_request("due", slot=1200), NOON) == 480
|
|
|
|
|
|
def test_due_mode_with_nothing_due_falls_back_to_slot_for_log() -> None:
|
|
"""Nothing due is not an error: the ordinary now-rule decides."""
|
|
with patch("diet_guard._claude_log.due_slots", return_value=()):
|
|
assert resolve_slot(_request("due", slot=1200), NOON) == 720
|
|
|
|
|
|
def test_normal_mode_snaps_the_hint_to_the_nearest_slot() -> None:
|
|
"""A hint of 11:50 lands on the 12:00 slot, ignoring the due list."""
|
|
with patch("diet_guard._claude_log.due_slots", return_value=(480,)) as due:
|
|
assert resolve_slot(_request("normal", slot=710), NOON) == 720
|
|
due.assert_not_called()
|
|
|
|
|
|
def test_log_estimate_writes_one_signed_claude_entry() -> None:
|
|
"""The entry carries the derived id, the estimate, the slot and the source."""
|
|
with patch("diet_guard._claude_log.due_slots", return_value=(720,)):
|
|
data = log_estimate("RID1", EST, _request(), NOON)
|
|
assert data["entry_id"] == entry_id_for("RID1")
|
|
assert (data["kcal"], data["slot_min"], data["desc"]) == (410.0, 720, "2 jajka")
|
|
(entry,) = next(iter(load_log().values()))
|
|
assert entry["source"] == "claude"
|
|
assert entry_slot_minute(entry) == 720
|
|
assert "components" not in entry
|
|
|
|
|
|
def test_components_are_carried_onto_the_entry() -> None:
|
|
"""A composite estimate keeps its per-item numbers on the entry."""
|
|
part = {"name": "jajko", "grams": 60.0, "kcal": 90.0, "protein_g": 6.0}
|
|
est = Estimate(
|
|
"x", 60.0, 90.0, 6.0, 0.5, 7.0, ({**part, "carbs_g": 0.5, "fat_g": 7.0},)
|
|
)
|
|
log_estimate("RID2", est, _request("normal"), NOON)
|
|
(entry,) = next(iter(load_log().values()))
|
|
assert [c["name"] for c in entry["components"]] == ["jajko"]
|
|
|
|
|
|
def test_replay_returns_the_existing_entry_without_logging_again() -> None:
|
|
"""The same rid twice: one entry, the second call echoes the first."""
|
|
first = log_estimate("RID3", EST, _request("normal"), NOON)
|
|
second = log_estimate("RID3", EST, _request("normal"), NOON)
|
|
assert second == first
|
|
assert sum(len(day) for day in load_log().values()) == 1
|
|
assert already_logged("RID3") == first
|
|
assert already_logged("never") is None
|
|
|
|
|
|
def test_property_each_rid_yields_exactly_one_entry() -> None:
|
|
"""Any replay order of any rids: one entry per distinct rid, never more.
|
|
|
|
The log persists across hypothesis examples (one test, one tmp dir), so
|
|
the expectation accumulates the rids seen so far.
|
|
"""
|
|
seen: set[str] = set()
|
|
|
|
@settings(
|
|
max_examples=40,
|
|
deadline=None,
|
|
suppress_health_check=[HealthCheck.function_scoped_fixture],
|
|
)
|
|
@given(rids=st.lists(st.sampled_from("ABCDEF"), min_size=1, max_size=12))
|
|
def replay(rids: list[str]) -> None:
|
|
for rid in rids:
|
|
log_estimate(rid, EST, _request("normal"), NOON)
|
|
seen.update(rids)
|
|
ids = {str(e["id"]) for day in load_log().values() for e in day}
|
|
assert ids == {entry_id_for(rid) for rid in seen}
|
|
|
|
replay()
|