From 9b30c1208f59dfaf18791a36668e86c06942d567 Mon Sep 17 00:00:00 2001 From: Vibe Code Date: Wed, 30 Sep 2026 20:35:34 +0000 Subject: [PATCH 01/11] =?UTF-8?q?feat:=20BP=20research=20capture=20(dev=20?= =?UTF-8?q?mode)=20=E2=80=94=20paired=20cuff=20reference=20+=20frozen=20?= =?UTF-8?q?=C2=B12=20min=20band=20window,=20CSV=20export=20set,=20isolatio?= =?UTF-8?q?n=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: BucciMobile --- lib/data/csv_export.dart | 40 +++++ lib/data/db.dart | 156 ++++++++++++++++- lib/health/bp_research_capture.dart | 187 ++++++++++++++++++++ lib/l10n/app_en.arb | 52 ++++++ lib/ui2/profile/bp_research.dart | 247 +++++++++++++++++++++++++++ lib/ui2/profile/settings.dart | 8 + test/bp_research_capture_test.dart | 77 +++++++++ test/bp_research_isolation_test.dart | 60 +++++++ 8 files changed, 826 insertions(+), 1 deletion(-) create mode 100644 lib/health/bp_research_capture.dart create mode 100644 lib/ui2/profile/bp_research.dart create mode 100644 test/bp_research_capture_test.dart create mode 100644 test/bp_research_isolation_test.dart diff --git a/lib/data/csv_export.dart b/lib/data/csv_export.dart index e061cafe6..5c1b803e1 100644 --- a/lib/data/csv_export.dart +++ b/lib/data/csv_export.dart @@ -283,6 +283,46 @@ const kCsvExportSets = [ ORDER BY d.date ASC ''', ), + CsvExportSet( + name: 'bp_research', + title: 'BP research captures (EXPERIMENTAL)', + // Paired cuff reference readings plus the band window frozen around + // each instant. Research data, not health data: never blended, never a + // training input, and absent stats stay EMPTY here exactly as they are + // NULL in the store — a spreadsheet cannot tell a zero from a reading + // afterwards, and a column of zeroes is a fabrication. + columns: [ + 'measured_at_ms', + 'device', + 'posture', + 'conditions', + 'systolic_mmhg', + 'diastolic_mmhg', + 'captured_at_ms', + 'window_start_ms', + 'window_end_ms', + 'onehz_rows', + 'rr_beats', + 'hr_mean', + 'rr_ms_mean', + 'rr_ms_min', + 'rr_ms_max', + 'rmssd_ms', + 'meta_json', + ], + sql: ''' + SELECT r.measured_at_ms, COALESCE(r.device, '') AS device, + COALESCE(r.posture, '') AS posture, + COALESCE(r.conditions, '') AS conditions, + r.systolic_mmhg, r.diastolic_mmhg, r.captured_at_ms, + w.window_start_ms, w.window_end_ms, w.onehz_rows, w.rr_beats, + w.hr_mean, w.rr_ms_mean, w.rr_ms_min, w.rr_ms_max, w.rmssd_ms, + COALESCE(w.meta_json, '') AS meta_json + FROM bp_research_reference r + LEFT JOIN bp_research_window w ON w.reference_id = r.id + ORDER BY r.measured_at_ms ASC + ''', + ), ]; /// What CSV deliberately does NOT carry, and why. Shown to the user on the diff --git a/lib/data/db.dart b/lib/data/db.dart index f4ee1ecfe..7b5c5a8a8 100644 --- a/lib/data/db.dart +++ b/lib/data/db.dart @@ -33,6 +33,7 @@ import '../import/import_container.dart'; import 'coverage_resolver.dart' show CoverageInterval; import 'day_label.dart'; import 'journal_fields.dart'; +import '../health/bp_research_capture.dart' import 'live_coverage_policy.dart'; import 'med_store.dart'; import 'models.dart'; @@ -349,7 +350,7 @@ class LocalDb { /// pass it: sqflite throws `ArgumentError('onCreate must be null if no /// version is specified')` BEFORE opening anything when `onCreate` is given /// without `version` (sqflite_common database_mixin.dart). - static const int schemaVersion = 54; + static const int schemaVersion = 55; /// SQLite caps host parameters per statement (`SQLITE_MAX_VARIABLE_NUMBER` — /// only 999 on the builds shipped with older Android/iOS). Any `IN (?, ?, …)` @@ -461,6 +462,7 @@ class LocalDb { await _createNotifFired(db); await _createNotifSlots(db); await _createAlarmSchedule(db); + await _createBpResearch(db); await _ensureCoachViews(db); }, onUpgrade: (db, oldV, newV) async { @@ -1073,6 +1075,18 @@ class LocalDb { // next free rung rather than collide with any of them. await _createEcgTables(db); } + if (oldV < 55) { + // BP research capture: paired cuff reference readings plus the + // band's own decoded 1 Hz / R-R window frozen around the + // measurement instant, for out-of-app comparison only. Two new + // tables, CREATE TABLE IF NOT EXISTS and NOTHING else — no + // backfill, no rewrite, no ADD COLUMN — so a throw here has + // nothing to roll back onto (invariant 11). Ships without a + // kAlgoVersion bump: nothing derived moves, and nothing derived + // may ever read these (see the guard comment in + // [_createBpResearch]). + await _createBpResearch(db); + } }, onOpen: (db) async { await _repairOpenSchema(db); @@ -1582,6 +1596,146 @@ class LocalDb { ); } + + /// BP research capture store (schema rung 55). + /// + /// EXPERIMENTAL, DEVELOPER-ONLY, and it stays that way. A cuff reading the + /// user types in next to the band data of the same instant is exactly the + /// pairing the `imported_measurement` guard exists to prevent becoming an + /// input: the moment a wrist series and a cuff series are regressed against + /// each other ON DEVICE, this app is making a cuffless-blood-pressure + /// claim from an uncleared device. So this is a SEPARATE store, read by + /// exactly one dev screen and one CSV export, and — like + /// `imported_measurement` and `observation` — the isolation is structural: + /// nothing in `compute/`, nothing that feeds `day_result` or + /// `metric_series`, and nothing that writes to HealthKit / Health Connect + /// may name either table. `bp_research_isolation_test.dart` fails the moment + /// anyone does. + /// + /// Captures are idempotent on `(measured_at_ms, device)`: retaking the same + /// cuff reading at the same instant re-states the window rather than + /// duplicating it. Legitimate repeat measurements minutes apart are + /// different instants and both stay. + static Future _createBpResearch(Database db) async { + await db.execute(''' + CREATE TABLE IF NOT EXISTS bp_research_reference ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + measured_at_ms INTEGER NOT NULL, + device TEXT, + posture TEXT, + conditions TEXT, + systolic_mmhg REAL NOT NULL, + diastolic_mmhg REAL NOT NULL, + captured_at_ms INTEGER NOT NULL, + UNIQUE (measured_at_ms, device) + ) + '''); + await db.execute(''' + CREATE TABLE IF NOT EXISTS bp_research_window ( + reference_id INTEGER NOT NULL PRIMARY KEY + REFERENCES bp_research_reference(id) ON DELETE CASCADE, + window_start_ms INTEGER NOT NULL, + window_end_ms INTEGER NOT NULL, + -- NULL-safe by design: the band may have had nothing to say at that + -- instant (not worn, not synced yet), and a missing window is recorded + -- as missing — never as zeroes. + onehz_rows INTEGER, + rr_beats INTEGER, + hr_mean REAL, + rr_ms_mean REAL, + rr_ms_min REAL, + rr_ms_max REAL, + rmssd_ms REAL, + meta_json TEXT + ) + '''); + await db.execute( + 'CREATE INDEX IF NOT EXISTS idx_bp_research_reference_at ' + 'ON bp_research_reference(measured_at_ms)', + ); + } + + /// Insert one cuff reference reading plus its frozen band window. + /// + /// Pure write; the caller computes the window stats (see + /// `lib/health/bp_research_capture.dart`). Idempotent on + /// `(measured_at_ms, device)`: `INSERT OR REPLACE` on the reference, then + /// the window row is restated in the same transaction so a retake can never + /// leave an old window under a new reference. + static Future putBpResearchCapture(BpResearchCapture c) async { + final db = await instance; + await db.transaction((txn) async { + final id = await txn.rawInsert( + 'INSERT OR REPLACE INTO bp_research_reference ' + '(measured_at_ms, device, posture, conditions, systolic_mmhg, ' + 'diastolic_mmhg, captured_at_ms) VALUES (?, ?, ?, ?, ?, ?, ?)', + [ + c.measuredAtMs, + c.device, + c.posture, + c.conditions, + c.systolicMmHg, + c.diastolicMmHg, + c.capturedAtMs, + ], + ); + // A capture with no band data stores NO window row — the LEFT JOIN in + // [bpResearchCaptures] renders it as an empty window, and `NOT NULL` + // on the window bounds is what keeps a half-written window out of the + // store. A retake that now finds band data replaces the absent row. + final w = c.window; + if (w == null) { + await txn.rawDelete( + 'DELETE FROM bp_research_window WHERE reference_id = ?', [id]); + return; + } + await txn.rawInsert( + 'INSERT OR REPLACE INTO bp_research_window ' + '(reference_id, window_start_ms, window_end_ms, onehz_rows, ' + 'rr_beats, hr_mean, rr_ms_mean, rr_ms_min, rr_ms_max, rmssd_ms, ' + 'meta_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', + [ + id, + w.windowStartMs, + w.windowEndMs, + w.onehzRows, + w.rrBeats, + w.hrMean, + w.rrMsMean, + w.rrMsMin, + w.rrMsMax, + w.rmssdMs, + w.metaJson, + ], + ); + }); + } + + /// All captures, newest first, for the dev screen and the CSV export. + static Future>> bpResearchCaptures() async { + final db = await instance; + return db.rawQuery(''' + SELECT r.id, r.measured_at_ms, r.device, r.posture, r.conditions, + r.systolic_mmhg, r.diastolic_mmhg, r.captured_at_ms, + w.window_start_ms, w.window_end_ms, w.onehz_rows, w.rr_beats, + w.hr_mean, w.rr_ms_mean, w.rr_ms_min, w.rr_ms_max, w.rmssd_ms, + w.meta_json + FROM bp_research_reference r + LEFT JOIN bp_research_window w ON w.reference_id = r.id + ORDER BY r.measured_at_ms DESC + '''); + } + + /// Delete one capture (dev screen). The window cascades. + static Future deleteBpResearchCapture(int id) async { + final db = await instance; + await db.delete( + 'bp_research_reference', + where: 'id = ?', + whereArgs: [id], + ); + } + /// Atomically claim [key] for a one-time OS notification fire. /// /// Returns true iff THIS caller won the claim (the row did not exist and we diff --git a/lib/health/bp_research_capture.dart b/lib/health/bp_research_capture.dart new file mode 100644 index 000000000..7307c4da4 --- /dev/null +++ b/lib/health/bp_research_capture.dart @@ -0,0 +1,187 @@ +// BP research capture — pair a cuff reading the user just took with the +// band's own decoded data from the minutes around that instant. +// +// EXPERIMENTAL / DEVELOPER-ONLY. This exists to build a paired dataset a +// human can analyse OUTSIDE this app (CSV export); it is not a blood +// pressure feature and never becomes one. The same guard that fences +// `imported_measurement` applies twice over here: nothing in `compute/`, +// nothing that writes `day_result` / `metric_series`, and nothing that +// writes to HealthKit / Health Connect may read these tables — a wrist +// series regressed against cuff readings inside this app is a +// cuffless-blood-pressure claim from an uncleared device, which is exactly +// the category that earned WHOOP an FDA Warning Letter in Jul 2025. +// +// A capture with no band data at that instant is stored as a capture with a +// NULL window. Missing is missing — never 0, never a fabricated average. + + +/// Window half-widths around the cuff instant (default ±2 min): the frozen +/// window covers [kBpResearchWindowPreMs] before the reference instant and +/// [kBpResearchWindowPostMs] after it. Lives here, next to the capture +/// logic, so the model file has no dependency direction to argue about. +const int kBpResearchWindowPreMs = 2 * 60 * 1000; +const int kBpResearchWindowPostMs = 2 * 60 * 1000; + +/// The frozen band window around one reference instant. Every field is +/// nullable for the same reason the storage layer's columns are: a stat the +/// window could not honestly compute (no valid HR, no beats) is absent, not +/// zero. +class BpResearchWindow { + const BpResearchWindow({ + required this.windowStartMs, + required this.windowEndMs, + this.onehzRows, + this.rrBeats, + this.hrMean, + this.rrMsMean, + this.rrMsMin, + this.rrMsMax, + this.rmssdMs, + this.metaJson, + }); + + final int windowStartMs; + final int windowEndMs; + final int? onehzRows; + final int? rrBeats; + final double? hrMean; + final double? rrMsMean; + final double? rrMsMin; + final double? rrMsMax; + final double? rmssdMs; + + /// Provenance the analysis needs and nothing else: device_id, firmware + /// string if known, sample counts by table. JSON, written verbatim. + final String? metaJson; +} + +/// One cuff reference reading plus its window, ready to store. +class BpResearchCapture { + const BpResearchCapture({ + required this.measuredAtMs, + required this.systolicMmHg, + required this.diastolicMmHg, + required this.capturedAtMs, + required this.device, + this.posture, + this.conditions, + this.window, + }); + + final int measuredAtMs; + final double systolicMmHg; + final double diastolicMmHg; + final int capturedAtMs; + + /// The cuff's own name ('OMRON', 'Withings BPM', …). NULL when the user + /// typed a bare pair of numbers with no device named. + final String? device; + final String? posture; + final String? conditions; + + /// Null when the band had nothing decoded in the window. + final BpResearchWindow? window; +} + +/// Same plausibility bounds as `health_measurement_import.dart`, for the +/// same reason: 400 mmHg is a cuff error, and a clamped reading is a +/// fabricated one. Out-of-bounds input is rejected, never corrected. +const (double, double) kResearchSystolicBounds = (50, 300); +const (double, double) kResearchDiastolicBounds = (20, 200); + +/// Compute the frozen band window around [measuredAtMs] from the decoded +/// store, pure and testable without a database (pass the rows in). +/// +/// Window: measured instant minus/plus [preMs]/[postMs] (default ±2 min, +/// [kBpResearchWindowPreMs]/[kBpResearchWindowPostMs]). Reads ONLY already-decoded +/// tables — `decoded_onehz` (HR) and `decoded_rr` (beat intervals). No raw +/// archive, no re-decode, nothing derived: the point is to freeze exactly +/// what the app already holds at the moment of the cuff reading. +BpResearchWindow? researchWindowFrom({ + required int measuredAtMs, + required List> onehzRows, + required List> rrRows, + int? preMs, + int? postMs, + String? deviceId, + String? metaJson, +}) { + final pre = preMs ?? kBpResearchWindowPreMs; + final post = postMs ?? kBpResearchWindowPostMs; + final start = measuredAtMs - pre; + final end = measuredAtMs + post; + + // decoded_onehz.rec_ts is epoch SECONDS; rr is rr_ts_ms (epoch ms). + final onehz = onehzRows + .where((r) { + final ts = r['rec_ts']; + return ts is int && ts * 1000 >= start && ts * 1000 <= end; + }) + .toList(growable: false); + final rr = rrRows + .where((r) { + final ts = r['rr_ts_ms']; + return ts is int && ts >= start && ts <= end; + }) + .toList(growable: false); + + if (onehz.isEmpty && rr.isEmpty) return null; + + // HR mean over VALID HR rows only — a run of hr_valid = 0 rows must not + // drag an average toward zero, and absent validity is absent, not false. + final hrs = onehz + .map((r) => r['hr']) + .whereType() + .where((h) => h > 0) + .toList(growable: false); + final hrMean = hrs.isEmpty + ? null + : hrs.reduce((a, b) => a + b) / hrs.length; + + final rrs = rr + .map((r) => r['rr_ms']) + .whereType() + .map((v) => v.toDouble()) + .toList(growable: false); + double? rrMean, rrMin, rrMax, rmssd; + if (rrs.isNotEmpty) { + rrMean = rrs.reduce((a, b) => a + b) / rrs.length; + rrMin = rrs.reduce((a, b) => a < b ? a : b); + rrMax = rrs.reduce((a, b) => a > b ? a : b); + // RMSSD over successive differences in window order (rr_ts_ms ASC is + // the caller's contract). Too few beats to form one difference: absent. + if (rrs.length >= 2) { + var sumSq = 0.0; + for (var i = 1; i < rrs.length; i++) { + final d = rrs[i] - rrs[i - 1]; + sumSq += d * d; + } + rmssd = _sqrt(sumSq / (rrs.length - 1)); + } + } + + return BpResearchWindow( + windowStartMs: start, + windowEndMs: end, + onehzRows: onehz.isEmpty ? null : onehz.length, + rrBeats: rr.isEmpty ? null : rr.length, + hrMean: hrMean?.toDouble(), + rrMsMean: rrMean, + rrMsMin: rrMin, + rrMsMax: rrMax, + rmssdMs: rmssd, + metaJson: metaJson, + ); +} + +double _sqrt(double v) => v <= 0 ? 0.0 : _sqrtNewton(v); + +double _sqrtNewton(double v) { + var x = v; + var y = (x + 1) / 2; + while ((y - x).abs() > 1e-12) { + x = y; + y = (x + v / x) / 2; + } + return y; +} diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index ae97e419e..bfd8524f3 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -12441,5 +12441,57 @@ "type": "int" } } + }, + "settingsBpResearchRowTitle": "BP research capture", + "@settingsBpResearchRowTitle": { + "description": "Developer settings row title for the experimental blood-pressure research capture screen" + }, + "settingsBpResearchRowSub": "EXPERIMENTAL. Pair a cuff reading with the band data of the same instant, for comparison outside this app. Never a health feature", + "@settingsBpResearchRowSub": { + "description": "Developer settings row subtitle for the BP research capture screen" + }, + "bpResearchTitle": "BP research capture", + "@bpResearchTitle": { + "description": "Title of the experimental BP research capture screen" + }, + "bpResearchIntro": "EXPERIMENTAL. Take a cuff reading, type the pair in, press capture. The band data of the ±2 minutes around that instant is frozen next to it — for you to compare outside this app. Nothing here is a health feature, nothing here feeds any score, and nothing here is ever blended with what the band measured.", + "@bpResearchIntro": { + "description": "Intro text of the BP research capture screen" + }, + "bpResearchSystolic": "Systolic (mmHg)", + "@bpResearchSystolic": { + "description": "Text field label for the systolic reference value" + }, + "bpResearchDiastolic": "Diastolic (mmHg)", + "@bpResearchDiastolic": { + "description": "Text field label for the diastolic reference value" + }, + "bpResearchDevice": "Cuff device (optional)", + "@bpResearchDevice": { + "description": "Text field label for the cuff device name" + }, + "bpResearchPosture": "Posture (optional)", + "@bpResearchPosture": { + "description": "Text field label for the posture during the reading" + }, + "bpResearchConditions": "Conditions (optional)", + "@bpResearchConditions": { + "description": "Text field label for measurement conditions" + }, + "bpResearchCapture": "Capture now", + "@bpResearchCapture": { + "description": "Button label to store one capture" + }, + "bpResearchHistory": "Captures", + "@bpResearchHistory": { + "description": "Section header for the stored capture list" + }, + "bpResearchBadValue": "That pair is not a blood pressure — check the numbers and try again. Nothing was stored.", + "@bpResearchBadValue": { + "description": "Snackbar text for an out-of-bounds reference pair, which is rejected, never clamped" + }, + "bpResearchExportHint": "Export all captures as CSV from Your data › Export CSV (set \"BP research captures\"). Research data — it never leaves the phone except through that file.", + "@bpResearchExportHint": { + "description": "Hint under the capture list explaining the CSV export path" } } diff --git a/lib/ui2/profile/bp_research.dart b/lib/ui2/profile/bp_research.dart new file mode 100644 index 000000000..4c92b1562 --- /dev/null +++ b/lib/ui2/profile/bp_research.dart @@ -0,0 +1,247 @@ +// BP research capture — DEVELOPER MODE ONLY. +// +// One flow: take a cuff blood pressure reading, type the pair in, press +// capture. The app freezes the band's own decoded data from the ±2 minutes +// around that instant (1 Hz HR, R-R intervals) next to the reference pair, +// and keeps every capture so a human can compare them over weeks — here in +// a list, or out of the app through the `bp_research` CSV export set. +// +// This is data COLLECTION, not a blood pressure feature: +// · Nothing derived reads these tables. No score, no baseline, no chart +// of ours takes a capture as an input. +// · Nothing here is ever blended with, averaged against, or corrected +// against anything the band measured. +// · Nothing here is exported to HealthKit / Health Connect. +// A window with no band data is stored as a capture with an EMPTY window — +// missing is missing, never zero. + +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:lucide_icons_flutter/lucide_icons.dart'; + +import '../../data/db.dart'; +import '../../health/bp_research_capture.dart'; +import '../../l10n/app_localizations.dart'; +import '../ui2.dart'; +import 'devices.dart' show formatDayTime; + +class BpResearchScreen extends StatefulWidget { + const BpResearchScreen({super.key}); + + @override + State createState() => _BpResearchScreenState(); +} + +class _BpResearchScreenState extends State { + final _sys = TextEditingController(); + final _dia = TextEditingController(); + final _device = TextEditingController(); + final _posture = TextEditingController(); + final _conditions = TextEditingController(); + List> _rows = const []; + bool _busy = false; + + @override + void initState() { + super.initState(); + _refresh(); + } + + @override + void dispose() { + _sys.dispose(); + _dia.dispose(); + _device.dispose(); + _posture.dispose(); + _conditions.dispose(); + super.dispose(); + } + + Future _refresh() async { + final rows = await LocalDb.bpResearchCaptures(); + if (mounted) setState(() => _rows = rows); + } + + Future _capture() async { + if (_busy) return; + final sys = double.tryParse(_sys.text); + final dia = double.tryParse(_dia.text); + final l = AppLocalizations.of(context); + if (sys == null || + dia == null || + sys < kResearchSystolicBounds.$1 || + sys > kResearchSystolicBounds.$2 || + dia < kResearchDiastolicBounds.$1 || + dia > kResearchDiastolicBounds.$2) { + if (mounted) { + ScaffoldMessenger.of(context).showSnackBar(SnackBar( + content: Text(l?.bpResearchBadValue ?? + 'That pair is not a blood pressure — check the numbers and ' + 'try again. Nothing was stored.'), + )); + } + return; + } + setState(() => _busy = true); + try { + final now = DateTime.now(); + final start = now.millisecondsSinceEpoch - kBpResearchWindowPreMs; + final end = now.millisecondsSinceEpoch + kBpResearchWindowPostMs; + final db = await LocalDb.instance; + // Read exactly what the app already holds around the instant. Two + // narrow range reads — never a day dump, never the raw archive. + final onehz = await db.rawQuery( + 'SELECT rec_ts, hr FROM decoded_onehz ' + 'WHERE device_id = ? AND rec_ts >= ? AND rec_ts <= ? ' + 'ORDER BY rec_ts ASC', + [LocalDb.kPrimaryDeviceId, start ~/ 1000, end ~/ 1000], + ); + final rr = await db.rawQuery( + 'SELECT rr_ts_ms, rr_ms FROM decoded_rr ' + 'WHERE device_id = ? AND rr_ts_ms >= ? AND rr_ts_ms <= ? ' + 'ORDER BY rr_ts_ms ASC', + [LocalDb.kPrimaryDeviceId, start, end], + ); + final window = researchWindowFrom( + measuredAtMs: now.millisecondsSinceEpoch, + onehzRows: onehz, + rrRows: rr, + ); + await LocalDb.putBpResearchCapture(BpResearchCapture( + measuredAtMs: now.millisecondsSinceEpoch, + systolicMmHg: sys, + diastolicMmHg: dia, + capturedAtMs: now.millisecondsSinceEpoch, + device: _device.text.trim().isEmpty ? null : _device.text.trim(), + posture: _posture.text.trim().isEmpty ? null : _posture.text.trim(), + conditions: + _conditions.text.trim().isEmpty ? null : _conditions.text.trim(), + window: window, + )); + _sys.clear(); + _dia.clear(); + await _refresh(); + } finally { + if (mounted) setState(() => _busy = false); + } + } + + @override + Widget build(BuildContext c) { + final l = AppLocalizations.of(c); + return Scaffold( + appBar: AppBar( + title: Text(l?.bpResearchTitle ?? 'BP research capture'), + ), + body: ListView( + padding: const EdgeInsets.all(S.x4), + children: [ + Text( + l?.bpResearchIntro ?? + 'EXPERIMENTAL. Take a cuff reading, type the pair in, ' + 'press capture. The band data of the ±2 minutes around that ' + 'instant is frozen next to it — for you to compare outside ' + 'this app. Nothing here is a health feature, nothing here ' + 'feeds any score, and nothing here is ever blended with what ' + 'the band measured.', + style: c.textTheme.bodySmall, + ), + const SizedBox(height: S.x4), + TextField( + controller: _sys, + keyboardType: TextInputType.number, + inputFormatters: [FilteringTextInputFormatter.allow(RegExp(r'[0-9]'))], + decoration: InputDecoration( + labelText: l?.bpResearchSystolic ?? 'Systolic (mmHg)', + ), + ), + const SizedBox(height: S.x2), + TextField( + controller: _dia, + keyboardType: TextInputType.number, + inputFormatters: [FilteringTextInputFormatter.allow(RegExp(r'[0-9]'))], + decoration: InputDecoration( + labelText: l?.bpResearchDiastolic ?? 'Diastolic (mmHg)', + ), + ), + const SizedBox(height: S.x2), + TextField( + controller: _device, + decoration: InputDecoration( + labelText: l?.bpResearchDevice ?? 'Cuff device (optional)', + ), + ), + const SizedBox(height: S.x2), + TextField( + controller: _posture, + decoration: InputDecoration( + labelText: l?.bpResearchPosture ?? 'Posture (optional)', + ), + ), + const SizedBox(height: S.x2), + TextField( + controller: _conditions, + decoration: InputDecoration( + labelText: l?.bpResearchConditions ?? 'Conditions (optional)', + ), + ), + const SizedBox(height: S.x4), + FilledButton.icon( + onPressed: _busy ? null : _capture, + icon: const Icon(LucideIcons.plus), + label: Text(l?.bpResearchCapture ?? 'Capture now'), + ), + const SizedBox(height: S.x6), + if (_rows.isNotEmpty) ...[ + Text(l?.bpResearchHistory ?? 'Captures', + style: c.textTheme.titleMedium), + const SizedBox(height: S.x2), + for (final r in _rows) + ListTile( + dense: true, + title: Text( + '${r['systolic_mmhg']}/${r['diastolic_mmhg']} mmHg — ' + '${formatDayTime(DateTime.fromMillisecondsSinceEpoch( + r['measured_at_ms'] as int), l)}', + ), + subtitle: Text(_windowSummary(r)), + trailing: IconButton( + icon: const Icon(LucideIcons.trash2, size: 18), + onPressed: () async { + await LocalDb.deleteBpResearchCapture(r['id'] as int); + await _refresh(); + }, + ), + ), + const SizedBox(height: S.x4), + Text( + l?.bpResearchExportHint ?? + 'Export all captures as CSV from Your data › Export CSV ' + '(set “BP research captures”). Research data — it never ' + 'leaves the phone except through that file.', + style: c.textTheme.bodySmall, + ), + ], + ], + ), + ); + } + + /// A window is summarised as what it actually holds. A NULL stat is shown + /// as absent — a dash, never a zero, and never a value that would read as + /// a measurement. + static String _windowSummary(Map r) { + final onehz = r['onehz_rows']; + final beats = r['rr_beats']; + final hr = r['hr_mean']; + final rmssd = r['rmssd_ms']; + if (onehz == null && beats == null) { + return 'No band data in the window — stored as-is.'; + } + final parts = []; + if (hr != null) parts.add('HR ${hr.toStringAsFixed(0)} bpm'); + if (rmssd != null) parts.add('RMSSD ${rmssd.toStringAsFixed(0)} ms'); + parts.add('${onehz ?? 0} 1 Hz rows, ${beats ?? 0} beats'); + return parts.join(' · '); + } +} diff --git a/lib/ui2/profile/settings.dart b/lib/ui2/profile/settings.dart index 2d24b6d13..5946eff02 100644 --- a/lib/ui2/profile/settings.dart +++ b/lib/ui2/profile/settings.dart @@ -36,6 +36,7 @@ import '../../theme/theme_controller.dart'; import '../ui2.dart'; import 'alarm.dart'; import 'band_notifications.dart'; +import 'bp_research.dart'; import 'data.dart'; import 'gallery.dart'; import 'gestures.dart'; @@ -793,6 +794,13 @@ class MoreSettingsView extends StatelessWidget { 'Every component, at any text scale, in either ' 'theme', onTap: onGallery), + SetRow(LucideIcons.heartPulse, C.purple, + l?.settingsBpResearchRowTitle ?? 'BP research capture', + sub: l?.settingsBpResearchRowSub ?? + 'EXPERIMENTAL. Pair a cuff reading with the band ' + 'data of the same instant, for comparison ' + 'outside this app. Never a health feature', + onTap: () => goto(c, const BpResearchScreen())), SetRow(LucideIcons.code, C.n500, l?.settingsDeveloperModeRowTitle ?? 'Developer mode', value: on, chevron: false, onTap: onToggleDev), diff --git a/test/bp_research_capture_test.dart b/test/bp_research_capture_test.dart new file mode 100644 index 000000000..0d0e10d20 --- /dev/null +++ b/test/bp_research_capture_test.dart @@ -0,0 +1,77 @@ +// Unit tests for the pure window computation behind the BP research capture. +// +// The rules under test are the ones the storage and UI lean on: +// · a window with no band data is NULL, not zeroes; +// · a stat the window cannot honestly compute (no valid HR, too few +// beats for RMSSD) is absent, never zero; +// · rows outside the window are ignored, whatever their table's epoch +// base is (decoded_onehz.rec_ts is SECONDS, decoded_rr.rr_ts_ms is ms). + +import 'package:flutter_test/flutter_test.dart'; +import 'package:openstrap_edge/health/bp_research_capture.dart'; + +void main() { + const at = 1700000000000; // ms + + test('no band data in the window yields a NULL window, not zeroes', () { + final w = researchWindowFrom( + measuredAtMs: at, + onehzRows: const [], + rrRows: const [], + ); + expect(w, isNull); + }); + + test('rows outside the ±2 min window are ignored (seconds vs ms bases)', () { + final w = researchWindowFrom( + measuredAtMs: at, + onehzRows: [ + // rec_ts is epoch SECONDS. Inside the window (at/1000 ± 120). + {'rec_ts': at ~/ 1000, 'hr': 60}, + // 10 minutes before: outside, must not land in any stat. + {'rec_ts': at ~/ 1000 - 600, 'hr': 180}, + ], + rrRows: [ + // rr_ts_ms is epoch MS. Inside. + {'rr_ts_ms': at, 'rr_ms': 1000}, + ], + ); + expect(w, isNotNull); + expect(w!.onehzRows, 1); + expect(w.hrMean, 60.0); + expect(w.rrBeats, 1); + expect(w.rmssdMs, isNull); // one beat forms no successive difference + }); + + test('invalid HR rows do not drag the mean toward zero', () { + final w = researchWindowFrom( + measuredAtMs: at, + onehzRows: [ + {'rec_ts': at ~/ 1000, 'hr': 0}, + {'rec_ts': at ~/ 1000 - 1, 'hr': 58}, + {'rec_ts': at ~/ 1000 - 2, 'hr': 62}, + ], + rrRows: const [], + ); + expect(w!.onehzRows, 3); + expect(w.hrMean, 60.0); // 0 excluded as invalid, not averaged in + }); + + test('RMSSD over successive differences, min/max preserved', () { + final w = researchWindowFrom( + measuredAtMs: at, + onehzRows: const [], + rrRows: [ + {'rr_ts_ms': at - 3000, 'rr_ms': 1000}, + {'rr_ts_ms': at - 2000, 'rr_ms': 1100}, + {'rr_ts_ms': at - 1000, 'rr_ms': 900}, + ], + ); + expect(w!.rrBeats, 3); + expect(w.rrMsMin, 900.0); + expect(w.rrMsMax, 1100.0); + // diffs: +100, -200 → sqrt((100² + 200²)/2) = sqrt(25000) = 158.11… + expect(w.rmssdMs!, closeTo(158.11, 0.01)); + expect(w.hrMean, isNull); // no 1 Hz rows: absent, not zero + }); +} diff --git a/test/bp_research_isolation_test.dart b/test/bp_research_isolation_test.dart new file mode 100644 index 000000000..0a9b7607f --- /dev/null +++ b/test/bp_research_isolation_test.dart @@ -0,0 +1,60 @@ +// THE INVARIANT the BP research store exists to have. +// +// `bp_research_reference` / `bp_research_window` hold a cuff reading next to +// the band's own decoded data from the same instant. That pairing is exactly +// the input a cuffless-blood-pressure claim would be built from, which is +// the category that earned WHOOP an FDA Warning Letter in Jul 2025 — so the +// tables are fenced the same way `observation` is: nothing outside the +// allow-list may name either table, and the allow-list names the readers +// whose whole job is showing/exporting research data to the user. +// +// Layer 1 (structural source scan) reuses the mechanism +// observation_isolation_test.dart is built on. A violation is SILENT — no +// exception, just a wrist-vs-cuff regression one PR later — so the control +// is a test that goes red, not a comment that says don't. + +import 'dart:io'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:path/path.dart' as p; + +/// The ONLY files allowed to name either BP research table. +/// +/// Adding to this list is the deliberate act the invariant asks for. Before +/// you do: a READER for the dev screen or the CSV export is fine. Anything +/// in `lib/compute/`, anything that feeds `day_result`, `metric_series` or +/// a baseline, and anything that writes to HealthKit / Health Connect is +/// the thing this whole file exists to stop. +const _allowed = { + 'lib/data/db.dart', + 'lib/data/csv_export.dart', + 'lib/health/bp_research_capture.dart', + 'lib/ui2/profile/bp_research.dart', +}; + +void main() { + test('no file outside the allow-list names a BP research table', () { + final offenders = []; + final lib = Directory('lib'); + for (final f in lib.listSync(recursive: true).whereType()) { + if (!f.path.endsWith('.dart')) continue; + final rel = p.normalize(f.path); + final s = f.readAsStringSync(); + final hit = s.contains('bp_research_reference') || + s.contains('bp_research_window') || + s.contains('bpResearchCaptures') || + s.contains('putBpResearchCapture') || + s.contains('deleteBpResearchCapture'); + if (hit && !_allowed.contains(rel)) offenders.add(rel); + } + expect(offenders, isEmpty, + reason: 'files naming the BP research store must be on the allow-list ' + 'in test/bp_research_isolation_test.dart'); + }); + + test('the allow-list files themselves all exist', () { + for (final rel in _allowed) { + expect(File(rel).existsSync(), isTrue, reason: '$rel has vanished'); + } + }); +} From f08ab4f97f3e6792d7b2ae331397e1b65919fd09 Mon Sep 17 00:00:00 2001 From: Vibe Nuage Agent Date: Wed, 30 Sep 2026 21:05:35 +0000 Subject: [PATCH 02/11] style: align BP research screen with app typography (F.head, p.ink2), null-safe band summary Co-authored-by: BucciMobile --- lib/data/db.dart | 2 +- lib/ui2/profile/bp_research.dart | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/data/db.dart b/lib/data/db.dart index 7b5c5a8a8..686eb391d 100644 --- a/lib/data/db.dart +++ b/lib/data/db.dart @@ -33,7 +33,7 @@ import '../import/import_container.dart'; import 'coverage_resolver.dart' show CoverageInterval; import 'day_label.dart'; import 'journal_fields.dart'; -import '../health/bp_research_capture.dart' +import '../health/bp_research_capture.dart'; import 'live_coverage_policy.dart'; import 'med_store.dart'; import 'models.dart'; diff --git a/lib/ui2/profile/bp_research.dart b/lib/ui2/profile/bp_research.dart index 4c92b1562..df49f1427 100644 --- a/lib/ui2/profile/bp_research.dart +++ b/lib/ui2/profile/bp_research.dart @@ -129,6 +129,7 @@ class _BpResearchScreenState extends State { @override Widget build(BuildContext c) { final l = AppLocalizations.of(c); + final p = P.of(c); return Scaffold( appBar: AppBar( title: Text(l?.bpResearchTitle ?? 'BP research capture'), @@ -144,7 +145,7 @@ class _BpResearchScreenState extends State { 'this app. Nothing here is a health feature, nothing here ' 'feeds any score, and nothing here is ever blended with what ' 'the band measured.', - style: c.textTheme.bodySmall, + style: F.cap.copyWith(color: p.ink2, height: 1.5), ), const SizedBox(height: S.x4), TextField( @@ -194,7 +195,7 @@ class _BpResearchScreenState extends State { const SizedBox(height: S.x6), if (_rows.isNotEmpty) ...[ Text(l?.bpResearchHistory ?? 'Captures', - style: c.textTheme.titleMedium), + style: F.head), const SizedBox(height: S.x2), for (final r in _rows) ListTile( @@ -219,7 +220,7 @@ class _BpResearchScreenState extends State { 'Export all captures as CSV from Your data › Export CSV ' '(set “BP research captures”). Research data — it never ' 'leaves the phone except through that file.', - style: c.textTheme.bodySmall, + style: F.cap.copyWith(color: p.ink2, height: 1.5), ), ], ], @@ -235,12 +236,13 @@ class _BpResearchScreenState extends State { final beats = r['rr_beats']; final hr = r['hr_mean']; final rmssd = r['rmssd_ms']; + if (onehz == null && beats == null) { return 'No band data in the window — stored as-is.'; } final parts = []; - if (hr != null) parts.add('HR ${hr.toStringAsFixed(0)} bpm'); - if (rmssd != null) parts.add('RMSSD ${rmssd.toStringAsFixed(0)} ms'); + if (hr is num) parts.add('HR ${hr.toStringAsFixed(0)} bpm'); + if (rmssd is num) parts.add('RMSSD ${rmssd.toStringAsFixed(0)} ms'); parts.add('${onehz ?? 0} 1 Hz rows, ${beats ?? 0} beats'); return parts.join(' · '); } From f09e863a47d26484a431fa1e5ec902df2483eb3b Mon Sep 17 00:00:00 2001 From: Vibe Nuage Agent Date: Wed, 30 Sep 2026 21:32:28 +0000 Subject: [PATCH 03/11] test: register BpResearchScreen in _notComponents (Scaffold route over its own research table) Co-authored-by: BucciMobile --- test/ui2_tokens_test.dart | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/ui2_tokens_test.dart b/test/ui2_tokens_test.dart index 05a6a0a55..effee3e28 100644 --- a/test/ui2_tokens_test.dart +++ b/test/ui2_tokens_test.dart @@ -271,6 +271,11 @@ const _notComponents = { // Where the hydration notification lands: a Scaffold route that reads and // writes the day's journal metrics. The one control on it — FieldStepper — // IS in the gallery. + // The BP research capture route: a Scaffold that reads and writes its own + // research table (never the health stores) and only exists behind the dev + // toggle. A gallery case would have to mock the database; the capture flow + // is what bp_research_capture_test.dart covers on the data side. + 'BpResearchScreen', // The coach chat and its BYOK setup: Scaffold routes that own an engine, a // 120 s network call and the keychain. `CoachFigure` — the part a gallery can // actually hold — IS in it. From 76bc9b2691a0d39c13d31a81825a03689c9e1ecd Mon Sep 17 00:00:00 2001 From: Vibe Nuage Agent Date: Thu, 1 Oct 2026 02:14:46 +0000 Subject: [PATCH 04/11] fix(review): address CodeRabbit findings on PR #477 - bp_research tables ride _restoreTables/_salvageTables (parent before child) so backup/restore and salvage no longer drop cuff references - putBpResearchCapture normalizes a NULL device to '' (NULL never equals NULL in UNIQUE, so retakes without a device duplicated the reference) and deletes the replaced row's window explicitly (no PRAGMA foreign_keys, so ON DELETE CASCADE is inert and INSERT OR REPLACE would orphan the old window under a fresh id) - deleteBpResearchCapture takes the window row in the same transaction - the capture screen rejects dia >= sys (swapped pairs) and reports store vs refresh failures separately - l10n: bpResearchBadValue states the supported range instead of clinical impossibility; bpResearchExportHint no longer claims the CSV is the only way research data leaves the phone (full-db backup and opt-in health share also carry it) - new test/bp_research_db_test.dart covers all of the above against the real DB Co-authored-by: BucciMobile --- lib/data/db.dart | 44 +++++++++-- lib/l10n/app_en.arb | 4 +- lib/ui2/profile/bp_research.dart | 13 ++- test/bp_research_db_test.dart | 131 +++++++++++++++++++++++++++++++ 4 files changed, 182 insertions(+), 10 deletions(-) create mode 100644 test/bp_research_db_test.dart diff --git a/lib/data/db.dart b/lib/data/db.dart index 686eb391d..d4f3832f1 100644 --- a/lib/data/db.dart +++ b/lib/data/db.dart @@ -167,6 +167,8 @@ class LocalDb { /// flash as we ACK, so in practice this is the only copy of those days too. static const _salvageTables = [ // Hand-entered. The only copy that exists anywhere. + 'bp_research_reference', + 'bp_research_window', 'journal', 'journal_metric', 'journal_field_def', @@ -1665,13 +1667,30 @@ class LocalDb { static Future putBpResearchCapture(BpResearchCapture c) async { final db = await instance; await db.transaction((txn) async { + // NULL never equals NULL in a UNIQUE constraint, so a retake with no + // device text would duplicate the reference instead of replacing it. + // Normalizing to '' keeps (measured_at_ms, device) unique either way, + // and the window of the row being replaced is deleted explicitly — + // without PRAGMA foreign_keys the ON DELETE CASCADE never runs, and + // INSERT OR REPLACE assigns a fresh id that would orphan it. + await txn.rawDelete( + 'DELETE FROM bp_research_window WHERE reference_id IN ' + '(SELECT id FROM bp_research_reference ' + 'WHERE measured_at_ms = ? AND device = ?)', + [c.measuredAtMs, c.device ?? ''], + ); + await txn.rawDelete( + 'DELETE FROM bp_research_reference ' + 'WHERE measured_at_ms = ? AND device = ?', + [c.measuredAtMs, c.device ?? ''], + ); final id = await txn.rawInsert( - 'INSERT OR REPLACE INTO bp_research_reference ' + 'INSERT INTO bp_research_reference ' '(measured_at_ms, device, posture, conditions, systolic_mmhg, ' 'diastolic_mmhg, captured_at_ms) VALUES (?, ?, ?, ?, ?, ?, ?)', [ c.measuredAtMs, - c.device, + c.device ?? '', c.posture, c.conditions, c.systolicMmHg, @@ -1729,11 +1748,20 @@ class LocalDb { /// Delete one capture (dev screen). The window cascades. static Future deleteBpResearchCapture(int id) async { final db = await instance; - await db.delete( - 'bp_research_reference', - where: 'id = ?', - whereArgs: [id], - ); + // No PRAGMA foreign_keys here, so the window's ON DELETE CASCADE is + // inert — the delete has to take the window row explicitly. + await db.transaction((txn) async { + await txn.delete( + 'bp_research_window', + where: 'reference_id = ?', + whereArgs: [id], + ); + await txn.delete( + 'bp_research_reference', + where: 'id = ?', + whereArgs: [id], + ); + }); } /// Atomically claim [key] for a one-time OS notification fire. @@ -8744,6 +8772,8 @@ class LocalDb { // banked. They were also simply MISSING here until now — nutrition, // medication, strength sets, symptoms and routes did not survive a // backup/restore round trip at all, the same omission `wipeAll` documents. + 'bp_research_reference', + 'bp_research_window', 'journal', 'journal_metric', 'journal_field_def', diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index bfd8524f3..10be52412 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -12486,11 +12486,11 @@ "@bpResearchHistory": { "description": "Section header for the stored capture list" }, - "bpResearchBadValue": "That pair is not a blood pressure — check the numbers and try again. Nothing was stored.", + "bpResearchBadValue": "That pair is outside the range this app supports (systolic 50–300, diastolic 20–200 mmHg, diastolic below systolic) — it was not saved. Check the numbers and try again.", "@bpResearchBadValue": { "description": "Snackbar text for an out-of-bounds reference pair, which is rejected, never clamped" }, - "bpResearchExportHint": "Export all captures as CSV from Your data › Export CSV (set \"BP research captures\"). Research data — it never leaves the phone except through that file.", + "bpResearchExportHint": "Export all captures as CSV from Your data › Export CSV (set \"BP research captures\"). This is the dedicated export for BP research data; a full-database backup or an opt-in health share also contains it.", "@bpResearchExportHint": { "description": "Hint under the capture list explaining the CSV export path" } diff --git a/lib/ui2/profile/bp_research.dart b/lib/ui2/profile/bp_research.dart index df49f1427..269acc8c4 100644 --- a/lib/ui2/profile/bp_research.dart +++ b/lib/ui2/profile/bp_research.dart @@ -72,7 +72,8 @@ class _BpResearchScreenState extends State { sys < kResearchSystolicBounds.$1 || sys > kResearchSystolicBounds.$2 || dia < kResearchDiastolicBounds.$1 || - dia > kResearchDiastolicBounds.$2) { + dia > kResearchDiastolicBounds.$2 || + dia >= sys) { if (mounted) { ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text(l?.bpResearchBadValue ?? @@ -83,6 +84,7 @@ class _BpResearchScreenState extends State { return; } setState(() => _busy = true); + var stored = false; try { final now = DateTime.now(); final start = now.millisecondsSinceEpoch - kBpResearchWindowPreMs; @@ -118,9 +120,18 @@ class _BpResearchScreenState extends State { _conditions.text.trim().isEmpty ? null : _conditions.text.trim(), window: window, )); + stored = true; _sys.clear(); _dia.clear(); await _refresh(); + } catch (e) { + if (mounted) { + ScaffoldMessenger.of(context).showSnackBar(SnackBar( + content: Text(stored + ? 'Capture saved, but refreshing the history failed. ($e)' + : 'Capture failed \u2014 nothing was stored. ($e)'), + )); + } } finally { if (mounted) setState(() => _busy = false); } diff --git a/test/bp_research_db_test.dart b/test/bp_research_db_test.dart new file mode 100644 index 000000000..1c0494dd1 --- /dev/null +++ b/test/bp_research_db_test.dart @@ -0,0 +1,131 @@ +// The BP research store guarantees the review fixes made explicit: +// · a retake with NO device text replaces the reference instead of +// duplicating it (NULL never equals NULL in a UNIQUE constraint, so +// the store normalizes to '' — the rows must stay one, not two); +// · deleting a capture removes its window row in the same transaction +// (no PRAGMA foreign_keys here, so the ON DELETE CASCADE is inert); +// · a retake that now finds band data replaces the old window instead +// of orphaning it under the replaced reference's old id. +// Runs the REAL LocalDb over sqflite_common_ffi. +import 'package:flutter_test/flutter_test.dart'; +import 'package:openstrap_edge/data/db.dart'; +import 'package:openstrap_edge/health/bp_research_capture.dart'; +import 'package:path/path.dart' as p; +import 'package:sqflite_common_ffi/sqflite_ffi.dart'; + +const _at = 1700000000000; // ms + +BpResearchCapture _capture( + int measuredAtMs, { + String? device, + BpResearchWindow? window, +}) => + BpResearchCapture( + measuredAtMs: measuredAtMs, + device: device, + posture: 'sitting', + conditions: 'rest', + systolicMmHg: 120, + diastolicMmHg: 80, + capturedAtMs: measuredAtMs, + window: window, + ); + +const _win = BpResearchWindow( + windowStartMs: _at - 120000, + windowEndMs: _at + 120000, + onehzRows: 240, + rrBeats: 200, + hrMean: 62.5, + rrMsMean: 960, + rrMsMin: 800, + rrMsMax: 1100, + rmssdMs: 42, + metaJson: null, +); + +void main() { + setUpAll(() async { + sqfliteFfiInit(); + databaseFactory = databaseFactoryFfi; + LocalDb.dbName = 'bp_research_db_test.db'; + final dir = await databaseFactory.getDatabasesPath(); + await databaseFactory.deleteDatabase(p.join(dir, LocalDb.dbName)); + }); + + tearDownAll(() async { + final db = await LocalDb.instance; + await db.close(); + }); + + test('a retake with no device replaces the reference, not duplicates it', + () async { + await LocalDb.putBpResearchCapture(_capture(_at)); + await LocalDb.putBpResearchCapture(_capture(_at, window: _win)); + final rows = await LocalDb.bpResearchCaptures(); + expect(rows, hasLength(1)); + expect(rows.first['device'], ''); + expect(rows.first['hr_mean'], 62.5); + }); + + test('a named device stays distinct from the no-device row', () async { + await LocalDb.putBpResearchCapture(_capture(_at, device: 'omron')); + final rows = await LocalDb.bpResearchCaptures(); + expect(rows, hasLength(2)); // the '' row from the previous test + omron + expect(rows.where((r) => r['device'] == 'omron'), hasLength(1)); + }); + + test('delete removes the window row too (the SQL cascade is inert)', + () async { + final db = await LocalDb.instance; + final before = + await db.rawQuery('SELECT COUNT(*) c FROM bp_research_window'); + expect(before.first['c'], greaterThan(0)); + final refs = await db + .rawQuery('SELECT id FROM bp_research_reference WHERE device = ?', + ['omron']); + await LocalDb.deleteBpResearchCapture(refs.first['id'] as int); + final orphaned = await db.rawQuery( + 'SELECT COUNT(*) c FROM bp_research_window ' + 'WHERE reference_id NOT IN (SELECT id FROM bp_research_reference)', + ); + expect(orphaned.first['c'], 0); + }); + + test('a retake that now finds band data replaces the absent window', + () async { + final later = _at + 60000; + await LocalDb.putBpResearchCapture(_capture(later)); + await LocalDb.putBpResearchCapture(_capture(later, window: _win)); + final db = await LocalDb.instance; + final windows = await db.rawQuery( + 'SELECT COUNT(*) c FROM bp_research_window ' + 'WHERE reference_id IN ' + '(SELECT id FROM bp_research_reference WHERE measured_at_ms = ?)', + [later]); + expect(windows.first['c'], 1); + final orphaned = await db.rawQuery( + 'SELECT COUNT(*) c FROM bp_research_window ' + 'WHERE reference_id NOT IN (SELECT id FROM bp_research_reference)', + ); + expect(orphaned.first['c'], 0); + }); + + test('the research tables ride the backup restore and salvage lists', + () async { + expect(LocalDb.restoreTablesForTest, contains('bp_research_reference')); + expect(LocalDb.restoreTablesForTest, contains('bp_research_window')); + expect(LocalDb.salvageTablesForTest, contains('bp_research_reference')); + expect(LocalDb.salvageTablesForTest, contains('bp_research_window')); + // Parent before child, in both lists. + int posOf(List l, String t) => l.indexOf(t); + expect( + posOf(LocalDb.restoreTablesForTest, 'bp_research_reference'), + lessThan(posOf(LocalDb.restoreTablesForTest, 'bp_research_window')), + ); + expect( + posOf(LocalDb.salvageTablesForTest, 'bp_research_reference'), + lessThan(posOf(LocalDb.salvageTablesForTest, 'bp_research_window')), + ); + }); +} From 66420981ab301fa77f3ed523a6fba17f0f50b5d1 Mon Sep 17 00:00:00 2001 From: Vibe Nuage Agent Date: Thu, 1 Oct 2026 02:57:19 +0000 Subject: [PATCH 05/11] fix(review): merge BP research captures by natural key on restore CodeRabbit follow-up on PR #477: the generic importer REPLACEs on the row's primary key, so a foreign export's bp_research_reference id=1 would eat this install's unrelated id=1 capture (AUTOINCREMENT ids are device-local), and the imported window would ride a stale reference_id. Both tables now take a dedicated merge branch in _mergeFromDbFile: references REPLACE on their natural UNIQUE (measured_at_ms, device) key with the source id dropped, a source->dest id map is built as they land, and each window row is remapped onto the destination reference and REPLACEd on its PK. A capture whose incoming window is absent keeps the window it already had; re-import converges. Covered by two new DB tests (natural-key collision with a foreign id=1, idempotent re-import). Co-authored-by: BucciMobile --- lib/data/db.dart | 103 +++++++++++++++++++++++++++++ test/bp_research_db_test.dart | 118 ++++++++++++++++++++++++++++++++++ 2 files changed, 221 insertions(+) diff --git a/lib/data/db.dart b/lib/data/db.dart index d4f3832f1..db2fa4369 100644 --- a/lib/data/db.dart +++ b/lib/data/db.dart @@ -8870,8 +8870,21 @@ class LocalDb { final info = await db.rawQuery('PRAGMA table_info($t)'); return {for (final c in info) (c['name'] as String)}; } + Future srcHasTable(String t, Database s) async { + // A salvage source may predate the window table; `SELECT *` on a + // missing table throws, so probe for its existence first. + final rows = await s.rawQuery( + "SELECT name FROM sqlite_master WHERE type='table' AND name = ?", + [t], + ); + return rows.isNotEmpty; + } final counts = {}; + // SOURCE→DEST id map for the BP research reference merge below: the + // window rows of a foreign export name their reference by the SOURCE + // database's AUTOINCREMENT id, which is meaningless here. + final bpIdMap = {}; // DISTINCT DAYS ACTUALLY WRITTEN — the number the caller reports as // "N days imported". // @@ -8930,6 +8943,96 @@ class LocalDb { if (e.isNoSuchTableError()) continue; rethrow; } + // BP RESEARCH CAPTURES MERGE BY NATURAL KEY, NOT BY SOURCE ID. The + // reference's `id` is a device-local AUTOINCREMENT and the window's + // `reference_id` names it, so the generic REPLACE-by-PK path would + // let a foreign export's id=1 eat this install's id=1 capture. Both + // tables are hand-typed and tiny (nothing writes them but the dev + // screen), so a dedicated two-query merge beats threading a special + // case through the paged loop: the reference REPLACEs on its natural + // UNIQUE (measured_at_ms, device) key, the window follows onto the + // DESTINATION id, and a capture whose incoming window is absent + // keeps the window it already had. Re-import converges. + if (t == 'bp_research_reference' || t == 'bp_research_window') { + // The reference pass builds the id map; the window pass that + // follows (references merge first) only consumes it. + try { + final refCols = await destCols('bp_research_reference'); + final winCols = await destCols('bp_research_window'); + final srcRefs = t == 'bp_research_reference' + ? await src.rawQuery('SELECT * FROM bp_research_reference') + : const >[]; + final srcWins = t == 'bp_research_window' && + await srcHasTable('bp_research_window', src) + ? await src.rawQuery('SELECT * FROM bp_research_window') + : const >[]; + await db.transaction((txn) async { + for (final r in srcRefs) { + final row = { + for (final e in r.entries) + if (refCols.contains(e.key)) e.key: e.value, + }; + final srcId = row.remove('id'); + final destId = await txn.rawInsert( + 'INSERT OR REPLACE INTO bp_research_reference ' + '(measured_at_ms, device, posture, conditions, ' + 'systolic_mmhg, diastolic_mmhg, captured_at_ms) ' + 'VALUES (?, ?, ?, ?, ?, ?, ?)', + [ + row['measured_at_ms'], + (row['device'] as String?) ?? '', + row['posture'], + row['conditions'], + row['systolic_mmhg'], + row['diastolic_mmhg'], + row['captured_at_ms'], + ], + ); + if (srcId is num) { + bpIdMap[srcId.toInt()] = destId; + } + } + for (final w in srcWins) { + final row = { + for (final e in w.entries) + if (winCols.contains(e.key)) e.key: e.value, + }; + final destRef = row.remove('reference_id'); + final mapped = destRef is num + ? bpIdMap[destRef.toInt()] + : null; + if (mapped == null) continue; + await txn.rawInsert( + 'INSERT OR REPLACE INTO bp_research_window ' + '(reference_id, window_start_ms, window_end_ms, ' + 'onehz_rows, rr_beats, hr_mean, rr_ms_mean, rr_ms_min, ' + 'rr_ms_max, rmssd_ms, meta_json) ' + 'VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', + [ + mapped, + row['window_start_ms'], + row['window_end_ms'], + row['onehz_rows'], + row['rr_beats'], + row['hr_mean'], + row['rr_ms_mean'], + row['rr_ms_min'], + row['rr_ms_max'], + row['rmssd_ms'], + row['meta_json'], + ], + ); + } + }); + counts[t] = t == 'bp_research_reference' + ? srcRefs.length + : srcWins.length; + } catch (_) { + if (!tolerant) rethrow; + counts[t] = 0; + } + continue; + } if (t == 'day_result') importedDays = {}; if (firstPage.isEmpty) { counts[t] = 0; diff --git a/test/bp_research_db_test.dart b/test/bp_research_db_test.dart index 1c0494dd1..0643eaf00 100644 --- a/test/bp_research_db_test.dart +++ b/test/bp_research_db_test.dart @@ -128,4 +128,122 @@ void main() { lessThan(posOf(LocalDb.salvageTablesForTest, 'bp_research_window')), ); }); + + // A foreign export's AUTOINCREMENT ids are meaningless on this install: + // its id=1 must never REPLACE an unrelated local capture that happens to + // hold id=1. The merge keys references on (measured_at_ms, device) and + // remaps each window onto the DESTINATION reference id. + test('restore merges captures by natural key, never by source id', + () async { + // Start from a clean store: earlier tests in this file leave rows + // behind, and this one asserts exact row sets. + final db0 = await LocalDb.instance; + await db0.delete('bp_research_window'); + await db0.delete('bp_research_reference'); + // Local state: one capture (id=1 by AUTOINCREMENT) plus its window. + await LocalDb.putBpResearchCapture(_capture(_at, device: 'local')); + // A foreign export whose DIFFERENT capture also carries id=1. + final srcPath = + p.join(await databaseFactory.getDatabasesPath(), 'bp_foreign.db'); + await databaseFactory.deleteDatabase(srcPath); + final src = await databaseFactory.openDatabase(srcPath); + await src.execute( + 'CREATE TABLE bp_research_reference (' + 'id INTEGER PRIMARY KEY AUTOINCREMENT, ' + 'measured_at_ms INTEGER NOT NULL, ' + 'device TEXT, posture TEXT, conditions TEXT, ' + 'systolic_mmhg REAL NOT NULL, diastolic_mmhg REAL NOT NULL, ' + 'captured_at_ms INTEGER NOT NULL, ' + 'UNIQUE (measured_at_ms, device))'); + await src.execute( + 'CREATE TABLE bp_research_window (' + 'reference_id INTEGER NOT NULL PRIMARY KEY, ' + 'window_start_ms INTEGER NOT NULL, window_end_ms INTEGER NOT NULL, ' + 'onehz_rows INTEGER, rr_beats INTEGER, hr_mean REAL, rr_ms_mean REAL, ' + 'rr_ms_min REAL, rr_ms_max REAL, rmssd_ms REAL, meta_json TEXT)'); + await src.insert('bp_research_reference', { + 'id': 1, // deliberately collides with the local capture's id + 'measured_at_ms': _at + 60000, + 'device': 'local', + 'posture': 'sitting', + 'conditions': 'rest', + 'systolic_mmhg': 130, + 'diastolic_mmhg': 85, + 'captured_at_ms': _at + 60000, + }); + await src.insert('bp_research_window', { + 'reference_id': 1, + 'window_start_ms': _at + 60000 - 120000, + 'window_end_ms': _at + 60000 + 120000, + 'onehz_rows': 240, + 'rr_beats': 200, + 'hr_mean': 71.0, + }); + await src.close(); + + final counts = await LocalDb.importFromDbFile(srcPath); + expect(counts['bp_research_reference'], 1); + expect(counts['bp_research_window'], 1); + + final db = await LocalDb.instance; + // Both captures survive: the foreign id=1 did not eat the local one. + final refs = await db.rawQuery( + 'SELECT measured_at_ms, systolic_mmhg FROM bp_research_reference ' + 'ORDER BY measured_at_ms'); + expect(refs, hasLength(2)); + expect(refs[0]['measured_at_ms'], _at); + expect(refs[0]['systolic_mmhg'], 120.0); + expect(refs[1]['measured_at_ms'], _at + 60000); + expect(refs[1]['systolic_mmhg'], 130.0); + // The imported window rides the imported reference's DESTINATION id, + // and no window is orphaned. + final orphaned = await db.rawQuery( + 'SELECT COUNT(*) c FROM bp_research_window ' + 'WHERE reference_id NOT IN (SELECT id FROM bp_research_reference)', + ); + expect(orphaned.first['c'], 0); + final importedWin = await db.rawQuery( + 'SELECT hr_mean FROM bp_research_window w ' + 'JOIN bp_research_reference r ON r.id = w.reference_id ' + 'WHERE r.measured_at_ms = ?', [_at + 60000]); + expect(importedWin.first['hr_mean'], 71.0); + await databaseFactory.deleteDatabase(srcPath); + }); + + test('a re-import of the same export converges (idempotent merge)', + () async { + final db0 = await LocalDb.instance; + await db0.delete('bp_research_window'); + await db0.delete('bp_research_reference'); + final srcPath = + p.join(await databaseFactory.getDatabasesPath(), 'bp_foreign2.db'); + await databaseFactory.deleteDatabase(srcPath); + final src = await databaseFactory.openDatabase(srcPath); + await src.execute( + 'CREATE TABLE bp_research_reference (' + 'id INTEGER PRIMARY KEY AUTOINCREMENT, ' + 'measured_at_ms INTEGER NOT NULL, ' + 'device TEXT, posture TEXT, conditions TEXT, ' + 'systolic_mmhg REAL NOT NULL, diastolic_mmhg REAL NOT NULL, ' + 'captured_at_ms INTEGER NOT NULL, ' + 'UNIQUE (measured_at_ms, device))'); + await src.insert('bp_research_reference', { + 'id': 1, + 'measured_at_ms': _at + 120000, + 'device': '', + 'systolic_mmhg': 118, + 'diastolic_mmhg': 76, + 'captured_at_ms': _at + 120000, + }); + await src.close(); + + await LocalDb.importFromDbFile(srcPath); + await LocalDb.importFromDbFile(srcPath); + final db = await LocalDb.instance; + final n = (await db.rawQuery( + 'SELECT COUNT(*) c FROM bp_research_reference ' + 'WHERE measured_at_ms = ?', [_at + 120000])).first['c']; + expect(n, 1); + await databaseFactory.deleteDatabase(srcPath); + }); } From 2001787178c6ac2f7808a0e2a0bc9231117c888c Mon Sep 17 00:00:00 2001 From: Vibe Nuage Agent Date: Thu, 1 Oct 2026 03:07:17 +0000 Subject: [PATCH 06/11] fix(review): keep the destination id on a colliding BP capture restore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit INSERT OR REPLACE on the natural key minted a fresh AUTOINCREMENT id, stranding the local window row under the old reference_id (no FK cascade here). The merge now UPDATES the colliding reference in place and keeps its id — the incoming window re-attaches to it, and a capture whose incoming window is absent genuinely keeps the window it had. New DB test covers the collision-without-window case. Co-authored-by: BucciMobile --- lib/data/db.dart | 59 ++++++++++++++++++++++++++--------- test/bp_research_db_test.dart | 59 +++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 14 deletions(-) diff --git a/lib/data/db.dart b/lib/data/db.dart index db2fa4369..d58c657b0 100644 --- a/lib/data/db.dart +++ b/lib/data/db.dart @@ -8973,21 +8973,52 @@ class LocalDb { if (refCols.contains(e.key)) e.key: e.value, }; final srcId = row.remove('id'); - final destId = await txn.rawInsert( - 'INSERT OR REPLACE INTO bp_research_reference ' - '(measured_at_ms, device, posture, conditions, ' - 'systolic_mmhg, diastolic_mmhg, captured_at_ms) ' - 'VALUES (?, ?, ?, ?, ?, ?, ?)', - [ - row['measured_at_ms'], - (row['device'] as String?) ?? '', - row['posture'], - row['conditions'], - row['systolic_mmhg'], - row['diastolic_mmhg'], - row['captured_at_ms'], - ], + // KEEP the destination id on collision. `INSERT OR + // REPLACE` would delete the colliding local row and mint a + // fresh AUTOINCREMENT id — stranding the local window row + // under the old reference_id with no FK cascade to take + // it, exactly the orphan putBpResearchCapture avoids by + // deleting first. UPDATE preserves the id the window is + // about to be re-attached to. + final device = (row['device'] as String?) ?? ''; + final existing = await txn.rawQuery( + 'SELECT id FROM bp_research_reference ' + 'WHERE measured_at_ms = ? AND device = ?', + [row['measured_at_ms'], device], ); + final int destId; + if (existing.isNotEmpty) { + destId = (existing.first['id'] as num).toInt(); + await txn.rawUpdate( + 'UPDATE bp_research_reference SET posture = ?, ' + 'conditions = ?, systolic_mmhg = ?, diastolic_mmhg = ?, ' + 'captured_at_ms = ? WHERE id = ?', + [ + row['posture'], + row['conditions'], + row['systolic_mmhg'], + row['diastolic_mmhg'], + row['captured_at_ms'], + destId, + ], + ); + } else { + destId = await txn.rawInsert( + 'INSERT INTO bp_research_reference ' + '(measured_at_ms, device, posture, conditions, ' + 'systolic_mmhg, diastolic_mmhg, captured_at_ms) ' + 'VALUES (?, ?, ?, ?, ?, ?, ?)', + [ + row['measured_at_ms'], + device, + row['posture'], + row['conditions'], + row['systolic_mmhg'], + row['diastolic_mmhg'], + row['captured_at_ms'], + ], + ); + } if (srcId is num) { bpIdMap[srcId.toInt()] = destId; } diff --git a/test/bp_research_db_test.dart b/test/bp_research_db_test.dart index 0643eaf00..5403fede5 100644 --- a/test/bp_research_db_test.dart +++ b/test/bp_research_db_test.dart @@ -210,6 +210,65 @@ void main() { await databaseFactory.deleteDatabase(srcPath); }); + test('a colliding restore keeps the destination id and its window', + () async { + final db0 = await LocalDb.instance; + await db0.delete('bp_research_window'); + await db0.delete('bp_research_reference'); + // Local capture WITH a window. + await LocalDb.putBpResearchCapture( + _capture(_at, device: 'local', window: _win)); + final localId = (await db0.rawQuery( + 'SELECT id FROM bp_research_reference')).first['id'] as int; + + // A foreign export of the SAME instant (same natural key) with no + // window row: the capture's fields update, the window survives. + final srcPath = + p.join(await databaseFactory.getDatabasesPath(), 'bp_foreign3.db'); + await databaseFactory.deleteDatabase(srcPath); + final src = await databaseFactory.openDatabase(srcPath); + await src.execute( + 'CREATE TABLE bp_research_reference (' + 'id INTEGER PRIMARY KEY AUTOINCREMENT, ' + 'measured_at_ms INTEGER NOT NULL, ' + 'device TEXT, posture TEXT, conditions TEXT, ' + 'systolic_mmhg REAL NOT NULL, diastolic_mmhg REAL NOT NULL, ' + 'captured_at_ms INTEGER NOT NULL, ' + 'UNIQUE (measured_at_ms, device))'); + await src.insert('bp_research_reference', { + 'id': 7, + 'measured_at_ms': _at, + 'device': 'local', + 'posture': 'standing', + 'conditions': 'after exercise', + 'systolic_mmhg': 140, + 'diastolic_mmhg': 90, + 'captured_at_ms': _at + 1000, + }); + await src.close(); + + await LocalDb.importFromDbFile(srcPath); + + final db = await LocalDb.instance; + final refs = await db.rawQuery( + 'SELECT id, posture, systolic_mmhg FROM bp_research_reference'); + expect(refs, hasLength(1)); + // The destination id is KEPT, so the window stays attached. + expect(refs.first['id'], localId); + expect(refs.first['posture'], 'standing'); + expect(refs.first['systolic_mmhg'], 140.0); + final orphaned = await db.rawQuery( + 'SELECT COUNT(*) c FROM bp_research_window ' + 'WHERE reference_id NOT IN (SELECT id FROM bp_research_reference)', + ); + expect(orphaned.first['c'], 0); + final win = await db.rawQuery( + 'SELECT hr_mean FROM bp_research_window WHERE reference_id = ?', + [localId]); + expect(win.first['hr_mean'], 62.5); + await databaseFactory.deleteDatabase(srcPath); + }); + test('a re-import of the same export converges (idempotent merge)', () async { final db0 = await LocalDb.instance; From 5fdc2205e5b255cdafb213b2f3f22c5c2e75f9ad Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Fri, 2 Oct 2026 08:06:08 +0530 Subject: [PATCH 07/11] bp research: fill the window once the band has synced it, atomic restore, trim comments --- lib/data/csv_export.dart | 6 +- lib/data/db.dart | 282 ++++++++++++--------------- lib/health/bp_research_capture.dart | 76 ++------ lib/l10n/app_en.arb | 16 +- lib/ui2/profile/bp_research.dart | 97 ++++----- lib/ui2/profile/settings.dart | 5 +- test/bp_research_db_test.dart | 47 +++++ test/bp_research_isolation_test.dart | 26 +-- test/ui2_tokens_test.dart | 7 +- 9 files changed, 244 insertions(+), 318 deletions(-) diff --git a/lib/data/csv_export.dart b/lib/data/csv_export.dart index 5c1b803e1..f62981815 100644 --- a/lib/data/csv_export.dart +++ b/lib/data/csv_export.dart @@ -286,11 +286,7 @@ const kCsvExportSets = [ CsvExportSet( name: 'bp_research', title: 'BP research captures (EXPERIMENTAL)', - // Paired cuff reference readings plus the band window frozen around - // each instant. Research data, not health data: never blended, never a - // training input, and absent stats stay EMPTY here exactly as they are - // NULL in the store — a spreadsheet cannot tell a zero from a reading - // afterwards, and a column of zeroes is a fabrication. + // Cuff readings plus their band window. Absent stats stay empty. columns: [ 'measured_at_ms', 'device', diff --git a/lib/data/db.dart b/lib/data/db.dart index d58c657b0..19f567fb4 100644 --- a/lib/data/db.dart +++ b/lib/data/db.dart @@ -1078,15 +1078,8 @@ class LocalDb { await _createEcgTables(db); } if (oldV < 55) { - // BP research capture: paired cuff reference readings plus the - // band's own decoded 1 Hz / R-R window frozen around the - // measurement instant, for out-of-app comparison only. Two new - // tables, CREATE TABLE IF NOT EXISTS and NOTHING else — no - // backfill, no rewrite, no ADD COLUMN — so a throw here has - // nothing to roll back onto (invariant 11). Ships without a - // kAlgoVersion bump: nothing derived moves, and nothing derived - // may ever read these (see the guard comment in - // [_createBpResearch]). + // BP research capture tables. Create-only, nothing derived reads + // them, so no kAlgoVersion bump. await _createBpResearch(db); } }, @@ -1179,6 +1172,7 @@ class LocalDb { 'INTEGER NOT NULL DEFAULT 0', ); await _createEcgTables(db); + await _createBpResearch(db); // Views LAST — they depend on metric_series / day_result / baselines / sessions // / notifications all existing. DROP+CREATE so a shape change takes effect. await _ensureCoachViews(db); @@ -1599,25 +1593,10 @@ class LocalDb { } - /// BP research capture store (schema rung 55). - /// - /// EXPERIMENTAL, DEVELOPER-ONLY, and it stays that way. A cuff reading the - /// user types in next to the band data of the same instant is exactly the - /// pairing the `imported_measurement` guard exists to prevent becoming an - /// input: the moment a wrist series and a cuff series are regressed against - /// each other ON DEVICE, this app is making a cuffless-blood-pressure - /// claim from an uncleared device. So this is a SEPARATE store, read by - /// exactly one dev screen and one CSV export, and — like - /// `imported_measurement` and `observation` — the isolation is structural: - /// nothing in `compute/`, nothing that feeds `day_result` or - /// `metric_series`, and nothing that writes to HealthKit / Health Connect - /// may name either table. `bp_research_isolation_test.dart` fails the moment - /// anyone does. - /// - /// Captures are idempotent on `(measured_at_ms, device)`: retaking the same - /// cuff reading at the same instant re-states the window rather than - /// duplicating it. Legitimate repeat measurements minutes apart are - /// different instants and both stay. + /// BP research capture store (dev mode only). Read only by the dev screen + /// and the CSV export; bp_research_isolation_test.dart keeps it that way. + /// Foreign keys are off in this database, so the window's cascade is inert + /// and every delete takes the window row explicitly. static Future _createBpResearch(Database db) async { await db.execute(''' CREATE TABLE IF NOT EXISTS bp_research_reference ( @@ -1638,9 +1617,6 @@ class LocalDb { REFERENCES bp_research_reference(id) ON DELETE CASCADE, window_start_ms INTEGER NOT NULL, window_end_ms INTEGER NOT NULL, - -- NULL-safe by design: the band may have had nothing to say at that - -- instant (not worn, not synced yet), and a missing window is recorded - -- as missing — never as zeroes. onehz_rows INTEGER, rr_beats INTEGER, hr_mean REAL, @@ -1657,22 +1633,12 @@ class LocalDb { ); } - /// Insert one cuff reference reading plus its frozen band window. - /// - /// Pure write; the caller computes the window stats (see - /// `lib/health/bp_research_capture.dart`). Idempotent on - /// `(measured_at_ms, device)`: `INSERT OR REPLACE` on the reference, then - /// the window row is restated in the same transaction so a retake can never - /// leave an old window under a new reference. + /// Insert one cuff reading plus its window, replacing any capture with the + /// same `(measured_at_ms, device)`. static Future putBpResearchCapture(BpResearchCapture c) async { final db = await instance; await db.transaction((txn) async { - // NULL never equals NULL in a UNIQUE constraint, so a retake with no - // device text would duplicate the reference instead of replacing it. - // Normalizing to '' keeps (measured_at_ms, device) unique either way, - // and the window of the row being replaced is deleted explicitly — - // without PRAGMA foreign_keys the ON DELETE CASCADE never runs, and - // INSERT OR REPLACE assigns a fresh id that would orphan it. + // device is stored as '' rather than NULL so the UNIQUE key holds. await txn.rawDelete( 'DELETE FROM bp_research_window WHERE reference_id IN ' '(SELECT id FROM bp_research_reference ' @@ -1698,16 +1664,9 @@ class LocalDb { c.capturedAtMs, ], ); - // A capture with no band data stores NO window row — the LEFT JOIN in - // [bpResearchCaptures] renders it as an empty window, and `NOT NULL` - // on the window bounds is what keeps a half-written window out of the - // store. A retake that now finds band data replaces the absent row. + // No band data = no window row. final w = c.window; - if (w == null) { - await txn.rawDelete( - 'DELETE FROM bp_research_window WHERE reference_id = ?', [id]); - return; - } + if (w == null) return; await txn.rawInsert( 'INSERT OR REPLACE INTO bp_research_window ' '(reference_id, window_start_ms, window_end_ms, onehz_rows, ' @@ -1745,11 +1704,71 @@ class LocalDb { '''); } - /// Delete one capture (dev screen). The window cascades. + /// Fill the window of every capture that has none yet, once the primary + /// band's synced 1 Hz data reaches the window end. At capture time the + /// window is still in the future and the band hasn't offloaded it, so it + /// can only be read later. Returns that data edge in epoch ms (null when + /// nothing is decoded): a windowless capture before it is still pending, + /// one at or after it had no band data. + static Future fillBpResearchWindows() async { + final db = await instance; + final edgeSec = Sqflite.firstIntValue(await db.rawQuery( + 'SELECT MAX(rec_ts) FROM decoded_onehz WHERE device_id = ?', + [kPrimaryDeviceId], + )); + if (edgeSec == null) return null; + final edgeMs = edgeSec * 1000; + final pending = await db.rawQuery( + 'SELECT r.id, r.measured_at_ms FROM bp_research_reference r ' + 'LEFT JOIN bp_research_window w ON w.reference_id = r.id ' + 'WHERE w.reference_id IS NULL AND r.measured_at_ms <= ?', + [edgeMs - kBpResearchWindowPostMs], + ); + for (final r in pending) { + final at = r['measured_at_ms'] as int; + final start = at - kBpResearchWindowPreMs; + final end = at + kBpResearchWindowPostMs; + final w = researchWindowFrom( + measuredAtMs: at, + onehzRows: await db.rawQuery( + 'SELECT rec_ts, hr FROM decoded_onehz ' + 'WHERE device_id = ? AND rec_ts >= ? AND rec_ts <= ? ' + 'ORDER BY rec_ts ASC', + [kPrimaryDeviceId, start ~/ 1000, end ~/ 1000], + ), + // Beats of one record share rr_ts_ms; beat_index orders them. + rrRows: await db.rawQuery( + 'SELECT rr_ts_ms, rr_ms FROM decoded_rr ' + 'WHERE device_id = ? AND rr_ts_ms >= ? AND rr_ts_ms <= ? ' + 'ORDER BY rr_ts_ms ASC, beat_index ASC', + [kPrimaryDeviceId, start, end], + ), + ); + if (w == null) continue; + await db.insert( + 'bp_research_window', + { + 'reference_id': r['id'], + 'window_start_ms': w.windowStartMs, + 'window_end_ms': w.windowEndMs, + 'onehz_rows': w.onehzRows, + 'rr_beats': w.rrBeats, + 'hr_mean': w.hrMean, + 'rr_ms_mean': w.rrMsMean, + 'rr_ms_min': w.rrMsMin, + 'rr_ms_max': w.rrMsMax, + 'rmssd_ms': w.rmssdMs, + 'meta_json': w.metaJson, + }, + conflictAlgorithm: ConflictAlgorithm.replace, + ); + } + return edgeMs; + } + + /// Delete one capture and its window. static Future deleteBpResearchCapture(int id) async { final db = await instance; - // No PRAGMA foreign_keys here, so the window's ON DELETE CASCADE is - // inert — the delete has to take the window row explicitly. await db.transaction((txn) async { await txn.delete( 'bp_research_window', @@ -8870,21 +8889,8 @@ class LocalDb { final info = await db.rawQuery('PRAGMA table_info($t)'); return {for (final c in info) (c['name'] as String)}; } - Future srcHasTable(String t, Database s) async { - // A salvage source may predate the window table; `SELECT *` on a - // missing table throws, so probe for its existence first. - final rows = await s.rawQuery( - "SELECT name FROM sqlite_master WHERE type='table' AND name = ?", - [t], - ); - return rows.isNotEmpty; - } final counts = {}; - // SOURCE→DEST id map for the BP research reference merge below: the - // window rows of a foreign export name their reference by the SOURCE - // database's AUTOINCREMENT id, which is meaningless here. - final bpIdMap = {}; // DISTINCT DAYS ACTUALLY WRITTEN — the number the caller reports as // "N days imported". // @@ -8943,48 +8949,41 @@ class LocalDb { if (e.isNoSuchTableError()) continue; rethrow; } - // BP RESEARCH CAPTURES MERGE BY NATURAL KEY, NOT BY SOURCE ID. The - // reference's `id` is a device-local AUTOINCREMENT and the window's - // `reference_id` names it, so the generic REPLACE-by-PK path would - // let a foreign export's id=1 eat this install's id=1 capture. Both - // tables are hand-typed and tiny (nothing writes them but the dev - // screen), so a dedicated two-query merge beats threading a special - // case through the paged loop: the reference REPLACEs on its natural - // UNIQUE (measured_at_ms, device) key, the window follows onto the - // DESTINATION id, and a capture whose incoming window is absent - // keeps the window it already had. Re-import converges. - if (t == 'bp_research_reference' || t == 'bp_research_window') { - // The reference pass builds the id map; the window pass that - // follows (references merge first) only consumes it. + // BP research captures merge on (measured_at_ms, device), never by + // source id, and keep the destination id so a local window stays + // attached. Both tables go in one transaction on the reference pass. + if (t == 'bp_research_window') continue; + if (t == 'bp_research_reference') { try { - final refCols = await destCols('bp_research_reference'); + final refs = + await src.rawQuery('SELECT * FROM bp_research_reference'); + var wins = const >[]; + try { + wins = await src.rawQuery('SELECT * FROM bp_research_window'); + } on DatabaseException catch (e) { + if (!e.isNoSuchTableError()) rethrow; + } + final winBySrcId = { + for (final w in wins) + if (w['reference_id'] is num) + (w['reference_id'] as num).toInt(): w, + }; final winCols = await destCols('bp_research_window'); - final srcRefs = t == 'bp_research_reference' - ? await src.rawQuery('SELECT * FROM bp_research_reference') - : const >[]; - final srcWins = t == 'bp_research_window' && - await srcHasTable('bp_research_window', src) - ? await src.rawQuery('SELECT * FROM bp_research_window') - : const >[]; + var winCount = 0; await db.transaction((txn) async { - for (final r in srcRefs) { - final row = { - for (final e in r.entries) - if (refCols.contains(e.key)) e.key: e.value, - }; - final srcId = row.remove('id'); - // KEEP the destination id on collision. `INSERT OR - // REPLACE` would delete the colliding local row and mint a - // fresh AUTOINCREMENT id — stranding the local window row - // under the old reference_id with no FK cascade to take - // it, exactly the orphan putBpResearchCapture avoids by - // deleting first. UPDATE preserves the id the window is - // about to be re-attached to. - final device = (row['device'] as String?) ?? ''; + for (final r in refs) { + final device = (r['device'] as String?) ?? ''; + final values = [ + r['posture'], + r['conditions'], + r['systolic_mmhg'], + r['diastolic_mmhg'], + r['captured_at_ms'], + ]; final existing = await txn.rawQuery( 'SELECT id FROM bp_research_reference ' 'WHERE measured_at_ms = ? AND device = ?', - [row['measured_at_ms'], device], + [r['measured_at_ms'], device], ); final int destId; if (existing.isNotEmpty) { @@ -8993,71 +8992,34 @@ class LocalDb { 'UPDATE bp_research_reference SET posture = ?, ' 'conditions = ?, systolic_mmhg = ?, diastolic_mmhg = ?, ' 'captured_at_ms = ? WHERE id = ?', - [ - row['posture'], - row['conditions'], - row['systolic_mmhg'], - row['diastolic_mmhg'], - row['captured_at_ms'], - destId, - ], + [...values, destId], ); } else { destId = await txn.rawInsert( 'INSERT INTO bp_research_reference ' - '(measured_at_ms, device, posture, conditions, ' - 'systolic_mmhg, diastolic_mmhg, captured_at_ms) ' + '(posture, conditions, systolic_mmhg, diastolic_mmhg, ' + 'captured_at_ms, measured_at_ms, device) ' 'VALUES (?, ?, ?, ?, ?, ?, ?)', - [ - row['measured_at_ms'], - device, - row['posture'], - row['conditions'], - row['systolic_mmhg'], - row['diastolic_mmhg'], - row['captured_at_ms'], - ], + [...values, r['measured_at_ms'], device], ); } - if (srcId is num) { - bpIdMap[srcId.toInt()] = destId; - } - } - for (final w in srcWins) { - final row = { - for (final e in w.entries) - if (winCols.contains(e.key)) e.key: e.value, - }; - final destRef = row.remove('reference_id'); - final mapped = destRef is num - ? bpIdMap[destRef.toInt()] - : null; - if (mapped == null) continue; - await txn.rawInsert( - 'INSERT OR REPLACE INTO bp_research_window ' - '(reference_id, window_start_ms, window_end_ms, ' - 'onehz_rows, rr_beats, hr_mean, rr_ms_mean, rr_ms_min, ' - 'rr_ms_max, rmssd_ms, meta_json) ' - 'VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', - [ - mapped, - row['window_start_ms'], - row['window_end_ms'], - row['onehz_rows'], - row['rr_beats'], - row['hr_mean'], - row['rr_ms_mean'], - row['rr_ms_min'], - row['rr_ms_max'], - row['rmssd_ms'], - row['meta_json'], - ], + final srcId = r['id']; + final w = srcId is num ? winBySrcId[srcId.toInt()] : null; + if (w == null) continue; + await txn.insert( + 'bp_research_window', + { + for (final e in w.entries) + if (winCols.contains(e.key)) e.key: e.value, + 'reference_id': destId, + }, + conflictAlgorithm: ConflictAlgorithm.replace, ); + winCount++; } }); - counts[t] = t == 'bp_research_reference' - ? srcRefs.length - : srcWins.length; + counts[t] = refs.length; + counts['bp_research_window'] = winCount; } catch (_) { if (!tolerant) rethrow; counts[t] = 0; diff --git a/lib/health/bp_research_capture.dart b/lib/health/bp_research_capture.dart index 7307c4da4..424a0dd3a 100644 --- a/lib/health/bp_research_capture.dart +++ b/lib/health/bp_research_capture.dart @@ -1,31 +1,16 @@ -// BP research capture — pair a cuff reading the user just took with the -// band's own decoded data from the minutes around that instant. -// -// EXPERIMENTAL / DEVELOPER-ONLY. This exists to build a paired dataset a -// human can analyse OUTSIDE this app (CSV export); it is not a blood -// pressure feature and never becomes one. The same guard that fences -// `imported_measurement` applies twice over here: nothing in `compute/`, -// nothing that writes `day_result` / `metric_series`, and nothing that -// writes to HealthKit / Health Connect may read these tables — a wrist -// series regressed against cuff readings inside this app is a -// cuffless-blood-pressure claim from an uncleared device, which is exactly -// the category that earned WHOOP an FDA Warning Letter in Jul 2025. -// -// A capture with no band data at that instant is stored as a capture with a -// NULL window. Missing is missing — never 0, never a fabricated average. - - -/// Window half-widths around the cuff instant (default ±2 min): the frozen -/// window covers [kBpResearchWindowPreMs] before the reference instant and -/// [kBpResearchWindowPostMs] after it. Lives here, next to the capture -/// logic, so the model file has no dependency direction to argue about. +// BP research capture (developer mode only): a cuff reading the user typed in, +// paired with the band's decoded data from the minutes around it, for +// analysis outside the app via the CSV export. Nothing derived reads these +// tables (bp_research_isolation_test.dart). No band data = NULL window. + +import 'dart:math' as math; + +/// Window half-widths around the cuff instant. const int kBpResearchWindowPreMs = 2 * 60 * 1000; const int kBpResearchWindowPostMs = 2 * 60 * 1000; -/// The frozen band window around one reference instant. Every field is -/// nullable for the same reason the storage layer's columns are: a stat the -/// window could not honestly compute (no valid HR, no beats) is absent, not -/// zero. +/// The band window around one reference instant. A stat that can't be +/// computed (no valid HR, no beats) is null, not zero. class BpResearchWindow { const BpResearchWindow({ required this.windowStartMs, @@ -50,8 +35,7 @@ class BpResearchWindow { final double? rrMsMax; final double? rmssdMs; - /// Provenance the analysis needs and nothing else: device_id, firmware - /// string if known, sample counts by table. JSON, written verbatim. + /// Optional provenance JSON, stored verbatim. final String? metaJson; } @@ -83,20 +67,14 @@ class BpResearchCapture { final BpResearchWindow? window; } -/// Same plausibility bounds as `health_measurement_import.dart`, for the -/// same reason: 400 mmHg is a cuff error, and a clamped reading is a -/// fabricated one. Out-of-bounds input is rejected, never corrected. +/// Same bounds as `health_measurement_import.dart`. Out-of-range input is +/// rejected, never clamped. const (double, double) kResearchSystolicBounds = (50, 300); const (double, double) kResearchDiastolicBounds = (20, 200); -/// Compute the frozen band window around [measuredAtMs] from the decoded -/// store, pure and testable without a database (pass the rows in). -/// -/// Window: measured instant minus/plus [preMs]/[postMs] (default ±2 min, -/// [kBpResearchWindowPreMs]/[kBpResearchWindowPostMs]). Reads ONLY already-decoded -/// tables — `decoded_onehz` (HR) and `decoded_rr` (beat intervals). No raw -/// archive, no re-decode, nothing derived: the point is to freeze exactly -/// what the app already holds at the moment of the cuff reading. +/// Window stats around [measuredAtMs] from `decoded_onehz` / `decoded_rr` +/// rows (rr rows ordered by `rr_ts_ms, beat_index`). Null when neither table +/// has a row in the window. BpResearchWindow? researchWindowFrom({ required int measuredAtMs, required List> onehzRows, @@ -127,8 +105,7 @@ BpResearchWindow? researchWindowFrom({ if (onehz.isEmpty && rr.isEmpty) return null; - // HR mean over VALID HR rows only — a run of hr_valid = 0 rows must not - // drag an average toward zero, and absent validity is absent, not false. + // hr 0 / null is "no reading", not a heart rate. final hrs = onehz .map((r) => r['hr']) .whereType() @@ -148,15 +125,14 @@ BpResearchWindow? researchWindowFrom({ rrMean = rrs.reduce((a, b) => a + b) / rrs.length; rrMin = rrs.reduce((a, b) => a < b ? a : b); rrMax = rrs.reduce((a, b) => a > b ? a : b); - // RMSSD over successive differences in window order (rr_ts_ms ASC is - // the caller's contract). Too few beats to form one difference: absent. + // Raw RMSSD over successive beats, no ectopic or gap rejection. if (rrs.length >= 2) { var sumSq = 0.0; for (var i = 1; i < rrs.length; i++) { final d = rrs[i] - rrs[i - 1]; sumSq += d * d; } - rmssd = _sqrt(sumSq / (rrs.length - 1)); + rmssd = math.sqrt(sumSq / (rrs.length - 1)); } } @@ -165,7 +141,7 @@ BpResearchWindow? researchWindowFrom({ windowEndMs: end, onehzRows: onehz.isEmpty ? null : onehz.length, rrBeats: rr.isEmpty ? null : rr.length, - hrMean: hrMean?.toDouble(), + hrMean: hrMean, rrMsMean: rrMean, rrMsMin: rrMin, rrMsMax: rrMax, @@ -173,15 +149,3 @@ BpResearchWindow? researchWindowFrom({ metaJson: metaJson, ); } - -double _sqrt(double v) => v <= 0 ? 0.0 : _sqrtNewton(v); - -double _sqrtNewton(double v) { - var x = v; - var y = (x + 1) / 2; - while ((y - x).abs() > 1e-12) { - x = y; - y = (x + v / x) / 2; - } - return y; -} diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 10be52412..000df5bb1 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -12446,7 +12446,7 @@ "@settingsBpResearchRowTitle": { "description": "Developer settings row title for the experimental blood-pressure research capture screen" }, - "settingsBpResearchRowSub": "EXPERIMENTAL. Pair a cuff reading with the band data of the same instant, for comparison outside this app. Never a health feature", + "settingsBpResearchRowSub": "Experimental. Pair cuff readings with band data for analysis outside the app", "@settingsBpResearchRowSub": { "description": "Developer settings row subtitle for the BP research capture screen" }, @@ -12454,7 +12454,7 @@ "@bpResearchTitle": { "description": "Title of the experimental BP research capture screen" }, - "bpResearchIntro": "EXPERIMENTAL. Take a cuff reading, type the pair in, press capture. The band data of the ±2 minutes around that instant is frozen next to it — for you to compare outside this app. Nothing here is a health feature, nothing here feeds any score, and nothing here is ever blended with what the band measured.", + "bpResearchIntro": "Experimental. Take a cuff reading, type it in and press capture. Band data from 2 minutes either side is added once the band has synced it.", "@bpResearchIntro": { "description": "Intro text of the BP research capture screen" }, @@ -12486,12 +12486,20 @@ "@bpResearchHistory": { "description": "Section header for the stored capture list" }, - "bpResearchBadValue": "That pair is outside the range this app supports (systolic 50–300, diastolic 20–200 mmHg, diastolic below systolic) — it was not saved. Check the numbers and try again.", + "bpResearchBadValue": "That pair is outside the supported range (systolic 50–300, diastolic 20–200 mmHg, diastolic below systolic). It was not saved.", "@bpResearchBadValue": { "description": "Snackbar text for an out-of-bounds reference pair, which is rejected, never clamped" }, - "bpResearchExportHint": "Export all captures as CSV from Your data › Export CSV (set \"BP research captures\"). This is the dedicated export for BP research data; a full-database backup or an opt-in health share also contains it.", + "bpResearchExportHint": "Export as CSV from Your data › Export CSV (set \"BP research captures\"). Backups and an opt-in health share include them too.", "@bpResearchExportHint": { "description": "Hint under the capture list explaining the CSV export path" + }, + "bpResearchWindowPending": "Waiting for the band to sync", + "@bpResearchWindowPending": { + "description": "Capture list subtitle while the band data around the reading has not synced yet" + }, + "bpResearchWindowEmpty": "No band data in the window", + "@bpResearchWindowEmpty": { + "description": "Capture list subtitle when the band recorded nothing around the reading" } } diff --git a/lib/ui2/profile/bp_research.dart b/lib/ui2/profile/bp_research.dart index 269acc8c4..bf5bd8327 100644 --- a/lib/ui2/profile/bp_research.dart +++ b/lib/ui2/profile/bp_research.dart @@ -1,19 +1,5 @@ -// BP research capture — DEVELOPER MODE ONLY. -// -// One flow: take a cuff blood pressure reading, type the pair in, press -// capture. The app freezes the band's own decoded data from the ±2 minutes -// around that instant (1 Hz HR, R-R intervals) next to the reference pair, -// and keeps every capture so a human can compare them over weeks — here in -// a list, or out of the app through the `bp_research` CSV export set. -// -// This is data COLLECTION, not a blood pressure feature: -// · Nothing derived reads these tables. No score, no baseline, no chart -// of ours takes a capture as an input. -// · Nothing here is ever blended with, averaged against, or corrected -// against anything the band measured. -// · Nothing here is exported to HealthKit / Health Connect. -// A window with no band data is stored as a capture with an EMPTY window — -// missing is missing, never zero. +// BP research capture (developer mode only): type in a cuff reading, and +// the band's synced data from 2 minutes either side is stored next to it. import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -39,6 +25,7 @@ class _BpResearchScreenState extends State { final _posture = TextEditingController(); final _conditions = TextEditingController(); List> _rows = const []; + int? _edgeMs; bool _busy = false; @override @@ -58,8 +45,14 @@ class _BpResearchScreenState extends State { } Future _refresh() async { + final edge = await LocalDb.fillBpResearchWindows(); final rows = await LocalDb.bpResearchCaptures(); - if (mounted) setState(() => _rows = rows); + if (mounted) { + setState(() { + _rows = rows; + _edgeMs = edge; + }); + } } Future _capture() async { @@ -77,8 +70,9 @@ class _BpResearchScreenState extends State { if (mounted) { ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text(l?.bpResearchBadValue ?? - 'That pair is not a blood pressure — check the numbers and ' - 'try again. Nothing was stored.'), + 'That pair is outside the supported range (systolic 50–300, ' + 'diastolic 20–200 mmHg, diastolic below systolic). It was not ' + 'saved.'), )); } return; @@ -86,39 +80,17 @@ class _BpResearchScreenState extends State { setState(() => _busy = true); var stored = false; try { - final now = DateTime.now(); - final start = now.millisecondsSinceEpoch - kBpResearchWindowPreMs; - final end = now.millisecondsSinceEpoch + kBpResearchWindowPostMs; - final db = await LocalDb.instance; - // Read exactly what the app already holds around the instant. Two - // narrow range reads — never a day dump, never the raw archive. - final onehz = await db.rawQuery( - 'SELECT rec_ts, hr FROM decoded_onehz ' - 'WHERE device_id = ? AND rec_ts >= ? AND rec_ts <= ? ' - 'ORDER BY rec_ts ASC', - [LocalDb.kPrimaryDeviceId, start ~/ 1000, end ~/ 1000], - ); - final rr = await db.rawQuery( - 'SELECT rr_ts_ms, rr_ms FROM decoded_rr ' - 'WHERE device_id = ? AND rr_ts_ms >= ? AND rr_ts_ms <= ? ' - 'ORDER BY rr_ts_ms ASC', - [LocalDb.kPrimaryDeviceId, start, end], - ); - final window = researchWindowFrom( - measuredAtMs: now.millisecondsSinceEpoch, - onehzRows: onehz, - rrRows: rr, - ); + final now = DateTime.now().millisecondsSinceEpoch; + // The window is filled in by _refresh once the band has synced it. await LocalDb.putBpResearchCapture(BpResearchCapture( - measuredAtMs: now.millisecondsSinceEpoch, + measuredAtMs: now, systolicMmHg: sys, diastolicMmHg: dia, - capturedAtMs: now.millisecondsSinceEpoch, + capturedAtMs: now, device: _device.text.trim().isEmpty ? null : _device.text.trim(), posture: _posture.text.trim().isEmpty ? null : _posture.text.trim(), conditions: _conditions.text.trim().isEmpty ? null : _conditions.text.trim(), - window: window, )); stored = true; _sys.clear(); @@ -128,8 +100,8 @@ class _BpResearchScreenState extends State { if (mounted) { ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text(stored - ? 'Capture saved, but refreshing the history failed. ($e)' - : 'Capture failed \u2014 nothing was stored. ($e)'), + ? 'Saved, but the list failed to refresh. ($e)' + : 'Capture failed, nothing was saved. ($e)'), )); } } finally { @@ -150,12 +122,9 @@ class _BpResearchScreenState extends State { children: [ Text( l?.bpResearchIntro ?? - 'EXPERIMENTAL. Take a cuff reading, type the pair in, ' - 'press capture. The band data of the ±2 minutes around that ' - 'instant is frozen next to it — for you to compare outside ' - 'this app. Nothing here is a health feature, nothing here ' - 'feeds any score, and nothing here is ever blended with what ' - 'the band measured.', + 'Experimental. Take a cuff reading, type it in and press ' + 'capture. Band data from 2 minutes either side is added once ' + 'the band has synced it.', style: F.cap.copyWith(color: p.ink2, height: 1.5), ), const SizedBox(height: S.x4), @@ -212,11 +181,12 @@ class _BpResearchScreenState extends State { ListTile( dense: true, title: Text( - '${r['systolic_mmhg']}/${r['diastolic_mmhg']} mmHg — ' + '${(r['systolic_mmhg'] as num).round()}/' + '${(r['diastolic_mmhg'] as num).round()} mmHg · ' '${formatDayTime(DateTime.fromMillisecondsSinceEpoch( r['measured_at_ms'] as int), l)}', ), - subtitle: Text(_windowSummary(r)), + subtitle: Text(_windowSummary(r, l)), trailing: IconButton( icon: const Icon(LucideIcons.trash2, size: 18), onPressed: () async { @@ -228,9 +198,9 @@ class _BpResearchScreenState extends State { const SizedBox(height: S.x4), Text( l?.bpResearchExportHint ?? - 'Export all captures as CSV from Your data › Export CSV ' - '(set “BP research captures”). Research data — it never ' - 'leaves the phone except through that file.', + 'Export as CSV from Your data › Export CSV (set "BP ' + 'research captures"). Backups and an opt-in health share ' + 'include them too.', style: F.cap.copyWith(color: p.ink2, height: 1.5), ), ], @@ -239,17 +209,18 @@ class _BpResearchScreenState extends State { ); } - /// A window is summarised as what it actually holds. A NULL stat is shown - /// as absent — a dash, never a zero, and never a value that would read as - /// a measurement. - static String _windowSummary(Map r) { + String _windowSummary(Map r, AppLocalizations? l) { final onehz = r['onehz_rows']; final beats = r['rr_beats']; final hr = r['hr_mean']; final rmssd = r['rmssd_ms']; if (onehz == null && beats == null) { - return 'No band data in the window — stored as-is.'; + final edge = _edgeMs; + final end = (r['measured_at_ms'] as int) + kBpResearchWindowPostMs; + return edge == null || end > edge + ? (l?.bpResearchWindowPending ?? 'Waiting for the band to sync') + : (l?.bpResearchWindowEmpty ?? 'No band data in the window'); } final parts = []; if (hr is num) parts.add('HR ${hr.toStringAsFixed(0)} bpm'); diff --git a/lib/ui2/profile/settings.dart b/lib/ui2/profile/settings.dart index 5946eff02..091bdfc5a 100644 --- a/lib/ui2/profile/settings.dart +++ b/lib/ui2/profile/settings.dart @@ -797,9 +797,8 @@ class MoreSettingsView extends StatelessWidget { SetRow(LucideIcons.heartPulse, C.purple, l?.settingsBpResearchRowTitle ?? 'BP research capture', sub: l?.settingsBpResearchRowSub ?? - 'EXPERIMENTAL. Pair a cuff reading with the band ' - 'data of the same instant, for comparison ' - 'outside this app. Never a health feature', + 'Experimental. Pair cuff readings with band data ' + 'for analysis outside the app', onTap: () => goto(c, const BpResearchScreen())), SetRow(LucideIcons.code, C.n500, l?.settingsDeveloperModeRowTitle ?? 'Developer mode', diff --git a/test/bp_research_db_test.dart b/test/bp_research_db_test.dart index 5403fede5..14ded2370 100644 --- a/test/bp_research_db_test.dart +++ b/test/bp_research_db_test.dart @@ -305,4 +305,51 @@ void main() { expect(n, 1); await databaseFactory.deleteDatabase(srcPath); }); + + test('the window is filled once the band has synced past its end', + () async { + final at = _at + 10 * 86400000; + final sec = at ~/ 1000; + final db = await LocalDb.instance; + Future onehz(int t, int hr) => db.insert('decoded_onehz', { + 'device_id': LocalDb.kPrimaryDeviceId, + 'ts_ms': t * 1000, + 'rec_ts': t, + 'counter': t, + 'hr': hr, + }); + Future beat(int t, int i, int rr) => db.insert('decoded_rr', { + 'device_id': LocalDb.kPrimaryDeviceId, + 'ts_ms': t * 1000, + 'rec_ts': t, + 'beat_index': i, + 'rr_ts_ms': t * 1000, + 'rr_ms': rr, + }); + // Beats of one record share rr_ts_ms; insert out of beat order. + await beat(sec, 1, 1000); + await beat(sec, 0, 800); + await beat(sec + 1, 0, 1100); + await onehz(sec, 60); + await onehz(sec + 1, 0); + await LocalDb.putBpResearchCapture(_capture(at)); + + Future> row() async => (await LocalDb + .bpResearchCaptures()) + .firstWhere((r) => r['measured_at_ms'] == at); + + // Synced data stops short of the window end: still pending. + final edge = await LocalDb.fillBpResearchWindows(); + expect(edge, (sec + 1) * 1000); + expect((await row())['onehz_rows'], isNull); + + await onehz(sec + 121, 61); + await LocalDb.fillBpResearchWindows(); + final r = await row(); + expect(r['onehz_rows'], 2); + expect(r['rr_beats'], 3); + expect(r['hr_mean'], 60); + // 800, 1000, 1100 in beat order. + expect(r['rmssd_ms'] as double, closeTo(158.11, 0.01)); + }); } diff --git a/test/bp_research_isolation_test.dart b/test/bp_research_isolation_test.dart index 0a9b7607f..113bc2618 100644 --- a/test/bp_research_isolation_test.dart +++ b/test/bp_research_isolation_test.dart @@ -1,30 +1,11 @@ -// THE INVARIANT the BP research store exists to have. -// -// `bp_research_reference` / `bp_research_window` hold a cuff reading next to -// the band's own decoded data from the same instant. That pairing is exactly -// the input a cuffless-blood-pressure claim would be built from, which is -// the category that earned WHOOP an FDA Warning Letter in Jul 2025 — so the -// tables are fenced the same way `observation` is: nothing outside the -// allow-list may name either table, and the allow-list names the readers -// whose whole job is showing/exporting research data to the user. -// -// Layer 1 (structural source scan) reuses the mechanism -// observation_isolation_test.dart is built on. A violation is SILENT — no -// exception, just a wrist-vs-cuff regression one PR later — so the control -// is a test that goes red, not a comment that says don't. +// Nothing outside the allow-list may name the BP research tables or their +// helpers, so a cuff reading can never become an input to derived output. import 'dart:io'; import 'package:flutter_test/flutter_test.dart'; import 'package:path/path.dart' as p; -/// The ONLY files allowed to name either BP research table. -/// -/// Adding to this list is the deliberate act the invariant asks for. Before -/// you do: a READER for the dev screen or the CSV export is fine. Anything -/// in `lib/compute/`, anything that feeds `day_result`, `metric_series` or -/// a baseline, and anything that writes to HealthKit / Health Connect is -/// the thing this whole file exists to stop. const _allowed = { 'lib/data/db.dart', 'lib/data/csv_export.dart', @@ -44,7 +25,8 @@ void main() { s.contains('bp_research_window') || s.contains('bpResearchCaptures') || s.contains('putBpResearchCapture') || - s.contains('deleteBpResearchCapture'); + s.contains('deleteBpResearchCapture') || + s.contains('fillBpResearchWindows'); if (hit && !_allowed.contains(rel)) offenders.add(rel); } expect(offenders, isEmpty, diff --git a/test/ui2_tokens_test.dart b/test/ui2_tokens_test.dart index effee3e28..51ec408cd 100644 --- a/test/ui2_tokens_test.dart +++ b/test/ui2_tokens_test.dart @@ -271,15 +271,12 @@ const _notComponents = { // Where the hydration notification lands: a Scaffold route that reads and // writes the day's journal metrics. The one control on it — FieldStepper — // IS in the gallery. - // The BP research capture route: a Scaffold that reads and writes its own - // research table (never the health stores) and only exists behind the dev - // toggle. A gallery case would have to mock the database; the capture flow - // is what bp_research_capture_test.dart covers on the data side. - 'BpResearchScreen', // The coach chat and its BYOK setup: Scaffold routes that own an engine, a // 120 s network call and the keychain. `CoachFigure` — the part a gallery can // actually hold — IS in it. 'CoachScreen', 'CoachSetup', 'AiBriefingScreen', + // Dev-only Scaffold route over its own research tables. + 'BpResearchScreen', // The two day-scoped Scaffold routes: each resolves a day, then reads the // bundle plus three or four stores to fill it. Their BODIES are what a // gallery can hold and both are in it as cases — `timeline_day`, From 31457ac6cf11eacc9b8c89224e4e2051be7bfab5 Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Fri, 2 Oct 2026 11:58:50 +0530 Subject: [PATCH 08/11] bp research: fill pending windows before the decoded prune, keep references when the window table is unreadable on salvage --- lib/data/db.dart | 10 ++++++- test/bp_research_db_test.dart | 56 +++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/lib/data/db.dart b/lib/data/db.dart index 19f567fb4..c34c705a9 100644 --- a/lib/data/db.dart +++ b/lib/data/db.dart @@ -8961,7 +8961,9 @@ class LocalDb { try { wins = await src.rawQuery('SELECT * FROM bp_research_window'); } on DatabaseException catch (e) { - if (!e.isNoSuchTableError()) rethrow; + // A missing or unreadable window table only costs the + // windows; the hand-entered references still come across. + if (!e.isNoSuchTableError() && !tolerant) rethrow; } final winBySrcId = { for (final w in wins) @@ -11163,6 +11165,12 @@ class LocalDb { /// TIME (epoch seconds) is strictly before [cutoffSec]. static Future pruneDecodedBeforeRecTs(int cutoffSec) async { final db = await instance; + // A pending BP research window can only be read from the decoded rows this + // is about to delete, so fill it first. Best effort: a dev-only capture + // must not stall retention. + try { + await fillBpResearchWindows(); + } catch (_) {} // `deleted` used to just stay 0 forever - none of the txn.delete() calls' // return values (rows actually deleted) were ever added to it, so the // caller's `if (deleted > 0) log(...)` never fired even on a real prune. diff --git a/test/bp_research_db_test.dart b/test/bp_research_db_test.dart index 14ded2370..ad09c9752 100644 --- a/test/bp_research_db_test.dart +++ b/test/bp_research_db_test.dart @@ -7,6 +7,8 @@ // · a retake that now finds band data replaces the old window instead // of orphaning it under the replaced reference's old id. // Runs the REAL LocalDb over sqflite_common_ffi. +import 'dart:io'; + import 'package:flutter_test/flutter_test.dart'; import 'package:openstrap_edge/data/db.dart'; import 'package:openstrap_edge/health/bp_research_capture.dart'; @@ -352,4 +354,58 @@ void main() { // 800, 1000, 1100 in beat order. expect(r['rmssd_ms'] as double, closeTo(158.11, 0.01)); }); + + test('the prune fills a pending window before deleting its rows', + () async { + final at = _at + 20 * 86400000; + final sec = at ~/ 1000; + final db = await LocalDb.instance; + for (final t in [sec, sec + 121]) { + await db.insert('decoded_onehz', { + 'device_id': LocalDb.kPrimaryDeviceId, + 'ts_ms': t * 1000, + 'rec_ts': t, + 'counter': t, + 'hr': 70, + }); + } + await LocalDb.putBpResearchCapture(_capture(at)); + // Nobody opened the dev screen; retention moves past the window. + await LocalDb.pruneDecodedBeforeRecTs(sec + 120); + final r = (await LocalDb.bpResearchCaptures()) + .firstWhere((r) => r['measured_at_ms'] == at); + expect(r['onehz_rows'], 1); + expect(r['hr_mean'], 70); + }); + + test('a salvage with an unreadable window table keeps the references', + () async { + const name = 'bp_research_salvage_test.db'; + final path = p.join(await databaseFactory.getDatabasesPath(), name); + await LocalDb.close(); + await databaseFactory.deleteDatabase(path); + LocalDb.dbName = name; + await LocalDb.putBpResearchCapture(_capture(_at, device: 'salvage')); + await LocalDb.close(); + // Brick the ladder (see startup_and_rebuild_recovery_test.dart) and make + // every read of the window table fail with something other than + // "no such table". + final raw = await databaseFactory.openDatabase(path); + await raw.execute('CREATE TABLE _raw_old (hex TEXT)'); + await raw.execute('DROP TABLE bp_research_window'); + await raw.execute('CREATE VIEW bp_research_window AS ' + 'SELECT abs(-9223372036854775807 - 1) AS reference_id'); + await raw.execute('PRAGMA user_version = 2'); + await raw.close(); + LocalDb.lastRebuild = null; + + final db = await LocalDb.instance; + expect(LocalDb.lastRebuild, isNotNull); + final refs = await db.query('bp_research_reference'); + expect(refs, hasLength(1)); + expect(refs.first['device'], 'salvage'); + expect(LocalDb.lastRebuild!.salvaged['bp_research_reference'], 1); + final q = File(LocalDb.lastRebuild!.quarantinePath); + if (q.existsSync()) q.deleteSync(); + }); } From fde8daa7c57adb911f519f202bef078385aad016 Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Fri, 2 Oct 2026 16:56:31 +0530 Subject: [PATCH 09/11] bp research: range the rr window read on ts_ms so it uses the pk instead of scanning decoded_rr --- lib/data/db.dart | 14 ++++++++++---- test/bp_research_db_test.dart | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/data/db.dart b/lib/data/db.dart index c34c705a9..edd3cf081 100644 --- a/lib/data/db.dart +++ b/lib/data/db.dart @@ -1704,6 +1704,15 @@ class LocalDb { '''); } + /// Beats of one record share rr_ts_ms; beat_index orders them. Ranged on + /// ts_ms (= rr_ts_ms for every row) so the read rides the + /// (device_id, ts_ms, beat_index) PK. rr_ts_ms has no index, so ranging on + /// it scanned and sorted the whole store once per pending capture. + @visibleForTesting + static const bpResearchRrWindowSql = 'SELECT rr_ts_ms, rr_ms FROM decoded_rr ' + 'WHERE device_id = ? AND ts_ms >= ? AND ts_ms <= ? ' + 'ORDER BY ts_ms ASC, beat_index ASC'; + /// Fill the window of every capture that has none yet, once the primary /// band's synced 1 Hz data reaches the window end. At capture time the /// window is still in the future and the band hasn't offloaded it, so it @@ -1736,11 +1745,8 @@ class LocalDb { 'ORDER BY rec_ts ASC', [kPrimaryDeviceId, start ~/ 1000, end ~/ 1000], ), - // Beats of one record share rr_ts_ms; beat_index orders them. rrRows: await db.rawQuery( - 'SELECT rr_ts_ms, rr_ms FROM decoded_rr ' - 'WHERE device_id = ? AND rr_ts_ms >= ? AND rr_ts_ms <= ? ' - 'ORDER BY rr_ts_ms ASC, beat_index ASC', + bpResearchRrWindowSql, [kPrimaryDeviceId, start, end], ), ); diff --git a/test/bp_research_db_test.dart b/test/bp_research_db_test.dart index ad09c9752..e57b5641c 100644 --- a/test/bp_research_db_test.dart +++ b/test/bp_research_db_test.dart @@ -378,6 +378,20 @@ void main() { expect(r['hr_mean'], 70); }); + test('the window RR read seeks the store instead of scanning it', + () async { + // A capture with no band data stays pending and is re-read on every + // prune, so this read must not walk and sort the whole decoded_rr. + final db = await LocalDb.instance; + final plan = (await db.rawQuery( + 'EXPLAIN QUERY PLAN ${LocalDb.bpResearchRrWindowSql}', + [LocalDb.kPrimaryDeviceId, 1, 9], + )).map((r) => r['detail'].toString()).join(' | '); + expect(plan.toUpperCase(), contains('TS_MS'), reason: plan); + expect(plan.toUpperCase(), isNot(contains('USE TEMP B-TREE')), + reason: plan); + }); + test('a salvage with an unreadable window table keeps the references', () async { const name = 'bp_research_salvage_test.db'; From 9dcaf1a0e09b833d1ee2375eb90107f7393571f1 Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Fri, 2 Oct 2026 16:56:31 +0530 Subject: [PATCH 10/11] bp research: fix export hint to the real row, localize the snackbars and window summary --- lib/l10n/app_en.arb | 50 +++++++++++++++++++++++++++++++- lib/ui2/profile/bp_research.dart | 25 +++++++++++----- 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 000df5bb1..47dbd7822 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -12490,7 +12490,7 @@ "@bpResearchBadValue": { "description": "Snackbar text for an out-of-bounds reference pair, which is rejected, never clamped" }, - "bpResearchExportHint": "Export as CSV from Your data › Export CSV (set \"BP research captures\"). Backups and an opt-in health share include them too.", + "bpResearchExportHint": "Included in Your data › Export as spreadsheets. Backups and an opt-in health share include them too.", "@bpResearchExportHint": { "description": "Hint under the capture list explaining the CSV export path" }, @@ -12501,5 +12501,53 @@ "bpResearchWindowEmpty": "No band data in the window", "@bpResearchWindowEmpty": { "description": "Capture list subtitle when the band recorded nothing around the reading" + }, + "bpResearchRefreshFailed": "Saved, but the list failed to refresh. ({error})", + "@bpResearchRefreshFailed": { + "description": "Snackbar when the capture was stored but the list reload threw", + "placeholders": { + "error": { + "type": "String" + } + } + }, + "bpResearchCaptureFailed": "Capture failed, nothing was saved. ({error})", + "@bpResearchCaptureFailed": { + "description": "Snackbar when storing the capture threw", + "placeholders": { + "error": { + "type": "String" + } + } + }, + "bpResearchWindowHr": "HR {bpm} bpm", + "@bpResearchWindowHr": { + "description": "Capture list subtitle part: mean heart rate in the window", + "placeholders": { + "bpm": { + "type": "String" + } + } + }, + "bpResearchWindowRmssd": "RMSSD {ms} ms", + "@bpResearchWindowRmssd": { + "description": "Capture list subtitle part: RMSSD in the window", + "placeholders": { + "ms": { + "type": "String" + } + } + }, + "bpResearchWindowCounts": "{rows} 1 Hz rows, {beats} beats", + "@bpResearchWindowCounts": { + "description": "Capture list subtitle part: how much band data the window holds", + "placeholders": { + "rows": { + "type": "String" + }, + "beats": { + "type": "String" + } + } } } diff --git a/lib/ui2/profile/bp_research.dart b/lib/ui2/profile/bp_research.dart index bf5bd8327..182962086 100644 --- a/lib/ui2/profile/bp_research.dart +++ b/lib/ui2/profile/bp_research.dart @@ -100,8 +100,10 @@ class _BpResearchScreenState extends State { if (mounted) { ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text(stored - ? 'Saved, but the list failed to refresh. ($e)' - : 'Capture failed, nothing was saved. ($e)'), + ? (l?.bpResearchRefreshFailed('$e') ?? + 'Saved, but the list failed to refresh. ($e)') + : (l?.bpResearchCaptureFailed('$e') ?? + 'Capture failed, nothing was saved. ($e)')), )); } } finally { @@ -198,9 +200,8 @@ class _BpResearchScreenState extends State { const SizedBox(height: S.x4), Text( l?.bpResearchExportHint ?? - 'Export as CSV from Your data › Export CSV (set "BP ' - 'research captures"). Backups and an opt-in health share ' - 'include them too.', + 'Included in Your data › Export as spreadsheets. Backups ' + 'and an opt-in health share include them too.', style: F.cap.copyWith(color: p.ink2, height: 1.5), ), ], @@ -223,9 +224,17 @@ class _BpResearchScreenState extends State { : (l?.bpResearchWindowEmpty ?? 'No band data in the window'); } final parts = []; - if (hr is num) parts.add('HR ${hr.toStringAsFixed(0)} bpm'); - if (rmssd is num) parts.add('RMSSD ${rmssd.toStringAsFixed(0)} ms'); - parts.add('${onehz ?? 0} 1 Hz rows, ${beats ?? 0} beats'); + if (hr is num) { + final v = hr.toStringAsFixed(0); + parts.add(l?.bpResearchWindowHr(v) ?? 'HR $v bpm'); + } + if (rmssd is num) { + final v = rmssd.toStringAsFixed(0); + parts.add(l?.bpResearchWindowRmssd(v) ?? 'RMSSD $v ms'); + } + final rows = '${onehz ?? 0}', n = '${beats ?? 0}'; + parts.add( + l?.bpResearchWindowCounts(rows, n) ?? '$rows 1 Hz rows, $n beats'); return parts.join(' · '); } } From b4c3e13331ba01c9972241501d6e5fba2164567e Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Sun, 4 Oct 2026 03:48:34 +0530 Subject: [PATCH 11/11] bp research: catch list reload errors on open and delete --- lib/l10n/app_en.arb | 9 +++++++++ lib/ui2/profile/bp_research.dart | 22 +++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 47dbd7822..6086fb69b 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -12511,6 +12511,15 @@ } } }, + "bpResearchLoadFailed": "The capture list failed to update. ({error})", + "@bpResearchLoadFailed": { + "description": "Snackbar when reloading the capture list (on open or after a delete) threw", + "placeholders": { + "error": { + "type": "String" + } + } + }, "bpResearchCaptureFailed": "Capture failed, nothing was saved. ({error})", "@bpResearchCaptureFailed": { "description": "Snackbar when storing the capture threw", diff --git a/lib/ui2/profile/bp_research.dart b/lib/ui2/profile/bp_research.dart index 182962086..8e3799d7c 100644 --- a/lib/ui2/profile/bp_research.dart +++ b/lib/ui2/profile/bp_research.dart @@ -31,7 +31,7 @@ class _BpResearchScreenState extends State { @override void initState() { super.initState(); - _refresh(); + _reload(); } @override @@ -55,6 +55,22 @@ class _BpResearchScreenState extends State { } } + /// Runs [before] (if any) then [_refresh]; a throw becomes a snackbar + /// instead of an unhandled async error. + Future _reload([Future Function()? before]) async { + try { + await before?.call(); + await _refresh(); + } catch (e) { + if (!mounted) return; + final l = AppLocalizations.of(context); + ScaffoldMessenger.of(context).showSnackBar(SnackBar( + content: Text(l?.bpResearchLoadFailed('$e') ?? + 'The capture list failed to update. ($e)'), + )); + } + } + Future _capture() async { if (_busy) return; final sys = double.tryParse(_sys.text); @@ -192,8 +208,8 @@ class _BpResearchScreenState extends State { trailing: IconButton( icon: const Icon(LucideIcons.trash2, size: 18), onPressed: () async { - await LocalDb.deleteBpResearchCapture(r['id'] as int); - await _refresh(); + await _reload(() => + LocalDb.deleteBpResearchCapture(r['id'] as int)); }, ), ),