Fix remaining health review issues and document ring clock skew

This commit is contained in:
James Babcock committed 2026-09-17 21:21:05 -07:00
1 parent 7677dd254e
commit 27a5fe47ef
10 files changed
+323 -191

No files matched your search

@@ -87,6 +87,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private final java.util.List<FaceclawMicStatusListener> micStatusListeners =
new java.util.concurrent.CopyOnWriteArrayList<>();
private volatile Thread workerThread;
// Ring connect/write/health waits must never park the glasses sender.
// FaceclawBleManager serializes GATT operations per device address.
private volatile Thread ringWorkerThread;
private volatile boolean running;
private volatile boolean userDisconnectRequested;
// Set when a connect attempt failed while an arm's Android bond is gone:
@@ -106,7 +109,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
// inline because notifications arrive on the GATT callback thread and
// bleManager.writeFrames() blocks waiting for onCharacteristicWrite, which
// that same thread has to deliver — writing there would deadlock until the
// write timeout. The worker loop drains the queue instead.
// write timeout. The ring worker drains the queue instead.
private final RingProtocol.Reassembler ringReassembler = new RingProtocol.Reassembler();
private final ArrayDeque<byte[]> ringOutbound = new ArrayDeque<>();
private final List<RingProtocol.HealthRecord> ringHealthRecords = new ArrayList<>();
@@ -159,7 +162,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private long ringHealthLastRequestedAtMs;
/**
* Set by {@link #requestRingHealthNow()} from whatever thread the UI is on;
* cleared by the worker loop, which is the only thread allowed to run the
* cleared by the ring worker, which is the only thread allowed to run the
* pull. Guarded by {@code lock}.
*/
private boolean ringHealthPullRequested;
@@ -389,6 +392,10 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
activeInstance = this;
workerThread = new Thread(this, "FaceclawBleCommunicator");
workerThread.start();
if (hasRingAddress()) {
ringWorkerThread = new Thread(this::runRingWorker, "FaceclawRing");
ringWorkerThread.start();
}
}
}
@@ -401,15 +408,19 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
releaseFaceclawFramebufferLease();
}
Thread threadToJoin;
Thread ringThreadToJoin;
synchronized (lock) {
userDisconnectRequested = true;
running = false;
audioCaptureActive = false;
audioPacketListener = null;
threadToJoin = workerThread;
ringThreadToJoin = ringWorkerThread;
lock.notifyAll();
}
setStateDisplay("disconnecting", "Disconnecting...");
interruptibleSleep.interrupt();
if (ringThreadToJoin != null) ringThreadToJoin.interrupt();
if (threadToJoin != null) {
threadToJoin.interrupt();
try {
@@ -418,8 +429,16 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
Thread.currentThread().interrupt();
}
}
if (ringThreadToJoin != null) {
try {
ringThreadToJoin.join(5_000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
synchronized (lock) {
workerThread = null;
ringWorkerThread = null;
resetSessionStateLocked();
clearAllMessagesLocked("disconnect");
// Unknown until the next connection's first push or settings poll.
@@ -1705,24 +1724,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
continue;
}
if (shouldAttemptRingConnect()) {
tryConnectRing("retry");
continue;
}
// Page ACKs queued from the GATT callback thread are written
// here, on the only thread allowed to block on a GATT write.
flushRingOutbound();
// An on-demand pull asked for from the UI thread runs here for
// the same reason: requestRingHealth() blocks on GATT writes and
// on its own RSP/DATA waits, which only this thread may do.
runRequestedRingHealthPull();
// An aborted pull is unfinished business, not a speculative
// repeat - see RING_HEALTH_ABORTED_RETRY_LIMIT. Cheap: returns
// immediately unless a pull actually failed to complete.
resumeAbortedRingHealthPull();
long sleepMs = driveSession();
if (sleepMs > 0) {
interruptibleSleep.sleep(sleepMs);
@@ -1735,6 +1736,35 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
logLine("communicator stop");
}
/** Sole owner of blocking ring operations; callbacks only enqueue ACKs. */
private void runRingWorker() {
while (running && !Thread.currentThread().isInterrupted()) {
try {
if (shouldAttemptRingConnect()) tryConnectRing("ring worker");
if (!running) break;
flushRingOutbound();
runRequestedRingHealthPull();
resumeAbortedRingHealthPull();
synchronized (lock) {
if (running) lock.wait(100);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
} catch (Throwable t) {
logLine("ring worker error: " + safeMessage(t));
// A ring failure must not tear down the glasses session.
synchronized (lock) {
ringConnected = false;
ringNotificationsReady = false;
ringReconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RING_RECONNECT_DELAY_MS;
}
bleManager.disconnect(ringAddress);
}
}
logLine("ring worker stop");
}
@Override public void onNotification(String address, String characteristicUuid, byte[] data) {
if (address == null || characteristicUuid == null || data == null) {
return;
@@ -2140,9 +2170,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
ringHealthPageCounter++;
lock.notifyAll();
}
// The worker loop does the actual write; wake it so the ACK is not held
// for a whole idle tick.
interruptibleSleep.interrupt();
// lock.notifyAll() wakes the ring worker, independently of the glasses.
}
private boolean journalRingFrame(RingProtocol.Frame frame) {
@@ -2201,6 +2229,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
emitBatteryState(headsetBattery, headsetCharging);
}
logLine(connected ? "direct ring BLE connected" : "direct ring BLE disconnected");
lock.notifyAll();
return;
}
if (address.equalsIgnoreCase(rightAddress)) {
@@ -2297,7 +2326,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
logLine("queue settings query for firmware info");
}
}
tryConnectRing("initial");
synchronized (lock) { lock.notifyAll(); }
} catch (Throwable t) {
logLine("connect failed: " + safeMessage(t));
String unpairedArm = firstUnpairedArm();
@@ -2361,9 +2390,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
return running
&& sessionReady
&& !ringNotificationsReady
&& now >= ringReconnectAfterMs
&& pendingMessages.isEmpty()
&& inFlightMessages.isEmpty();
&& now >= ringReconnectAfterMs;
}
}
@@ -2554,6 +2581,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
public void requestRingHealthNow() {
synchronized (lock) {
ringHealthPullRequested = true;
lock.notifyAll();
}
interruptibleSleep.interrupt();
}
@@ -2650,100 +2678,25 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
}
/**
* Device-channel prelude the ring requires before it will answer any
* channel-0x02 (health) request. <b>Confirmed live, 2026-09-10: without
* this, the ring never responds at all to a bare health REQ - not even
* an RSP.</b> With it, RSPs start flowing immediately. The direct-ring
* path has no other handshake or auth step, so this is the whole gate.
* Device-channel initialization. 00:0e enables health recording; 00:05 sets
* the clock (signed timezone minutes plus Unix seconds).
*
* <p>These five frames were found by byte-for-byte comparison against a
* real Even-app sync (pkt 8814-9153 in the capture behind
* {@code knowledge/staging/faceclaw-ring-protocol-decode-return.md}),
* replayed here in the same order, right after the ring connects.
* <b>Which of the five is actually load-bearing is unknown</b> - all
* five go out together because that combination is the only one proven
* to work; nobody has yet tried removing any of them. Three of them
* (00:08, 06:02, 00:0A) carry payload bytes with no known meaning beyond
* "the ring accepted them from Even's app" - opaque constants, copied
* verbatim, not derived. Do not change them without new evidence.
* The inherited extra timezone shift is WRONG: on this PDT ring, fresh HR
* timestamps track receipt time + 7h. Firmware expects UTC seconds and
* applies the timezone separately. Do not simply deploy a seven-hour
* rewind: stock firmware can format its health database on a backward
* jump >= 1h. Existing pages also contain mixed clock domains and invalid
* daily anchors, so their dates cannot be repaired by one subtraction.
*
* <p>The other two (00:0E clock-set, 00:05 day-anchor) need a live
* value: both carry the current Unix time offset by the local UTC offset,
* which in EDT is +14400s (4h) - and +14400s is what a real Even write
* carries. Verified 2026-09-11 against six separate real Even clock-set
* writes. As of 2026-09-12 the offset is COMPUTED from the device time
* zone rather than hardcoded, which is identical in EDT and stays correct
* across a DST change; see the comment in {@code sendRingHandshake()}.
*
* <p><b>Skew trap - read before "correcting" this number.</b> Earlier on
* 2026-09-11 this was changed to +28800s (8h) and that was wrong. The
* mistake: btsnoop packet timestamps on this phone run exactly 4 hours
* BEHIND the Android system clock (the system clock itself is correct -
* {@code adb shell date} agrees with real time). Measuring Even's sent
* value against the btsnoop timestamp therefore double-counts the 4h and
* makes a correct +14400 look like +28800. The skew is verified
* sub-second by lining btsnoop up against logcat on the same connect:
* logcat "direct ring BLE connected" 05:58:24.616 vs btsnoop MTU Req
* 01:58:24.618; "ring ready" .983 vs CCCD Write Rsp .981; "handshake
* sent" 25.085 vs first Write Command 25.071. Same milliseconds, hour
* off by four. <b>Always convert btsnoop timestamps to real local time
* (+4h) before comparing them to anything.</b>
*
* <p>A stale, yesterday's timestamp here was separately tested and ruled
* out as the reason DATA wasn't following RSP - but nothing says a stale
* value is harmless either, so this always sends the true current time.
*/
/**
* Round 2 (2026-09-11, ~00:55 EDT): compared this handshake against a
* fresh HCI-snoop capture of Even's own app completing a real successful
* sync minutes earlier. Two real findings from that comparison:
* - The 00:0A payload (the "app identity?" blob) is byte-for-byte
* identical between that capture and one from two nights before - a
* true fixed constant, not a rotating per-session token. Rules out the
* "stale identity token" theory tried first.
* - Even's real sequence also sends battery (00:01), firmware version
* (00:02), and device id (00:0B) - none of which round 1 ever sent -
* interleaved with the health requests, not just once upfront. Notably
* the firmware-version query lands immediately before Even's own first
* successful health request in the real trace. `06:02`, which round 1
* did send, never appears anywhere in that real successful sync -
* dropped here since there's now real evidence it isn't needed and it
* was never more than a guess to begin with.
* This round adds 00:01/00:02/00:0B up front rather than interleaved
* (interleaving would need restructuring requestRingHealth() too - a
* bigger change deferred until this simpler version is shown to help or
* not).
* TODO: controlled UTC migration after preserving ring history. Retain the
* existing wire behavior until that migration is authorized and validated.
* See notes/ring-health-live-validation.md for evidence and the next test.
*/
private void sendRingHandshake() {
// +14400s was hardcoded here because every capture behind this work was
// taken in EDT, where 14400s happens to be the magnitude of the UTC
// offset - so the constant was right by coincidence of season, not by
// derivation, and would have gone an hour wrong at the next DST change.
// This computes it instead: identical in EDT, correct year-round.
//
// ⚠ MIND THE SIGN. The ring's clock runs AHEAD of real time by the
// magnitude of a west-of-UTC offset, so the quantity wanted here is
// NEGATIVE `TimeZone.getOffset()`, which is itself negative west of UTC
// (-14400000ms in EDT). Getting this backwards sets the ring's clock 8h
// wrong rather than 0h wrong. Measured 2026-09-12: the un-negated form
// logged -14400 and the ring then returned step buckets dated 8h out.
//
// Note this is the OPPOSITE of the "ring stores naive local time"
// hypothesis, which predicts `epoch + utc_offset` = epoch - 14400. The
// measured, working value is epoch + 14400. The hypothesis is therefore
// NOT confirmed by this code; what is preserved here is the behaviour
// verified against six real Even clock-set writes.
//
// ⚠ PAIRED with `ringClockOffsetMs()` in `app/health/health-live.ts`,
// which subtracts the same quantity on the way back out. These two must
// move together; both now compute the value rather than hardcoding EDT.
long nowMs = System.currentTimeMillis();
long clockOffsetSeconds = -TimeZone.getDefault().getOffset(nowMs) / 1000L;
long liveClockSeconds = (nowMs / 1000L) + clockOffsetSeconds;
// Asserted at every handshake: in EDT this must read 14400. The clock write
// is the part of this protocol that took two sessions to get working, so a
// changed value here is the one way a working pull silently breaks.
Log.i(TAG, "ring handshake clock offset seconds = " + clockOffsetSeconds
Log.i(TAG, "ring handshake legacy clock offset seconds = " + clockOffsetSeconds
+ " (tz " + TimeZone.getDefault().getID() + ")");
byte[] clock = le32(liveClockSeconds);
@@ -2924,11 +2877,11 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
* "genuinely nothing new" other than knowing independently whether fresh
* data should exist (e.g. from the export's own sampling cadence).
*
* <p>Runs on the worker thread (from connectRing), which is the only
* <p>Runs on the ring worker (from connectRing), which is the only
* thread allowed to call the blocking write path - the wait loops below
* block that same thread, which is why {@link #flushRingOutbound} is
* called inside the response and DATA waits rather than relying on the
* main loop's own call to it.
* ring loop's own call to it.
*/
private boolean requestRingHealth() {
// A previous attempt may have left an ACK queued after a failed write.
+1 -5
View File
@@ -8,15 +8,11 @@ import { Application } from '@nativescript/core'
import { registerShareIntentHandler } from './native/share-intents'
import { installNativeUserAgent } from './util/http'
import { registerPhoneRotation } from './native/phone-rotation'
installNativeUserAgent()
registerShareIntentHandler()
registerPhoneRotation()
import { startLiveHealthSync, startAlignedRingPull } from './health/health-live'
installNativeUserAgent()
registerShareIntentHandler()
registerPhoneRotation()
// Ring health records live only in the communicator's memory until something
// stores them. Both health surfaces store on open, but a pull that lands while
+2 -3
View File
@@ -28,7 +28,7 @@ import {
type InProcessAppOptions,
type InProcessWindow,
} from "../../ui/shell/in-process-window";
import { dailySummary, hypnogram, rollupSeries, type DailySummary } from "../../health/health-derive";
import { dailySummary, dailyHypnogram, rollupSeries, type DailySummary } from "../../health/health-derive";
import {
drawGlancePage,
GLANCE_PAGES,
@@ -114,8 +114,7 @@ class HealthLayer implements Layer {
});
}
this.hourly = hourly;
const night = sessions.find((session) => session.dayStartMs === today);
this.stageBands = night ? hypnogram(night) : [];
this.stageBands = dailyHypnogram(sessions, today);
this.fixture = isFixtureData();
} catch (error) {
console.warn("health glance reload failed", error);
+40 -9
View File
@@ -165,9 +165,7 @@ export function dailySummary(
const inDay = samples.filter(
(sample) => sample.startMs >= dayStartMs && sample.startMs < dayEndMs,
);
const night = sleepSessions
.filter((session) => session.dayStartMs === dayStartMs)
.sort((a, b) => b.totalSec - a.totalSec)[0];
const nights = sleepSessions.filter((session) => session.dayStartMs === dayStartMs);
return {
dayStartMs,
steps: Math.round(sumOf(inDay, "steps")),
@@ -175,7 +173,7 @@ export function dailySummary(
heartRate: summarise(inDay, "heartRate"),
spo2: summarise(inDay, "spo2"),
hrv: summarise(inDay, "hrv"),
sleep: night ? sleepSummary(night) : null,
sleep: combinedSleepSummary(nights),
};
}
@@ -250,6 +248,21 @@ export function sleepSummary(session: SleepSession): SleepSummary {
};
}
/** Every recorded block attributed to the day contributes to its headline. */
export function combinedSleepSummary(sessions: readonly SleepSession[]): SleepSummary | null {
if (sessions.length === 0) return null;
const combined: SleepSession = {
...sessions[0], totalSec: 0, wakeSec: 0, remSec: 0, lightSec: 0, deepSec: 0,
timeResolved: sessions.every((session) => session.timeResolved),
};
for (const session of sessions) {
for (const field of ["totalSec", "wakeSec", "remSec", "lightSec", "deepSec"] as const) {
combined[field] += Math.max(0, session[field]);
}
}
return sleepSummary(combined);
}
/**
* One stage's seconds, from the summary's own bands.
*
@@ -272,11 +285,7 @@ export function stageSeconds(stage: SleepStageName, summary: SleepSummary): numb
* night with no record comes back as `hasData: false` at its real position
* rather than shifting every later night one column left.
*
* Two sessions attributed to the same day are SUMMED rather than the longest
* winning. That differs from `dailySummary`, which picks the longest because it
* is answering "how did you sleep last night" with one headline number; here
* the column is the day's total time in each stage, and dropping a nap would
* make the bar disagree with the sleep total shown beside it.
* Sessions attributed to the same day are summed, as in `dailySummary`.
*/
export function sleepNights(
sessions: readonly SleepSession[],
@@ -336,6 +345,28 @@ export function hypnogram(session: SleepSession): { stage: SleepStageName | null
}));
}
/** Chronological blocks, with unrecorded intervals left as unknown gaps. */
export function dailyHypnogram(sessions: readonly SleepSession[], dayStartMs: number): ReturnType<typeof hypnogram> {
const result: ReturnType<typeof hypnogram> = [];
let previousEnd: number | null = null;
for (const session of sessions.filter((s) => s.dayStartMs === dayStartMs)
.sort((a, b) => a.startMs - b.startMs)) {
if (previousEnd !== null && session.timeResolved && session.startMs > previousEnd) {
result.push({ stage: null, seconds: (session.startMs - previousEnd) / 1000 });
}
const bands = hypnogram(session);
result.push(...bands);
// A session without stage data still occupies its recorded interval.
const covered = bands.reduce((total, band) => total + band.seconds, 0);
const duration = session.timeResolved
? Math.max(0, (session.endMs - session.startMs) / 1000)
: session.totalSec + session.wakeSec;
if (duration > covered) result.push({ stage: null, seconds: duration - covered });
previousEnd = session.timeResolved ? Math.max(previousEnd ?? 0, session.endMs) : null;
}
return result;
}
/**
* Date formatting, done by hand.
*
+10 -54
View File
@@ -77,66 +77,22 @@ function activeCommunicator(): any {
}
/**
* The ring timestamps in a frame 4 hours ahead of real time, and that is our
* own doing: the handshake sets its clock to `now + 14400s`, because that is
* what a real Even write carries (verified against six of them). Even's app
* evidently subtracts the same offset on the way back out. We were not, so
* every hourly sample landed 4 hours in the future.
*
* MEASURED, not theorised. Stored samples read 09:00/10:00/11:00 local while
* the actual time was 07:15; minus four hours gives 05:00/06:00/07:00, which
* is exactly right for a ring that had just reported the current hour.
*
* ⚠ This value is PAIRED with the offset in `sendRingHandshake()`. They must
* move together.
*
* Both were hardcoded to 14400. As of 2026-09-12 both COMPUTE it: 14400 was
* only ever right because every capture behind this work was taken in EDT,
* where 14400s is the magnitude of the UTC offset. Computing it is identical
* today and survives DST.
*
* ⚠ MIND THE SIGN. The quantity is "how far AHEAD of real time the ring's clock
* runs", which is the magnitude of a west-of-UTC offset — positive 14400 in
* EDT. `getTimezoneOffset()` is already minutes WEST of UTC (+240 in EDT), so
* it is used as-is, NOT negated. Java's `TimeZone.getOffset()` uses the
* opposite convention and is negated there; the two agree on +14400.
*
* Worth recording because the first cut of this change got the sign backwards
* on both sides at once — consistently, so they stayed "paired", and still
* wrong by 8 hours. Only the handshake's own assertion log caught it.
*
* Note this direction is the OPPOSITE of the "ring stores naive local time"
* hypothesis (which predicts `epoch + utc_offset`, i.e. 4h behind). Measured
* behaviour is 4h ahead. The hypothesis is unconfirmed; the measurement rules.
* Legacy compensation for the incorrect extra timezone shift sent by the
* handshake. This is not a ring protocol requirement. Fresh HR timestamps on
* the PDT test ring run seven hours ahead, but older values and daily anchors
* do not share that shift. Keep this paired with the existing handshake until
* a controlled UTC migration; do not apply it retrospectively to raw history.
* See notes/ring-health-live-validation.md.
*/
function ringClockOffsetMs(atMs: number): number {
return new Date(atMs).getTimezoneOffset() * 60 * 1000;
}
/**
* Sleep timestamps need TWICE the correction the hourly samples need.
*
* ⚠ EMPIRICAL, AND THE MECHANISM IS UNEXPLAINED. Do not read a story into the
* factor of 2. The last time this file invented a tidy explanation for a ring
* clock quirk ("the ring stores naive local time") it was falsified the next
* day, which is why the hourly comment above ends with "the measurement rules".
* The same applies here, more so: nothing about a sleep record is known to
* differ from an hourly one in how it is stamped, and yet the correction does.
*
* The evidence, 2026-09-12, the first real sleep record off the hardware:
* `start_ts` decodes raw to 10:21:01 EDT. Chris confirmed the true window two
* independent ways - he slept "straight through from around 1:30 AM to 9:30 AM",
* and separately reported seeing only the first block with "another 4 or so
* hours after that". Under -4h the block is 06:21 -> 09:25, which puts the
* missing time BEFORE it and contradicts what the device showed. Under -8h it
* is 02:21 -> 05:25, leaving 05:25 -> 09:30 (4h05m) missing AFTER it, which is
* what he saw.
*
* Expressed as twice `ringClockOffsetMs` rather than a fresh 28800 so the
* November DST change moves the hourly correction and this one together. Like
* the hourly path it is evaluated at the RAW instant, so within ~8h of a DST
* switch it can pick the wrong side by an hour - the same caveat, deliberately
* kept identical rather than special-cased here.
* Inherited sleep-specific heuristic, still unvalidated on this ring (no sleep
* DATA page received yet). Firmware describes UTC timestamps, not a double
* timezone offset. Remove alongside the controlled clock migration, then
* validate against a recorded night; it is not evidence of correct placement.
*/
function sleepClockOffsetMs(atMs: number): number {
return 2 * ringClockOffsetMs(atMs);
+64 -1
View File
@@ -41,7 +41,7 @@ Faceclaw now uses the direct status while the ring link is connected, falling
back to the glasses report on disconnect. This does not repair the separate
stock ring-to-glasses connection or its cached battery reading.
## Remaining clock validation
## Clock investigation (follow-up)
Successful decoding does not establish correct chart timestamps. This ring's
reported day anchor had nonzero hour/minute/second components; the inherited
@@ -55,3 +55,66 @@ CRC-valid frames are now retained before acknowledgement in the app-private
preserves evidence and permits later reprocessing; it is not yet an automatic
replay/import path. The earlier rejected pages were not journaled by the old
build, and re-delivery of them is not guaranteed.
`python3 tools/ring-clock-audit.py <journal.jsonl>` reports the relevant time
fields without emitting measurement values. It reads the journal without
modifying it. All instants below are UTC, avoiding local display ambiguity.
| Field | Observed value |
| --- | --- |
| Phone reception of fresh HR page | Sep 18 04:07:28.792 |
| Latest HR timestamp in that page | Sep 18 11:07:25 |
| Difference | +25,196.208 seconds (seven hours minus 3.792 seconds) |
| Daily anchor, shared across metric pages | Sep 17 12:47:44 |
| Timezone in those pages | -420 minutes |
| Anchor's local time | 05:47:44, not midnight |
| Latest HRV / SpO2 timestamps | Sep 18 03:39:03 / 03:38:51 |
The handshake explicitly sends `phone Unix seconds - UTC offset`, or UTC +7h
in PDT. The fresh HR timestamp is consistent with that incorrect initialization.
The older HRV/SpO2 timestamps precede reception and cannot be assumed to have
the same error. The unchanged bucket index 20 and non-midnight anchor also
indicate that a clock change did not rebuild all existing daily metadata.
We cannot infer trustworthy dates for those buckets from the latest HR time.
The sibling openCFW checkout's `r1/docs/correlation/CLOCK-PRODUCTION-CORRELATION.md`
and recovered RTC evidence describe command `00:05` as UTC epoch seconds plus a
separate signed timezone offset. The clock applies that offset for local-calendar
bucketing. Command `00:0e` is a health-recording setting, not a second clock setter.
The inherited comments claiming that UTC+offset and double sleep correction were
protocol requirements have therefore been replaced with explicit migration TODOs.
**Do not silently rewind the live ring.**
`r1/docs/correlation/TIME-HEALTH-ROLLOVER-CORRELATION.md` and
`r1_health_plan_time_transition` document stock behavior: a backward jump of at
least 3,600 seconds requests health-database formatting and synchronization cursor
reset. A seven-hour correction can erase history still held only by the ring.
The local phone-store backup is `/tmp/faceclaw-health-before-clock.tar`; it
preserves received frames and derived records, not every possible unreceived page.
No clock correction or historical timestamp rewrite was performed in this follow-up.
The next controlled test is to finish a health pull, preserve its raw pages, then
set `00:05` to actual UTC seconds with the phone's offset still in its separate
field. Remove the matching display corrections for new records and keep old
ambiguous records archived separately. Verify the command response, fresh HR time
against receipt time, and a new daily anchor against local midnight. This test
requires permission for the firmware's possible history reset. Sleep clock and
duration units still require a real sleep DATA page; no sleep has been observed
on this new ring.
## Remaining initial-review fixes
- Ring connection, handshake, health waits and page ACKs now run on one dedicated
ring worker. GATT locking is per address, so the glasses sender continues while
the ring connects or waits for pages. Shutdown interrupts and joins both workers.
- Daily sleep totals include all sessions attributed to the wake-up day. The
detail view orders their stage blocks chronologically and leaves unrecorded
intervals unknown instead of counting them as sleep.
- Startup registers the share-intent handler and native user agent once.
Validation: Android build and full TypeScript check passed; focused health and
ring tests passed (36 passed, one pre-existing timezone-specific skip). Installed
on the Pixel 7a. The 21:19:58–21:20:08 local-time pull answered all five health
requests on the ring worker while the glasses sender delivered and received
ACKs for images roughly every 0.8 seconds throughout that interval. Battery
remained 85%. The historical timestamp heuristics are still unchanged.
+32 -2
View File
@@ -6,13 +6,14 @@ import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.CountDownLatch;
public final class RingHealthTransferTest implements AutoCloseable {
private final Object lock = new Object();
private final ArrayDeque<byte[]> ringOutbound = new ArrayDeque<>();
private final ScheduledExecutorService callbacks = Executors.newSingleThreadScheduledExecutor();
private final List<String> writes = new ArrayList<>();
private boolean running = true;
private volatile boolean running = true;
private boolean ringConnected = true;
private boolean ringNotificationsReady = true;
private boolean ringHealthRspSeen;
@@ -21,6 +22,20 @@ public final class RingHealthTransferTest implements AutoCloseable {
private boolean failAck;
private boolean answer = true;
private boolean delayRsp;
private boolean pullRequested = true;
private final CountDownLatch requestStarted = new CountDownLatch(1);
private final String ringAddress = "ring";
private long ringReconnectAfterMs;
private static class ConnectionOptions { static final int RING_RECONNECT_DELAY_MS = 1000; }
private static class BleManager { void disconnect(String address) {} }
private final BleManager bleManager = new BleManager();
private static String safeMessage(Throwable t) { return t.getMessage(); }
private boolean shouldAttemptRingConnect() { return false; }
private void tryConnectRing(String reason) { throw new AssertionError("unexpected connect"); }
private void runRequestedRingHealthPull() {
if (pullRequested) { pullRequested = false; requestRingHealth(); }
}
private void resumeAbortedRingHealthPull() {}
private static final long RING_HEALTH_RSP_TIMEOUT_MS = 500;
private static final long RING_HEALTH_DATA_IDLE_MS = 100;
private static final int[] RING_DEVICE_PING_CMD_LO = {10, 1, 2, 11};
@@ -53,6 +68,7 @@ public final class RingHealthTransferTest implements AutoCloseable {
try { Thread.sleep(130); } catch (InterruptedException e) { throw new AssertionError(e); }
if (pageId % 10 == 1) callbacks.schedule(() -> page(pageId + 1), 5, TimeUnit.MILLISECONDS);
} else {
requestStarted.countDown();
int command = RingProtocol.parse(frame).cmdHi;
writes.add("req:" + command);
if (answer) {
@@ -73,7 +89,21 @@ public final class RingHealthTransferTest implements AutoCloseable {
@Override public void close() { callbacks.shutdownNow(); }
public static void main(String[] args) {
public static void main(String[] args) throws Exception {
try (RingHealthTransferTest transfer = new RingHealthTransferTest()) {
transfer.answer = false;
Thread worker = new Thread(transfer::runRingWorker);
worker.start();
if (!transfer.requestStarted.await(1, TimeUnit.SECONDS)) throw new AssertionError("no request");
// The glasses sender can still take the session lock while the
// ring is waiting on its RSP. Shutdown must cancel that wait too.
long start = SystemClock.elapsedRealtime();
synchronized (transfer.lock) { transfer.running = false; transfer.lock.notifyAll(); }
worker.interrupt();
worker.join(300);
if (worker.isAlive()) throw new AssertionError("ring worker did not stop");
if (SystemClock.elapsedRealtime() - start >= 300) throw new AssertionError("session lock blocked");
}
try (RingHealthTransferTest transfer = new RingHealthTransferTest()) {
transfer.delayRsp = true;
if (!transfer.requestRingHealth()) throw new AssertionError("pull failed");
+27
View File
@@ -22,6 +22,7 @@ const assert = require("node:assert/strict");
const { buildFixtures } = require("../.test-build/app/health/health-fixtures.js");
const {
dailySummary,
dailyHypnogram,
sleepNights,
sleepSummary,
stageSeconds,
@@ -103,6 +104,32 @@ test("two sessions on one day are summed, not deduped to the longest", () => {
Math.abs(nights[0].lightSec - (3600 + 1800) * 0.6) < 1e-6,
"stage totals add across both sessions",
);
const summary = dailySummary([], [night(3600, 300), night(1800, 60)], today).sleep;
assert.equal(summary.totalSec, 5400);
assert.equal(summary.wakeSec, 360);
assert.equal(summary.efficiencyPercent, 5400 / 5760 * 100);
assert.equal(dailySummary([], [], today).sleep, null);
});
test("split-night detail includes both blocks and leaves the gap unknown", () => {
const today = startOfLocalDay(Date.now());
const block = (hour, stageId) => ({
dayStartMs: today, startMs: today + hour * 3600000,
endMs: today + (hour + 1) * 3600000,
totalSec: 3600, wakeSec: 0, remSec: 0, lightSec: 3600, deepSec: 0,
segments: [{ stageId, halfMinutes: 120 }], timeResolved: true,
});
const first = block(1, 1), second = block(3, 2);
const bands = dailyHypnogram([second, first, { ...block(1, 3), dayStartMs: today - DAY_MS }], today);
assert.equal(bands.length, 3);
assert.deepEqual(bands[1], { stage: null, seconds: 3600 });
assert.equal(bands[0].seconds, 3600);
assert.equal(bands[2].seconds, 3600);
assert.notEqual(bands[0].stage, bands[2].stage);
assert.equal(dailySummary([], [first, second], today).sleep.totalSec, 7200,
"an unrecorded gap is not counted as sleep");
assert.deepEqual(dailyHypnogram([{ ...first, segments: [] }], today),
[{ stage: null, seconds: 3600 }]);
});
test("a stage's lane duration comes from the named totals", () => {
+9 -1
View File
@@ -1,4 +1,5 @@
const test = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
@@ -11,12 +12,19 @@ test("ring health drains ACK-paced pages before advancing and aborts on write fa
"../App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java"), "utf8");
// Compile the actual worker methods with a fake GATT transport. This keeps
// Android out of the JVM test without copying the transfer implementation.
const methods = ["requestRingHealth", "awaitRingHealthRsp", "awaitRingHealthDataIdle", "flushRingOutbound"]
const methods = ["requestRingHealth", "awaitRingHealthRsp", "awaitRingHealthDataIdle", "flushRingOutbound", "runRingWorker"]
.map((name) => {
const match = source.match(new RegExp(` private (?:boolean|int|void) ${name}\\([^]*?^ }`, "m"));
if (!match) throw new Error(`Missing production method: ${name}`);
return match[0];
}).join("\n");
for (const signature of ["@Override public void run()", "private void connectLoopOnce()"]) {
const start = source.indexOf(signature);
assert.ok(start >= 0);
const body = source.slice(start, source.indexOf("\n }", start));
assert.doesNotMatch(body, /tryConnectRing\(|requestRingHealth\(|flushRingOutbound\(|runRequestedRingHealthPull\(/,
"the glasses sender must not call blocking ring operations");
}
const fixture = fs.readFileSync(path.join(__dirname, "fixtures/RingHealthTransferTest.java"), "utf8");
const harness = path.join(directory, "RingHealthTransferTest.java");
fs.writeFileSync(harness, fixture.replace("// TRANSFER_METHODS", methods));
+69
View File
@@ -0,0 +1,69 @@
#!/usr/bin/env python3
"""Inspect timestamps in an app-private ring-frames.jsonl export (read-only).
Prints clock metadata, never measurement values. A latest timestamp may be old;
its age is not by itself a clock measurement. Repeated *future* HR timestamps
tracking receipt time are stronger evidence. All output instants are UTC.
"""
import argparse
import datetime
import json
import struct
def utc(seconds):
return datetime.datetime.fromtimestamp(seconds, datetime.timezone.utc).isoformat()
def audit(record):
raw = bytes.fromhex(record["raw"])
if len(raw) < 24 or raw[5:8] != b"\x64\x02\x64" or raw[10] != 2 or raw[12] != 1:
return None
if int.from_bytes(raw[13:15], "little") != len(raw) - 5:
return None
command = raw[11]
metric = {1: "heart_rate", 2: "spo2", 4: "hrv", 5: "steps"}.get(command)
if metric is None:
return None
payload = raw[15:]
count, offset, anchor = struct.unpack_from("<BhI", payload, 2)
width = 2 if command == 4 else 1
stride = 7 if command == 5 else 1 + 3 * width
header = len(payload) - count * stride
if header not in ((9,) if command == 5 else (9, 13 + width)):
return None
received = record["receivedAtMs"] / 1000
result = {
"received_utc": utc(received),
"metric": metric,
"timezone_minutes": offset,
"anchor_utc": utc(anchor),
"anchor_local_seconds_after_midnight": (anchor + offset * 60) % 86400,
"bucket_indices": list(payload[header::stride]),
}
if command != 5 and header != 9:
latest = struct.unpack_from("<I", payload, 9)[0]
result["latest_utc"] = utc(latest)
result["latest_minus_received_seconds"] = round(latest - received, 3)
return result
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("journal")
args = parser.parse_args()
with open(args.journal, encoding="utf-8") as journal:
for number, line in enumerate(journal, 1):
if not line.strip():
continue
try:
result = audit(json.loads(line))
except (ValueError, KeyError, TypeError, struct.error) as error:
raise SystemExit(f"Invalid journal line {number}: {error}") from error
if result is not None:
print(json.dumps(result))
if __name__ == "__main__":
main()