From e4c74af5ae5006f108a34a4a8704d75837b43e13 Mon Sep 17 00:00:00 2001 From: KuljeetSinghXDA <22261392+KuljeetSinghXDA@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:36:30 +0530 Subject: [PATCH 1/5] health: the legacy steps purge retries a delete the store refused MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _purgeLegacyStepsIfNeeded discarded the bool from _nativeDelete and advanced `health_steps_purged_through` anyway. On Health Connect false is a genuine failure — the same rule healthDeleteClearedRange already applies to every other delete — so a denied STEPS write left the fabricated step samples earlier versions wrote in the store for good, counted beside the user's real steps. Read the result through healthDeleteClearedRange and advance only when the range is confirmed clear. A refused range is remembered in health_steps_purge_pending_from, so the next exported day deletes from that day's midnight instead of stepping over it; the marker clears on success. --- lib/health/health_export.dart | 49 +++++++- test/health_legacy_steps_purge_test.dart | 142 +++++++++++++++++++++++ 2 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 test/health_legacy_steps_purge_test.dart diff --git a/lib/health/health_export.dart b/lib/health/health_export.dart index 1144d4a5a..5fb09fbd2 100644 --- a/lib/health/health_export.dart +++ b/lib/health/health_export.dart @@ -698,6 +698,13 @@ class HealthExporter { static const _kStepsPurgeCursor = 'health_steps_purged_through'; String? _stepsPurgedThrough; + /// The earliest day whose legacy-STEPS delete the store did NOT confirm. + /// Empty when there is none. Kept apart from [_kStepsPurgeCursor], which is + /// the newest CONFIRMED day, so a refused range is retried by a later day's + /// delete instead of being stepped over and forgotten. + static const _kStepsPurgePendingFrom = 'health_steps_purge_pending_from'; + String? _stepsPurgePendingFrom; + /// Delete the fabricated STEPS samples earlier versions wrote for [date]. /// /// ONE-SHOT, and deliberately not part of the day's success accounting: this @@ -711,15 +718,55 @@ class HealthExporter { DateTime dayEnd, ) async { _stepsPurgedThrough ??= await LocalDb.getCursor(_kStepsPurgeCursor) ?? ''; + _stepsPurgePendingFrom ??= + await LocalDb.getCursor(_kStepsPurgePendingFrom) ?? ''; final through = _stepsPurgedThrough!; + final pending = _stepsPurgePendingFrom!; if (through.isNotEmpty && date.compareTo(through) <= 0) return; if (_storeHung) return; + // A day the store refused to clear stays in the range until a delete + // confirms it. Starting at that day's midnight is what stops the next + // day's delete from stepping over it. + var from = dayStart; + if (pending.isNotEmpty) { + final startSec = localDayStartSec(pending); + if (startSec != null) { + final pendingStart = + DateTime.fromMillisecondsSinceEpoch(startSec * 1000); + if (pendingStart.isBefore(from)) from = pendingStart; + } + } + Future rememberPending() async { + if (pending.isEmpty || date.compareTo(pending) < 0) { + _stepsPurgePendingFrom = date; + await LocalDb.setCursor(_kStepsPurgePendingFrom, date); + } + } + try { - await _nativeDelete(HealthDataType.STEPS, dayStart, dayEnd); + // A false from the store is a genuine failure on Health Connect — the + // same rule every other delete here reads through this helper — so an + // unconfirmed purge must not be recorded as done. On Apple a false means + // "found nothing", which the helper translates back to cleared. + final cleared = healthDeleteClearedRange( + deleted: await _nativeDelete(HealthDataType.STEPS, from, dayEnd), + ios: _apple, + ); + if (!cleared) { + await rememberPending(); + debugPrint('[health] purge legacy steps $date: the store did not ' + 'confirm the delete'); + return; + } + if (pending.isNotEmpty) { + _stepsPurgePendingFrom = ''; + await LocalDb.setCursor(_kStepsPurgePendingFrom, ''); + } _stepsPurgedThrough = date; await LocalDb.setCursor(_kStepsPurgeCursor, date); } catch (e) { if (e is TimeoutException) _storeHung = true; + await rememberPending(); // Leave the cursor where it is so the next pass retries this day. debugPrint('[health] purge legacy steps $date: $e'); } diff --git a/test/health_legacy_steps_purge_test.dart b/test/health_legacy_steps_purge_test.dart new file mode 100644 index 000000000..42f8b11de --- /dev/null +++ b/test/health_legacy_steps_purge_test.dart @@ -0,0 +1,142 @@ +// The one-shot legacy-STEPS purge must not mark a day purged when the store +// did not confirm the delete. +// +// `_purgeLegacyStepsIfNeeded` discarded the bool from `_nativeDelete`. On +// Health Connect a false delete is a genuine failure (a denied STEPS write +// permission, an unmapped type, a store error) — `healthDeleteClearedRange` +// documents exactly that, and the sibling prefix purge already retries on it. +// The cursor advanced anyway, so the fabricated step samples earlier versions +// wrote for that day were never asked for again: the store keeps adding them +// to the user's real step total. +import 'dart:convert'; + +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:path/path.dart' as p; +import 'package:sqflite_common_ffi/sqflite_ffi.dart'; +import 'package:openstrap_edge/data/db.dart'; +import 'package:openstrap_edge/health/health_export.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + const channel = MethodChannel('flutter_health'); + const appleSleep = MethodChannel('openstrap/healthkit_sleep'); + + final now = DateTime.now(); + DateTime dayAt(int back) => DateTime(now.year, now.month, now.day - back); + String labelOf(DateTime d) => + '${d.year.toString().padLeft(4, '0')}-' + '${d.month.toString().padLeft(2, '0')}-' + '${d.day.toString().padLeft(2, '0')}'; + final older = dayAt(2); + final newer = dayAt(1); + final olderDay = labelOf(older); + final newerDay = labelOf(newer); + + // Every STEPS delete that is not the epoch-to-cursor prefix purge. + final stepDeletes = <(int, int)>[]; + var perDayDeletesFail = 0; + + Future putDay(String day) => LocalDb.putDayResult( + dayId: day, + algoVersion: 120, + payloadJson: jsonEncode({ + 'scalars': {'rhr': 55}, + }), + windowJson: '{}', + finalized: true, + ); + + setUpAll(() async { + sqfliteFfiInit(); + databaseFactory = databaseFactoryFfi; + LocalDb.dbName = 'openstrap_legacy_steps_purge_test.db'; + await databaseFactory.deleteDatabase( + p.join(await databaseFactory.getDatabasesPath(), LocalDb.dbName), + ); + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger + .setMockMethodCallHandler(channel, (call) async { + final args = call.arguments is Map ? call.arguments as Map : const {}; + if (call.method == 'delete') { + final start = (args['startTime'] as num?)?.toInt() ?? 0; + final end = (args['endTime'] as num?)?.toInt() ?? 0; + if (args['dataTypeKey'] == 'STEPS' && start != 0) { + stepDeletes.add((start, end)); + if (perDayDeletesFail > 0) { + perDayDeletesFail--; + return false; + } + } + return true; + } + return true; + }); + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger + .setMockMethodCallHandler(appleSleep, (_) async => true); + }); + + setUp(() async { + stepDeletes.clear(); + perDayDeletesFail = 0; + await LocalDb.setCursor('health_export_through', ''); + await LocalDb.setCursor('health_steps_purged_through', ''); + await LocalDb.setCursor('health_steps_purge_pending_from', ''); + await LocalDb.setCursor('health_export_retry_state', ''); + final days = await LocalDb.dayResultVersions(); + if (days.isNotEmpty) { + await (await LocalDb.instance).delete('day_result'); + } + await putDay(olderDay); + await putDay(newerDay); + }); + + tearDownAll(() async { + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger + .setMockMethodCallHandler(channel, null); + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger + .setMockMethodCallHandler(appleSleep, null); + await LocalDb.close(); + await databaseFactory.deleteDatabase( + p.join(await databaseFactory.getDatabasesPath(), LocalDb.dbName), + ); + }); + + test('an unconfirmed delete does not advance the purge cursor', () async { + perDayDeletesFail = 2; + await HealthExporter(isApple: false).exportAll(forceRetry: true); + expect( + await LocalDb.getCursor('health_steps_purged_through'), + isEmpty, + reason: 'neither day was confirmed purged, so neither may be recorded', + ); + expect( + await LocalDb.getCursor('health_steps_purge_pending_from'), + olderDay, + reason: 'the earliest refused day is the one to retry from', + ); + }); + + test( + 'the next day retries the refused range, then clears the marker', + () async { + perDayDeletesFail = 1; + await HealthExporter(isApple: false).exportAll(forceRetry: true); + expect(stepDeletes, hasLength(2)); + expect( + stepDeletes.first.$1, + older.millisecondsSinceEpoch, + reason: 'the first day purges its own range', + ); + expect( + stepDeletes.last.$1, + older.millisecondsSinceEpoch, + reason: 'the newer day widens its range over the day the store refused', + ); + expect(await LocalDb.getCursor('health_steps_purged_through'), newerDay); + expect( + await LocalDb.getCursor('health_steps_purge_pending_from'), + isEmpty, + ); + }, + ); +} From 19d84f8504ce5e3ab3bb89c74eabfc65d4f41ee5 Mon Sep 17 00:00:00 2001 From: KuljeetSinghXDA <22261392+KuljeetSinghXDA@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:51:36 +0530 Subject: [PATCH 2/5] health: retry a refused legacy steps purge per day, never widened The first version widened a later day's delete back over the refused day. That range also covers the refused day's valid strap-counter samples, which only that day's own export rewrites, so a retry could erase measured steps nothing puts back. Keep a pending MAP of {day: (startMs, endMs)} instead: - delete strictly one day's own range, never another day's; - retry only while that day is being exported again, so its steps are rewritten immediately after the purge; - persist the intent before the delete and clear only that day's entry on a confirmed clear; - store absolute bounds, so a timezone move cannot shift the retry and leave a gap; - skip while step writes are held by the prefix purge, or the delete would remove valid steps that the held write cannot restore. A cursor written by an older release is left alone: it has no per-day evidence, and re-purging behind it would delete valid samples for days this pass does not rewrite. Tests: per-day scoping, persisted-before-delete, stored-epoch retry, later marker survives an earlier success, and no cursor advance without confirmation. The day ids now come from dayLabelOf. --- lib/health/health_export.dart | 147 ++++++++++++++----- test/health_legacy_steps_purge_test.dart | 176 ++++++++++++++++------- 2 files changed, 230 insertions(+), 93 deletions(-) diff --git a/lib/health/health_export.dart b/lib/health/health_export.dart index 5fb09fbd2..7e6960d90 100644 --- a/lib/health/health_export.dart +++ b/lib/health/health_export.dart @@ -694,16 +694,67 @@ class HealthExporter { if (t != HealthDataType.STEPS && t != HealthDataType.HEART_RATE) t, ]; - /// Cursor for the one-shot legacy-STEPS purge: the newest day already purged. + /// Cursor for the one-shot legacy-STEPS purge: the newest day CONFIRMED + /// purged. + /// + /// A cursor written by an older release is taken at face value: that release + /// advanced it even when the store refused the delete, but it kept no per-day + /// evidence. Re-purging behind it would delete valid strap-counter samples + /// for days this pass does not rewrite, so the safe choice is to leave those + /// days alone rather than trade one data defect for a worse one. static const _kStepsPurgeCursor = 'health_steps_purged_through'; String? _stepsPurgedThrough; - /// The earliest day whose legacy-STEPS delete the store did NOT confirm. - /// Empty when there is none. Kept apart from [_kStepsPurgeCursor], which is - /// the newest CONFIRMED day, so a refused range is retried by a later day's - /// delete instead of being stepped over and forgotten. - static const _kStepsPurgePendingFrom = 'health_steps_purge_pending_from'; - String? _stepsPurgePendingFrom; + /// Per-day retries for a legacy-STEPS delete the store did NOT confirm. + /// + /// `{day: (startMs, endMs)}`, captured when the delete was first refused. + /// A MAP, not one earliest day: widening a later day's delete over an earlier + /// one would remove that earlier day's valid strap steps, and only the day + /// being exported has its steps rewritten afterwards. Each entry is its own + /// day's absolute bounds, so a timezone change cannot move the retry range + /// and leave a gap. Written BEFORE the delete is issued and removed only + /// when that same day's delete is confirmed. + static const _kStepsPurgePending = 'health_steps_purge_pending'; + Map? _stepsPurgePending; + + Future> _loadPendingPurges() async { + final raw = await LocalDb.getCursor(_kStepsPurgePending) ?? ''; + if (raw.isEmpty) return {}; + try { + final decoded = jsonDecode(raw); + if (decoded is! List) return {}; + return { + for (final entry in decoded.whereType()) + if (entry['day'] is String && + entry['start_ms'] is num && + entry['end_ms'] is num) + entry['day'] as String: ( + startMs: (entry['start_ms'] as num).toInt(), + endMs: (entry['end_ms'] as num).toInt(), + ), + }; + } catch (e) { + debugPrint('[health] steps purge pending: $e'); + return {}; + } + } + + Future _savePendingPurges() async { + final pending = _stepsPurgePending!; + await LocalDb.setCursor( + _kStepsPurgePending, + pending.isEmpty + ? '' + : jsonEncode([ + for (final entry in pending.entries) + { + 'day': entry.key, + 'start_ms': entry.value.startMs, + 'end_ms': entry.value.endMs, + }, + ]), + ); + } /// Delete the fabricated STEPS samples earlier versions wrote for [date]. /// @@ -712,62 +763,80 @@ class HealthExporter { /// exporting the day. A failure here must not stall the export cursor for a /// type nothing writes. Days are walked ascending, so the cursor advances /// monotonically and a re-exported tail day is not re-purged. + /// + /// STRICTLY ONE DAY PER DELETE. A retry keeps that day's own absolute bounds + /// and runs only while that day is being exported again, which rewrites its + /// steps immediately afterwards. Widening a delete over earlier days would + /// erase valid strap-counter samples that nothing rewrites. Future _purgeLegacyStepsIfNeeded( String date, DateTime dayStart, DateTime dayEnd, ) async { _stepsPurgedThrough ??= await LocalDb.getCursor(_kStepsPurgeCursor) ?? ''; - _stepsPurgePendingFrom ??= - await LocalDb.getCursor(_kStepsPurgePendingFrom) ?? ''; + _stepsPurgePending ??= await _loadPendingPurges(); final through = _stepsPurgedThrough!; - final pending = _stepsPurgePendingFrom!; - if (through.isNotEmpty && date.compareTo(through) <= 0) return; - if (_storeHung) return; - // A day the store refused to clear stays in the range until a delete - // confirms it. Starting at that day's midnight is what stops the next - // day's delete from stepping over it. - var from = dayStart; - if (pending.isNotEmpty) { - final startSec = localDayStartSec(pending); - if (startSec != null) { - final pendingStart = - DateTime.fromMillisecondsSinceEpoch(startSec * 1000); - if (pendingStart.isBefore(from)) from = pendingStart; - } + final pending = _stepsPurgePending!; + final retry = pending[date]; + // A day with a persisted retry is being written right now, so its purge + // runs before its new steps. Days at or before the confirmed cursor + // without a retry are done. + if (retry == null && through.isNotEmpty && date.compareTo(through) <= 0) { + return; } - Future rememberPending() async { - if (pending.isEmpty || date.compareTo(pending) < 0) { - _stepsPurgePendingFrom = date; - await LocalDb.setCursor(_kStepsPurgePendingFrom, date); - } - } - + if (_storeHung) return; + // Steps are held while the prefix purge has not finished. A purge now would + // delete the current day's valid samples and the held write would not put + // them back, so leave both the marker and the day for a later pass. + if (_stepWritesHeld) return; + final range = + retry ?? + ( + startMs: dayStart.millisecondsSinceEpoch, + endMs: dayEnd.millisecondsSinceEpoch, + ); + // Retry intent BEFORE the delete: a crash after a refused delete must + // leave the day recoverable. The bounds are absolute, so a later timezone + // change cannot move the range and leave a gap. + var addedHere = false; try { + if (retry == null) { + pending[date] = range; + addedHere = true; + // If this write fails the delete below is not issued, so an + // unrecorded attempt cannot be lost to a crash. + await _savePendingPurges(); + } // A false from the store is a genuine failure on Health Connect — the // same rule every other delete here reads through this helper — so an // unconfirmed purge must not be recorded as done. On Apple a false means // "found nothing", which the helper translates back to cleared. final cleared = healthDeleteClearedRange( - deleted: await _nativeDelete(HealthDataType.STEPS, from, dayEnd), + deleted: await _nativeDelete( + HealthDataType.STEPS, + DateTime.fromMillisecondsSinceEpoch(range.startMs), + DateTime.fromMillisecondsSinceEpoch(range.endMs), + ), ios: _apple, ); if (!cleared) { - await rememberPending(); debugPrint('[health] purge legacy steps $date: the store did not ' 'confirm the delete'); return; } - if (pending.isNotEmpty) { - _stepsPurgePendingFrom = ''; - await LocalDb.setCursor(_kStepsPurgePendingFrom, ''); + // ONLY this day. This delete did not cover another pending day's range, + // so that marker must survive for its own re-export. + pending.remove(date); + await _savePendingPurges(); + if (through.isEmpty || date.compareTo(through) > 0) { + _stepsPurgedThrough = date; + await LocalDb.setCursor(_kStepsPurgeCursor, date); } - _stepsPurgedThrough = date; - await LocalDb.setCursor(_kStepsPurgeCursor, date); } catch (e) { + if (addedHere) pending.remove(date); if (e is TimeoutException) _storeHung = true; - await rememberPending(); - // Leave the cursor where it is so the next pass retries this day. + // Either the retry intent is on disk already, or no delete was issued. + // Both leave the day retryable and the cursor aiming at it. debugPrint('[health] purge legacy steps $date: $e'); } } diff --git a/test/health_legacy_steps_purge_test.dart b/test/health_legacy_steps_purge_test.dart index 42f8b11de..61eccb806 100644 --- a/test/health_legacy_steps_purge_test.dart +++ b/test/health_legacy_steps_purge_test.dart @@ -1,22 +1,25 @@ // The one-shot legacy-STEPS purge must not mark a day purged when the store -// did not confirm the delete. +// did not confirm the delete, and it must never widen a retry over an earlier +// day's valid strap-counter samples. // -// `_purgeLegacyStepsIfNeeded` discarded the bool from `_nativeDelete`. On -// Health Connect a false delete is a genuine failure (a denied STEPS write -// permission, an unmapped type, a store error) — `healthDeleteClearedRange` -// documents exactly that, and the sibling prefix purge already retries on it. -// The cursor advanced anyway, so the fabricated step samples earlier versions -// wrote for that day were never asked for again: the store keeps adding them -// to the user's real step total. +// `_purgeLegacyStepsIfNeeded` used to discard the bool from `_nativeDelete` and +// advance `health_steps_purged_through`. On Health Connect a false delete is a +// genuine failure (a denied STEPS write permission, an unmapped type, a store +// error) — `healthDeleteClearedRange` documents exactly that. The cursor +// advanced anyway, so the fabricated step samples earlier versions wrote were +// never asked for again. import 'dart:convert'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:path/path.dart' as p; import 'package:sqflite_common_ffi/sqflite_ffi.dart'; +import 'package:openstrap_edge/data/day_label.dart'; import 'package:openstrap_edge/data/db.dart'; import 'package:openstrap_edge/health/health_export.dart'; +const String kPendingCursor = 'health_steps_purge_pending'; + void main() { TestWidgetsFlutterBinding.ensureInitialized(); const channel = MethodChannel('flutter_health'); @@ -24,18 +27,14 @@ void main() { final now = DateTime.now(); DateTime dayAt(int back) => DateTime(now.year, now.month, now.day - back); - String labelOf(DateTime d) => - '${d.year.toString().padLeft(4, '0')}-' - '${d.month.toString().padLeft(2, '0')}-' - '${d.day.toString().padLeft(2, '0')}'; final older = dayAt(2); final newer = dayAt(1); - final olderDay = labelOf(older); - final newerDay = labelOf(newer); + final olderDay = dayLabelOf(older); + final newerDay = dayLabelOf(newer); // Every STEPS delete that is not the epoch-to-cursor prefix purge. - final stepDeletes = <(int, int)>[]; - var perDayDeletesFail = 0; + final stepDeletes = <({int start, int end, String pendingBefore})>[]; + final failStarts = {}; Future putDay(String day) => LocalDb.putDayResult( dayId: day, @@ -47,6 +46,18 @@ void main() { finalized: true, ); + Future> pending() async { + final raw = await LocalDb.getCursor(kPendingCursor) ?? ''; + if (raw.isEmpty) return {}; + return { + for (final entry in (jsonDecode(raw) as List).whereType()) + entry['day'] as String: ( + startMs: (entry['start_ms'] as num).toInt(), + endMs: (entry['end_ms'] as num).toInt(), + ), + }; + } + setUpAll(() async { sqfliteFfiInit(); databaseFactory = databaseFactoryFfi; @@ -61,11 +72,16 @@ void main() { final start = (args['startTime'] as num?)?.toInt() ?? 0; final end = (args['endTime'] as num?)?.toInt() ?? 0; if (args['dataTypeKey'] == 'STEPS' && start != 0) { - stepDeletes.add((start, end)); - if (perDayDeletesFail > 0) { - perDayDeletesFail--; - return false; - } + // The retry intent must already be persisted when the delete is + // issued, not written after it returns. + final pendingBefore = + await LocalDb.getCursor(kPendingCursor) ?? ''; + stepDeletes.add(( + start: start, + end: end, + pendingBefore: pendingBefore, + )); + if (failStarts.contains(start)) return false; } return true; } @@ -77,15 +93,12 @@ void main() { setUp(() async { stepDeletes.clear(); - perDayDeletesFail = 0; + failStarts.clear(); await LocalDb.setCursor('health_export_through', ''); await LocalDb.setCursor('health_steps_purged_through', ''); - await LocalDb.setCursor('health_steps_purge_pending_from', ''); + await LocalDb.setCursor(kPendingCursor, ''); await LocalDb.setCursor('health_export_retry_state', ''); - final days = await LocalDb.dayResultVersions(); - if (days.isNotEmpty) { - await (await LocalDb.instance).delete('day_result'); - } + await (await LocalDb.instance).delete('day_result'); await putDay(olderDay); await putDay(newerDay); }); @@ -102,41 +115,96 @@ void main() { }); test('an unconfirmed delete does not advance the purge cursor', () async { - perDayDeletesFail = 2; + failStarts.addAll({ + older.millisecondsSinceEpoch, + newer.millisecondsSinceEpoch, + }); await HealthExporter(isApple: false).exportAll(forceRetry: true); expect( await LocalDb.getCursor('health_steps_purged_through'), isEmpty, reason: 'neither day was confirmed purged, so neither may be recorded', ); + final left = await pending(); + expect(left.keys, containsAll([olderDay, newerDay])); + expect(left[olderDay]!.startMs, older.millisecondsSinceEpoch); + }); + + test('a later day never widens over an earlier refused day', () async { + failStarts.add(older.millisecondsSinceEpoch); + await HealthExporter(isApple: false).exportAll(forceRetry: true); + expect(stepDeletes, hasLength(2)); expect( - await LocalDb.getCursor('health_steps_purge_pending_from'), - olderDay, - reason: 'the earliest refused day is the one to retry from', + stepDeletes.last.start, + newer.millisecondsSinceEpoch, + reason: + 'the newer day clears only its own range; widening would delete ' + 'the older day\'s valid steps that nothing rewrites', ); + final left = await pending(); + expect(left.keys, [olderDay], reason: 'only the confirmed day clears'); + expect(await LocalDb.getCursor('health_steps_purged_through'), newerDay); }); - test( - 'the next day retries the refused range, then clears the marker', - () async { - perDayDeletesFail = 1; - await HealthExporter(isApple: false).exportAll(forceRetry: true); - expect(stepDeletes, hasLength(2)); - expect( - stepDeletes.first.$1, - older.millisecondsSinceEpoch, - reason: 'the first day purges its own range', - ); - expect( - stepDeletes.last.$1, - older.millisecondsSinceEpoch, - reason: 'the newer day widens its range over the day the store refused', - ); - expect(await LocalDb.getCursor('health_steps_purged_through'), newerDay); - expect( - await LocalDb.getCursor('health_steps_purge_pending_from'), - isEmpty, - ); - }, - ); + test('retry intent is persisted before the delete is issued', () async { + failStarts.add(older.millisecondsSinceEpoch); + await HealthExporter(isApple: false).exportAll(forceRetry: true); + final first = stepDeletes.first; + expect(first.start, older.millisecondsSinceEpoch); + expect(first.pendingBefore, isNotEmpty); + expect(first.pendingBefore, contains(olderDay)); + expect(first.pendingBefore, contains('${older.millisecondsSinceEpoch}')); + }); + + test('a re-exported refused day retries its stored absolute bounds', () async { + failStarts.add(older.millisecondsSinceEpoch); + await HealthExporter(isApple: false).exportAll(forceRetry: true); + final left = await pending(); + expect(left.keys, [olderDay]); + + // Move the stored start an hour earlier, as if the timezone had moved east + // between the refusal and the retry. The retry must use the stored epoch, + // not recompute midnight from the day label. + final stored = left[olderDay]!; + final earlier = stored.startMs - const Duration(hours: 1).inMilliseconds; + await LocalDb.setCursor( + kPendingCursor, + jsonEncode([ + {'day': olderDay, 'start_ms': earlier, 'end_ms': stored.endMs}, + ]), + ); + failStarts.clear(); + await LocalDb.setCursor('health_export_through', ''); + + final before = stepDeletes.length; + await HealthExporter(isApple: false).exportAll(forceRetry: true); + final retries = stepDeletes.skip(before).toList(); + expect( + retries.any((d) => d.start == earlier && d.end == stored.endMs), + isTrue, + reason: 'the retry uses the absolute bounds captured at the refusal', + ); + expect((await pending()).containsKey(olderDay), isFalse); + expect(await LocalDb.getCursor('health_steps_purged_through'), newerDay); + }); + + test('a refused later day survives an earlier day\'s success', () async { + failStarts.addAll({ + older.millisecondsSinceEpoch, + newer.millisecondsSinceEpoch, + }); + await HealthExporter(isApple: false).exportAll(forceRetry: true); + expect((await pending()).keys, containsAll([olderDay, newerDay])); + + await LocalDb.setCursor('health_export_through', ''); + failStarts + ..clear() + ..add(newer.millisecondsSinceEpoch); + await HealthExporter(isApple: false).exportAll(forceRetry: true); + + expect((await pending()).keys, [ + newerDay, + ], reason: 'clearing the older day must not clear the newer day\'s marker'); + expect(await LocalDb.getCursor('health_steps_purged_through'), olderDay); + }); } From 2290dff0a381ad04da67320448b7babe69b7c233 Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:50:16 +0530 Subject: [PATCH 3/5] health: clip a purge retry to the day being exported, keep a saved retry when the delete throws --- lib/health/health_export.dart | 46 ++++++++++++++++-------- test/health_legacy_steps_purge_test.dart | 16 ++++++--- 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/lib/health/health_export.dart b/lib/health/health_export.dart index 7e6960d90..49b059af8 100644 --- a/lib/health/health_export.dart +++ b/lib/health/health_export.dart @@ -789,23 +789,37 @@ class HealthExporter { // delete the current day's valid samples and the held write would not put // them back, so leave both the marker and the day for a later pass. if (_stepWritesHeld) return; - final range = - retry ?? - ( - startMs: dayStart.millisecondsSinceEpoch, - endMs: dayEnd.millisecondsSinceEpoch, - ); + final today = ( + startMs: dayStart.millisecondsSinceEpoch, + endMs: dayEnd.millisecondsSinceEpoch, + ); + // A retry keeps the range of the first attempt, but only the part of it + // that is still THIS day. If the timezone has moved since, the stored range + // can reach into a neighbouring day that is not being exported now, and + // that day's valid strap steps would be deleted and never rewritten. A + // clipped retry can leave a few fabricated samples behind; that is the + // lesser defect. + final stored = retry; + final clipped = stored == null + ? today + : ( + startMs: stored.startMs > today.startMs ? stored.startMs : today.startMs, + endMs: stored.endMs < today.endMs ? stored.endMs : today.endMs, + ); + final range = clipped.endMs > clipped.startMs ? clipped : today; // Retry intent BEFORE the delete: a crash after a refused delete must - // leave the day recoverable. The bounds are absolute, so a later timezone - // change cannot move the range and leave a gap. - var addedHere = false; + // leave the day recoverable. try { if (retry == null) { pending[date] = range; - addedHere = true; - // If this write fails the delete below is not issued, so an - // unrecorded attempt cannot be lost to a crash. - await _savePendingPurges(); + try { + await _savePendingPurges(); + } catch (_) { + // The delete below is not issued, so an unrecorded attempt cannot be + // lost to a crash: take the entry back out of memory too. + pending.remove(date); + rethrow; + } } // A false from the store is a genuine failure on Health Connect — the // same rule every other delete here reads through this helper — so an @@ -833,10 +847,12 @@ class HealthExporter { await LocalDb.setCursor(_kStepsPurgeCursor, date); } } catch (e) { - if (addedHere) pending.remove(date); if (e is TimeoutException) _storeHung = true; // Either the retry intent is on disk already, or no delete was issued. - // Both leave the day retryable and the cursor aiming at it. + // Both leave the day retryable and the cursor aiming at it. The entry is + // NOT taken out of memory here: a delete that threw after the intent was + // saved must keep it, or the next save for another day would rewrite the + // file from the map and drop it. debugPrint('[health] purge legacy steps $date: $e'); } } diff --git a/test/health_legacy_steps_purge_test.dart b/test/health_legacy_steps_purge_test.dart index 61eccb806..61c1580c8 100644 --- a/test/health_legacy_steps_purge_test.dart +++ b/test/health_legacy_steps_purge_test.dart @@ -156,15 +156,16 @@ void main() { expect(first.pendingBefore, contains('${older.millisecondsSinceEpoch}')); }); - test('a re-exported refused day retries its stored absolute bounds', () async { + test('a retry is clipped to the part of its stored range that is still that day', () async { failStarts.add(older.millisecondsSinceEpoch); await HealthExporter(isApple: false).exportAll(forceRetry: true); final left = await pending(); expect(left.keys, [olderDay]); // Move the stored start an hour earlier, as if the timezone had moved east - // between the refusal and the retry. The retry must use the stored epoch, - // not recompute midnight from the day label. + // between the refusal and the retry. The stored range now reaches an hour + // into the PREVIOUS day, whose valid strap steps nothing rewrites, so the + // retry is clipped to this day instead of using the stored epoch verbatim. final stored = left[olderDay]!; final earlier = stored.startMs - const Duration(hours: 1).inMilliseconds; await LocalDb.setCursor( @@ -180,9 +181,14 @@ void main() { await HealthExporter(isApple: false).exportAll(forceRetry: true); final retries = stepDeletes.skip(before).toList(); expect( - retries.any((d) => d.start == earlier && d.end == stored.endMs), + retries.any((d) => d.start == stored.startMs && d.end == stored.endMs), isTrue, - reason: 'the retry uses the absolute bounds captured at the refusal', + reason: 'the retry covers this day', + ); + expect( + retries.any((d) => d.start == earlier), + isFalse, + reason: 'and never reaches into the previous day', ); expect((await pending()).containsKey(olderDay), isFalse); expect(await LocalDb.getCursor('health_steps_purged_through'), newerDay); From f86e3aa5bd18dd1a9b5fbd8f961ba491a84db21b Mon Sep 17 00:00:00 2001 From: KuljeetSinghXDA <22261392+KuljeetSinghXDA@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:59:46 +0530 Subject: [PATCH 4/5] fix(health): hold day out of clean exports when legacy purge is refused --- lib/health/health_export.dart | 30 ++++--- test/health_legacy_steps_purge_test.dart | 102 +++++++++++++++-------- 2 files changed, 83 insertions(+), 49 deletions(-) diff --git a/lib/health/health_export.dart b/lib/health/health_export.dart index 49b059af8..5d4331b08 100644 --- a/lib/health/health_export.dart +++ b/lib/health/health_export.dart @@ -758,17 +758,17 @@ class HealthExporter { /// Delete the fabricated STEPS samples earlier versions wrote for [date]. /// - /// ONE-SHOT, and deliberately not part of the day's success accounting: this - /// is a migration cleaning up data we should never have written, not part of - /// exporting the day. A failure here must not stall the export cursor for a - /// type nothing writes. Days are walked ascending, so the cursor advances - /// monotonically and a re-exported tail day is not re-purged. + /// Returns true when [date] is confirmed purged (or already behind the + /// cursor with no pending retry) and false when the purge could not be + /// confirmed. Callers treat false like a purge hold (`_dayHeldForPurge`) + /// so `health_export_through` does not advance past [date] and an ordinary + /// later pass retries it (delete-then-write) without spending an attempt. /// /// STRICTLY ONE DAY PER DELETE. A retry keeps that day's own absolute bounds /// and runs only while that day is being exported again, which rewrites its /// steps immediately afterwards. Widening a delete over earlier days would /// erase valid strap-counter samples that nothing rewrites. - Future _purgeLegacyStepsIfNeeded( + Future _purgeLegacyStepsIfNeeded( String date, DateTime dayStart, DateTime dayEnd, @@ -782,13 +782,13 @@ class HealthExporter { // runs before its new steps. Days at or before the confirmed cursor // without a retry are done. if (retry == null && through.isNotEmpty && date.compareTo(through) <= 0) { - return; + return true; } - if (_storeHung) return; + if (_storeHung) return false; // Steps are held while the prefix purge has not finished. A purge now would // delete the current day's valid samples and the held write would not put // them back, so leave both the marker and the day for a later pass. - if (_stepWritesHeld) return; + if (_stepWritesHeld) return false; final today = ( startMs: dayStart.millisecondsSinceEpoch, endMs: dayEnd.millisecondsSinceEpoch, @@ -836,7 +836,7 @@ class HealthExporter { if (!cleared) { debugPrint('[health] purge legacy steps $date: the store did not ' 'confirm the delete'); - return; + return false; } // ONLY this day. This delete did not cover another pending day's range, // so that marker must survive for its own re-export. @@ -846,6 +846,7 @@ class HealthExporter { _stepsPurgedThrough = date; await LocalDb.setCursor(_kStepsPurgeCursor, date); } + return true; } catch (e) { if (e is TimeoutException) _storeHung = true; // Either the retry intent is on disk already, or no delete was issued. @@ -854,6 +855,7 @@ class HealthExporter { // saved must keep it, or the next save for another day would rewrite the // file from the map and drop it. debugPrint('[health] purge legacy steps $date: $e'); + return false; } } @@ -1528,8 +1530,12 @@ class HealthExporter { } // One-shot cleanup of the fabricated step samples earlier versions wrote. - // Outside the success accounting on purpose — see the method doc. - await _purgeLegacyStepsIfNeeded(date, dayStart, dayEnd); + // A refused purge holds the day out of the clean-export set (like a steps + // hold) so the cursor does not skip it and an ordinary pass retries it. + if (!await _purgeLegacyStepsIfNeeded(date, dayStart, dayEnd)) { + success = false; + _dayHeldForPurge = true; + } // Continuous Heart Rate (minute-by-minute average). List>? hrRows; diff --git a/test/health_legacy_steps_purge_test.dart b/test/health_legacy_steps_purge_test.dart index 61c1580c8..5ab527e15 100644 --- a/test/health_legacy_steps_purge_test.dart +++ b/test/health_legacy_steps_purge_test.dart @@ -156,43 +156,46 @@ void main() { expect(first.pendingBefore, contains('${older.millisecondsSinceEpoch}')); }); - test('a retry is clipped to the part of its stored range that is still that day', () async { - failStarts.add(older.millisecondsSinceEpoch); - await HealthExporter(isApple: false).exportAll(forceRetry: true); - final left = await pending(); - expect(left.keys, [olderDay]); - - // Move the stored start an hour earlier, as if the timezone had moved east - // between the refusal and the retry. The stored range now reaches an hour - // into the PREVIOUS day, whose valid strap steps nothing rewrites, so the - // retry is clipped to this day instead of using the stored epoch verbatim. - final stored = left[olderDay]!; - final earlier = stored.startMs - const Duration(hours: 1).inMilliseconds; - await LocalDb.setCursor( - kPendingCursor, - jsonEncode([ - {'day': olderDay, 'start_ms': earlier, 'end_ms': stored.endMs}, - ]), - ); - failStarts.clear(); - await LocalDb.setCursor('health_export_through', ''); - - final before = stepDeletes.length; - await HealthExporter(isApple: false).exportAll(forceRetry: true); - final retries = stepDeletes.skip(before).toList(); - expect( - retries.any((d) => d.start == stored.startMs && d.end == stored.endMs), - isTrue, - reason: 'the retry covers this day', - ); - expect( - retries.any((d) => d.start == earlier), - isFalse, - reason: 'and never reaches into the previous day', - ); - expect((await pending()).containsKey(olderDay), isFalse); - expect(await LocalDb.getCursor('health_steps_purged_through'), newerDay); - }); + test( + 'a retry is clipped to the part of its stored range that is still that day', + () async { + failStarts.add(older.millisecondsSinceEpoch); + await HealthExporter(isApple: false).exportAll(forceRetry: true); + final left = await pending(); + expect(left.keys, [olderDay]); + + // Move the stored start an hour earlier, as if the timezone had moved east + // between the refusal and the retry. The stored range now reaches an hour + // into the PREVIOUS day, whose valid strap steps nothing rewrites, so the + // retry is clipped to this day instead of using the stored epoch verbatim. + final stored = left[olderDay]!; + final earlier = stored.startMs - const Duration(hours: 1).inMilliseconds; + await LocalDb.setCursor( + kPendingCursor, + jsonEncode([ + {'day': olderDay, 'start_ms': earlier, 'end_ms': stored.endMs}, + ]), + ); + failStarts.clear(); + await LocalDb.setCursor('health_export_through', ''); + + final before = stepDeletes.length; + await HealthExporter(isApple: false).exportAll(forceRetry: true); + final retries = stepDeletes.skip(before).toList(); + expect( + retries.any((d) => d.start == stored.startMs && d.end == stored.endMs), + isTrue, + reason: 'the retry covers this day', + ); + expect( + retries.any((d) => d.start == earlier), + isFalse, + reason: 'and never reaches into the previous day', + ); + expect((await pending()).containsKey(olderDay), isFalse); + expect(await LocalDb.getCursor('health_steps_purged_through'), newerDay); + }, + ); test('a refused later day survives an earlier day\'s success', () async { failStarts.addAll({ @@ -213,4 +216,29 @@ void main() { ], reason: 'clearing the older day must not clear the newer day\'s marker'); expect(await LocalDb.getCursor('health_steps_purged_through'), olderDay); }); + + test('an ordinary later pass retries a refused finalized day', () async { + failStarts.add(older.millisecondsSinceEpoch); + await HealthExporter(isApple: false).exportAll(forceRetry: true); + expect((await pending()).keys, [olderDay]); + expect( + await LocalDb.getCursor('health_export_through'), + isEmpty, + reason: 'the refused purge holds the export cursor behind the day', + ); + + failStarts.clear(); + final before = stepDeletes.length; + await HealthExporter(isApple: false).exportAll(); + expect( + stepDeletes + .skip(before) + .any((d) => d.start == older.millisecondsSinceEpoch), + isTrue, + reason: 'an ordinary pass retries the held finalized day', + ); + expect(await pending(), isEmpty); + expect(await LocalDb.getCursor('health_steps_purged_through'), newerDay); + expect(await LocalDb.getCursor('health_export_through'), newerDay); + }); } From 16dabbcb2c898807b8a1a129227a4638a7980ae0 Mon Sep 17 00:00:00 2001 From: KuljeetSinghXDA <22261392+KuljeetSinghXDA@users.noreply.github.com> Date: Tue, 17 Nov 2026 01:17:17 +0530 Subject: [PATCH 5/5] fix(health): hold purge cursor when saving retry intent fails --- lib/health/health_export.dart | 15 ++++++++--- test/health_legacy_steps_purge_test.dart | 32 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/lib/health/health_export.dart b/lib/health/health_export.dart index 5d4331b08..f0df57144 100644 --- a/lib/health/health_export.dart +++ b/lib/health/health_export.dart @@ -539,6 +539,11 @@ class HealthExporter { /// behind the purge: exportAll keeps it pending without spending an attempt. bool _dayHeldForPurge = false; + /// Set when saving a day's purge retry intent fails in the current pass so + /// a later day's confirmed delete cannot advance `_stepsPurgedThrough` past + /// the unrecorded day. + bool _stepsPurgeCursorHeld = false; + final bool? _isAppleOverride; HealthExporter({ @@ -784,7 +789,7 @@ class HealthExporter { if (retry == null && through.isNotEmpty && date.compareTo(through) <= 0) { return true; } - if (_storeHung) return false; + if (_storeHung || _stepsPurgeCursorHeld) return false; // Steps are held while the prefix purge has not finished. A purge now would // delete the current day's valid samples and the held write would not put // them back, so leave both the marker and the day for a later pass. @@ -816,8 +821,10 @@ class HealthExporter { await _savePendingPurges(); } catch (_) { // The delete below is not issued, so an unrecorded attempt cannot be - // lost to a crash: take the entry back out of memory too. + // lost to a crash: take the entry back out of memory and hold the + // purge cursor so a later day in this pass cannot jump over this one. pending.remove(date); + _stepsPurgeCursorHeld = true; rethrow; } } @@ -842,7 +849,8 @@ class HealthExporter { // so that marker must survive for its own re-export. pending.remove(date); await _savePendingPurges(); - if (through.isEmpty || date.compareTo(through) > 0) { + if (!_stepsPurgeCursorHeld && + (through.isEmpty || date.compareTo(through) > 0)) { _stepsPurgedThrough = date; await LocalDb.setCursor(_kStepsPurgeCursor, date); } @@ -1098,6 +1106,7 @@ class HealthExporter { if (await _androidUnavailable() != null) return 0; // HC missing/outdated return _workoutLock.run(() async { _storeHung = false; + _stepsPurgeCursorHeld = false; var days = 0; try { await ensureHealthSleepExportEpoch( diff --git a/test/health_legacy_steps_purge_test.dart b/test/health_legacy_steps_purge_test.dart index 5ab527e15..fd832ca20 100644 --- a/test/health_legacy_steps_purge_test.dart +++ b/test/health_legacy_steps_purge_test.dart @@ -241,4 +241,36 @@ void main() { expect(await LocalDb.getCursor('health_steps_purged_through'), newerDay); expect(await LocalDb.getCursor('health_export_through'), newerDay); }); + + test( + 'an intent-save failure never lets a later day skip the failed day', + () async { + final db = await LocalDb.instance; + await db.execute( + "CREATE TRIGGER fail_pending_save BEFORE INSERT ON sync_cursor " + "WHEN NEW.name = '$kPendingCursor' AND NEW.value LIKE '%$olderDay%' " + "BEGIN SELECT RAISE(ABORT, 'simulated save failure'); END;", + ); + try { + await HealthExporter(isApple: false).exportAll(forceRetry: true); + } finally { + await db.execute('DROP TRIGGER IF EXISTS fail_pending_save'); + } + + // Even if the newer day purged cleanly, the purge cursor must not jump + // past the older day whose intent save failed. + expect(await LocalDb.getCursor('health_steps_purged_through'), isEmpty); + + final before = stepDeletes.length; + await HealthExporter(isApple: false).exportAll(); + expect( + stepDeletes + .skip(before) + .any((d) => d.start == older.millisecondsSinceEpoch), + isTrue, + reason: 'the older day still purges once cursor saves work again', + ); + expect(await LocalDb.getCursor('health_steps_purged_through'), newerDay); + }, + ); }