From e4eea9fbeedb8ec1df26c3bd1861cfbc93225299 Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Sat, 28 Nov 2026 21:52:13 +0530 Subject: [PATCH] import: let a longer noop export finish a day an earlier one cut off mid-day --- lib/compute/derivation_engine.dart | 28 +++++++++++++++++++++++++-- test/derive_day_window_test.dart | 31 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/lib/compute/derivation_engine.dart b/lib/compute/derivation_engine.dart index 872bf5b82..03282ea02 100644 --- a/lib/compute/derivation_engine.dart +++ b/lib/compute/derivation_engine.dart @@ -6808,7 +6808,9 @@ class DerivationEngine { // so importing a file that overlaps real history destroyed it // silently and permanently. Same guard the WHOOP importer has always // had; it just was not shared. - if (await LocalDb.isMeasuredDay(day.date)) { + final existing = await LocalDb.dayResult(day.date); + if (LocalDb.isMeasuredDayRow(existing) && + !importReachesFurther(existing, dataNowSec)) { _log('import day ${day.date} kept (already measured on this device)'); continue; } @@ -7874,7 +7876,14 @@ class DerivationEngine { // test compares the row's wake with THIS edge, not a fresher one read // later: ingest keeps committing during a pass, and a newer edge would // call a night settled whose wake came from a truncated substrate. - payloadJson: jsonEncode({...bundle, 'data_edge_sec': dataNowSec}), + payloadJson: jsonEncode({ + ...bundle, + 'data_edge_sec': dataNowSec, + // An import's snapshot of however much its file held: a later import + // reaching further into the day may replace it + // ([importReachesFurther]). Only deriveImportedDays force-finalizes. + if (forceFinalize && suppliedSubstrate) 'import_snapshot': true, + }), windowJson: jsonEncode( ((day.sleepJson['window'] as Map?) ?? const {}).cast(), ), @@ -8974,6 +8983,21 @@ class DerivationEngine { static Map? _decodeBundle(Object? json) => SeriesCodec.decodePayloadJson(json); + /// True when [row] was written by an earlier [deriveImportedDays] and the + /// new import's [edgeSec] reaches past the edge that row was derived + /// against. An export taken mid-day force-finalizes that day from the + /// morning only; a later, longer export of the same range must be able to + /// finish it. The same file again, or an older shorter one, reaches no + /// further and keeps the row. A band-derived row never carries the marker. + @visibleForTesting + static bool importReachesFurther(Map? row, int edgeSec) { + if (row == null) return false; + final p = _decodeBundle(row['payload_json']); + if (p == null || p['import_snapshot'] != true) return false; + final prevEdge = (p['data_edge_sec'] as num?)?.toInt(); + return prevEdge != null && edgeSec > prevEdge; + } + /// One `day_result` row as a cross-day input record, with the per-day /// masks the rollup needs. Static and pure so it runs in the isolate. @visibleForTesting diff --git a/test/derive_day_window_test.dart b/test/derive_day_window_test.dart index 5dc39d62d..ee4166772 100644 --- a/test/derive_day_window_test.dart +++ b/test/derive_day_window_test.dart @@ -375,4 +375,35 @@ void main() { expect(n, 1); expect(debugSegmentedDayCount, 1); }); + + // An export taken mid-day finalizes that day from the morning only. A later, + // longer export of the same day must be able to finish it. + test('a later import that reaches further replaces a partial import day', + () async { + const day = '2026-04-27'; + final start = DateTime(2026, 4, 27, 9).millisecondsSinceEpoch ~/ 1000; + Future imp(int hours) => DerivationEngine().deriveImportedDays( + _synthDay(start, hours * 3600), const Profile(), {day}); + Future edge() async => (jsonDecode( + (await LocalDb.dayResult(day))!['payload_json'] as String) + as Map)['data_edge_sec'] as int?; + + expect(await imp(2), 1); + final partialEdge = await edge(); + expect(await imp(6), 1, reason: 'the longer export finishes the day'); + expect(await edge(), greaterThan(partialEdge!)); + expect(await imp(6), 0, reason: 'the same file again changes nothing'); + expect(await imp(2), 0, reason: 'an older shorter file never wins'); + }); + + test('a band-derived day never yields to an import', () { + final row = { + 'payload_json': jsonEncode({'data_edge_sec': 100}), + }; + expect(DerivationEngine.importReachesFurther(row, 200), isFalse); + row['payload_json'] = + jsonEncode({'data_edge_sec': 100, 'import_snapshot': true}); + expect(DerivationEngine.importReachesFurther(row, 200), isTrue); + expect(DerivationEngine.importReachesFurther(row, 100), isFalse); + }); }