From 5add5fa55f754ea87949868dbab88717854e4426 Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Wed, 18 Nov 2026 22:32:07 +0100 Subject: [PATCH] Report sync failures; dev extra; deploy script The phone showed "Not syncing" with no way to learn why: a release build keeps no log. attemptSync now records the outcome and files a sync.failed report to the board with the exception. A refresh error after a landed sync no longer marks it failed. Also: hypothesis in pyproject's dev extra; gatelock 0.16.0, claude-agent-sdk 0.2.184 and stale app pins bumped; scripts/deploy.sh as the CLAUDE.md deploy command. Co-Authored-By: Claude Opus 5.5 --- .pre-commit-config.yaml | 4 +- AGENTS.md | 7 +-- app/lib/screens/log_meal_sync_mixin.dart | 28 +++++---- app/lib/services/background_sync_service.dart | 28 ++++----- app/lib/services/sync_attempt.dart | 42 ++++++++++++++ app/pubspec.lock | 28 ++++----- app/pubspec.yaml | 8 +-- app/test/services/sync_attempt_test.dart | 58 +++++++++++++++++++ pyproject.toml | 7 ++- requirements.txt | 4 +- scripts/deploy.sh | 37 ++++++++++++ 11 files changed, 195 insertions(+), 56 deletions(-) create mode 100644 app/lib/services/sync_attempt.dart create mode 100644 app/test/services/sync_attempt_test.dart create mode 100755 scripts/deploy.sh diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index dab495e..c377f2c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -111,12 +111,12 @@ repos: # direct URLs for one distribution, so any pin that drifts from # the manifest becomes an unresolvable hook environment rather # than merely an old one. - - gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.15.1#subdirectory=gatelock + - gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.16.0#subdirectory=gatelock - crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.11.1#subdirectory=crdt-sync - freedays @ git+https://github.com/kuhyx/utils@freedays-v0.4.3#subdirectory=freedays - sickdays @ git+https://github.com/kuhyx/utils@sickdays-v0.5.5#subdirectory=sickdays - rtdb-relay @ git+https://github.com/kuhyx/utils@rtdb-relay-v0.2.0#subdirectory=rtdb_relay - - claude-agent-sdk==0.2.183 + - claude-agent-sdk==0.2.184 - httpx==0.28.1 - mcp>=2,<3 - pydantic diff --git a/AGENTS.md b/AGENTS.md index 250a0b3..b4830fd 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -22,6 +22,7 @@ show adherence, streaks, a YTD tally and weekly/monthly averages, all derived fr - test-changed: `scripts/test_changed.sh` (maps py and app/ dart changes to their tests, no coverage) - lint: `pre-commit run --all-files` - coverage: `python -m pytest -n auto -p no:sugar -q` (writes coverage.lcov) +- deploy: `bash scripts/deploy.sh` (pull ~/src/diet-guard, system-Python install, desktop app; health-checked) - coverage-gaps: `coverage-gaps coverage.lcov` ## Scheduling @@ -238,10 +239,8 @@ only); the CLI, gate and phone show both. ## Flutter/Dart AI rules -_Vendored from [flutter/flutter -docs/rules/rules.md](https://github.com/flutter/flutter/blob/main/docs/rules/rules.md) via -`~/.claude/CLAUDE.md` Flutter AI tooling setup. Re-fetch periodically. Split across five files to -stay under the 250-line cap._ +_Vendored from [flutter/flutter docs/rules/rules.md](https://github.com/flutter/flutter/blob/main/docs/rules/rules.md) +via `~/.claude/CLAUDE.md`. Re-fetch periodically. Split across five files for the 250-line cap._ - [Language, style and architecture](docs/DOCS-flutter-rules-language.md) - [Architecture, lint rules, state and data](docs/DOCS-flutter-rules-state-and-data.md) diff --git a/app/lib/screens/log_meal_sync_mixin.dart b/app/lib/screens/log_meal_sync_mixin.dart index 00f0a15..1465f9d 100644 --- a/app/lib/screens/log_meal_sync_mixin.dart +++ b/app/lib/screens/log_meal_sync_mixin.dart @@ -8,6 +8,7 @@ import 'package:diet_guard_app/services/foodbank_service.dart'; import 'package:diet_guard_app/services/github_client_factory.dart'; import 'package:diet_guard_app/services/health_steps.dart'; import 'package:diet_guard_app/services/log_storage_service.dart'; +import 'package:diet_guard_app/services/sync_attempt.dart'; import 'package:diet_guard_app/services/sync_health.dart'; import 'package:diet_guard_app/services/sync_service.dart'; import 'package:diet_guard_app/services/sync_settings.dart'; @@ -68,6 +69,7 @@ mixin LogMealSyncMixin on State Future autoSync() async { if (autoSyncing) return; autoSyncing = true; + bool? landed; // null until the sync itself ran try { final settings = await SyncSettings.load(); // Either backend counts. `isConfigured` means "has a GitHub token", so @@ -87,17 +89,19 @@ mixin LogMealSyncMixin on State return; } final client = createGitHubClient(settings, httpClient: syncHttpClient); - try { - // Foreground only (this mixin is UI), and never prompts: fresh - // Health Connect steps ride along in this tick's body.json. - await refreshSteps(); - await runSync(await syncBackend(client)); - await SyncHealth.recordSuccess(); - } finally { - client.close(); - } + landed = await attemptSync(() async { + try { + // Foreground only (this mixin is UI), and never prompts: fresh + // Health Connect steps ride along in this tick's body.json. + await refreshSteps(); + await runSync(await syncBackend(client)); + } finally { + client.close(); + } + }); if (!mounted) return; - await refreshSlots(); + // Outside attemptSync: a refresh error is no sync failure. + if (landed) await refreshSlots(); await refreshSyncHealth(); } on Object catch (error, stackTrace) { // Best-effort, but never silent: this swallowed the reason a desktop @@ -110,7 +114,9 @@ mixin LogMealSyncMixin on State error: error, stackTrace: stackTrace, ); - await SyncHealth.recordFailure(); + // attemptSync already recorded the sync's own outcome; only a failure + // before it (loading settings) is one here. + if (landed == null) await SyncHealth.recordFailure(); if (mounted) await refreshSyncHealth(); } finally { autoSyncing = false; diff --git a/app/lib/services/background_sync_service.dart b/app/lib/services/background_sync_service.dart index 50b445f..e042ff3 100644 --- a/app/lib/services/background_sync_service.dart +++ b/app/lib/services/background_sync_service.dart @@ -15,6 +15,7 @@ import 'package:diet_guard_app/services/github_client_factory.dart'; import 'package:diet_guard_app/services/kuchnia_credential_service.dart'; import 'package:diet_guard_app/services/log_storage_service.dart'; import 'package:diet_guard_app/services/meal_schedule_service.dart'; +import 'package:diet_guard_app/services/sync_attempt.dart'; import 'package:diet_guard_app/services/sync_device_id.dart'; import 'package:diet_guard_app/services/sync_health.dart'; import 'package:diet_guard_app/services/sync_service.dart'; @@ -90,21 +91,14 @@ Future backgroundSyncPush({http.Client? httpClient}) async { return true; // nothing to push; don't retry } final client = createGitHubClient(settings, httpClient: httpClient); - try { - await runSync(await syncBackend(client)); - await SyncHealth.recordSuccess(); - return true; - } on Exception catch (error, stackTrace) { - // Never silent: this is the only signal that a background push failed. - log( - 'diet_guard background sync failed', - level: 1000, - error: error, - stackTrace: stackTrace, - ); - await SyncHealth.recordFailure(); - return false; // offline / transient error -> retry with backoff - } finally { - client.close(); - } + // Never silent: attemptSync records the outcome, the only signal that a + // background push failed. False -> WorkManager retries with backoff. + final landed = await attemptSync(() async { + try { + await runSync(await syncBackend(client)); + } finally { + client.close(); + } + }); + return landed; } diff --git a/app/lib/services/sync_attempt.dart b/app/lib/services/sync_attempt.dart new file mode 100644 index 0000000..debc6b7 --- /dev/null +++ b/app/lib/services/sync_attempt.dart @@ -0,0 +1,42 @@ +/// Runs one sync and records its outcome for the "not syncing" banner. +/// +/// The banner said "Not syncing" on a phone whose sync looked fine, and the +/// release build keeps no log that could say why (see `sync_health.dart`). +/// So a failure here also goes to the board through the app-wide reporter: +/// the next one files its own ticket with the exception that caused it. +/// Inside a WorkManager isolate no reporter is installed, so a background +/// failure is only recorded, not filed. +/// +/// Only the sync itself is timed in: screen refreshes after a landed sync +/// run outside, so their errors never mark a working sync as failed. +library; + +import 'dart:developer'; + +import 'package:crdt_sync_flutter/crdt_sync_flutter.dart' show AppErrors; +import 'package:diet_guard_app/services/sync_health.dart'; + +/// Runs [sync]; records success, or failure plus a `sync.failed` report. +/// +/// Returns whether the sync landed. Never throws. +Future attemptSync(Future Function() sync) async { + try { + await sync(); + } on Object catch (error, stackTrace) { + log( + 'diet_guard sync failed', + level: 1000, + error: error, + stackTrace: stackTrace, + ); + AppErrors.instance?.reporter.report( + 'sync.failed', + error, + stack: stackTrace, + ); + await SyncHealth.recordFailure(); + return false; + } + await SyncHealth.recordSuccess(); + return true; +} diff --git a/app/pubspec.lock b/app/pubspec.lock index 45527fb..4f79062 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -5,10 +5,10 @@ packages: dependency: transitive description: name: android_file_picker - sha256: "04c1306eda42c23ecbf449c909f4fa05c5e66fc48cf5f994d8d08b9d45e5396b" + sha256: ec3cf2a2c7a9b2bd56747e85f80efdd994e563049dfb0981a23903acfdd58cb5 url: "https://pub.dev" source: hosted - version: "2.1.5" + version: "2.2.0" archive: dependency: transitive description: @@ -207,11 +207,11 @@ packages: dependency: "direct main" description: path: data_transfer_flutter - ref: "data_transfer_flutter-v0.2.2" - resolved-ref: eef7af4c86837ee82891493587ff3456297e5dea + ref: "data_transfer_flutter-v0.2.3" + resolved-ref: bb2f92f459283a29ad4864cf37704cc5f749cc6d url: "https://github.com/kuhyx/utils" source: git - version: "0.2.2" + version: "0.2.3" dbus: dependency: transitive description: @@ -280,10 +280,10 @@ packages: dependency: transitive description: name: file_picker - sha256: "079dc46b753abbddb7f77cd87e6ed9ecc62902580707514568321bdf8e050a86" + sha256: c3780977700b8962c3b9b6a9aa4e2c988956893d70d4a4fa2d7adf4ec9b9eaee url: "https://pub.dev" source: hosted - version: "13.3.3" + version: "13.4.0" file_picker_darwin: dependency: transitive description: @@ -304,10 +304,10 @@ packages: dependency: transitive description: name: file_picker_platform_interface - sha256: "336e7168306834a2b68e6168ed768e00d3fabeac23154d5793e277ca9f97cf20" + sha256: a2ef71c4628f92a4471d424576e3bf7042b40a2683c9f9a0e3033a7fc7f7763c url: "https://pub.dev" source: hosted - version: "4.2.0" + version: "4.3.0" file_picker_web: dependency: transitive description: @@ -413,10 +413,10 @@ packages: dependency: "direct main" description: name: flutter_local_notifications - sha256: fac349e7361f80591a0cedff03548f807282c9faee9fc6192763166e5f81def2 + sha256: "7d37d69ded3028af4f22531a0097a3baa478a384dff2e23e873707c93df3b704" url: "https://pub.dev" source: hosted - version: "23.0.2" + version: "23.0.3" flutter_local_notifications_linux: dependency: transitive description: @@ -1067,11 +1067,11 @@ packages: dependency: "direct main" description: path: sync_settings_ui - ref: "sync_settings_ui-v0.8.3" - resolved-ref: dde5566b2c6eb38a8e1e58a4252925f3baca8ad1 + ref: "sync_settings_ui-v0.8.4" + resolved-ref: "15ef5b44f186cea0ab163cc88cb8743c51d58247" url: "https://github.com/kuhyx/utils" source: git - version: "0.8.3" + version: "0.8.4" synchronized: dependency: transitive description: diff --git a/app/pubspec.yaml b/app/pubspec.yaml index 0930102..301d2a9 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -2,7 +2,7 @@ name: diet_guard_app description: "Companion phone app for diet_guard: log meals on the go." publish_to: 'none' -version: 1.0.180+1 +version: 1.0.181+1 environment: sdk: ^3.12.2 @@ -27,12 +27,12 @@ dependencies: data_transfer_flutter: git: url: https://github.com/kuhyx/utils - ref: data_transfer_flutter-v0.2.2 + ref: data_transfer_flutter-v0.2.3 path: data_transfer_flutter fl_chart: 1.3.1 flutter: sdk: flutter - flutter_local_notifications: 23.0.2 + flutter_local_notifications: 23.0.3 flutter_secure_storage: 11.2.0 # One-tap sign-in for the sync account. App-side only: crdt_sync stays pure @@ -66,7 +66,7 @@ dependencies: sync_settings_ui: git: url: https://github.com/kuhyx/utils - ref: sync_settings_ui-v0.8.3 + ref: sync_settings_ui-v0.8.4 path: sync_settings_ui url_launcher: 6.3.3 uuid: 4.6.0 diff --git a/app/test/services/sync_attempt_test.dart b/app/test/services/sync_attempt_test.dart new file mode 100644 index 0000000..156c24f --- /dev/null +++ b/app/test/services/sync_attempt_test.dart @@ -0,0 +1,58 @@ +import 'package:crdt_sync_flutter/crdt_sync_flutter.dart' + show AppErrors, AppErrorsConfig, ErrorQueue; +import 'package:diet_guard_app/services/sync_attempt.dart'; +import 'package:diet_guard_app/services/sync_health.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +const _config = AppErrorsConfig( + app: 'diet-guard', + repo: 'kuhyx/diet-guard', + package: 'diet_guard_app', +); + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + late ErrorQueue queue; + + setUp(() async { + SharedPreferences.setMockInitialValues({}); + queue = await ErrorQueue.open(await SharedPreferences.getInstance()); + AppErrors.instance = AppErrors.forTest(_config, queue); + }); + + tearDown(() => AppErrors.instance = null); + + test('a sync that lands records success and reports nothing', () async { + expect(await attemptSync(() async {}), isTrue); + final status = await SyncHealth.read(); + expect(status.lastSuccess, isNotNull); + expect(status.failureKind, isNull); + expect(queue.entries, isEmpty); + }); + + test('a failed sync records the failure and files its cause', () async { + final ok = await attemptSync(() async => throw StateError('locked')); + expect(ok, isFalse); + final status = await SyncHealth.read(); + expect(status.failureKind, SyncFailureKind.failed); + final entry = queue.entries.single; + expect(entry.kind, 'sync.failed'); + expect(entry.error, contains('locked')); + }); + + test('without installed reporting a failure is still recorded', () async { + AppErrors.instance = null; + expect(await attemptSync(() async => throw StateError('x')), isFalse); + expect((await SyncHealth.read()).failureKind, SyncFailureKind.failed); + }); + + test('a later success clears the failure (the banner goes away)', () async { + await attemptSync(() async => throw StateError('offline')); + await attemptSync(() async {}); + final status = await SyncHealth.read(); + expect(status.failureKind, isNull); + expect(status.message, isNull); + }); +} diff --git a/pyproject.toml b/pyproject.toml index 901f6b5..cc06847 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,12 +4,12 @@ version = "1.0.0" description = "Tkinter/systemd log-to-unlock meal gate with calorie/macro budget tracking" requires-python = ">=3.14" dependencies = [ - "gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.15.1#subdirectory=gatelock", + "gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.16.0#subdirectory=gatelock", "crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.11.1#subdirectory=crdt-sync", "freedays @ git+https://github.com/kuhyx/utils@freedays-v0.4.3#subdirectory=freedays", "holds @ git+https://github.com/kuhyx/utils@holds-v0.1.0#subdirectory=holds", "sickdays @ git+https://github.com/kuhyx/utils@sickdays-v0.5.5#subdirectory=sickdays", - "claude-agent-sdk==0.2.183", + "claude-agent-sdk==0.2.184", "defusedxml==0.7.1", "httpx==0.28.1", "requests==2.34.2", @@ -23,6 +23,9 @@ dependencies = [ # to import the MCP SDK. [project.optional-dependencies] mcp = ["mcp==2.3.0"] +# Test-only: the property tests (test_claude_log.py) need it; same pin as +# requirements.txt. +dev = ["hypothesis==6.170.15"] # Without this, setuptools' flat-layout auto-discovery treats the Flutter # app/ directory as a second top-level package and refuses to build. diff --git a/requirements.txt b/requirements.txt index b9051d5..1d073ee 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,13 +1,13 @@ # Diet Guard — runtime + development dependencies # Runtime: tkinter/json/hmac/fcntl (stdlib) plus gatelock and requests below. bandit==1.9.4 -claude-agent-sdk==0.2.183 +claude-agent-sdk==0.2.184 codespell==2.4.3 coverage==7.17.3 crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.11.1#subdirectory=crdt-sync defusedxml==0.7.1 freedays @ git+https://github.com/kuhyx/utils@freedays-v0.4.3#subdirectory=freedays -gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.15.1#subdirectory=gatelock +gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.16.0#subdirectory=gatelock holds @ git+https://github.com/kuhyx/utils@holds-v0.1.0#subdirectory=holds httpx==0.28.1 hypothesis==6.170.15 diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100755 index 0000000..24b14f7 --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,37 @@ +#!/bin/bash +# ============================================================================ +# Deploy main everywhere it runs on this PC, then prove it. +# +# 1. Pull the durable clone the gate timer runs from (editable install). +# 2. Reinstall it into system Python's user site: diet-guard-gate.service +# runs /usr/bin/python, not a venv (CLAUDE.md, the 2026-06-19 outage). +# 3. Rebuild and reinstall the desktop app package (app/install_arch.sh). +# +# The phone APK ships through CI (release-apk.yml) and phone_deploy.sh. +# Idempotent; exits non-zero unless every health check passes. +# ============================================================================ + +set -euo pipefail + +readonly REPO="$HOME/src/diet-guard" +readonly PY=/usr/bin/python3 + +fail() { + echo "deploy: $*" >&2 + exit 1 +} + +git -C "$REPO" pull --ff-only +"$PY" -m pip install --user --break-system-packages -q -e "$REPO" +(cd "$REPO/app" && bash install_arch.sh) + +# Health: the gate's own interpreter imports the code the timer will run... +"$PY" -c 'import diet_guard._cli, diet_guard._errors, diet_guard._gatelock' \ + || fail "system python cannot import the gate" +systemctl --user is-enabled --quiet diet-guard-gate.timer \ + || fail "diet-guard-gate.timer is not enabled" +# ...and the desktop package is the version this checkout builds. +want="$(sed -n 's/^version:[[:space:]]*\([^+]*\).*/\1/p' "$REPO/app/pubspec.yaml")" +have="$(pacman -Q diet-guard-app | awk '{print $2}')" +[[ "$have" == "$want-1" ]] || fail "desktop app is $have, want $want-1" +echo "deploy: ok (gate imports, timer enabled, desktop app $have)"