Fix ring health persistence and page acknowledgements

This commit is contained in:
James Babcock committed 2026-09-17 20:23:15 -07:00
1 parent 22c851a6b9
commit c595cb4b6e
10 files changed
+472 -92

No files matched your search

@@ -2885,10 +2885,13 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
* <p>Runs on the worker thread (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 explicitly after each type rather than relying on the main
* loop's own call to it.
* called inside the response and DATA waits rather than relying on the
* main loop's own call to it.
*/
private boolean requestRingHealth() {
// A previous attempt may have left an ACK queued after a failed write.
// Never start another type's request ahead of that acknowledgement.
if (flushRingOutbound() < 0) return false;
int[] commands = RingProtocol.HEALTH_COMMANDS;
int rspCount = 0;
for (int i = 0; i < commands.length; i++) {
@@ -2910,14 +2913,14 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
if (awaitRingHealthRsp(RING_HEALTH_RSP_TIMEOUT_MS)) {
rspCount++;
awaitRingHealthDataIdle(RING_HEALTH_DATA_IDLE_MS);
if (!awaitRingHealthDataIdle(RING_HEALTH_DATA_IDLE_MS)) {
logLine("ring health: pull ABORTED while acknowledging DATA");
return false;
}
} else {
logLine("ring health: no RSP for command 0x" + Integer.toHexString(command) + ", moving on");
logLine("ring health: pull ABORTED waiting for RSP at 0x" + Integer.toHexString(command));
return false;
}
// Write out any ACK(s) queued by DATA pages that just landed,
// right now rather than after the whole loop - this is the fix
// for the bug the doc above describes.
flushRingOutbound();
if (i < commands.length - 1) {
sendRingDevicePing(RING_DEVICE_PING_CMD_LO[i % RING_DEVICE_PING_CMD_LO.length]);
@@ -2955,8 +2958,14 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
* this protocol's request path, it trusts the ring answers in order. */
private boolean awaitRingHealthRsp(long timeoutMs) {
long deadline = SystemClock.elapsedRealtime() + timeoutMs;
synchronized (lock) {
while (running && ringConnected && !ringHealthRspSeen) {
while (true) {
// A DATA notification may precede its RSP. Writes must happen
// outside lock so GATT callbacks can continue delivering pages.
if (flushRingOutbound() < 0) return false;
synchronized (lock) {
if (!running || !ringConnected) return false;
if (ringHealthRspSeen) return true;
if (!ringOutbound.isEmpty()) continue;
long remaining = deadline - SystemClock.elapsedRealtime();
if (remaining <= 0) {
return false;
@@ -2965,10 +2974,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
lock.wait(Math.min(remaining, 100));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return ringHealthRspSeen;
return false;
}
}
return ringHealthRspSeen;
}
}
@@ -2977,25 +2985,30 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
* transfer isn't cut short. Not filtered by health type - correct given
* the ring only has one type in flight at a time (see the class doc
* above), so any page arriving here belongs to the type just requested. */
private void awaitRingHealthDataIdle(long idleMs) {
private boolean awaitRingHealthDataIdle(long idleMs) {
long idleDeadline = SystemClock.elapsedRealtime() + idleMs;
synchronized (lock) {
int lastSeenCounter = ringHealthPageCounter;
while (running && ringConnected) {
long remaining = idleDeadline - SystemClock.elapsedRealtime();
if (remaining <= 0) {
return;
}
if (ringHealthPageCounter != lastSeenCounter) {
int lastSeenCounter = -1;
while (true) {
// The next page can depend on this ACK. Waiting for idle first
// mistakes an ACK-paced transfer for a completed transfer.
int acknowledged = flushRingOutbound();
if (acknowledged < 0) return false;
synchronized (lock) {
if (!running || !ringConnected) return false;
if (acknowledged > 0 || ringHealthPageCounter != lastSeenCounter) {
lastSeenCounter = ringHealthPageCounter;
// Start the next-page window AFTER the blocking ACK write.
idleDeadline = SystemClock.elapsedRealtime() + idleMs;
remaining = idleMs;
}
// A callback may have queued a page between flush and lock.
if (!ringOutbound.isEmpty()) continue;
long remaining = idleDeadline - SystemClock.elapsedRealtime();
if (remaining <= 0) return true;
try {
lock.wait(Math.min(remaining, 100));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
return false;
}
}
}
@@ -3046,23 +3059,25 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
}
/**
* Drain queued ring frames (page ACKs). Worker-thread only. Returns true
* when anything was written.
* Drain queued ring frames (page ACKs). Worker-thread only. Returns the
* number written, or -1 if the link is unavailable or a write failed.
*/
private boolean flushRingOutbound() {
boolean wroteAny = false;
private int flushRingOutbound() {
int acknowledged = 0;
while (true) {
byte[] frame;
synchronized (lock) {
if (!ringNotificationsReady || ringOutbound.isEmpty()) {
return wroteAny;
}
frame = ringOutbound.poll();
if (!ringNotificationsReady) return -1;
if (ringOutbound.isEmpty()) return acknowledged;
frame = ringOutbound.peek();
}
if (frame == null) {
return wroteAny;
if (!writeRingFrame(frame, "page ack")) return -1;
synchronized (lock) {
// Keep failed writes queued for retry. A disconnect can clear
// the queue during the write, so remove this exact frame only.
ringOutbound.remove(frame);
acknowledged++;
}
wroteAny |= writeRingFrame(frame, "page ack");
}
}
-12
View File
@@ -13,7 +13,6 @@ installNativeUserAgent()
registerShareIntentHandler()
registerPhoneRotation()
import { resyncSystemAppearance } from './native/system-appearance'
import { startLiveHealthSync, startAlignedRingPull } from './health/health-live'
installNativeUserAgent()
@@ -30,17 +29,6 @@ startLiveHealthSync()
// so a ring that stays connected is never read.
startAlignedRingPull()
// Live system dark/light switches while the app is running were confirmed
// (Chris, both directions, 2026-09-02) to leave the Ghost companion's Terminal
// and Doc panes white-on-white and Rich View visually dark-locked -- while a
// COLD launch in either theme renders correctly. That proves the app.css
// .ns-dark rules themselves are right and the LIVE-UPDATE signal is the gap,
// not any one pane's styling -- see system-appearance.ts for the actual fix
// and why it resyncs from two triggers instead of trusting the framework's
// own automatic recolor alone.
Application.on(Application.systemAppearanceChangedEvent, resyncSystemAppearance)
Application.on(Application.resumeEvent, resyncSystemAppearance)
Application.run({ moduleName: 'app-root' })
// Don't place any code after the application has been started
+3 -1
View File
@@ -467,7 +467,9 @@ export function syncLiveRecords(): LiveSyncResult {
const store = healthStore();
const samplesWritten = store.ingestSamples(samples);
const sleepWritten = store.ingestSleep(sleep);
if (samplesWritten > 0 || sleepWritten > 0) markLiveData();
// Also mark on a retry whose samples were already appended before a later
// write failed. Otherwise opening Health could seed fixtures over real data.
markLiveData();
// ONLY here. Both store writes have returned, so the records are durable.
// If either threw, or the process died above this line, nothing is cleared
+22 -16
View File
@@ -20,7 +20,7 @@
import { File, knownFolders } from "@nativescript/core";
import { buildFixtures, FIXTURE_VERSION } from "./health-fixtures";
import { healthStore } from "./health-store-files";
import { checkedFileOperation, healthStore } from "./health-store-files";
const MARKER_FILE = "fixture-marker.json";
@@ -63,11 +63,10 @@ export function hasLiveData(): boolean {
/** Record that real data has been stored. Called by the live ingest path. */
export function markLiveData(): void {
try {
File.fromPath(livePath()).writeTextSync(JSON.stringify({ firstAtMs: Date.now() }));
} catch (error) {
console.warn("health live marker write failed", error);
}
if (hasLiveData()) return;
checkedFileOperation((onError) =>
File.fromPath(livePath()).writeTextSync(JSON.stringify({ firstAtMs: Date.now() }), onError),
);
}
/**
@@ -89,12 +88,17 @@ export function purgeFixtureData(): void {
(name.startsWith("samples-") && name.endsWith(".jsonl")) ||
name === "sleep.jsonl" ||
name === "rollups.json";
if (isStoreFile) File.fromPath(entity.path).removeSync();
if (isStoreFile) {
checkedFileOperation((onError) => File.fromPath(entity.path).removeSync(onError));
}
}
} catch (error) {
console.warn("health fixture purge failed", error);
// Keep the badge until every fixture file is gone. A failed purge must
// stop live ingest so it cannot mix real records with remaining fixtures.
clearFixtureMarker();
} finally {
// Also invalidate after a partial purge; cached rows no longer match disk.
healthStore().resetCache();
}
clearFixtureMarker();
}
/**
@@ -116,10 +120,14 @@ export function seedFixtures(days = 45): boolean {
try {
const store = healthStore();
const { samples, sleep } = buildFixtures({ days, nowMs: Date.now() });
// Label before the first append so a failed seed remains identifiable and
// can be purged before real data is ingested.
const marker: Marker = { version: FIXTURE_VERSION, seededAtMs: Date.now(), days };
checkedFileOperation((onError) =>
File.fromPath(markerPath()).writeTextSync(JSON.stringify(marker), onError),
);
store.ingestSamples(samples);
store.ingestSleep(sleep);
const marker: Marker = { version: FIXTURE_VERSION, seededAtMs: Date.now(), days };
File.fromPath(markerPath()).writeTextSync(JSON.stringify(marker));
return true;
} catch (error) {
console.warn("health fixture seed failed", error);
@@ -129,9 +137,7 @@ export function seedFixtures(days = 45): boolean {
/** Drop the marker. Call before the first real ingest. */
export function clearFixtureMarker(): void {
try {
if (File.exists(markerPath())) File.fromPath(markerPath()).removeSync();
} catch (error) {
console.warn("health fixture marker clear failed", error);
if (File.exists(markerPath())) {
checkedFileOperation((onError) => File.fromPath(markerPath()).removeSync(onError));
}
}
+13 -18
View File
@@ -42,27 +42,13 @@ class FileBackend implements HealthStorageBackend {
}
append(name: string, text: string): void {
try {
// `File.fromPath` creates the file when it is missing, and
// `appendTextSync` is the durable single-call append - deliberately not
// read-modify-write, which would put every record already on disk at
// risk on every new one.
File.fromPath(pathFor(name)).appendTextSync(text, (error) => {
console.warn(`health store append failed (${name})`, error);
});
} catch (error) {
console.warn(`health store append threw (${name})`, error);
}
// NativeScript reports IO failures through callbacks as well as throws.
// Propagate both so the caller retains the unconsumed ring batch.
checkedFileOperation((onError) => File.fromPath(pathFor(name)).appendTextSync(text, onError));
}
write(name: string, text: string): void {
try {
File.fromPath(pathFor(name)).writeTextSync(text, (error) => {
console.warn(`health store write failed (${name})`, error);
});
} catch (error) {
console.warn(`health store write threw (${name})`, error);
}
checkedFileOperation((onError) => File.fromPath(pathFor(name)).writeTextSync(text, onError));
}
list(): string[] {
@@ -77,6 +63,15 @@ class FileBackend implements HealthStorageBackend {
}
}
/** Convert a synchronous NativeScript error callback into a thrown failure. */
export function checkedFileOperation(operation: (onError: (error: any) => void) => void): void {
let failure: Error | undefined;
operation((error) => {
failure = error instanceof Error ? error : new Error(error?.message ?? String(error));
});
if (failure) throw failure;
}
let store: HealthStore | null = null;
/** The app's one health store. Created on first use. */
+31 -11
View File
@@ -121,9 +121,18 @@ export class HealthStore {
private readonly shards = new Map<string, Map<string, HealthSample>>();
private sleep: SleepSession[] | null = null;
private rollups: RollupFile | null = null;
private rollupsDirty = false;
constructor(private readonly backend: HealthStorageBackend) {}
/** Forget cached data after the backing files have been purged. */
resetCache(): void {
this.shards.clear();
this.sleep = null;
this.rollups = null;
this.rollupsDirty = false;
}
// -------------------------------------------------------------------------
// Writing
@@ -154,26 +163,36 @@ export class HealthStore {
* If the shard grows again, the value is changing and the question is why.
*/
ingestSamples(samples: readonly HealthSample[]): number {
const byShard = new Map<string, HealthSample[]>();
const byShard = new Map<string, Map<string, HealthSample>>();
for (const sample of samples) {
if (!Number.isFinite(sample.startMs) || sample.spanMs <= 0) continue;
const name = shardName(sample.startMs);
const shard = this.loadShard(name);
const key = sampleKey(sample);
const existing = shard.get(key);
const pending = byShard.get(name) ?? new Map<string, HealthSample>();
const existing = pending.get(key) ?? shard.get(key);
if (existing && sameSample(existing, sample)) continue;
shard.set(key, sample);
const pending = byShard.get(name);
if (pending) pending.push(sample);
else byShard.set(name, [sample]);
pending.set(key, sample);
byShard.set(name, pending);
}
let written = 0;
for (const [name, list] of byShard) {
for (const [name, pending] of byShard) {
const list = [...pending.values()];
const text = list.map((sample) => `${JSON.stringify(toLine(sample))}\n`).join("");
this.backend.append(name, text);
// A failed append can leave an incomplete final line. Start each batch
// on a fresh line so retrying cannot concatenate a valid record onto it.
this.backend.append(name, `\n${text}`);
this.rollupsDirty = true;
// Only durable values participate in deduplication. If another shard
// fails, a retry skips this successful append and retries the rest.
const shard = this.loadShard(name);
for (const [key, sample] of pending) shard.set(key, sample);
written += list.length;
}
if (written > 0) this.updateRollupsFor(samples);
if (this.rollupsDirty) {
this.updateRollupsFor(samples);
this.rollupsDirty = false;
}
return written;
}
@@ -184,7 +203,7 @@ export class HealthStore {
* later sync replaces rather than duplicates.
*/
ingestSleep(sessions: readonly SleepSession[]): number {
const held = this.loadSleep();
const held = this.loadSleep().slice();
const fresh: SleepSession[] = [];
for (const session of sessions) {
const index = held.findIndex(
@@ -200,7 +219,8 @@ export class HealthStore {
fresh.push(session);
}
if (fresh.length > 0) {
this.backend.append(SLEEP_FILE, fresh.map((s) => `${JSON.stringify(s)}\n`).join(""));
this.backend.append(SLEEP_FILE, "\n" + fresh.map((s) => `${JSON.stringify(s)}\n`).join(""));
this.sleep = held;
}
return fresh.length;
}
+107
View File
@@ -0,0 +1,107 @@
package com.faceclaw.app;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
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 boolean ringConnected = true;
private boolean ringNotificationsReady = true;
private boolean ringHealthRspSeen;
private int ringHealthPageCounter;
private int seq;
private boolean failAck;
private boolean answer = true;
private boolean delayRsp;
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};
private static class SystemClock {
static long elapsedRealtime() { return System.nanoTime() / 1_000_000; }
}
private int nextRingSeqLocked() { return ++seq; }
private int nextRingNonceLocked() { return seq; }
private void logLine(String message) {}
private void sendRingDevicePing(int command) {}
private void page(int id) {
synchronized (lock) {
ringOutbound.add(new byte[] {(byte) id});
ringHealthPageCounter++;
lock.notifyAll();
}
}
private boolean writeRingFrame(byte[] frame, String what) {
if (Thread.holdsLock(lock)) throw new AssertionError("GATT write under session lock");
if (what.equals("page ack")) {
if (failAck) return false;
int pageId = frame[0] & 0xff;
writes.add("ack:" + pageId);
// Exercise a write longer than the idle timeout. Page two arrives
// only after page one's ACK has actually completed.
try { Thread.sleep(130); } catch (InterruptedException e) { throw new AssertionError(e); }
if (pageId % 10 == 1) callbacks.schedule(() -> page(pageId + 1), 5, TimeUnit.MILLISECONDS);
} else {
int command = RingProtocol.parse(frame).cmdHi;
writes.add("req:" + command);
if (answer) {
page(command * 10 + 1);
if (delayRsp) {
callbacks.schedule(() -> {
synchronized (lock) { ringHealthRspSeen = true; lock.notifyAll(); }
}, 20, TimeUnit.MILLISECONDS);
} else {
synchronized (lock) { ringHealthRspSeen = true; lock.notifyAll(); }
}
}
}
return true;
}
// TRANSFER_METHODS
@Override public void close() { callbacks.shutdownNow(); }
public static void main(String[] args) {
try (RingHealthTransferTest transfer = new RingHealthTransferTest()) {
transfer.delayRsp = true;
if (!transfer.requestRingHealth()) throw new AssertionError("pull failed");
List<String> expected = new ArrayList<>();
for (int command : RingProtocol.HEALTH_COMMANDS) {
expected.add("req:" + command);
expected.add("ack:" + (command * 10 + 1));
expected.add("ack:" + (command * 10 + 2));
}
if (!transfer.writes.equals(expected)) throw new AssertionError("premature next type: " + transfer.writes);
}
try (RingHealthTransferTest transfer = new RingHealthTransferTest()) {
transfer.failAck = true;
if (transfer.requestRingHealth()) throw new AssertionError("failed ACK completed pull");
if (transfer.writes.size() != 1) throw new AssertionError("advanced after failed ACK");
if (transfer.ringOutbound.size() != 1) throw new AssertionError("failed ACK was discarded");
transfer.failAck = false;
if (!transfer.awaitRingHealthDataIdle(RING_HEALTH_DATA_IDLE_MS)) throw new AssertionError("retry failed");
if (!transfer.ringOutbound.isEmpty()) throw new AssertionError("retry did not drain ACKs");
}
try (RingHealthTransferTest transfer = new RingHealthTransferTest()) {
transfer.answer = false;
if (transfer.requestRingHealth()) throw new AssertionError("missing RSP completed pull");
if (transfer.writes.size() != 1) throw new AssertionError("advanced without RSP");
}
try (RingHealthTransferTest transfer = new RingHealthTransferTest()) {
transfer.ringConnected = false;
if (transfer.awaitRingHealthDataIdle(100)) throw new AssertionError("disconnect completed transfer");
}
}
}
+149
View File
@@ -0,0 +1,149 @@
const test = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const vm = require("node:vm");
const ts = require("typescript");
// Run the actual live ingest, seed and file-backend modules with NativeScript
// files and the Java communicator replaced at their platform boundaries.
function liveEnvironment() {
const files = new Map();
const cache = new Map();
const faults = { append: null, remove: null, write: null };
const today = new Date();
today.setHours(0, 0, 0, 0);
const start = today.getTime();
let consumed = false;
const record = {
cmdHi: 1,
anchorUnixSeconds: (start + today.getTimezoneOffset() * 60000) / 1000,
groups: [{ hourIndex: 0, min: 40, max: 50, avg: 45 }],
};
const communicator = {
takeRingHealthBatch: () => ({
getWatermark: () => 1,
getRecords: () => ({ size: () => consumed ? 0 : 1, get: () => record }),
}),
clearRingHealthRecordsBelow: () => { consumed = true; return 1; },
};
const native = {
File: {
exists: (name) => files.has(name),
fromPath: (name) => ({
readTextSync: () => files.get(name),
appendTextSync: (text, onError) => {
if (faults.append) return onError(faults.append);
files.set(name, (files.get(name) ?? "") + text);
},
writeTextSync: (text, onError) => {
if (faults.write) return onError(faults.write);
files.set(name, text);
},
removeSync: (onError) => {
if (faults.remove) return onError(faults.remove);
files.delete(name);
},
}),
},
knownFolders: { documents: () => ({ getFolder: () => ({
path: "/health",
getEntitiesSync: () => [...files.keys()].map((name) => ({ path: name, name: path.basename(name) })),
}) }) },
};
function load(filename) {
if (!filename.endsWith(".ts")) filename += ".ts";
if (cache.has(filename)) return cache.get(filename).exports;
const module = { exports: {} };
cache.set(filename, module);
const code = ts.transpileModule(fs.readFileSync(filename, "utf8"), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText;
vm.runInNewContext(code, {
module, exports: module.exports, Date,
require: (name) => name === "@nativescript/core" ? native : load(path.resolve(path.dirname(filename), name)),
console: { log() {}, warn() {} },
com: { faceclaw: { app: { FaceclawBleCommunicator: { getActive: () => communicator } } } },
}, { filename });
return module.exports;
}
return {
files, faults, start, consumed: () => consumed,
load: (name) => load(path.join(__dirname, "../app/health", name)),
};
}
test("first live sync removes fixture samples, sleep and rollups from memory and disk", () => {
const env = liveEnvironment();
const seed = env.load("health-seed");
const store = env.load("health-store-files").healthStore();
assert.equal(seed.seedFixturesIfNeeded(2), true);
assert.ok(store.sleepSessions().length > 0);
assert.ok(store.samplesInRange(env.start, env.start + 86400000).length > 1);
env.load("health-live").syncLiveRecords();
assert.equal(seed.isFixtureData(), false);
assert.equal(env.consumed(), true);
assert.equal(store.sleepSessions().length, 0);
const samples = store.samplesInRange(env.start, env.start + 86400000);
assert.equal(samples.length, 1);
assert.equal(samples[0].avg, 45);
const rollups = JSON.parse(env.files.get("/health/rollups.json"));
assert.equal(Object.keys(rollups.days).length, 1);
const day = Object.values(rollups.days)[0];
assert.deepEqual(Object.keys(day), ["heartRate"]);
assert.equal(day.heartRate.count, 1);
});
test("a failed fixture purge retains the badge and ring batch until retry succeeds", () => {
const env = liveEnvironment();
const seed = env.load("health-seed");
seed.seedFixturesIfNeeded(2);
const live = env.load("health-live");
env.faults.remove = new Error("delete failed");
assert.throws(() => live.syncLiveRecords(), /delete failed/);
assert.equal(env.consumed(), false);
assert.equal(seed.isFixtureData(), true);
env.faults.remove = null;
live.syncLiveRecords();
assert.equal(env.consumed(), true);
assert.equal(seed.isFixtureData(), false);
});
test("NativeScript append callback failures retain the batch and retry persists it", () => {
const env = liveEnvironment();
const live = env.load("health-live");
env.faults.append = { message: "disk full" };
assert.throws(() => live.syncLiveRecords(), /disk full/);
assert.equal(env.consumed(), false);
assert.equal([...env.files.keys()].filter((name) => name.includes("samples-")).length, 0);
env.faults.append = null;
assert.equal(live.syncLiveRecords().samplesWritten, 1);
assert.equal(env.consumed(), true);
assert.equal([...env.files.keys()].filter((name) => name.includes("samples-")).length, 1);
});
test("NativeScript rollup write callback failures propagate without consuming the batch", () => {
const env = liveEnvironment();
env.faults.write = { message: "write failed" };
assert.throws(() => env.load("health-live").syncLiveRecords(), /write failed/);
assert.equal(env.consumed(), false);
env.faults.write = null;
assert.equal(env.load("health-live").syncLiveRecords().samplesWritten, 0);
assert.equal(env.consumed(), true);
assert.equal(env.files.has("/health/rollups.json"), true);
assert.equal(env.load("health-seed").hasLiveData(), true);
assert.equal(env.load("health-seed").seedFixturesIfNeeded(), false);
});
test("a failed fixture append stays labelled and is purged before live ingest", () => {
const env = liveEnvironment();
const seed = env.load("health-seed");
env.faults.append = new Error("disk full");
assert.equal(seed.seedFixturesIfNeeded(2), false);
assert.equal(seed.isFixtureData(), true);
env.faults.append = null;
env.load("health-live").syncLiveRecords();
assert.equal(seed.isFixtureData(), false);
assert.equal(env.load("health-store-files").healthStore().sleepSessions().length, 0);
});
+63
View File
@@ -148,6 +148,69 @@ test("a single unreadable line does not cost the rest of the file", () => {
// ---------------------------------------------------------------------------
// Ingest: the rules that decline to guess
test("failed sample corrections stay retryable, including a partially stored batch", () => {
const backend = memoryBackend();
const store = new HealthStore(backend);
const first = new Date(2026, 7, 31).getTime();
const second = new Date(2026, 8, 1).getTime();
const original = sample("heartRate", second, 50, 80, 60);
store.ingestSamples([original]);
const append = backend.append;
backend.append = (name, text) => {
if (name === "samples-2026-09.jsonl") throw new Error("disk full");
append(name, text);
};
const batch = [sample("heartRate", first, 50, 90, 70), sample("heartRate", second, 50, 100, 80)];
assert.throws(() => store.ingestSamples(batch), /disk full/);
assert.equal(store.samplesInRange(second, second + DAY_MS)[0].avg, 60);
backend.append = append;
assert.equal(store.ingestSamples(batch), 1, "only the failed shard should need retry");
const reloaded = new HealthStore(backend);
assert.equal(reloaded.samplesInRange(first, second + DAY_MS).length, 2);
assert.equal(reloaded.samplesInRange(second, second + DAY_MS)[0].avg, 80);
});
test("failed sleep appends and corrections do not poison deduplication", () => {
const backend = memoryBackend();
const store = new HealthStore(backend);
const session = buildFixtures({ days: 1, nowMs: Date.now() }).sleep[0];
assert.ok(session);
const append = backend.append;
backend.append = () => { throw new Error("disk full"); };
assert.throws(() => store.ingestSleep([session]), /disk full/);
assert.equal(store.sleepSessions().length, 0);
backend.append = append;
assert.equal(store.ingestSleep([session]), 1);
const correction = { ...session, totalSec: session.totalSec + 60 };
backend.append = () => { throw new Error("disk full"); };
assert.throws(() => store.ingestSleep([correction]), /disk full/);
assert.equal(store.sleepSessions()[0].totalSec, session.totalSec);
backend.append = append;
assert.equal(store.ingestSleep([correction]), 1);
assert.equal(new HealthStore(backend).sleepSessions()[0].totalSec, correction.totalSec);
});
test("retry after a partial append preserves the first sample and sleep record", () => {
const backend = memoryBackend();
const store = new HealthStore(backend);
const start = startOfLocalDay(Date.now());
const row = sample("heartRate", start, 50, 90, 70);
const session = buildFixtures({ days: 1, nowMs: Date.now() }).sleep[0];
const append = backend.append;
backend.append = (name, text) => {
append(name, text.slice(0, 12));
throw new Error("partial append");
};
assert.throws(() => store.ingestSamples([row]), /partial append/);
assert.throws(() => store.ingestSleep([session]), /partial append/);
backend.append = append;
assert.equal(store.ingestSamples([row]), 1);
assert.equal(store.ingestSleep([session]), 1);
const recovered = new HealthStore(backend);
assert.equal(recovered.samplesInRange(start, start + DAY_MS)[0].avg, 70);
assert.equal(recovered.sleepSessions()[0].totalSec, session.totalSec);
});
test("an unanchored hourly page is dropped, not dated", () => {
const result = convertRecords([
{
+35
View File
@@ -0,0 +1,35 @@
const test = require("node:test");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const { execFileSync } = require("node:child_process");
test("ring health drains ACK-paced pages before advancing and aborts on write failure", () => {
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "faceclaw-ring-health-"));
const javaBin = (name) => process.env.JAVA_HOME ? path.join(process.env.JAVA_HOME, "bin", name) : name;
const source = fs.readFileSync(path.join(__dirname,
"../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"]
.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");
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));
try {
execFileSync(javaBin("javac"), ["-d", directory, harness,
path.join(__dirname, "../App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/RingProtocol.java"),
path.join(__dirname, "../notes/ring-protocol-selftest/RingProtocolSelfTest.java"),
], { stdio: "pipe" });
execFileSync(javaBin("java"), ["-ea", "-cp", directory, "com.faceclaw.app.RingHealthTransferTest"],
{ stdio: "pipe", timeout: 15000 });
execFileSync(javaBin("java"), ["-ea", "-cp", directory, "com.faceclaw.app.RingProtocolSelfTest"],
{ stdio: "pipe", timeout: 15000 });
} finally {
fs.rmSync(directory, { recursive: true, force: true });
}
});