diet-guard/app/lib/services/budget_schedule.dart
Krzysztof kuhy Rudnicki f7f06aee53
Some checks failed
pre-commit / pre-commit (push) Has been cancelled
Tests / test (3.10) (push) Has been cancelled
Tests / test (3.11) (push) Has been cancelled
Tests / test (3.12) (push) Has been cancelled
Remove unused features; fix reminder false alarms; share all state
Four asks, plus the fixes two review passes turned up.

Removals (all unused):
- "Repeat last meal" button, and the reward/temptation-bundling prompt it
  was the sole trigger for. Replaced with a post-log progress card showing
  kcal against budget, macros, and the adherence streak.
- The multi-item meal builder, on both the gate and the phone. The stored
  `components` field stays: nothing writes it now, but historical composite
  entries and the food bank still read it and it is part of the wire format.
- Meal photos entirely: 13 files, `imagePath`, the photo filter, the
  wrapper's /blobs/ route, image_picker, and the CAMERA permission.

Reminder false alarms had three real causes, all fixed:
- Logging never reconciled notifications, so a reminder sat on screen up to
  15 minutes after the meal was logged -- on the phone itself.
- The background check never pulled, so a PC-logged meal nagged forever
  until the app was opened. Now mirrors _cli_gate._should_lock: cheap local
  check, network only when a slot looks due, then re-check.
- Meals before 08:00 / after 22:00 were attributed to no slot at all. New
  slot_for_log/slotForLog clamps them to the first/last slot, byte-identical
  across Python and Dart. Deliberately separate from elapsed_slots, which
  would otherwise make every slot fall due at 23:00.

Budget history, so lowering the budget stops rewriting the past:
- Forward-only (effective_from, kcal) entries; each day is judged against
  the budget in force then. Verified live: setting 2000 left a 36-day
  adherence streak and the whole calendar pixel-identical.
- Rides as hist:<date> fields on the existing budget record. crdt_sync
  merges per-field over the union of field names and both devices push the
  merged log, so a device that predates the feature relays them untouched --
  no coordinated rollout, no second document.
- The seed guard tests emptiness, not file presence: _sync can write an
  empty history after merging with a pre-feature peer, and a presence check
  would then never seed again, silently reclassifying every past day.

One shared source of truth for every piece of data:
- Body weight now travels as its own `weight` CRDT field. Inside `value` it
  was collateral damage of whole-map LWW -- any phone budget edit deleted it,
  and the protein target with it.
- Both halves of the food bank sync. The curated half (foods added without
  eating them) was the last device-local state. The derived half merges with
  count as the clock, so LWW means max-count-wins; the log stays
  authoritative for membership, because a CRDT union never shrinks and a
  peer's stale copy would otherwise resurrect a deleted food permanently.
- Bank adapters honour tombstones, and Dart parses bank records defensively:
  a cast failure there is an Error, not an Exception, so one malformed
  remote record would have broken phone sync permanently.

_test_guard.py blocks any pathlib write to real user state while pytest is
in the process, whatever conftest is loaded. A throwaway test run outside
the package conftest destroyed a real GitHub token during this work; this
makes that class of accident impossible rather than merely discouraged.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-26 16:39:52 +02:00

168 lines
5.7 KiB
Dart

