[Developer] #204 Phase 1 MVP — Flutter app skeleton complete
- Drift 21 tables (8 catalog + 11 user + habit_dose_variants + meta_kv) with R1~R10 CHECK constraints and 19 indexes - 8 hand-crafted seed JSON catalogs in app/assets/seed/ (refs 84, protocols 34, methodologies 21, frame_patterns 30, reward_menu_items 30, break_protocols 8, common_frames 5, diet_patterns 5) - 6 domain functions: recommend_variant, compute_streak, validate_frame_level, active_habit_quota, weekly_minimum_ratio, seed_importer (transactional, idempotent) - 4 vertical-slice Riverpod screens: HabitList, HabitCreate, CheckIn, Streak - 31 unit tests passing; flutter analyze clean - OQ-5 streak semantics: missing entry ≠ explicit blank (missing = end of history; only TrackerValue.blank triggers Never-miss-twice) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
162
app/lib/domain/streak/compute_streak.dart
Normal file
162
app/lib/domain/streak/compute_streak.dart
Normal file
@@ -0,0 +1,162 @@
|
||||
import '../models/tracker_entry.dart';
|
||||
|
||||
enum RewardTier { t0, t1, t2, t3, t4 }
|
||||
|
||||
extension RewardTierX on RewardTier {
|
||||
String get dbValue {
|
||||
switch (this) {
|
||||
case RewardTier.t0:
|
||||
return 'T0';
|
||||
case RewardTier.t1:
|
||||
return 'T1';
|
||||
case RewardTier.t2:
|
||||
return 'T2';
|
||||
case RewardTier.t3:
|
||||
return 'T3';
|
||||
case RewardTier.t4:
|
||||
return 'T4';
|
||||
}
|
||||
}
|
||||
|
||||
int get rank => RewardTier.values.indexOf(this);
|
||||
}
|
||||
|
||||
class StreakState {
|
||||
final int currentStreak;
|
||||
final int longestStreak;
|
||||
final int doneCountInPhase42;
|
||||
final int doneCountInWindow30;
|
||||
final RewardTier currentTier;
|
||||
final bool neverMissTwiceBroken;
|
||||
|
||||
const StreakState({
|
||||
required this.currentStreak,
|
||||
required this.longestStreak,
|
||||
required this.doneCountInPhase42,
|
||||
required this.doneCountInWindow30,
|
||||
required this.currentTier,
|
||||
required this.neverMissTwiceBroken,
|
||||
});
|
||||
|
||||
static const StreakState empty = StreakState(
|
||||
currentStreak: 0,
|
||||
longestStreak: 0,
|
||||
doneCountInPhase42: 0,
|
||||
doneCountInWindow30: 0,
|
||||
currentTier: RewardTier.t0,
|
||||
neverMissTwiceBroken: false,
|
||||
);
|
||||
}
|
||||
|
||||
/// fn-compute-streak: computes streak + 5-tier milestone with Never miss twice.
|
||||
///
|
||||
/// OQ-5 decision (2026-06-11):
|
||||
/// - 2+ consecutive blank → tier demoted (T3→T2, T2→T1, T1→T0), streak = 0,
|
||||
/// neverMissTwiceBroken = true.
|
||||
/// - 1 blank → streak = 0, tier stays. Next done starts at 1.
|
||||
///
|
||||
/// Pure function. [habitStartedAt] is the habit's started_at (YYYY-MM-DD).
|
||||
StreakState computeStreak({
|
||||
required Iterable<TrackerEntryModel> entries,
|
||||
required DateTime asOf,
|
||||
required String habitStartedAt,
|
||||
}) {
|
||||
// 1. Index by date string (YYYY-MM-DD).
|
||||
final byDate = <String, TrackerEntryModel>{};
|
||||
for (final e in entries) {
|
||||
byDate[e.date] = e;
|
||||
}
|
||||
|
||||
if (byDate.isEmpty) return StreakState.empty;
|
||||
|
||||
final startDate = DateTime.parse(habitStartedAt);
|
||||
final asOfDate = DateTime(asOf.year, asOf.month, asOf.day);
|
||||
|
||||
// 2. currentStreak (Never miss twice).
|
||||
//
|
||||
// Semantics:
|
||||
// - Walk back from asOf, stopping at habit start.
|
||||
// - "No entry record" for a date means tracking hasn't reached that day (or
|
||||
// user hasn't synced) — treat as the end of streak history, do not penalize.
|
||||
// - Explicit TrackerValue.blank is the penalty signal. 1 blank zeroes the
|
||||
// streak (tier stays); 2 consecutive blanks set neverMissTwiceBroken.
|
||||
var streak = 0;
|
||||
var consecutiveBlank = 0;
|
||||
var neverMissTwiceBroken = false;
|
||||
var cursor = asOfDate;
|
||||
|
||||
while (!cursor.isBefore(startDate)) {
|
||||
final e = byDate[_ymd(cursor)];
|
||||
if (e == null) {
|
||||
// End of recorded history.
|
||||
break;
|
||||
}
|
||||
if (e.value == TrackerValue.blank) {
|
||||
consecutiveBlank += 1;
|
||||
if (consecutiveBlank >= 2) {
|
||||
neverMissTwiceBroken = true;
|
||||
streak = 0;
|
||||
break;
|
||||
}
|
||||
// 1 blank so far: streak is broken, but keep walking one more day to
|
||||
// detect a possible double-blank.
|
||||
streak = 0;
|
||||
cursor = cursor.subtract(const Duration(days: 1));
|
||||
continue;
|
||||
}
|
||||
// Done.
|
||||
if (consecutiveBlank > 0) {
|
||||
// Previous step saw a single blank, now done → single-blank confirmed.
|
||||
break;
|
||||
}
|
||||
streak += 1;
|
||||
cursor = cursor.subtract(const Duration(days: 1));
|
||||
}
|
||||
|
||||
// 3. longestStreak over all entries.
|
||||
final sortedDates = byDate.keys.toList()..sort();
|
||||
var longest = 0;
|
||||
var run = 0;
|
||||
for (final d in sortedDates) {
|
||||
if (byDate[d]!.value == TrackerValue.done) {
|
||||
run += 1;
|
||||
if (run > longest) longest = run;
|
||||
} else {
|
||||
run = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Window counts.
|
||||
final win30Start = asOfDate.subtract(const Duration(days: 29));
|
||||
final win42Start = asOfDate.subtract(const Duration(days: 41));
|
||||
var window30 = 0;
|
||||
var window42 = 0;
|
||||
for (final e in byDate.values) {
|
||||
if (e.value != TrackerValue.done) continue;
|
||||
final d = DateTime.parse(e.date);
|
||||
if (!d.isBefore(win30Start) && !d.isAfter(asOfDate)) window30 += 1;
|
||||
if (!d.isBefore(win42Start) && !d.isAfter(asOfDate)) window42 += 1;
|
||||
}
|
||||
|
||||
// 5. Tier judgment.
|
||||
var tier = RewardTier.t0;
|
||||
if (streak >= 3) tier = RewardTier.t1;
|
||||
if (streak >= 7) tier = RewardTier.t2;
|
||||
if (window30 >= 24 && tier.rank < RewardTier.t3.rank) tier = RewardTier.t3;
|
||||
final phaseDay = asOfDate.difference(startDate).inDays + 1;
|
||||
if (phaseDay >= 42 && window42 >= 30) tier = RewardTier.t4;
|
||||
|
||||
return StreakState(
|
||||
currentStreak: streak,
|
||||
longestStreak: longest,
|
||||
doneCountInPhase42: window42,
|
||||
doneCountInWindow30: window30,
|
||||
currentTier: tier,
|
||||
neverMissTwiceBroken: neverMissTwiceBroken,
|
||||
);
|
||||
}
|
||||
|
||||
String _ymd(DateTime d) =>
|
||||
'${d.year.toString().padLeft(4, '0')}-'
|
||||
'${d.month.toString().padLeft(2, '0')}-'
|
||||
'${d.day.toString().padLeft(2, '0')}';
|
||||
66
app/lib/domain/streak/weekly_minimum_ratio.dart
Normal file
66
app/lib/domain/streak/weekly_minimum_ratio.dart
Normal file
@@ -0,0 +1,66 @@
|
||||
import '../models/habit.dart';
|
||||
import '../models/tracker_entry.dart';
|
||||
|
||||
/// fn-weekly-minimum-ratio
|
||||
///
|
||||
/// Computes the share of done check-ins in the last 7 days that used the
|
||||
/// habit's "minimum" dose variant. Useful for L2-conditional habits where the
|
||||
/// user picks a minimum option on bad-condition days. A high ratio is fine
|
||||
/// (the protocol's whole point), but it also signals the user is mostly
|
||||
/// running on minimum dose — a context the UI can surface.
|
||||
///
|
||||
/// Returns 0.0 when there are no done entries in the window (no division by
|
||||
/// zero).
|
||||
class WeeklyMinimumRatio {
|
||||
final int totalDone;
|
||||
final int minimumUsed;
|
||||
final double ratio; // 0.0..1.0
|
||||
final DateTime windowStart; // inclusive, YYYY-MM-DD == windowStart
|
||||
final DateTime windowEnd; // inclusive
|
||||
|
||||
const WeeklyMinimumRatio({
|
||||
required this.totalDone,
|
||||
required this.minimumUsed,
|
||||
required this.ratio,
|
||||
required this.windowStart,
|
||||
required this.windowEnd,
|
||||
});
|
||||
}
|
||||
|
||||
/// Pure function: caller resolves the variant rows and passes them in.
|
||||
///
|
||||
/// - [entries] should already be filtered to the habit and to value=done.
|
||||
/// - [variantsById] maps variant_id → variant (only minimums need to be
|
||||
/// present, but a full map is fine).
|
||||
/// - [asOf] is treated as the inclusive end of the 7-day window.
|
||||
WeeklyMinimumRatio computeWeeklyMinimumRatio({
|
||||
required Iterable<TrackerEntryModel> entries,
|
||||
required Map<String, HabitDoseVariantModel> variantsById,
|
||||
required DateTime asOf,
|
||||
}) {
|
||||
final end = DateTime(asOf.year, asOf.month, asOf.day);
|
||||
final start = end.subtract(const Duration(days: 6));
|
||||
|
||||
var totalDone = 0;
|
||||
var minimumUsed = 0;
|
||||
|
||||
for (final e in entries) {
|
||||
if (e.value != TrackerValue.done) continue;
|
||||
final d = DateTime.parse(e.date);
|
||||
if (d.isBefore(start) || d.isAfter(end)) continue;
|
||||
totalDone += 1;
|
||||
final vId = e.variantId;
|
||||
if (vId == null) continue;
|
||||
final v = variantsById[vId];
|
||||
if (v != null && v.isMinimum) minimumUsed += 1;
|
||||
}
|
||||
|
||||
final ratio = totalDone == 0 ? 0.0 : minimumUsed / totalDone;
|
||||
return WeeklyMinimumRatio(
|
||||
totalDone: totalDone,
|
||||
minimumUsed: minimumUsed,
|
||||
ratio: ratio,
|
||||
windowStart: start,
|
||||
windowEnd: end,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user