Files
GroundedHelper/frontend/lib/Grounded/utils/IntegrityEngine.dart
alvocool 16bff634b5 Initial commit: Grounded Flutter frontend
A to-do app that doesn't believe you — an enforcement layer rather than a
neutral ledger.

Architecture ported from Autoreceptives/Frontend/Receptive: stacked MVVM with
the mandatory 4-file screen pattern, one ParentViewModel owning the loading /
network / error overlays and the handleError decision tree, one AppDataManager
gateway, dio comms carrying the three identity headers, secure storage with
random-suffixed keys, and a single-chokepoint Navigator. Package root and Dart
package name are both Grounded; org is nya.

The enforcement engine, one unit per formula in utils/:

- DebtEngine      w(class) x severity(d) x decay(t), sublinear severity so old
                  misses cannot swamp the score; abandonment 2x with 30-day
                  decay immunity; late complete retains 30%
- StandingEngine  Good -> Warned -> Grounded -> Lockdown, derived not set;
                  Grounded replaces home with the overdue queue
- CapacityEngine  blocks over-scheduling against p50 of historically completed
                  minutes, with a learned per-category estimation multiplier
- IntegrityEngine session integrity, weekly volume, plyometric contact ceiling
                  and enforced recovery gaps
- ExcuseAnalyser  on-device excuse clustering plus the confrontation copy
- GuardrailEngine distress detection and rationed amnesty
- ToneEngine      all enforcement copy, so the tone cap lives in one place

CommitmentEvent is append-only and is the source of truth rather than the
status field, which is what makes honest history and excuse analysis possible.

Goals contain commitments via parentId, and a task can be run from a
full-screen runner that derives elapsed time from wall-clock so screen-off
cannot lose time. Backgrounding pauses the clock and is counted. The runner is
mirrored into an ongoing notification, with alarm-class full-screen intents
reserved for non-negotiables.

Design language, fonts, icon and native splash are in place; Mason bricks are
retargeted to this project and verified end-to-end.

flutter analyze lib/ reports no errors.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Mfu2gQLSFN21YRBcU2NrTt
2026-07-27 09:11:17 +03:00

192 lines
5.4 KiB
Dart

import '../about/external/data/ExercisePrescription.dart';
import '../about/external/data/SessionLog.dart';
import '../about/external/data/SetLog.dart';
/// Did you do the session, or a watered-down version of it?
///
/// ```
/// integrity = 0.5 x (prescribed_sets_done / prescribed_sets)
/// + 0.3 x (1 - mean |actual_reps - target| / target)
/// + 0.2 x (hard_exercises_done / hard_exercises_prescribed)
/// ```
///
/// Skipping the hard exercise while doing the easy ones is the most common
/// form of self-deception in training, and the third term is what detects it.
class IntegrityEngine {
static const double setsWeight = 0.5;
static const double repsWeight = 0.3;
static const double hardWeight = 0.2;
static double score(
SessionLog session,
List<ExercisePrescription> prescribed,
) {
if (prescribed.isEmpty) {
return 0;
}
return (setsWeight * _setCompletion(session, prescribed)) +
(repsWeight * _repAccuracy(session, prescribed)) +
(hardWeight * _hardCompletion(session, prescribed));
}
/// Prescribed sets actually done, capped at 1 so overshooting one lift does
/// not paper over skipping another.
static double _setCompletion(
SessionLog session,
List<ExercisePrescription> prescribed,
) {
int prescribedSets = 0;
int doneSets = 0;
for (ExercisePrescription item in prescribed) {
prescribedSets = prescribedSets + item.sets;
final int done = session.sets
.where((entry) =>
entry.exerciseId == item.exerciseId && entry.isPrescribed)
.length;
doneSets = doneSets + (done > item.sets ? item.sets : done);
}
if (prescribedSets == 0) {
return 0;
}
return doneSets / prescribedSets;
}
/// How close the reps landed to target, averaged across prescribed sets.
static double _repAccuracy(
SessionLog session,
List<ExercisePrescription> prescribed,
) {
double totalError = 0;
int counted = 0;
for (ExercisePrescription item in prescribed) {
if (item.targetReps <= 0) {
continue;
}
final List<SetLog> done = session.sets
.where((entry) =>
entry.exerciseId == item.exerciseId && entry.isPrescribed)
.toList();
for (SetLog entry in done) {
final double error =
(entry.reps - item.targetReps).abs() / item.targetReps;
totalError = totalError + (error > 1 ? 1 : error);
counted = counted + 1;
}
}
if (counted == 0) {
return 0;
}
return 1 - (totalError / counted);
}
/// The term that catches cherry-picking. "Hard" comes from your historical
/// RPE on that exercise, not a static label.
static double _hardCompletion(
SessionLog session,
List<ExercisePrescription> prescribed,
) {
final List<ExercisePrescription> hard =
prescribed.where((item) => item.hard).toList();
if (hard.isEmpty) {
return 1;
}
int done = 0;
for (ExercisePrescription item in hard) {
final bool touched = session.sets.any((entry) =>
entry.exerciseId == item.exerciseId && entry.isPrescribed);
if (touched) {
done = done + 1;
}
}
return done / hard.length;
}
/// Whether an exercise counts as hard for this user, derived from historical
/// RPE rather than asserted up front.
static bool isHard(List<double> historicalRpe, {double cutoff = 8.0}) {
if (historicalRpe.isEmpty) {
return false;
}
double total = 0;
for (double value in historicalRpe) {
total = total + value;
}
return (total / historicalRpe.length) >= cutoff;
}
/// Weekly volume per muscle group, for under-target warnings — "you went to
/// the gym" is not a metric.
static Map<String, int> weeklySetsByMuscleGroup(List<SessionLog> sessions) {
final Map<String, int> volume = <String, int>{};
for (SessionLog session in sessions) {
for (SetLog entry in session.sets) {
if (entry.muscleGroup.isEmpty) {
continue;
}
volume[entry.muscleGroup] = (volume[entry.muscleGroup] ?? 0) + 1;
}
}
return volume;
}
/// Total plyometric ground contacts across the week.
static int weeklyContacts(List<SessionLog> sessions) {
int total = 0;
for (SessionLog session in sessions) {
total = total + session.contacts;
}
return total;
}
/// Plyo is the one modality where the app stops you rather than pushes you.
/// CNS and connective tissue do not recover on a motivation schedule.
static bool contactCeilingBreached(List<SessionLog> sessions, int ceiling) {
if (ceiling <= 0) {
return false;
}
return weeklyContacts(sessions) >= ceiling;
}
/// Whether enough recovery has passed since the last high-intensity lower
/// body session.
static bool recoveredEnough(
DateTime? lastHighIntensityLowerBody,
int requiredHours, {
DateTime? now,
}) {
if (lastHighIntensityLowerBody == null) {
return true;
}
final DateTime moment = now ?? DateTime.now();
return moment.difference(lastHighIntensityLowerBody).inHours >=
requiredHours;
}
/// Training through a deload logs as non-compliance, the same as skipping.
static bool violatesDeload(SessionLog session, bool isDeloadWeek) {
if (!isDeloadWeek) {
return false;
}
return session.sets.any((entry) => entry.rpe >= 8);
}
}