diet-guard/app/lib/services/sync_service.dart
Krzysztof kuhy Rudnicki e23f7ac3b4
Some checks are pending
pre-commit / pre-commit (push) Waiting to run
Tests / test (3.10) (push) Waiting to run
Tests / test (3.11) (push) Waiting to run
Tests / test (3.12) (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
Push under a persisted per-install uuid, not a fixed role id
diet_guard pushed as "pc" (Python), "phone" (Android) and "desktop" (web).
A role constant collides the moment a second machine takes the same role,
and after a reinstall the new install inherits the previous one's CRDT
identity -- the same reasoning home_inventory already records for choosing a
uuid. Both halves now mint a uuid once and persist it: the PC in
~/.local/share/diet_guard/.device_id, the app under SharedPreferences
'crdt.nodeId' (the key todo and home_inventory already use).

The old constant survives as the legacy id and is passed to sync_log /
syncLog, so devices/<legacy>/ is skipped as this device's own. Without that
every tick would re-download and re-merge this device's own pre-migration
log as though a peer had written it -- idempotent under LWW, but hundreds of
KB, 96 times a day. It drops to None once that path is reclaimed; a test
covers both states, including a negative control proving the skip is the
legacy id's doing.

All four documents move together (food_log, food_bank, food_bank_manual,
budget), and the hist:<date> fields riding on the budget record are
untouched -- merge_record unions field names, so they relay through
unchanged.

The resolver lives in its own module (_device.py / sync_device_id.dart)
because _sync_merge needs it for HLC stamping too and importing _sync there
would be a cycle. Dart resolves it asynchronously at startup, before
LogStorageService.init(); currentSyncDeviceId falls back to the compile-time
role constant until then, so a too-early Hlc stamp gets a valid id rather
than crashing.

conftest redirects the new id file in the same change that introduces it --
without that a test mints a uuid into the real state dir and this machine's
live sync identity is decided by whichever test ran first. Verified by
checking ~/.local/share/diet_guard/ before and after: untouched.

Verified live, not just against mocks: the sync timer ran mid-change and
migrated this machine to f8f46da6. All 358 food_log records and all 100
food_bank records are present under the new path (0 missing vs devices/pc/),
and the new log's HLCs carry only its own id -- confirming it did not
re-merge the old path as a peer.

Python: 687 tests, 100% branch coverage. App: 518 tests, analyzer clean.
Pins bumped to crdt-sync-v0.6.0 / crdt_sync_dart-v0.7.0 (pyproject,
requirements.txt, .pre-commit-config.yaml, pubspec), and the new library is
installed into /usr/bin/python's user site-packages so the production timer
does not ImportError on its next tick.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GZH89WEqAjuJT8WZdqc7AP
2026-08-09 17:18:42 +02:00

153 lines
6.0 KiB
Dart

/// Cross-device log sync orchestration for the diet_guard companion app.
///
/// Pulls every other device's pushed log from GitHub-backed dumb storage via
/// crdt_sync's shared transport ([GitHubClient]/[syncLog]), merges with the
/// local log via crdt_sync's shared CRDT scheme (`sync_merge.dart` adapts
/// [FoodEntry]s to/from [Record]), rebuilds the food bank, and pushes this
/// device's own merged log back up in the new Record-based wire format.
///
/// The daily budget syncs alongside the food log in the same tick (see
/// [_syncBudget]): a sibling `budget.json` per device, merged
/// last-writer-wins by edit time rather than union-of-immutable-entries,
/// since a budget (unlike a food-log entry) can be edited repeatedly.
library;
import 'package:crdt_sync/crdt_sync.dart';
import 'package:diet_guard_app/models/food_entry.dart';
import 'package:diet_guard_app/services/app_settings_service.dart';
import 'package:diet_guard_app/services/budget_history_service.dart';
import 'package:diet_guard_app/services/foodbank_service.dart';
import 'package:diet_guard_app/services/log_storage_service.dart';
import 'package:diet_guard_app/services/sync_device_id.dart';
import 'package:diet_guard_app/services/sync_merge.dart';
import 'package:diet_guard_app/services/sync_state_factory.dart';
const _devicesDir = 'diet-guard-sync/devices';
/// Runs one full sync tick: pull, merge, persist, push.
///
/// Returns the merged log as it now sits on disk locally. Propagates any
/// [GitHubSyncError] from the client for the caller (auto-sync / the manual
/// "Sync now" action) to decide how to report.
Future<DayLog> runSync(RemoteStore client, {SyncStateStore? stateStore}) async {
final logService = LogStorageService.instance;
final local = await logService.readLog();
final mergedLog = await syncLog(
client: client,
deviceId: currentSyncDeviceId,
legacyDeviceId: legacySyncDeviceId,
pathPrefix: _devicesDir,
localLog: dayLogToLog(local),
encode: encodeLogForPush,
decode: parseRemoteLog,
filename: 'food_log.json',
commitMessage: 'diet_guard_app sync',
// Without this every sync re-downloads every peer's whole food log --
// hundreds of KB -- whether or not anything changed, and re-pushes an
// unchanged one. That is the traffic the Firebase free tier's monthly
// budget depends on not happening.
stateStore: stateStore ?? await openSyncStateStore(),
);
final merged = logToDayLog(mergedLog);
await logService.writeLog(merged);
await FoodBankService.instance.rebuildAndPersist(merged);
await _syncBudget(client);
await _syncFoodBank(client);
await _syncManualBank(client);
return merged;
}
/// Pulls, merges, persists and pushes the log-derived food bank.
///
/// Runs after the local rebuild above, so this device's records already
/// reflect the merged log; the merge then unions in whatever another device
/// knows, max-count winning per food. Mirrors `_sync._sync_food_bank`.
Future<void> _syncFoodBank(RemoteStore client) async {
final merged = await syncLog(
client: client,
deviceId: currentSyncDeviceId,
legacyDeviceId: legacySyncDeviceId,
pathPrefix: _devicesDir,
localLog: foodBankToLog(await FoodBankService.instance.readBank()),
encode: encodeFoodBankForPush,
decode: parseRemoteFoodBank,
filename: 'food_bank.json',
commitMessage: 'diet_guard_app sync',
);
await FoodBankService.instance.writeBank(logToFoodBank(merged));
}
/// Pulls, merges, persists and pushes the hand-curated food bank.
///
/// Curated entries are the one part of the bank that is not derivable from
/// the food log, so unlike `food_bank.json` they need a real merge:
/// last-writer-wins per food name by edit time, union across devices. Mirrors
/// `_sync._sync_manual_bank`.
Future<void> _syncManualBank(RemoteStore client) async {
final local = await FoodBankService.instance.readManualBank();
final merged = await syncLog(
client: client,
deviceId: currentSyncDeviceId,
legacyDeviceId: legacySyncDeviceId,
pathPrefix: _devicesDir,
localLog: manualBankToLog(local),
encode: encodeManualBankForPush,
decode: parseRemoteManualBank,
filename: 'food_bank_manual.json',
commitMessage: 'diet_guard_app sync',
);
await FoodBankService.instance.applyMergedManualBank(logToManualBank(merged));
}
/// Pulls other devices' budgets, merges, applies the winner locally, pushes.
///
/// Reuses [syncLog] (it always pushes the merged result, even an empty
/// one -- harmless and consistent with the food-log sync above). This
/// device contributes nothing to the merge until the goal has been
/// explicitly set at least once (see
/// [AppSettingsService.dailyKcalGoalUpdatedAt]), so a fresh install's
/// unset 2200 default can never spuriously outrank a real budget synced
/// from elsewhere.
Future<void> _syncBudget(RemoteStore client) async {
final updatedAt = AppSettingsService.dailyKcalGoalUpdatedAt;
final localRecord = updatedAt == null
? null
: <String, dynamic>{
'v': 2,
'b': AppSettingsService.dailyKcalGoal,
't': updatedAt.toIso8601String(),
};
final mergedBudgetLog = await syncLog(
client: client,
deviceId: currentSyncDeviceId,
legacyDeviceId: legacySyncDeviceId,
pathPrefix: _devicesDir,
localLog: budgetToLog(
localRecord,
BudgetHistoryService.schedule.entries,
),
encode: encodeBudgetForPush,
decode: parseRemoteBudget,
filename: 'budget.json',
commitMessage: 'diet_guard_app sync',
);
// The history merges by field union, so it is applied unconditionally --
// independently of whether the scalar budget resolved to anything.
await BudgetHistoryService.instance.applyMerged(
logToHistory(mergedBudgetLog),
);
final merged = logToBudget(mergedBudgetLog);
final mergedKcal = merged?['b'];
if (merged == null || mergedKcal is! int) return;
await AppSettingsService.instance.applySyncedBudget(
mergedKcal,
updatedAt: DateTime.tryParse(merged['t'] as String? ?? ''),
);
}