Skip to content

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

Closed
aryanorastar wants to merge 2 commits into
BasedHardware:mainfrom
aryanorastar:fix/5734-limitless-clock-drift
Closed

aryanorastar wants to merge 2 commits into
BasedHardware:mainfrom
aryanorastar:fix/5734-limitless-clock-drift

Conversation

@aryanorastar

@aryanorastar aryanorastar commented Aug 5, 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: the backend merge (get_closest_conversation_to_timestamps) 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.

Part 1: Parse Type-8 BLE notifications (f4 → f8 → f6 EPOCH_MS) during the connect listen window, store clockDriftOffsetMs = pendant − phone before SetCurrentTime, and freeze that reading (post-sync Type-8s would show ~0 drift). _timeSynced is set before the write so a mid-write Type-8 cannot clobber the offline offset.

Part 2: In flash_page_wal_sync.dart, subtract the offset from page timestamps via correctedFlashPageTimestampMs before session-gap detection and filename embedding. Drift is applied only when a real page timestamp was parsed — the DateTime.now() fallback is already phone time and is not double-corrected.

Out of scope: Part 3 sync-mode toggle; #5733 firmware diagnostic pages.

Fixes #5734.

Product invariants affected

none

How it was verified

cd app && flutter test test/unit/limitless_clock_drift_test.dart
# 9 passed
cd app && dart analyze lib/services/devices/connectors/limitless_connection.dart \
  lib/services/wals/flash_page_wal_sync.dart test/unit/limitless_clock_drift_test.dart
# No issues found

Could not exercise a live Limitless pendant reconnect + duplicate-conversation repro in this environment. Parser + connect-time capture + post-sync freeze + flash correction helper (including missing-timestamp path) are unit-tested.

Tests

app/test/unit/limitless_clock_drift_test.dart — Type-8 f8→f6 / nested f8→f6→f1 parse; unterminated varint; connect captures drift before msg6; post-sync Type-8 does not clobber; correctedFlashPageTimestampMs subtracts drift / skips double-correct / null drift.

Failure class (fixes)

Failure-Class: none

Scoped cleanups (optional)

n/a

…rdware#5734)

Measure pendant−phone drift from Type-8 BLE clock notifications before
SetCurrentTime, then subtract that offset during flash-page WAL sync so
batch uploads overlap real-time conversations instead of duplicating.

Co-authored-by: Cursor <[email protected]>
@aryanorastar
aryanorastar requested a review from mdmohsin7 as a code owner August 5, 2026 16:29
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 3 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

- Only subtract drift when a real flash page timestamp exists (avoid
  double-correcting DateTime.now fallback).
- Freeze _timeSynced before SetCurrentTime write so mid-write Type-8s
  cannot clobber the offline offset.
- Reject unterminated/overlong varints in Type-8 parse.
- Replace tautological math test with correctedFlashPageTimestampMs cases;
  clears prefer_const_declarations analyzer ratchet.

Co-authored-by: Cursor <[email protected]>
@aryanorastar

Copy link
Copy Markdown
Collaborator Author

Follow-up f89affe0 addresses all four cubic findings + the analyzer ratchet:

  1. Freeze _timeSynced before SetCurrentTime write
  2. Reject unterminated/overlong Type-8 varints
  3. Only apply drift when a real flash timestamp_ms exists (no double-correct of DateTime.now() fallback)
  4. Replace tautological test with correctedFlashPageTimestampMs cases (also clears prefer_const_declarations ratchet)

flutter test test/unit/limitless_clock_drift_test.dart → 9 passed; dart analyze on touched files → no issues.

@Git-on-my-level Git-on-my-level added needs-maintainer-review Needs a human maintainer to sign off before merge flutter flutter work labels Aug 5, 2026
@Git-on-my-level

Copy link
Copy Markdown
Member

Thanks for this clean fix for #5734 — the drift-capture-then-freeze design is sound and the fail-open behavior (null offset => unchanged timestamps) avoids regressions when no Type-8 clock notification arrives before SetCurrentTime.

