diet-guard/app/lib/services/sync_merge_schedule.dart
Krzysztof kuhy Rudnicki 24dbffc432 Sync the meal schedule as sched: fields on the budget record
Without this the schedule would be device-local, which is the failure this
whole feature has to avoid: the phone sets five meals, the PC still derives
four, and the PC's gate nags for a 12:00 checkpoint the phone never offered
and can never satisfy. So sync lands before either edit surface, not after.

The history rides the existing `budget` CRDT record as one
`sched:<YYYY-MM-DD>` field per entry, exactly like the budget's own `hist:`
fields. merge_record is per-field LWW over the *union* of field names and both
devices push the merged record, so a device that predates the feature relays
the fields untouched -- no coordinated release. There is a test for precisely
that: merging our push with a budget-only peer keeps both schedule entries.

Verified the two languages agree on the wire, not just in their own suites --
Python and Dart both emit
  {"sched:1970-01-01":{"f":8,"l":20,"n":4},"sched:2026-08-16":{"f":8,"l":20,"n":5}}
The Hlc is derived from the *parsed* timestamp on both sides, which matters
because the epoch fallback formats differently in each language
(1970-01-01T00:00:00+00:00 vs ...000Z) and a string comparison would diverge.

Two guards worth naming:

- A device with no history contributes no `sched:` fields at all, so an unset
  default can never outrank a peer's real schedule; and an empty merge result
  is never written back, so a pre-feature peer cannot erase a real history.
- Dart's `_syncBudget` synthesises a local record when the schedule has been
  edited but the budget never has -- `budgetToLog(null, ...)` returns an empty
  Log and would otherwise drop the schedule fields too. Its budget value
  carries an epoch stamp, so it loses every LWW race against a real edit.

sync_merge/_schedule.py and sync_merge_schedule.dart are separate modules
rather than additions to the budget adapters, which were at 186 and 170 lines
against the 250 cap. Each declares its own record-id constant so the Dart
pair does not import each other.

772 Python tests at 100% branch coverage, 619 Dart.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0159b4E5hh512jYUeaJVNxfM
2026-08-16 14:49:08 +02:00

99 lines
3.9 KiB
Dart

/// Meal-schedule history as extra fields on the shared `budget` record.
///
/// Split out of `sync_merge_budget.dart` to keep both under the repo's
/// 250-line limit; the two halves are otherwise the same idea and share one
/// record.
///
/// The schedule rides as one `sched:<YYYY-MM-DD>` field per history entry,
/// exactly like the budget's `hist:` fields. `mergeRecord` is per-field
/// last-writer-wins over the *union* of field names, and both devices push
/// the *merged* record rather than their own, so a device that predates meal
/// schedules neither clobbers those fields nor blocks them -- it relays them
/// untouched. That is what makes this shippable without a coordinated
/// release.
///
/// KEEP IN SYNC WITH `diet_guard/sync_merge/_schedule.py`.
library;
import 'package:crdt_sync/crdt_sync.dart';
import 'package:diet_guard_app/models/meal_schedule.dart';
import 'package:diet_guard_app/services/meal_schedule_history.dart';
import 'package:diet_guard_app/services/sync_device_id.dart';
/// The shared record these fields live on. Declared here rather than imported
/// from `sync_merge_budget.dart` so the two modules do not import each other;
/// `_schedule.py` declares its own copy for the same reason.
const _budgetRecordId = 'budget';
/// Field-name prefix for the effective-from meal-schedule history.
const scheduleFieldPrefix = 'sched:';
/// Derives a deterministic [Hlc] for one schedule entry from its edit time.
///
/// Identical inputs always yield the same clock, so re-syncing an unchanged
/// history is a no-op. Derived from the *parsed* timestamp rather than the
/// raw string, so the two languages agree even though they format the epoch
/// fallback differently.
Hlc scheduleHlc(ScheduleEntry entry) {
final wallTimeMs =
DateTime.tryParse(entry.editedAt)?.toUtc().millisecondsSinceEpoch ?? 0;
return Hlc.newTick(currentSyncDeviceId, wallTimeMsOverride: wallTimeMs);
}
/// Returns the `sched:` fields contributed by [entries].
///
/// Empty when this device has no history, so a device that has never edited
/// a schedule contributes nothing to the merge rather than pushing the unset
/// default over a peer's real value.
Map<String, (dynamic, Hlc)> scheduleFields(List<ScheduleEntry> entries) => {
for (final entry in entries)
'$scheduleFieldPrefix${entry.effectiveFrom}': (
{
'f': entry.schedule.first,
'l': entry.schedule.last,
'n': entry.schedule.count,
},
scheduleHlc(entry),
),
};
/// Extracts the meal-schedule history from a merged budget [Log].
///
/// Each entry's `editedAt` is reconstructed from its field Hlc rather than
/// carried separately, so the stored timestamp and the clock the merge
/// compared can never drift apart. Malformed values are skipped, so one bad
/// field from a peer cannot take out the whole history.
List<ScheduleEntry> logToScheduleHistory(Log log) {
final record = log[_budgetRecordId];
if (record == null) return const [];
final entries = <ScheduleEntry>[];
for (final field in record.fields.entries) {
if (!field.key.startsWith(scheduleFieldPrefix)) continue;
final value = field.value.$1;
if (value is! Map) continue;
final first = value['f'];
final last = value['l'];
final count = value['n'];
if (first is! int || last is! int || count is! int) continue;
entries.add(
ScheduleEntry(
effectiveFrom: field.key.substring(scheduleFieldPrefix.length),
// Normalised on the way in, so a peer running a future version with a
// wider range cannot hand us a schedule we would derive slots
// differently from.
schedule: MealSchedule(
first: first,
last: last,
count: count,
).normalized(),
editedAt: DateTime.fromMillisecondsSinceEpoch(
field.value.$2.wallTimeMs,
isUtc: true,
).toLocal().toIso8601String(),
),
);
}
entries.sort((a, b) => a.effectiveFrom.compareTo(b.effectiveFrom));
return entries;
}