release: v1.32.0 — hardening and scalability (#580)

* release: prepare v1.32.0

* test: stabilize water capture re-entry
This commit is contained in:
Marc-André Bombeck authored and GitHub committed 2026-07-22 04:14:15 +02:00
1 parent b01822be48
commit 62eb431d66
372 files changed
+25178 -8561

No files matched your search

@@ -68,6 +68,19 @@ describe("pickCanonicalWorkout — fixture (a) two-source same workout", () => {
expect(result.clusters[0].pickedSource).toBe("APPLE_HEALTH");
});
it("keeps every workout from the winning source in one cluster", () => {
const result = pickCanonicalWorkout([
row("apple-1", "APPLE_HEALTH", "2026-05-14T07:30:00.000Z"),
row("withings-1", "WITHINGS", "2026-05-14T07:31:00.000Z"),
row("apple-2", "APPLE_HEALTH", "2026-05-14T07:32:00.000Z"),
]);
expect(result.canonical.map((item) => item.id)).toEqual([
"apple-1",
"apple-2",
]);
});
it("still picks APPLE_HEALTH when the Withings row arrives first", () => {
const result = pickCanonicalWorkout([
row("withings-1", "WITHINGS", "2026-05-14T07:30:00.000Z"),
@@ -164,6 +177,20 @@ describe("pickCanonicalWorkout — proximity-window boundary", () => {
expect(result.canonical).toHaveLength(1);
});
it("anchors a cluster to its first row instead of chaining neighbours", () => {
const result = pickCanonicalWorkout([
row("anchor", "APPLE_HEALTH", "2026-05-14T07:30:00.000Z"),
row("neighbour", "WITHINGS", "2026-05-14T07:34:00.000Z"),
row("chained", "WITHINGS", "2026-05-14T07:38:00.000Z"),
]);
expect(result.clusters).toHaveLength(2);
expect(result.canonical.map((item) => item.id)).toEqual([
"anchor",
"chained",
]);
});
it("keeps two rows just outside the proximity window", () => {
// 5 minutes and 1 second — picker rejects, two clusters.
const result = pickCanonicalWorkout([
@@ -203,16 +230,13 @@ describe("pickCanonicalWorkout — determinism", () => {
expect(a.canonical.map((r) => r.id)).toEqual(b.canonical.map((r) => r.id));
});
it("breaks same-timestamp ties via id ordering", () => {
it("preserves same-source workouts at the same timestamp", () => {
const result = pickCanonicalWorkout([
row("a-2", "APPLE_HEALTH", "2026-05-14T07:30:00.000Z"),
row("a-1", "APPLE_HEALTH", "2026-05-14T07:30:00.000Z"),
]);
// Both rows are APPLE_HEALTH within the same cluster — the picker
// keeps the lexicographically-earlier `id` so the choice is
// independent of the caller's input order.
expect(result.canonical).toHaveLength(1);
expect(result.canonical[0].id).toBe("a-1");
expect(result.canonical.map((item) => item.id)).toEqual(["a-1", "a-2"]);
});
});
@@ -240,6 +264,55 @@ describe("pickCanonicalWorkout — field-merge (v1.29.x, WHOOP HR-loss fix)", ()
expect(result.clusters[0].picked.avgHeartRate).toBe(142);
});
it("backfills a lower-priority donor only onto its nearest winning-source workout", () => {
const fixtures = [
row(
"apple-earlier",
"APPLE_HEALTH",
"2026-05-14T07:30:00.000Z",
"running",
{
avgHeartRate: null,
},
),
row(
"apple-later",
"APPLE_HEALTH",
"2026-05-14T07:34:00.000Z",
"running",
{
avgHeartRate: null,
},
),
row("whoop-later", "WHOOP", "2026-05-14T07:34:00.000Z", "running", {
avgHeartRate: 151,
}),
];
for (const input of [fixtures, [...fixtures].reverse()]) {
const result = pickCanonicalWorkout(input);
expect(
result.canonical.map(({ id, source, avgHeartRate }) => ({
id,
source,
avgHeartRate,
})),
).toEqual([
{
id: "apple-earlier",
source: "APPLE_HEALTH",
avgHeartRate: null,
},
{
id: "apple-later",
source: "APPLE_HEALTH",
avgHeartRate: 151,
},
]);
}
});
it("never overwrites a field the base row already has", () => {
const result = pickCanonicalWorkout([
row("apple-1", "APPLE_HEALTH", "2026-05-14T07:30:00.000Z", "cycling", {
@@ -338,25 +411,21 @@ describe("pickCanonicalWorkout — field-merge (v1.29.x, WHOOP HR-loss fix)", ()
});
});
describe("pickCanonicalWorkout — Strava brick-workout collapse (data-integrity audit H4)", () => {
// Before the Strava sport-map fix, `mapActivity()` wrote the raw Strava
// label ("Ride", "Run") straight into `sportType` — neither string is a
// member of `workoutSportTypeEnum`, so `isSpecificSportType()` classified
// BOTH rows as generic. Two generic rows are always sport-compatible
// (`genericPairing` in pick-canonical-workout.ts), so a same-source brick
// session — a Strava ride ending ~15:00 immediately followed by a Strava
// run starting 15:03 — clustered into ONE cluster and the ladder pick
// kept only one leg, silently dropping the other from every list, tile,
// and total.
it("pre-fix: two raw (non-canonical) Strava labels close in time COLLAPSE into one cluster", () => {
describe("pickCanonicalWorkout — Strava brick-workout regression", () => {
// Legacy Strava labels ("Ride", "Run") are both generic to this picker,
// so they share a cluster. The source is identical, however, which means
// both legs are distinct records and must survive.
it("preserves two same-source raw Strava workouts in one cluster", () => {
const result = pickCanonicalWorkout([
row("strava-ride", "STRAVA", "2026-06-01T15:00:00.000Z", "Ride"),
row("strava-run", "STRAVA", "2026-06-01T15:03:00.000Z", "Run"),
]);
// This is the confirmed bug, pinned so a future change can't silently
// "fix" clustering itself and mask a regression in the sport-map layer.
expect(result.clusters).toHaveLength(1);
expect(result.canonical).toHaveLength(1);
expect(result.canonical.map((item) => item.id)).toEqual([
"strava-ride",
"strava-run",
]);
});
it("post-fix: a canonical Strava Ride + Run pair survives as two distinct rows", () => {
+91 -62
View File
@@ -32,18 +32,17 @@
* and keep the first source that has a row in the cluster as the
* BASE of the canonical row. All other rows from the same cluster
* are dropped from the canonical list.
* 3. Field-merge (multi-element clusters only): the base row is
* backfilled with any of `avgHeartRate`, `maxHeartRate`,
* `totalEnergyKcal`, `totalDistanceM`, `elevationM` it is itself
* missing, from the ladder-highest cluster member that HAS that
* field — so a higher-priority twin with a null HR (e.g. a
* Withings summary) doesn't discard the HR a lower-priority twin
* (e.g. WHOOP) actually recorded. The same step adopts a specific
* sport type from a member when the base row's own sportType is
* generic (`"other"` / unrecognised). Every OTHER field on the
* base row (id, source, startedAt, route, metadata, ...) is
* untouched — the merge only ever fills a gap, never overwrites a
* value the base row already has.
* 3. Field-merge (multi-element clusters only): each workout from the
* winning source is backfilled only from losing-source members matched
* to that workout by sport identity and start-time proximity. A donor is
* assigned to one deterministic best winner (exact sport before generic
* compatibility, then nearest timestamp). Missing `avgHeartRate`,
* `maxHeartRate`, `totalEnergyKcal`, `totalDistanceM`, and `elevationM`
* fields come from the ladder-highest matched donor that has the field.
* The same step adopts a specific sport type when the base row is generic
* (`"other"` / unrecognised). Every OTHER field on the base row (id,
* source, startedAt, route, metadata, ...) is untouched — the merge only
* ever fills a gap, never overwrites a value the base row already has.
* 4. Single-element clusters (no near-duplicate) pass through
* unchanged regardless of source — no merge step runs.
*
@@ -133,6 +132,50 @@ function isSpecificSportType(value: string): boolean {
);
}
function areSportTypesCompatible(left: string, right: string): boolean {
return (
left === right || !isSpecificSportType(left) || !isSpecificSportType(right)
);
}
/**
* Match one losing-source donor to one winning-source workout. Exact sport
* identity is stronger than generic compatibility; within the same identity
* strength, the nearest start wins. `winners` is already startedAt/id-sorted,
* so an exact tie resolves deterministically to the first winner.
*/
function findMatchingWinner<T extends WorkoutPickerRow>(
donor: T,
winners: readonly T[],
proximityMs: number,
): T | undefined {
let best: T | undefined;
let bestHasExactSport = false;
let bestDeltaMs = Infinity;
for (const winner of winners) {
if (!areSportTypesCompatible(winner.sportType, donor.sportType)) continue;
const deltaMs = Math.abs(
winner.startedAt.getTime() - donor.startedAt.getTime(),
);
if (deltaMs > proximityMs) continue;
const hasExactSport = winner.sportType === donor.sportType;
if (
!best ||
(hasExactSport && !bestHasExactSport) ||
(hasExactSport === bestHasExactSport && deltaMs < bestDeltaMs)
) {
best = winner;
bestHasExactSport = hasExactSport;
bestDeltaMs = deltaMs;
}
}
return best;
}
/** Ladder position of `source`, or `ladder.length` (last) when the source
* isn't on the ladder at all — an unranked source only wins a merge field
* when no ranked member has it. */
@@ -247,35 +290,28 @@ export interface PickCanonicalWorkoutOptions {
sourcePriority?: readonly MeasurementSource[];
/**
* Cluster window. Defaults to `DEFAULT_WORKOUT_PROXIMITY_MINUTES`.
* Workouts whose start timestamps are within ±this many minutes of
* an already-clustered workout join the same cluster; outside the
* window they form a new cluster even with the same sport type.
* Workouts whose start timestamps are within this many minutes of
* the cluster anchor join that cluster; later neighbours cannot extend
* the window.
*/
proximityMinutes?: number;
}
/**
* Pick the canonical-source workout list. See file-level docs for the
* algorithm; returns a subset of the input plus a per-cluster pick
* record useful for debug overlays / audit logging.
* algorithm; returns the surviving input rows plus one representative
* record per overlap cluster for debug overlays and audit logging.
*/
export function pickCanonicalWorkout<T extends WorkoutPickerRow>(
rows: readonly T[],
options: PickCanonicalWorkoutOptions = {},
): {
canonical: T[];
/**
* One entry per cluster. Useful when the caller wants to log
* "we dropped a WITHINGS row in favour of an APPLE_HEALTH twin"
* without re-running the picker.
*/
clusters: Array<{
/** Every row that clustered together, UNMERGED — the raw inputs the
* merge step read from. */
/** Every row that clustered together, unmerged. */
members: T[];
pickedSource: MeasurementSource;
/** The ladder winner AFTER the step-3 field-merge — the same object
* that landed in `canonical` for this cluster. */
/** Representative winning row after cross-source field merge. */
picked: T;
}>;
} {
@@ -311,34 +347,19 @@ export function pickCanonicalWorkout<T extends WorkoutPickerRow>(
// way — the core invariant is unchanged.
const clusters: T[][] = [];
for (const row of sorted) {
// Find an existing cluster whose latest member is within the
// proximity window and sport-compatible. Walking from the most
// recent cluster backward is O(k) per row where k is the open
// cluster count; for the workout volume HealthLog ingests (single
// user, tens of workouts per week) this is well below the
// measurement bucket-map cost.
// Compare against the first row in the cluster. A later neighbour
// inside the window must not extend the cluster beyond its anchor.
let placed = false;
for (let i = clusters.length - 1; i >= 0; i--) {
const cluster = clusters[i];
const head = cluster[0];
const sameSport = head.sportType === row.sportType;
if (!sameSport) {
const genericPairing =
!isSpecificSportType(head.sportType) ||
!isSpecificSportType(row.sportType);
if (!genericPairing) continue;
}
const last = cluster[cluster.length - 1];
if (row.startedAt.getTime() - last.startedAt.getTime() <= proximityMs) {
if (!areSportTypesCompatible(head.sportType, row.sportType)) continue;
if (row.startedAt.getTime() - head.startedAt.getTime() <= proximityMs) {
cluster.push(row);
placed = true;
break;
}
// Sorted input + outside-window, exact sport match: every
// earlier cluster of the same sport is even further away, no
// point continuing. A generic↔specific pairing has no such
// monotonic guarantee once different-sport clusters interleave
// in time, so keep scanning rather than abort the whole search.
if (sameSport) break;
}
if (!placed) {
@@ -346,9 +367,8 @@ export function pickCanonicalWorkout<T extends WorkoutPickerRow>(
}
}
// Per-cluster: walk the ladder, keep the first source with a row.
// Ties within a source are broken by the sorted-id order baked in
// above.
// Preserve single-source clusters. For a real cross-source overlap,
// select the winning source and retain every workout it contributed.
const canonical: T[] = [];
const clusterRecords: Array<{
members: T[];
@@ -356,14 +376,13 @@ export function pickCanonicalWorkout<T extends WorkoutPickerRow>(
picked: T;
}> = [];
for (const cluster of clusters) {
// Single-element fast path — no contention.
if (cluster.length === 1) {
const only = cluster[0];
canonical.push(only);
const sources = new Set(cluster.map((row) => row.source));
if (sources.size === 1) {
canonical.push(...cluster);
clusterRecords.push({
members: cluster,
pickedSource: only.source,
picked: only,
pickedSource: cluster[0].source,
picked: cluster[0],
});
continue;
}
@@ -375,10 +394,6 @@ export function pickCanonicalWorkout<T extends WorkoutPickerRow>(
break;
}
}
// Fallback: none of the ladder entries appear in the cluster
// (impossible with the current four-member enum + four-member
// default ladder, but the helper accepts a custom ladder). Keep
// every row so the picker never silently drops data.
if (!pickedSource) {
canonical.push(...cluster);
clusterRecords.push({
@@ -388,13 +403,27 @@ export function pickCanonicalWorkout<T extends WorkoutPickerRow>(
});
continue;
}
const winning = cluster.find((row) => row.source === pickedSource)!;
const merged = mergeClusterFields(winning, cluster, ladder);
canonical.push(merged);
const winningRows = cluster.filter((row) => row.source === pickedSource);
const membersByWinner = new Map<T, T[]>(
winningRows.map((winner) => [winner, [winner]]),
);
for (const donor of cluster) {
if (donor.source === pickedSource) continue;
const matchedWinner = findMatchingWinner(donor, winningRows, proximityMs);
if (matchedWinner) membersByWinner.get(matchedWinner)?.push(donor);
}
const winners = winningRows.map((winner) =>
mergeClusterFields(
winner,
membersByWinner.get(winner) ?? [winner],
ladder,
),
);
canonical.push(...winners);
clusterRecords.push({
members: cluster,
pickedSource,
picked: merged,
picked: winners[0],
});
}