A few observations from static review:

  1. correctedFlashPageTimestampMs correctly returns phoneNowMs untouched when pageTimestampMs is null, so the DateTime.now() fallback is never double-corrected. Good.
  2. Setting _timeSynced = true before the writeCharacteristic await (rather than after) is the right call — it prevents a mid-write Type-8 from clobbering the offline offset with a near-zero post-sync reading.
  3. The varint hardening (returning [null, startPos] on unterminated or shift > 63) is a solid defensive improvement over the original loop.
  4. The pendantEpochMs <= 1577836800000 sanity floor (2020-01-01) correctly rejects bogus epochs.

The one gap you already flagged: drift capture depends on the pendant spontaneously emitting a Type-8 clock notification during the connect listen window, which couldn't be validated against real hardware here. The parser + freeze + correction logic is well unit-tested, so the main remaining validation is a live pendant reconnect + duplicate-conversation repro.

CI is green (Dart Analyze & Tests, Android Compile Smoke, Backend Hermetic Merge Gate). Requesting human maintainer sign-off before merge given the hardware-validation gap. Nice work.


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

@aryanorastar

Copy link
Copy Markdown
Collaborator Author

@Git-on-my-level Thanks for the positive signal.

Agreed on the hardware gap — drift capture depends on a live Type-8 during the connect window, which we couldn't exercise here. Parser + freeze-before-write + correctedFlashPageTimestampMs (including null-timestamp / no-double-correct) are covered by unit tests @f89affe0.

No further author code changes pending. Leaving live pendant reconnect + duplicate-conversation smoke and APPROVE/merge to @mdmohsin7 / maintainers.

@aryanorastar

Copy link
Copy Markdown
Collaborator Author

@mdmohsin7 Friendly code-owner bump on #5734.

Head @f89affe0 is still fully green (Dart Analyze, Android Compile, Preflight). David’s pass confirmed the drift-capture-then-freeze design and cubic hardenings; remaining gap is live Limitless pendant reconnect + duplicate-conversation smoke (couldn’t exercise hardware here).

If the approach looks good (or you’re comfortable merging on the unit-tested parser/freeze/correction with fail-open null offset), an APPROVE from you clears the merge gate. Thanks!

@mdmohsin7 mdmohsin7 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please provide tests with the limitless device. There shouldn't be any skew/drift in time because each flashpage of limitless has a timestamp and we read that timestamp while parsing the flashpage

@aryanorastar

Copy link
Copy Markdown
Collaborator Author

@mdmohsin7 Thanks for the review — pausing code changes until we align on the diagnosis.

On device tests: I don't have a Limitless pendant in this environment, so I can't run a live reconnect + flash-download + duplicate-conversation repro. What's on the PR today is hermetic only (limitless_clock_drift_test.dart + existing golden transport fakes). If device-side smoke is required for merge, I'll need that validation from someone with hardware (or guidance on the preferred fixture/harness if one exists in-tree).

On "no skew because flash pages already have timestamps": Agreed we already parse Field 1 timestamp_ms in _parseFlashPageInfo and use it in WAL sync. The #5734 claim (which this PR followed) was not "missing timestamps," but a clock-domain mismatch:

  • real-time segments land in phone wall time
  • flash Field 1 is stamped from the pendant RTC at write time
  • _initialize() only does forward SetCurrentTime and never measures pre-sync pendant clock, so pages written while the RTC had drifted keep those RTC values; backend merge (get_closest_conversation_to_timestamps) then misses overlap → duplicates

If your read is that Field 1 is already phone-aligned absolute epoch (so subtracting connect-time Type-8 drift would be wrong / unnecessary), then this PR's Part 2 is the wrong fix and I should close it rather than push more correction logic.

Could you confirm which is correct?

  1. Flash timestamp_ms can diverge from phone time across an offline window → correction (or another approach) is needed for Limitless pendant: clock drift causes duplicate conversations and missing timestamp correction #5734 duplicates, or
  2. Flash timestamps are already trustworthy → Limitless pendant: clock drift causes duplicate conversations and missing timestamp correction #5734 duplicates have a different root cause (happy to retarget / close this PR and follow your preferred direction)

Either way I won't ship more drift math without your call + device confirmation. Thanks.

@aryanorastar
aryanorastar requested a review from mdmohsin7 August 6, 2026 06:40
@mdmohsin7

Copy link
Copy Markdown
Member

real-time segments land in phone wall time

This is an assumption

so pages written while the RTC had drifted keep those RTC values

That is not how the limitless device works. It does not have any drift.

And besides the issue was already fixed by the person who opened the issue

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants