mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-19 05:24:20 +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
123 lines
6.3 KiB
Bash
Executable File
123 lines
6.3 KiB
Bash
Executable File
#!/bin/bash
|
|
# ============================================================================
|
|
# Diet Guard installer: log-to-unlock gate with a freely-editable budget.
|
|
#
|
|
# Usage: bash install.sh
|
|
#
|
|
# What it does:
|
|
# 1. Ensures system deps (setxkbmap for VT-disable, requests for OFF lookups)
|
|
# 2. pip-installs this package + gatelock into system Python's user
|
|
# site-packages (the systemd service runs /usr/bin/python directly, not
|
|
# a venv, so the package must live where that interpreter can find it —
|
|
# see CLAUDE.md's "Production dependency installation" section)
|
|
# 3. Installs + enables the systemd user timer that fires the gate every ~15m
|
|
# 4. Installs + enables the systemd user timer that syncs the log every ~15m
|
|
# (the sync itself stays unconfigured -- and a no-op -- until you create
|
|
# a sync token; see the reminder this step prints)
|
|
# 5. Sets your initial daily budget from biometrics (only if not already set;
|
|
# it stays freely editable afterward from the gate or the phone app)
|
|
# ============================================================================
|
|
|
|
set -euo pipefail
|
|
|
|
# Split declare/assign so the command-substitution exit code is not masked (SC2155).
|
|
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
|
|
readonly SCRIPT_DIR
|
|
readonly REPO_DIR="$SCRIPT_DIR"
|
|
readonly SERVICE_SRC="$SCRIPT_DIR/diet-guard-gate.service"
|
|
readonly TIMER_SRC="$SCRIPT_DIR/diet-guard-gate.timer"
|
|
readonly SYNC_SERVICE_SRC="$SCRIPT_DIR/diet-guard-sync.service"
|
|
readonly CLAUDE_SERVICE_SRC="$SCRIPT_DIR/diet-guard-claude.service"
|
|
# The relay worker's own Firebase session (never the gate's: two processes
|
|
# refreshing one token file corrupt it). Seeded once, see the step below.
|
|
readonly CLAUDE_SESSION_FILE="$HOME/.config/diet_guard_claude/firebase_auth.json"
|
|
readonly SYSTEMD_USER_DIR="$HOME/.config/systemd/user"
|
|
readonly DATA_DIR="$HOME/.local/share/diet_guard"
|
|
readonly BUDGET_FILE="$DATA_DIR/.budget"
|
|
readonly SYNC_TOKEN_FILE="$HOME/.config/diet_guard/sync_token"
|
|
|
|
echo "=== Diet Guard Installer ==="
|
|
|
|
# 1. System dependencies ------------------------------------------------------
|
|
echo "[1/5] Checking system dependencies..."
|
|
if ! command -v setxkbmap &>/dev/null; then
|
|
echo " Installing xorg-setxkbmap (gate disables VT switching while locked)..."
|
|
sudo pacman -S --noconfirm xorg-setxkbmap
|
|
else
|
|
echo " setxkbmap present"
|
|
fi
|
|
|
|
# 2. Install this package + its dependencies into system Python -------------
|
|
echo "[2/5] Installing diet_guard + dependencies for /usr/bin/python..."
|
|
/usr/bin/python3 -m pip install --user --break-system-packages -e "$REPO_DIR"
|
|
echo " Installed. Verifying import..."
|
|
/usr/bin/python3 -c "import diet_guard; import gatelock; import claude_agent_sdk, rtdb_relay, httpx" \
|
|
&& echo " diet_guard, gatelock and the Claude relay deps import cleanly from the system interpreter."
|
|
|
|
# 3. systemd user timer + service (gate) -------------------------------------
|
|
echo "[3/5] Installing the gate's systemd user timer + service..."
|
|
mkdir -p "$SYSTEMD_USER_DIR"
|
|
cp "$SERVICE_SRC" "$SYSTEMD_USER_DIR/diet-guard-gate.service"
|
|
cp "$TIMER_SRC" "$SYSTEMD_USER_DIR/diet-guard-gate.timer"
|
|
systemctl --user daemon-reload
|
|
systemctl --user enable --now diet-guard-gate.timer
|
|
echo " Timer enabled and started (fires the gate every ~15 min)."
|
|
|
|
# 4. Retire the old periodic sync timer ---------------------------------------
|
|
# Sync is event-driven now (before a lock, after a meal is logged -- see
|
|
# diet_guard/_sync_events.py), so the ~15 min timer is gone. Removing the unit
|
|
# from the repo does NOT stop an already-installed copy: without this block it
|
|
# would keep firing forever from $SYSTEMD_USER_DIR with no unit file in git to
|
|
# explain it. `|| true` because a fresh machine has no such unit and `disable`
|
|
# exits non-zero there, which would abort the install under `set -euo pipefail`.
|
|
echo "[4/5] Removing the retired periodic sync timer (sync is event-driven)..."
|
|
systemctl --user disable --now diet-guard-sync.timer 2>/dev/null || true
|
|
rm -f "$SYSTEMD_USER_DIR/diet-guard-sync.timer"
|
|
# The *service* stays: it is how a manual one-off tick is run
|
|
# (`systemctl --user start diet-guard-sync`). Only the timer is retired.
|
|
cp "$SYNC_SERVICE_SRC" "$SYSTEMD_USER_DIR/diet-guard-sync.service"
|
|
systemctl --user daemon-reload
|
|
echo " Periodic sync timer removed; 'python -m diet_guard sync' still works."
|
|
if [[ -e "$SYNC_TOKEN_FILE" ]]; then
|
|
echo " Sync token already present at $SYNC_TOKEN_FILE."
|
|
else
|
|
echo " No sync token yet at $SYNC_TOKEN_FILE -- sync will no-op (and log a"
|
|
echo " failure) after every meal you log, until you create a GitHub PAT"
|
|
echo " scoped to the diet-guard-sync repo's contents and save it there,"
|
|
echo " mode 600: chmod 600 \"$SYNC_TOKEN_FILE\""
|
|
fi
|
|
|
|
# 4b. The "send to Claude" relay worker ------------------------------------
|
|
# Serves the phone's meal requests (docs/DOCS-claude-relay.md). It needs its own
|
|
# signed-in Firebase session, which only one Google consent can create, so the
|
|
# unit is installed always but started only once the session file exists.
|
|
echo "[4b/5] Installing the Claude relay worker unit..."
|
|
cp "$CLAUDE_SERVICE_SRC" "$SYSTEMD_USER_DIR/diet-guard-claude.service"
|
|
systemctl --user daemon-reload
|
|
if [[ -f "$CLAUDE_SESSION_FILE" ]]; then
|
|
systemctl --user enable --now diet-guard-claude.service
|
|
echo " Relay worker enabled and started."
|
|
else
|
|
echo " Not started: no session at $CLAUDE_SESSION_FILE. Seed it once (one"
|
|
echo " Google consent) in ~/src/utils/crdt-sync:"
|
|
echo " python3 -m tool.seed_session --client-id ... --client-secret ... --app diet_guard_claude"
|
|
echo " then re-run this installer."
|
|
fi
|
|
|
|
# 5. Set the initial daily budget ---------------------------------------------
|
|
echo "[5/5] Setting your initial daily budget..."
|
|
if [[ -e "$BUDGET_FILE" ]]; then
|
|
echo " Budget already set at $BUDGET_FILE - skipping init."
|
|
echo " Edit it any time from the gate's calendar tab or the phone app."
|
|
else
|
|
echo " Enter your biometrics (used once then discarded to compute a"
|
|
echo " starting value; you can change the number itself any time from"
|
|
echo " the gate's calendar tab or the phone app):"
|
|
python -m diet_guard init
|
|
fi
|
|
|
|
echo "=== Installation complete ==="
|
|
echo "The gate checks every ~15 min and locks until you log a meal once a meal"
|
|
echo "slot passes unlogged (08:00/12:00/16:00/20:00 by default; editable)."
|
|
echo "Test the lock now (safe, closeable): python -m diet_guard gate --demo"
|