Files
faceclaw/app/health/health-store-files.ts
T
Chris LeeandClaude Opus 5 22c851a6b9 Health: on-device store, derivations and a glasses glance for ring data
The companion half of the ring protocol, offered less as finished UI than as
an executable answer to "what does this ring data actually mean" -- the part
that took two sessions to work out and that a future contributor would
otherwise have to redo.

- app/health/: wire->domain conversion, an append-only on-device store with
  deduping ingest, rollups, a per-day step-bucket ledger, sleep stage mapping
  (0=wake 1=REM 2=light 3=deep, verified against 34 real nights), and chart
  derivations.
- app/apps/health/: a glasses glance page over that store.
- tools/health-preview.cjs: renders the real chart code headlessly, so layout
  can be checked without an emulator.
- 26 new tests, all passing.

Three clock frames meet in this data and mixing them up causes real bugs, so
they are documented at each site. The ring's clock is deliberately set to
now + 4h (matching what a real Even write carries), so every timestamp it
reports is in that shifted frame and must be corrected on read. Sleep is the
exception: it needs -8h, twice the correction, confirmed against a known
night. Why the ring wants these quantities is unexplained -- the code says so
rather than inventing a tidy mechanism, because the tidy story was tried and
measurement falsified it.

Collection runs on a wall-clock-aligned tick at :01 and :31. Aligned because
the ring's step buckets close on 10-minute boundaries, and because an elapsed
timer drifts off the boundary it is meant to track; offset by a minute because
firing exactly on the boundary risks catching a bucket before it is finalised.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-17 20:14:09 -07:00

87 lines
2.5 KiB
TypeScript

/**
* The real, on-device backing for `HealthStore`: files under the app's
* documents folder, using the same `@nativescript/core` File API that
* `open-apps-persistence.ts` already uses for the open-window list.
*
* Split out from `health-store.ts` purely so that module can stay free of
* NativeScript imports and run under plain node in `tests/`; all the logic
* worth testing is over there.
*/
import { File, Folder, knownFolders } from "@nativescript/core";
import { HealthStore, type HealthStorageBackend } from "./health-store";
const FOLDER_NAME = "health";
function healthFolder(): Folder {
return knownFolders.documents().getFolder(FOLDER_NAME);
}
function pathFor(name: string): string {
return `${healthFolder().path}/${name}`;
}
class FileBackend implements HealthStorageBackend {
exists(name: string): boolean {
try {
return File.exists(pathFor(name));
} catch {
return false;
}
}
read(name: string): string | null {
try {
if (!File.exists(pathFor(name))) return null;
return File.fromPath(pathFor(name)).readTextSync();
} catch (error) {
console.warn(`health store read failed (${name})`, error);
return null;
}
}
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);
}
}
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);
}
}
list(): string[] {
try {
return healthFolder()
.getEntitiesSync()
.map((entity) => entity.name);
} catch (error) {
console.warn("health store list failed", error);
return [];
}
}
}
let store: HealthStore | null = null;
/** The app's one health store. Created on first use. */
export function healthStore(): HealthStore {
if (!store) store = new HealthStore(new FileBackend());
return store;
}