Skip to content

fix(app): correct Limitless flash timestamps for clock drift (#5734) - #14291

Merged
kodjima33 merged 2 commits into
BasedHardware:mainfrom
apoorvdarshan:cursor/limitless-clock-drift-078c
Sep 17, 2026
Merged

kodjima33 merged 2 commits into
BasedHardware:mainfrom
apoorvdarshan:cursor/limitless-clock-drift-078c

Conversation

@apoorvdarshan

@apoorvdarshan apoorvdarshan commented Sep 17, 2026 •

Copy link
Copy Markdown
Collaborator

What changed and why

Limitless duplicates conversations when real-time streaming and batch flash download cover the same audio: backend merge (get_closest_conversation_to_timestamps, ±2 min) works when timestamps overlap, but pendant RTC drift leaves stored flash pages misaligned with phone-time real-time segments. _initialize() only forwards SetCurrentTime and never measures drift; flash WAL sync used raw page timestamps.

This is a fresh client-only implementation of #5734 (not a revive of closed #11145). Differences from that PR:

  • Parser + correction live in a pure LimitlessClockDrift helper so they can be unit-tested without a BLE stack or the 1.9k-line connection class.
  • Connect waits the existing 1s RX window for a Type-8 clock and returns early when one arrives, then freezes _timeSynced before the SetCurrentTime write so a mid-write Type-8 cannot clobber the offline offset.
  • Parser accepts BLE-wrapped f4→f8→f6, nested f8→f6→f1, and f1=8 + sibling f6; rejects mode-only Type-8, unterminated varints, and epochs below the 2020 floor.
  • Implausible offsets (|drift| > 7 days) fail open (no correction).
  • correctedFlashPageTimestampMs subtracts drift only for a real page RTC. Missing/zero/pre-2020 timestamps return phone-now unchanged — extractFramesWithSessionInfo no longer bakes DateTime.now() into timestamp_ms so the helper can tell those apart (the fix(app): correct Limitless flash timestamps for clock drift (#5734) #11145 null-only check still double-corrected that fallback).

Out of scope: Part 3 sync-mode toggle; #5733 firmware diagnostic pages. Complementary to backend FC-offline-merge-clock-offset-dup (#11165), which still catches residual clock-offset dups after merge.

Fixes #5734.

Product invariants affected

none

How it was verified

# Flutter is not installed in this Cloud VM (backend-only snapshot).
# Helper has no Flutter/package imports; ran the same assertions via Dart 3.8.1:
dart run /tmp/limitless_clock_drift_standalone_test.dart
# 15 checks passed: f8→f6, f8→f6→f1, BLE wrap, f1=8+f6, missing field,
# mode-only Type-8, unterminated varint, below-floor epoch, measure,
# implausible drift, bad pendant, subtract / null / zero / null-drift correction.

CI should run cd app && flutter test test/unit/limitless_clock_drift_test.dart (same cases via flutter_test).

Could not exercise a live Limitless pendant reconnect + duplicate-conversation repro in this environment. Could not run the full app/test.sh suite (no Flutter SDK).

Tests

app/test/unit/limitless_clock_drift_test.dart — Type-8 parse (f8→f6, nested f8→f6→f1, BLE wrap, f1=8 sibling f6, missing field, mode-only, unterminated varint, below-floor); measureClockDriftOffsetMs (offset / implausible / invalid); correctedFlashPageTimestampMs (subtract / skip missing / skip zero / null drift).

Failure class (fixes)

Failure-Class: FC-offline-merge-clock-offset-dup

Scoped cleanups (optional)

n/a

Review in cubic

Measure pendant-phone drift from Type-8 clock notifications before
SetCurrentTime, then subtract that offset from flash-page timestamps
used for session-gap detection and WAL filenames so backend merge
dedupes real-time and batch conversations.

Failure-Class: FC-offline-merge-clock-offset-dup

Co-authored-by: Apoorv Darshan <[email protected]>
@apoorvdarshan
apoorvdarshan marked this pull request as ready for review September 17, 2026 01:35
Reformat the two files Formatting CI flagged with the Flutter 3.44.5 /
Dart 3.12.2 formatter at language version 3.0 and --line-length 120.

No behavior changes.

Co-authored-by: Apoorv Darshan <[email protected]>
@apoorvdarshan

Copy link
Copy Markdown
Collaborator Author

Pushed a format-only commit to this branch (84678684b9): ran dart format --line-length 120 on app/lib/services/devices/connectors/limitless_connection.dart and app/test/unit/limitless_clock_drift_test.dart with Dart 3.12.2 (Flutter 3.44.5) at package language version 3.0. No behavior changes — should clear the Formatting check.

@Git-on-my-level

Copy link
Copy Markdown
Member

Thanks @apoorvdarshan — this is a well-constructed fix for #5734. Reviewed all four files; the Dart-side implementation looks solid, with one cross-platform question before this ships.

What I verified:

  • limitless_clock_drift.dart: clean pure helper. The protobuf walker handles the three observed Type-8 shapes (f4-wrapped f8->f6, nested f8->f6->f1, f1=8 + sibling f6), rejects mode-only Type-8 and unterminated varints, floors epochs at 2020-01-01, and clamps implausible offsets at 7 days — fail-open (no correction) on every rejection path, which is the right bias for a timestamp fix.
  • limitless_connection.dart: capturing drift only from the first pre-sync Type-8 and freezing _timeSynced before the SetCurrentTime write (so a mid-write Type-8 can't clobber the offline offset) is the subtle part of this fix and it's handled correctly. The 1s connect wait is reused rather than extended, so no new latency when no clock arrives.
  • flash_page_wal_sync.dart: correctedFlashPageTimestampMs(pageTimestampMs: pageData['timestamp_ms'] as int?, ...) now tolerates the nullable shape, and I confirmed extractFramesWithSessionInfo has exactly one consumer (this file); the helper always returns a non-null int, so the downstream filename/session-gap arithmetic keeps its old behavior. The no-double-correction of the DateTime.now() fallback (also handled by dropping the fallback from the two extraction return sites in limitless_connection.dart) is a real improvement over the earlier closed attempt.
  • limitless_clock_drift_test.dart: 15 tests covering parse shapes, rejections, drift math, and the correction semantics, including the zero/unset-timestamp case.

One material question — native-path parity. limitless_golden_test.dart's own regeneration note says an intentional change to pinned Dart protocol behavior requires updating LimitlessProtocol.kt to match. The native Android flash-drain path (LimitlessFlashDrainEngine.kt, wired in OmiBleForegroundService.kt, with the Swift twin in app/ios/Runner/Limitless/) still appends pages with raw page.timestampMs and LimitlessBatchAudioWriter.kt still names and session-splits batch files from that uncorrected pendant RTC. If those native Transcribe-Later drains are live production flash-download routes feeding the same backend merge, the duplicate-conversation bug this PR fixes on the Flutter path will persist there. Was leaving the native ports untouched a deliberate scope decision (i.e. the native drain is not yet reachable in production), and if not, should the drift correction be ported before this merges? A one-line note on the intended rollout would unblock this.

Minor, non-blocking: a single connect-time offset is applied uniformly to pages written across the whole offline window, but RTC drift is a rate — pages from early in a long offline window can stay misaligned beyond the backend's ±2 min merge tolerance. The PR description says backend #11165 catches residual dups, so this may be acceptable; worth confirming that's the safety net.

Cross-platform parity decision needed (Dart-only fix vs the Kotlin/Swift drain engines) — leaving for human maintainer review.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Git-on-my-level Git-on-my-level added flutter flutter work positive-signal Automation verified a genuine fix/quality contribution needs-maintainer-review Needs a human maintainer to sign off before merge labels Sep 17, 2026

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confidence 4/5: root cause stated, fixes #5734, CI green, adds tests (diff size only miss). Confirmed not already fixed on main. Merging.

@kodjima33
kodjima33 merged commit 02b6c3a into BasedHardware:main Sep 17, 2026
32 of 33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flutter flutter work needs-maintainer-review Needs a human maintainer to sign off before merge positive-signal Automation verified a genuine fix/quality contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Limitless pendant: clock drift causes duplicate conversations and missing timestamp correction

4 participants