feat(environment): air quality, pollen and UV per day, locations sealed at rest

Fetch the Open-Meteo air-quality feed (CAMS, domains=auto) with the weather:
17 hourly variables folded into local days by fixed rules (means and maxima
only with 18 of 24 hours, ozone as the highest 8-hour running mean, UV only
with the midday hours), a value the feed did not cover stored as null, never
as zero. Requests are cut into 90-day chunks, newest first, and chunks before
mid 2022 ask for the eight pollutant and index variables only. An air-quality
failure never costs the weather row; the day stays unfetched and the nightly
gap fill (up to 90 days per account, newest first) asks for it again.

Every Open-Meteo request asks an instance-wide budget kept in Postgres first
(500 a minute, 4000 an hour, 8000 a day, weighted by the hosted pricing
rule). A refused request is not sent and the run ends as done with
budget_blocked. OPENMETEO_AIR_QUALITY_URL and ENVIRONMENT_AIR_QUALITY_DISABLED
are honoured; the account switch lives on PATCH /api/environment/preferences.

The home, each dated location period and each stored day keep their coarse
location sealed as one value under healthlog/environment-location/v1. The
writers write the sealed value only, the readers open it and fall back to the
readable columns for rows the free-text encryption backfill has not reached,
and the backfill now seals all three and clears the readable columns. The
backup carries the location readable in a portable file and sealed in a
disaster-recovery file; a restore seals either under the receiving key.

Three correlation channels join the environment family (PM2.5, the ozone
8-hour high, the day's highest pollen count), read through the averaged
same-day window and left empty while air quality is off. The Coach and MCP
gain get_environment: module-gated, clamped to the lookback limit, no
coordinate or place name anywhere, with provenance and the attribution
lines. The settings section gets the switch, the coverage and the newest
day's values; the dashboard shows a quiet chip on a day with high pollen,
a warm night or very poor air. Copernicus and Open-Meteo are credited next
to the values.
This commit is contained in:
Marc-André Bombeck committed 2026-10-08 10:02:39 +02:00
1 parent aca497d9ac
commit 3c98d95a40
73 files changed
+6061 -385

No files matched your search

@@ -6,6 +6,7 @@
* a person instead of ciphertext. Every refusal here leaves the account's
* current value in place and names the column in the restore's skip report.
*/
import { Buffer } from "node:buffer";
import { afterEach, describe, expect, it, vi } from "vitest";
process.env.ENCRYPTION_KEY ??=
@@ -19,6 +20,7 @@ import {
buildAccountSettingsBackupSection,
} from "@/lib/export/account-settings-backup";
import { UNREADABLE_EXPORT_MARKER } from "@/lib/export/unreadable-marker";
import { openLocation, sealLocation } from "@/lib/environment/location-cipher";
import { reasoningEffortFor } from "@/lib/ai/reasoning-effort";
import {
coachReasoningLevel,
@@ -324,3 +326,74 @@ describe("admitAccountSettings", () => {
expect(data).toEqual({ heightCm: 180 });
});
});
describe("the environment home (v1.42, sealed at rest)", () => {
it("carries the home readable in a portable file, opened from its sealed copy", async () => {
const { accountSettings } = await buildAccountSettingsBackupSection(
prismaReturning(
rowWith({
homeLocationEncrypted: sealLocation({
lat: 51.5,
lon: 7.2,
label: "Bochum, Germany",
}),
}),
),
"u1",
{ purpose: "portable-export" },
);
expect(accountSettings).toMatchObject({
homeLat: 51.5,
homeLon: 7.2,
homeLabel: "Bochum, Germany",
});
expect(accountSettings).not.toHaveProperty("homeLocationEncrypted");
});
it("carries the sealed copy verbatim in a disaster-recovery file", async () => {
const sealed = sealLocation({ lat: 51.5, lon: 7.2, label: "Bochum" });
const { accountSettings } = await buildAccountSettingsBackupSection(
prismaReturning(rowWith({ homeLocationEncrypted: sealed })),
"u1",
{ purpose: "disaster-recovery" },
);
expect(accountSettings!.homeLocationEncrypted).toBe(
Buffer.from(sealed).toString("base64"),
);
});
it("seals a readable home from a file and writes the readable columns empty", () => {
const { data, refused } = admitAccountSettings(
{ homeLat: 48.137, homeLon: 11.575, homeLabel: "Munich" },
ctx,
);
expect(refused).toEqual([]);
expect(data).toMatchObject({
homeLat: null,
homeLon: null,
homeLabel: null,
});
expect(openLocation(data.homeLocationEncrypted as Uint8Array)).toEqual({
lat: 48.1,
lon: 11.6,
label: "Munich",
});
});
it("takes a sealed home as it came, and clears the home when the file has none", () => {
const sealed = sealLocation({ lat: 1, lon: 2, label: "x" });
const restored = admitAccountSettings(
{ homeLocationEncrypted: Buffer.from(sealed).toString("base64") },
ctx,
).data.homeLocationEncrypted as Uint8Array;
expect(Buffer.from(restored).equals(Buffer.from(sealed))).toBe(true);
expect(
admitAccountSettings({ homeLat: null, homeLon: null }, ctx).data,
).toEqual({
homeLocationEncrypted: null,
homeLat: null,
homeLon: null,
homeLabel: null,
});
});
});