/// Effective-from history of the daily kcal budget.
///
/// Pure mirror of `diet_guard/_budget_history.py` -- keep the two in sync, a
/// divergence means the PC and the phone classify the same day differently.
///
/// The budget is a single freely-editable number, but *classifying a past day*
/// must use the budget that applied on that day; otherwise lowering the budget
/// silently reclassifies months of history, breaking the adherence streak and
/// the year-to-date tally for days that were adherent at the time.
library;
import 'package:flutter/foundation.dart';
/// The budget assumed when nothing has ever been set on this device.
const int kDefaultDailyKcalGoal = 2200;
/// The effective-from date the migration seeds, so the pre-history budget
/// covers every day that was ever logged.
const String kEpochDay = '1970-01-01';
const String _epochIso = '1970-01-01T00:00:00.000Z';
const int _historyVersion = 1;
/// One budget value and the date it started applying.
@immutable
class BudgetEntry {
/// Creates a [BudgetEntry].
const BudgetEntry({
required this.effectiveFrom,
required this.kcal,
required this.editedAt,
});
/// Parses one stored entry, or null when it is malformed.
static BudgetEntry? tryFromJson(String day, Object? raw) {
if (raw is! Map) return null;
final kcal = raw['b'];
if (kcal is! int) return null;
final editedAt = raw['t'];
return BudgetEntry(
effectiveFrom: day,
kcal: kcal,
editedAt: editedAt is String ? editedAt : _epochIso,
);
}
/// `YYYY-MM-DD`; the first day this value applies to.
final String effectiveFrom;
/// The daily budget in kcal from [effectiveFrom] onward.
final int kcal;
/// Local ISO-8601 timestamp of the edit that created this entry, used to
/// derive a deterministic Hlc for the sync merge.
final String editedAt;
/// Serializes to the stored entry shape.
Map<String, dynamic> toJson() => {'b': kcal, 't': editedAt};
@override
bool operator ==(Object other) =>
other is BudgetEntry &&
other.effectiveFrom == effectiveFrom &&
other.kcal == kcal &&
other.editedAt == editedAt;
@override
int get hashCode => Object.hash(effectiveFrom, kcal, editedAt);
@override
String toString() => 'BudgetEntry($effectiveFrom, $kcal, $editedAt)';
}
/// The budget as a function of the day, with a fallback for empty history.
class BudgetSchedule {
/// Creates a [BudgetSchedule] from [entries], ascending by effective-from.
const BudgetSchedule(this.entries, {this.fallback = kDefaultDailyKcalGoal});
/// A schedule with no history at all; every day resolves to [fallback].
static const empty = BudgetSchedule(<BudgetEntry>[]);
/// Ascending by [BudgetEntry.effectiveFrom].
final List<BudgetEntry> entries;
/// Used when no entry applies -- an unset device, or a day before the
/// earliest entry. The second case does not arise in practice, since the
/// migration always seeds [kEpochDay].
final int fallback;
/// Returns the budget that applied on [day] (a `YYYY-MM-DD` key).
int forDay(String day) {
var result = fallback;
for (final entry in entries) {
if (entry.effectiveFrom.compareTo(day) > 0) break;
result = entry.kcal;
}
return result;
}
/// Returns the budget in force today.
int get current => forDay(_dateKey(DateTime.now()));
/// Returns this schedule with [when]'s date set to [kcal].
///
/// Keyed on the date, so editing the budget twice in one day replaces that
/// day's entry rather than accumulating duplicates.
BudgetSchedule upsert(int kcal, {DateTime? when}) {
final moment = when ?? DateTime.now();
final day = _dateKey(moment);
final kept = entries.where((e) => e.effectiveFrom != day).toList()
..add(
BudgetEntry(
effectiveFrom: day,
kcal: kcal,
editedAt: moment.toIso8601String(),
),
)
..sort((a, b) => a.effectiveFrom.compareTo(b.effectiveFrom));
return BudgetSchedule(kept, fallback: fallback);
}
/// Parses a stored history document, tolerating anything malformed.
///
/// A corrupt or unrecognised history degrades to "no history", which
/// callers already handle by falling back to the current scalar budget.
static List<BudgetEntry> parse(Object? raw) {
if (raw is! Map || raw['v'] != _historyVersion) return const [];
final stored = raw['e'];
if (stored is! Map) return const [];
final entries = <BudgetEntry>[];
for (final day in stored.keys) {
final entry = BudgetEntry.tryFromJson('$day', stored[day]);
if (entry != null) entries.add(entry);
}
entries.sort((a, b) => a.effectiveFrom.compareTo(b.effectiveFrom));
return entries;
}
/// Serializes [entries] to the stored document shape.
static Map<String, dynamic> encode(List<BudgetEntry> entries) => {
'v': _historyVersion,
'e': {for (final e in entries) e.effectiveFrom: e.toJson()},
};
/// Returns the one-entry history that grandfathers an existing budget.
///
/// The pre-history budget is treated as having applied since [kEpochDay],
/// so every already-logged day keeps the value it was actually judged
/// against. Reuses the caller's own [editedAt] rather than "now", which is
/// what lets two devices seed independently and still converge on the same
/// value at the same Hlc wall time.
static List<BudgetEntry> seed(int kcal, DateTime? editedAt) => [
BudgetEntry(
effectiveFrom: kEpochDay,
kcal: kcal,
editedAt: editedAt?.toIso8601String() ?? _epochIso,
),
];
}
String _dateKey(DateTime d) {
final y = d.year.toString().padLeft(4, '0');
final m = d.month.toString().padLeft(2, '0');
final day = d.day.toString().padLeft(2, '0');
return '$y-$m-$day';
}