diff --git a/.gitignore b/.gitignore index 00b2441a2..5197ca4dc 100644 --- a/.gitignore +++ b/.gitignore @@ -311,3 +311,9 @@ v2/crates/wifi-densepose-sensing-server/data/ # ADR-324: wasm-bindgen output for ruview-offaxis is generated locally # (see the crate README); never commit generated artifacts. v2/crates/ruview-offaxis/pkg/ + +# fal.ai visual-teacher LoRA training data/weights -- regenerable, never +# commit training images or model weights (ADR-353) +scripts/fal-visual-teacher/training-data/ +scripts/fal-visual-teacher/training-data.zip +scripts/fal-visual-teacher/output/ diff --git a/assets/fal-visual-teacher-sample.json b/assets/fal-visual-teacher-sample.json new file mode 100644 index 000000000..3a4a28315 --- /dev/null +++ b/assets/fal-visual-teacher-sample.json @@ -0,0 +1,7 @@ +{ + "endpoint": "fal-ai/flux-lora", + "lora_url": "https://v3b.fal.media/files/b/0aa8c60d/OjaBs6Gi8pMhZqMo79iym_pytorch_lora_weights.safetensors", + "prompt": "ruviewstyle room visualization, a living room with a glowing cyan pose skeleton overlay, wifi signal arcs radiating across the space, a vitals readout panel, dark tech aesthetic, synthetic sensor visualization", + "scale": 1.0, + "source_image_url": "https://v3b.fal.media/files/b/0aa8cf67/JH1qyC3GCKQ9ojkrAUXe9.png" +} \ No newline at end of file diff --git a/assets/fal-visual-teacher-sample.png b/assets/fal-visual-teacher-sample.png new file mode 100644 index 000000000..011e3ae41 Binary files /dev/null and b/assets/fal-visual-teacher-sample.png differ diff --git a/docs/adr/ADR-353-fal-visual-teacher-lora.md b/docs/adr/ADR-353-fal-visual-teacher-lora.md new file mode 100644 index 000000000..e6444e54b --- /dev/null +++ b/docs/adr/ADR-353-fal-visual-teacher-lora.md @@ -0,0 +1,162 @@ +# ADR-353: fal.ai synthetic visual teacher LoRA + +- **Status**: Accepted +- **Date**: 2026-09-02 +- **Deciders**: ruv +- **Owners**: RuView visualization / neural-styling maintainers +- **Tags**: fal, lora, synthetic-visualization, display-only, cost-control +- **Parent**: None +- **Extends**: None +- **Supersedes**: None + +## Decision + +RuView will use one real, fal.ai-trained LoRA fine-tune (a "synthetic visual +teacher") to steer fal.ai image/video generation toward a consistent +"RuView-style" room visualization — glowing pose overlays, WiFi signal arcs, +vitals readout panels, dark tech aesthetic — instead of relying on ad hoc +prompt engineering against a stock model. + +**The architecture boundary is non-negotiable:** + +> The implementation boundary is now clear: fal.ai will train an optional +> synthetic visual teacher, while RuView's measured RF, pose, vitals, +> identity, and confidence remain local and authoritative. + +Concretely: + +- The trained model produced here is ONLY ever allowed to generate + supplementary synthetic visual/pixel output for display or broadcast. +- It must NEVER be a source of, or influence, any measured/derived value: RF/ + CSI measurements, pose estimates, vitals (heart rate/breathing rate), + identity, or confidence/quality scores. Those stay local, deterministic, + and authoritative — the existing `wifi-densepose-engine` / + `ruview-twin` / `ruview-witness` stack — completely untouched by this work. +- Any output this model produces must be clearly framed as SYNTHETIC per this + repo's rule: "Never present WiFi sensing as camera-grade." +- This model has no read or write path into the RF/pose/vitals/identity + pipeline. It consumes only a text prompt (and optionally a reference + image) and returns pixels for display. + +**Evidence status:** the training data extraction, training submission +script, and validation script are real and committed. The training run +completed and validation succeeded — trained model id, actual spend, and the +validation sample are recorded in "Result" below. Actual-vs-billed cost +reconciliation against the fal.ai account dashboard remains UNMEASURED (the +figure below is a documented-pricing estimate). + +## Context + +A concurrent workstream (`ruview-fal-room-viz` / `ruview-room-viz-h3-upgrade`) +generated real fal.ai room-visualization videos across several style presets +(architectural, abstract, branded, and an "h3" refresh: architectural, +cinematic, minimal, abstract) using stock fal.ai video models driven by +prompt engineering alone. A separate concurrently-running agent +("realtime-dual-renderer") is building a neural-styling layer that needs a +*specific* trained model id to call, rather than continuing to rely on prompt +engineering against a generic model. + +This ADR's job is narrow: turn the real captured style presets into a real +trained fal.ai LoRA, verify it actually works, and hand off the model id. It +does not implement the renderer integration — that is +`realtime-dual-renderer`'s scope — and it does not touch the measured +RF/pose/vitals/identity/confidence pipeline in any way. + +## Training data + +Source: the real fal.ai-generated room-visualization MP4s already captured +this session (read from `/tmp/room-viz-*/room-viz.mp4` on `ruvultra`, one per +style preset — not committed to the repo; the repo has no camera or person +data in these clips, they are synthetic renders). `scripts/fal-visual-teacher/extract_frames.py` +extracts one JPEG frame every 1.5 seconds from each of the 7 real style-preset +videos (architectural, abstract, branded, h3-architectural, h3-cinematic, +h3-minimal, h3-abstract), producing 27 frames total, each paired with a +caption `.txt` describing the shared RuView visual motifs. Frames and the +training zip are gitignored (`scripts/fal-visual-teacher/training-data/`, +`training-data.zip`) — they are regenerable from the source MP4s and are not +committed, consistent with the repo's no-generated-artifacts rule. + +## Training endpoint and pricing (verified 2026-09-02) + +- **Endpoint**: `fal-ai/flux-lora-fast-training` — a real, currently-listed + fal.ai LoRA training endpoint for FLUX.1, verified against + `https://fal.ai/models/fal-ai/flux-lora-fast-training/api`. +- **Pricing**: $2 per training run at the default 1000 steps, scaling + linearly with steps (verified against fal.ai's own pricing text on the + model page). +- **Mode**: submitted with `is_style: true` (style LoRA, not + subject/character LoRA) and explicit per-frame caption files, so fal.ai's + auto-captioning/segmentation is skipped in favor of our own captions. +- **Inference endpoint** (validation only): `fal-ai/flux-lora`, $0.035 per + megapixel, accepting `loras: [{ path, scale }]` pointing at the trained + `diffusers_lora_file` URL. + +## Budget + +- **Original cap**: $15 total (this task's cap only; not shared with other + concurrent tasks' budgets this session). +- **Raised cap**: the user explicitly raised this task's cap to **$100** on + 2026-09-02, mid-run, as real headroom for a larger/better run or retries — + not an instruction to spend all of it. +- **Planned spend**: ~$2 for the training run (1000 steps, default) + ~$0.04 + per validation inference image. +- **Actual spend**: ~$2.04 (one 1000-step training run at $2/run + one + landscape_16_9 validation image at $0.035/megapixel, rounded up to 1 + megapixel). This is a documented-pricing estimate (`CLAIMED`), not yet + reconciled against the fal.ai account billing dashboard (`MEASURED`). + Comfortably under both the original $15 cap and the raised $100 cap. +- **Why no larger follow-up run**: the first validation image already showed + a clean, on-style result (glowing pose overlay, WiFi arcs, vitals panels, + dark aesthetic, all present and legible) on the first attempt. Spending + further into the raised budget for marginal improvement was judged not + worth it against a result that already meets the bar; the fallback path in + "Consequences" was not needed. + +## Result + +- **Trained model id / LoRA weights URL**: + `https://v3b.fal.media/files/b/0aa8c60d/OjaBs6Gi8pMhZqMo79iym_pytorch_lora_weights.safetensors` + (131 MB `diffusers_lora_file`, paired `config.json` at + `https://v3b.fal.media/files/b/0aa8c60d/-GU6dd_2ZFqq18I0CuPOB_config.json`). + Use with `fal-ai/flux-lora` (or any fal.ai FLUX.1 endpoint accepting a + `loras: [{ path, scale }]` array) — trigger word `ruviewstyle`. Full + training record: `scripts/fal-visual-teacher/output/training-result.json` + (gitignored — regenerate by re-running `train_lora.py`, or copy the URL + above). +- **Training request id**: `01a060b2-c940-7972-abf9-ea48847eb2ae` (fal.ai + `fal-ai/flux-lora-fast-training`, 1000 steps, `is_style: true`, 27 training + images, completed in 376s on an H100/H200 worker). +- **Actual spend**: ~$2.04 (see Budget above). +- **Validation sample**: `assets/fal-visual-teacher-sample.png` (+ sibling + `assets/fal-visual-teacher-sample.json` with the exact prompt, LoRA scale + 1.0, and source fal.ai URL) — a real `fal-ai/flux-lora` inference call + using the trained LoRA, generated 2026-09-02. The image shows a glowing + cyan pose skeleton, WiFi signal arcs, vitals-style readout panels, and a + HUD-style TV overlay in a dark living room — confirming the LoRA learned + and reproduces the intended RuView visual motifs. +- **Consumer**: `realtime-dual-renderer` (separate worktree + `~/projects/ruview-worktrees/realtime-render`) — notified directly with the + model id above. + +## Consequences + +A real trained LoRA gives the display layer a specific, repeatable style +target instead of prompt engineering against a stock model, at a bounded and +small real cost ($2 training + cents per generated image). Because the model +only ever produces pixels for a prompt, and has no schema path into any +measured/derived RuView value, it cannot silently become a second source of +truth for pose, vitals, identity, or confidence — that boundary is structural +(no shared types, no shared data path), not just documented convention. + +If the trained LoRA does not meaningfully improve style consistency over the +stock model (a genuine risk with 27 training images and default steps), the +fallback is to keep using prompt engineering against the stock model and note +that outcome honestly here rather than claim a training win that isn't real. + +## References + +- `scripts/fal-visual-teacher/extract_frames.py` — real frame extraction +- `scripts/fal-visual-teacher/train_lora.py` — real training submission +- `scripts/fal-visual-teacher/validate.py` — real validation inference +- [fal.ai Flux LoRA Fast Training](https://fal.ai/models/fal-ai/flux-lora-fast-training) +- [fal.ai FLUX.1 with LoRAs (inference)](https://fal.ai/models/fal-ai/flux-lora) diff --git a/docs/adr/ADR-354-forecast-governance-enforcement-in-ruforecast-submodule.md b/docs/adr/ADR-354-forecast-governance-enforcement-in-ruforecast-submodule.md new file mode 100644 index 000000000..9fc039159 --- /dev/null +++ b/docs/adr/ADR-354-forecast-governance-enforcement-in-ruforecast-submodule.md @@ -0,0 +1,89 @@ +# ADR-354: fal.ai forecast governance and spend enforcement now real, in the vendored RuForecast submodule + +- **Status**: Accepted +- **Date**: 2026-09-02 +- **Deciders**: ruv +- **Owners**: RuView forecast training, security, and release maintainers +- **Tags**: forecasting, training, fal, governance, submodule, cost-control +- **Parent**: ADR-349 +- **Extends**: ADR-348, ADR-349 +- **Supersedes**: None + +## Decision + +ADR-349 specified two governance controls for RuView Forecast's fal.ai +hosted path: operator-verified dataset/schema/policy/recipe authorization, +and an explicit local spend-approval record bound to the request digest and +maximum units. A 9-agent code review of the training stack merged as PR +#1766 found that neither control was actually wired into the code path that +builds a submittable, fal-destined training contract — `FalGovernanceVerifier` +existed with zero callers outside its own tests, and no spend-approval type +existed anywhere in the codebase. + +That code no longer lives directly in this repository. Between the review +and this fix, `v2/crates/ruview-forecast-{core,model,train}` was extracted +into its own repository, `github.com/ruvnet/RuForecast`, and is now vendored +back into `v2/crates/ruforecast` as a git submodule (crates renamed to +`ruforecast-{core,model,train}`). This ADR records two things at the RuView +level: + +1. **The governance and spend-approval gaps are fixed**, in + `ruvnet/RuForecast` PR #2 (branch `fix/governance-and-spend-enforcement`). + The full technical decision record — what changed, why, and the + requirements/acceptance table — lives in that repository's own + [ADR-350](https://github.com/ruvnet/RuForecast/blob/main/docs/adr/ADR-350-fal-governance-and-spend-enforcement.md), + which this ADR defers to rather than duplicating. In summary: a + fal-destined `TrainSpec` can no longer be constructed without a real, + operator-signed, single-use-verified governance receipt; a hosted + reservation can no longer be built without a real, operator-signed spend + approval bound to the exact request digest and cost ceiling; and several + related correctness/resilience gaps on the same trust boundary + (unauthenticated Direct Server webhook, no execution timeout, mismatched + local/hosted artifact budgets, cancellation permanently wedging a job) + were fixed in the same pass. +2. **The submodule pointer in this repository is bumped** to the commit + containing that fix, once `ruvnet/RuForecast`'s own CI is green on that + PR and it merges. This RuView-side change is deliberately small: a + pointer bump plus this ADR, not a re-implementation or re-review of code + that now lives, is tested, and is reviewed in its own repository. + +## Context + +RuView's own copies of ADR-348 and ADR-349 predate the extraction and still +describe the crates as if they lived directly under `v2/crates/`. They are +left as-is here (not corrected path-by-path) since the canonical, actively +maintained copies of the forecast-specific ADRs now live in +`ruvnet/RuForecast`'s own `docs/adr/`, which was seeded with copies of +ADR-348/349 at extraction time and now also carries ADR-350. Consult that +repository for the forecast subsystem's own decision history going forward; +this repository's ADR series should record RuView-level integration +decisions about the vendored submodule (like this one), not attempt to +re-host the submodule's internal history. + +This repo's CLAUDE.md requires an ADR for load-bearing upstream changes. +The governance-enforcement fix is load-bearing for RuView even though none +of its source lines are: it changes what RuView's vendored forecast +subsystem actually requires before it will reach real, billable fal.ai +infrastructure on behalf of a RuView deployment. + +## Consequences + +Once the submodule pointer here is bumped, any RuView-side code, CLI +wrapper, or documentation that assumes the old, unenforced fal.ai +submission path (a bare `SourceState::synthetic` claim with no verified +governance receipt or spend approval) will no longer compile or run against +the vendored crate — `TrainSpec::new_fal_synthetic` and +`ReservedSyntheticSubmission::reserve` both gained new required parameters. +A repository-wide search found no such RuView-side caller today; this ADR +records the change in case one is added later without checking the +submodule's current API first. + +This pointer bump must not merge before `ruvnet/RuForecast` PR #2 merges and +its CI is green — see that PR for status. + +## References + +- [ADR-349](./ADR-349-governed-local-and-fal-forecast-training.md) (this repository's copy; canonical copy is in `ruvnet/RuForecast`) +- [ruvnet/RuForecast PR #2](https://github.com/ruvnet/RuForecast/pull/2) +- [ruvnet/RuForecast ADR-350](https://github.com/ruvnet/RuForecast/blob/main/docs/adr/ADR-350-fal-governance-and-spend-enforcement.md) +- [`v2/crates/ruforecast`](../../v2/crates/ruforecast) (submodule) diff --git a/docs/adr/ADR-355-local-empty-room-bootstrap-prior.md b/docs/adr/ADR-355-local-empty-room-bootstrap-prior.md new file mode 100644 index 000000000..e03ef06d7 --- /dev/null +++ b/docs/adr/ADR-355-local-empty-room-bootstrap-prior.md @@ -0,0 +1,134 @@ +# ADR 355: Local empty room bootstrap prior + +Status: Accepted + +Date: 2026-09-04 + +## Context + +Most installations are occupied when RuView first starts. An uncalibrated +signal heuristic can interpret HVAC, clock error, neighboring motion, or a +short covariance window as a person. It can also surface plausible numeric +vitals before a room model establishes occupant attribution. + +One empty home is not representative training data for all homes and contains +no positive human examples. It is useful only as a recent negative prior for +the same installation. It cannot define a universal human signal model. + +The field model was memory only. The server also used a relative `data/` +directory, which is unreliable for embedded application hosts. + +## Decision + +RuView may persist a completed empty room field model as a local bootstrap +prior only after two server controlled stages: + +1. Normal calibration reaches both 1,000 accepted frames and 600 seconds. + The frame target represents twenty complete fifty frame runtime windows. + A caller may select one currently live ESP32 source. The server checks its + freshness before replacing an active bootstrap prior, binds collection to + that source, and rejects frames from other radios. When no source is + selected, the first accepted ESP32 source preserves legacy behavior. +2. Promotion observes 12 new samples spaced by at least one second. Each + sample has a four second bounded acquisition timeout. At least 10 must + resolve empty, all ticks must be fresh, and none may publish numeric vital + signs. + +The stored image contains aggregate means, environmental modes, mode energies, +noise statistics, sorted scalar residual references, configuration, one source +node ID, and a server generated model ID. It excludes raw CSI, waveforms, +device addresses, room names, pose, identity, credentials, and positive human +labels. Runtime matching compares a fifty frame mean with the learned residual +distribution. Its score is empirical background conformance, not an absence +probability. + +The image is bound to a SHA 256 digest of the stable installation ID, carries +an integrity digest over the exact stored payload bytes, is capped at 1 MiB, +and inherits the field model expiry. +Files with an invalid schema, dimensions, values, node set, installation +binding, digest, size, or lifetime fail closed. + +Restoring an image creates a field model in `bootstrap_only` mode. It may +reduce an uncalibrated false person count, but it cannot authorize calibrated +evidence or numeric heart and breathing rates. Explicit calibration replaces +it with fresh statistics. Cancelling an unfinished capture restores a valid +prior. Administrator scoped reset removes it. + +A restored image may receive one operator confirmed held out empty refinement. +The server measures 12 fresh, source bound runtime residuals at one second +spacing and requires zero stale samples and zero numeric vital samples. It +persists only the scalar residuals. The new boundary is the larger of the +existing threshold and 110 percent of the held out 95th percentile, capped at +125 percent of the prior threshold. The one use marker persists in the model, +so repeated requests cannot ratchet the threshold upward. + +The status endpoint reports the learned runtime window, residual threshold, +reference count, current background conformance, and the negative only +authority boundary. A restored model never resumes collection and never +persists raw calibration frames. + +The server accepts `--data-dir` or `RUVIEW_DATA_DIR`, allowing an application +host to provide a protected state directory. + +## Consequences + +Startup can use recent measured background structure instead of only generic +heuristics. No accuracy lift is claimed until occupied and empty held out data +from multiple installations is recorded. + +The largest failure mode is overfitting to a temporarily quiet home and +suppressing a weak occupant. Controls are low authority, 24 hour expiry, +installation binding, vital abstention, explicit recalibration, and reset. + +A human signal model still requires positive, consented, leakage separated +occupied sequences. Empty data is never relabeled as human training. + +## Implementation + +1. `wifi-densepose-signal::ruvsense::field_model` exports and validates + `FieldModelSnapshotV1`. +2. `wifi-densepose-sensing-server::bootstrap_baseline` owns schema, integrity, + storage, lifetime, and the held out gate. +3. `POST /api/v1/calibration/bootstrap/promote` measures the validation window + and stores the image only on pass. +4. `GET /api/v1/calibration/status` reports bootstrap authority and explicitly + denies calibrated evidence and numeric vital authority. +5. Numeric vitals fail closed unless an explicit fresh calibration resolves + exactly one occupant at sufficient signal quality and confidence. +6. The legacy `edge_vitals` WebSocket message applies the same publication + gate as `sensing_update`. When the bootstrap background matches, it emits + absent state, zero people, null numeric rates, and an explicit abstention + reason instead of forwarding raw edge candidates. +7. `POST /api/v1/calibration/start?source_node_id=N` selects a live source for + deterministic single link capture. A missing or stale selection fails + without disabling the active bootstrap prior. +8. `POST /api/v1/calibration/bootstrap/refine?confirmed_empty=true&source_node_id=N` + performs the bounded one time held out refinement. It requires administrator + authority when transport authentication is enabled. + +## Measured installation evidence + +One physical empty home run on source Node 5 collected 1,762 accepted frames +over 655 seconds and produced 35 privacy reduced reference windows. Promotion +observed 12 of 12 fresh empty samples, zero stale samples, and zero numeric +vital samples. After process restart, 12 of 12 fresh samples matched the empty +background and contained no numeric vital signs. Background conformance scores +ranged from 50.0 to 64.3 percent with a median of 54.3 percent. + +This is measured negative evidence for one installation. No occupied positive +holdout was recorded, so it does not prove person recall, person counting, or +cross home accuracy improvement. + +## Acceptance test + +With a physically empty room, complete calibration on stable nodes, promote +the single source model, and restart the server with the same installation ID +and data directory. Status must report `binding_mode=bootstrap_only`, zero +collection frames, and false authority for calibrated evidence and numeric +vitals. Twelve fresh empty samples must contain no numeric vital signs. +Starting a new room calibration must replace the prior and begin at frame zero. +When several radios are live, selecting Node 5 must leave Nodes 3 and 7 out of +the source set for the entire capture. +After restart, a confirmed empty holdout may refine the restored boundary once. +The stored snapshot must reject a second refinement and must continue to deny +calibrated evidence and numeric vital authority. diff --git a/docs/adr/ADR-356-rate-truthful-csi-and-reliability-gated-adaptation.md b/docs/adr/ADR-356-rate-truthful-csi-and-reliability-gated-adaptation.md new file mode 100644 index 000000000..9562ce1a2 --- /dev/null +++ b/docs/adr/ADR-356-rate-truthful-csi-and-reliability-gated-adaptation.md @@ -0,0 +1,151 @@ +# ADR 356: Rate truthful CSI and reliability gated room adaptation + +Status: Accepted + +Date: 2026-09-05 + +## Context + +RuView mixed three different notions of time. ESP32 firmware requested a packet +rate, the server observed a delivered rate, and several server paths still used +fixed 2 Hz, 10 Hz, or 20 Hz assumptions. One feature named +`dominant_freq_hz` was not a temporal frequency at all. It was the index of the +strongest subcarrier multiplied by 0.05. A second defect compared a newly +inserted frame with itself, forcing that motion delta to zero. + +These errors can move a spectral peak into the breathing or heartbeat band and +produce plausible but false values. They also make models sensitive to WiFi +traffic and processor scheduling rather than physical motion. + +ESP-IDF documents that `wifi_csi_info_t.first_word_invalid` marks the first four +CSI bytes invalid because of a hardware limitation. It also documents that the +CSI receive callback runs in the WiFi task and should hand work to a lower +priority task rather than perform lengthy processing. RuView serialized the +invalid bytes unchanged and fed the original buffer to edge processing. + +Recent work suggests four useful directions with different maturity: + +1. OpenCSI represents each link as a dimensionless residual against its own + quiet period temporal standard deviation and exposes baseline maturity. Its + reported transfer results are limited to binary presence. +2. MORIC derives motion centered delay and Doppler views to reduce dependence + on static environment specific CSI structure. +3. Parameter efficient CSI crowd counting combines self supervised pretraining, + small room adapters, and a stateful counting machine. +4. WiFi JEPA masks complete radio links and predicts their latent embeddings, + using real and ray traced CSI for pose representation learning. + +The papers report promising external results. None is measured RuView evidence, +and none authorizes deployment without leakage free occupied and empty held out +evaluation. + +## Decision + +### Firmware ingestion + +1. When ESP-IDF marks the first CSI word invalid, zero the first four serialized + CSI bytes while retaining the fixed packet shape. +2. Set ADR-018 byte 19 bit 5 to record that sanitation occurred. +3. Feed edge DSP from the sanitized serialized payload, never the original + invalid driver buffer. +4. Keep callback work bounded to sanitation, serialization, and the existing + queue handoff. Do not add transforms to the WiFi task. + +### Server time model + +1. Estimate delivered CSI rate from accepted monotonic frame timestamps after + five samples. Use 20 Hz only during startup. +2. Cap the physical input rate at the firmware contract of 50 Hz so a burst or + timestamp artifact cannot retune downstream filters above their supported + range. +3. Reconfigure the vital detector only after a 20 percent rate change. Clear + temporal and smoothing state whenever its clock changes. Samples calculated + with different clocks must never be blended. +4. Compute temporal dominant frequency from the recent mean amplitude series + with demeaning, a Hann window, and bounded direct Fourier bins from 0.1 Hz to + the smaller of 4 Hz or 45 percent of sample rate. +5. Return zero for short, flat, nonfinite, or spectrally ambiguous windows. +6. Compare the current frame with the prior frame, not itself, for motion delta. +7. Export zero sample rate until the estimate is mature. Never present a startup + fallback as a measured node property. + +### Model compatibility and room reliability + +1. Preserve the historical fifth input feature for version 1 adaptive models at + the model boundary. Version 2 and later models consume the true temporal + frequency. This prevents an existing model from silently receiving a + different input distribution after a server update. +2. A background model is unreliable until it contains at least ten finite, + nonnegative residual references. An unreliable model cannot suppress a + runtime change and exposes no conformance score. +3. Expose background maturity, reliability, and a robust residual Z score based + on the median and median absolute deviation. +4. Keep the persisted empty home prior negative only. It cannot authorize + calibrated presence, person count, pose, or numeric vitals. + +### Model roadmap + +The following work remains proposal only: + +1. Evaluate an OpenCSI style per link quiet residual normalization for binary + presence without replacing absolute magnitude features used for motion. +2. Evaluate delay and Doppler views after phase sanitation for activity. +3. Pretrain a frozen multilink encoder and adapt small room specific modules for + counting. +4. Evaluate link masked self supervision for pose only when at least three + spatially distinct links and synchronized positive pose labels exist. + +Every candidate uses immutable manifests and splits by room, subject, +recording, and time. It is compared with the frozen baseline under identical +seeds. Counting includes confusion matrices and mean absolute error. Pose +includes PCK at 20 centimetres, MPJPE, and the mean pose baseline. A shuffled +label control must fail. + +Promotion requires at least 10 percent held out lift, less than 10 milliseconds +added p95 inference latency, no more than 20 percent peak memory overhead, zero +leakage or secret findings, frozen anchor retention, and no regression in empty +rejection or occupied recall. Promotion remains an explicit maintainer action. + +## Consequences + +The firmware no longer preserves a known invalid prefix as sensor evidence. The +server uses an observed physical clock for spectral features and vital filters. +Existing version 1 adaptive models remain compatible. Sparse background samples +fail open to observation and fail closed for suppression. + +The direct Fourier estimator is intentionally small and bounded to 128 temporal +samples. It is portable and deterministic, but it is not a replacement for a +phase sanitized delay and Doppler pipeline. + +The largest remaining uncertainty is human recall. Empty home evidence can +measure false presence but cannot show whether weak occupied signals are being +suppressed. The fix path is a consented, leakage separated occupied holdout with +multiple spatial links and synchronized reference labels. + +## Research basis + +1. Espressif ESP-IDF WiFi CSI programming guide: + https://docs.espressif.com/projects/esp-idf/en/v5.5.4/esp32/api-guides/wifi.html +2. Espressif ESP CSI reference implementation: + https://github.com/espressif/esp-csi +3. OpenCSI: + https://arxiv.org/abs/2607.26665 +4. MORIC: + https://arxiv.org/abs/2506.12997 +5. Parameter efficient CSI crowd counting: + https://arxiv.org/abs/2601.02203 +6. WiFi JEPA: + https://arxiv.org/abs/2607.11064 + +## Acceptance test + +Run the firmware serializer host tests and the focused hardware, signal, and +sensing server Rust suites. The deterministic serializer test must prove the +first four bytes are zero only when ESP-IDF marks them invalid and bit 5 must +round trip through the Rust parser. The temporal estimator must recover a +1.2 Hz synthetic signal within 0.21 Hz, abstain on short and flat inputs, and +remain below 10 milliseconds p95 on the target Mac. Existing version 1 model +input must retain the legacy proxy. A background model with nine references +must never report a match or suppress a runtime change. Physical firmware is +not qualified until an S3 and C6 boot and stream the sanitized flag without +watchdog, memory, or transport regressions during a five minute burn in. diff --git a/docs/adr/ADR-357-raw-csi-calibration-sample-integrity.md b/docs/adr/ADR-357-raw-csi-calibration-sample-integrity.md new file mode 100644 index 000000000..6cdd25a3a --- /dev/null +++ b/docs/adr/ADR-357-raw-csi-calibration-sample-integrity.md @@ -0,0 +1,143 @@ +# ADR 357: Raw CSI calibration sample integrity + +Status: Accepted + +Date: 2026-09-06 + +## Context + +The empty room field model requires 1,000 accepted observations over at least +600 seconds. The sensing server previously invoked calibration from both the +raw CSI path and the one second edge vital path. Edge vital packets did not add +a CSI observation, so that path repeatedly fed the existing history tail. A +counter could therefore satisfy the frame gate without representing 1,000 +independent CSI packets. + +Node liveness also combined edge vital and CSI traffic. A source emitting only +edge vital summaries could appear live enough to start calibration even though +it had no fresh raw CSI. Finally, cloning the complete per node history before +each feed copied as much as 204,800 bytes for a 100 frame, 256 subcarrier +window even though calibration consumes only the current observation. + +The earlier single installation result recorded in ADR 355 predates this +admission boundary. Its frame count is not accepted as evidence of independent +CSI coverage. It remains useful only as diagnosis of the failure mode and must +not be promoted as a production bootstrap prior. + +## Decision + +1. Only a packet on the frozen, model specific CSI grid may advance field + calibration. Edge vital packets update their own health and presentation + state but never feed the field model. The independent live inference grid + remains unchanged. +2. Each calibration session stores the last admitted wrapping sequence for + every contributing node. Duplicate sequences are ignored. Normal forward + progress and the `u32::MAX` to zero wrap are accepted. +3. A packet one through sixteen sequence values behind the current high water mark + is bounded UDP reordering. It is recorded and dropped without feeding the + model or moving the high water mark. A backward depth above sixteen, or an + ambiguous half range sequence, latches that node as faulted for the remainder + of the session. Status exposes the policy, reorder window, per node reorder + count, maximum observed depth, and `sequence_fault_node_ids`. Finalization + returns `calibration_sequence_discontinuity` after a fault. The operator must + cancel and restart the empty room capture. +4. A requested source may start calibration only after a header only, bounded + observation window identifies one stable grid. The selected grid needs at + least ten seconds of evidence, a measured rate of at least 2 Hz, a latest + sample newer than five seconds, and no observed gap of five seconds or more. + Calibration status reports the immutable grid, selection evidence, current + grid age, and `last_sequence_by_node` without exposing CSI values. +5. Start, cancel, reset, and process initialization clear sequence admission + state. A failed feed does not advance the sequence cursor, so the same + packet may be retried after a transient model input failure. +6. The field bridge receives the current amplitude slice rather than a cloned + history. A separate bounded runtime history retains only that same frozen + grid, so the model is never trained on one symbol layout and evaluated on a + different layout. Canonical grid normalization remains unchanged. +7. The exact source grid is stored inside the digest protected bootstrap v2 + payload. A restored model rebuilds only its matching runtime history. An + unbound legacy image cannot become an active startup prior. +8. Successful finalization clears the selected grid runtime history. Bootstrap + promotion waits up to 35 seconds for a complete fresh runtime window before + scoring twelve additional samples. This covers a 50 frame window at the 2 Hz + admission floor plus ten seconds of bounded jitter. Calibration observations + therefore cannot enter the held out decision window. +9. Stop and promotion require the frozen grid to remain fresh and reject any + latched sequence fault immediately before persistence. Bootstrap v2 accepts + exactly one source node because the current runtime supports one grid bound + field model. +10. The legacy `--calibrate` startup switch fails closed before opening ports. + Grid evidence does not exist at process start, so operators start the server, + wait ten seconds, then call the explicit single source calibration API. + +## Consequences + +Calibration frame count now means unique, forward raw CSI observations within +one uninterrupted sequence epoch. A vitals only node cannot produce a +bootstrap model. Late packets never become calibration evidence. + +One physical Node 5 capture observed sequence 4,119,566 followed by 4,119,563, +4,119,564, and 4,119,565 in one receive batch. A second independent 90 second +capture observed bounded late packets at depths 9, 10, and 11. The window of +sixteen covers the measured maximum with five positions of margin. At the +firmware acceptance ceiling of 50 Hz, the window represents no more than 320 +milliseconds. Late packets are always dropped. Larger regressions remain +failed closed. + +The same 90 second capture measured three interleaved HT grids on Node 5. The +64 subcarrier stream delivered 436 frames at 4.84 Hz with a 1.56 second maximum +gap. The 128 stream delivered 100 frames at 1.11 Hz with a 5.05 second maximum +gap. The 192 stream delivered 49 frames at 0.54 Hz with an 11.53 second maximum +gap. Only the 64 grid exceeded the 2 Hz admission floor and stayed inside the +five second freshness boundary, so the final field model binds to that grid. + +The current wire format still has no authenticated boot epoch or stable device +identity. A restart very early in a session or a same endpoint adversary cannot +be distinguished cryptographically. A future ADR 018 revision must carry an +authenticated device identifier and a random per boot identifier before the +server claims exact restart or identity collision detection. + +No new raw CSI is persisted. Sequence cursors and fault markers remain bounded +in memory to active calibration sources. The bootstrap schema advances to v2 +to protect the grid identity while its negative only authority remains +unchanged. + +## Implementation + +1. `NodeState` records header only recent grid evidence and a separate bounded + field model history. +2. `AppStateInner::maybe_feed_calibration_frame` owns source binding, sequence + ordering, fault latching, model feed, and cursor commit. +3. `field_bridge::maybe_feed_calibration` accepts exactly one current amplitude + slice and rejects an empty observation. +4. Calibration start, status, stop, cancel, reset, promotion, refinement, and + restart enforce the frozen source and grid contract. +5. `bootstrap_baseline` persists the source grid inside the verified v2 image. +6. Finalization clears training history, and promotion blocks until the model + has a complete fresh scoring window plus twelve separately spaced samples. +7. Startup calibration without measured grid evidence exits with a typed + operator instruction instead of creating a model that can never receive a + frame. + +## Acceptance test + +Unit tests must prove stable grid selection, rate and gap rejection, first +packet acceptance, forward progress, duplicate rejection, source and grid +isolation, maximum counter wrap, empty input retry, measured depth eleven +reordering without a model feed, depth sixteen acceptance, depth seventeen +latching, half range rejection, session clear, receipt visibility, protected +grid persistence, and typed stop refusal. +Tests must also prove startup flag refusal, missing and stale grid refusal, +complete holdout isolation, and sequence fault rejection during holdout. + +On physical hardware, begin a Node 5 empty room capture and observe a fresh +sixty second interval. Calibration frame growth must match unique 64 subcarrier +Node 5 sequences while the independent global inference grid remains unchanged. +Edge vital packets and other grids must produce zero growth. The bound grid +must remain active, sequence faults must remain empty, and numeric vital output +must remain absent. Any bounded late packet must appear in the reorder receipts +while leaving the model count unchanged. Complete both the 1,000 frame and 600 +second gates, then run the twelve sample server controlled bootstrap holdout. +Restart and prove that only the persisted grid rebuilds the runtime history. +Do not claim human detection improvement until a separate consented occupied +holdout demonstrates retained recall. diff --git a/docs/adr/README.md b/docs/adr/README.md index d317a01c5..095c4bccf 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -95,6 +95,9 @@ Statuses: **Proposed** (under discussion), **Accepted** (approved and/or impleme | [ADR-348](ADR-348-independent-rust-multivariate-forecasting.md) | Independent Rust multivariate forecasting for RuView | Proposed | | [ADR-349](ADR-349-governed-local-and-fal-forecast-training.md) | Governed local and fal.ai forecast training | Proposed | | [ADR-350](ADR-350-ruvector-predictive-memory-and-ruvllm-boundary.md) | RuVector predictive memory and RuVLLM authority boundary | Proposed | +| [ADR-354](ADR-354-forecast-governance-enforcement-in-ruforecast-submodule.md) | fal.ai forecast governance and spend enforcement now real, in the vendored RuForecast submodule | Accepted | +| [ADR-355](ADR-355-local-empty-room-bootstrap-prior.md) | Local empty room bootstrap prior | Accepted | +| [ADR-356](ADR-356-rate-truthful-csi-and-reliability-gated-adaptation.md) | Rate truthful CSI and reliability gated room adaptation | Accepted | ### Platform and UI diff --git a/docs/benchmarks/ruforecast.md b/docs/benchmarks/ruforecast.md index a8393c53e..f032ac7d6 100644 --- a/docs/benchmarks/ruforecast.md +++ b/docs/benchmarks/ruforecast.md @@ -378,3 +378,157 @@ Reproducer: `v2/crates/ruforecast/crates/ruforecast-train/examples/bidmc_prepare one 76-row (context 64 + horizon 12) window per eligible patient, and writes governed `train.jsonl`/`test.jsonl`/`train-local.toml` per judge split. Raw data cached at `/tmp/bidmc-raw/` on the training host only. + +### Zero-shot pretrained-checkpoint comparison (2026-09-02) + +Every RuForecast result above this line is a **from-scratch-trained** model +(RuForecast's own Burn/Rust architecture, trained on a tiny real or synthetic +corpus) evaluated against trivial baselines. All of them lost. A literature +pass after the BIDMC result above (arXiv:2505.11349, "context parroting"; +arXiv:2607.20027, zero-shot HRV forecasting with TimesFM/Chronos/MOIRAI on +wearables) converged on an explanation: published foundation-model wins over +naive baselines on short-horizon, smooth physiological signals come from +**massive real pretraining corpora** (Chronos: ~10M real+augmented series; +TimesFM: ~100B real time points), not from architecture alone — and +RuForecast's from-scratch training used a real corpus many orders of +magnitude smaller (tens to low hundreds of windows), so re-implementing +these architectures from scratch and training small was never going to +replicate their wins. + +This test checks the other half of that hypothesis directly: does a **real, +public, pretrained checkpoint**, run **zero-shot** (no fine-tuning, no +training of any kind on this repo's data) on the **exact same BIDMC +cross-entity test windows and metrics** as the BIDMC result above, actually +beat the naive baselines? If the massive-pretraining-corpus explanation is +right, it should — unlike every from-scratch attempt this session. + +**Setup (kept identical to the BIDMC test above for a fair comparison):** +same `/tmp/bidmc-raw/` cache (53 real ICU patients, PhysioNet BIDMC, Open +Data Commons Attribution License v1.0), same window construction (context +64s + horizon 12s, 1 Hz, variates HR/RESP/SpO2, one window per patient from +that patient's first 76 rows), same two disjoint patient-partition judges (A: +train 1-37/test 38-53 contiguous; B: train odd/test even interleaved — train +patients are irrelevant here since nothing is trained, only the *test* +partition is used), same quantile set +`[0.05,0.10,0.25,0.50,0.75,0.90,0.95]`, same seasonal period (12), and the +same WQL formula (`2*sum(pinball)/(numQuantiles*sum|actual|)`) and MAE +definition as `ruforecast-core::metrics.rs`. Baselines were independently +re-derived from `ruforecast-core::baseline.rs`'s exact logic in Python and +cross-checked against the BIDMC section above: reproduced last-value WQL was +0.01171 (A) / 0.01013 (B) versus the Rust CLI's originally reported +0.01159 / 0.01002 — a ~1% gap attributable to f32 (Rust) vs f64 (Python) +accumulation, not a methodology difference, so this reproduction is treated +as validated. + +Four real, public, pretrained-checkpoint models were run zero-shot on GPU +(RTX 5080, `ruvultra`), per-variate univariate (none of these checkpoints has +a native multivariate mode for these two families, so HR/RESP/SpO2 were +forecast independently and recombined into the same window-level WQL/MAE as +the baselines and the from-scratch RuForecast model): + +- **Chronos-Bolt base/small** (`amazon/chronos-bolt-base`, + `amazon/chronos-bolt-small`) — direct quantile-regression heads, + deterministic given input (no sampling). +- **Chronos (T5) small** (`amazon/chronos-t5-small`) — the original + sampling-based Chronos family; `predict_quantiles` internally draws + samples (default `num_samples`), so this one model's numbers below carry + run-to-run sampling variance the other three don't. +- **TimesFM 2.5 (200M, PyTorch)** (`google/timesfm-2.5-200m-pytorch`) — + quantile output uses 10 channels; per the installed package's source + (`timesfm_2p5_base.py`, `decode_index: int = 5`) and empirical + verification across multiple windows/variates, channel 5 is the point + forecast and channels 1-9 are the native quantile levels [0.1..0.9] in + order (channel 0 is an unused duplicate, not a real quantile). Levels 0.05 + and 0.95 were clamped to the nearest native level (0.10/0.90); 0.25 and + 0.75 were linearly interpolated between the adjacent native levels — the + same class of approximation Chronos-Bolt's own pipeline applies + internally when asked for quantile levels outside its trained range (a + warning it printed for 0.05/0.95 on every call). + +| Judge | Model | Test windows | MAE | WQL | WQL vs last-value | +|---|---|---:|---:|---:|---:| +| A | last-value (baseline) | 16 | 0.7882 | 0.01171 | — | +| A | seasonal-naive (baseline) | 16 | 1.0885 | 0.01617 | worse | +| A | Chronos-Bolt-base (zero-shot) | 16 | 0.7802 | 0.00756 | **-35.4%** | +| A | Chronos-Bolt-small (zero-shot) | 16 | 0.7212 | 0.00684 | **-41.6%** | +| A | Chronos-T5-small (zero-shot) | 16 | 0.9487 | 0.00955 | **-18.4%** | +| A | TimesFM-2.5-200M (zero-shot) | 16 | 0.7490 | 0.00731 | **-37.6%** | +| B | last-value (baseline) | 26 | 0.6944 | 0.01013 | — | +| B | seasonal-naive (baseline) | 26 | 0.8697 | 0.01269 | worse | +| B | Chronos-Bolt-base (zero-shot) | 26 | 0.7096 | 0.00719 | **-29.0%** | +| B | Chronos-Bolt-small (zero-shot) | 26 | 0.6852 | 0.00676 | **-33.3%** | +| B | Chronos-T5-small (zero-shot) | 26 | 0.8254 | 0.00886 | **-12.5%** | +| B | TimesFM-2.5-200M (zero-shot) | 26 | 0.6511 | 0.00698 | **-31.1%** | + +**Honest result: this is the first test all session where a model beats the +trivial baselines, and it does so decisively and consistently** — all four +pretrained checkpoints beat both last-value and seasonal-naive on WQL, on +both independently-partitioned judges, by double-digit-to-40% margins. This +is a materially larger and more consistent margin than the closest published +direct analog found in the literature pass (zero-shot HRV forecasting, +arXiv:2607.20027: Chronos beat EWMA by ~3%, MOIRAI showed no significant +advantage over a mean baseline, p=0.953) — plausibly because BIDMC's 1 Hz +ICU HR/RESP/SpO2 signals are smoother and more autocorrelated than the HRV +series in that paper, which is itself a fair, disclosed difference between +the two comparisons rather than a claim of a stronger general result. + +On MAE (point accuracy) the picture is more mixed: Chronos-Bolt-small and +TimesFM beat last-value's MAE on both judges, but Chronos-Bolt-base and +Chronos-T5-small do not consistently (e.g. Chronos-Bolt-base's MAE is +essentially tied with last-value on judge A, worse on judge B, despite a +much better WQL there) — the pretrained models' advantage is concentrated in +calibrated quantile spread (what WQL scores), not in shifting the central +point forecast much past what last-value already gets from strong +autocorrelation in these signals. + +**This directly confirms the massive-pretraining-corpus hypothesis from the +literature pass**: the same short-horizon, smooth physiological-signal task +on which every from-scratch RuForecast configuration this session lost to +naive baselines is won, cleanly, by checkpoints that carry a pretraining +corpus many orders of magnitude larger than any real dataset available to +this project. This is evidence *for* zero-shot/foundation-model inference +(or later, fine-tuning a pretrained checkpoint) as the credible next lever +for RuForecast-style short-horizon vitals forecasting — not for continuing +to scale from-scratch training on this project's own small real corpora, +which the BIDMC and household tests above already showed does not close +this gap even with genuine, cross-entity real data. + +**Scope and caveats, stated plainly:** +- This is zero-shot inference only — no fine-tuning was attempted on any + checkpoint. A fine-tuned comparison (e.g. LoRA-adapting Chronos-Bolt on + RuForecast's own training windows) is a natural, not-yet-run follow-up. +- Univariate-per-variate forecasting was used for all four models because + neither Chronos nor TimesFM in these releases has a native multivariate + mode matching RuForecast's joint HR/RESP/SpO2 modeling; a true + multivariate foundation model (e.g. MOIRAI, which does support cross-variate + attention) was not tested here and is a natural next step. +- Chronos-T5-small's `predict_quantiles` samples internally (unlike + Chronos-Bolt's direct quantile heads or TimesFM's quantile head); its + numbers above are one run and carry unquantified sampling variance the + other three models' numbers don't. +- No inference-cost/latency comparison against RuForecast's own (much + smaller, ~tens-of-KB) model is included; all four pretrained checkpoints + are orders of magnitude larger (small-to-200M+ parameters) and ran on a + discrete GPU (RTX 5080), not the ESP32-class edge target RuForecast's own + architecture is designed for. A model that wins on WQL here is not + automatically deployable to the same edge budget RuForecast targets — that + tradeoff is unresolved by this test and is itself a reason zero-shot + inference (call out to a hosted/cloud checkpoint) and from-scratch + small-model training remain different tools for different deployment + constraints, not a strict replacement of one by the other. + +Reproducer: `scripts/ruforecast-zeroshot/bidmc_windows.py` (window builder, +baselines, and metrics — ported line-for-line from +`ruforecast-train/examples/bidmc_prepare.rs` and +`ruforecast-core/src/{baseline,metrics}.rs`, verified against this document's +own BIDMC last-value/seasonal-naive numbers) and +`scripts/ruforecast-zeroshot/run_zeroshot_eval.py` (zero-shot inference and +scoring), both in this worktree (`research/ruforecast-zeroshot-eval`). Run +against `/tmp/bidmc-raw/` (downloaded fresh from PhysioNet if not already +cached) with `python run_zeroshot_eval.py --models chronos-bolt-base +chronos-bolt-small chronos-t5-small timesfm-2.5-200m --judges a b` inside a +venv with `chronos-forecasting`, `timesfm`, and a CUDA-enabled `torch` +installed (`amazon/chronos-*` and `google/timesfm-2.5-200m-pytorch` download +automatically from HuggingFace Hub on first run; no HF token was required). +Model weights and the raw BIDMC CSVs are not committed — `/tmp/bidmc-raw/` +and the HuggingFace cache live only on the host that ran this evaluation. diff --git a/docs/research/wiflow-reproduction-protocol.md b/docs/research/wiflow-reproduction-protocol.md new file mode 100644 index 000000000..5bd807717 --- /dev/null +++ b/docs/research/wiflow-reproduction-protocol.md @@ -0,0 +1,63 @@ +# WiFlow Reproduction Protocol + +Status: Proposed research protocol + +Tracks issue #1786. + +## Objective + +Determine whether a CSI optical flow style representation improves reusable camera free motion intelligence in RuView rather than only improving a proxy flow metric. + +## Required capture metadata + +Every run records hardware family, firmware, antenna count and geometry, channel width, center frequency, packet rate, timestamp source, synchronization method, room identity, participant identity class, label source, calibration, packet loss, missing subcarriers, preprocessing version, model commit, accelerator, software versions, power mode, and random seeds. + +## Baselines + +1. current RuView CSI preprocessing plus strongest task specific sensing head +2. WiFlow style motion field representation +3. simpler temporal derivative or phase and amplitude motion baseline + +All conditions use identical train, validation, and held out environment splits. + +## Metrics + +Primary intermediate metrics: + +* endpoint error or equivalent flow error +* directional consistency +* temporal stability +* uncertainty calibration + +Required downstream metrics: + +* tracking quality +* occupancy or presence quality +* pose or activity metric where available +* spatial state consistency in RuField or WorldGraph + +Systems metrics: + +* p50 and p95 inference latency +* peak memory +* CPU, GPU, or accelerator utilization +* energy per inference where measurable +* bytes per observation and sustained bandwidth + +## Stress tests + +Test unseen rooms, unseen participants, changed antenna geometry, a second RF hardware family, packet loss, burst loss, missing subcarriers, lower packet rate, clock jitter, and partial calibration failure. + +## Promotion gate + +The representation graduates only when the same learned representation improves at least two downstream RuView tasks on held out environments, or reaches equivalent downstream quality with materially lower edge cost. + +A better optical flow proxy score alone is insufficient. + +## Security and privacy + +Visual or LiDAR labels may be used as privileged training evidence but must not become a runtime dependency for camera free deployment claims. Dataset provenance and consent requirements remain explicit. + +## Rollback + +The experiment is isolated behind a representation interface. Existing RuView sensing heads remain the production fallback. diff --git a/firmware/esp32-csi-node/README.md b/firmware/esp32-csi-node/README.md index ebdddc2d0..2469814f7 100644 --- a/firmware/esp32-csi-node/README.md +++ b/firmware/esp32-csi-node/README.md @@ -257,10 +257,17 @@ Offset Size Field 12 4 Sequence number (LE u32) 16 1 RSSI (i8) 17 1 Noise floor (i8) -18 2 Reserved +18 1 PPDU type (ADR-110; zero when tagging is disabled) +19 1 Flags: bit0=40 MHz, bit2=STBC, bit4=sync valid, + bit5=invalid first CSI word zeroed by firmware 20 N*2 I/Q pairs (n_antennas * n_subcarriers * 2 bytes) ``` +When ESP-IDF marks `first_word_invalid`, the firmware preserves packet geometry, +zeros the first four CSI bytes, and sets byte 19 bit 5. The same sanitized bytes +feed the edge DSP path. This prevents a documented hardware artifact from being +learned as motion while keeping older readers wire compatible. + ### Vitals Packet (32 bytes) ``` diff --git a/firmware/esp32-csi-node/main/CMakeLists.txt b/firmware/esp32-csi-node/main/CMakeLists.txt index 0cabf5df2..dc527813a 100644 --- a/firmware/esp32-csi-node/main/CMakeLists.txt +++ b/firmware/esp32-csi-node/main/CMakeLists.txt @@ -1,5 +1,6 @@ set(SRCS - "main.c" "csi_collector.c" "stream_sender.c" "nvs_config.c" + "main.c" "csi_collector.c" + "thermal.c" "stream_sender.c" "nvs_config.c" "edge_processing.c" "ota_update.c" "power_mgmt.c" "wasm_runtime.c" "wasm_upload.c" "rvf_parser.c" "mmwave_sensor.c" diff --git a/firmware/esp32-csi-node/main/Kconfig.projbuild b/firmware/esp32-csi-node/main/Kconfig.projbuild index aa3ccaaae..b7f7b5a8d 100644 --- a/firmware/esp32-csi-node/main/Kconfig.projbuild +++ b/firmware/esp32-csi-node/main/Kconfig.projbuild @@ -518,6 +518,140 @@ menu "Onboard LED (ADR-183)" (reaches yellow with less motion). endmenu + +menu "Thermal monitoring" + +config THERMAL_MONITOR + bool "Read the on-chip temperature sensor and throttle the radio" + depends on IDF_TARGET_ESP32C6 + default y + help + The C6 has a temperature sensor this firmware otherwise never reads. + Harmless on a bare bench board; not once a node is sealed in a printed + enclosure on a wall with the radio transmitting continuously. + + What is being protected is the ENCLOSURE, not the silicon. The part is + rated far past anything it sees here, but PLA softens at its glass + transition (~55-60 C) and sags slowly under its own weight, which never + announces itself. + +config THERMAL_THROTTLE + bool "Act on the temperature by reducing transmit power" + depends on THERMAL_MONITOR + default n + help + Separate from monitoring on purpose. Reading the sensor is inert; + reducing transmit power moves every RSSI the node has, and doing that + mid-experiment gives any surprise two candidate causes. + + Leave off until the fleet is stable and the logs show a node genuinely + running hot. If none ever does -- which is the likely outcome -- this + never needs enabling, and the thresholds below cost nothing. + +config THERMAL_WARN_C + int "Warn above this die temperature (C)" + depends on THERMAL_MONITOR + range 40 100 + default 65 + +config THERMAL_THROTTLE_C + int "Reduce transmit power above this die temperature (C)" + depends on THERMAL_MONITOR + range 45 105 + default 72 + help + Die temperature, not enclosure temperature -- the sensor is on the chip + and runs hotter than the plastic around it. ~72 C on the die in a small + vented box is roughly a 50-55 C enclosure, which is where PLA begins to + matter. Setting this at 55 would throttle a perfectly healthy node. + + PETG (glass transition ~80 C) removes the concern; raise this if the + whole fleet is printed in it. + +config THERMAL_CRITICAL_C + int "Minimum transmit power above this die temperature (C)" + depends on THERMAL_MONITOR + range 50 110 + default 80 + +config THERMAL_HYSTERESIS_C + int "Degrees of cooling required before stepping down a thermal state" + depends on THERMAL_MONITOR + range 1 20 + default 8 + help + Recovering at the same threshold that tripped would oscillate, and every + oscillation is a visible RSSI step on every link the node has. + + Applies to EVERY downward transition, each relative to the threshold that + was crossed on the way up: leaving CRITICAL needs CRITICAL_C minus this, + leaving THROTTLED needs THROTTLE_C minus this, leaving WARN needs WARN_C + minus this. Rising transitions are immediate at the plain thresholds -- + heat is a reason to act now. + +endmenu + +menu "Network supervision" + +config UPLINK_WATCHDOG + bool "Restart the node if the CSI uplink stops for too long" + default y + help + Bounds how long a node can sit powered, capturing, and unable to reach + the network. + + MEASURED 2026-08-30: an AP-side event at 03:37 EDT disconnected all three + wall-mounted nodes within three minutes of each other, and every one of + them stayed dead for 5.5-7.5 hours until it was physically unplugged. The + specific cause of that outage (a reconnect counter that gave up + permanently) is fixed in main.c, but this exists so the NEXT unknown + wedge costs minutes instead of a night of data. + + The watchdog counts a successful sendto(), which on UDP means the network + stack accepted the datagram -- not that the aggregator received it. That + is deliberate: stopping the server does NOT reboot the fleet, because + sendto keeps succeeding into a socket whose peer is gone. It fires only + when the node's own network path is broken. + + It arms on the first successful send rather than at boot, so a node + powered on while the AP is down does not reboot-loop; the unbounded + reconnect handles that case instead. + +config UPLINK_WATCHDOG_TIMEOUT_S + int "Restart after this many seconds with no successful send" + depends on UPLINK_WATCHDOG + range 30 3600 + default 1200 + help + Must sit above the worst SELF-HEALING gap, or the watchdog reboots nodes + that were about to recover on their own -- which is worse than the fault + it guards against, because every reboot re-runs ESP-NOW leader election + and discards the mesh time-sync state. + + This was 120 s on the reasoning that the ENOMEM backoff caps at 2 s so a + healthy node is never quiet for long. A 58-minute soak on 2026-08-30 + falsified that: nodes 0 and 2 each went silent repeatedly with a median + gap of 210 s and a maximum of 225 s, and recovered every time without + intervention. 120 s would have rebooted them every few minutes forever. + (The cause of those 210 s gaps is still unknown -- it is not the ENOMEM + backoff, which cannot produce a gap that long.) + + Sized from the completed 112-minute soak, not the partial one: 30 + silences across two nodes, median 225 s, p90 225 s, and a single 510 s + outlier that looks like two cycles running together. A third such merge + would reach ~735 s, so 600 s was too tight -- it left only 90 s of margin + over an event already observed once. + + 1200 s is 2.35x the observed maximum while still bounding a true wedge to + twenty minutes rather than the 7.5 hours measured the same day. Revisit + it if the ~225 s flapping is ever explained and fixed; until then do not + lower it below ~900 s. + + If nodes do restart unexpectedly, check the reset reason in the sync + packet's health field before assuming the watchdog is responsible. + +endmenu + config CSI_GATE_MESH_ALIGNED bool "Align the CSI rate gate to mesh time (enables cross-node pairing)" default y diff --git a/firmware/esp32-csi-node/main/adaptive_controller.c b/firmware/esp32-csi-node/main/adaptive_controller.c index fe4bf9d5b..7c80cf83e 100644 --- a/firmware/esp32-csi-node/main/adaptive_controller.c +++ b/firmware/esp32-csi-node/main/adaptive_controller.c @@ -13,6 +13,7 @@ */ #include "adaptive_controller.h" +#include "thermal.h" #include "rv_radio_ops.h" #include "rv_feature_state.h" #include "rv_mesh.h" @@ -331,6 +332,10 @@ static void emit_feature_state(void) static void slow_loop_cb(TimerHandle_t t) { (void)t; + /* Sample the die and apply the thermal policy. The slow loop (30 s) is the + * right cadence: the die does not change quickly, and reading it more + * often buys nothing but noise. No-op unless CONFIG_THERMAL_MONITOR. */ + thermal_tick(); /* ADR-081 L3: publish a HEALTH mesh message every slow tick * (default 30 s). The coordinator uses these to track liveness and * detect sync-error drift. */ diff --git a/firmware/esp32-csi-node/main/csi_collector.c b/firmware/esp32-csi-node/main/csi_collector.c index 52b3a9002..57c5b49f8 100644 --- a/firmware/esp32-csi-node/main/csi_collector.c +++ b/firmware/esp32-csi-node/main/csi_collector.c @@ -266,8 +266,16 @@ size_t csi_serialize_frame(const wifi_csi_info_t *info, uint8_t *buf, size_t buf buf[19] = 0; #endif - /* I/Q data */ + /* I/Q data. ESP-IDF documents that the first four CSI bytes are invalid + * when first_word_invalid is set. Never let those hardware artifacts feed + * either the host or edge DSP path. Preserve the fixed ADR-018 geometry by + * zeroing rather than removing bytes, and mark the sanitation in bit 5. */ memcpy(&buf[CSI_HEADER_SIZE], info->buf, iq_len); + if (info->first_word_invalid && iq_len > 0) { + size_t invalid_len = iq_len < 4 ? iq_len : 4; + memset(&buf[CSI_HEADER_SIZE], 0, invalid_len); + buf[19] |= CSI_FLAG_FIRST_WORD_SANITIZED; + } return frame_size; } @@ -377,13 +385,16 @@ static void wifi_csi_callback(void *ctx, wifi_csi_info_t *info) * while the on-device Tier 1/2 pipeline receives a uniform, sustainable * stream. Enqueuing every burst frame overloaded the unicore C6 DSP and * turned 30-40 callback pps into an irregular approximately 8 Hz subset. */ - if (info->buf && info->len > 0) { + if (frame_len > CSI_HEADER_SIZE) { if (s_next_edge_enqueue_us == 0) { s_next_edge_enqueue_us = now_us; } if (now_us >= s_next_edge_enqueue_us) { - (void)edge_enqueue_csi((const uint8_t *)info->buf, (uint16_t)info->len, + /* Reuse the sanitized ADR-018 payload. Feeding info->buf here + * would reintroduce first_word_invalid artifacts on device. */ + (void)edge_enqueue_csi(&frame_buf[CSI_HEADER_SIZE], + (uint16_t)(frame_len - CSI_HEADER_SIZE), (int8_t)info->rx_ctrl.rssi, info->rx_ctrl.channel); /* Preserve the configured sample clock instead of resetting it to diff --git a/firmware/esp32-csi-node/main/csi_collector.h b/firmware/esp32-csi-node/main/csi_collector.h index 92f431053..cce99393c 100644 --- a/firmware/esp32-csi-node/main/csi_collector.h +++ b/firmware/esp32-csi-node/main/csi_collector.h @@ -17,6 +17,9 @@ /** ADR-018 header size in bytes. */ #define CSI_HEADER_SIZE 20 +/** ADR-018 byte 19: ESP-IDF reported and firmware sanitized invalid CSI prefix. */ +#define CSI_FLAG_FIRST_WORD_SANITIZED (1U << 5) + /** Maximum frame buffer size (header + 4 antennas * 256 subcarriers * 2 bytes). */ #define CSI_MAX_FRAME_SIZE (CSI_HEADER_SIZE + 4 * 256 * 2) diff --git a/firmware/esp32-csi-node/main/edge_processing.h b/firmware/esp32-csi-node/main/edge_processing.h index cb53ad66c..0fe280b0a 100644 --- a/firmware/esp32-csi-node/main/edge_processing.h +++ b/firmware/esp32-csi-node/main/edge_processing.h @@ -33,7 +33,31 @@ #define EDGE_MAX_IQ_BYTES 1024 /**< Max I/Q payload per slot. */ #define EDGE_PHASE_HISTORY_LEN 256 /**< Phase history buffer depth. */ #define EDGE_TOP_K 8 /**< Top-K subcarriers to track. */ -#define EDGE_MAX_SUBCARRIERS 128 /**< Max subcarriers per frame. */ +/** + * Max subcarriers per frame the edge pipeline will process. + * + * Target-conditional since 2026-08-28. The original 128 was an ESP32-S3 + * assumption: pre-HE chips report at most 128 CSI bins. ADR-110 onboarded the + * ESP32-C6 to the *capture* path but not to this one, and a C6 associated to an + * HE-capable AP delivers HE20 frames with 256 bins (iq_len = 512 bytes). + * + * `process_frame()` guards with `n_subcarriers > EDGE_MAX_SUBCARRIERS -> return`, + * so on C6 every frame was rejected and the whole edge pipeline — vitals, + * presence, fall detection, per-slot counting — silently did nothing. The Edge + * DSP task started, logged its banner, and never processed a frame. Confirmed + * on hardware: no edge_proc log past init and no vitals packet ever reaching + * the sink. + * + * HE-capable parts get 256; pre-HE parts keep 128 so they don't pay ~3.5 KB of + * .bss they can never use. CONFIG_SOC_WIFI_HE_SUPPORT is the same switch + * csi_collector.c uses to pick its rx_ctrl layout. + */ +#if CONFIG_SOC_WIFI_HE_SUPPORT +#define EDGE_MAX_SUBCARRIERS 256 /**< HE20 carries 256 CSI bins (C6/C5). */ +#else +#define EDGE_MAX_SUBCARRIERS 128 /**< Pre-HE parts (S3 etc). */ +#endif + /* ---- Measured sample-rate tracking ---- * diff --git a/firmware/esp32-csi-node/main/main.c b/firmware/esp32-csi-node/main/main.c index 6991efe68..660899708 100644 --- a/firmware/esp32-csi-node/main/main.c +++ b/firmware/esp32-csi-node/main/main.c @@ -21,6 +21,7 @@ #include "led_strip.h" #include "csi_collector.h" +#include "thermal.h" #include "stream_sender.h" #include "nvs_config.h" #include "edge_processing.h" @@ -59,7 +60,52 @@ nvs_config_t g_nvs_config; static EventGroupHandle_t s_wifi_event_group; static int s_retry_num = 0; -#define MAX_RETRY 10 + +/* WiFi reconnection. + * + * This used to be `#define MAX_RETRY 10` with the disconnect handler giving up + * permanently once s_retry_num reached it: it set WIFI_FAIL_BIT and never + * called esp_wifi_connect() again. s_retry_num is only cleared on a successful + * IP_EVENT_STA_GOT_IP, so it accumulated over the node's entire uptime -- ten + * disconnects spread across days left the node permanently off the network + * with nothing but a power cycle to clear it. + * + * MEASURED 2026-08-30: a single AP-side event at 03:37:03 EDT disconnected all + * three wall-mounted nodes within three minutes of each other. Every one of + * them latched off and stayed off for 5.5-7.5 hours, until it was physically + * unplugged. CSI capture kept running throughout (it is driver-level and needs + * no IP), so the boards looked alive on the bench and dead to the server. + * + * Retry is now unbounded with exponential backoff. Two things that were + * conflated are now separate: WIFI_BOOT_WAIT_ATTEMPTS only releases app_main + * from its startup wait so the rest of the node can boot; it does not stop the + * node trying to reconnect. Nothing stops the node trying to reconnect. */ +#define WIFI_BOOT_WAIT_ATTEMPTS 10 +#define WIFI_RETRY_BASE_MS 500 +#define WIFI_RETRY_MAX_MS 30000 + +static esp_timer_handle_t s_reconnect_timer; + +/* 500 ms doubling to a 30 s ceiling. Backoff matters because the failure this + * guards against is an AP that is down or rebooting: hammering + * esp_wifi_connect() at full rate for the minutes an AP takes to come back + * wastes power and floods the log without reconnecting any sooner. */ +static uint32_t wifi_retry_delay_ms(int attempt) +{ + int shift = attempt - 1; + if (shift < 0) shift = 0; + if (shift > 6) shift = 6; + uint32_t d = (uint32_t)WIFI_RETRY_BASE_MS << shift; + return d > WIFI_RETRY_MAX_MS ? WIFI_RETRY_MAX_MS : d; +} + +/* Runs on the esp_timer task, not the event loop task -- esp_wifi_connect() + * must not be reached through a delay inside the event handler itself. */ +static void wifi_reconnect_cb(void *arg) +{ + (void)arg; + esp_wifi_connect(); +} static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) @@ -69,13 +115,22 @@ static void event_handler(void *arg, esp_event_base_t event_base, } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { wifi_event_sta_disconnected_t *disc = (wifi_event_sta_disconnected_t *)event_data; ESP_LOGW(TAG, "WiFi disconnected, reason=%d rssi=%d", disc->reason, disc->rssi); - if (s_retry_num < MAX_RETRY) { - esp_wifi_connect(); - s_retry_num++; - ESP_LOGI(TAG, "Retrying WiFi connection (%d/%d)", s_retry_num, MAX_RETRY); - } else { + s_retry_num++; + /* Release the boot wait once, so a node that comes up while the AP is + * down still starts CSI capture and the mesh instead of blocking in + * app_main. Retrying continues regardless. */ + if (s_retry_num == WIFI_BOOT_WAIT_ATTEMPTS) { xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); } + uint32_t delay_ms = wifi_retry_delay_ms(s_retry_num); + if (s_reconnect_timer != NULL) { + esp_timer_stop(s_reconnect_timer); /* may not be running; harmless */ + esp_timer_start_once(s_reconnect_timer, (uint64_t)delay_ms * 1000); + } else { + esp_wifi_connect(); /* timer unavailable: retry now */ + } + ESP_LOGI(TAG, "Reconnecting in %lu ms (attempt %d)", + (unsigned long)delay_ms, s_retry_num); } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; ESP_LOGI(TAG, "Got IP: " IPSTR, IP2STR(&event->ip_info.ip)); @@ -84,6 +139,41 @@ static void event_handler(void *arg, esp_event_base_t event_base, } } +#ifdef CONFIG_UPLINK_WATCHDOG +/* Uplink supervision. + * + * The reconnect fix above addresses the one wedge whose mechanism we have + * proven. This catches the ones we have not: any state in which the node is + * powered, capturing, and unable to put a packet on the wire. It bounds such a + * state to CONFIG_UPLINK_WATCHDOG_TIMEOUT_S instead of the 7.5 hours measured + * on 2026-08-30. + * + * The signal is a successful sendto(), which on UDP means the stack accepted + * the datagram -- NOT that the server received it. That distinction is what + * makes this safe: stopping the aggregator does not reboot the fleet, because + * sendto keeps succeeding into a socket whose peer is gone. It fires only when + * the node's own network path is broken. + * + * It arms on the first successful send rather than at boot, so a node that has + * never reached the network does not reboot-loop while the AP is down; the + * unbounded reconnect above owns that case. */ +static void uplink_watchdog_tick(void) +{ + int64_t last = stream_sender_last_success_us(); + if (last == 0) { + return; /* never delivered -- not armed yet */ + } + int64_t idle_s = (esp_timer_get_time() - last) / 1000000; + if (idle_s < CONFIG_UPLINK_WATCHDOG_TIMEOUT_S) { + return; + } + ESP_LOGE(TAG, "uplink watchdog: no successful send for %lld s " + "(limit %d s) -- restarting", + (long long)idle_s, CONFIG_UPLINK_WATCHDOG_TIMEOUT_S); + esp_restart(); +} +#endif /* CONFIG_UPLINK_WATCHDOG */ + static void wifi_init_sta(void) { s_wifi_event_group = xEventGroupCreate(); @@ -135,6 +225,17 @@ static void wifi_init_sta(void) } #endif + const esp_timer_create_args_t reconnect_args = { + .callback = &wifi_reconnect_cb, + .name = "wifi_reconnect", + }; + esp_err_t rc_ret = esp_timer_create(&reconnect_args, &s_reconnect_timer); + if (rc_ret != ESP_OK) { + ESP_LOGW(TAG, "reconnect timer create failed: %s (falling back to " + "immediate retry)", esp_err_to_name(rc_ret)); + s_reconnect_timer = NULL; + } + ESP_ERROR_CHECK(esp_wifi_start()); ESP_LOGI(TAG, "WiFi STA initialized, connecting to SSID: %s", g_nvs_config.wifi_ssid); @@ -147,7 +248,9 @@ static void wifi_init_sta(void) if (bits & WIFI_CONNECTED_BIT) { ESP_LOGI(TAG, "Connected to WiFi"); } else if (bits & WIFI_FAIL_BIT) { - ESP_LOGE(TAG, "Failed to connect to WiFi after %d retries", MAX_RETRY); + ESP_LOGW(TAG, "Not connected after %d attempts -- continuing boot; " + "reconnection keeps retrying in the background", + WIFI_BOOT_WAIT_ATTEMPTS); } } @@ -318,6 +421,36 @@ void app_main(void) ESP_LOGI(TAG, "Mock CSI active (scenario=%d)", CONFIG_CSI_MOCK_SCENARIO); } #else + /* Why did we just start? Nothing recorded this before, and it is the + * single most useful line in a fleet log: a node that reboots tells you + * almost nothing, but a node that reboots with ESP_RST_BROWNOUT tells you + * its supply sagged, which on shared outlets is a wiring problem rather + * than a firmware one. Three nodes dropping together on 2026-08-28 would + * have been answered by this line. */ + { + esp_reset_reason_t rr = esp_reset_reason(); + const char *why = + rr == ESP_RST_POWERON ? "power-on" : + rr == ESP_RST_EXT ? "external reset" : + rr == ESP_RST_SW ? "software restart" : + rr == ESP_RST_PANIC ? "PANIC (crash)" : + rr == ESP_RST_INT_WDT ? "interrupt watchdog" : + rr == ESP_RST_TASK_WDT ? "task watchdog" : + rr == ESP_RST_WDT ? "other watchdog" : + rr == ESP_RST_BROWNOUT ? "BROWNOUT (supply sagged)" : + rr == ESP_RST_DEEPSLEEP? "deep-sleep wake" : "unknown"; + if (rr == ESP_RST_PANIC || rr == ESP_RST_BROWNOUT || + rr == ESP_RST_INT_WDT || rr == ESP_RST_TASK_WDT) { + ESP_LOGW(TAG, "reset reason: %s (%d) <-- not a clean start", why, (int)rr); + } else { + ESP_LOGI(TAG, "reset reason: %s (%d)", why, (int)rr); + } + } + + /* Bring thermal monitoring up before the radio is loaded, so the first + * reading is taken against a known-idle baseline rather than mid-burst. */ + thermal_init(); + csi_collector_init(); /* ADR-073: Start multi-frequency channel hopping if configured in NVS. */ @@ -510,8 +643,11 @@ void app_main(void) (swarm_ret == ESP_OK) ? g_nvs_config.seed_url : "off", (adapt_ret == ESP_OK) ? "on" : "off"); - /* Main loop — keep alive */ + /* Main loop — keep alive, and supervise the uplink. */ while (1) { vTaskDelay(pdMS_TO_TICKS(10000)); +#ifdef CONFIG_UPLINK_WATCHDOG + uplink_watchdog_tick(); +#endif } } diff --git a/firmware/esp32-csi-node/main/stream_sender.c b/firmware/esp32-csi-node/main/stream_sender.c index ecfc6ec9f..b766aff52 100644 --- a/firmware/esp32-csi-node/main/stream_sender.c +++ b/firmware/esp32-csi-node/main/stream_sender.c @@ -37,6 +37,12 @@ static uint32_t s_enomem_suppressed = 0; * succeeds. */ static uint32_t s_enomem_streak = 0; +/* Timestamp of the last sendto() the stack accepted, on either path; 0 until + * the node has ever delivered anything. Read by the uplink watchdog in main.c + * -- see the rationale there for why "accepted by the stack" is deliberately + * not "received by the server". */ +static int64_t s_last_success_us = 0; + static int sender_init_internal(const char *ip, uint16_t port) { s_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); @@ -118,6 +124,7 @@ int stream_sender_send(const uint8_t *data, size_t len) /* A send got through — buffer pressure cleared; reset the backoff streak. */ s_enomem_streak = 0; + s_last_success_us = esp_timer_get_time(); return sent; } @@ -146,9 +153,18 @@ int stream_sender_send_priority(const uint8_t *data, size_t len) } return -1; } + /* Counts for the watchdog: the mesh sync packet rides this path at ~1 Hz, + * and its getting through proves the node's network path is intact even + * when the bulk CSI stream is being suppressed by the ENOMEM backoff. */ + s_last_success_us = esp_timer_get_time(); return sent; } +int64_t stream_sender_last_success_us(void) +{ + return s_last_success_us; +} + void stream_sender_deinit(void) { if (s_sock >= 0) { diff --git a/firmware/esp32-csi-node/main/stream_sender.h b/firmware/esp32-csi-node/main/stream_sender.h index 198bbee4e..8dedca431 100644 --- a/firmware/esp32-csi-node/main/stream_sender.h +++ b/firmware/esp32-csi-node/main/stream_sender.h @@ -50,6 +50,18 @@ int stream_sender_send(const uint8_t *data, size_t len); */ int stream_sender_send_priority(const uint8_t *data, size_t len); +/** + * esp_timer timestamp (microseconds) of the last sendto() the network stack + * accepted, on either the bulk or the priority path. Returns 0 if the node has + * not successfully sent anything since boot. + * + * This reports that the datagram was accepted for transmission, NOT that the + * aggregator received it -- UDP gives no such signal. Used by the uplink + * watchdog to detect a node that is powered and capturing but cannot reach the + * network. + */ +int64_t stream_sender_last_success_us(void); + /** * Close the UDP sender socket. */ diff --git a/firmware/esp32-csi-node/main/thermal.c b/firmware/esp32-csi-node/main/thermal.c new file mode 100644 index 000000000..9759a7f15 --- /dev/null +++ b/firmware/esp32-csi-node/main/thermal.c @@ -0,0 +1,201 @@ +/* On-chip thermal monitoring and radio throttling. + * + * The ESP32-C6 carries a temperature sensor (SOC_TEMP_SENSOR_SUPPORTED) that + * nothing in this firmware has ever read. That was tolerable on a bare board + * on a bench; it is not once nine nodes are sealed in printed enclosures and + * left on a wall for months with the radio transmitting continuously. + * + * What we are protecting is the enclosure, not the silicon. The part is rated + * well past anything it will see here, but PLA goes soft at its glass + * transition around 55-60 C -- and that is sagging under its own weight over + * weeks, not a dramatic failure, so it would never announce itself. + * + * The sensor reads DIE temperature, which runs hotter than the plastic around + * it. Thresholds are therefore set against the die with that offset in mind: + * a die at ~72 C in a small vented box is roughly a 50-55 C enclosure, which + * is where PLA starts to matter. They are deliberately not set at 55 C -- that + * would throttle a perfectly healthy node. + * + * Throttling reduces WiFi transmit power, which is the dominant heat source + * and degrades gracefully: links weaken but keep working, where cutting the + * capture rate would blind the node entirely. + * + * IMPORTANT for anyone reading link diagnostics: a throttled node's RSSI drops + * at both ends, which looks exactly like an obstruction. `thermal_state()` and + * `thermal_tx_dbm()` exist so that can be distinguished from a cage or a wall + * rather than misdiagnosed as one. + */ + +#include "sdkconfig.h" +#include "thermal.h" + +#if defined(CONFIG_THERMAL_MONITOR) && defined(CONFIG_IDF_TARGET_ESP32C6) + +#include "driver/temperature_sensor.h" +#include "esp_wifi.h" +#include "esp_log.h" +#include "esp_timer.h" +#include "esp_system.h" +#include "esp_heap_caps.h" + +static const char *TAG = "thermal"; + +static temperature_sensor_handle_t s_sensor; +static bool s_ready; +static float s_last_c = -273.0f; +static float s_peak_c = -273.0f; +static thermal_state_t s_state = THERMAL_OK; + +/* esp_wifi_set_max_tx_power() takes 0.25 dBm units. 80 = 20 dBm, the default + * on this part; each step down is 3 dB, which halves radiated power. */ +#define TXP_FULL 80 /* 20 dBm */ +#define TXP_STEP1 68 /* 17 dBm */ +#define TXP_STEP2 56 /* 14 dBm */ +#define TXP_MIN 44 /* 11 dBm */ + +static int8_t s_txp = TXP_FULL; + +#ifdef CONFIG_THERMAL_THROTTLE +/* Only referenced from the throttling block in thermal_tick(). Guarded so a + * monitor-only build (the default, and what is on the wall) does not warn + * about an unused function. */ +static void apply_tx_power(int8_t q) +{ + if (q == s_txp) { + return; + } + esp_err_t r = esp_wifi_set_max_tx_power(q); + if (r == ESP_OK) { + ESP_LOGW(TAG, "tx power %d.%02d dBm -> %d.%02d dBm (thermal)", + s_txp / 4, (s_txp % 4) * 25, q / 4, (q % 4) * 25); + s_txp = q; + } else { + ESP_LOGW(TAG, "esp_wifi_set_max_tx_power(%d) failed: %s", + q, esp_err_to_name(r)); + } +} +#endif /* CONFIG_THERMAL_THROTTLE */ + +esp_err_t thermal_init(void) +{ + /* The requested span must fit ENTIRELY inside one of the sensor's + * predefined measurement ranges -- the driver walks a table and takes the + * first that contains it, so a span crossing two boundaries is rejected + * with ESP_ERR_INVALID_ARG rather than clamped. (-10,110) looks reasonable + * and is invalid for exactly that reason; it failed on real hardware. + * + * (20,100) is the right first choice here: the warn/throttle/critical + * thresholds all sit comfortably inside it, and indoor ambient is + * essentially never below 20 C. The wider fallbacks exist so an unheated + * room, or a different chip revision with a different table, degrades to a + * usable range instead of leaving the node unmonitored. */ + static const struct { int lo, hi; } spans[] = { + {20, 100}, {-10, 80}, {-30, 50}, {50, 125}, + }; + esp_err_t r = ESP_ERR_INVALID_ARG; + for (size_t i = 0; i < sizeof(spans) / sizeof(spans[0]); i++) { + temperature_sensor_config_t cfg = + TEMPERATURE_SENSOR_CONFIG_DEFAULT(spans[i].lo, spans[i].hi); + r = temperature_sensor_install(&cfg, &s_sensor); + if (r == ESP_OK) { + ESP_LOGI(TAG, "sensor range %d..%d C", spans[i].lo, spans[i].hi); + break; + } + } + if (r != ESP_OK) { + ESP_LOGW(TAG, "sensor install failed on every range: %s " + "(continuing unmonitored)", esp_err_to_name(r)); + return r; + } + r = temperature_sensor_enable(s_sensor); + if (r != ESP_OK) { + ESP_LOGW(TAG, "sensor enable failed: %s", esp_err_to_name(r)); + return r; + } + s_ready = true; + ESP_LOGI(TAG, "monitoring on: warn %d C, throttle %d C, critical %d C " + "(die temperature; protecting the enclosure, not the chip)", + CONFIG_THERMAL_WARN_C, CONFIG_THERMAL_THROTTLE_C, + CONFIG_THERMAL_CRITICAL_C); + return ESP_OK; +} + +void thermal_tick(void) +{ + if (!s_ready) { + return; + } + float c; + if (temperature_sensor_get_celsius(s_sensor, &c) != ESP_OK) { + return; + } + s_last_c = c; + if (c > s_peak_c) { + s_peak_c = c; + } + + /* Hysteresis on the way down: recovering at the same threshold that + * tripped would oscillate, and each oscillation is a visible RSSI step on + * every link this node has. The decision itself lives in thermal.h as a + * pure function so a host test can exercise the same code this runs. */ + const thermal_state_t next = thermal_next_state(s_state, c, + CONFIG_THERMAL_WARN_C, + CONFIG_THERMAL_THROTTLE_C, + CONFIG_THERMAL_CRITICAL_C, + CONFIG_THERMAL_HYSTERESIS_C); + + if (next != s_state) { + ESP_LOGW(TAG, "%.1f C: %s -> %s", + c, thermal_state_name(s_state), thermal_state_name(next)); + s_state = next; + } + +#ifdef CONFIG_THERMAL_THROTTLE + /* Acting on the reading is separate from taking it. Measuring is inert; + * changing transmit power mid-experiment moves every RSSI this node has, + * which is not something to introduce during a fleet bring-up when any + * surprise needs a single candidate cause. Turn this on once the fleet is + * stable AND the logs show a node actually gets hot -- if none ever does, + * it never needs turning on at all. */ + switch (s_state) { + case THERMAL_CRITICAL: apply_tx_power(TXP_MIN); break; + case THERMAL_THROTTLED: apply_tx_power(TXP_STEP2); break; + case THERMAL_WARN: apply_tx_power(TXP_STEP1); break; + case THERMAL_OK: apply_tx_power(TXP_FULL); break; + } +#endif + + static int64_t s_last_log_us; + int64_t now = esp_timer_get_time(); + if (now - s_last_log_us > 60LL * 1000 * 1000) { + s_last_log_us = now; + /* Heap rides along because it is the other thing that fails slowly + * and silently over weeks. A node that reboots mysteriously after ten + * days looks identical to one that overheated, unless the minimum + * free heap has been visibly walking downwards the whole time. */ + ESP_LOGI(TAG, "die %.1f C (peak %.1f C) state=%s tx=%d.%02d dBm " + "heap %u min %u", + s_last_c, s_peak_c, thermal_state_name(s_state), + s_txp / 4, (s_txp % 4) * 25, + (unsigned)esp_get_free_heap_size(), + (unsigned)esp_get_minimum_free_heap_size()); + } +} + +float thermal_celsius(void) { return s_last_c; } +float thermal_peak_celsius(void) { return s_peak_c; } +thermal_state_t thermal_state(void) { return s_state; } +int8_t thermal_tx_dbm(void) { return (int8_t)(s_txp / 4); } + +const char *thermal_state_name(thermal_state_t s) +{ + switch (s) { + case THERMAL_OK: return "ok"; + case THERMAL_WARN: return "warn"; + case THERMAL_THROTTLED: return "throttled"; + case THERMAL_CRITICAL: return "critical"; + } + return "?"; +} + +#endif /* CONFIG_THERMAL_MONITOR && ESP32C6 */ diff --git a/firmware/esp32-csi-node/main/thermal.h b/firmware/esp32-csi-node/main/thermal.h new file mode 100644 index 000000000..c4529f34d --- /dev/null +++ b/firmware/esp32-csi-node/main/thermal.h @@ -0,0 +1,106 @@ +/* On-chip thermal monitoring and radio throttling. See thermal.c for why the + * thresholds are set against die temperature rather than the plastic. */ +#pragma once + +#include "esp_err.h" +#include + +typedef enum { + THERMAL_OK = 0, + THERMAL_WARN, + THERMAL_THROTTLED, + THERMAL_CRITICAL, +} thermal_state_t; + +/** + * Decide the next thermal state from a reading and the current state. + * + * Pure: no globals, no hardware. Kept in the public header so a host test can + * exercise the exact function the device build runs, rather than a copy of it + * that is free to drift. + * + * Rising edges act immediately -- heat is a reason to react now. EVERY falling + * edge requires cooling a full `hyst_c` below the threshold that was crossed on + * the way up, because recovering at the same point that tripped oscillates, and + * each oscillation steps this node's transmit power, which moves the RSSI of + * every link it has. A reader cannot distinguish that from a wall moving. + * + * The CRITICAL -> THROTTLED edge is the reason this exists as a function. It + * used to be unconditional: any reading below `critical_c` dropped the state + * immediately, so a node sitting on the critical boundary flapped between + * minimum and step-2 transmit power indefinitely -- precisely the oscillation + * the hysteresis elsewhere was added to prevent. With defaults (critical 80, + * hysteresis 8) leaving CRITICAL now needs 72 C, which is exactly the THROTTLED + * entry point. + * + * @param cur Current state. + * @param c Die temperature, degrees C. + * @param warn_c Warn threshold. + * @param throttle_c Throttle threshold. + * @param critical_c Critical threshold. + * @param hyst_c Degrees of cooling required before any step down. + * @return The state to adopt. + */ +static inline thermal_state_t thermal_next_state(thermal_state_t cur, float c, + int warn_c, int throttle_c, + int critical_c, int hyst_c) +{ + const float h = (float)hyst_c; + + /* Rising: immediate, at the plain thresholds. */ + if (c >= (float)critical_c) return THERMAL_CRITICAL; + + /* Falling out of CRITICAL needs a full hysteresis band, like every other + * downward step. Without this the state tracks `c` across critical_c with + * no damping at all. */ + if (cur == THERMAL_CRITICAL && c >= (float)critical_c - h) { + return THERMAL_CRITICAL; + } + + if (c >= (float)throttle_c) return THERMAL_THROTTLED; + + if (cur == THERMAL_THROTTLED && c >= (float)throttle_c - h) { + return THERMAL_THROTTLED; + } + + if (c >= (float)warn_c) return THERMAL_WARN; + + if (cur == THERMAL_WARN && c >= (float)warn_c - h) { + return THERMAL_WARN; + } + + return THERMAL_OK; +} + +#if defined(CONFIG_THERMAL_MONITOR) && defined(CONFIG_IDF_TARGET_ESP32C6) + +esp_err_t thermal_init(void); + +/* Sample and apply policy. Call from a slow loop -- the die does not change + * fast, and reading it more often buys nothing. */ +void thermal_tick(void); + +float thermal_celsius(void); +float thermal_peak_celsius(void); +thermal_state_t thermal_state(void); + +/* Current WiFi transmit ceiling in whole dBm. + * + * Exposed because a throttled node's RSSI falls at both ends, which is + * indistinguishable from an obstruction in the link diagnostics. Anything + * reading link strength should check this before concluding a wall moved. */ +int8_t thermal_tx_dbm(void); + +const char *thermal_state_name(thermal_state_t s); + +#else /* stubs: S3 build and when monitoring is off */ + +static inline esp_err_t thermal_init(void) { return ESP_OK; } +static inline void thermal_tick(void) {} +static inline float thermal_celsius(void) { return -273.0f; } +static inline float thermal_peak_celsius(void) { return -273.0f; } +static inline thermal_state_t thermal_state(void) { return THERMAL_OK; } +static inline int8_t thermal_tx_dbm(void) { return 20; } +static inline const char *thermal_state_name(thermal_state_t s) { (void)s; return "n/a"; } + +#endif diff --git a/firmware/esp32-csi-node/sdkconfig.defaults.esp32c6 b/firmware/esp32-csi-node/sdkconfig.defaults.esp32c6 index e67ee9ade..9e8051b09 100644 --- a/firmware/esp32-csi-node/sdkconfig.defaults.esp32c6 +++ b/firmware/esp32-csi-node/sdkconfig.defaults.esp32c6 @@ -77,3 +77,22 @@ CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH=8192 # ── Power: keep CPU at max 160 MHz (C6 ceiling) for DSP throughput ── CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_160=y CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ=160 + +# ADR-110 D1 / ruvnet/RuView#762: the 802.15.4 RX path never delivers a +# protocol frame on IDF v5.4. Upstream tested five configurations -- channel +# 15 and 26, OpenThread on and off, promiscuous on and off, PAN-matched and +# broadcast -- and every one gave rx=0 with TX at 100% reliability. ESP-NOW +# shipped as the workaround and is what this firmware actually syncs on +# (measured 99.56% cross-board delivery, 104 us smoothed offset). +# +# The C6 has a single 2.4 GHz transceiver shared by WiFi, BLE and 802.15.4, +# so leaving the 15.4 stack running costs coexistence time-slicing against +# the CSI capture that is the entire point of the node -- beaconing every +# 100 ms and listening on a radio that provably returns nothing. Disable it +# and give the airtime back to WiFi, which matters most at nine nodes where +# the 50 Hz receive gate is already the binding constraint. +# +# Safe to turn off: the ADR-018 byte 19 bit 4 "sync valid" flag is ORed from +# both transports and the 15.4 branch is #ifdef-gated on this symbol, so the +# working ESP-NOW path keeps setting it. +CONFIG_C6_TIMESYNC_ENABLE=n diff --git a/firmware/esp32-csi-node/test/Makefile b/firmware/esp32-csi-node/test/Makefile index b55899455..f07145574 100644 --- a/firmware/esp32-csi-node/test/Makefile +++ b/firmware/esp32-csi-node/test/Makefile @@ -39,14 +39,35 @@ CFLAGS = -fsanitize=fuzzer,address,undefined -g -O1 \ STUBS_SRC = stubs/esp_stubs.c MAIN_DIR = ../main +# GNU ld and Apple's ld spell dead section elimination differently. +# Keep the host sanitation test portable across the Linux CI runner and macOS. +UNAME_S := $(shell uname -s) +ifeq ($(UNAME_S),Darwin) +DEAD_STRIP_LDFLAGS := -Wl,-dead_strip +else +DEAD_STRIP_LDFLAGS := -Wl,--gc-sections +endif + # Default fuzz duration (seconds) and jobs FUZZ_DURATION ?= 30 FUZZ_JOBS ?= 1 .PHONY: all clean run_serialize run_edge run_nvs run_all test_adr110 run_adr110 \ - test_vitals run_vitals test_mmwave_detect run_mmwave_detect host_tests + test_vitals run_vitals test_mmwave_detect run_mmwave_detect host_tests \ + test_thermal run_thermal test_csi_sanitize run_csi_sanitize -all: fuzz_serialize fuzz_edge fuzz_nvs test_adr110 test_vitals test_mmwave_detect +all: fuzz_serialize fuzz_edge fuzz_nvs test_adr110 test_vitals test_mmwave_detect \ + test_thermal test_csi_sanitize + +# --- ESP-IDF invalid first-word sanitation --- +# Links the production serializer. Function sections and dead stripping keep +# unrelated device-only callback code out of this host executable. +test_csi_sanitize: test_csi_sanitize.c $(MAIN_DIR)/csi_collector.c $(STUBS_SRC) + cc -std=c99 -Wall -Wextra -Istubs -I$(MAIN_DIR) -ffunction-sections \ + $^ $(DEAD_STRIP_LDFLAGS) -o $@ -lm + +run_csi_sanitize: test_csi_sanitize + ./test_csi_sanitize # --- ADR-110 encoding unit tests --- # Host-side, no libFuzzer needed — plain C99 deterministic table tests @@ -80,8 +101,19 @@ test_mmwave_detect: test_mmwave_detect.c $(MAIN_DIR)/mmwave_detect.h run_mmwave_detect: test_mmwave_detect ./test_mmwave_detect -host_tests: run_adr110 run_vitals run_mmwave_detect - @echo "Host tests passed (ADR-110 + vitals #998/#996 + mmwave detect #1135)" +# --- Thermal state hysteresis --- +# Host-side. Exercises thermal_next_state() from ../main/thermal.h directly -- +# the same function the device build runs -- so the test and the firmware cannot +# drift. Pins every edge in both directions, including the CRITICAL -> THROTTLED +# flap that had no hysteresis at all. +test_thermal: test_thermal_hysteresis.c $(MAIN_DIR)/thermal.h + cc -std=c99 -Wall -Wextra -Istubs -I$(MAIN_DIR) -o $@ $< + +run_thermal: test_thermal + ./test_thermal + +host_tests: run_adr110 run_vitals run_mmwave_detect run_thermal run_csi_sanitize + @echo "Host tests passed (ADR-110 + CSI sanitation + vitals + mmwave + thermal)" # --- Serialize fuzzer --- # Tests csi_serialize_frame() with random wifi_csi_info_t inputs. @@ -117,5 +149,6 @@ run_nvs: fuzz_nvs run_all: run_serialize run_edge run_nvs clean: - rm -f fuzz_serialize fuzz_edge fuzz_nvs test_adr110 test_vitals + rm -f fuzz_serialize fuzz_edge fuzz_nvs test_adr110 test_vitals test_mmwave_detect \ + test_thermal test_csi_sanitize rm -rf corpus_serialize/ corpus_edge/ corpus_nvs/ diff --git a/firmware/esp32-csi-node/test/fuzz_csi_serialize.c b/firmware/esp32-csi-node/test/fuzz_csi_serialize.c index 1eb3af855..4aade0421 100644 --- a/firmware/esp32-csi-node/test/fuzz_csi_serialize.c +++ b/firmware/esp32-csi-node/test/fuzz_csi_serialize.c @@ -90,6 +90,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) info.rx_ctrl.sig_mode = legacy_inputs & 0x03; info.rx_ctrl.cwb = (legacy_inputs >> 2) & 0x01; info.rx_ctrl.stbc = (legacy_inputs >> 3) & 0x01; + info.first_word_invalid = (test_case & 0x80) != 0; /* Use remaining fuzz data as I/Q buffer content. */ uint16_t iq_len; @@ -136,6 +137,17 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) if (result > out_len) { __builtin_trap(); /* Buffer overflow detected. */ } + if (result >= CSI_HEADER_SIZE + iq_len && info.first_word_invalid && iq_len > 0) { + size_t invalid_len = iq_len < 4 ? iq_len : 4; + for (size_t i = 0; i < invalid_len; i++) { + if (out_buf[CSI_HEADER_SIZE + i] != 0) { + __builtin_trap(); /* Invalid hardware prefix escaped sanitation. */ + } + } + if ((out_buf[19] & CSI_FLAG_FIRST_WORD_SANITIZED) == 0) { + __builtin_trap(); /* Sanitized payload must carry provenance. */ + } + } /* --- Test case 1: NULL info pointer --- */ if (test_case & 0x01) { diff --git a/firmware/esp32-csi-node/test/stubs/esp_stubs.h b/firmware/esp32-csi-node/test/stubs/esp_stubs.h index 3d849a89b..e2dc08abc 100644 --- a/firmware/esp32-csi-node/test/stubs/esp_stubs.h +++ b/firmware/esp32-csi-node/test/stubs/esp_stubs.h @@ -90,6 +90,7 @@ typedef struct { typedef struct { wifi_pkt_rx_ctrl_t rx_ctrl; uint8_t mac[6]; + bool first_word_invalid; int16_t len; /**< Length of the I/Q buffer in bytes. */ int8_t *buf; /**< Pointer to I/Q data. */ } wifi_csi_info_t; diff --git a/firmware/esp32-csi-node/test/test_csi_sanitize.c b/firmware/esp32-csi-node/test/test_csi_sanitize.c new file mode 100644 index 000000000..f0776a76a --- /dev/null +++ b/firmware/esp32-csi-node/test/test_csi_sanitize.c @@ -0,0 +1,56 @@ +/** Deterministic host test for ESP-IDF first_word_invalid sanitation. */ + +#include "esp_stubs.h" +#include "nvs_config.h" +#include "csi_collector.h" + +#include +#include +#include + +nvs_config_t g_nvs_config; + +static void make_info(wifi_csi_info_t *info, int8_t *iq, uint16_t len, bool invalid) +{ + memset(info, 0, sizeof(*info)); + info->rx_ctrl.channel = 6; + info->rx_ctrl.rssi = -42; + info->rx_ctrl.noise_floor = -91; + info->len = (int16_t)len; + info->buf = iq; + info->first_word_invalid = invalid; +} + +int main(void) +{ + int8_t iq[] = {1, 2, 3, 4, 5, 6, 7, 8}; + uint8_t frame[CSI_MAX_FRAME_SIZE]; + wifi_csi_info_t info; + + make_info(&info, iq, sizeof(iq), true); + size_t len = csi_serialize_frame(&info, frame, sizeof(frame)); + assert(len == CSI_HEADER_SIZE + sizeof(iq)); + assert(frame[CSI_HEADER_SIZE] == 0); + assert(frame[CSI_HEADER_SIZE + 1] == 0); + assert(frame[CSI_HEADER_SIZE + 2] == 0); + assert(frame[CSI_HEADER_SIZE + 3] == 0); + assert(frame[CSI_HEADER_SIZE + 4] == 5); + assert((frame[19] & CSI_FLAG_FIRST_WORD_SANITIZED) != 0); + + make_info(&info, iq, sizeof(iq), false); + len = csi_serialize_frame(&info, frame, sizeof(frame)); + assert(len == CSI_HEADER_SIZE + sizeof(iq)); + assert(memcmp(&frame[CSI_HEADER_SIZE], iq, sizeof(iq)) == 0); + assert((frame[19] & CSI_FLAG_FIRST_WORD_SANITIZED) == 0); + + int8_t short_iq[] = {9, 10}; + make_info(&info, short_iq, sizeof(short_iq), true); + len = csi_serialize_frame(&info, frame, sizeof(frame)); + assert(len == CSI_HEADER_SIZE + sizeof(short_iq)); + assert(frame[CSI_HEADER_SIZE] == 0); + assert(frame[CSI_HEADER_SIZE + 1] == 0); + assert((frame[19] & CSI_FLAG_FIRST_WORD_SANITIZED) != 0); + + puts("CSI invalid-prefix sanitation: PASS"); + return 0; +} diff --git a/firmware/esp32-csi-node/test/test_thermal_hysteresis.c b/firmware/esp32-csi-node/test/test_thermal_hysteresis.c new file mode 100644 index 000000000..24630a22c --- /dev/null +++ b/firmware/esp32-csi-node/test/test_thermal_hysteresis.c @@ -0,0 +1,147 @@ +/** + * @file test_thermal_hysteresis.c + * @brief Pins every thermal transition, in both directions. + * + * Exercises `thermal_next_state()` from ../main/thermal.h directly -- the same + * function the device build runs, not a copy -- so the test and the firmware + * cannot drift apart. + * + * The regression that motivated this: the CRITICAL -> THROTTLED edge was + * unconditional. Any reading below `critical_c` dropped the state at once, so a + * node sitting on the critical boundary flapped between minimum and step-2 + * transmit power forever. Every oscillation moves the RSSI of every link that + * node has, at both ends, which a reader cannot tell apart from an obstruction + * -- exactly the failure the hysteresis on the other edges exists to prevent. + * + * Defaults under test: warn 65, throttle 72, critical 80, hysteresis 8. + */ + +#include + +#include "thermal.h" + +#define WARN_C 65 +#define THROTTLE_C 72 +#define CRITICAL_C 80 +#define HYST_C 8 + +static int g_failures = 0; + +static const char *name(thermal_state_t s) +{ + switch (s) { + case THERMAL_OK: return "OK"; + case THERMAL_WARN: return "WARN"; + case THERMAL_THROTTLED: return "THROTTLED"; + case THERMAL_CRITICAL: return "CRITICAL"; + } + return "?"; +} + +static thermal_state_t step(thermal_state_t cur, float c) +{ + return thermal_next_state(cur, c, WARN_C, THROTTLE_C, CRITICAL_C, HYST_C); +} + +static void expect(thermal_state_t cur, float c, thermal_state_t want, + const char *why) +{ + thermal_state_t got = step(cur, c); + if (got != want) { + printf(" FAIL: from %-9s at %5.1f C -> %s, expected %s\n %s\n", + name(cur), c, name(got), name(want), why); + g_failures++; + } +} + +int main(void) +{ + printf("thermal hysteresis (warn %d, throttle %d, critical %d, hyst %d)\n", + WARN_C, THROTTLE_C, CRITICAL_C, HYST_C); + + /* ---- Rising edges are immediate. Heat is a reason to act now. ---- */ + expect(THERMAL_OK, 64.9f, THERMAL_OK, "below warn"); + expect(THERMAL_OK, 65.0f, THERMAL_WARN, "warn trips at the threshold"); + expect(THERMAL_WARN, 72.0f, THERMAL_THROTTLED, "throttle trips at the threshold"); + expect(THERMAL_THROTTLED, 80.0f, THERMAL_CRITICAL, "critical trips at the threshold"); + /* A jump straight past intermediate bands must not be damped on the way up. */ + expect(THERMAL_OK, 95.0f, THERMAL_CRITICAL, "a large rise skips straight to critical"); + + /* ---- THE REGRESSION: falling out of CRITICAL. ---- + * + * Before the fix, 79.9 C dropped the state to THROTTLED immediately, so a + * node hovering at 80 alternated TXP_MIN and TXP_STEP2 indefinitely. */ + expect(THERMAL_CRITICAL, 79.9f, THERMAL_CRITICAL, + "0.1 C below critical must NOT step down -- this is the flap"); + expect(THERMAL_CRITICAL, 75.0f, THERMAL_CRITICAL, + "still inside the hysteresis band"); + expect(THERMAL_CRITICAL, 72.1f, THERMAL_CRITICAL, + "just inside the band (critical 80 - hyst 8 = 72)"); + /* Once it has cooled a full band it adopts the band it is ACTUALLY in, + * which under these defaults is WARN, not THROTTLED: the critical exit + * point (80 - 8 = 72) coincides exactly with the throttle threshold, so + * anything cool enough to leave CRITICAL is already below THROTTLED's + * entry. THROTTLED is therefore unreachable descending from CRITICAL at + * hysteresis 8 -- a consequence of the chosen constants, not of the logic. + * With a smaller hysteresis the intermediate step reappears, which the + * next assertion pins. */ + expect(THERMAL_CRITICAL, 71.9f, THERMAL_WARN, + "a full band below critical, and 71.9 is in the warn band"); + + /* Same descent with hysteresis 4: the exit point is 76, which lands inside + * THROTTLED's band, so the intermediate power step is used. */ + { + thermal_state_t got = thermal_next_state(THERMAL_CRITICAL, 75.9f, + WARN_C, THROTTLE_C, CRITICAL_C, 4); + if (got != THERMAL_THROTTLED) { + printf(" FAIL: from CRITICAL at 75.9 C with hyst 4 -> %s, " + "expected THROTTLED\n", name(got)); + g_failures++; + } + thermal_state_t held = thermal_next_state(THERMAL_CRITICAL, 76.1f, + WARN_C, THROTTLE_C, CRITICAL_C, 4); + if (held != THERMAL_CRITICAL) { + printf(" FAIL: from CRITICAL at 76.1 C with hyst 4 -> %s, " + "expected CRITICAL\n", name(held)); + g_failures++; + } + } + + /* Prove the flap is gone by walking across the boundary repeatedly: a node + * dithering either side of 80 must not change state on the down-swings. */ + { + thermal_state_t s = THERMAL_CRITICAL; + int changes = 0; + const float dither[] = {80.2f, 79.8f, 80.1f, 79.7f, 80.3f, 79.9f}; + for (unsigned i = 0; i < sizeof(dither) / sizeof(dither[0]); i++) { + thermal_state_t n = step(s, dither[i]); + if (n != s) changes++; + s = n; + } + if (changes != 0) { + printf(" FAIL: dithering around the critical boundary produced %d " + "state change(s); each one steps transmit power\n", changes); + g_failures++; + } + } + + /* ---- Falling out of THROTTLED. Behaviour preserved from before. ---- */ + expect(THERMAL_THROTTLED, 71.9f, THERMAL_THROTTLED, "just below the throttle point holds"); + expect(THERMAL_THROTTLED, 64.1f, THERMAL_THROTTLED, "still inside the band"); + expect(THERMAL_THROTTLED, 63.9f, THERMAL_OK, "throttle 72 - hyst 8 = 64 releases"); + + /* ---- Falling out of WARN. ---- */ + expect(THERMAL_WARN, 64.9f, THERMAL_WARN, "just below warn holds"); + expect(THERMAL_WARN, 57.1f, THERMAL_WARN, "still inside the band"); + expect(THERMAL_WARN, 56.9f, THERMAL_OK, "warn 65 - hyst 8 = 57 releases"); + + /* ---- OK is stable well below everything. ---- */ + expect(THERMAL_OK, 30.0f, THERMAL_OK, "a cold node stays OK"); + + if (g_failures == 0) { + printf(" PASS\n"); + return 0; + } + printf(" %d check(s) FAILED\n", g_failures); + return 1; +} diff --git a/scripts/fal-visual-teacher/extract_frames.py b/scripts/fal-visual-teacher/extract_frames.py new file mode 100644 index 000000000..130e3ea6e --- /dev/null +++ b/scripts/fal-visual-teacher/extract_frames.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python3 +"""Extract training stills from the real fal.ai room-visualization MP4s. + +Reads the source videos (real fal.ai-generated room visualizations, one per +style preset) and writes JPEG frames + a caption .txt per frame into a local +(gitignored) training-data directory, ready to be zipped for fal.ai LoRA +training. Frame interval is time-based (not frame-count-based) so each +source contributes visual diversity proportional to its length. +""" + +import argparse +import shutil +import subprocess +import sys +from pathlib import Path + +REPO_ROOT = Path(__file__).resolve().parents[2] + +# (source mp4 path, style tag, caption) -- sources are real fal.ai output, +# captured earlier this session by the fal-room-viz / room-viz-h3-upgrade +# agents. Caption describes the RuView visual style so the LoRA learns the +# recurring motifs (glowing pose overlay, WiFi signal arcs, vitals readout, +# dark tech aesthetic) rather than any single room layout. +SOURCES = [ + ("/tmp/room-viz-architectural/room-viz.mp4", "architectural", + "ruview style room visualization, architectural wireframe overlay, " + "glowing cyan pose skeleton, wifi signal arcs, dark tech aesthetic"), + ("/tmp/room-viz-abstract/room-viz.mp4", "abstract", + "ruview style room visualization, abstract particle field, " + "glowing pose overlay, wifi signal arcs, dark tech aesthetic"), + ("/tmp/room-viz-branded/room-viz.mp4", "branded", + "ruview style room visualization, branded hud overlay, " + "glowing cyan and emerald pose skeleton, vitals readout, dark tech aesthetic"), + ("/tmp/room-viz-h3-architectural/room-viz.mp4", "h3-architectural", + "ruview style room visualization, architectural wireframe overlay, " + "glowing pose skeleton, wifi signal arcs, vitals readout, dark tech aesthetic"), + ("/tmp/room-viz-h3-cinematic/room-viz.mp4", "h3-cinematic", + "ruview style room visualization, cinematic lighting, " + "glowing pose overlay, wifi signal arcs, vitals readout, dark tech aesthetic"), + ("/tmp/room-viz-h3-minimal/room-viz.mp4", "h3-minimal", + "ruview style room visualization, minimal clean overlay, " + "glowing pose skeleton, subtle wifi signal arcs, dark tech aesthetic"), + ("/tmp/room-viz-h3-abstract/room-viz.mp4", "h3-abstract", + "ruview style room visualization, abstract particle field, " + "glowing pose overlay, wifi signal arcs, dark tech aesthetic"), +] + +FRAME_INTERVAL_SEC = 1.5 + + +def extract(src: Path, style: str, caption: str, out_dir: Path) -> int: + pattern = out_dir / f"{style}_%03d.jpg" + cmd = [ + "ffmpeg", "-y", "-loglevel", "error", + "-i", str(src), + "-vf", f"fps=1/{FRAME_INTERVAL_SEC}", + "-q:v", "2", + str(pattern), + ] + subprocess.run(cmd, check=True) + frames = sorted(out_dir.glob(f"{style}_*.jpg")) + for frame in frames: + frame.with_suffix(".txt").write_text(caption, encoding="utf-8") + return len(frames) + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + "--out", default=str(REPO_ROOT / "scripts" / "fal-visual-teacher" / "training-data"), + help="Output directory for extracted frames + captions (gitignored)", + ) + args = parser.parse_args() + + out_dir = Path(args.out) + if out_dir.exists(): + shutil.rmtree(out_dir) + out_dir.mkdir(parents=True) + + total = 0 + for src_str, style, caption in SOURCES: + src = Path(src_str) + if not src.exists(): + print(f"SKIP (missing): {src}", file=sys.stderr) + continue + n = extract(src, style, caption, out_dir) + print(f"{style}: {n} frames from {src}") + total += n + + print(f"\nTotal frames extracted: {total}") + print(f"Training data dir: {out_dir}") + if total < 4: + print("ERROR: fal.ai training needs at least 4 images", file=sys.stderr) + return 1 + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/fal-visual-teacher/train_lora.py b/scripts/fal-visual-teacher/train_lora.py new file mode 100644 index 000000000..ff408ed27 --- /dev/null +++ b/scripts/fal-visual-teacher/train_lora.py @@ -0,0 +1,124 @@ +#!/usr/bin/env python3 +"""Submit a real fal.ai LoRA training job for the RuView synthetic visual teacher. + +Zips the extracted training-data directory, uploads it to fal.ai storage, +submits a training job to fal-ai/flux-lora-fast-training, polls until it +completes, and writes the resulting trained-model reference (diffusers LoRA +weights URL + config URL) to a local (gitignored) JSON file. + +Architecture boundary (ADR-353): the model trained here is ONLY ever a +source of supplementary synthetic visual/pixel output. It must never +influence, or be treated as a source of, measured/derived RF, pose, vitals, +identity, or confidence values. + +Requires FAL_KEY in the environment (fetch from GCP Secret Manager, +project cognitum-20260110, secret MINIMAX_MUSIC_FAL_KEY, before running). +""" + +import argparse +import json +import os +import sys +import time +import zipfile +from pathlib import Path + +SCRIPT_DIR = Path(__file__).resolve().parent +TRAINING_ENDPOINT = "fal-ai/flux-lora-fast-training" + + +def zip_training_data(src_dir: Path, zip_path: Path) -> None: + if zip_path.exists(): + zip_path.unlink() + with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf: + for f in sorted(src_dir.iterdir()): + if f.is_file(): + zf.write(f, arcname=f.name) + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + "--training-data", default=str(SCRIPT_DIR / "training-data"), + help="Directory of extracted frames + captions (from extract_frames.py)", + ) + parser.add_argument( + "--steps", type=int, default=1000, + help="Training steps (fal-ai/flux-lora-fast-training default is 1000, ~$2)", + ) + parser.add_argument( + "--trigger-word", default="ruviewstyle", + help="Trigger token to prepend so the LoRA is invocable at inference time", + ) + parser.add_argument( + "--out", default=str(SCRIPT_DIR / "output" / "training-result.json"), + help="Where to write the trained model reference JSON (gitignored)", + ) + args = parser.parse_args() + + if not os.environ.get("FAL_KEY"): + print("ERROR: FAL_KEY not set in environment", file=sys.stderr) + return 1 + + import fal_client # imported after the FAL_KEY check for a clearer error + + training_dir = Path(args.training_data) + if not training_dir.exists(): + print(f"ERROR: {training_dir} not found -- run extract_frames.py first", file=sys.stderr) + return 1 + + zip_path = training_dir.parent / "training-data.zip" + print(f"Zipping {training_dir} -> {zip_path}") + zip_training_data(training_dir, zip_path) + print(f"Zip size: {zip_path.stat().st_size / 1024:.1f} KB") + + print("Uploading training zip to fal.ai storage...") + images_data_url = fal_client.upload_file(str(zip_path)) + print(f"Uploaded: {images_data_url}") + + training_args = { + "images_data_url": images_data_url, + "steps": args.steps, + "trigger_word": args.trigger_word, + # This is a style LoRA (recurring visual aesthetic across varied room + # layouts), not a subject/character LoRA -- fal.ai's is_style flag + # disables segmentation/auto-captioning so our own caption .txt files + # (one per frame, describing the shared style) are used as-is. + "is_style": True, + } + print(f"Submitting training job to {TRAINING_ENDPOINT} (steps={args.steps})...") + handle = fal_client.submit(TRAINING_ENDPOINT, arguments=training_args) + print(f"request_id: {handle.request_id}") + + start = time.time() + for event in handle.iter_events(with_logs=True, interval=5.0): + print(f"[{time.time() - start:.0f}s] {event}") + + result = handle.get() + elapsed = time.time() - start + print(f"Training completed in {elapsed:.0f}s") + + out_path = Path(args.out) + out_path.parent.mkdir(parents=True, exist_ok=True) + record = { + "endpoint": TRAINING_ENDPOINT, + "request_id": handle.request_id, + "steps": args.steps, + "trigger_word": args.trigger_word, + "training_images": sum(1 for f in training_dir.glob("*.jpg")), + "elapsed_seconds": round(elapsed), + "result": result, + } + out_path.write_text(json.dumps(record, indent=2), encoding="utf-8") + print(f"Wrote {out_path}") + + lora_file = (result or {}).get("diffusers_lora_file", {}).get("url") + if lora_file: + print(f"\nTrained LoRA weights URL: {lora_file}") + else: + print("\nWARNING: no diffusers_lora_file.url in result -- inspect the JSON", file=sys.stderr) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/fal-visual-teacher/validate.py b/scripts/fal-visual-teacher/validate.py new file mode 100644 index 000000000..0a5573917 --- /dev/null +++ b/scripts/fal-visual-teacher/validate.py @@ -0,0 +1,95 @@ +#!/usr/bin/env python3 +"""Run a real validation inference call against the trained visual-teacher LoRA. + +Loads the trained model reference written by train_lora.py, calls +fal-ai/flux-lora with the trained LoRA weights, and saves the generated +image as evidence that training actually produced a working style LoRA. + +This inference call, and the LoRA it exercises, produce SYNTHETIC display +imagery only (ADR-353) -- never a source of measured RF/pose/vitals/identity +or confidence values. + +Requires FAL_KEY in the environment. +""" + +import argparse +import json +import os +import sys +import urllib.request +from pathlib import Path + +REPO_ROOT = Path(__file__).resolve().parents[2] +SCRIPT_DIR = Path(__file__).resolve().parent +INFERENCE_ENDPOINT = "fal-ai/flux-lora" + +DEFAULT_PROMPT = ( + "ruviewstyle room visualization, a living room with a glowing cyan pose " + "skeleton overlay, wifi signal arcs radiating across the space, a vitals " + "readout panel, dark tech aesthetic, synthetic sensor visualization" +) + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + "--training-result", default=str(SCRIPT_DIR / "output" / "training-result.json"), + ) + parser.add_argument("--prompt", default=DEFAULT_PROMPT) + parser.add_argument("--scale", type=float, default=1.0, help="LoRA application strength") + parser.add_argument( + "--out", default=str(REPO_ROOT / "assets" / "fal-visual-teacher-sample.png"), + ) + args = parser.parse_args() + + if not os.environ.get("FAL_KEY"): + print("ERROR: FAL_KEY not set in environment", file=sys.stderr) + return 1 + + import fal_client + + training_result_path = Path(args.training_result) + record = json.loads(training_result_path.read_text(encoding="utf-8")) + lora_url = record["result"]["diffusers_lora_file"]["url"] + print(f"Using trained LoRA: {lora_url}") + + print(f"Submitting validation inference to {INFERENCE_ENDPOINT}...") + result = fal_client.subscribe( + INFERENCE_ENDPOINT, + arguments={ + "prompt": args.prompt, + "loras": [{"path": lora_url, "scale": args.scale}], + "image_size": "landscape_16_9", + "num_images": 1, + "output_format": "png", + }, + with_logs=True, + ) + + images = result.get("images", []) + if not images: + print(f"ERROR: no images in result: {result}", file=sys.stderr) + return 1 + + image_url = images[0]["url"] + print(f"Generated image: {image_url}") + + out_path = Path(args.out) + out_path.parent.mkdir(parents=True, exist_ok=True) + urllib.request.urlretrieve(image_url, out_path) + print(f"Saved validation sample to {out_path}") + + meta_path = out_path.with_suffix(".json") + meta_path.write_text(json.dumps({ + "endpoint": INFERENCE_ENDPOINT, + "lora_url": lora_url, + "prompt": args.prompt, + "scale": args.scale, + "source_image_url": image_url, + }, indent=2), encoding="utf-8") + print(f"Saved validation metadata to {meta_path}") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/ruforecast-zeroshot/bidmc_windows.py b/scripts/ruforecast-zeroshot/bidmc_windows.py new file mode 100644 index 000000000..2c4348fc5 --- /dev/null +++ b/scripts/ruforecast-zeroshot/bidmc_windows.py @@ -0,0 +1,160 @@ +""" +Faithful Python port of the window/baseline/metric definitions in +v2/crates/ruforecast/crates/ruforecast-train/examples/bidmc_prepare.rs and +v2/crates/ruforecast/crates/ruforecast-core/src/{baseline,metrics}.rs so the +zero-shot pretrained-model comparison is apples-to-apples with RuForecast's +own real-data evaluation. + +CONTEXT=64, HORIZON=12, VARIATES=3 (HR, RESP, SpO2), quantiles as in +ForecastModelConfig::tiny_ci(), seasonal_period=12 (the `evaluate` CLI default). +""" +import csv +import math +import os + +CONTEXT = 64 +HORIZON = 12 +WINDOW_ROWS = CONTEXT + HORIZON +VARIATE_NAMES = ["HR", "RESP", "SpO2"] +QUANTILES = [0.05, 0.10, 0.25, 0.50, 0.75, 0.90, 0.95] +SEASONAL_PERIOD = 12 +RAW_DIR = "/tmp/bidmc-raw" + + +def read_patient(pid: int): + path = os.path.join(RAW_DIR, f"bidmc_{pid:02d}_Numerics.csv") + if not os.path.exists(path): + return None + rows = [] + with open(path, newline="") as f: + reader = csv.reader(f) + next(reader) # header + for parts in reader: + if len(parts) < 5: + continue + parts = [p.strip() for p in parts] + try: + hr = float(parts[1]) + resp = float(parts[3]) + spo2 = float(parts[4]) + except ValueError: + return None # malformed row -> treat like the Rust `?` early-return + rows.append((hr, resp, spo2)) + return rows + + +def build_window(pid: int): + """One window per patient: first CONTEXT rows are context, next HORIZON + rows are targets. Mirrors build_window() in bidmc_prepare.rs exactly + (context_start = row 0, no scanning for a "best" window).""" + rows = read_patient(pid) + if rows is None or len(rows) < WINDOW_ROWS: + return None + first = rows[:WINDOW_ROWS] + for row in first: + for v in row: + if not math.isfinite(v): + return None + context = first[:CONTEXT] # list of (hr, resp, spo2), CONTEXT rows + targets = first[CONTEXT:WINDOW_ROWS] # HORIZON rows + return {"patient": pid, "context": context, "targets": targets} + + +def judge_split(judge: str): + if judge == "a": + train_ids = list(range(1, 38)) + test_ids = list(range(38, 54)) + elif judge == "b": + train_ids = [i for i in range(1, 54) if i % 2 == 1] + test_ids = [i for i in range(1, 54) if i % 2 == 0] + else: + raise ValueError(judge) + return train_ids, test_ids + + +def build_test_windows(judge: str): + _, test_ids = judge_split(judge) + windows = [] + excluded = [] + for pid in test_ids: + w = build_window(pid) + if w is None: + excluded.append(pid) + else: + windows.append(w) + return windows, excluded + + +# ---- baselines (mirror ruforecast-core::baseline.rs) ---- + +def last_value_forecast(context): + """Repeat the last observed context row for every future step (every + quantile gets the same point value -> degenerate distribution).""" + last = context[-1] + return [[last[v] for _ in QUANTILES] for _ in range(HORIZON) for v in range(len(last))] + # NOTE: shape handled explicitly in metric functions below instead; see + # last_value_predictions() for the actually-used [horizon][variate] form. + + +def last_value_predictions(context): + last = context[-1] + return [[last[v] for v in range(len(last))] for _ in range(HORIZON)] + + +def seasonal_naive_predictions(context, period=SEASONAL_PERIOD): + """context has CONTEXT=64 rows; base = len(context) - period; for step in + 0..HORIZON: row = base + step % period.""" + base = len(context) - period + preds = [] + for step in range(HORIZON): + row = base + (step % period) + preds.append(list(context[row])) + return preds + + +# ---- metrics (mirror ruforecast-core::metrics.rs) ---- + +def mae(actual_flat, predicted_flat): + total, count = 0.0, 0 + for a, p in zip(actual_flat, predicted_flat): + total += abs(a - p) + count += 1 + return total / count if count else float("nan") + + +def weighted_quantile_loss(actual_flat, quantile_preds_flat): + """actual_flat: list of HORIZON*VARIATES actuals in step-major order + (step0-var0,step0-var1,step0-var2, step1-var0, ...). + quantile_preds_flat: same order, each entry is a list over QUANTILES. + Matches weighted_quantile_loss() in metrics.rs: sum pinball over every + (cell, quantile), normalize by 2 / (num_quantiles * sum(|actual|)).""" + loss = 0.0 + scale = 0.0 + for actual, preds in zip(actual_flat, quantile_preds_flat): + scale += abs(actual) + for q, pred in zip(QUANTILES, preds): + residual = actual - pred + loss += q * residual if residual >= 0 else (q - 1.0) * residual + if scale == 0.0: + raise ValueError("weighted quantile loss undefined: all-zero actual targets") + return 2.0 * loss / (len(QUANTILES) * scale) + + +def flatten_targets_step_major(targets): + """targets: HORIZON rows of VARIATES values -> flat step-major list.""" + out = [] + for row in targets: + out.extend(row) + return out + + +def point_to_quantile_preds(point_preds): + """point_preds: HORIZON rows of VARIATES point values (deterministic + baseline) -> step-major flat list where every quantile equals the point + value, matching how LastValue/SeasonalNaive fill every quantile slot + with the same repeated value.""" + out = [] + for row in point_preds: + for v in row: + out.append([v for _ in QUANTILES]) + return out diff --git a/scripts/ruforecast-zeroshot/run_zeroshot_eval.py b/scripts/ruforecast-zeroshot/run_zeroshot_eval.py new file mode 100644 index 000000000..051b53581 --- /dev/null +++ b/scripts/ruforecast-zeroshot/run_zeroshot_eval.py @@ -0,0 +1,193 @@ +""" +Real zero-shot pretrained-checkpoint comparison for RuForecast (see +docs/benchmarks/ruforecast.md). Runs Amazon Chronos-Bolt (and TimesFM, if +installed) zero-shot inference against the SAME BIDMC test windows, +baselines, and metrics RuForecast's own `evaluate` CLI uses, and prints a +markdown table. + +No fine-tuning. No training. Every "model" forecast below comes from a +public pretrained checkpoint's zero-shot .predict()/.forecast() call. +""" +import sys +import time +import numpy as np + +sys.path.insert(0, "/tmp") # bidmc_windows.py copied there +from bidmc_windows import ( + CONTEXT, HORIZON, VARIATE_NAMES, QUANTILES, SEASONAL_PERIOD, + build_test_windows, judge_split, + last_value_predictions, seasonal_naive_predictions, + mae, weighted_quantile_loss, flatten_targets_step_major, point_to_quantile_preds, +) + + +def eval_baselines(windows): + """Returns dict: name -> (mae, wql) aggregated over ALL test windows and + variates, matching how the Rust evaluate CLI aggregates (one running + mean/scale accumulation across the whole test set, not averaged per + window).""" + results = {} + for name, fn in [("last-value", last_value_predictions), + ("seasonal-naive", lambda ctx: seasonal_naive_predictions(ctx, SEASONAL_PERIOD))]: + all_actual, all_point_pred, all_qpred = [], [], [] + for w in windows: + preds = fn(w["context"]) # HORIZON rows x VARIATES + actual_flat = flatten_targets_step_major(w["targets"]) + point_flat = flatten_targets_step_major(preds) + qpred_flat = point_to_quantile_preds(preds) + all_actual.extend(actual_flat) + all_point_pred.extend(point_flat) + all_qpred.extend(qpred_flat) + results[name] = (mae(all_actual, all_point_pred), weighted_quantile_loss(all_actual, all_qpred)) + return results + + +def chronos_zero_shot_predictions(windows, model_name="amazon/chronos-bolt-base", device="cuda"): + """Per-variate univariate zero-shot forecast (Chronos has no native + multivariate mode) via ChronosPipeline. Returns list parallel to + `windows`, each entry: HORIZON rows x VARIATES quantile-lists.""" + from chronos import BaseChronosPipeline + pipeline = BaseChronosPipeline.from_pretrained(model_name, device_map=device, torch_dtype="bfloat16" if device == "cuda" else "float32") + import torch + + all_preds = [] + for w in windows: + ctx = w["context"] # HORIZON... actually CONTEXT rows x VARIATES + per_variate_q = [] + for v in range(len(VARIATE_NAMES)): + series = torch.tensor([row[v] for row in ctx], dtype=torch.float32) + quantiles, _mean = pipeline.predict_quantiles( + inputs=series, + prediction_length=HORIZON, + quantile_levels=QUANTILES, + ) + # quantiles shape: [1, HORIZON, len(QUANTILES)] + per_variate_q.append(quantiles[0].numpy()) # [HORIZON, Q] + # reshape to HORIZON rows, each VARIATES entries of Q-length lists + rows = [] + for h in range(HORIZON): + row = [per_variate_q[v][h].tolist() for v in range(len(VARIATE_NAMES))] + rows.append(row) + all_preds.append(rows) + return all_preds + + +def timesfm_zero_shot_predictions(windows, model_repo="google/timesfm-2.5-200m-pytorch"): + """Per-variate univariate zero-shot forecast via TimesFM 2.5 200M torch. + Quantile output has 10 columns; per the installed package's source + (timesfm_2p5_base.py: `decode_index: int = 5`), column 5 is the + point/mean forecast and columns 1..9 are quantiles [0.1..0.9] in order + (verified empirically: columns 1-9 are monotonic and column 5 exactly + equals the returned point forecast on every sampled window/variate; + column 0 is an unused duplicate channel, not a real quantile level). + TimesFM has no 0.05/0.95 levels (clamped to 0.1/0.9, same treatment + Chronos-Bolt applies for out-of-range levels) and no native 0.25/0.75 + (linearly interpolated between the adjacent 0.1-spaced levels it does + have).""" + import timesfm + import numpy as np + + m = timesfm.TimesFM_2p5_200M_torch.from_pretrained(model_repo) + m.compile(timesfm.ForecastConfig( + max_context=CONTEXT, max_horizon=HORIZON, normalize_inputs=True, + use_continuous_quantile_head=True, fix_quantile_crossing=True, + )) + native_levels = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9] # columns 1..9 + + def sample_at(row10, level): + if level <= native_levels[0]: + return row10[1] + if level >= native_levels[-1]: + return row10[9] + for i in range(len(native_levels) - 1): + lo, hi = native_levels[i], native_levels[i + 1] + if lo <= level <= hi: + col_lo, col_hi = 1 + i, 1 + i + 1 + if hi == lo: + return row10[col_lo] + frac = (level - lo) / (hi - lo) + return row10[col_lo] + frac * (row10[col_hi] - row10[col_lo]) + raise ValueError(level) + + all_preds = [] + for w in windows: + ctx = w["context"] + per_variate_rows = [] + for v in range(len(VARIATE_NAMES)): + series = np.array([row[v] for row in ctx], dtype=np.float32) + _point, quant = m.forecast(horizon=HORIZON, inputs=[series]) + per_variate_rows.append(quant[0]) # [HORIZON, 10] + rows = [] + for h in range(HORIZON): + row = [] + for v in range(len(VARIATE_NAMES)): + row10 = per_variate_rows[v][h] + row.append([sample_at(row10, q) for q in QUANTILES]) + rows.append(row) + all_preds.append(rows) + return all_preds + + +def eval_pretrained(name, predict_fn, windows): + t0 = time.time() + preds = predict_fn(windows) + elapsed = time.time() - t0 + all_actual, all_point_pred, all_qpred = [], [], [] + median_idx = QUANTILES.index(0.50) + for w, pred_rows in zip(windows, preds): + actual_flat = flatten_targets_step_major(w["targets"]) + qpred_flat = [] + point_flat = [] + for row in pred_rows: # HORIZON rows, each VARIATES quantile-lists + for var_qs in row: + qpred_flat.append(list(var_qs)) + point_flat.append(var_qs[median_idx]) + all_actual.extend(actual_flat) + all_point_pred.extend(point_flat) + all_qpred.extend(qpred_flat) + m = mae(all_actual, all_point_pred) + wql = weighted_quantile_loss(all_actual, all_qpred) + return m, wql, elapsed + + +def main(): + import argparse + parser = argparse.ArgumentParser() + parser.add_argument("--models", nargs="+", default=["chronos-bolt-base"]) + parser.add_argument("--judges", nargs="+", default=["a", "b"]) + args = parser.parse_args() + + print("| Judge | Model | Test windows | MAE | WQL | Wall time (s) |") + print("|---|---|---:|---:|---:|---:|") + + for judge in args.judges: + windows, excluded = build_test_windows(judge) + train_ids, test_ids = judge_split(judge) + print(f"", file=sys.stderr) + + baselines = eval_baselines(windows) + for name, (m, wql) in baselines.items(): + print(f"| {judge} | {name} (baseline) | {len(windows)} | {m:.4f} | {wql:.5f} | - |") + + for model in args.models: + if model == "chronos-bolt-base": + fn = lambda w: chronos_zero_shot_predictions(w, "amazon/chronos-bolt-base") + elif model == "chronos-bolt-small": + fn = lambda w: chronos_zero_shot_predictions(w, "amazon/chronos-bolt-small") + elif model == "chronos-t5-small": + fn = lambda w: chronos_zero_shot_predictions(w, "amazon/chronos-t5-small") + elif model == "timesfm-2.5-200m": + fn = timesfm_zero_shot_predictions + else: + print(f"unknown model {model}", file=sys.stderr) + continue + try: + m, wql, elapsed = eval_pretrained(model, fn, windows) + print(f"| {judge} | {model} (zero-shot) | {len(windows)} | {m:.4f} | {wql:.5f} | {elapsed:.1f} |") + except Exception as e: + print(f"| {judge} | {model} (zero-shot) | {len(windows)} | FAILED | FAILED | - |") + print(f"ERROR running {model} on judge {judge}: {e}", file=sys.stderr) + + +if __name__ == "__main__": + main() diff --git a/ui/app.js b/ui/app.js index 5c5bada66..55e4f741e 100644 --- a/ui/app.js +++ b/ui/app.js @@ -28,6 +28,7 @@ import { i18n } from './utils/i18n.js'; import { ScreenshotTool } from './utils/screenshot.js'; import { UptimeClock } from './utils/uptime-clock.js'; import { QuickSettings } from './utils/quick-settings.js'; +import { dataSourceBanner } from './utils/data-source-banner.js'; class WiFiDensePoseApp { constructor() { @@ -98,6 +99,10 @@ class WiFiDensePoseApp { this.showBackendStatus('Backend unavailable — start sensing-server', 'warning'); } + // Mount the global provenance bar before the service starts, so the very + // first non-live state is visible on whichever tab is showing. + dataSourceBanner.init(); + // Start the sensing WebSocket service early so the dashboard and // live-demo tabs can show the correct data-source status immediately. sensingService.start(); @@ -360,6 +365,22 @@ class WiFiDensePoseApp { window.addEventListener('beforeunload', () => { this.cleanup(); }); + + // `beforeunload` above tears down every component and disconnects the + // sensing WebSocket. If the browser restores this page from bfcache on + // Back/Forward instead of doing a fresh navigation, that torn-down state + // comes back frozen exactly as-is — nothing re-runs DOMContentLoaded, so + // sensingService.start() never fires again and disposed components (e.g. + // Room Builder) never get recreated. `pageshow`'s `event.persisted` is + // the standard signal for "this is a bfcache restore, not a fresh load"; + // forcing a real reload here is simpler and more robust than trying to + // hand-reinitialize every component/connection back to a clean state. + window.addEventListener('pageshow', (event) => { + if (event.persisted) { + console.log('Page restored from bfcache after cleanup() — forcing reload'); + window.location.reload(); + } + }); } // Handle window resize diff --git a/ui/components/DashboardTab.js b/ui/components/DashboardTab.js index 9ecd02262..5e505fa99 100644 --- a/ui/components/DashboardTab.js +++ b/ui/components/DashboardTab.js @@ -95,7 +95,8 @@ export class DashboardTab { 'live': { text: 'ESP32', status: 'healthy', msg: 'Real hardware connected' }, 'server-simulated': { text: 'SIMULATED', status: 'warning', msg: 'Server running without hardware' }, 'reconnecting': { text: 'RECONNECTING', status: 'degraded', msg: 'Attempting to connect...' }, - 'simulated': { text: 'OFFLINE', status: 'unhealthy', msg: 'Server unreachable, local fallback' }, + 'unreachable': { text: 'NO DATA', status: 'unhealthy', msg: 'Server unreachable — readings below are stale' }, + 'simulated': { text: 'INVENTED', status: 'unhealthy', msg: 'Browser-generated data, not measured' }, }; const cfg = config[ds] || config['reconnecting']; el.className = `component-status status-${cfg.status}`; diff --git a/ui/components/LiveDemoTab.js b/ui/components/LiveDemoTab.js index 4dec767d2..b4de941c3 100644 --- a/ui/components/LiveDemoTab.js +++ b/ui/components/LiveDemoTab.js @@ -1318,7 +1318,8 @@ export class LiveDemoTab { const ds = sensingService.dataSource; if (ds === 'live') return 'Active \u2014 ESP32 Live'; if (ds === 'server-simulated') return 'Active \u2014 Simulated Data'; - if (ds === 'simulated') return 'Active \u2014 Offline Simulation'; + if (ds === 'unreachable') return 'No Data \u2014 Server Unreachable'; + if (ds === 'simulated') return 'INVENTED DATA \u2014 Not Measured'; return 'Connecting...'; } @@ -1331,7 +1332,8 @@ export class LiveDemoTab { 'live': { text: 'LIVE \u2014 ESP32 Hardware Connected', cls: 'demo-source-live' }, 'server-simulated': { text: 'SIMULATED DATA \u2014 No Hardware Detected', cls: 'demo-source-sim' }, 'reconnecting': { text: 'RECONNECTING TO SERVER...', cls: 'demo-source-reconnecting' }, - 'simulated': { text: 'OFFLINE \u2014 Server Unreachable, Local Sim', cls: 'demo-source-offline' }, + 'unreachable': { text: 'NO DATA \u2014 Server Unreachable, Display Is Stale', cls: 'demo-source-offline' }, + 'simulated': { text: 'INVENTED DATA \u2014 Generated By Your Browser, Not Measured', cls: 'demo-source-offline' }, }; const cfg = config[ds] || config['reconnecting']; banner.textContent = cfg.text; diff --git a/ui/components/SensingTab.js b/ui/components/SensingTab.js index 33387eefe..4ff233bf9 100644 --- a/ui/components/SensingTab.js +++ b/ui/components/SensingTab.js @@ -228,7 +228,8 @@ export class SensingTab { 'live': { text: 'LIVE \u2014 ESP32 HARDWARE', cls: 'sensing-source-live' }, 'server-simulated': { text: 'SIMULATED \u2014 NO HARDWARE', cls: 'sensing-source-server-sim' }, 'reconnecting': { text: 'RECONNECTING...', cls: 'sensing-source-reconnecting' }, - 'simulated': { text: 'OFFLINE \u2014 CLIENT SIMULATION', cls: 'sensing-source-simulated' }, + 'unreachable': { text: 'NO DATA \u2014 SERVER UNREACHABLE', cls: 'sensing-source-simulated' }, + 'simulated': { text: 'INVENTED DATA \u2014 NOT MEASURED', cls: 'sensing-source-simulated' }, }; const cfg = bannerConfig[dataSource] || bannerConfig.reconnecting; banner.textContent = cfg.text; diff --git a/ui/services/sensing.service.js b/ui/services/sensing.service.js index 57e37cdc2..09f2e6b94 100644 --- a/ui/services/sensing.service.js +++ b/ui/services/sensing.service.js @@ -37,6 +37,39 @@ const MAX_RECONNECT_ATTEMPTS = 20; const SIM_FALLBACK_AFTER_ATTEMPTS = 5; const SIMULATION_INTERVAL = 500; // ms +export const SIM_FALLBACK_STORAGE_KEY = 'ruview-client-simulation'; + +/** + * Whether the client may invent sensing frames when the server is unreachable. + * + * Default **off**, deliberately. The generated frames are plausible — presence + * derived from `Math.sin(t * 0.3)` crossing a fixed threshold produces a steady + * absent/present_still alternation that reads exactly like a real classifier + * struggling at its decision boundary. A reader with no reason to suspect the + * server had dropped will diagnose the sensing pipeline, and be wrong. That is + * not a hypothetical: it cost a full debugging session on 2026-08-28, chasing a + * "flip-flop" that was this function all along while the server sat at a + * perfectly steady presence=true. + * + * For an instrument, "no signal" is the honest reading and a useful one. + * Synthetic motion is only appropriate for a demo, so it must be asked for: + * `?simulate=1`, or `localStorage['ruview-client-simulation'] = 'true'`. + * + * Repository rule: never present synthetic output as measurement (CLAUDE.md). + */ +function clientSimulationAllowed() { + try { + const params = new URLSearchParams(window.location.search); + if (params.get('simulate') === '1') return true; + if (params.get('simulate') === '0') return false; + } catch { /* no window/location — non-browser test context */ } + try { + return localStorage.getItem(SIM_FALLBACK_STORAGE_KEY) === 'true'; + } catch { + return false; + } +} + class SensingService { constructor() { /** @type {WebSocket|null} */ @@ -46,6 +79,11 @@ class SensingService { this._reconnectAttempt = 0; this._reconnectTimer = null; this._simTimer = null; + // Guards the await window inside `_connect()` (see the comment there). + this._connectInFlight = false; + // Bumped per connect attempt and by `stop()`; lets a socket recognise that + // it has been abandoned and stop driving reconnect state. + this._connectEpoch = 0; // Connection state: disconnected | connecting | connected | reconnecting | simulated this._state = 'disconnected'; // Data-source label exposed to the UI: @@ -72,6 +110,10 @@ class SensingService { /** Stop the service entirely. */ stop() { + // Invalidate any attempt still inside its await window, so a socket opened + // after this call does not resurrect the service. + this._connectEpoch++; + this._connectInFlight = false; this._clearTimers(); if (this._ws) { this._ws.close(1000, 'client stop'); @@ -114,7 +156,10 @@ class SensingService { * Current data source label. * "live" — frames are arriving from the real ESP32 over WebSocket * "reconnecting" — WebSocket disconnected; actively retrying, no frames emitted - * "simulated" — max reconnect attempts exhausted; emitting synthetic frames + * "unreachable" — retries exhausted, client simulation off; NO frames emitted + * and whatever is on screen is the last live reading, stale + * "simulated" — client simulation explicitly enabled and running; every + * frame on screen is invented */ get dataSource() { return this._dataSource; @@ -129,6 +174,22 @@ class SensingService { // reconnects would fail on the second attempt. async _connect() { if (this._ws && this._ws.readyState <= WebSocket.OPEN) return; + // The readyState guard above cannot stand alone: this function awaits + // ticket minting before it assigns `this._ws`, so two callers entering + // during that window BOTH pass the guard and BOTH open a socket. The + // second assignment orphans the first — a socket nobody references, still + // open on the server, whose `onclose` fires later and calls + // `_scheduleReconnect()` even though a healthy socket is live. That walks + // `_reconnectAttempt` upward with no outage and eventually trips the + // simulation fallback. Observed on the server as connect/disconnect churn + // on every page load. + if (this._connectInFlight) return; + this._connectInFlight = true; + + // Identifies this attempt for the lifetime of its socket. `stop()` and any + // superseding attempt bump it, so a late event from an abandoned socket is + // recognised and ignored rather than driving reconnect state. + const epoch = ++this._connectEpoch; this._setState('connecting'); @@ -140,15 +201,34 @@ class SensingService { // predating ADR-272, connecting without a ticket is correct. } + if (epoch !== this._connectEpoch) { + // Superseded or stopped while minting. The ticket is single-use and now + // spent; the current attempt mints its own. + this._connectInFlight = false; + return; + } + + let ws; try { - this._ws = new WebSocket(url); + ws = new WebSocket(url); } catch (err) { console.warn('[Sensing] WebSocket constructor failed:', err.message); + this._connectInFlight = false; this._fallbackToSimulation(); return; } - this._ws.onopen = () => { + this._ws = ws; + this._connectInFlight = false; + + /** True while `ws` is still the socket this service is driving. */ + const isCurrent = () => epoch === this._connectEpoch && this._ws === ws; + + ws.onopen = () => { + if (!isCurrent()) { + ws.close(1000, 'superseded'); + return; + } console.info('[Sensing] Connected to', SENSING_WS_URL); this._reconnectAttempt = 0; this._stopSimulation(); @@ -158,7 +238,8 @@ class SensingService { this._detectServerSource(); }; - this._ws.onmessage = (evt) => { + ws.onmessage = (evt) => { + if (!isCurrent()) return; try { const data = JSON.parse(evt.data); this._handleData(data); @@ -167,11 +248,17 @@ class SensingService { } }; - this._ws.onerror = () => { + ws.onerror = () => { // onerror is always followed by onclose, so we handle reconnect there }; - this._ws.onclose = (evt) => { + ws.onclose = (evt) => { + if (!isCurrent()) { + // An abandoned socket closing is not an outage. Reconnect state + // belongs to whichever attempt is current. + console.debug('[Sensing] Ignoring close from superseded socket'); + return; + } console.info('[Sensing] Connection closed (code=%d)', evt.code); this._ws = null; if (evt.code !== 1000) { @@ -184,18 +271,31 @@ class SensingService { } _scheduleReconnect() { + // A retry is already pending; stacking timers would multiply the attempt + // count for a single outage. + if (this._reconnectTimer) return; + if (this._reconnectAttempt >= MAX_RECONNECT_ATTEMPTS) { - console.warn('[Sensing] Max reconnect attempts (%d) reached, switching to simulation', MAX_RECONNECT_ATTEMPTS); - this._fallbackToSimulation(); - return; + if (clientSimulationAllowed()) { + console.warn('[Sensing] Max reconnect attempts (%d) reached, switching to simulation', MAX_RECONNECT_ATTEMPTS); + this._fallbackToSimulation(); + return; + } + // With simulation off there is nothing to give up *for*: keep retrying at + // the ceiling delay so the UI recovers by itself when the server returns, + // rather than requiring a hard refresh. + this._setDataSource('unreachable'); } const delay = RECONNECT_DELAYS[Math.min(this._reconnectAttempt, RECONNECT_DELAYS.length - 1)]; - this._reconnectAttempt++; + // Hold at the cap rather than counting into the thousands over a long + // outage; the delay is already saturated and the number is only read by a + // human in the console. + this._reconnectAttempt = Math.min(this._reconnectAttempt + 1, MAX_RECONNECT_ATTEMPTS); console.info('[Sensing] Reconnecting in %dms (attempt %d/%d)', delay, this._reconnectAttempt, MAX_RECONNECT_ATTEMPTS); this._setState('reconnecting'); - this._setDataSource('reconnecting'); + if (this._dataSource !== 'unreachable') this._setDataSource('reconnecting'); this._reconnectTimer = setTimeout(() => { this._reconnectTimer = null; @@ -205,13 +305,23 @@ class SensingService { // Only start simulation after several failed attempts so a brief hiccup // does not immediately switch the UI to "SIMULATED DATA". if (this._reconnectAttempt >= SIM_FALLBACK_AFTER_ATTEMPTS && this._state !== 'simulated') { - this._fallbackToSimulation(); + if (clientSimulationAllowed()) { + this._fallbackToSimulation(); + } else { + // Say "the server is gone" instead of inventing a room. + this._setDataSource('unreachable'); + } } } // ---- Simulation fallback ----------------------------------------------- _fallbackToSimulation() { + if (!clientSimulationAllowed()) { + this._setDataSource('unreachable'); + this._scheduleReconnect(); + return; + } this._setState('simulated'); this._setDataSource('simulated'); if (this._simTimer) return; // already running diff --git a/ui/style.css b/ui/style.css index 55c1905e1..f8ffd952b 100644 --- a/ui/style.css +++ b/ui/style.css @@ -1804,7 +1804,11 @@ canvas { flex-direction: column; gap: var(--space-12); overflow-y: auto; + /* Without this, `overflow-y: auto` alone still resolves the x axis to auto, + so anything marginally too wide scrolls sideways instead of wrapping. */ + overflow-x: hidden; max-height: 600px; + min-width: 0; } .sensing-card { @@ -1812,6 +1816,10 @@ canvas { border: 1px solid var(--color-card-border); border-radius: var(--radius-md); padding: var(--space-12); + /* Flex items default to min-width:auto, which refuses to shrink below their + content — one wide child then widens the whole 320px column and puts a + horizontal scrollbar on the panel. Cards must shrink to the column. */ + min-width: 0; } .sensing-card-title { diff --git a/ui/utils/data-source-banner.js b/ui/utils/data-source-banner.js new file mode 100644 index 000000000..d5a412eb1 --- /dev/null +++ b/ui/utils/data-source-banner.js @@ -0,0 +1,131 @@ +// Global data-source banner. +// +// The per-tab banner in SensingTab was correct and still invisible where it +// mattered: a reader on Dashboard or Live Demo never saw it, so a screen full +// of client-invented sensing frames was indistinguishable from measurement. +// This bar is fixed to the viewport and present on every tab, so the provenance +// of what is on screen is never more than a glance away. +// +// It shows nothing at all while data is live. An always-on badge becomes +// wallpaper; appearing only when the reading is not trustworthy keeps it +// meaningful. + +import { sensingService } from '../services/sensing.service.js'; + +const STYLE_ID = 'ruview-data-source-banner-style'; +const BAR_ID = 'ruview-data-source-banner'; + +const CONFIG = { + // 'live' is intentionally absent: no banner is shown for trustworthy data. + 'server-simulated': { + text: 'SYNTHETIC DATA — the server is generating this, no hardware is being read', + bg: '#7c3a00', + fg: '#ffd9a0', + }, + reconnecting: { + text: 'RECONNECTING — the display is frozen at the last live reading', + bg: '#33302a', + fg: '#e8dcc0', + }, + unreachable: { + text: 'SERVER UNREACHABLE — nothing on this screen is current', + bg: '#5c1010', + fg: '#ffc9c9', + }, + simulated: { + text: 'SIMULATED — every value on this screen is invented by your browser, not measured', + bg: '#6b0f6b', + fg: '#ffc9ff', + }, +}; + +const CSS = ` +#${BAR_ID} { + position: fixed; + top: 0; + left: 0; + right: 0; + z-index: 100000; + display: none; + padding: 7px 14px; + font: 600 13px/1.35 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; + letter-spacing: 0.04em; + text-align: center; + border-bottom: 1px solid rgba(0, 0, 0, 0.45); + box-shadow: 0 2px 10px rgba(0, 0, 0, 0.35); +} +#${BAR_ID}.is-visible { display: block; } +/* Simulated data is the dangerous case — it looks real. Make the bar move so + it cannot be mistaken for a static decoration. */ +#${BAR_ID}.is-simulated { animation: ruview-dsb-pulse 1.6s ease-in-out infinite; } +@keyframes ruview-dsb-pulse { 50% { filter: brightness(1.45); } } +@media (prefers-reduced-motion: reduce) { + #${BAR_ID}.is-simulated { animation: none; } +} +body.ruview-has-data-source-banner { padding-top: 30px; } +`; + +export class DataSourceBanner { + constructor() { + this._bar = null; + this._unsubscribe = null; + } + + init() { + if (typeof document === 'undefined') return; + + if (!document.getElementById(STYLE_ID)) { + const style = document.createElement('style'); + style.id = STYLE_ID; + style.textContent = CSS; + document.head.appendChild(style); + } + + this._bar = document.getElementById(BAR_ID); + if (!this._bar) { + this._bar = document.createElement('div'); + this._bar.id = BAR_ID; + this._bar.setAttribute('role', 'status'); + // Announce changes, but don't interrupt: the reader is mid-task and the + // message describes context, not an error to act on right now. + this._bar.setAttribute('aria-live', 'polite'); + document.body.appendChild(this._bar); + } + + // dataSource changes are published on the state-listener channel. + this._unsubscribe = sensingService.onStateChange(() => this.render()); + this.render(); + } + + render() { + if (!this._bar) return; + const cfg = CONFIG[sensingService.dataSource]; + + if (!cfg) { + this._bar.classList.remove('is-visible', 'is-simulated'); + document.body.classList.remove('ruview-has-data-source-banner'); + return; + } + + this._bar.textContent = cfg.text; + this._bar.style.background = cfg.bg; + this._bar.style.color = cfg.fg; + this._bar.classList.add('is-visible'); + this._bar.classList.toggle('is-simulated', sensingService.dataSource === 'simulated'); + document.body.classList.add('ruview-has-data-source-banner'); + } + + dispose() { + if (this._unsubscribe) { + this._unsubscribe(); + this._unsubscribe = null; + } + if (this._bar && this._bar.parentNode) { + this._bar.parentNode.removeChild(this._bar); + } + this._bar = null; + document.body.classList.remove('ruview-has-data-source-banner'); + } +} + +export const dataSourceBanner = new DataSourceBanner(); diff --git a/v2/crates/ruforecast b/v2/crates/ruforecast index 448c035cc..a802067f0 160000 --- a/v2/crates/ruforecast +++ b/v2/crates/ruforecast @@ -1 +1 @@ -Subproject commit 448c035cc18f2fa7c05aab711c6ae0fd5cfc4e4b +Subproject commit a802067f0444e10f267c5052119524de1b21747d diff --git a/v2/crates/wifi-densepose-hardware/src/csi_frame.rs b/v2/crates/wifi-densepose-hardware/src/csi_frame.rs index 412240eca..19cfa0d34 100644 --- a/v2/crates/wifi-densepose-hardware/src/csi_frame.rs +++ b/v2/crates/wifi-densepose-hardware/src/csi_frame.rs @@ -145,13 +145,16 @@ impl PpduType { /// bit 2 : STBC /// bit 3 : LDPC (reserved — not yet populated by firmware) /// bit 4 : 802.15.4 time-sync valid (C6 only) -/// bit 5-7 : reserved +/// bit 5 : ESP-IDF invalid first CSI word was zeroed by firmware +/// bit 6-7 : reserved #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)] pub struct Adr018Flags { pub bw40: bool, pub stbc: bool, pub ldpc: bool, pub ieee802154_sync_valid: bool, + #[serde(default)] + pub first_word_sanitized: bool, } impl Adr018Flags { @@ -161,6 +164,7 @@ impl Adr018Flags { stbc: (b & 0x04) != 0, ldpc: (b & 0x08) != 0, ieee802154_sync_valid: (b & 0x10) != 0, + first_word_sanitized: (b & 0x20) != 0, } } pub fn to_byte(self) -> u8 { @@ -169,6 +173,7 @@ impl Adr018Flags { if self.stbc { b |= 0x04; } if self.ldpc { b |= 0x08; } if self.ieee802154_sync_valid { b |= 0x10; } + if self.first_word_sanitized { b |= 0x20; } b } } diff --git a/v2/crates/wifi-densepose-hardware/src/esp32_parser.rs b/v2/crates/wifi-densepose-hardware/src/esp32_parser.rs index fffe3a22f..20d060666 100644 --- a/v2/crates/wifi-densepose-hardware/src/esp32_parser.rs +++ b/v2/crates/wifi-densepose-hardware/src/esp32_parser.rs @@ -413,15 +413,16 @@ mod tests { #[test] fn adr110_flags_round_trip_all_bits() { - // All known flag bits set: bw40 (0x01) + STBC (0x04) + LDPC (0x08) + 15.4-sync (0x10) = 0x1D - let data = build_test_frame_with_he(6, 1, &[(0, 0); 56], 1, 0x1D); + // All known flag bits set: bw40 + STBC + LDPC + sync + sanitized prefix = 0x3D. + let data = build_test_frame_with_he(6, 1, &[(0, 0); 56], 1, 0x3D); let (frame, _) = Esp32CsiParser::parse_frame(&data).unwrap(); assert!(frame.metadata.adr018_flags.bw40); assert!(frame.metadata.adr018_flags.stbc); assert!(frame.metadata.adr018_flags.ldpc); assert!(frame.metadata.adr018_flags.ieee802154_sync_valid); + assert!(frame.metadata.adr018_flags.first_word_sanitized); // Round-trip the encoder - assert_eq!(frame.metadata.adr018_flags.to_byte(), 0x1D); + assert_eq!(frame.metadata.adr018_flags.to_byte(), 0x3D); } #[test] diff --git a/v2/crates/wifi-densepose-sensing-server/Cargo.toml b/v2/crates/wifi-densepose-sensing-server/Cargo.toml index 3ab1a3c30..eaae6dfc3 100644 --- a/v2/crates/wifi-densepose-sensing-server/Cargo.toml +++ b/v2/crates/wifi-densepose-sensing-server/Cargo.toml @@ -30,7 +30,7 @@ mdns-sd = "0.11" # Serialization serde = { workspace = true } -serde_json.workspace = true +serde_json = { workspace = true, features = ["raw_value"] } # Logging tracing.workspace = true diff --git a/v2/crates/wifi-densepose-sensing-server/README.md b/v2/crates/wifi-densepose-sensing-server/README.md index dd8ae39c2..2224a6ddf 100644 --- a/v2/crates/wifi-densepose-sensing-server/README.md +++ b/v2/crates/wifi-densepose-sensing-server/README.md @@ -43,6 +43,8 @@ per ADR-022 Phase 3. - **WebSocket broadcast** -- Real-time sensing updates pushed to all connected clients at `ws://localhost:8765/ws/sensing`. - **Static file serving** -- Hosts the sensing UI on port 8080 with CORS headers. +- **Private startup baseline** -- Restores a recent, installation-bound empty-room field model + without storing raw CSI or authorizing numeric vital signs (ADR-355). ## Modules @@ -74,6 +76,23 @@ cargo run -p wifi-densepose-sensing-server -- \ --static-dir ./ui ``` +### Empty-room startup baseline + +Provide a stable installation ID and an application-owned private state directory: + +```bash +cargo run -p wifi-densepose-sensing-server -- \ + --installation-id installation-01 \ + --data-dir /path/to/private/application-state +``` + +Complete the normal empty-room calibration, then call +`POST /api/v1/calibration/bootstrap/promote`. The server measures a separate 12-sample holdout +before storing an aggregate field-model snapshot. On a later start with the same installation ID, +the snapshot is restored with `bootstrap_only` authority. It can reduce startup background false +positives, but cannot authorize calibrated evidence or numeric heart and breathing rates. Use +`POST /api/v1/calibration/reset` with administrator scope to remove it. + ### Using as a library ```rust diff --git a/v2/crates/wifi-densepose-sensing-server/src/adaptive_classifier.rs b/v2/crates/wifi-densepose-sensing-server/src/adaptive_classifier.rs index 3a662d07a..f5baecbf7 100644 --- a/v2/crates/wifi-densepose-sensing-server/src/adaptive_classifier.rs +++ b/v2/crates/wifi-densepose-sensing-server/src/adaptive_classifier.rs @@ -139,6 +139,39 @@ pub fn features_from_runtime(feat: &serde_json::Value, amps: &[f64]) -> [f64; N_ ] } +/// Preserve the fifth feature's historical meaning for models trained before +/// the server began measuring temporal frequency from a frame window. +/// +/// Version 1 models learned a proxy equal to the strongest subcarrier index +/// multiplied by 0.05. Version 2 and later models consume the measured +/// temporal frequency in hertz. Keeping this translation at the model boundary +/// prevents a server upgrade from silently changing an existing model's input +/// distribution. +pub fn compatible_dominant_frequency( + model_version: u32, + temporal_frequency_hz: f64, + amplitudes: &[f64], +) -> f64 { + if model_version >= 2 { + return if temporal_frequency_hz.is_finite() { + temporal_frequency_hz.max(0.0) + } else { + 0.0 + }; + } + + amplitudes + .iter() + .enumerate() + .filter(|(_, amplitude)| amplitude.is_finite()) + .max_by(|(_, left), (_, right)| { + left.partial_cmp(right) + .unwrap_or(std::cmp::Ordering::Equal) + }) + .map(|(index, _)| index as f64 * 0.05) + .unwrap_or(0.0) +} + /// Compute statistical features from raw subcarrier amplitudes. fn subcarrier_stats(amps: &[f64]) -> (f64, f64, f64, f64, f64, f64, f64, f64) { if amps.is_empty() { @@ -642,3 +675,21 @@ pub fn train_from_recordings(recordings_dir: &Path) -> Result PathBuf { PathBuf::from("data/adaptive_model.json") } + +#[cfg(test)] +mod compatibility_tests { + use super::compatible_dominant_frequency; + + #[test] + fn version_one_keeps_legacy_subcarrier_proxy() { + let amplitudes = [1.0, 2.0, 8.0, 3.0]; + assert_eq!(compatible_dominant_frequency(1, 1.25, &litudes), 0.10); + } + + #[test] + fn version_two_uses_measured_temporal_frequency() { + let amplitudes = [1.0, 20.0, 2.0]; + assert_eq!(compatible_dominant_frequency(2, 1.25, &litudes), 1.25); + assert_eq!(compatible_dominant_frequency(2, f64::NAN, &litudes), 0.0); + } +} diff --git a/v2/crates/wifi-densepose-sensing-server/src/bearer_auth.rs b/v2/crates/wifi-densepose-sensing-server/src/bearer_auth.rs index 783ed7c17..d8a9cbfa5 100644 --- a/v2/crates/wifi-densepose-sensing-server/src/bearer_auth.rs +++ b/v2/crates/wifi-densepose-sensing-server/src/bearer_auth.rs @@ -491,6 +491,10 @@ const READ_SAFE_MUTATIONS: &[&str] = &[ // Capture and calibration: create data, never destroy it. "/api/v1/calibration/start", "/api/v1/calibration/stop", + // Promotion consumes only server measured held out evidence and stores a + // bounded privacy reduced prior. Cancel discards only an unfinished run. + "/api/v1/calibration/bootstrap/promote", + "/api/v1/calibration/cancel", "/api/v1/pose/calibrate", "/api/v1/recording/start", "/api/v1/recording/stop", @@ -1970,6 +1974,7 @@ mod scope_gate_polarity_tests { (Method::DELETE, "/api/v1/models/m1"), (Method::DELETE, "/api/v1/recording/r1"), (Method::POST, "/api/v1/config/ground-truth"), + (Method::POST, "/api/v1/calibration/reset"), ] { assert_eq!( required_scope_for(&m, p), diff --git a/v2/crates/wifi-densepose-sensing-server/src/bootstrap_baseline.rs b/v2/crates/wifi-densepose-sensing-server/src/bootstrap_baseline.rs new file mode 100644 index 000000000..b6ce978c8 --- /dev/null +++ b/v2/crates/wifi-densepose-sensing-server/src/bootstrap_baseline.rs @@ -0,0 +1,532 @@ +//! Local, privacy reduced empty room bootstrap model persistence. +//! +//! A bootstrap image is deliberately lower authority than a completed room +//! calibration. It can suppress background perturbations while the server +//! starts, but restoring it never authorizes calibrated evidence or vitals. + +use rand::{rngs::OsRng, RngCore}; +use serde::{Deserialize, Serialize}; +use serde_json::value::RawValue; +use sha2::{Digest, Sha256}; +use std::fs::{self, OpenOptions}; +use std::io::{Read, Write}; +use std::path::{Path, PathBuf}; +use thiserror::Error; +use wifi_densepose_signal::ruvsense::field_model::{ + FieldModel, FieldModelError, FieldModelSnapshotV1, +}; + +pub const BOOTSTRAP_BASELINE_SCHEMA: &str = "ruview.bootstrap-empty-field-model.v2"; +pub const BOOTSTRAP_BASELINE_AUTHORITY: &str = "bootstrap_only"; +pub const BOOTSTRAP_VALIDATION_SAMPLES: usize = 12; +pub const BOOTSTRAP_VALIDATION_MIN_EMPTY: usize = 10; +pub const BOOTSTRAP_VALIDATION_MIN_SPACING_MS: u64 = 1_000; +pub const BOOTSTRAP_VALIDATION_SAMPLE_TIMEOUT_MS: u64 = 4_000; +const BOOTSTRAP_MAX_FILE_BYTES: u64 = 1_048_576; + +/// Exact CSI layout used to train the persisted empty room model. +/// +/// Restoring code must require incoming frames to match this binding before +/// the bootstrap image can influence inference. +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] +#[serde(deny_unknown_fields)] +pub struct BootstrapCsiGrid { + pub n_subcarriers: u16, + pub ppdu_type: u8, +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +#[serde(deny_unknown_fields)] +struct BootstrapPayloadV2 { + schema: String, + authority: String, + installation_binding_sha256: String, + source_node_ids: Vec, + source_grid: BootstrapCsiGrid, + source_model_id: String, + created_at_unix_ms: u64, + expires_at_unix_ms: u64, + field_model: FieldModelSnapshotV1, +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +#[serde(deny_unknown_fields)] +struct BootstrapImageV2 { + payload: BootstrapPayloadV2, + content_sha256: String, +} + +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields)] +struct BootstrapImageRawV2 { + payload: Box, + content_sha256: String, +} + +#[derive(Debug, Clone, Serialize, PartialEq, Eq)] +pub struct BootstrapBaselineMetadata { + pub authority: &'static str, + pub source_node_ids: Vec, + pub source_grid: BootstrapCsiGrid, + pub source_model_id: String, + pub created_at_unix_ms: u64, + pub expires_at_unix_ms: u64, + pub content_sha256: String, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct BootstrapValidationSample { + pub fresh_tick: bool, + pub calibrated_empty: bool, + pub vital_signs_absent: bool, +} + +#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)] +pub struct BootstrapValidationResult { + pub sample_count: usize, + pub empty_sample_count: usize, + pub stale_sample_count: usize, + pub vital_sign_sample_count: usize, + pub passed: bool, +} + +#[derive(Debug, Error)] +pub enum BootstrapBaselineError { + #[error("a stable installation_id is required for a local bootstrap baseline")] + MissingInstallationId, + #[error("bootstrap baseline path is invalid")] + InvalidPath, + #[error("bootstrap baseline file is too large")] + FileTooLarge, + #[error("bootstrap baseline image is malformed: {0}")] + Malformed(String), + #[error("bootstrap baseline belongs to another installation")] + InstallationMismatch, + #[error("bootstrap baseline has expired")] + Expired, + #[error("bootstrap baseline digest does not verify")] + DigestMismatch, + #[error("field model snapshot is invalid: {0}")] + FieldModel(#[from] FieldModelError), + #[error("bootstrap baseline storage failed: {0}")] + Io(#[from] std::io::Error), + #[error("bootstrap baseline serialization failed: {0}")] + Json(#[from] serde_json::Error), +} + +pub fn path_in(data_dir: &Path) -> PathBuf { + data_dir.join("calibration").join("bootstrap-empty-v1.json") +} + +pub fn installation_binding(installation_id: &str) -> Result { + let installation_id = installation_id.trim(); + if installation_id.is_empty() || installation_id.len() > 128 { + return Err(BootstrapBaselineError::MissingInstallationId); + } + Ok(hex_sha256(installation_id.as_bytes())) +} + +pub fn evaluate_validation(samples: &[BootstrapValidationSample]) -> BootstrapValidationResult { + let empty_sample_count = samples + .iter() + .filter(|sample| sample.fresh_tick && sample.calibrated_empty) + .count(); + let stale_sample_count = samples.iter().filter(|sample| !sample.fresh_tick).count(); + let vital_sign_sample_count = samples + .iter() + .filter(|sample| !sample.vital_signs_absent) + .count(); + BootstrapValidationResult { + sample_count: samples.len(), + empty_sample_count, + stale_sample_count, + vital_sign_sample_count, + passed: samples.len() == BOOTSTRAP_VALIDATION_SAMPLES + && empty_sample_count >= BOOTSTRAP_VALIDATION_MIN_EMPTY + && stale_sample_count == 0 + && vital_sign_sample_count == 0, + } +} + +pub fn store( + path: &Path, + installation_id: &str, + source_node_ids: &[u8], + source_grid: BootstrapCsiGrid, + source_model_id: &str, + created_at_unix_ms: u64, + field_model: &FieldModel, +) -> Result { + let snapshot = field_model.export_snapshot()?; + let calibrated_at_ms = snapshot.modes.calibrated_at_us / 1_000; + let expiry_ms = (snapshot.config.baseline_expiry_s * 1_000.0) as u64; + let payload = BootstrapPayloadV2 { + schema: BOOTSTRAP_BASELINE_SCHEMA.to_string(), + authority: BOOTSTRAP_BASELINE_AUTHORITY.to_string(), + installation_binding_sha256: installation_binding(installation_id)?, + source_node_ids: source_node_ids.to_vec(), + source_grid, + source_model_id: source_model_id.to_string(), + created_at_unix_ms, + expires_at_unix_ms: calibrated_at_ms.saturating_add(expiry_ms), + field_model: snapshot, + }; + validate_payload(&payload, installation_id, created_at_unix_ms)?; + let content_sha256 = payload_digest(&payload)?; + let image = BootstrapImageV2 { + payload, + content_sha256, + }; + let bytes = serde_json::to_vec(&image)?; + if bytes.len() as u64 > BOOTSTRAP_MAX_FILE_BYTES { + return Err(BootstrapBaselineError::FileTooLarge); + } + atomic_write_private(path, &bytes)?; + Ok(metadata(&image)) +} + +pub fn load( + path: &Path, + installation_id: &str, + current_unix_ms: u64, +) -> Result<(FieldModel, BootstrapBaselineMetadata), BootstrapBaselineError> { + let file_metadata = fs::symlink_metadata(path)?; + if file_metadata.file_type().is_symlink() || !file_metadata.is_file() { + return Err(BootstrapBaselineError::InvalidPath); + } + if file_metadata.len() > BOOTSTRAP_MAX_FILE_BYTES { + return Err(BootstrapBaselineError::FileTooLarge); + } + let mut bytes = Vec::with_capacity(file_metadata.len() as usize); + OpenOptions::new() + .read(true) + .open(path)? + .take(BOOTSTRAP_MAX_FILE_BYTES + 1) + .read_to_end(&mut bytes)?; + if bytes.len() as u64 > BOOTSTRAP_MAX_FILE_BYTES { + return Err(BootstrapBaselineError::FileTooLarge); + } + // Verify the exact payload bytes written by storage before decoding the + // floating point model. Equivalent JSON number spellings may serialize + // differently, so decode and reserialize is not an integrity contract. + let raw_image: BootstrapImageRawV2 = serde_json::from_slice(&bytes)?; + if hex_sha256(raw_image.payload.get().as_bytes()) != raw_image.content_sha256 { + return Err(BootstrapBaselineError::DigestMismatch); + } + let payload: BootstrapPayloadV2 = serde_json::from_str(raw_image.payload.get())?; + validate_payload(&payload, installation_id, current_unix_ms)?; + let image = BootstrapImageV2 { + payload, + content_sha256: raw_image.content_sha256, + }; + let model = FieldModel::from_snapshot( + image.payload.field_model.clone(), + current_unix_ms.saturating_mul(1_000), + )?; + Ok((model, metadata(&image))) +} + +pub fn remove(path: &Path) -> Result { + match fs::symlink_metadata(path) { + Ok(metadata) => { + if metadata.file_type().is_symlink() || !metadata.is_file() { + return Err(BootstrapBaselineError::InvalidPath); + } + fs::remove_file(path)?; + Ok(true) + } + Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(false), + Err(error) => Err(error.into()), + } +} + +fn validate_payload( + payload: &BootstrapPayloadV2, + installation_id: &str, + current_unix_ms: u64, +) -> Result<(), BootstrapBaselineError> { + if payload.schema != BOOTSTRAP_BASELINE_SCHEMA + || payload.authority != BOOTSTRAP_BASELINE_AUTHORITY + || payload.source_model_id.is_empty() + || payload.source_model_id.len() > 128 + || payload.source_node_ids.len() != 1 + || !(1..=2_048).contains(&payload.source_grid.n_subcarriers) + || payload.source_grid.ppdu_type > 3 + || payload.created_at_unix_ms == 0 + || payload.expires_at_unix_ms <= payload.created_at_unix_ms + { + return Err(BootstrapBaselineError::Malformed( + "invalid schema, authority, identity, node set, source grid, or lifetime".into(), + )); + } + if payload.installation_binding_sha256 != installation_binding(installation_id)? { + return Err(BootstrapBaselineError::InstallationMismatch); + } + if current_unix_ms > payload.expires_at_unix_ms { + return Err(BootstrapBaselineError::Expired); + } + Ok(()) +} + +fn payload_digest(payload: &BootstrapPayloadV2) -> Result { + Ok(hex_sha256(&serde_json::to_vec(payload)?)) +} + +fn metadata(image: &BootstrapImageV2) -> BootstrapBaselineMetadata { + BootstrapBaselineMetadata { + authority: BOOTSTRAP_BASELINE_AUTHORITY, + source_node_ids: image.payload.source_node_ids.clone(), + source_grid: image.payload.source_grid, + source_model_id: image.payload.source_model_id.clone(), + created_at_unix_ms: image.payload.created_at_unix_ms, + expires_at_unix_ms: image.payload.expires_at_unix_ms, + content_sha256: image.content_sha256.clone(), + } +} + +fn hex_sha256(bytes: &[u8]) -> String { + let digest = Sha256::digest(bytes); + digest.iter().map(|byte| format!("{byte:02x}")).collect() +} + +fn atomic_write_private(path: &Path, bytes: &[u8]) -> Result<(), BootstrapBaselineError> { + let parent = path.parent().ok_or(BootstrapBaselineError::InvalidPath)?; + fs::create_dir_all(parent)?; + let file_name = path + .file_name() + .and_then(|name| name.to_str()) + .ok_or(BootstrapBaselineError::InvalidPath)?; + let mut nonce = [0_u8; 8]; + OsRng.fill_bytes(&mut nonce); + let temporary = parent.join(format!( + ".{file_name}.{:016x}.tmp", + u64::from_le_bytes(nonce) + )); + + let mut options = OpenOptions::new(); + options.write(true).create_new(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + options.mode(0o600); + } + let write_result = (|| -> Result<(), std::io::Error> { + let mut file = options.open(&temporary)?; + file.write_all(bytes)?; + file.sync_all()?; + fs::rename(&temporary, path)?; + Ok(()) + })(); + if write_result.is_err() { + let _ = fs::remove_file(&temporary); + } + write_result.map_err(Into::into) +} + +#[cfg(test)] +mod tests { + use super::*; + use wifi_densepose_signal::ruvsense::field_model::FieldModelConfig; + + const TEST_GRID: BootstrapCsiGrid = BootstrapCsiGrid { + n_subcarriers: 192, + ppdu_type: 2, + }; + + fn completed_model(now_us: u64) -> FieldModel { + let mut model = FieldModel::new(FieldModelConfig { + n_links: 1, + n_subcarriers: 4, + n_modes: 2, + min_calibration_frames: 3, + min_calibration_duration_s: 0.0, + baseline_expiry_s: 3_600.0, + }) + .unwrap(); + for index in 0..3 { + model + .feed_calibration(&[vec![1.0 + index as f64, 2.0, 3.0, 4.0]]) + .unwrap(); + } + model.finalize_calibration(now_us, 7).unwrap(); + model + } + + #[test] + fn round_trip_is_installation_bound_and_contains_no_raw_frames() { + let dir = tempfile::tempdir().unwrap(); + let path = path_in(dir.path()); + let now_ms = 1_000_000; + let model = completed_model(now_ms * 1_000); + let stored = store(&path, "home-a", &[5], TEST_GRID, "model", now_ms, &model).unwrap(); + let text = String::from_utf8(fs::read(&path).unwrap()).unwrap(); + assert!(!text.contains("home-a")); + assert!(!text.contains("raw_csi")); + assert_eq!(stored.source_grid, TEST_GRID); + + let (restored, loaded) = load(&path, "home-a", now_ms + 1).unwrap(); + assert_eq!(stored, loaded); + assert_eq!( + restored.export_snapshot().unwrap(), + model.export_snapshot().unwrap() + ); + assert!(matches!( + load(&path, "home-b", now_ms + 1), + Err(BootstrapBaselineError::InstallationMismatch) + )); + } + + #[test] + fn tamper_and_expiry_fail_closed() { + let dir = tempfile::tempdir().unwrap(); + let path = path_in(dir.path()); + let now_ms = 1_000_000; + let model = completed_model(now_ms * 1_000); + store(&path, "home-a", &[5], TEST_GRID, "model", now_ms, &model).unwrap(); + + let mut image: serde_json::Value = + serde_json::from_slice(&fs::read(&path).unwrap()).unwrap(); + image["payload"]["source_grid"]["n_subcarriers"] = serde_json::json!(64); + fs::write(&path, serde_json::to_vec(&image).unwrap()).unwrap(); + assert!(matches!( + load(&path, "home-a", now_ms + 1), + Err(BootstrapBaselineError::DigestMismatch) + )); + + store(&path, "home-a", &[5], TEST_GRID, "model", now_ms, &model).unwrap(); + assert!(matches!( + load(&path, "home-a", now_ms + 3_600_001), + Err(BootstrapBaselineError::Expired) + | Err(BootstrapBaselineError::FieldModel( + FieldModelError::BaselineExpired { .. } + )) + )); + } + + #[test] + fn store_rejects_invalid_source_grid() { + let dir = tempfile::tempdir().unwrap(); + let path = path_in(dir.path()); + let now_ms = 1_000_000; + let model = completed_model(now_ms * 1_000); + + for source_grid in [ + BootstrapCsiGrid { + n_subcarriers: 0, + ppdu_type: 0, + }, + BootstrapCsiGrid { + n_subcarriers: 2_049, + ppdu_type: 0, + }, + BootstrapCsiGrid { + n_subcarriers: 192, + ppdu_type: 4, + }, + ] { + assert!(matches!( + store(&path, "home-a", &[5], source_grid, "model", now_ms, &model,), + Err(BootstrapBaselineError::Malformed(_)) + )); + } + } + + #[test] + fn store_requires_exactly_one_source_node() { + let dir = tempfile::tempdir().unwrap(); + let path = path_in(dir.path()); + let now_ms = 1_000_000; + let model = completed_model(now_ms * 1_000); + + for source_node_ids in [&[][..], &[5, 7][..]] { + assert!(matches!( + store( + &path, + "home-a", + source_node_ids, + TEST_GRID, + "model", + now_ms, + &model, + ), + Err(BootstrapBaselineError::Malformed(_)) + )); + } + } + + #[test] + fn source_grid_is_required_when_loading_a_v2_image() { + let dir = tempfile::tempdir().unwrap(); + let path = path_in(dir.path()); + let now_ms = 1_000_000; + let model = completed_model(now_ms * 1_000); + store(&path, "home-a", &[5], TEST_GRID, "model", now_ms, &model).unwrap(); + + let mut image: serde_json::Value = + serde_json::from_slice(&fs::read(&path).unwrap()).unwrap(); + image["payload"] + .as_object_mut() + .unwrap() + .remove("source_grid"); + let payload = serde_json::to_vec(&image["payload"]).unwrap(); + let rewritten = format!( + "{{\"payload\":{},\"content_sha256\":\"{}\"}}", + String::from_utf8(payload.clone()).unwrap(), + hex_sha256(&payload) + ); + fs::write(&path, rewritten).unwrap(); + + assert!(matches!( + load(&path, "home-a", now_ms + 1), + Err(BootstrapBaselineError::Json(_)) + )); + } + + #[test] + fn promotion_gate_requires_twelve_fresh_samples_and_zero_vitals() { + assert_eq!(BOOTSTRAP_VALIDATION_MIN_SPACING_MS, 1_000); + assert_eq!(BOOTSTRAP_VALIDATION_SAMPLE_TIMEOUT_MS, 4_000); + assert!(BOOTSTRAP_VALIDATION_SAMPLE_TIMEOUT_MS >= BOOTSTRAP_VALIDATION_MIN_SPACING_MS); + let passing = vec![ + BootstrapValidationSample { + fresh_tick: true, + calibrated_empty: true, + vital_signs_absent: true, + }; + BOOTSTRAP_VALIDATION_SAMPLES + ]; + assert!(evaluate_validation(&passing).passed); + + let mut stale = passing.clone(); + stale[0].fresh_tick = false; + assert!(!evaluate_validation(&stale).passed); + let mut vital = passing; + vital[0].vital_signs_absent = false; + assert!(!evaluate_validation(&vital).passed); + } + + #[test] + fn restart_verifies_the_exact_stored_payload_bytes() { + let dir = tempfile::tempdir().unwrap(); + let path = path_in(dir.path()); + let now_ms = 1_000_000; + let model = completed_model(now_ms * 1_000); + store(&path, "home-a", &[5], TEST_GRID, "model", now_ms, &model).unwrap(); + + let original = fs::read_to_string(&path).unwrap(); + let image: BootstrapImageRawV2 = serde_json::from_str(&original).unwrap(); + let rewritten_payload = image.payload.get().replacen( + "\"min_calibration_duration_s\":0.0", + "\"min_calibration_duration_s\":0.00", + 1, + ); + assert_ne!(rewritten_payload, image.payload.get()); + let digest = hex_sha256(rewritten_payload.as_bytes()); + let rewritten = + format!("{{\"payload\":{rewritten_payload},\"content_sha256\":\"{digest}\"}}"); + fs::write(&path, rewritten).unwrap(); + + let (_, loaded) = load(&path, "home-a", now_ms + 1).unwrap(); + assert_eq!(loaded.content_sha256, digest); + } +} diff --git a/v2/crates/wifi-densepose-sensing-server/src/csi.rs b/v2/crates/wifi-densepose-sensing-server/src/csi.rs index 6d0cd9004..a49e97c16 100644 --- a/v2/crates/wifi-densepose-sensing-server/src/csi.rs +++ b/v2/crates/wifi-densepose-sensing-server/src/csi.rs @@ -11,6 +11,141 @@ use crate::vital_signs::VitalSigns; const EDGE_MAX_PERSONS: u8 = 4; +/// Estimate a real temporal motion frequency from recent CSI amplitude +/// history. Each frame is reduced to its mean amplitude, detrended, Hann +/// windowed, and evaluated on exact DFT bins. +pub fn estimate_temporal_dominant_frequency_hz( + frame_history: &VecDeque>, + sample_rate_hz: f64, +) -> f64 { + if !sample_rate_hz.is_finite() || sample_rate_hz <= 0.0 { + return 0.0; + } + + let recent_frames: Vec<&Vec> = frame_history + .iter() + .rev() + .take(128) + .collect(); + if recent_frames + .iter() + .any(|frame| frame.is_empty() || frame.iter().any(|value| !value.is_finite())) + { + return 0.0; + } + let samples: Vec = recent_frames + .into_iter() + .rev() + .map(|frame| frame.iter().sum::() / frame.len() as f64) + .collect(); + let n = samples.len(); + if n < 16 { + return 0.0; + } + + let mean = samples.iter().sum::() / n as f64; + let upper_hz = 4.0_f64.min(sample_rate_hz * 0.45); + if upper_hz < 0.1 { + return 0.0; + } + + let mut powers = Vec::new(); + for bin in 1..=(n / 2) { + let frequency_hz = bin as f64 * sample_rate_hz / n as f64; + if !(0.1..=upper_hz).contains(&frequency_hz) { + continue; + } + let mut real = 0.0; + let mut imag = 0.0; + let angle_step = 2.0 * std::f64::consts::PI * bin as f64 / n as f64; + let (step_sin, step_cos) = angle_step.sin_cos(); + let mut angle_cos = 1.0; + let mut angle_sin = 0.0; + for (index, sample) in samples.iter().enumerate() { + let window = + 0.5 * (1.0 - (2.0 * std::f64::consts::PI * index as f64 / (n - 1) as f64).cos()); + let value = (sample - mean) * window; + real += value * angle_cos; + imag -= value * angle_sin; + let next_cos = angle_cos * step_cos - angle_sin * step_sin; + angle_sin = angle_sin * step_cos + angle_cos * step_sin; + angle_cos = next_cos; + } + powers.push((frequency_hz, real * real + imag * imag)); + } + if powers.is_empty() { + return 0.0; + } + let mean_power = powers.iter().map(|(_, power)| power).sum::() / powers.len() as f64; + let (peak_hz, peak_power) = powers + .into_iter() + .max_by(|left, right| { + left.1 + .partial_cmp(&right.1) + .unwrap_or(std::cmp::Ordering::Equal) + }) + .unwrap_or((0.0, 0.0)); + if mean_power <= f64::EPSILON || peak_power / mean_power < 2.0 { + 0.0 + } else { + peak_hz + } +} + +#[cfg(test)] +mod temporal_frequency_tests { + use super::*; + + #[test] + fn recovers_temporal_frequency_in_hertz() { + let sample_rate_hz = 20.0; + let target_hz = 1.2; + let mut history = VecDeque::new(); + for sample in 0..100 { + let t = sample as f64 / sample_rate_hz; + let value = 20.0 + 2.0 * (2.0 * std::f64::consts::PI * target_hz * t).sin(); + history.push_back(vec![value; 56]); + } + let measured = estimate_temporal_dominant_frequency_hz(&history, sample_rate_hz); + assert!((measured - target_hz).abs() <= 0.21, "measured {measured} Hz"); + } + + #[test] + fn abstains_on_short_or_flat_history() { + let short = VecDeque::from(vec![vec![20.0; 56]; 15]); + assert_eq!(estimate_temporal_dominant_frequency_hz(&short, 20.0), 0.0); + let flat = VecDeque::from(vec![vec![20.0; 56]; 100]); + assert_eq!(estimate_temporal_dominant_frequency_hz(&flat, 20.0), 0.0); + let mut corrupt = flat; + corrupt[50][4] = f64::NAN; + assert_eq!(estimate_temporal_dominant_frequency_hz(&corrupt, 20.0), 0.0); + } + + #[test] + fn temporal_frequency_p95_stays_below_server_budget() { + let sample_rate_hz = 50.0; + let mut history = VecDeque::new(); + for sample in 0..128 { + let t = sample as f64 / sample_rate_hz; + let value = 20.0 + 2.0 * (2.0 * std::f64::consts::PI * 1.2 * t).sin(); + history.push_back(vec![value; 256]); + } + let mut timings = Vec::with_capacity(1_000); + for _ in 0..1_000 { + let start = std::time::Instant::now(); + std::hint::black_box(estimate_temporal_dominant_frequency_hz( + std::hint::black_box(&history), + sample_rate_hz, + )); + timings.push(start.elapsed()); + } + timings.sort_unstable(); + let p95 = timings[949]; + eprintln!("temporal dominant frequency p95: {p95:?}"); + assert!(p95 < std::time::Duration::from_millis(10), "p95 {p95:?}"); + } +} + /// Person count is supporting evidence, never an independent occupancy claim. /// Return zero and mark invalid for contradictory or out-of-range firmware. fn sanitize_edge_person_count(presence: bool, raw: u8) -> (u8, bool) { @@ -469,14 +604,7 @@ pub fn extract_features_from_frame( 0.0 }; - let peak_idx = frame - .amplitudes - .iter() - .enumerate() - .max_by(|a, b| a.1.partial_cmp(b.1).unwrap_or(std::cmp::Ordering::Equal)) - .map(|(i, _)| i) - .unwrap_or(0); - let dominant_freq_hz = peak_idx as f64 * 0.05; + let dominant_freq_hz = estimate_temporal_dominant_frequency_hz(frame_history, sample_rate_hz); let threshold = mean_amp * 1.2; let change_points = frame @@ -485,7 +613,9 @@ pub fn extract_features_from_frame( .filter(|w| (w[0] < threshold) != (w[1] < threshold)) .count(); - let temporal_motion_score = if let Some(prev_frame) = frame_history.back() { + // The caller appends the current frame before extraction, so the true + // predecessor is the second item from the back. + let temporal_motion_score = if let Some(prev_frame) = frame_history.iter().rev().nth(1) { let n_cmp = n_sub.min(prev_frame.len()); if n_cmp > 0 { let diff_energy: f64 = (0..n_cmp) @@ -636,13 +766,18 @@ pub fn adaptive_override( .back() .map(|v| v.as_slice()) .unwrap_or(&[]); + let dominant_frequency = adaptive_classifier::compatible_dominant_frequency( + model.version, + features.dominant_freq_hz, + amps, + ); let feat_arr = adaptive_classifier::features_from_runtime( &serde_json::json!({ "variance": features.variance, "motion_band_power": features.motion_band_power, "breathing_band_power": features.breathing_band_power, "spectral_power": features.spectral_power, - "dominant_freq_hz": features.dominant_freq_hz, + "dominant_freq_hz": dominant_frequency, "change_points": features.change_points, "mean_rssi": features.mean_rssi, }), diff --git a/v2/crates/wifi-densepose-sensing-server/src/field_bridge.rs b/v2/crates/wifi-densepose-sensing-server/src/field_bridge.rs index 5b7f588fd..4ba83b0e7 100644 --- a/v2/crates/wifi-densepose-sensing-server/src/field_bridge.rs +++ b/v2/crates/wifi-densepose-sensing-server/src/field_bridge.rs @@ -40,6 +40,106 @@ const ENERGY_THRESH_3: f64 = 25.0; /// reliable — genuine higher counts come from the multistatic fusion path. const MAX_SINGLE_LINK_OCCUPANCY: usize = 3; +/// Low-authority comparison of the current source window with a restored +/// empty-room bootstrap image. This is background conformance only and cannot +/// authorize presence, person count, pose, identity, or vital signs. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct BootstrapBackgroundMatch { + pub matches_empty: bool, + pub score: Option, + pub normalized_residual_z: Option, + pub maturity: f64, + pub reliable: bool, + pub residual_energy: f64, + pub residual_energy_threshold: f64, + pub window_size: usize, + pub reference_window_count: usize, +} + +fn mean_frame(frames: &[Vec]) -> Option> { + let width = frames.first()?.len(); + if width == 0 { + return None; + } + let mut mean = vec![0.0_f64; width]; + let mut count = 0_usize; + for frame in frames { + if frame.len() != width { + continue; + } + for (sum, value) in mean.iter_mut().zip(frame.iter()) { + *sum += value; + } + count += 1; + } + if count == 0 { + return None; + } + for value in &mut mean { + *value /= count as f64; + } + Some(mean) +} + +fn perturbation_occupancy(field: &FieldModel, frames: &[Vec]) -> Option { + let adaptive_threshold = field.empty_room_residual_energy_threshold(); + let frame = match adaptive_threshold { + Some(_) => mean_frame(frames)?, + None => frames.first()?.clone(), + }; + let perturbation = field.extract_perturbation(&[frame]).ok()?; + let count = match adaptive_threshold { + Some(threshold) => { + let empty_threshold = threshold.max(1.0); + if perturbation.total_energy > empty_threshold * 25.0 { + 3 + } else if perturbation.total_energy > empty_threshold * 12.0 { + 2 + } else if perturbation.total_energy > empty_threshold { + 1 + } else { + 0 + } + } + None => { + if perturbation.total_energy > ENERGY_THRESH_3 { + 3 + } else if perturbation.total_energy > ENERGY_THRESH_2 { + 2 + } else if perturbation.total_energy > 1.0 { + 1 + } else { + 0 + } + } + }; + Some(count) +} + +/// Provenance for a count produced by the calibrated field model. Callers +/// that attach calibration evidence must never substitute the score heuristic +/// when the calibrated path cannot evaluate the observation. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum CalibratedOccupancyMethod { + Eigenvalue, + PerturbationEnergy, +} + +impl CalibratedOccupancyMethod { + pub const fn wire_name(self) -> &'static str { + match self { + Self::Eigenvalue => "field_model_eigenvalue_v1", + Self::PerturbationEnergy => "field_model_perturbation_energy_v1", + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct CalibratedOccupancy { + pub person_count: usize, + pub method: CalibratedOccupancyMethod, +} + /// Create a FieldModelConfig for single-link mode (one ESP32 node = one link). /// This avoids the DimensionMismatch error when feeding single-frame observations. pub fn single_link_config() -> FieldModelConfig { @@ -49,6 +149,18 @@ pub fn single_link_config() -> FieldModelConfig { } } +/// Resolve the model status at the observation wall clock. `FieldModel::status` +/// records the state at collection or restore time and does not advance as a +/// long-running server crosses the stale and expiry boundaries. +pub fn calibration_status_at(field: &FieldModel, observed_at_us: u64) -> CalibrationStatus { + match field.status() { + CalibrationStatus::Uncalibrated | CalibrationStatus::Collecting => field.status(), + CalibrationStatus::Fresh | CalibrationStatus::Stale | CalibrationStatus::Expired => { + field.check_freshness(observed_at_us) + } + } +} + /// Estimate occupancy using the FieldModel when calibrated, falling back /// to the score-based heuristic otherwise. /// @@ -58,16 +170,17 @@ pub fn single_link_config() -> FieldModelConfig { pub fn occupancy_or_fallback( field: &FieldModel, frame_history: &VecDeque>, + observed_at_us: u64, smoothed_score: f64, prev_count: usize, ) -> usize { - match field.status() { + match calibration_status_at(field, observed_at_us) { CalibrationStatus::Fresh | CalibrationStatus::Stale => { let frames: Vec> = frame_history .iter() .rev() .take(OCCUPANCY_WINDOW) - .cloned() + .map(|frame| CALIB_NORMALIZER.resample_to_canonical(frame)) .collect(); if frames.is_empty() { @@ -87,51 +200,119 @@ pub fn occupancy_or_fallback( // Fallback: perturbation energy thresholds. // FieldModel expects [n_links][n_subcarriers] — we use n_links=1. - let observation = vec![frames[0].clone()]; - match field.extract_perturbation(&observation) { - Ok(perturbation) => { - if perturbation.total_energy > ENERGY_THRESH_3 { - 3 - } else if perturbation.total_energy > ENERGY_THRESH_2 { - 2 - } else if perturbation.total_energy > 1.0 { - 1 - } else { - 0 - } - } - Err(_) => score_to_person_count(smoothed_score, prev_count), - } + perturbation_occupancy(field, &frames) + .unwrap_or_else(|| score_to_person_count(smoothed_score, prev_count)) } _ => score_to_person_count(smoothed_score, prev_count), } } -/// Feed the latest frame to the FieldModel during calibration collection. +/// Return true only when a restored low-authority bootstrap model has a full +/// runtime window and that model resolves the window as empty. This helper is +/// deliberately one-way: a bootstrap prior may suppress an obvious background +/// false positive, but cannot authorize presence or vital signs. +pub fn bootstrap_empty_prior_applies( + field: &FieldModel, + frame_history: &VecDeque>, + observed_at_us: u64, +) -> bool { + bootstrap_background_match(field, frame_history, observed_at_us) + .is_some_and(|result| result.matches_empty) +} + +/// Score the current canonical runtime window against the learned empty-room +/// residual references. Legacy models without scalar references preserve the +/// binary match but intentionally return no score. +pub fn bootstrap_background_match( + field: &FieldModel, + frame_history: &VecDeque>, + observed_at_us: u64, +) -> Option { + if frame_history.len() < OCCUPANCY_WINDOW + || field.check_freshness(observed_at_us) != CalibrationStatus::Fresh + { + return None; + } + let frames: Vec> = frame_history + .iter() + .rev() + .take(OCCUPANCY_WINDOW) + .map(|frame| CALIB_NORMALIZER.resample_to_canonical(frame)) + .collect(); + let result = field.empty_room_match(&frames)?; + Some(BootstrapBackgroundMatch { + matches_empty: result.matches_empty, + score: result.score, + normalized_residual_z: result.normalized_residual_z, + maturity: result.maturity, + reliable: result.reliable, + residual_energy: result.residual_energy, + residual_energy_threshold: result.residual_energy_threshold, + window_size: result.window_size, + reference_window_count: result.reference_window_count, + }) +} + +/// Produce a fail-closed occupancy result from a fresh calibrated model. +pub fn calibrated_occupancy( + field: &FieldModel, + frame_history: &VecDeque>, + observed_at_us: u64, +) -> Option { + if field.check_freshness(observed_at_us) != CalibrationStatus::Fresh { + return None; + } + let frames: Vec> = frame_history + .iter() + .rev() + .take(OCCUPANCY_WINDOW) + .map(|frame| CALIB_NORMALIZER.resample_to_canonical(frame)) + .collect(); + if frames.is_empty() { + return None; + } + if let Ok(person_count) = field.estimate_occupancy(&frames) { + return Some(CalibratedOccupancy { + person_count: person_count.min(MAX_SINGLE_LINK_OCCUPANCY), + method: CalibratedOccupancyMethod::Eigenvalue, + }); + } + Some(CalibratedOccupancy { + person_count: perturbation_occupancy(field, &frames)?, + method: CalibratedOccupancyMethod::PerturbationEnergy, + }) +} + +/// Feed the current CSI frame to the FieldModel during calibration. /// /// Acts while the model is `Uncalibrated` or `Collecting`. The first fed frame /// flips a freshly-started (`Uncalibrated`) model to `Collecting` inside /// `feed_calibration`; without accepting the `Uncalibrated` state here the two /// gates deadlock and the frame count never leaves 0 (calibration/start yields -/// an `Uncalibrated` model that nothing would ever advance). Wraps the latest -/// frame as a single-link observation (n_links=1) and feeds it. -pub fn maybe_feed_calibration(field: &mut FieldModel, frame_history: &VecDeque>) { +/// an `Uncalibrated` model that nothing would ever advance). Edge-vitals +/// packets have no CSI observation and must never call this helper. The caller +/// owns the stateful sequence admission gate. Wraps the current frame as a +/// single-link observation (n_links=1) and feeds it. +pub fn maybe_feed_calibration(field: &mut FieldModel, amplitudes: &[f64]) -> bool { if !matches!( field.status(), CalibrationStatus::Uncalibrated | CalibrationStatus::Collecting - ) { - return; + ) || amplitudes.is_empty() + { + return false; } - if let Some(latest) = frame_history.back() { - // Resample the raw amplitude vector onto the FieldModel's canonical - // 56-tone grid before feeding. Real HT40 nodes stream 128-wide frames; - // feeding those raw made every `feed_calibration` fail DimensionMismatch - // (swallowed at debug level), pinning frame_count at 0 even after the - // status-gate deadlock was fixed. Single-link observation: [1][56]. - let canonical = CALIB_NORMALIZER.resample_to_canonical(latest); - let observations = vec![canonical]; - if let Err(e) = field.feed_calibration(&observations) { + // Resample the raw amplitude vector onto the FieldModel's canonical + // 56-tone grid before feeding. Real HT40 nodes stream 128-wide frames; + // feeding those raw made every `feed_calibration` fail DimensionMismatch + // (swallowed at debug level), pinning frame_count at 0 even after the + // status-gate deadlock was fixed. Single-link observation: [1][56]. + let canonical = CALIB_NORMALIZER.resample_to_canonical(amplitudes); + let observations = vec![canonical]; + match field.feed_calibration(&observations) { + Ok(()) => true, + Err(e) => { tracing::debug!("FieldModel calibration feed: {e}"); + false } } } @@ -174,6 +355,33 @@ pub fn parse_node_positions(input: &str) -> Vec<[f32; 3]> { mod tests { use super::*; + fn fresh_test_model() -> FieldModel { + let config = FieldModelConfig { + n_links: 1, + n_subcarriers: 56, + n_modes: 3, + min_calibration_frames: 10, + min_calibration_duration_s: 0.0, + baseline_expiry_s: 2.0, + }; + let mut field = FieldModel::new(config).expect("field model"); + for index in 0..20 { + let phase = index as f64 * 0.1; + let observation = vec![(0..56) + .map(|subcarrier| { + 1.0 + subcarrier as f64 * 0.01 + (phase + subcarrier as f64 * 0.03).sin() * 0.01 + }) + .collect()]; + field + .feed_calibration(&observation) + .expect("calibration frame"); + } + field + .finalize_calibration(1_000_000, 7) + .expect("finalize calibration"); + field + } + #[test] fn test_parse_node_positions() { let positions = parse_node_positions("0,0,1.5;3,0,1.5;1.5,3,1.5"); @@ -215,10 +423,7 @@ mod tests { // n_subcarriers defaults to 56; one single-link frame of that width. let frame = vec![0.5_f64; 56]; - let mut history: VecDeque> = VecDeque::new(); - history.push_back(frame); - - maybe_feed_calibration(&mut field, &history); + assert!(maybe_feed_calibration(&mut field, &frame)); assert_eq!( field.status(), @@ -231,8 +436,8 @@ mod tests { "frame count must advance past 0" ); - // Subsequent frames keep accumulating while Collecting. - maybe_feed_calibration(&mut field, &history); + // A subsequent unique frame keeps accumulating while Collecting. + assert!(maybe_feed_calibration(&mut field, &frame)); assert_eq!(field.calibration_frame_count(), 2); } @@ -247,10 +452,7 @@ mod tests { // 128-wide frame (HT40), NOT the model's 56 — would DimensionMismatch raw. let wide = vec![0.5_f64; 128]; - let mut history: VecDeque> = VecDeque::new(); - history.push_back(wide); - - maybe_feed_calibration(&mut field, &history); + assert!(maybe_feed_calibration(&mut field, &wide)); assert_eq!( field.status(), @@ -263,4 +465,129 @@ mod tests { "wide frame must accumulate, not be silently dropped" ); } + + #[test] + fn maybe_feed_calibration_rejects_an_empty_observation() { + let mut field = FieldModel::new(single_link_config()).expect("field model"); + assert!(!maybe_feed_calibration(&mut field, &[])); + assert_eq!(field.calibration_frame_count(), 0); + } + + #[test] + fn calibrated_occupancy_is_fresh_model_only_and_normalizes_runtime_frames() { + let field = fresh_test_model(); + let mut history = VecDeque::new(); + history.push_back(vec![1.02; 128]); + let result = + calibrated_occupancy(&field, &history, 1_500_000).expect("fresh calibrated inference"); + assert!(result.person_count <= MAX_SINGLE_LINK_OCCUPANCY); + assert!(matches!( + result.method, + CalibratedOccupancyMethod::Eigenvalue | CalibratedOccupancyMethod::PerturbationEnergy + )); + assert_eq!(calibrated_occupancy(&field, &history, 4_000_000), None); + assert_eq!( + calibrated_occupancy(&field, &VecDeque::new(), 1_500_000), + None + ); + } + + #[test] + fn occupancy_falls_back_after_expiry_without_a_process_restart() { + let field = fresh_test_model(); + let baseline = field.modes().unwrap().baseline[0].clone(); + let history: VecDeque> = + (0..OCCUPANCY_WINDOW).map(|_| baseline.clone()).collect(); + + assert_eq!(field.status(), CalibrationStatus::Fresh); + assert_eq!( + calibration_status_at(&field, 1_500_000), + CalibrationStatus::Fresh + ); + assert_eq!( + occupancy_or_fallback(&field, &history, 1_500_000, 0.0, 0), + 0, + "a fresh field model may resolve its learned baseline as empty" + ); + + assert_eq!(field.status(), CalibrationStatus::Fresh); + assert_eq!( + calibration_status_at(&field, 4_000_000), + CalibrationStatus::Expired + ); + assert_eq!( + occupancy_or_fallback(&field, &history, 4_000_000, 0.0, 0), + score_to_person_count(0.0, 0), + "an expired in-memory model must fall back even though its cached status is fresh" + ); + } + + #[test] + fn adaptive_runtime_reference_scores_empty_and_rejects_shift() { + let config = FieldModelConfig { + n_links: 1, + n_subcarriers: 56, + n_modes: 3, + min_calibration_frames: 500, + min_calibration_duration_s: 0.0, + baseline_expiry_s: 60.0, + }; + let mut field = FieldModel::new(config).expect("field model"); + let mut calibration = Vec::new(); + for frame_index in 0..600 { + let time = frame_index as f64 * 0.071; + let frame: Vec = (0..56) + .map(|subcarrier| { + let carrier = subcarrier as f64; + 18.0 + carrier * 0.08 + + (time + carrier * 0.13).sin() * 0.8 + + (time * 0.37 + carrier * 0.031).cos() * 0.35 + }) + .collect(); + field.feed_calibration(&[frame.clone()]).unwrap(); + calibration.push(frame); + } + field.finalize_calibration(1_000_000, 7).unwrap(); + + let empty_history: VecDeque> = calibration[550..600].iter().cloned().collect(); + let empty = calibrated_occupancy(&field, &empty_history, 1_500_000) + .expect("adaptive perturbation result"); + assert_eq!(empty.person_count, 0); + let background = bootstrap_background_match(&field, &empty_history, 1_500_000) + .expect("background score"); + assert!(background.matches_empty); + assert_eq!(background.reference_window_count, 12); + assert!(background.reliable); + assert_eq!(background.maturity, 0.6); + assert!(background.normalized_residual_z.is_some()); + assert!(background + .score + .is_some_and(|score| (0.5..=1.0).contains(&score))); + + let short_history: VecDeque> = empty_history.iter().take(49).cloned().collect(); + assert!(!bootstrap_empty_prior_applies( + &field, + &short_history, + 1_500_000 + )); + + let shifted_history: VecDeque> = calibration[550..600] + .iter() + .map(|frame| { + frame + .iter() + .enumerate() + .map(|(index, value)| value + if index % 3 == 0 { 8.0 } else { -5.0 }) + .collect() + }) + .collect(); + let shifted = calibrated_occupancy(&field, &shifted_history, 1_500_000) + .expect("shifted perturbation result"); + assert!(shifted.person_count >= 1); + let shifted_background = bootstrap_background_match(&field, &shifted_history, 1_500_000) + .expect("shifted background result"); + assert!(!shifted_background.matches_empty); + assert_eq!(shifted_background.score, Some(0.0)); + assert!(shifted_background.reliable); + } } diff --git a/v2/crates/wifi-densepose-sensing-server/src/lib.rs b/v2/crates/wifi-densepose-sensing-server/src/lib.rs index 93d00f118..0cf0214f1 100644 --- a/v2/crates/wifi-densepose-sensing-server/src/lib.rs +++ b/v2/crates/wifi-densepose-sensing-server/src/lib.rs @@ -9,6 +9,7 @@ //! - Real-time CSI introspection / low-latency tap (`introspection`, ADR-099) pub mod bearer_auth; +pub mod bootstrap_baseline; pub mod browser_session; pub mod ws_ticket; pub mod cli; diff --git a/v2/crates/wifi-densepose-sensing-server/src/main.rs b/v2/crates/wifi-densepose-sensing-server/src/main.rs index 3c6ca0428..b843efce2 100644 --- a/v2/crates/wifi-densepose-sensing-server/src/main.rs +++ b/v2/crates/wifi-densepose-sensing-server/src/main.rs @@ -39,11 +39,17 @@ use wifi_densepose_sensing_server::{ dataset, embedding, error_response, graph_transformer, rufield_surface, semconv, telemetry, trainer, }; +use wifi_densepose_sensing_server::bootstrap_baseline::{ + self, BootstrapBaselineMetadata, BootstrapValidationSample, + BOOTSTRAP_VALIDATION_MIN_SPACING_MS, BOOTSTRAP_VALIDATION_SAMPLES, + BOOTSTRAP_VALIDATION_SAMPLE_TIMEOUT_MS, +}; // ADR-295 / ADR-297: canonical provenance state + per-node/room inference. use wifi_densepose_sensing_server::inference::{fuse_room, NodeInference, RoomInference}; use wifi_densepose_sensing_server::provenance::SourceState; use ruvector_mincut::{DynamicMinCut, MinCutBuilder}; +use rand::{rngs::OsRng, RngCore}; use std::collections::{BTreeMap, HashMap, VecDeque}; use std::net::SocketAddr; use std::path::PathBuf; @@ -142,6 +148,11 @@ struct Args { #[arg(long, env = "RUVIEW_INSTALLATION_ID")] installation_id: Option, + /// Private server state directory used for runtime configuration and the + /// installation bound empty room bootstrap prior. + #[arg(long, default_value = "data", env = "RUVIEW_DATA_DIR")] + data_dir: PathBuf, + /// Human-readable local service name shown by commissioning clients. #[arg( long, @@ -318,6 +329,59 @@ impl Esp32Frame { } } +/// Exact CSI symbol grid used by one field model. Subcarrier count alone is +/// insufficient because HT-LTF and HE-LTF use different tone layouts. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +struct CsiGridKey { + n_subcarriers: u16, + ppdu_type: u8, +} + +impl CsiGridKey { + fn from_frame(frame: &Esp32Frame) -> Self { + Self { + n_subcarriers: frame.n_subcarriers, + ppdu_type: frame.ppdu_type.to_byte(), + } + } + + fn as_bootstrap_grid(self) -> bootstrap_baseline::BootstrapCsiGrid { + bootstrap_baseline::BootstrapCsiGrid { + n_subcarriers: self.n_subcarriers, + ppdu_type: self.ppdu_type, + } + } + + fn from_bootstrap_grid(grid: bootstrap_baseline::BootstrapCsiGrid) -> Self { + Self { + n_subcarriers: grid.n_subcarriers, + ppdu_type: grid.ppdu_type, + } + } +} + +#[derive(Debug, Clone, Copy)] +struct RawGridObservation { + grid: CsiGridKey, + observed_at: std::time::Instant, +} + +#[derive(Debug, Clone, Copy, Serialize)] +struct CalibrationGridEvidence { + sample_count: usize, + span_s: f64, + rate_hz: f64, + max_gap_s: f64, + latest_age_s: f64, +} + +#[derive(Debug, Clone, Copy)] +struct CalibrationGridBinding { + source_node_id: u8, + grid: CsiGridKey, + evidence: Option, +} + /// Sensing update broadcast to WebSocket clients #[derive(Debug, Clone, Serialize, Deserialize)] struct SensingUpdate { @@ -491,6 +555,57 @@ fn classification_from_room(room: &RoomInference) -> ClassificationInfo { } } +/// Minimum time a candidate room-level classification must be observed +/// *consistently* before the debounced room state actually flips to it. +/// +/// `fuse_room` recomputes its plurality vote fresh every cycle with no +/// memory of its own — unlike each node's own classification, which is +/// individually debounced (`smooth_and_classify_node`). Confirmed live +/// (2026-08-28) as a real source of "flapping": with per-node confidences +/// sitting near the classification boundary (44%/83%/53% observed), whichever +/// 2 of 3 nodes happen to agree can flip cycle-to-cycle even while each +/// node's own reading is individually stable. This adds the missing +/// room-level hysteresis, same idea as the per-node debounce, one layer up. +const ROOM_DEBOUNCE_DURATION_SECS: f64 = 1.5; + +/// Debounced counterpart to [`classification_from_room`] — apply this at +/// live call sites instead, so the room's `presence`/`motion_level` only +/// flips after `raw`'s new candidate class has been observed continuously +/// for [`ROOM_DEBOUNCE_DURATION_SECS`], not on the very first cycle it wins +/// `fuse_room`'s plurality vote. `confidence` is passed through un-debounced +/// (it's a continuous value, not a discrete state, so there's nothing to +/// flap) — only `motion_level`/`presence` are held back. +fn debounce_room_classification(state: &mut AppStateInner, raw: &RoomInference) -> ClassificationInfo { + let candidate = if raw.classification == "unavailable" { + "absent" + } else { + raw.classification.as_str() + }; + let now = std::time::Instant::now(); + + if candidate == state.room_debounced_level { + // Already stable in this state — reset any in-flight candidate. + state.room_debounce_candidate = candidate.to_string(); + state.room_debounce_since = None; + } else if candidate == state.room_debounce_candidate { + let since = *state.room_debounce_since.get_or_insert(now); + if now.duration_since(since).as_secs_f64() >= ROOM_DEBOUNCE_DURATION_SECS { + state.room_debounced_level = candidate.to_string(); + state.room_debounce_since = None; + } + } else { + // New candidate — restart the hold timer. + state.room_debounce_candidate = candidate.to_string(); + state.room_debounce_since = Some(now); + } + + ClassificationInfo { + motion_level: state.room_debounced_level.clone(), + presence: state.room_debounced_level != "absent", + confidence: raw.confidence, + } +} + /// ADR-297 — the window a node may be silent before it stops contributing to /// the fused room aggregate (its entities go stale/unavailable rather than /// holding a frozen online value). Mirrors the 10 s active-node filter used to @@ -612,6 +727,98 @@ mod issue_1554_room_classification_tests { } } +#[cfg(test)] +mod debounce_room_classification_tests { + //! `fuse_room`'s plurality vote has no memory of its own — confirmed live + //! (2026-08-28) as a real "flapping" source when per-node confidences sit + //! near the classification boundary: whichever 2 of 3 nodes happen to + //! agree on a given cycle can flip the room result even while each + //! node's own reading is individually stable. `debounce_room_classification` + //! adds the missing room-level hysteresis (same idea as each node's own + //! debounce, one layer up). + use super::{debounce_room_classification, AppStateInner, RoomInference}; + use std::time::{Duration, Instant}; + + fn room(classification: &str, confidence: f64) -> RoomInference { + RoomInference { + classification: classification.to_string(), + confidence, + contributing_nodes: 3, + } + } + + #[test] + fn single_cycle_candidate_does_not_flip_immediately() { + let mut state = AppStateInner::minimal(); + let c = debounce_room_classification(&mut state, &room("present_still", 0.6)); + assert_eq!(c.motion_level, "absent", "one cycle must not be enough to flip"); + assert!(!c.presence); + } + + #[test] + fn repeated_but_recent_candidate_still_does_not_flip() { + let mut state = AppStateInner::minimal(); + debounce_room_classification(&mut state, &room("present_still", 0.6)); + // Second cycle, essentially no time elapsed — still under the hold duration. + let c = debounce_room_classification(&mut state, &room("present_still", 0.6)); + assert_eq!(c.motion_level, "absent"); + } + + #[test] + fn candidate_sustained_past_hold_duration_flips() { + let mut state = AppStateInner::minimal(); + // Simulate "present_still" having already been the candidate for 2s + // (> ROOM_DEBOUNCE_DURATION_SECS = 1.5s) without needing to sleep in + // the test. + state.room_debounce_candidate = "present_still".to_string(); + state.room_debounce_since = Some(Instant::now() - Duration::from_secs(2)); + + let c = debounce_room_classification(&mut state, &room("present_still", 0.7)); + assert_eq!(c.motion_level, "present_still"); + assert!(c.presence); + } + + #[test] + fn flapping_candidate_resets_the_hold_timer() { + let mut state = AppStateInner::minimal(); + // "present_still" has been pending 1s (not yet enough to flip). + state.room_debounce_candidate = "present_still".to_string(); + state.room_debounce_since = Some(Instant::now() - Duration::from_secs(1)); + + // A different candidate arrives — this is exactly the flapping + // pattern (2-of-3 node agreement flipping cycle to cycle). The hold + // timer must restart for the new candidate, not carry over. + let c = debounce_room_classification(&mut state, &room("active", 0.5)); + assert_eq!(c.motion_level, "absent", "still debounced to the old stable state"); + assert_eq!(state.room_debounce_candidate, "active"); + assert!( + state.room_debounce_since.unwrap().elapsed() < Duration::from_millis(100), + "hold timer must have restarted for the new candidate" + ); + } + + #[test] + fn already_stable_state_reports_immediately_without_waiting() { + let mut state = AppStateInner::minimal(); + state.room_debounced_level = "present_moving".to_string(); + let c = debounce_room_classification(&mut state, &room("present_moving", 0.9)); + assert_eq!(c.motion_level, "present_moving", "no need to re-debounce an already-stable state"); + assert!(c.presence); + } + + #[test] + fn unavailable_room_debounces_toward_absent_like_any_other_candidate() { + let mut state = AppStateInner::minimal(); + state.room_debounced_level = "present_still".to_string(); + state.room_debounce_candidate = "absent".to_string(); + state.room_debounce_since = Some(Instant::now() - Duration::from_secs(2)); + + let c = debounce_room_classification(&mut state, &RoomInference::unavailable()); + assert_eq!(c.motion_level, "absent"); + assert!(!c.presence); + } +} + #[derive(Debug, Clone, Serialize, Deserialize)] struct SignalField { grid_size: [usize; 3], @@ -695,6 +902,9 @@ struct NodeState { latest_sync: Option, /// Last time a sync packet from this node was received (for staleness). latest_sync_at: Option, + /// Arrival time of the newest grid-admitted raw CSI frame. Edge-vitals + /// packets intentionally do not refresh this clock. + latest_accepted_csi_at: Option, /// Sequence number of the newest CSI frame admitted to `frame_history`. /// Kept alongside the history so multistatic fusion can timestamp the /// exact sample it consumes, rather than the host's UDP arrival time. @@ -729,6 +939,15 @@ struct NodeState { /// the two symbol grids in `frame_history` corrupts variance/baseline /// statistics. See [`NodeState::accept_grid`]. active_grid: Option<(u16, wifi_densepose_hardware::PpduType)>, + /// Header-only recent raw grid observations used to choose one stable, + /// comparable calibration stream. No CSI amplitudes are retained here. + raw_grid_observations: VecDeque, + /// Runtime history for the exact grid bound into the active field model. + /// This must not reuse `frame_history`, whose independent inference grid + /// may be wider and therefore not bin-comparable. + field_model_history: VecDeque>, + field_model_latest_sequence: Option, + field_model_latest_seen: Option, } /// Default EMA alpha for temporal keypoint smoothing (RuVector Phase 2). @@ -752,6 +971,14 @@ const NOVELTY_HISTORY_CAPACITY: usize = 64; /// ADR-084 Pass 3 — feature-vector schema version. Bump on changes to /// subcarrier ordering / normalisation so banks reject stale data. const NOVELTY_SKETCH_VERSION: u16 = 1; +/// Recent header-only evidence window used to choose a stable field-model +/// stream. The rate floor is 20% above the 1,000 / 600 s completion minimum. +const CALIBRATION_GRID_EVIDENCE_WINDOW: std::time::Duration = + std::time::Duration::from_secs(20); +const CALIBRATION_GRID_EVIDENCE_CAPACITY: usize = 1_024; +const CALIBRATION_GRID_MIN_SPAN_S: f64 = 10.0; +const CALIBRATION_GRID_MIN_RATE_HZ: f64 = 2.0; +const CALIBRATION_GRID_MAX_GAP_S: f64 = 5.0; /// Lower plausibility floor (seconds) for a CSI inter-frame delta. /// @@ -761,10 +988,10 @@ const NOVELTY_SKETCH_VERSION: u16 = 1; /// bursts whose intra-burst arrival deltas are tens of microseconds apart — /// a 36 µs delta yields `1/dt ≈ 27 kHz`, which the old `< 1 s` guard let /// straight into the EMA and inflated `csi_fps_ema` by 1–3 orders of -/// magnitude (issue #1180). We reject any delta implying more than 200 fps -/// (4× the physical ceiling, leaving slack for benign arrival jitter); such -/// deltas are burst artifacts, not distinct production intervals. +/// magnitude (issue #1180). We reject sub-5 ms deltas as burst artifacts and +/// cap accepted estimates to the firmware's 50 fps physical ceiling. pub(crate) const MIN_PLAUSIBLE_CSI_DT_SEC: f64 = 0.005; +pub(crate) const MAX_PHYSICAL_CSI_FPS: f64 = 50.0; /// ADR-110 iter 18 — EMA update for per-node CSI fps tracking. /// @@ -780,7 +1007,7 @@ pub(crate) fn update_csi_fps_ema(prev_fps: f64, dt_sec: f64) -> Option { if !(dt_sec >= MIN_PLAUSIBLE_CSI_DT_SEC && dt_sec < 1.0) { return None; } - let instantaneous = 1.0 / dt_sec; + let instantaneous = (1.0 / dt_sec).min(MAX_PHYSICAL_CSI_FPS); // y[n] = y[n-1] + (x - y[n-1]) / 8 Some(prev_fps + (instantaneous - prev_fps) / 8.0) } @@ -829,6 +1056,15 @@ mod fps_ema_tests { assert!(update_csi_fps_ema(40.0, 0.005).is_some()); } + #[test] + fn accepted_burst_edge_is_capped_to_firmware_ceiling() { + let mut fps = 50.0; + for _ in 0..32 { + fps = update_csi_fps_ema(fps, 0.005).unwrap(); + } + assert!(fps <= 50.0, "reported {fps} Hz above firmware ceiling"); + } + #[test] fn burst_interleaved_with_nominal_stays_in_band() { // A true ~40 fps node whose frames arrive in sub-ms bursts: feeding @@ -850,6 +1086,18 @@ mod fps_ema_tests { } impl NodeState { + /// Measured sample rate only after enough inter-frame deltas have warmed + /// the EMA. The physical ceiling is enforced again at the consumption + /// boundary so restored or malformed state cannot overclock DSP math. + fn measured_sample_rate_hz(&self) -> Option { + (self.csi_fps_samples >= 5 && self.csi_fps_ema.is_finite()) + .then(|| self.csi_fps_ema.clamp(1.0, MAX_PHYSICAL_CSI_FPS)) + } + + fn effective_sample_rate_hz(&self) -> f64 { + self.measured_sample_rate_hz().unwrap_or(20.0) + } + /// ADR-110 §A0.12 timestamp recovery: given a CSI frame's node-local /// `esp_timer_get_time()` snapshot, return the mesh-aligned epoch /// computed from this node's most recent sync packet — or `None` @@ -897,7 +1145,7 @@ impl NodeState { // samples; until then fall back to the 20 Hz firmware ceiling. The // §A0.12 capture showed real bench fps ≈ 10, so the measured value // is significantly more accurate than the constant fallback. - let fps = if self.csi_fps_samples >= 5 { self.csi_fps_ema } else { 20.0 }; + let fps = self.effective_sample_rate_hz(); Some(sync.mesh_aligned_us_for_sequence(frame_sequence, fps)) } @@ -945,7 +1193,7 @@ impl NodeState { is_valid: sync.flags.is_valid, smoothed: sync.flags.smoothed_used, sequence: sync.sequence, - csi_fps_ema: self.csi_fps_ema, + csi_fps_ema: self.effective_sample_rate_hz(), csi_fps_samples: self.csi_fps_samples, staleness_ms: self.latest_sync_at.map(|t| t.elapsed().as_millis() as u64), }) @@ -986,6 +1234,7 @@ impl NodeState { sync_valid: bool, now: std::time::Instant, ) -> bool { + self.latest_accepted_csi_at = Some(now); self.latest_csi_sequence = Some(sequence); self.latest_csi_sync_valid = sync_valid; self.observe_csi_frame_arrival(now) @@ -1009,12 +1258,13 @@ impl NodeState { hr_buffer: VecDeque::with_capacity(8), br_buffer: VecDeque::with_capacity(8), rssi_history: VecDeque::new(), - vital_detector: VitalSignDetector::new(10.0), + vital_detector: VitalSignDetector::new(20.0), latest_vitals: VitalSigns::default(), last_frame_time: None, edge_vitals: None, latest_sync: None, latest_sync_at: None, + latest_accepted_csi_at: None, latest_csi_sequence: None, latest_csi_sync_valid: false, csi_fps_ema: 20.0, @@ -1032,9 +1282,129 @@ impl NodeState { ), last_novelty_score: None, active_grid: None, + raw_grid_observations: VecDeque::with_capacity( + CALIBRATION_GRID_EVIDENCE_CAPACITY, + ), + field_model_history: VecDeque::with_capacity(FRAME_HISTORY_CAPACITY), + field_model_latest_sequence: None, + field_model_latest_seen: None, } } + fn observe_raw_grid(&mut self, grid: CsiGridKey, now: std::time::Instant) { + while self + .raw_grid_observations + .front() + .is_some_and(|sample| { + now.saturating_duration_since(sample.observed_at) + > CALIBRATION_GRID_EVIDENCE_WINDOW + }) + { + self.raw_grid_observations.pop_front(); + } + if self.raw_grid_observations.len() == CALIBRATION_GRID_EVIDENCE_CAPACITY { + self.raw_grid_observations.pop_front(); + } + self.raw_grid_observations.push_back(RawGridObservation { + grid, + observed_at: now, + }); + } + + fn calibration_grid_candidates( + &self, + now: std::time::Instant, + ) -> Vec<(CsiGridKey, CalibrationGridEvidence)> { + let mut grouped: Vec<(CsiGridKey, Vec)> = Vec::new(); + for sample in self.raw_grid_observations.iter().filter(|sample| { + now.saturating_duration_since(sample.observed_at) + <= CALIBRATION_GRID_EVIDENCE_WINDOW + }) { + if let Some((_, arrivals)) = grouped + .iter_mut() + .find(|(candidate, _)| *candidate == sample.grid) + { + arrivals.push(sample.observed_at); + } else { + grouped.push((sample.grid, vec![sample.observed_at])); + } + } + grouped + .into_iter() + .filter_map(|(grid, arrivals)| { + let first = *arrivals.first()?; + let last = *arrivals.last()?; + let span_s = last.saturating_duration_since(first).as_secs_f64(); + let latest_age_s = now.saturating_duration_since(last).as_secs_f64(); + let mut max_gap_s = latest_age_s; + for pair in arrivals.windows(2) { + max_gap_s = max_gap_s.max( + pair[1] + .saturating_duration_since(pair[0]) + .as_secs_f64(), + ); + } + let rate_hz = if span_s > 0.0 { + arrivals.len().saturating_sub(1) as f64 / span_s + } else { + 0.0 + }; + Some(( + grid, + CalibrationGridEvidence { + sample_count: arrivals.len(), + span_s, + rate_hz, + max_gap_s, + latest_age_s, + }, + )) + }) + .collect() + } + + fn select_calibration_grid( + &self, + now: std::time::Instant, + ) -> Option<(CsiGridKey, CalibrationGridEvidence)> { + self.calibration_grid_candidates(now) + .into_iter() + .filter(|(grid, evidence)| { + grid.ppdu_type <= 3 + && evidence.span_s >= CALIBRATION_GRID_MIN_SPAN_S + && evidence.rate_hz >= CALIBRATION_GRID_MIN_RATE_HZ + && evidence.max_gap_s < CALIBRATION_GRID_MAX_GAP_S + && evidence.latest_age_s < CALIBRATION_GRID_MAX_GAP_S + }) + .min_by(|(left_grid, left), (right_grid, right)| { + left.max_gap_s + .total_cmp(&right.max_gap_s) + .then_with(|| right.rate_hz.total_cmp(&left.rate_hz)) + .then_with(|| right_grid.n_subcarriers.cmp(&left_grid.n_subcarriers)) + .then_with(|| left_grid.ppdu_type.cmp(&right_grid.ppdu_type)) + }) + } + + fn clear_field_model_history(&mut self) { + self.field_model_history.clear(); + self.field_model_latest_sequence = None; + self.field_model_latest_seen = None; + } + + fn push_field_model_frame( + &mut self, + sequence: u32, + amplitudes: &[f64], + now: std::time::Instant, + ) { + self.field_model_history.push_back(amplitudes.to_vec()); + if self.field_model_history.len() > FRAME_HISTORY_CAPACITY { + self.field_model_history.pop_front(); + } + self.field_model_latest_sequence = Some(sequence); + self.field_model_latest_seen = Some(now); + } + /// ADR-110 / issue #1005 grid gate: decide whether a frame on `grid` /// may enter this node's feature path, and update `active_grid`. /// @@ -1195,7 +1565,7 @@ fn build_node_features( }, rssi_dbm: ns.rssi_history.back().copied().unwrap_or(0.0), last_seen_ms, - frame_rate_hz: 0.0, // Computed elsewhere; not yet plumbed here. + frame_rate_hz: ns.measured_sample_rate_hz().unwrap_or(0.0), stale, novelty_score: ns.last_novelty_score, } @@ -1410,6 +1780,15 @@ struct AppStateInner { /// Per-node sensing state for multi-node deployments. /// Keyed by `node_id` from the ESP32 frame header. node_states: HashMap, + /// Debounced room-level classification state — see + /// `debounce_room_classification`. `fuse_room` is a fresh, memoryless + /// plurality vote every cycle with no debounce of its own (unlike each + /// node's own classification, which is debounced individually), so with + /// per-node confidences sitting near a boundary the room-level result + /// can flip cycle-to-cycle even though each node's own reading is stable. + room_debounced_level: String, + room_debounce_candidate: String, + room_debounce_since: Option, // ── Accuracy sprint: Kalman tracker, multistatic fusion, eigenvalue counting ── /// Global Kalman-based pose tracker for stable person IDs and smoothed keypoints. pose_tracker: PoseTracker, @@ -1417,6 +1796,13 @@ struct AppStateInner { last_tracker_instant: Option, /// Attention-weighted multi-node CSI fusion engine. multistatic_fuser: MultistaticFuser, + /// Node positions parsed from --node-positions, in the same order given + /// to `multistatic_fuser.set_node_positions`. Real fleets use + /// non-sequential logical node IDs, so this is NOT keyed by node_id — + /// entry `i` applies to the i-th smallest currently-active node_id, the + /// same convention `MultistaticFuser::fuse` uses. See + /// `node_positions_by_active_id`. + node_positions_config: Vec<[f32; 3]>, /// Governed trust-path bridge (ADR-135..146): runs the same live frames /// through the privacy/provenance/witness control plane. Does not alter /// person-count behavior; its trust state (witness, effective class, @@ -1426,6 +1812,30 @@ struct AppStateInner { engine_bridge: engine_bridge::EngineBridge, /// SVD-based room field model for eigenvalue person counting (None until calibration). field_model: Option, + /// Stable installation identity used only to bind local persisted state. + installation_id: Option, + /// Metadata for the privacy reduced empty room image, when available. + bootstrap_baseline: Option, + /// A restored prior can affect startup occupancy but cannot authorize vitals. + bootstrap_baseline_active: bool, + /// Server generated identity for the current explicit calibration model. + calibration_model_id: Option, + /// Nodes that actually contributed frames to the current calibration. + calibration_source_node_ids: std::collections::BTreeSet, + /// Immutable source and CSI symbol grid selected from recent header-only + /// evidence for the current field model. + calibration_grid_binding: Option, + /// Last admitted raw CSI sequence per contributing node. Edge-vitals + /// packets do not carry a new CSI observation and cannot advance this map. + calibration_last_sequences: HashMap, + /// Late packets inside the bounded UDP reorder window are ignored rather + /// than counted. These receipts make the transport behavior auditable. + calibration_reordered_packets: HashMap, + calibration_max_reorder_depth: HashMap, + /// Nodes whose sequence moved backward beyond the bounded UDP reorder + /// window during the current session. Without a wire boot epoch, the + /// session must stay failed closed after a material discontinuity. + calibration_sequence_fault_node_ids: std::collections::BTreeSet, // ── ADR-044 §5.2: adaptive rolling-p95 normalization ───────────────────── /// Rolling P95 of `FeatureInfo.variance` over the last ~30 s (600 frames @ 20 Hz). pub(crate) p95_variance: RollingP95, @@ -1528,21 +1938,225 @@ mod adr323_pose_physics_http_tests { /// If no ESP32 frame arrives within this duration, source reverts to offline. const ESP32_OFFLINE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5); +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum CalibrationSequenceOrder { + First, + Forward, + Duplicate, + Reordered(u32), + Discontinuity, +} + +/// Two independent live Node 5 captures observed bounded same-source UDP +/// reordering through depth eleven. Sixteen covers that measured maximum with +/// five positions of margin. At the firmware's 50 Hz ceiling it spans at most +/// 320 ms; larger backward jumps stay failed closed. Late packets inside this +/// window are always dropped and never become model evidence. +const CALIBRATION_SEQUENCE_REORDER_WINDOW: u32 = 16; + +fn calibration_sequence_order(previous: Option, sequence: u32) -> CalibrationSequenceOrder { + let Some(previous) = previous else { + return CalibrationSequenceOrder::First; + }; + let delta = sequence.wrapping_sub(previous); + if delta == 0 { + CalibrationSequenceOrder::Duplicate + } else if delta < (1_u32 << 31) { + CalibrationSequenceOrder::Forward + } else { + let reorder_depth = previous.wrapping_sub(sequence); + if reorder_depth <= CALIBRATION_SEQUENCE_REORDER_WINDOW { + CalibrationSequenceOrder::Reordered(reorder_depth) + } else { + CalibrationSequenceOrder::Discontinuity + } + } +} + impl AppStateInner { + /// Admit at most one raw CSI observation per forward node sequence into + /// the current calibration session. This stateful boundary prevents a + /// caller from advancing the model with a replayed history tail. + fn maybe_feed_calibration_frame( + &mut self, + node_id: u8, + grid: CsiGridKey, + sequence: u32, + amplitudes: &[f64], + observed_at: std::time::Instant, + ) -> bool { + let binding_matches = self.calibration_grid_binding.is_some_and(|binding| { + binding.source_node_id == node_id && binding.grid == grid + }); + if !binding_matches + || !calibration_source_accepts(&self.calibration_source_node_ids, node_id) + || self.calibration_sequence_fault_node_ids.contains(&node_id) + { + return false; + } + let previous_sequence = self.calibration_last_sequences.get(&node_id).copied(); + match calibration_sequence_order(previous_sequence, sequence) { + CalibrationSequenceOrder::Duplicate => return false, + CalibrationSequenceOrder::Reordered(depth) => { + *self + .calibration_reordered_packets + .entry(node_id) + .or_default() += 1; + self.calibration_max_reorder_depth + .entry(node_id) + .and_modify(|observed| *observed = (*observed).max(depth)) + .or_insert(depth); + debug!( + node_id, + previous_sequence, + sequence, + depth, + "Ignored bounded late CSI packet during calibration" + ); + return false; + } + CalibrationSequenceOrder::Discontinuity => { + self.calibration_sequence_fault_node_ids.insert(node_id); + warn!( + node_id, + previous_sequence, + sequence, + "Calibration source sequence discontinuity exceeded reorder window; restart the empty-room capture" + ); + return false; + } + CalibrationSequenceOrder::First | CalibrationSequenceOrder::Forward => {} + } + let accepted = self.field_model.as_mut().is_some_and(|field| { + if matches!( + field.status(), + CalibrationStatus::Uncalibrated | CalibrationStatus::Collecting + ) { + field_bridge::maybe_feed_calibration(field, amplitudes) + } else { + field.check_freshness( + (chrono::Utc::now().timestamp_millis().max(0) as u64) + .saturating_mul(1_000), + ) == CalibrationStatus::Fresh + } + }); + if accepted { + self.calibration_source_node_ids.insert(node_id); + self.calibration_last_sequences.insert(node_id, sequence); + if let Some(node) = self.node_states.get_mut(&node_id) { + node.push_field_model_frame(sequence, amplitudes, observed_at); + } + } + accepted + } + + fn clear_calibration_sequence_state(&mut self) { + self.calibration_last_sequences.clear(); + self.calibration_reordered_packets.clear(); + self.calibration_max_reorder_depth.clear(); + self.calibration_sequence_fault_node_ids.clear(); + } + + fn clear_field_model_binding(&mut self) { + self.calibration_grid_binding = None; + for node in self.node_states.values_mut() { + node.clear_field_model_history(); + } + } + + fn calibration_grid_is_fresh( + &self, + binding: CalibrationGridBinding, + now: std::time::Instant, + ) -> bool { + !self + .calibration_sequence_fault_node_ids + .contains(&binding.source_node_id) + && self + .node_states + .get(&binding.source_node_id) + .and_then(|node| node.field_model_latest_seen) + .is_some_and(|seen| { + now.saturating_duration_since(seen) < ESP32_OFFLINE_TIMEOUT + }) + } + + fn field_model_holdout_ready( + &self, + binding: CalibrationGridBinding, + required_window_size: usize, + now: std::time::Instant, + ) -> bool { + self.calibration_grid_binding.is_some_and(|current| { + current.source_node_id == binding.source_node_id && current.grid == binding.grid + }) && self.calibration_grid_is_fresh(binding, now) + && self + .node_states + .get(&binding.source_node_id) + .is_some_and(|node| node.field_model_history.len() >= required_window_size) + } + + fn begin_field_model_holdout(&mut self, binding: CalibrationGridBinding) { + if let Some(node) = self.node_states.get_mut(&binding.source_node_id) { + node.clear_field_model_history(); + } + } + + fn field_model_status_at(&self, observed_at_unix_ms: u64) -> Option { + self.field_model.as_ref().map(|model| { + field_bridge::calibration_status_at(model, observed_at_unix_ms.saturating_mul(1_000)) + }) + } + + fn field_model_active_at(&self, observed_at_unix_ms: u64) -> bool { + self.field_model_status_at(observed_at_unix_ms) + .is_some_and(|status| status != CalibrationStatus::Expired) + } + + fn bootstrap_baseline_active_at(&self, observed_at_unix_ms: u64) -> bool { + self.bootstrap_baseline_active && self.field_model_active_at(observed_at_unix_ms) + } + + fn explicit_calibration_fresh_at(&self, observed_at_unix_ms: u64) -> bool { + !self.bootstrap_baseline_active + && self.field_model_status_at(observed_at_unix_ms) == Some(CalibrationStatus::Fresh) + } + /// Return the effective data source, accounting for ESP32 frame timeout. /// If the source is "esp32" but no frame has arrived in 5 seconds, returns /// "esp32:offline" so the UI can distinguish active vs stale connections. /// Person count: eigenvalue-based if field model is calibrated, else heuristic. /// Uses global frame_history if populated, otherwise the freshest per-node history. - fn person_count(&self) -> usize { + fn person_count_at(&self, observed_at_unix_ms: u64) -> usize { + // A persisted bootstrap model has negative-only authority. Its only + // allowed occupancy effect is the explicit empty-background suppression + // in `bootstrap_empty_prior_applies`; it must never infer positive count. + if self.bootstrap_baseline_active { + return score_to_person_count(self.smoothed_person_score, self.prev_person_count); + } match self.field_model.as_ref() { Some(fm) => { - // Prefer global frame_history (populated by wifi/simulate paths). - // Fall back to freshest per-node history (populated by ESP32 paths). - let history = if !self.frame_history.is_empty() { + // A single-link model may score only the source node it was + // calibrated against. Applying one node's baseline to a + // different radio creates deterministic false occupancy from + // hardware-specific amplitude offsets. + let bound_source_node_id = (self.calibration_source_node_ids.len() == 1) + .then(|| self.calibration_source_node_ids.iter().next().copied()) + .flatten() + .or_else(|| { + self.bootstrap_baseline.as_ref().and_then(|metadata| { + (metadata.source_node_ids.len() == 1) + .then_some(metadata.source_node_ids[0]) + }) + }); + let history = if let Some(node_id) = bound_source_node_id { + self.node_states + .get(&node_id) + .map(|state| &state.field_model_history) + .unwrap_or(&self.frame_history) + } else if !self.frame_history.is_empty() { &self.frame_history } else { - // Find the node with the most recent frame self.node_states .values() .filter(|ns| !ns.frame_history.is_empty()) @@ -1553,6 +2167,7 @@ impl AppStateInner { field_bridge::occupancy_or_fallback( fm, history, + observed_at_unix_ms.saturating_mul(1_000), self.smoothed_person_score, self.prev_person_count, ) @@ -1561,6 +2176,56 @@ impl AppStateInner { } } + fn person_count(&self) -> usize { + self.person_count_at(chrono::Utc::now().timestamp_millis().max(0) as u64) + } + + /// A restored startup image has negative-only authority. It may suppress + /// an empty-room false positive after one complete runtime window, but it + /// cannot establish positive presence or publish vital signs. + fn bootstrap_empty_prior_applies(&self, observed_at_unix_ms: u64) -> bool { + self.bootstrap_background_match(observed_at_unix_ms) + .is_some_and(|result| result.matches_empty) + } + + /// Return current background conformance for the exact source node bound + /// into the restored image. This remains diagnostic and negative-only. + fn bootstrap_background_match( + &self, + observed_at_unix_ms: u64, + ) -> Option { + if !self.bootstrap_baseline_active { + return None; + } + let field_model = self.field_model.as_ref()?; + let metadata = self.bootstrap_baseline.as_ref()?; + let [source_node_id] = metadata.source_node_ids.as_slice() else { + return None; + }; + let binding = self.calibration_grid_binding?; + if binding.source_node_id != *source_node_id + || binding.grid != CsiGridKey::from_bootstrap_grid(metadata.source_grid) + || self + .calibration_sequence_fault_node_ids + .contains(source_node_id) + { + return None; + } + let node = self.node_states.get(source_node_id)?; + if !node.field_model_latest_seen.is_some_and(|seen| { + std::time::Instant::now().saturating_duration_since(seen) + < ESP32_OFFLINE_TIMEOUT + }) { + return None; + } + let history = &node.field_model_history; + field_bridge::bootstrap_background_match( + field_model, + history, + observed_at_unix_ms.saturating_mul(1_000), + ) + } + fn effective_source(&self) -> String { if self.source == "esp32" { if let Some(last) = self.last_esp32_frame { @@ -1612,6 +2277,10 @@ impl AppStateInner { /// Number of frames retained in `frame_history` for temporal analysis. /// At 500 ms ticks this covers ~50 seconds; at 100 ms ticks ~10 seconds. const FRAME_HISTORY_CAPACITY: usize = 100; +/// A qualified grid may run at the 2 Hz admission floor. Rebuilding a 50 frame +/// runtime scoring window then needs 25 seconds, so promotion grants ten more +/// seconds for ordinary bounded jitter before failing closed. +const BOOTSTRAP_HOLDOUT_PREWARM_TIMEOUT_MS: u64 = 35_000; type SharedState = Arc>; @@ -1675,9 +2344,13 @@ impl AppStateInner { training_progress_tx: broadcast::channel::(256).0, adaptive_model: None, node_states: HashMap::new(), + room_debounced_level: "absent".to_string(), + room_debounce_candidate: "absent".to_string(), + room_debounce_since: None, pose_tracker: PoseTracker::new(), last_tracker_instant: None, multistatic_fuser: MultistaticFuser::new(), + node_positions_config: Vec::new(), engine_bridge: engine_bridge::EngineBridge::new( wifi_densepose_bfld::PrivacyMode::PrivateHome, 1, @@ -1686,6 +2359,16 @@ impl AppStateInner { None, ), field_model: None, + installation_id: None, + bootstrap_baseline: None, + bootstrap_baseline_active: false, + calibration_model_id: None, + calibration_source_node_ids: std::collections::BTreeSet::new(), + calibration_grid_binding: None, + calibration_last_sequences: HashMap::new(), + calibration_reordered_packets: HashMap::new(), + calibration_max_reorder_depth: HashMap::new(), + calibration_sequence_fault_node_ids: std::collections::BTreeSet::new(), p95_variance: RollingP95::new(600, 60), p95_motion_band_power: RollingP95::new(600, 60), p95_spectral_power: RollingP95::new(600, 60), @@ -1700,6 +2383,113 @@ impl AppStateInner { } } +#[cfg(test)] +mod calibration_expiry_tests { + use super::*; + use wifi_densepose_signal::ruvsense::field_model::FieldModelConfig; + + fn finalized_model(calibrated_at_unix_ms: u64) -> FieldModel { + let mut model = FieldModel::new(FieldModelConfig { + n_links: 1, + n_subcarriers: 56, + n_modes: 3, + min_calibration_frames: 10, + min_calibration_duration_s: 0.0, + baseline_expiry_s: 2.0, + }) + .unwrap(); + for frame_index in 0..20 { + let frame = (0..56) + .map(|subcarrier| { + 10.0 + subcarrier as f64 * 0.01 + + (frame_index as f64 * 0.1 + subcarrier as f64 * 0.03).sin() * 0.01 + }) + .collect(); + model.feed_calibration(&[frame]).unwrap(); + } + model + .finalize_calibration(calibrated_at_unix_ms.saturating_mul(1_000), 7) + .unwrap(); + model + } + + fn state_with_model(bootstrap: bool) -> AppStateInner { + let model = finalized_model(1_000); + let baseline = model.modes().unwrap().baseline[0].clone(); + let mut state = AppStateInner::minimal(); + state.frame_history = (0..50).map(|_| baseline.clone()).collect(); + let mut node = NodeState::new(); + node.field_model_history = (0..50).map(|_| baseline.clone()).collect(); + node.field_model_latest_sequence = Some(50); + node.field_model_latest_seen = Some(std::time::Instant::now()); + state.node_states.insert(5, node); + state.calibration_source_node_ids.insert(5); + state.calibration_grid_binding = Some(CalibrationGridBinding { + source_node_id: 5, + grid: CsiGridKey { + n_subcarriers: 64, + ppdu_type: 0, + }, + evidence: None, + }); + state.field_model = Some(model); + state.bootstrap_baseline_active = bootstrap; + if bootstrap { + state.bootstrap_baseline = Some(BootstrapBaselineMetadata { + authority: bootstrap_baseline::BOOTSTRAP_BASELINE_AUTHORITY, + source_node_ids: vec![5], + source_grid: bootstrap_baseline::BootstrapCsiGrid { + n_subcarriers: 64, + ppdu_type: 0, + }, + source_model_id: "test-model".to_string(), + created_at_unix_ms: 1_000, + expires_at_unix_ms: 3_000, + content_sha256: "00".repeat(32), + }); + } + state + } + + #[test] + fn bootstrap_field_model_remains_negative_only_for_person_count() { + let runtime = state_with_model(false); + assert_eq!(runtime.person_count_at(1_500), 0); + + let bootstrap = state_with_model(true); + assert_eq!( + bootstrap.person_count_at(1_500), + score_to_person_count(0.0, 0), + "a bootstrap prior must not derive a positive or negative count through the runtime model path" + ); + } + + #[tokio::test] + async fn long_running_expiry_is_not_reported_active_for_runtime_or_bootstrap() { + let runtime = Arc::new(RwLock::new(state_with_model(false))); + let Json(runtime_status) = calibration_status(State(runtime)).await; + assert_eq!(runtime_status["active"], false); + assert_eq!(runtime_status["status"], "expired"); + assert_eq!(runtime_status["binding_mode"], "none"); + + let bootstrap = Arc::new(RwLock::new(state_with_model(true))); + let Json(bootstrap_status) = calibration_status(State(bootstrap)).await; + assert_eq!(bootstrap_status["active"], false); + assert_eq!(bootstrap_status["status"], "expired"); + assert_eq!(bootstrap_status["binding_mode"], "none"); + assert_eq!(bootstrap_status["bootstrap_baseline"]["stored"], true); + assert_eq!(bootstrap_status["bootstrap_baseline"]["active"], false); + assert_eq!( + bootstrap_status["bootstrap_baseline"]["calibrated_evidence_authorized"], + false + ); + assert_eq!( + bootstrap_status["bootstrap_baseline"]["numeric_vitals_authorized"], + false + ); + } +} + // ── ESP32 Edge Vitals Packet (ADR-039, magic 0xC511_0002) ──────────────────── /// Decoded vitals packet from ESP32 edge processing pipeline. @@ -2534,15 +3324,9 @@ fn extract_features_from_frame( 0.0 }; - // ── Dominant frequency via peak subcarrier index ── - let peak_idx = frame - .amplitudes - .iter() - .enumerate() - .max_by(|a, b| a.1.partial_cmp(b.1).unwrap_or(std::cmp::Ordering::Equal)) - .map(|(i, _)| i) - .unwrap_or(0); - let dominant_freq_hz = peak_idx as f64 * 0.05; + // A subcarrier index is a spatial-frequency bin, not temporal hertz. + let dominant_freq_hz = + csi::estimate_temporal_dominant_frequency_hz(frame_history, sample_rate_hz); // ── Change point detection (threshold-crossing count in current frame) ── let threshold = mean_amp * 1.2; @@ -2555,7 +3339,9 @@ fn extract_features_from_frame( // ── Motion score: sliding-window temporal difference ── // Compare current frame against the most recent historical frame. // The difference is normalised by the mean amplitude to be scale-invariant. - let temporal_motion_score = if let Some(prev_frame) = frame_history.back() { + // Callers append the current frame first, making the true predecessor the + // second item from the back. + let temporal_motion_score = if let Some(prev_frame) = frame_history.iter().rev().nth(1) { let n_cmp = n_sub.min(prev_frame.len()); if n_cmp > 0 { let diff_energy: f64 = (0..n_cmp) @@ -2639,14 +3425,44 @@ fn raw_classify(score: f64) -> String { } /// Debounce frames required before state transition (at ~10 FPS = ~0.4s). +/// Used only by [`smooth_and_classify`] (simulated / WiFi-scan sources, +/// which run on a fixed tick cadence close to this design point). const DEBOUNCE_FRAMES: u32 = 4; /// EMA alpha for motion smoothing (~1s time constant at 10 FPS). +/// Used only by [`smooth_and_classify`]; see above. const MOTION_EMA_ALPHA: f64 = 0.15; /// EMA alpha for slow-adapting baseline (~30s time constant at 10 FPS). +/// Used only by [`smooth_and_classify`]; see above. const BASELINE_EMA_ALPHA: f64 = 0.003; /// Number of warm-up frames before baseline subtraction kicks in. +/// Used only by [`smooth_and_classify`]; see above. const BASELINE_WARMUP: u64 = 50; +/// Debounce duration required before [`smooth_and_classify_node`] accepts a +/// state transition. Equivalent to the original `DEBOUNCE_FRAMES = 4` at the +/// assumed 10 FPS design point. +const NODE_DEBOUNCE_DURATION_SECS: f64 = 0.4; +/// Time constant for [`smooth_and_classify_node`]'s motion-score EMA, +/// derived from `MOTION_EMA_ALPHA` at the assumed 10 FPS design point +/// (τ = -T / ln(1-α)) so behavior is unchanged for a node running at +/// exactly 10 FPS. +/// +/// Frame-count/fixed-alpha smoothing silently scales with actual CSI +/// arrival rate. ESP32-C6 boards observed in the field run CSI at +/// ~48-50 FPS, not the assumed 10 FPS — ~5x faster — which made the +/// debounce trigger in ~0.08s instead of ~0.4s and compressed the "1s" +/// motion EMA to ~0.2s of real smoothing, letting raw per-frame noise +/// flip the reported label almost directly (the reported UI "seizure"). +/// Deriving a per-frame alpha from the node's actual measured +/// `csi_fps_ema` makes the smoothing strength invariant to arrival rate. +const NODE_MOTION_TIME_CONSTANT_SECS: f64 = 0.6154; +/// Time constant for [`smooth_and_classify_node`]'s baseline EMA, same +/// derivation as `NODE_MOTION_TIME_CONSTANT_SECS` from `BASELINE_EMA_ALPHA`. +const NODE_BASELINE_TIME_CONSTANT_SECS: f64 = 33.28; +/// Baseline warm-up duration before baseline subtraction kicks in for +/// [`smooth_and_classify_node`] (originally 50 frames at 10 FPS = 5s). +const NODE_BASELINE_WARMUP_SECS: f64 = 5.0; + /// Apply EMA smoothing, adaptive baseline subtraction, and hysteresis debounce /// to the raw classification. Mutates the smoothing state in `AppStateInner`. fn smooth_and_classify(state: &mut AppStateInner, raw: &mut ClassificationInfo, raw_motion: f64) { @@ -2700,18 +3516,27 @@ fn smooth_and_classify(state: &mut AppStateInner, raw: &mut ClassificationInfo, /// Per-node variant of `smooth_and_classify` that operates on a `NodeState` /// instead of `AppStateInner` (issue #249). fn smooth_and_classify_node(ns: &mut NodeState, raw: &mut ClassificationInfo, raw_motion: f64) { + // Derive this frame's effective time step from the node's actual + // measured CSI rate (already EMA-tracked, burst-filtered elsewhere — + // see `observe_csi_frame_arrival`) rather than assuming 10 FPS. + let fps = ns.csi_fps_ema.max(1.0); + let dt = 1.0 / fps; + let motion_alpha = 1.0 - (-dt / NODE_MOTION_TIME_CONSTANT_SECS).exp(); + let baseline_alpha = 1.0 - (-dt / NODE_BASELINE_TIME_CONSTANT_SECS).exp(); + let debounce_frames_needed = ((NODE_DEBOUNCE_DURATION_SECS * fps).round() as u32).max(1); + let warmup_frames_needed = ((NODE_BASELINE_WARMUP_SECS * fps).round() as u64).max(1); + ns.baseline_frames += 1; - if ns.baseline_frames < BASELINE_WARMUP { + if ns.baseline_frames < warmup_frames_needed { ns.baseline_motion = ns.baseline_motion * 0.9 + raw_motion * 0.1; } else if raw_motion < ns.smoothed_motion + 0.05 { ns.baseline_motion = - ns.baseline_motion * (1.0 - BASELINE_EMA_ALPHA) + raw_motion * BASELINE_EMA_ALPHA; + ns.baseline_motion * (1.0 - baseline_alpha) + raw_motion * baseline_alpha; } let adjusted = (raw_motion - ns.baseline_motion * 0.7).max(0.0); - ns.smoothed_motion = - ns.smoothed_motion * (1.0 - MOTION_EMA_ALPHA) + adjusted * MOTION_EMA_ALPHA; + ns.smoothed_motion = ns.smoothed_motion * (1.0 - motion_alpha) + adjusted * motion_alpha; let sm = ns.smoothed_motion; let candidate = raw_classify(sm); @@ -2721,7 +3546,7 @@ fn smooth_and_classify_node(ns: &mut NodeState, raw: &mut ClassificationInfo, ra ns.debounce_candidate = candidate; } else if candidate == ns.debounce_candidate { ns.debounce_counter += 1; - if ns.debounce_counter >= DEBOUNCE_FRAMES { + if ns.debounce_counter >= debounce_frames_needed { ns.current_motion_level = candidate; ns.debounce_counter = 0; } @@ -2749,13 +3574,18 @@ fn adaptive_override( .back() .map(|v| v.as_slice()) .unwrap_or(&[]); + let dominant_frequency = adaptive_classifier::compatible_dominant_frequency( + model.version, + features.dominant_freq_hz, + amps, + ); let feat_arr = adaptive_classifier::features_from_runtime( &serde_json::json!({ "variance": features.variance, "motion_band_power": features.motion_band_power, "breathing_band_power": features.breathing_band_power, "spectral_power": features.spectral_power, - "dominant_freq_hz": features.dominant_freq_hz, + "dominant_freq_hz": dominant_frequency, "change_points": features.change_points, "mean_rssi": features.mean_rssi, }), @@ -3162,7 +3992,7 @@ async fn windows_wifi_task(state: SharedState, tick_ms: u64) { feat_variance.min(1.0), &sub_variances, ), - vital_signs: Some(vitals), + vital_signs: None, enhanced_motion, enhanced_breathing, posture: posture_str, @@ -3326,7 +4156,7 @@ async fn windows_wifi_fallback_tick(state: &SharedState, seq: u32) { feat_variance.min(1.0), &sub_variances, ), - vital_signs: Some(vitals), + vital_signs: None, enhanced_motion: None, enhanced_breathing: None, posture: None, @@ -5777,13 +6607,707 @@ async fn adaptive_unload(State(state): State) -> Json Option { + if !explicit_calibration_fresh || person_count != 1 { + return None; + } + let signal_quality = candidates.signal_quality.clamp(0.0, 1.0); + if signal_quality < VITAL_PUBLICATION_MIN_SIGNAL_QUALITY { + return None; + } + let breathing_rate_bpm = (candidates.breathing_confidence + >= VITAL_PUBLICATION_MIN_CONFIDENCE) + .then_some(candidates.breathing_rate_bpm) + .flatten(); + let heart_rate_bpm = + (candidates.heartbeat_confidence >= VITAL_PUBLICATION_MIN_CONFIDENCE) + .then_some(candidates.heart_rate_bpm) + .flatten(); + if breathing_rate_bpm.is_none() && heart_rate_bpm.is_none() { + return None; + } + Some(VitalSigns { + breathing_rate_bpm, + heart_rate_bpm, + breathing_confidence: candidates.breathing_confidence.clamp(0.0, 1.0), + heartbeat_confidence: candidates.heartbeat_confidence.clamp(0.0, 1.0), + signal_quality, + }) +} + +fn edge_vitals_message_for_publication( + raw: &Esp32VitalsPacket, + published_vitals: Option<&VitalSigns>, + bootstrap_empty: bool, + explicit_calibration_fresh: bool, + person_count: usize, +) -> serde_json::Value { + let numeric_vitals_authorized = published_vitals.is_some(); + let abstention_reason = if numeric_vitals_authorized { + None + } else if bootstrap_empty { + Some("bootstrap_empty_background") + } else if !explicit_calibration_fresh { + Some("explicit_calibration_required") + } else if person_count != 1 { + Some("exactly_one_occupant_required") + } else { + Some("vital_quality_gate_failed") + }; + let effective_presence = raw.presence && !bootstrap_empty; + let effective_motion = raw.motion && !bootstrap_empty; + let effective_person_count = if bootstrap_empty { 0 } else { person_count }; + + serde_json::json!({ + "type": "edge_vitals", + "node_id": raw.node_id, + "presence": effective_presence, + "fall_detected": raw.fall_detected && !bootstrap_empty, + "motion": effective_motion, + "breathing_rate_bpm": published_vitals + .and_then(|vitals| vitals.breathing_rate_bpm), + "heartrate_bpm": published_vitals + .and_then(|vitals| vitals.heart_rate_bpm), + "n_persons": effective_person_count, + "person_count_valid": raw.person_count_valid && !bootstrap_empty, + "motion_energy": if bootstrap_empty { 0.0 } else { raw.motion_energy }, + "presence_score": if bootstrap_empty { 0.0 } else { raw.presence_score }, + "rssi": raw.rssi, + "calibrated_evidence_authorized": explicit_calibration_fresh, + "numeric_vitals_authorized": numeric_vitals_authorized, + "abstention_reason": abstention_reason, + }) +} + +fn opaque_calibration_model_id() -> String { + let mut bytes = [0_u8; 16]; + OsRng.fill_bytes(&mut bytes); + let suffix: String = bytes.iter().map(|byte| format!("{byte:02x}")).collect(); + format!("cal-model-{suffix}") +} + +fn calibration_source_accepts( + source_node_ids: &std::collections::BTreeSet, + node_id: u8, +) -> bool { + source_node_ids.is_empty() || source_node_ids.contains(&node_id) +} + +fn calibration_source_has_fresh_csi(node: &NodeState, now: std::time::Instant) -> bool { + node.latest_accepted_csi_at + .is_some_and(|seen| now.saturating_duration_since(seen) < ESP32_OFFLINE_TIMEOUT) +} + +#[cfg(test)] +mod bootstrap_vital_publication_tests { + use super::*; + + fn test_grid() -> CsiGridKey { + CsiGridKey { + n_subcarriers: 64, + ppdu_type: 0, + } + } + + fn bind_test_calibration(state: &mut AppStateInner) { + state.calibration_source_node_ids.insert(5); + state.calibration_grid_binding = Some(CalibrationGridBinding { + source_node_id: 5, + grid: test_grid(), + evidence: None, + }); + state.node_states.entry(5).or_insert_with(NodeState::new); + } + + fn feed_test_frame( + state: &mut AppStateInner, + node_id: u8, + sequence: u32, + amplitudes: &[f64], + ) -> bool { + state.maybe_feed_calibration_frame( + node_id, + test_grid(), + sequence, + amplitudes, + std::time::Instant::now(), + ) + } + + #[test] + fn single_link_calibration_binds_to_first_source() { + let mut sources = std::collections::BTreeSet::new(); + assert!(calibration_source_accepts(&sources, 5)); + sources.insert(5); + assert!(calibration_source_accepts(&sources, 5)); + assert!(!calibration_source_accepts(&sources, 3)); + } + + #[test] + fn explicit_single_link_source_rejects_other_live_nodes() { + let sources = std::collections::BTreeSet::from([5]); + assert!(calibration_source_accepts(&sources, 5)); + assert!(!calibration_source_accepts(&sources, 3)); + assert!(!calibration_source_accepts(&sources, 7)); + } + + #[test] + fn edge_vitals_liveness_cannot_authorize_raw_csi_calibration() { + let now = std::time::Instant::now(); + let mut node = NodeState::new(); + node.last_frame_time = Some(now); + assert!(!calibration_source_has_fresh_csi(&node, now)); + + node.observe_accepted_csi_frame(9, false, now); + assert!(calibration_source_has_fresh_csi(&node, now)); + assert!(!calibration_source_has_fresh_csi( + &node, + now + ESP32_OFFLINE_TIMEOUT + )); + } + + #[test] + fn calibration_grid_selection_prefers_stable_rate_qualified_stream() { + let start = std::time::Instant::now(); + let mut node = NodeState::new(); + let stable = CsiGridKey { + n_subcarriers: 64, + ppdu_type: 0, + }; + let sparse = CsiGridKey { + n_subcarriers: 192, + ppdu_type: 0, + }; + for index in 0..=48 { + node.observe_raw_grid( + stable, + start + std::time::Duration::from_millis(index * 250), + ); + } + for index in 0..=6 { + node.observe_raw_grid( + sparse, + start + std::time::Duration::from_secs(index * 2), + ); + } + let (selected, evidence) = node + .select_calibration_grid(start + std::time::Duration::from_secs(12)) + .expect("stable grid is eligible"); + assert_eq!(selected, stable); + assert!(evidence.rate_hz >= 3.9); + assert!(evidence.max_gap_s < 5.0); + } + + #[tokio::test] + async fn calibration_start_requires_evidence_then_binds_the_qualified_grid() { + let mut inner = AppStateInner::minimal(); + inner.node_states.insert(5, NodeState::new()); + let state = Arc::new(RwLock::new(inner)); + + let Json(unavailable) = calibration_start( + State(state.clone()), + Query(CalibrationStartQuery { + source_node_id: Some(5), + }), + ) + .await; + assert_eq!(unavailable["success"], false); + assert_eq!(unavailable["error_code"], "calibration_grid_unavailable"); + { + let state = state.read().await; + assert!(state.field_model.is_none()); + assert!(state.calibration_model_id.is_none()); + assert!(state.calibration_source_node_ids.is_empty()); + assert!(state.calibration_grid_binding.is_none()); + } + + let now = std::time::Instant::now(); + let start = now - std::time::Duration::from_secs(10); + { + let mut state = state.write().await; + let node = state.node_states.get_mut(&5).expect("node"); + for index in 0..=40 { + node.observe_raw_grid( + test_grid(), + start + std::time::Duration::from_millis(index * 250), + ); + } + } + let Json(started) = calibration_start( + State(state.clone()), + Query(CalibrationStartQuery { + source_node_id: Some(5), + }), + ) + .await; + assert_eq!(started["success"], true); + assert_eq!(started["source_node_ids"], serde_json::json!([5])); + assert_eq!(started["source_grid"]["n_subcarriers"], 64); + assert_eq!(started["source_grid"]["ppdu_type"], 0); + let state = state.read().await; + assert!(state.field_model.is_some()); + assert!(state.calibration_model_id.is_some()); + assert_eq!(state.calibration_source_node_ids, [5].into_iter().collect()); + assert_eq!( + state.calibration_grid_binding.map(|binding| binding.grid), + Some(test_grid()) + ); + } + + #[test] + fn non_bound_grid_never_enters_field_model_history() { + let mut state = AppStateInner::minimal(); + state.field_model = Some( + FieldModel::new(field_bridge::single_link_config()).expect("field model"), + ); + bind_test_calibration(&mut state); + let wrong_grid = CsiGridKey { + n_subcarriers: 192, + ppdu_type: 0, + }; + assert!(!state.maybe_feed_calibration_frame( + 5, + wrong_grid, + 41, + &[0.25; 192], + std::time::Instant::now(), + )); + assert!(state + .node_states + .get(&5) + .expect("node") + .field_model_history + .is_empty()); + assert!(state.calibration_last_sequences.is_empty()); + } + + #[test] + fn calibration_session_counts_each_unique_bound_csi_sequence_once() { + let mut state = AppStateInner::minimal(); + state.field_model = Some( + FieldModel::new(field_bridge::single_link_config()).expect("field model"), + ); + bind_test_calibration(&mut state); + + let first = vec![0.25; 56]; + assert!(feed_test_frame(&mut state, 5, 41, &first)); + assert!(!feed_test_frame(&mut state, 5, 41, &first)); + + let second = vec![0.5; 56]; + assert!(feed_test_frame(&mut state, 5, 42, &second)); + assert!(!feed_test_frame(&mut state, 3, 43, &second)); + + let model = state.field_model.as_ref().expect("field model remains active"); + assert_eq!(model.calibration_frame_count(), 2); + assert_eq!(state.calibration_last_sequences.get(&5), Some(&42)); + assert!(!state.calibration_last_sequences.contains_key(&3)); + } + + #[test] + fn calibration_bounded_reorder_is_dropped_without_feeding_model() { + let mut state = AppStateInner::minimal(); + state.field_model = Some( + FieldModel::new(field_bridge::single_link_config()).expect("field model"), + ); + bind_test_calibration(&mut state); + let frame = vec![0.25; 56]; + + assert!(feed_test_frame(&mut state, 5, 4_119_566, &frame)); + assert!(!feed_test_frame(&mut state, 5, 4_119_555, &frame)); + assert!(!feed_test_frame(&mut state, 5, 4_119_556, &frame)); + assert!(!feed_test_frame(&mut state, 5, 4_119_557, &frame)); + assert!(feed_test_frame(&mut state, 5, 4_119_567, &frame)); + assert!(state.calibration_sequence_fault_node_ids.is_empty()); + assert_eq!(state.calibration_reordered_packets.get(&5), Some(&3)); + assert_eq!(state.calibration_max_reorder_depth.get(&5), Some(&11)); + assert_eq!( + state + .field_model + .as_ref() + .expect("field model") + .calibration_frame_count(), + 2 + ); + } + + #[test] + fn calibration_sequence_discontinuity_latches_session_failure() { + let mut state = AppStateInner::minimal(); + state.field_model = Some( + FieldModel::new(field_bridge::single_link_config()).expect("field model"), + ); + bind_test_calibration(&mut state); + let frame = vec![0.25; 56]; + + assert!(feed_test_frame(&mut state, 5, 100, &frame)); + assert!(!feed_test_frame(&mut state, 5, 83, &frame)); + assert!(!feed_test_frame(&mut state, 5, 101, &frame)); + assert!(state.calibration_sequence_fault_node_ids.contains(&5)); + assert_eq!( + state + .field_model + .as_ref() + .expect("field model") + .calibration_frame_count(), + 1 + ); + + state.clear_calibration_sequence_state(); + assert!(state.calibration_sequence_fault_node_ids.is_empty()); + assert!(state.calibration_last_sequences.is_empty()); + assert!(state.calibration_reordered_packets.is_empty()); + assert!(state.calibration_max_reorder_depth.is_empty()); + } + + #[test] + fn calibration_sequence_wraps_and_failed_empty_input_can_retry() { + let mut state = AppStateInner::minimal(); + state.field_model = Some( + FieldModel::new(field_bridge::single_link_config()).expect("field model"), + ); + bind_test_calibration(&mut state); + let frame = vec![0.25; 56]; + + assert!(!feed_test_frame(&mut state, 5, u32::MAX, &[])); + assert!(state.calibration_last_sequences.is_empty()); + assert!(feed_test_frame(&mut state, 5, u32::MAX, &frame)); + assert!(feed_test_frame(&mut state, 5, 0, &frame)); + assert!(!feed_test_frame(&mut state, 5, 0, &frame)); + assert!(feed_test_frame(&mut state, 5, 1, &frame)); + assert_eq!( + state + .field_model + .as_ref() + .expect("field model") + .calibration_frame_count(), + 3 + ); + } + + #[test] + fn calibration_sequence_order_drops_bounded_late_and_rejects_discontinuity() { + assert_eq!( + calibration_sequence_order(None, 7), + CalibrationSequenceOrder::First + ); + assert_eq!( + calibration_sequence_order(Some(7), 8), + CalibrationSequenceOrder::Forward + ); + assert_eq!( + calibration_sequence_order(Some(8), 8), + CalibrationSequenceOrder::Duplicate + ); + assert_eq!( + calibration_sequence_order(Some(8), 7), + CalibrationSequenceOrder::Reordered(1) + ); + assert_eq!( + calibration_sequence_order(Some(32), 16), + CalibrationSequenceOrder::Reordered(16) + ); + assert_eq!( + calibration_sequence_order(Some(32), 15), + CalibrationSequenceOrder::Discontinuity + ); + assert_eq!( + calibration_sequence_order(Some(0), 1_u32 << 31), + CalibrationSequenceOrder::Discontinuity + ); + assert_eq!( + calibration_sequence_order(Some(u32::MAX), 0), + CalibrationSequenceOrder::Forward + ); + } + + #[tokio::test] + async fn calibration_status_and_stop_fail_closed_after_sequence_regression() { + let mut inner = AppStateInner::minimal(); + inner.field_model = Some( + FieldModel::new(field_bridge::single_link_config()).expect("field model"), + ); + inner.calibration_model_id = Some("cal-model-test".to_string()); + inner.calibration_source_node_ids.insert(5); + inner.calibration_last_sequences.insert(5, 41); + inner.calibration_reordered_packets.insert(5, 3); + inner.calibration_max_reorder_depth.insert(5, 3); + inner.calibration_sequence_fault_node_ids.insert(5); + let state = Arc::new(RwLock::new(inner)); + + let Json(status) = calibration_status(State(state.clone())).await; + assert_eq!(status["last_sequence_by_node"]["5"], 41); + assert_eq!(status["reorder_window_sequences"], 16); + assert_eq!(status["reordered_packets_by_node"]["5"], 3); + assert_eq!(status["max_reorder_depth_by_node"]["5"], 3); + assert_eq!(status["sequence_fault_node_ids"], serde_json::json!([5])); + + let Json(stopped) = calibration_stop(State(state)).await; + assert_eq!(stopped["success"], false); + assert_eq!( + stopped["error_code"], + "calibration_sequence_discontinuity" + ); + assert_eq!(stopped["model_id"], "cal-model-test"); + assert_eq!(stopped["source_node_ids"], serde_json::json!([5])); + assert_eq!(stopped["reordered_packets_by_node"]["5"], 3); + assert_eq!(stopped["max_reorder_depth_by_node"]["5"], 3); + } + + #[tokio::test] + async fn calibration_stop_rejects_missing_or_stale_grid_identity() { + let mut inner = AppStateInner::minimal(); + inner.field_model = Some( + FieldModel::new(field_bridge::single_link_config()).expect("field model"), + ); + inner.calibration_model_id = Some("cal-model-test".to_string()); + inner.calibration_source_node_ids.insert(5); + let state = Arc::new(RwLock::new(inner)); + + let Json(missing) = calibration_stop(State(state.clone())).await; + assert_eq!(missing["success"], false); + assert_eq!(missing["error_code"], "calibration_grid_identity_missing"); + + { + let mut state = state.write().await; + state.calibration_grid_binding = Some(CalibrationGridBinding { + source_node_id: 5, + grid: test_grid(), + evidence: None, + }); + state.node_states.insert(5, NodeState::new()); + } + let Json(stale) = calibration_stop(State(state)).await; + assert_eq!(stale["success"], false); + assert_eq!(stale["error_code"], "calibration_grid_stale"); + } + + #[test] + fn post_finalize_holdout_excludes_training_history_and_sequence_faults() { + let mut state = AppStateInner::minimal(); + bind_test_calibration(&mut state); + let binding = state.calibration_grid_binding.expect("binding"); + let now = std::time::Instant::now(); + { + let node = state.node_states.get_mut(&5).expect("node"); + for sequence in 0..50 { + node.push_field_model_frame(sequence, &[0.25; 64], now); + } + } + assert!(state.field_model_holdout_ready(binding, 50, now)); + + state.begin_field_model_holdout(binding); + assert!(state + .node_states + .get(&5) + .expect("node") + .field_model_history + .is_empty()); + assert!(!state.field_model_holdout_ready(binding, 50, now)); + + { + let node = state.node_states.get_mut(&5).expect("node"); + for sequence in 50..100 { + node.push_field_model_frame(sequence, &[0.5; 64], now); + } + } + assert!(state.field_model_holdout_ready(binding, 50, now)); + state.calibration_sequence_fault_node_ids.insert(5); + assert!(!state.field_model_holdout_ready(binding, 50, now)); + } + + #[test] + fn startup_calibration_flag_fails_closed_before_state_creation() { + assert!(validate_startup_calibration_request(false).is_ok()); + let error = validate_startup_calibration_request(true).expect_err("must fail closed"); + assert!(error.contains("--calibrate")); + assert!(error.contains("10 seconds")); + assert!(error.contains("source_node_id")); + } + + fn strong_candidates() -> VitalSigns { + VitalSigns { + breathing_rate_bpm: Some(15.0), + heart_rate_bpm: Some(72.0), + breathing_confidence: 0.9, + heartbeat_confidence: 0.9, + signal_quality: 0.9, + } + } + + #[test] + fn uncalibrated_empty_and_multi_person_rooms_publish_no_vitals() { + let candidates = strong_candidates(); + assert!(vitals_for_publication(&candidates, false, 1).is_none()); + assert!(vitals_for_publication(&candidates, true, 0).is_none()); + assert!(vitals_for_publication(&candidates, true, 2).is_none()); + } + + #[test] + fn one_explicitly_calibrated_occupant_may_publish_qualified_vitals() { + let published = vitals_for_publication(&strong_candidates(), true, 1).unwrap(); + assert_eq!(published.breathing_rate_bpm, Some(15.0)); + assert_eq!(published.heart_rate_bpm, Some(72.0)); + } + + #[test] + fn weak_vital_candidates_fail_closed() { + let mut candidates = strong_candidates(); + candidates.signal_quality = 0.2; + assert!(vitals_for_publication(&candidates, true, 1).is_none()); + } + + fn raw_edge_vitals() -> Esp32VitalsPacket { + Esp32VitalsPacket { + node_id: 5, + presence: true, + fall_detected: true, + motion: true, + breathing_rate_bpm: 15.0, + heartrate_bpm: 72.0, + rssi: -42, + n_persons: 1, + person_count_valid: true, + motion_energy: 0.8, + presence_score: 0.9, + timestamp_ms: 42, + } + } + + #[test] + fn bootstrap_empty_background_suppresses_legacy_edge_vitals() { + let message = edge_vitals_message_for_publication( + &raw_edge_vitals(), + None, + true, + false, + 0, + ); + assert_eq!(message["presence"], false); + assert_eq!(message["motion"], false); + assert_eq!(message["fall_detected"], false); + assert_eq!(message["n_persons"], 0); + assert!(message["breathing_rate_bpm"].is_null()); + assert!(message["heartrate_bpm"].is_null()); + assert_eq!(message["numeric_vitals_authorized"], false); + assert_eq!(message["abstention_reason"], "bootstrap_empty_background"); + } + + #[test] + fn explicit_single_occupant_may_publish_legacy_edge_vitals() { + let published = vitals_for_publication(&strong_candidates(), true, 1).unwrap(); + let message = edge_vitals_message_for_publication( + &raw_edge_vitals(), + Some(&published), + false, + true, + 1, + ); + assert_eq!(message["presence"], true); + assert_eq!(message["fall_detected"], true); + assert_eq!(message["n_persons"], 1); + assert_eq!(message["breathing_rate_bpm"], 15.0); + assert_eq!(message["heartrate_bpm"], 72.0); + assert_eq!(message["numeric_vitals_authorized"], true); + assert!(message["abstention_reason"].is_null()); + } +} + // ── Field model calibration endpoints (eigenvalue person counting) ────────── -async fn calibration_start(State(state): State) -> Json { +#[derive(Debug, Default, Deserialize)] +struct CalibrationStartQuery { + /// Optional explicit single-link source. When omitted, the first accepted + /// ESP32 packet preserves the legacy binding behavior. + source_node_id: Option, +} + +#[derive(Debug, Default, Deserialize)] +struct BootstrapRefineQuery { + #[serde(default)] + confirmed_empty: bool, + source_node_id: Option, +} + +async fn calibration_start( + State(state): State, + Query(query): Query, +) -> Json { let mut s = state.write().await; + let now = std::time::Instant::now(); + let selected_binding = if let Some(source_node_id) = query.source_node_id { + let Some(node) = s.node_states.get(&source_node_id) else { + return Json(serde_json::json!({ + "success": false, + "error_code": "calibration_source_unavailable", + "error": "The requested calibration source is missing.", + "source_node_id": source_node_id, + })); + }; + let candidates = node.calibration_grid_candidates(now); + let Some((grid, evidence)) = node.select_calibration_grid(now) else { + return Json(serde_json::json!({ + "success": false, + "error_code": "calibration_grid_unavailable", + "error": "No raw CSI grid has at least 10 seconds of evidence, a 2 Hz measured rate, and a maximum gap below 5 seconds.", + "source_node_id": source_node_id, + "candidates": candidates.into_iter().map(|(grid, evidence)| serde_json::json!({ + "grid": grid, + "evidence": evidence, + })).collect::>(), + })); + }; + CalibrationGridBinding { + source_node_id, + grid, + evidence: Some(evidence), + } + } else { + let eligible: Vec = s + .node_states + .iter() + .filter_map(|(&source_node_id, node)| { + node.select_calibration_grid(now).map(|(grid, evidence)| { + CalibrationGridBinding { + source_node_id, + grid, + evidence: Some(evidence), + } + }) + }) + .collect(); + let [binding] = eligible.as_slice() else { + return Json(serde_json::json!({ + "success": false, + "error_code": "calibration_source_required", + "error": "Choose exactly one eligible ESP32 source for room calibration.", + "eligible_sources": eligible.iter().map(|binding| serde_json::json!({ + "source_node_id": binding.source_node_id, + "grid": binding.grid, + "evidence": binding.evidence, + })).collect::>(), + })); + }; + *binding + }; + if s.bootstrap_baseline_active { + s.field_model = None; + s.bootstrap_baseline_active = false; + } // Guard: don't discard an in-progress or fresh calibration if let Some(ref fm) = s.field_model { - match fm.status() { + let observed_at_us = + (chrono::Utc::now().timestamp_millis().max(0) as u64).saturating_mul(1_000); + match field_bridge::calibration_status_at(fm, observed_at_us) { CalibrationStatus::Collecting => { return Json(serde_json::json!({ "success": false, @@ -5803,9 +7327,20 @@ async fn calibration_start(State(state): State) -> Json { s.field_model = Some(fm); + s.calibration_model_id = Some(opaque_calibration_model_id()); + s.calibration_source_node_ids.clear(); + s.clear_field_model_binding(); + s.clear_calibration_sequence_state(); + s.calibration_source_node_ids + .insert(selected_binding.source_node_id); + s.calibration_grid_binding = Some(selected_binding); Json(serde_json::json!({ "success": true, "message": "Calibration started — keep room empty while frames accumulate.", + "model_id": s.calibration_model_id, + "source_node_ids": s.calibration_source_node_ids, + "source_grid": selected_binding.grid, + "grid_evidence": selected_binding.evidence, })) } // ADR-080 #2: FieldModel init error chain stays server-side only. @@ -5815,6 +7350,55 @@ async fn calibration_start(State(state): State) -> Json) -> Json { let mut s = state.write().await; + let model_id = s.calibration_model_id.clone(); + let source_node_ids: Vec = s.calibration_source_node_ids.iter().copied().collect(); + let source_grid = s.calibration_grid_binding.map(|binding| binding.grid); + let reordered_packets_by_node = s.calibration_reordered_packets.clone(); + let max_reorder_depth_by_node = s.calibration_max_reorder_depth.clone(); + if !s.calibration_sequence_fault_node_ids.is_empty() { + return Json(serde_json::json!({ + "success": false, + "error_code": "calibration_sequence_discontinuity", + "error": "Raw CSI sequence moved backward beyond the bounded UDP reorder window. Cancel and restart the empty-room calibration.", + "model_id": model_id, + "source_node_ids": source_node_ids, + "source_grid": source_grid, + "reordered_packets_by_node": reordered_packets_by_node, + "max_reorder_depth_by_node": max_reorder_depth_by_node, + "sequence_fault_node_ids": s.calibration_sequence_fault_node_ids, + })); + } + let Some(binding) = s.calibration_grid_binding else { + return Json(serde_json::json!({ + "success": false, + "error_code": "calibration_grid_identity_missing", + "error": "The active calibration has no frozen CSI grid identity. Cancel and restart the empty room capture.", + "model_id": model_id, + "source_node_ids": source_node_ids, + })); + }; + if source_node_ids.as_slice() != [binding.source_node_id] + || !s.calibration_grid_is_fresh(binding, std::time::Instant::now()) + { + let latest_seen_ms = s + .node_states + .get(&binding.source_node_id) + .and_then(|node| node.field_model_latest_seen) + .map(|seen| { + std::time::Instant::now() + .saturating_duration_since(seen) + .as_millis() as u64 + }); + return Json(serde_json::json!({ + "success": false, + "error_code": "calibration_grid_stale", + "error": "The frozen calibration CSI grid is stale or no longer matches its source. Restore the source and retry before finalizing.", + "model_id": model_id, + "source_node_ids": source_node_ids, + "source_grid": source_grid, + "latest_seen_ms": latest_seen_ms, + })); + } if let Some(ref mut fm) = s.field_model { // Guard: finalizing before enough empty-room frames have accumulated // is a client-side sequencing error, not a server fault. Return a @@ -5822,25 +7406,53 @@ async fn calibration_stop(State(state): State) -> Json 0.0 && elapsed_s < need_s) { + let mut resp = serde_json::json!({ "success": false, - "error": "Not enough calibration frames yet — keep the room empty and poll /calibration/status until frame_count reaches the target.", + "error": "Calibration not complete yet — keep the room empty and poll /calibration/status until both frame_count and elapsed_s reach their targets.", "frame_count": have, "frames_needed": need, - })); + "elapsed_s": elapsed_s, + "duration_needed_s": need_s, + "frames_per_second": fm.calibration_frames_per_second(), + }); + // #1756: a fast fleet satisfies the frame gate in ~1/N of the + // intended wall-clock window. Make that explicit instead of + // silently collecting, so the operator sees why the session + // keeps running. + if have >= need && elapsed_s < need_s { + tracing::warn!( + "Calibration frame target reached in {elapsed_s:.1}s — substantially less than the intended {need_s:.0}s window; continuing to collect for slow environmental variation (#1756)" + ); + resp["warning"] = serde_json::json!(format!( + "frame target reached in {elapsed_s:.1}s — substantially less than the intended {need_s:.0}s window; continuing to collect for slow environmental variation" + )); + } + return Json(resp); } let ts = chrono::Utc::now().timestamp_micros() as u64; match fm.finalize_calibration(ts, 0) { Ok(modes) => { let baseline = modes.baseline_eigenvalue_count; let variance_explained = modes.variance_explained; + let holdout_window_size = modes.baseline_runtime_window_size; + let final_frame_count = fm.calibration_frame_count(); + s.begin_field_model_holdout(binding); info!("Field model calibrated: baseline_eigenvalues={baseline}, variance_explained={variance_explained:.2}"); Json(serde_json::json!({ "success": true, "baseline_eigenvalue_count": baseline, "variance_explained": variance_explained, - "frame_count": fm.calibration_frame_count(), + "frame_count": final_frame_count, + "elapsed_s": elapsed_s, + "holdout_window_size": holdout_window_size, + "model_id": model_id, + "source_node_ids": source_node_ids, + "source_grid": source_grid, + "reordered_packets_by_node": reordered_packets_by_node, + "max_reorder_depth_by_node": max_reorder_depth_by_node, })) } // ADR-080 #2: finalize error chain stays server-side only. @@ -5856,17 +7468,694 @@ async fn calibration_stop(State(state): State) -> Json) -> Json { let s = state.read().await; - match s.field_model.as_ref() { - Some(fm) => Json(serde_json::json!({ - "active": true, - "status": format!("{:?}", fm.status()), - "frame_count": fm.calibration_frame_count(), - })), - None => Json(serde_json::json!({ + let now = std::time::Instant::now(); + let observed_at_unix_ms = chrono::Utc::now().timestamp_millis().max(0) as u64; + let effective_status = s.field_model_status_at(observed_at_unix_ms); + let active = s.field_model_active_at(observed_at_unix_ms); + let bootstrap_active = s.bootstrap_baseline_active_at(observed_at_unix_ms); + let bootstrap_background_match = s.bootstrap_background_match(observed_at_unix_ms); + let runtime_reference = s.field_model.as_ref().and_then(|model| { + model.modes().map(|modes| serde_json::json!({ + "window_size": modes.baseline_runtime_window_size, + "significant_eigenvalue_p95": modes.baseline_runtime_eigenvalue_count, + "residual_energy_threshold": modes.empty_room_residual_energy_threshold, + "residual_reference_window_count": modes.empty_room_residual_energy_reference.len(), + "residual_refinement_count": modes.empty_room_residual_refinement_count, + "raw_calibration_frames_persisted": false, + })) + }); + let (frame_count, min_frames, elapsed_s, frames_per_second, min_duration_s) = s + .field_model + .as_ref() + .map_or((0, 0, 0.0, 0.0, 0.0), |model| { + ( + model.calibration_frame_count(), + model.min_calibration_frames(), + model.calibration_elapsed_s(), + model.calibration_frames_per_second(), + model.min_calibration_duration_s(), + ) + }); + let status = effective_status.map_or_else( + || "none".to_string(), + |status| format!("{status:?}").to_lowercase(), + ); + let grid_binding = s.calibration_grid_binding.map(|binding| { + let node = s.node_states.get(&binding.source_node_id); + let latest_seen_ms = node + .and_then(|node| node.field_model_latest_seen) + .map(|seen| now.saturating_duration_since(seen).as_millis() as u64); + serde_json::json!({ + "source_node_id": binding.source_node_id, + "grid": binding.grid, + "selection_evidence": binding.evidence, + "latest_seen_ms": latest_seen_ms, + "runtime_history_frames": node.map_or(0, |node| node.field_model_history.len()), + "runtime_history_capacity": FRAME_HISTORY_CAPACITY, + "status": if latest_seen_ms.is_some_and(|age| age < ESP32_OFFLINE_TIMEOUT.as_millis() as u64) + && !s.calibration_sequence_fault_node_ids.contains(&binding.source_node_id) + { "active" } else { "stale" }, + }) + }); + Json(serde_json::json!({ + "active": active, + "status": status, + "frame_count": frame_count, + "min_frames": min_frames, + "elapsed_s": elapsed_s, + "frames_per_second": frames_per_second, + "min_duration_s": min_duration_s, + "model_id": s.calibration_model_id, + "source_node_ids": s.calibration_source_node_ids, + "grid_binding": grid_binding, + "last_sequence_by_node": s.calibration_last_sequences, + "sequence_policy": "forward_only_with_bounded_udp_reorder_drop_v1", + "reorder_window_sequences": CALIBRATION_SEQUENCE_REORDER_WINDOW, + "reordered_packets_by_node": s.calibration_reordered_packets, + "max_reorder_depth_by_node": s.calibration_max_reorder_depth, + "sequence_fault_node_ids": s.calibration_sequence_fault_node_ids, + "runtime_reference": runtime_reference, + "binding_mode": if bootstrap_active { "bootstrap_only" } else if active { "runtime" } else { "none" }, + "bootstrap_baseline": s.bootstrap_baseline.as_ref().map(|metadata| serde_json::json!({ + "stored": true, + "active": bootstrap_active, + "authority": metadata.authority, + "source_node_ids": metadata.source_node_ids, + "source_grid": metadata.source_grid, + "source_model_id": metadata.source_model_id, + "created_at_unix_ms": metadata.created_at_unix_ms, + "expires_at_unix_ms": metadata.expires_at_unix_ms, + "content_sha256": metadata.content_sha256, + "background_match": bootstrap_background_match.map(|result| serde_json::json!({ + "state": if result.matches_empty { "matched" } else { "changed" }, + "matches_empty": result.matches_empty, + "score": result.score, + "normalized_residual_z": result.normalized_residual_z, + "maturity": result.maturity, + "reliable": result.reliable, + "residual_energy": result.residual_energy, + "residual_energy_threshold": result.residual_energy_threshold, + "window_size": result.window_size, + "reference_window_count": result.reference_window_count, + })), + "calibrated_evidence_authorized": false, + "numeric_vitals_authorized": false, + })).unwrap_or_else(|| serde_json::json!({ + "stored": false, "active": false, - "status": "none", + "authority": "none", + "calibrated_evidence_authorized": false, + "numeric_vitals_authorized": false, })), + })) +} + +/// Validate a completed empty room model against twelve fresh server observed +/// samples, then persist only aggregate statistics. The request cannot supply +/// scores, labels, raw CSI, or a replacement model. +async fn calibration_promote_bootstrap( + State(state): State, +) -> Json { + let (expected_model_id, expected_binding, holdout_window_size) = { + let s = state.read().await; + let observed_at_unix_ms = chrono::Utc::now().timestamp_millis().max(0) as u64; + if s.bootstrap_baseline_active || !s.explicit_calibration_fresh_at(observed_at_unix_ms) { + return Json(serde_json::json!({ + "success": false, + "error_code": "calibration_not_finalized", + "error": "Finalize an explicit room calibration before validating a startup baseline.", + })); + } + let model_id = match s.calibration_model_id.clone() { + Some(model_id) => model_id, + None => { + return Json(serde_json::json!({ + "success": false, + "error_code": "calibration_model_identity_missing", + "error": "The active calibration has no server generated model identity.", + })); + } + }; + let Some(binding) = s.calibration_grid_binding else { + return Json(serde_json::json!({ + "success": false, + "error_code": "calibration_grid_identity_missing", + "error": "The completed calibration has no frozen CSI grid identity.", + })); + }; + if s.calibration_sequence_fault_node_ids + .contains(&binding.source_node_id) + { + return Json(serde_json::json!({ + "success": false, + "error_code": "calibration_sequence_discontinuity", + "error": "The calibration CSI sequence faulted before validation. The model was not stored.", + })); + } + let Some(runtime_window_size) = s + .field_model + .as_ref() + .and_then(|model| model.modes()) + .and_then(|modes| modes.baseline_runtime_window_size) + .filter(|window_size| *window_size > 0 && *window_size <= FRAME_HISTORY_CAPACITY) + else { + return Json(serde_json::json!({ + "success": false, + "error_code": "calibration_runtime_window_invalid", + "error": "The completed calibration has no valid runtime scoring window.", + })); + }; + (model_id, binding, runtime_window_size) + }; + + let prewarm_deadline = tokio::time::Instant::now() + + Duration::from_millis(BOOTSTRAP_HOLDOUT_PREWARM_TIMEOUT_MS); + loop { + let (identity_matches, sequence_faulted, ready, current_window_size) = { + let s = state.read().await; + let observed_at_unix_ms = chrono::Utc::now().timestamp_millis().max(0) as u64; + let identity_matches = s.calibration_model_id.as_deref() + == Some(expected_model_id.as_str()) + && s.explicit_calibration_fresh_at(observed_at_unix_ms) + && s.calibration_grid_binding.is_some_and(|binding| { + binding.source_node_id == expected_binding.source_node_id + && binding.grid == expected_binding.grid + }); + let sequence_faulted = s + .calibration_sequence_fault_node_ids + .contains(&expected_binding.source_node_id); + let current_window_size = s + .node_states + .get(&expected_binding.source_node_id) + .map_or(0, |node| node.field_model_history.len()); + ( + identity_matches, + sequence_faulted, + s.field_model_holdout_ready( + expected_binding, + holdout_window_size, + std::time::Instant::now(), + ), + current_window_size, + ) + }; + if sequence_faulted { + return Json(serde_json::json!({ + "success": false, + "error_code": "calibration_sequence_discontinuity", + "error": "The calibration CSI sequence faulted while preparing held out validation. The model was not stored.", + })); + } + if !identity_matches { + return Json(serde_json::json!({ + "success": false, + "error_code": "calibration_model_changed", + "error": "Calibration changed while preparing held out validation. The model was not stored.", + })); + } + if ready { + break; + } + if tokio::time::Instant::now() >= prewarm_deadline { + return Json(serde_json::json!({ + "success": false, + "error_code": "bootstrap_holdout_prewarm_timeout", + "error": "A complete fresh CSI scoring window was not available before the held out validation timeout. The model was not stored.", + "required_window_size": holdout_window_size, + "current_window_size": current_window_size, + })); + } + tokio::time::sleep(Duration::from_millis(50)).await; } + + let mut samples = Vec::with_capacity(BOOTSTRAP_VALIDATION_SAMPLES); + let mut last_sequence = None; + let mut next_sample_at = tokio::time::Instant::now(); + for _ in 0..BOOTSTRAP_VALIDATION_SAMPLES { + tokio::time::sleep_until(next_sample_at).await; + let interval_deadline = tokio::time::Instant::now() + + Duration::from_millis(BOOTSTRAP_VALIDATION_SAMPLE_TIMEOUT_MS); + let mut sample = BootstrapValidationSample { + fresh_tick: false, + calibrated_empty: false, + vital_signs_absent: true, + }; + while tokio::time::Instant::now() < interval_deadline { + let observed = { + let s = state.read().await; + let observed_at_unix_ms = chrono::Utc::now().timestamp_millis().max(0) as u64; + if s.calibration_model_id.as_deref() != Some(expected_model_id.as_str()) + || !s.explicit_calibration_fresh_at(observed_at_unix_ms) + || !s.calibration_grid_binding.is_some_and(|binding| { + binding.source_node_id == expected_binding.source_node_id + && binding.grid == expected_binding.grid + }) + || s.calibration_sequence_fault_node_ids + .contains(&expected_binding.source_node_id) + { + None + } else { + s.node_states + .get(&expected_binding.source_node_id) + .and_then(|node| { + let sequence = node.field_model_latest_sequence?; + let fresh = node.field_model_latest_seen.is_some_and(|seen| { + std::time::Instant::now().saturating_duration_since(seen) + < ESP32_OFFLINE_TIMEOUT + }) && Some(sequence) != last_sequence; + let held_out = s.field_model_holdout_ready( + expected_binding, + holdout_window_size, + std::time::Instant::now(), + ); + (fresh && held_out).then_some(( + sequence, + s.person_count() == 0, + s.latest_update + .as_ref() + .is_none_or(|update| update.vital_signs.is_none()), + )) + }) + } + }; + if let Some((sequence, calibrated_empty, vital_signs_absent)) = observed { + last_sequence = Some(sequence); + sample = BootstrapValidationSample { + fresh_tick: true, + calibrated_empty, + vital_signs_absent, + }; + break; + } + tokio::time::sleep(Duration::from_millis(50)).await; + } + samples.push(sample); + next_sample_at = tokio::time::Instant::now() + + Duration::from_millis(BOOTSTRAP_VALIDATION_MIN_SPACING_MS); + } + + let validation = bootstrap_baseline::evaluate_validation(&samples); + if !validation.passed { + return Json(serde_json::json!({ + "success": false, + "error_code": "bootstrap_validation_failed", + "error": "The held out empty room check did not pass. The model was not stored.", + "validation": validation, + })); + } + + let mut s = state.write().await; + let observed_at_unix_ms = chrono::Utc::now().timestamp_millis().max(0) as u64; + if s.calibration_model_id.as_deref() != Some(expected_model_id.as_str()) + || !s.explicit_calibration_fresh_at(observed_at_unix_ms) + || !s.calibration_grid_binding.is_some_and(|binding| { + binding.source_node_id == expected_binding.source_node_id + && binding.grid == expected_binding.grid + }) + || s.calibration_sequence_fault_node_ids + .contains(&expected_binding.source_node_id) + { + return Json(serde_json::json!({ + "success": false, + "error_code": "calibration_model_changed", + "error": "Calibration changed during validation. The model was not stored.", + })); + } + let Some(installation_id) = s.installation_id.clone() else { + return Json(serde_json::json!({ + "success": false, + "error_code": "installation_id_required", + "error": "A stable installation identity is required to store a local startup baseline.", + })); + }; + let source_node_ids: Vec = s.calibration_source_node_ids.iter().copied().collect(); + if source_node_ids.len() != 1 { + return Json(serde_json::json!({ + "success": false, + "error_code": "calibration_source_nodes_missing", + "error": "The single-link startup baseline must contain exactly one contributing ESP32 node.", + })); + } + let Some(field_model) = s.field_model.as_ref() else { + return Json(serde_json::json!({ + "success": false, + "error_code": "no_field_model", + "error": "Calibration changed during validation. The model was not stored.", + })); + }; + let path = bootstrap_baseline::path_in(&s.data_dir); + let created_at_unix_ms = chrono::Utc::now().timestamp_millis() as u64; + let stored = match bootstrap_baseline::store( + &path, + &installation_id, + &source_node_ids, + expected_binding.grid.as_bootstrap_grid(), + &expected_model_id, + created_at_unix_ms, + field_model, + ) { + Ok(metadata) => metadata, + Err(error) => { + return error_response::internal_error_json("bootstrap baseline store", error) + } + }; + s.bootstrap_baseline = Some(stored.clone()); + info!( + model_id = %stored.source_model_id, + source_node_ids = ?stored.source_node_ids, + content_sha256 = %stored.content_sha256, + "Stored validated privacy reduced startup baseline" + ); + Json(serde_json::json!({ + "success": true, + "message": "Validated startup baseline stored locally.", + "validation": validation, + "holdout_window_size": holdout_window_size, + "bootstrap_baseline": stored, + "calibrated_evidence_authorized": false, + "numeric_vitals_authorized": false, + })) +} + +/// Measure one separate operator-confirmed empty holdout and make a bounded, +/// single-use adjustment to a restored bootstrap residual boundary. Only +/// scalar residual energies enter the persisted image; raw CSI never does. +async fn calibration_refine_bootstrap( + State(state): State, + Query(query): Query, +) -> Json { + if !query.confirmed_empty { + return Json(serde_json::json!({ + "success": false, + "error_code": "empty_confirmation_required", + "error": "Explicit empty-room confirmation is required for bootstrap refinement.", + })); + } + + let (expected_model_id, expected_digest, source_node_id, expected_grid) = { + let s = state.read().await; + let observed_at_unix_ms = chrono::Utc::now().timestamp_millis().max(0) as u64; + let Some(metadata) = s + .bootstrap_baseline + .as_ref() + .filter(|_| s.bootstrap_baseline_active_at(observed_at_unix_ms)) + else { + return Json(serde_json::json!({ + "success": false, + "error_code": "bootstrap_not_active", + "error": "A restored startup baseline must be active before refinement.", + })); + }; + let [source_node_id] = metadata.source_node_ids.as_slice() else { + return Json(serde_json::json!({ + "success": false, + "error_code": "bootstrap_source_invalid", + "error": "Bootstrap refinement requires exactly one bound source node.", + })); + }; + if query + .source_node_id + .is_some_and(|requested| requested != *source_node_id) + { + return Json(serde_json::json!({ + "success": false, + "error_code": "bootstrap_source_mismatch", + "error": "The requested source does not match the stored bootstrap source.", + })); + } + ( + metadata.source_model_id.clone(), + metadata.content_sha256.clone(), + *source_node_id, + CsiGridKey::from_bootstrap_grid(metadata.source_grid), + ) + }; + + let mut residuals = Vec::with_capacity(BOOTSTRAP_VALIDATION_SAMPLES); + let mut stale_sample_count = 0usize; + let mut vital_sign_sample_count = 0usize; + let mut last_sequence = None; + let mut next_sample_at = tokio::time::Instant::now(); + for _ in 0..BOOTSTRAP_VALIDATION_SAMPLES { + tokio::time::sleep_until(next_sample_at).await; + let interval_deadline = tokio::time::Instant::now() + + Duration::from_millis(BOOTSTRAP_VALIDATION_SAMPLE_TIMEOUT_MS); + let mut observed = None; + while tokio::time::Instant::now() < interval_deadline { + observed = { + let s = state.read().await; + let observed_at_unix_ms = chrono::Utc::now().timestamp_millis().max(0) as u64; + let identity_matches = s.bootstrap_baseline_active_at(observed_at_unix_ms) + && s.bootstrap_baseline.as_ref().is_some_and(|metadata| { + metadata.source_model_id == expected_model_id + && metadata.content_sha256 == expected_digest + && metadata.source_node_ids.as_slice() == [source_node_id] + && CsiGridKey::from_bootstrap_grid(metadata.source_grid) + == expected_grid + }) + && s.calibration_grid_binding.is_some_and(|binding| { + binding.source_node_id == source_node_id + && binding.grid == expected_grid + }); + if !identity_matches { + None + } else { + s.node_states.get(&source_node_id).and_then(|node| { + let sequence = node.field_model_latest_sequence?; + let fresh = node.field_model_latest_seen.is_some_and(|seen| { + std::time::Instant::now().saturating_duration_since(seen) + < ESP32_OFFLINE_TIMEOUT + }) && Some(sequence) != last_sequence; + if !fresh { + return None; + } + let model = s.field_model.as_ref()?; + let recent_frames: Vec> = + node.field_model_history.iter().cloned().collect(); + let residual = model.empty_room_match(&recent_frames)?.residual_energy; + let vital_signs_absent = s + .latest_update + .as_ref() + .is_some_and(|update| update.vital_signs.is_none()); + Some((sequence, residual, vital_signs_absent)) + }) + } + }; + if observed.is_some() { + break; + } + tokio::time::sleep(Duration::from_millis(50)).await; + } + if let Some((sequence, residual, vital_signs_absent)) = observed { + last_sequence = Some(sequence); + residuals.push(residual); + if !vital_signs_absent { + vital_sign_sample_count += 1; + } + } else { + stale_sample_count += 1; + } + next_sample_at = tokio::time::Instant::now() + + Duration::from_millis(BOOTSTRAP_VALIDATION_MIN_SPACING_MS); + } + + if residuals.len() != BOOTSTRAP_VALIDATION_SAMPLES + || stale_sample_count != 0 + || vital_sign_sample_count != 0 + { + return Json(serde_json::json!({ + "success": false, + "error_code": "bootstrap_refinement_validation_failed", + "error": "The fresh empty-room refinement gate did not pass. The stored baseline was not changed.", + "validation": { + "sample_count": residuals.len(), + "stale_sample_count": stale_sample_count, + "vital_sign_sample_count": vital_sign_sample_count, + }, + })); + } + + let mut s = state.write().await; + let observed_at_unix_ms = chrono::Utc::now().timestamp_millis().max(0) as u64; + let identity_matches = s.bootstrap_baseline_active_at(observed_at_unix_ms) + && s.bootstrap_baseline.as_ref().is_some_and(|metadata| { + metadata.source_model_id == expected_model_id + && metadata.content_sha256 == expected_digest + && metadata.source_node_ids.as_slice() == [source_node_id] + && CsiGridKey::from_bootstrap_grid(metadata.source_grid) == expected_grid + }) + && s.calibration_grid_binding.is_some_and(|binding| { + binding.source_node_id == source_node_id && binding.grid == expected_grid + }); + if !identity_matches { + return Json(serde_json::json!({ + "success": false, + "error_code": "bootstrap_changed", + "error": "The bootstrap identity changed during refinement. The stored baseline was not changed.", + })); + } + let candidate_snapshot = match s + .field_model + .as_ref() + .ok_or(wifi_densepose_signal::ruvsense::field_model::FieldModelError::NotCalibrated) + .and_then(|model| model.export_snapshot()) + { + Ok(snapshot) => snapshot, + Err(_) => { + return Json(serde_json::json!({ + "success": false, + "error_code": "bootstrap_refinement_rejected", + "error": "The bootstrap model rejected this bounded refinement.", + })); + } + }; + let now_us = chrono::Utc::now().timestamp_micros() as u64; + let mut candidate = match FieldModel::from_snapshot(candidate_snapshot, now_us) { + Ok(model) => model, + Err(_) => { + return Json(serde_json::json!({ + "success": false, + "error_code": "bootstrap_refinement_rejected", + "error": "The bootstrap model rejected this bounded refinement.", + })); + } + }; + let refinement = match candidate.refine_empty_room_residual_boundary(&residuals) { + Ok(refinement) => refinement, + Err(_) => { + return Json(serde_json::json!({ + "success": false, + "error_code": "bootstrap_refinement_rejected", + "error": "The bootstrap model rejected this bounded refinement.", + })); + } + }; + let Some(installation_id) = s.installation_id.clone() else { + return Json(serde_json::json!({ + "success": false, + "error_code": "installation_id_required", + "error": "A stable installation identity is required to store a local startup baseline.", + })); + }; + let path = bootstrap_baseline::path_in(&s.data_dir); + let created_at_unix_ms = chrono::Utc::now().timestamp_millis() as u64; + let stored = match bootstrap_baseline::store( + &path, + &installation_id, + &[source_node_id], + expected_grid.as_bootstrap_grid(), + &expected_model_id, + created_at_unix_ms, + &candidate, + ) { + Ok(metadata) => metadata, + Err(error) => { + return error_response::internal_error_json("bootstrap baseline refinement store", error) + } + }; + s.field_model = Some(candidate); + s.bootstrap_baseline = Some(stored.clone()); + Json(serde_json::json!({ + "success": true, + "message": "The local empty-room startup baseline was refined.", + "validation": { + "sample_count": residuals.len(), + "stale_sample_count": stale_sample_count, + "vital_sign_sample_count": vital_sign_sample_count, + }, + "refinement": { + "threshold_before": refinement.threshold_before, + "threshold_after": refinement.threshold_after, + "held_out_p95": refinement.held_out_p95, + "held_out_sample_count": refinement.held_out_sample_count, + "reference_window_count": refinement.reference_window_count, + }, + "bootstrap_baseline": stored, + "calibrated_evidence_authorized": false, + "numeric_vitals_authorized": false, + })) +} + +/// Cancel only an unfinished capture. Completed model deletion remains on the +/// administrator scoped reset route. +async fn calibration_cancel(State(state): State) -> Json { + let mut s = state.write().await; + let cancellable = s.field_model.as_ref().is_some_and(|model| { + matches!( + model.status(), + CalibrationStatus::Uncalibrated | CalibrationStatus::Collecting + ) + }); + if !cancellable { + return Json(serde_json::json!({ + "success": false, + "error_code": "calibration_not_collecting", + "error": "Only an unfinished empty room capture can be cancelled.", + })); + } + s.field_model = None; + s.calibration_model_id = None; + s.calibration_source_node_ids.clear(); + s.clear_field_model_binding(); + s.clear_calibration_sequence_state(); + if let Some(installation_id) = s.installation_id.clone() { + let path = bootstrap_baseline::path_in(&s.data_dir); + if let Ok((model, metadata)) = bootstrap_baseline::load( + &path, + &installation_id, + chrono::Utc::now().timestamp_millis() as u64, + ) { + let [source_node_id] = metadata.source_node_ids.as_slice() else { + return Json(serde_json::json!({ + "success": false, + "error_code": "bootstrap_source_invalid", + "error": "The stored bootstrap source identity is invalid.", + })); + }; + let binding = CalibrationGridBinding { + source_node_id: *source_node_id, + grid: CsiGridKey::from_bootstrap_grid(metadata.source_grid), + evidence: None, + }; + s.field_model = Some(model); + s.calibration_source_node_ids.insert(*source_node_id); + s.calibration_grid_binding = Some(binding); + s.bootstrap_baseline = Some(metadata); + s.bootstrap_baseline_active = true; + } + } + Json(serde_json::json!({ + "success": true, + "message": "Unfinished empty room capture cancelled.", + "status": if s.bootstrap_baseline_active { "bootstrap" } else { "none" }, + })) +} + +async fn calibration_reset(State(state): State) -> Json { + let mut s = state.write().await; + s.field_model = None; + s.calibration_model_id = None; + s.calibration_source_node_ids.clear(); + s.clear_field_model_binding(); + s.clear_calibration_sequence_state(); + s.bootstrap_baseline_active = false; + let path = bootstrap_baseline::path_in(&s.data_dir); + let bootstrap_removed = match bootstrap_baseline::remove(&path) { + Ok(removed) => { + s.bootstrap_baseline = None; + removed + } + Err(error) => { + warn!(%error, "Could not remove stored bootstrap baseline during reset"); + false + } + }; + Json(serde_json::json!({ + "success": true, + "message": "Calibration model reset.", + "status": "none", + "bootstrap_removed": bootstrap_removed, + })) } /// Compatibility surface used by the bundled dashboard. Activity history is @@ -5891,16 +8180,26 @@ fn chrono_timestamp() -> u64 { async fn vital_signs_endpoint(State(state): State) -> Json { let s = state.read().await; - let vs = &s.latest_vitals; + let observed_at_unix_ms = chrono::Utc::now().timestamp_millis().max(0) as u64; + let person_count = s.person_count_at(observed_at_unix_ms); + let explicit_calibration_fresh = + s.explicit_calibration_fresh_at(observed_at_unix_ms); + let published = vitals_for_publication( + &s.latest_vitals, + explicit_calibration_fresh, + person_count, + ); let (br_len, br_cap, hb_len, hb_cap) = s.vital_detector.buffer_status(); Json(serde_json::json!({ "vital_signs": { - "breathing_rate_bpm": vs.breathing_rate_bpm, - "heart_rate_bpm": vs.heart_rate_bpm, - "breathing_confidence": vs.breathing_confidence, - "heartbeat_confidence": vs.heartbeat_confidence, - "signal_quality": vs.signal_quality, + "breathing_rate_bpm": published.as_ref().and_then(|value| value.breathing_rate_bpm), + "heart_rate_bpm": published.as_ref().and_then(|value| value.heart_rate_bpm), + "breathing_confidence": published.as_ref().map(|value| value.breathing_confidence), + "heartbeat_confidence": published.as_ref().map(|value| value.heartbeat_confidence), + "signal_quality": published.as_ref().map(|value| value.signal_quality), }, + "authority": if published.is_some() { "explicit_calibration" } else { "abstained" }, + "abstention_reason": if published.is_some() { serde_json::Value::Null } else { serde_json::json!("fresh explicit calibration with exactly one occupant and qualified evidence required") }, "buffer_status": { "breathing_samples": br_len, "breathing_capacity": br_cap, @@ -5965,6 +8264,16 @@ async fn edge_registry_endpoint( /// GET /api/v1/edge-vitals — latest edge vitals from ESP32 (ADR-039). async fn edge_vitals_endpoint(State(state): State) -> Json { let s = state.read().await; + let observed_at_unix_ms = chrono::Utc::now().timestamp_millis().max(0) as u64; + let explicit_single_occupant = s.explicit_calibration_fresh_at(observed_at_unix_ms) + && s.person_count_at(observed_at_unix_ms) == 1; + if !explicit_single_occupant { + return Json(serde_json::json!({ + "status": "abstained", + "edge_vitals": null, + "message": "Fresh explicit calibration with exactly one occupant is required.", + })); + } match &s.edge_vitals { Some(v) => Json(serde_json::json!({ "status": "ok", @@ -6220,6 +8529,7 @@ async fn mesh_endpoint(State(state): State) -> Json) -> Json { let s = state.read().await; let now = std::time::Instant::now(); + let observed_at_unix_ms = chrono::Utc::now().timestamp_millis().max(0) as u64; let nodes: Vec = s .node_states .iter() @@ -6228,16 +8538,35 @@ async fn nodes_endpoint(State(state): State) -> Json 5000; let status = if stale { "stale" } else { "active" }; + let csi_status = if csi_elapsed_ms.is_some_and(|age| { + age < ESP32_OFFLINE_TIMEOUT.as_millis() as u64 + }) { + "active" + } else { + "stale" + }; let rssi = ns.rssi_history.back().copied().unwrap_or(-90.0); + let bootstrap_empty = s.bootstrap_baseline_active + && s.bootstrap_baseline.as_ref().is_some_and(|metadata| { + metadata.source_node_ids.as_slice() == [id] + }) + && s.bootstrap_background_match(observed_at_unix_ms) + .is_some_and(|result| result.matches_empty); serde_json::json!({ "node_id": id, "status": status, "last_seen_ms": elapsed_ms, + "csi_status": csi_status, + "csi_last_seen_ms": csi_elapsed_ms, + "csi_sequence": ns.latest_csi_sequence, "rssi_dbm": rssi, - "motion_level": &ns.current_motion_level, - "person_count": ns.prev_person_count, + "motion_level": if bootstrap_empty { "absent" } else { &ns.current_motion_level }, + "person_count": if bootstrap_empty { 0 } else { ns.prev_person_count }, "person_count_valid": ns.edge_vitals .as_ref() .map(|vitals| vitals.person_count_valid), @@ -6267,6 +8596,99 @@ async fn info_page() -> Html { ) } +/// Resolve `--node-positions` entries to the currently active node IDs. +/// +/// `MultistaticFuser::fuse` assigns `node_positions[i]` to the i-th node in +/// its `node_frames` argument, which callers build sorted ascending by +/// node_id (see `node_frames_from_states_with_guard`). Real fleets use +/// non-sequential logical node IDs (e.g. 11, 12, 13), so a naive +/// `node_id -> node_positions_config[node_id]` lookup silently misses for +/// every real deployment. This mirrors the same ascending-rank convention so +/// the live `NodeInfo.position` field agrees with what fusion actually used. +fn node_positions_by_active_id( + node_positions_config: &[[f32; 3]], + node_states: &HashMap, + now: std::time::Instant, +) -> HashMap { + let mut active_ids: Vec = node_states + .iter() + .filter(|(_, n)| { + n.last_frame_time + .is_some_and(|t| now.duration_since(t).as_secs() < 10) + }) + .map(|(&id, _)| id) + .collect(); + active_ids.sort_unstable(); + active_ids + .into_iter() + .enumerate() + .filter_map(|(rank, id)| { + node_positions_config + .get(rank) + .map(|p| (id, [p[0] as f64, p[1] as f64, p[2] as f64])) + }) + .collect() +} + +#[cfg(test)] +mod node_positions_by_active_id_tests { + use super::*; + + fn active_node(now: std::time::Instant) -> NodeState { + let mut ns = NodeState::new(); + ns.last_frame_time = Some(now); + ns + } + + #[test] + fn non_sequential_node_ids_get_positions_by_ascending_rank() { + // Real fleets use logical IDs like 11, 12, 13 — not 0, 1, 2. The + // configured position list must map by ascending node_id rank, not + // by treating node_id as a direct index into the list. + let now = std::time::Instant::now(); + let mut node_states = HashMap::new(); + node_states.insert(13, active_node(now)); + node_states.insert(11, active_node(now)); + node_states.insert(12, active_node(now)); + + let configured = [[1.0, 0.0, 0.0], [2.0, 0.0, 0.0], [3.0, 0.0, 0.0]]; + let resolved = node_positions_by_active_id(&configured, &node_states, now); + + assert_eq!(resolved.get(&11), Some(&[1.0, 0.0, 0.0])); + assert_eq!(resolved.get(&12), Some(&[2.0, 0.0, 0.0])); + assert_eq!(resolved.get(&13), Some(&[3.0, 0.0, 0.0])); + } + + #[test] + fn stale_nodes_are_excluded_from_rank_assignment() { + let now = std::time::Instant::now(); + let mut node_states = HashMap::new(); + node_states.insert(11, active_node(now)); + let mut stale = NodeState::new(); + stale.last_frame_time = + Some(now - std::time::Duration::from_secs(30)); + node_states.insert(12, stale); + node_states.insert(13, active_node(now)); + + let configured = [[1.0, 0.0, 0.0], [3.0, 0.0, 0.0]]; + let resolved = node_positions_by_active_id(&configured, &node_states, now); + + assert_eq!(resolved.get(&11), Some(&[1.0, 0.0, 0.0])); + assert_eq!(resolved.get(&12), None, "stale node must not consume a rank"); + assert_eq!(resolved.get(&13), Some(&[3.0, 0.0, 0.0])); + } + + #[test] + fn missing_config_entries_fall_through_to_the_caller_default() { + let now = std::time::Instant::now(); + let mut node_states = HashMap::new(); + node_states.insert(11, active_node(now)); + + let resolved = node_positions_by_active_id(&[], &node_states, now); + assert_eq!(resolved.get(&11), None); + } +} + // ── UDP receiver task ──────────────────────────────────────────────────────── async fn udp_receiver_task( @@ -6390,24 +8812,6 @@ async fn udp_receiver_task( vitals.presence ); let mut s = state.write().await; - // Broadcast vitals via WebSocket. - if let Ok(json) = serde_json::to_string(&serde_json::json!({ - "type": "edge_vitals", - "node_id": vitals.node_id, - "presence": vitals.presence, - "fall_detected": vitals.fall_detected, - "motion": vitals.motion, - "breathing_rate_bpm": vitals.breathing_rate_bpm, - "heartrate_bpm": vitals.heartrate_bpm, - "n_persons": vitals.n_persons, - "person_count_valid": vitals.person_count_valid, - "motion_energy": vitals.motion_energy, - "presence_score": vitals.presence_score, - "rssi": vitals.rssi, - })) { - let _ = s.tx.send(json); - } - // Issue #323: Also emit a sensing_update so the UI renders // detections for ESP32 nodes running the edge DSP pipeline // (Tier 2+). Without this, vitals arrive but the UI shows @@ -6455,9 +8859,18 @@ async fn udp_receiver_task( 0.05 }; - // Aggregate person count: gate on presence first (matching WiFi path). + let observed_at_unix_ms = + chrono::Utc::now().timestamp_millis().max(0) as u64; + let bootstrap_empty = + s.bootstrap_empty_prior_applies(observed_at_unix_ms); + + // A startup prior has negative-only authority. Once a full + // runtime window matches the stored background, raw edge + // presence cannot force the count back to one. let now = std::time::Instant::now(); - let total_persons = if vitals.presence { + let total_persons = if bootstrap_empty { + 0 + } else if vitals.presence { let dedup = s.dedup_factor; let (fused, fallback_count) = multistatic_bridge::fuse_or_fallback( &s.multistatic_fuser, @@ -6475,7 +8888,10 @@ async fn udp_receiver_task( // #803: don't let the saturating activity score // discard count-aware per-node estimates. let count = - aggregate_person_count(s.person_count(), &s.node_states); + aggregate_person_count( + s.person_count_at(observed_at_unix_ms), + &s.node_states, + ); s.prev_person_count = count; count.max(1) // presence=true => at least 1 } @@ -6504,18 +8920,9 @@ async fn udp_receiver_task( sref.engine_bridge.observe_cycle(&sref.node_states, now_ms); } - // Feed field model calibration if active (use per-node history for ESP32). - if let Some(frame_history) = s - .node_states - .get(&node_id) - .map(|ns| ns.frame_history.clone()) - { - if let Some(ref mut fm) = s.field_model { - field_bridge::maybe_feed_calibration(fm, &frame_history); - } - } - // Build nodes array with all active nodes. + let resolved_positions = + node_positions_by_active_id(&s.node_positions_config, &s.node_states, now); let active_nodes: Vec = s .node_states .iter() @@ -6526,7 +8933,10 @@ async fn udp_receiver_task( .map(|(&id, n)| NodeInfo { node_id: id, rssi_dbm: n.rssi_history.back().copied().unwrap_or(0.0), - position: [2.0, 0.0, 1.5], + position: resolved_positions + .get(&id) + .copied() + .unwrap_or([2.0, 0.0, 1.5]), amplitude: vec![], subcarrier_count: 0, // Vitals-only path; still expose the sync snapshot @@ -6568,7 +8978,15 @@ async fn udp_receiver_task( // node's own reading no longer overwrites the room's. The // old ad-hoc "boost confidence by node count" is replaced by // `room_inference`'s freshness-weighted multi-node confidence. - let classification = classification_from_room(&room_inference); + let classification = if bootstrap_empty { + ClassificationInfo { + motion_level: "absent".to_string(), + presence: false, + confidence: 0.0, + } + } else { + debounce_room_classification(&mut s, &room_inference) + }; let signal_field = generate_signal_field( fused_features.mean_rssi, @@ -6577,6 +8995,37 @@ async fn udp_receiver_task( (vitals.presence_score as f64).min(1.0), &[], ); + let vital_candidates = VitalSigns { + breathing_rate_bpm: (vitals.breathing_rate_bpm > 0.0) + .then_some(vitals.breathing_rate_bpm), + heart_rate_bpm: (vitals.heartrate_bpm > 0.0) + .then_some(vitals.heartrate_bpm), + breathing_confidence: if vitals.presence { 0.7 } else { 0.0 }, + heartbeat_confidence: if vitals.presence { 0.7 } else { 0.0 }, + signal_quality: vitals.presence_score as f64, + }; + let explicit_calibration_fresh = + s.explicit_calibration_fresh_at(observed_at_unix_ms); + let published_vitals = vitals_for_publication( + &vital_candidates, + explicit_calibration_fresh, + total_persons, + ); + + // Keep the legacy edge_vitals stream aligned with the + // governed sensing envelope. Raw numeric candidates are + // never published before the explicit calibration and + // single-occupant gates have passed. + let edge_vitals_message = edge_vitals_message_for_publication( + &vitals, + published_vitals.as_ref(), + bootstrap_empty, + explicit_calibration_fresh, + total_persons, + ); + if let Ok(json) = serde_json::to_string(&edge_vitals_message) { + let _ = s.tx.send(json); + } let mut update = SensingUpdate { msg_type: "sensing_update".to_string(), @@ -6587,21 +9036,7 @@ async fn udp_receiver_task( features: fused_features.clone(), classification, signal_field, - vital_signs: Some(VitalSigns { - breathing_rate_bpm: if vitals.breathing_rate_bpm > 0.0 { - Some(vitals.breathing_rate_bpm) - } else { - None - }, - heart_rate_bpm: if vitals.heartrate_bpm > 0.0 { - Some(vitals.heartrate_bpm) - } else { - None - }, - breathing_confidence: if vitals.presence { 0.7 } else { 0.0 }, - heartbeat_confidence: if vitals.presence { 0.7 } else { 0.0 }, - signal_quality: vitals.presence_score as f64, - }), + vital_signs: published_vitals, enhanced_motion: None, enhanced_breathing: None, posture: None, @@ -6749,7 +9184,25 @@ async fn udp_receiver_task( let mut s = state.write().await; s.source = "esp32".to_string(); - s.last_esp32_frame = Some(std::time::Instant::now()); + let observed_at = std::time::Instant::now(); + s.last_esp32_frame = Some(observed_at); + let grid_key = CsiGridKey::from_frame(&frame); + s.node_states + .entry(frame.node_id) + .or_insert_with(NodeState::new) + .observe_raw_grid(grid_key, observed_at); + + // The field model owns a separate, frozen grid path. Feed + // it before the global inference grid gate so a stable + // 64-bin stream can remain comparable even when the + // independent live feature path prefers a wider grid. + s.maybe_feed_calibration_frame( + frame.node_id, + grid_key, + frame.sequence, + &frame.amplitudes, + observed_at, + ); // ── ADR-110 / issue #1005: per-node subcarrier-grid gate ── // ESP32-C6 nodes interleave HE-SU 256-bin frames (~84%) @@ -6845,7 +9298,7 @@ async fn udp_receiver_task( ns.frame_history.pop_front(); } - let sample_rate_hz = 1000.0 / 500.0_f64; + let sample_rate_hz = ns.effective_sample_rate_hz(); let ( features, mut classification, @@ -6858,13 +9311,19 @@ async fn udp_receiver_task( // Adaptive override using cloned model (safe, no raw pointers). if let Some(ref model) = adaptive_model_clone { let amps = ns.frame_history.back().map(|v| v.as_slice()).unwrap_or(&[]); + let dominant_frequency = + adaptive_classifier::compatible_dominant_frequency( + model.version, + features.dominant_freq_hz, + amps, + ); let feat_arr = adaptive_classifier::features_from_runtime( &serde_json::json!({ "variance": features.variance, "motion_band_power": features.motion_band_power, "breathing_band_power": features.breathing_band_power, "spectral_power": features.spectral_power, - "dominant_freq_hz": features.dominant_freq_hz, + "dominant_freq_hz": dominant_frequency, "change_points": features.change_points, "mean_rssi": features.mean_rssi, }), @@ -6882,6 +9341,18 @@ async fn udp_receiver_task( ns.rssi_history.pop_front(); } + if ns.csi_fps_samples >= 5 + && ns.vital_detector.reconfigure_sample_rate(sample_rate_hz) + { + // Never smooth estimates computed against two clocks. + ns.smoothed_hr = 0.0; + ns.smoothed_br = 0.0; + ns.smoothed_hr_conf = 0.0; + ns.smoothed_br_conf = 0.0; + ns.hr_buffer.clear(); + ns.br_buffer.clear(); + } + let raw_vitals = ns .vital_detector .process_frame(&frame.amplitudes, &frame.phases); @@ -6931,9 +9402,17 @@ async fn udp_receiver_task( 0.05 }; - // Aggregate person count: gate on presence first (matching WiFi path). + let observed_at_unix_ms = + chrono::Utc::now().timestamp_millis().max(0) as u64; + let bootstrap_empty = + s.bootstrap_empty_prior_applies(observed_at_unix_ms); + + // A restored prior can suppress a background-only raw + // classification. It cannot authorize positive presence. let now = std::time::Instant::now(); - let total_persons = if classification.presence { + let total_persons = if bootstrap_empty { + 0 + } else if classification.presence { let dedup = s.dedup_factor; let (fused, fallback_count) = multistatic_bridge::fuse_or_fallback( &s.multistatic_fuser, @@ -6951,7 +9430,10 @@ async fn udp_receiver_task( // #803: don't let the saturating activity score // discard count-aware per-node estimates. let count = - aggregate_person_count(s.person_count(), &s.node_states); + aggregate_person_count( + s.person_count_at(observed_at_unix_ms), + &s.node_states, + ); s.prev_person_count = count; count.max(1) } @@ -6980,17 +9462,6 @@ async fn udp_receiver_task( sref.engine_bridge.observe_cycle(&sref.node_states, now_ms); } - // Feed field model calibration if active (use per-node history for ESP32). - if let Some(frame_history) = s - .node_states - .get(&node_id) - .map(|ns| ns.frame_history.clone()) - { - if let Some(ref mut fm) = s.field_model { - field_bridge::maybe_feed_calibration(fm, &frame_history); - } - } - // Build nodes array with all active nodes. ADR-141 output // gating (review finding 1c): when the governed engine // emitted this cycle at class Restricted (base mode, or a @@ -7000,6 +9471,8 @@ async fn udp_receiver_task( // privacy gate applies at Restricted (drop amplitude/phase // proxies). let suppress_raw = s.engine_bridge.suppress_raw_outputs(); + let resolved_positions = + node_positions_by_active_id(&s.node_positions_config, &s.node_states, now); let active_nodes: Vec = s .node_states .iter() @@ -7010,7 +9483,10 @@ async fn udp_receiver_task( .map(|(&id, n)| NodeInfo { node_id: id, rssi_dbm: n.rssi_history.back().copied().unwrap_or(0.0), - position: [2.0, 0.0, 1.5], + position: resolved_positions + .get(&id) + .copied() + .unwrap_or([2.0, 0.0, 1.5]), amplitude: if suppress_raw { vec![] } else { @@ -7037,6 +9513,22 @@ async fn udp_receiver_task( active_nodes.iter().filter_map(|ni| ni.node_inference.as_ref()), NODE_STALE_AFTER_MS, ); + let room_classification = if bootstrap_empty { + ClassificationInfo { + motion_level: "absent".to_string(), + presence: false, + confidence: 0.0, + } + } else { + debounce_room_classification(&mut s, &room_inference) + }; + let explicit_calibration_fresh = + s.explicit_calibration_fresh_at(observed_at_unix_ms); + let published_vitals = vitals_for_publication( + &vitals, + explicit_calibration_fresh, + total_persons, + ); let mut update = SensingUpdate { msg_type: "sensing_update".to_string(), @@ -7050,7 +9542,7 @@ async fn udp_receiver_task( // `classification` (this node's own smoothed reading) // still drives `motion_score`/`total_persons` above, // which are legitimately this-packet-local. - classification: classification_from_room(&room_inference), + classification: room_classification, signal_field: generate_signal_field( fused_features.mean_rssi, motion_score, @@ -7058,7 +9550,7 @@ async fn udp_receiver_task( fused_features.variance.min(1.0), &sub_variances, ), - vital_signs: Some(vitals), + vital_signs: published_vitals, enhanced_motion: None, enhanced_breathing: None, posture: None, @@ -7313,7 +9805,7 @@ async fn simulated_data_task(state: SharedState, tick_ms: u64) { features.variance.min(1.0), &sub_variances, ), - vital_signs: Some(vitals), + vital_signs: None, enhanced_motion: None, enhanced_breathing: None, posture: None, @@ -7774,6 +10266,14 @@ fn coalesce_ui_path(initial: std::path::PathBuf) -> std::path::PathBuf { initial } +fn validate_startup_calibration_request(calibrate: bool) -> Result<(), &'static str> { + if calibrate { + Err("--calibrate cannot safely select a source and CSI grid at process start. Start the server normally, wait at least 10 seconds for live grid evidence, then POST /api/v1/calibration/start with one explicit source_node_id.") + } else { + Ok(()) + } +} + #[tokio::main] async fn main() { // Initialize tracing; with the `otel` feature and @@ -7785,6 +10285,11 @@ async fn main() { let mut args = Args::parse(); args.ui_path = coalesce_ui_path(args.ui_path); + if let Err(error) = validate_startup_calibration_request(args.calibrate) { + eprintln!("Error: {error}"); + std::process::exit(2); + } + // Handle --benchmark mode: run vital sign benchmark and exit if args.benchmark { eprintln!("Running vital sign detection benchmark (1000 frames)..."); @@ -8478,12 +10983,49 @@ async fn main() { ); // ADR-044 §5.3: load persisted runtime config from the data directory. - let data_dir = std::path::PathBuf::from("data"); + let data_dir = args.data_dir.clone(); + if let Err(error) = std::fs::create_dir_all(&data_dir) { + warn!(path = %data_dir.display(), %error, "Could not create server data directory"); + } let runtime_config = load_runtime_config(&data_dir); // ADR-271: resolve (or generate + persist) the browser-session signing key // before any request can arrive. Zero-config for a single appliance; the // env var still wins for a multi-instance deployment that must share one. wifi_densepose_sensing_server::browser_session::init_secret(&data_dir); + let (bootstrap_field_model, bootstrap_metadata, bootstrap_baseline_active) = + if !args.calibrate { + args.installation_id.as_deref().map_or( + (None, None, false), + |installation_id| { + let path = bootstrap_baseline::path_in(&data_dir); + match bootstrap_baseline::load( + &path, + installation_id, + chrono::Utc::now().timestamp_millis() as u64, + ) { + Ok((model, metadata)) => { + info!( + source_model_id = %metadata.source_model_id, + source_node_ids = ?metadata.source_node_ids, + "Restored privacy reduced empty room bootstrap prior" + ); + (Some(model), Some(metadata), true) + } + Err(bootstrap_baseline::BootstrapBaselineError::Io(error)) + if error.kind() == std::io::ErrorKind::NotFound => + { + (None, None, false) + } + Err(error) => { + warn!(%error, "Ignored invalid empty room bootstrap prior"); + (None, None, false) + } + } + }, + ) + } else { + (None, None, false) + }; info!( "Loaded runtime config: dedup_factor={:.2}", runtime_config.dedup_factor @@ -8581,7 +11123,7 @@ async fn main() { // threaded into `engine_bridge` so both fusion paths honor the same // WDP_TDM_SLOTS/WDP_GUARD_INTERVAL_US-derived guard (#1049/#1057). let mut engine_bridge_multistatic_cfg: Option = None; - + let mut node_positions_config: Vec<[f32; 3]> = Vec::new(); let state: SharedState = Arc::new(RwLock::new(AppStateInner { latest_update: None, rssi_history: VecDeque::new(), @@ -8647,6 +11189,9 @@ async fn main() { ); }), node_states: HashMap::new(), + room_debounced_level: "absent".to_string(), + room_debounce_candidate: "absent".to_string(), + room_debounce_since: None, // Accuracy sprint pose_tracker: PoseTracker::new(), last_tracker_instant: None, @@ -8672,6 +11217,7 @@ async fn main() { "Configured {} node positions for multistatic fusion", positions.len() ); + node_positions_config = positions.clone(); fuser.set_node_positions(positions); } } @@ -8681,6 +11227,7 @@ async fn main() { }); fuser }, + node_positions_config, engine_bridge: engine_bridge::EngineBridge::new( wifi_densepose_bfld::PrivacyMode::PrivateHome, 1, @@ -8688,12 +11235,29 @@ async fn main() { "Default Room", engine_bridge_multistatic_cfg, ), - field_model: if args.calibrate { - info!("Field model calibration enabled — room should be empty during startup"); - FieldModel::new(field_bridge::single_link_config()).ok() - } else { - None - }, + field_model: bootstrap_field_model, + installation_id: args.installation_id.clone(), + bootstrap_baseline: bootstrap_metadata.clone(), + bootstrap_baseline_active, + calibration_model_id: None, + calibration_source_node_ids: bootstrap_metadata + .as_ref() + .map(|metadata| metadata.source_node_ids.iter().copied().collect()) + .unwrap_or_default(), + calibration_grid_binding: bootstrap_metadata.as_ref().and_then(|metadata| { + let [source_node_id] = metadata.source_node_ids.as_slice() else { + return None; + }; + Some(CalibrationGridBinding { + source_node_id: *source_node_id, + grid: CsiGridKey::from_bootstrap_grid(metadata.source_grid), + evidence: None, + }) + }), + calibration_last_sequences: HashMap::new(), + calibration_reordered_packets: HashMap::new(), + calibration_max_reorder_depth: HashMap::new(), + calibration_sequence_fault_node_ids: std::collections::BTreeSet::new(), // ADR-044 §5.2: rolling-P95 over ~30 s at 20 Hz; warm-up after 60 samples. p95_variance: RollingP95::new(600, 60), p95_motion_band_power: RollingP95::new(600, 60), @@ -8989,7 +11553,17 @@ async fn main() { // Field model calibration (eigenvalue-based person counting) .route("/api/v1/calibration/start", post(calibration_start)) .route("/api/v1/calibration/stop", post(calibration_stop)) + .route( + "/api/v1/calibration/bootstrap/promote", + post(calibration_promote_bootstrap), + ) + .route( + "/api/v1/calibration/bootstrap/refine", + post(calibration_refine_bootstrap), + ) + .route("/api/v1/calibration/cancel", post(calibration_cancel)) .route("/api/v1/calibration/status", get(calibration_status)) + .route("/api/v1/calibration/reset", post(calibration_reset)) // ADR-044 §5.3: runtime-configurable dedup factor .route( "/api/v1/config/dedup-factor", @@ -9320,6 +11894,20 @@ mod sync_snapshot_helper_tests { assert_eq!(snap.csi_fps_samples, 42); } + #[test] + fn exported_node_rate_is_mature_and_physically_bounded() { + let mut ns = NodeState::new(); + assert_eq!(ns.measured_sample_rate_hz(), None); + + ns.csi_fps_ema = 74.0; + ns.csi_fps_samples = 5; + assert_eq!(ns.measured_sample_rate_hz(), Some(50.0)); + + ns.latest_sync = Some(populated_sync(9)); + ns.latest_sync_at = Some(std::time::Instant::now()); + assert_eq!(ns.sync_snapshot().unwrap().csi_fps_ema, 50.0); + } + #[test] fn observe_csi_frame_arrival_ignores_subms_bursts() { // Issue #1180 regression: a ~40 fps node whose frames are delivered diff --git a/v2/crates/wifi-densepose-sensing-server/src/types.rs b/v2/crates/wifi-densepose-sensing-server/src/types.rs index da2630497..e86ceaba9 100644 --- a/v2/crates/wifi-densepose-sensing-server/src/types.rs +++ b/v2/crates/wifi-densepose-sensing-server/src/types.rs @@ -544,6 +544,10 @@ impl AppStateInner { pub fn person_count(&self) -> usize { use crate::csi::score_to_person_count; use crate::field_bridge; + let observed_at_us = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|duration| duration.as_micros() as u64) + .unwrap_or(0); match self.field_model.as_ref() { Some(fm) => { let history = if !self.frame_history.is_empty() { @@ -559,6 +563,7 @@ impl AppStateInner { field_bridge::occupancy_or_fallback( fm, history, + observed_at_us, self.smoothed_person_score, self.prev_person_count, ) diff --git a/v2/crates/wifi-densepose-sensing-server/src/vital_signs.rs b/v2/crates/wifi-densepose-sensing-server/src/vital_signs.rs index 10558c6cd..d6a310685 100644 --- a/v2/crates/wifi-densepose-sensing-server/src/vital_signs.rs +++ b/v2/crates/wifi-densepose-sensing-server/src/vital_signs.rs @@ -346,6 +346,31 @@ impl VitalSignDetector { self.frame_count = 0; } + /// Retune temporal windows to an observed CSI clock. Changes smaller than + /// twenty percent are ignored to avoid resetting on normal arrival jitter. + /// A real clock change clears history so samples from different clocks are + /// never interpreted in one spectrum. + pub fn reconfigure_sample_rate(&mut self, sample_rate: f64) -> bool { + if !sample_rate.is_finite() || sample_rate < 1.0 { + return false; + } + let relative_change = (sample_rate - self.sample_rate).abs() / self.sample_rate.max(1.0); + if relative_change < 0.20 { + return false; + } + self.sample_rate = sample_rate; + self.breathing_capacity = + ((sample_rate * self.breathing_window_secs) as usize).max(1); + self.heartbeat_capacity = + ((sample_rate * self.heartbeat_window_secs) as usize).max(1); + self.reset(); + true + } + + pub fn sample_rate_hz(&self) -> f64 { + self.sample_rate + } + /// Current buffer fill levels for diagnostics. /// Returns (breathing_len, breathing_capacity, heartbeat_len, heartbeat_capacity). pub fn buffer_status(&self) -> (usize, usize, usize, usize) { @@ -842,6 +867,22 @@ mod tests { assert_eq!(hb_len, 0); } + #[test] + fn measured_clock_reconfigures_once_and_clears_mixed_history() { + let mut detector = VitalSignDetector::new(20.0); + let amp = vec![10.0; 56]; + let phase = vec![0.0; 56]; + for _ in 0..32 { + detector.process_frame(&, &phase); + } + assert!(!detector.reconfigure_sample_rate(18.0)); + assert_eq!(detector.sample_rate_hz(), 20.0); + assert!(detector.reconfigure_sample_rate(12.0)); + assert_eq!(detector.sample_rate_hz(), 12.0); + assert_eq!(detector.buffer_status(), (0, 360, 0, 180)); + assert!(!detector.reconfigure_sample_rate(f64::NAN)); + } + #[test] fn test_vital_signs_default() { let vs = VitalSigns::default(); diff --git a/v2/crates/wifi-densepose-signal/benches/dsp_perf_bench.rs b/v2/crates/wifi-densepose-signal/benches/dsp_perf_bench.rs index 9d65980b1..b0f2a0838 100644 --- a/v2/crates/wifi-densepose-signal/benches/dsp_perf_bench.rs +++ b/v2/crates/wifi-densepose-signal/benches/dsp_perf_bench.rs @@ -287,6 +287,7 @@ mod eig { n_links, n_modes: 3, min_calibration_frames: 20, + min_calibration_duration_s: 0.0, baseline_expiry_s: 86_400.0, }; let mut model = FieldModel::new(config).unwrap(); diff --git a/v2/crates/wifi-densepose-signal/src/ruvsense/field_model.rs b/v2/crates/wifi-densepose-signal/src/ruvsense/field_model.rs index 210e6d0a0..41596d179 100644 --- a/v2/crates/wifi-densepose-signal/src/ruvsense/field_model.rs +++ b/v2/crates/wifi-densepose-signal/src/ruvsense/field_model.rs @@ -7,7 +7,8 @@ //! (the residual). //! //! # Algorithm -//! 1. Collect CSI during empty-room calibration (>=10 min at 20 Hz) +//! 1. Collect CSI during empty-room calibration (>=10 min wall-clock and at +//! least twenty complete runtime-sized background windows) //! 2. Compute per-link baseline mean (Welford online accumulator) //! 3. Decompose covariance via SVD to extract environmental modes //! 4. At runtime: observation - baseline, project out top-K modes, keep residual @@ -22,6 +23,44 @@ use ndarray::Array2; use ndarray_linalg::Eigh; #[cfg(feature = "eigenvalue")] use ndarray_linalg::UPLO; +use serde::{Deserialize, Serialize}; +use std::collections::VecDeque; + +// --------------------------------------------------------------------------- +// Calibration window constants +// --------------------------------------------------------------------------- + +/// Intended wall-clock duration of the empty-room calibration window (s). +/// +/// The Welford statistics exist to absorb slow environmental variation — +/// HVAC cycles, thermal drift — so the calibration gate is expressed in +/// wall-clock time. Every accepted node packet advances the same field model, +/// so N nodes reach any frame target N times faster; frames alone therefore +/// cannot gate calibration (#1756). +pub const CALIBRATION_DURATION_S: f64 = 600.0; + +/// A recent window whose mean remains within this residual energy of the +/// empty room manifold is treated as background before eigenvalue counting. +const EMPTY_ROOM_RESIDUAL_ENERGY_MAX: f64 = 1.0; + +/// Runtime occupancy is evaluated over fifty frames. Keep ten raw windows +/// from the end of calibration for covariance rank learning, plus bounded +/// privacy reduced window means spanning the full capture for residual +/// conformance scoring. +const RUNTIME_OCCUPANCY_WINDOW: usize = 50; +const CALIBRATION_REFERENCE_FRAMES: usize = RUNTIME_OCCUPANCY_WINDOW * 10; +/// Minimum independent runtime sized means required to learn an empirical +/// empty room residual distribution. Wall clock duration remains the primary +/// coverage gate, so slow hardware is not forced to imitate a nominal rate. +pub const MIN_BACKGROUND_REFERENCE_WINDOWS: usize = 20; +pub const MIN_CALIBRATION_FRAMES: usize = + RUNTIME_OCCUPANCY_WINDOW * MIN_BACKGROUND_REFERENCE_WINDOWS; +const CALIBRATION_BACKGROUND_WINDOWS_MAX: usize = 512; +const EMPTY_ROOM_RESIDUAL_MARGIN: f64 = 1.5; +const EMPTY_ROOM_HELD_OUT_MARGIN: f64 = 1.10; +const EMPTY_ROOM_REFINEMENT_MAX_LIFT: f64 = 1.25; +const EMPTY_ROOM_REFINEMENT_MIN_SAMPLES: usize = 10; +const EMPTY_ROOM_REFINEMENT_MAX_SAMPLES: usize = 64; // --------------------------------------------------------------------------- // Error types @@ -34,6 +73,12 @@ pub enum FieldModelError { #[error("Insufficient calibration frames: need {needed}, got {got}")] InsufficientCalibration { needed: usize, got: usize }, + /// Calibration window shorter than the intended wall-clock duration. + /// A frame count alone cannot gate calibration: N nodes streaming in + /// aggregate reach any frame target in 1/N of the intended window (#1756). + #[error("Calibration window too short: need {needed_s:.1}s of wall-clock time, got {got_s:.1}s")] + InsufficientCalibrationDuration { needed_s: f64, got_s: f64 }, + /// Dimensionality mismatch between observation and baseline. #[error("Dimension mismatch: baseline has {expected} subcarriers, observation has {got}")] DimensionMismatch { expected: usize, got: usize }, @@ -236,7 +281,8 @@ impl LinkBaselineStats { // --------------------------------------------------------------------------- /// Configuration for field model calibration and runtime. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +#[serde(deny_unknown_fields)] pub struct FieldModelConfig { /// Number of links in the mesh. pub n_links: usize, @@ -244,8 +290,17 @@ pub struct FieldModelConfig { pub n_subcarriers: usize, /// Number of environmental modes to retain (K). Max 5. pub n_modes: usize, - /// Minimum calibration frames before baseline is valid (10 min at 20 Hz = 12000). + /// Minimum calibration frames before baseline is valid. This is the + /// runtime window size times the minimum number of independent background + /// references. A frame count alone is not sufficient: N nodes streaming + /// in aggregate reach it in 1/N of the window, so + /// `min_calibration_duration_s` also gates finalization (#1756). pub min_calibration_frames: usize, + /// Minimum wall-clock duration of the calibration window in seconds. + /// Slow environmental variation (HVAC cycles, thermal drift) can only be + /// observed over time, regardless of how many frames a fast fleet + /// delivers (#1756). 0 disables the duration gate (tests only). + pub min_calibration_duration_s: f64, /// Baseline expiry in seconds (default 86400 = 24 hours). pub baseline_expiry_s: f64, } @@ -256,7 +311,8 @@ impl Default for FieldModelConfig { n_links: 6, n_subcarriers: 56, n_modes: 3, - min_calibration_frames: 12_000, + min_calibration_frames: MIN_CALIBRATION_FRAMES, + min_calibration_duration_s: CALIBRATION_DURATION_S, baseline_expiry_s: 86_400.0, } } @@ -267,7 +323,8 @@ impl Default for FieldModelConfig { /// Learned from SVD on the covariance of CSI amplitudes during /// empty-room calibration. The top-K modes capture environmental /// variation (temperature, humidity, time-of-day effects). -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +#[serde(deny_unknown_fields)] pub struct FieldNormalMode { /// Per-link baseline mean: `[n_links][n_subcarriers]`. pub baseline: Vec>, @@ -291,6 +348,58 @@ pub struct FieldNormalMode { /// per-window sample size. Defaults to 0.0 in the diagonal-fallback path. /// Issue #942. pub baseline_noise_var: f64, + /// Empty room significant eigenvalue count learned from runtime sized + /// windows. The percentile is stored instead of raw CSI. `None` identifies + /// snapshots created before runtime window calibration was introduced. + #[serde(default)] + pub baseline_runtime_eigenvalue_count: Option, + /// Number of frames used for the runtime window reference above. + #[serde(default)] + pub baseline_runtime_window_size: Option, + /// Hardware adaptive upper bound for the residual energy of an empty room + /// window mean. Only this aggregate boundary is persisted. + #[serde(default)] + pub empty_room_residual_energy_threshold: Option, + /// Sorted residual energies of privacy reduced runtime window means sampled + /// across the full calibration. Raw CSI is never persisted. These scalar + /// references support an empirical background match score without turning + /// an empty room model into positive person evidence. + #[serde(default)] + pub empty_room_residual_energy_reference: Vec, + /// Number of operator-confirmed held-out empty refinements. Version one + /// permits one bounded refinement so repeated calls cannot ratchet the + /// background boundary upward until a weak occupant is suppressed. + #[serde(default)] + pub empty_room_residual_refinement_count: u8, +} + +/// A bounded comparison between one runtime window and the learned empty room +/// residual distribution. `score` is an empirical conformance score, not a +/// probability that a person is absent. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct EmptyRoomMatch { + pub matches_empty: bool, + pub score: Option, + /// Robust residual distance from the quiet reference median. + pub normalized_residual_z: Option, + /// Reference coverage relative to the normal calibration target. + pub maturity: f64, + /// Whether the reference set is large and valid enough to suppress change. + pub reliable: bool, + pub residual_energy: f64, + pub residual_energy_threshold: f64, + pub window_size: usize, + pub reference_window_count: usize, +} + +/// Receipt for one bounded, operator-confirmed empty-room refinement. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct EmptyRoomRefinement { + pub threshold_before: f64, + pub threshold_after: f64, + pub held_out_p95: f64, + pub held_out_sample_count: usize, + pub reference_window_count: usize, } /// Body perturbation extracted from a CSI observation. @@ -325,6 +434,18 @@ pub enum CalibrationStatus { Expired, } +/// Privacy reduced restart image for a completed field model. +/// +/// This contains only aggregate baseline statistics and environmental modes. +/// It excludes raw CSI frames, device addresses, room names, credentials, and +/// calibration authority. A restored image is only a bootstrap prior. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +#[serde(deny_unknown_fields)] +pub struct FieldModelSnapshotV1 { + pub config: FieldModelConfig, + pub modes: FieldNormalMode, +} + /// The persistent field model for a single room. /// /// Maintains per-link Welford statistics during calibration, then @@ -345,6 +466,19 @@ pub struct FieldModel { covariance_sum: Option>, /// Number of frames accumulated into covariance_sum. covariance_count: u64, + /// Monotonic timestamp of the first accepted calibration frame of the + /// current session; `None` until collection starts (#1756). + calibration_started: Option, + /// Bounded, memory only tail used to learn inference sized empty room + /// covariance statistics. It is never included in a snapshot. + calibration_reference_tail: VecDeque>>, + /// Per link sum for the current privacy reduced runtime sized window. + calibration_window_sum: Vec>, + /// Number of accepted observations in `calibration_window_sum`. + calibration_window_count: usize, + /// Runtime sized per link means spanning the full calibration. This is + /// bounded, memory only, and reduced to scalar residuals at finalization. + calibration_window_means: VecDeque>>, } /// Diagonal variance fallback for when full covariance SVD is unavailable. @@ -400,6 +534,154 @@ fn diagonal_fallback( (mode_energies, environmental_modes, baseline_count) } +#[cfg(feature = "eigenvalue")] +fn significant_eigenvalue_count( + frames: &[Vec], + n_subcarriers: usize, + baseline_noise_var: f64, +) -> Option { + if frames.len() < 10 { + return None; + } + + let mut mean = vec![0.0_f64; n_subcarriers]; + let mut count = 0_usize; + for frame in frames { + if frame.len() >= n_subcarriers { + for index in 0..n_subcarriers { + mean[index] += frame[index]; + } + count += 1; + } + } + if count < 2 { + return None; + } + for value in &mut mean { + *value /= count as f64; + } + + let mut covariance = Array2::::zeros((n_subcarriers, n_subcarriers)); + for frame in frames { + if frame.len() >= n_subcarriers { + for row in 0..n_subcarriers { + let centered_row = frame[row] - mean[row]; + for column in row..n_subcarriers { + let value = centered_row * (frame[column] - mean[column]); + covariance[[row, column]] += value; + if row != column { + covariance[[column, row]] += value; + } + } + } + } + } + covariance *= 1.0 / (count as f64 - 1.0); + + let (eigenvalues, _) = covariance.eigh(UPLO::Upper).ok()?; + let mut positive: Vec = eigenvalues + .iter() + .copied() + .filter(|value| *value > 1e-10) + .collect(); + positive.sort_by(|left, right| left.partial_cmp(right).unwrap_or(std::cmp::Ordering::Equal)); + let local_noise_var = if positive.len() >= 4 { + let half = positive.len() / 2; + positive[..half].iter().sum::() / half as f64 + } else { + positive.first().copied()? + }; + let noise_var = if baseline_noise_var > 0.0 { + local_noise_var.max(baseline_noise_var) + } else { + local_noise_var + }; + let ratio = n_subcarriers as f64 / count as f64; + let threshold = noise_var * (1.0 + ratio.sqrt()).powi(2); + Some( + eigenvalues + .iter() + .filter(|&&value| value > threshold) + .count(), + ) +} + +fn residual_energy_for_link( + modes: &FieldNormalMode, + link_index: usize, + observation: &[f64], +) -> Option { + let baseline = modes.baseline.get(link_index)?; + if observation.len() != baseline.len() { + return None; + } + let mut residual: Vec = observation + .iter() + .zip(baseline.iter()) + .map(|(value, reference)| value - reference) + .collect(); + for mode in &modes.environmental_modes { + let projection: f64 = residual + .iter() + .zip(mode.iter()) + .map(|(value, basis)| value * basis) + .sum(); + for (value, basis) in residual.iter_mut().zip(mode.iter()) { + *value -= projection * basis; + } + } + Some( + residual + .iter() + .map(|value| value * value) + .sum::() + .sqrt(), + ) +} + +fn percentile_95(mut values: Vec) -> Option { + values.retain(|value| value.is_finite()); + values.sort_by(|left, right| left.partial_cmp(right).unwrap_or(std::cmp::Ordering::Equal)); + let index = values + .len() + .saturating_mul(95) + .div_ceil(100) + .saturating_sub(1); + values.get(index).copied() +} + +fn robust_residual_z(reference: &[f64], residual: f64) -> Option { + if !residual.is_finite() || reference.is_empty() { + return None; + } + let mut sorted: Vec = reference + .iter() + .copied() + .filter(|value| value.is_finite() && *value >= 0.0) + .collect(); + if sorted.len() != reference.len() { + return None; + } + sorted.sort_by(|left, right| left.partial_cmp(right).unwrap_or(std::cmp::Ordering::Equal)); + let median = sorted[sorted.len() / 2]; + let mut deviations: Vec = sorted.iter().map(|value| (value - median).abs()).collect(); + deviations.sort_by(|left, right| left.partial_cmp(right).unwrap_or(std::cmp::Ordering::Equal)); + let mad = deviations[deviations.len() / 2]; + let robust_scale = (1.4826 * mad).max((median.abs() * 0.01).max(f64::EPSILON)); + Some((residual - median) / robust_scale) +} + +#[cfg(feature = "eigenvalue")] +fn percentile_95_usize(mut values: Vec) -> Option { + values.sort_unstable(); + let index = values + .len() + .saturating_mul(95) + .div_ceil(100) + .saturating_sub(1); + values.get(index).copied() +} + impl FieldModel { /// Create a new field model for the given configuration. pub fn new(config: FieldModelConfig) -> Result { @@ -420,6 +702,7 @@ impl FieldModel { let link_stats = (0..config.n_links) .map(|_| LinkBaselineStats::new(config.n_subcarriers)) .collect(); + let calibration_window_sum = vec![vec![0.0_f64; config.n_subcarriers]; config.n_links]; Ok(Self { config, @@ -429,6 +712,11 @@ impl FieldModel { last_calibration_us: 0, covariance_sum: None, covariance_count: 0, + calibration_started: None, + calibration_reference_tail: VecDeque::with_capacity(CALIBRATION_REFERENCE_FRAMES), + calibration_window_sum, + calibration_window_count: 0, + calibration_window_means: VecDeque::with_capacity(CALIBRATION_BACKGROUND_WINDOWS_MAX), }) } @@ -442,6 +730,191 @@ impl FieldModel { self.modes.as_ref() } + /// Hardware adaptive empty room boundary learned at finalization. Returns + /// `None` for legacy snapshots that predate runtime window calibration. + pub fn empty_room_residual_energy_threshold(&self) -> Option { + self.modes + .as_ref() + .and_then(|modes| modes.empty_room_residual_energy_threshold) + } + + /// Refine a completed single-link empty-room boundary with scalar residuals + /// measured during a separate operator-confirmed empty holdout. + /// + /// This never ingests or persists raw CSI. The lift is capped at 25 percent + /// and may happen only once per snapshot, preventing repeated calls from + /// silently erasing weak occupied evidence. + pub fn refine_empty_room_residual_boundary( + &mut self, + held_out_residuals: &[f64], + ) -> Result { + if self.config.n_links != 1 || self.status != CalibrationStatus::Fresh { + return Err(FieldModelError::NotCalibrated); + } + if !(EMPTY_ROOM_REFINEMENT_MIN_SAMPLES..=EMPTY_ROOM_REFINEMENT_MAX_SAMPLES) + .contains(&held_out_residuals.len()) + || held_out_residuals + .iter() + .any(|value| !value.is_finite() || *value < 0.0) + { + return Err(FieldModelError::InvalidConfig( + "held-out empty residuals are missing, excessive, or non-finite".into(), + )); + } + let modes = self.modes.as_mut().ok_or(FieldModelError::NotCalibrated)?; + if modes.empty_room_residual_refinement_count != 0 { + return Err(FieldModelError::InvalidConfig( + "the empty-room boundary already has its one permitted held-out refinement" + .into(), + )); + } + if modes.empty_room_residual_energy_reference.len() + held_out_residuals.len() + > CALIBRATION_BACKGROUND_WINDOWS_MAX + { + return Err(FieldModelError::InvalidConfig( + "the empty-room residual reference limit would be exceeded".into(), + )); + } + let threshold_before = modes + .empty_room_residual_energy_threshold + .ok_or(FieldModelError::NotCalibrated)? + .max(EMPTY_ROOM_RESIDUAL_ENERGY_MAX); + let held_out_p95 = percentile_95(held_out_residuals.to_vec()) + .ok_or(FieldModelError::NotCalibrated)?; + let proposed = held_out_p95 * EMPTY_ROOM_HELD_OUT_MARGIN; + let threshold_after = threshold_before + .max(proposed.min(threshold_before * EMPTY_ROOM_REFINEMENT_MAX_LIFT)); + + modes + .empty_room_residual_energy_reference + .extend_from_slice(held_out_residuals); + modes + .empty_room_residual_energy_reference + .sort_by(|left, right| { + left.partial_cmp(right).unwrap_or(std::cmp::Ordering::Equal) + }); + modes.empty_room_residual_energy_threshold = Some(threshold_after); + modes.empty_room_residual_refinement_count = 1; + + Ok(EmptyRoomRefinement { + threshold_before, + threshold_after, + held_out_p95, + held_out_sample_count: held_out_residuals.len(), + reference_window_count: modes.empty_room_residual_energy_reference.len(), + }) + } + + /// Export aggregate model state suitable for local restart persistence. + /// Raw calibration observations and Welford accumulators are not exported. + pub fn export_snapshot(&self) -> Result { + let modes = self.modes.clone().ok_or(FieldModelError::NotCalibrated)?; + Ok(FieldModelSnapshotV1 { + config: self.config.clone(), + modes, + }) + } + + /// Restore a bounded, validated aggregate snapshot. + pub fn from_snapshot( + snapshot: FieldModelSnapshotV1, + current_us: u64, + ) -> Result { + let config = &snapshot.config; + if config.n_links > 16 || config.n_subcarriers > 2_048 { + return Err(FieldModelError::InvalidConfig( + "snapshot dimensions exceed bounded limits".into(), + )); + } + if !config.min_calibration_duration_s.is_finite() + || config.min_calibration_duration_s < 0.0 + || !config.baseline_expiry_s.is_finite() + || config.baseline_expiry_s <= 0.0 + || config.baseline_expiry_s > 604_800.0 + { + return Err(FieldModelError::InvalidConfig( + "snapshot timing values are invalid".into(), + )); + } + + let modes = &snapshot.modes; + let baseline_shape_valid = modes.baseline.len() == config.n_links + && modes + .baseline + .iter() + .all(|link| link.len() == config.n_subcarriers); + let mode_shape_valid = modes.environmental_modes.len() == modes.mode_energies.len() + && modes.environmental_modes.len() <= config.n_modes + && modes + .environmental_modes + .iter() + .all(|mode| mode.len() == config.n_subcarriers); + let values_valid = modes + .baseline + .iter() + .flatten() + .chain(modes.environmental_modes.iter().flatten()) + .all(|value| value.is_finite()) + && modes + .mode_energies + .iter() + .all(|value| value.is_finite() && *value >= 0.0) + && modes.variance_explained.is_finite() + && (0.0..=10.0).contains(&modes.variance_explained) + && modes.baseline_noise_var.is_finite() + && modes.baseline_noise_var >= 0.0 + && modes.baseline_eigenvalue_count <= config.n_subcarriers + && modes + .baseline_runtime_eigenvalue_count + .is_none_or(|count| count <= config.n_subcarriers) + && modes + .baseline_runtime_window_size + .is_none_or(|size| (10..=CALIBRATION_REFERENCE_FRAMES).contains(&size)) + && modes + .empty_room_residual_energy_threshold + .is_none_or(|threshold| threshold.is_finite() && threshold >= 0.0) + && modes.empty_room_residual_energy_reference.len() + <= CALIBRATION_BACKGROUND_WINDOWS_MAX + && modes.empty_room_residual_refinement_count <= 1 + && modes + .empty_room_residual_energy_reference + .iter() + .all(|value| value.is_finite() && *value >= 0.0) + && modes + .empty_room_residual_energy_reference + .windows(2) + .all(|window| window[0] <= window[1]); + if !baseline_shape_valid || !mode_shape_valid || !values_valid { + return Err(FieldModelError::InvalidConfig( + "snapshot modes are malformed or non finite".into(), + )); + } + if modes.calibrated_at_us == 0 + || modes.calibrated_at_us > current_us.saturating_add(300_000_000) + { + return Err(FieldModelError::InvalidConfig( + "snapshot calibration time is invalid".into(), + )); + } + let elapsed_s = current_us.saturating_sub(modes.calibrated_at_us) as f64 / 1_000_000.0; + if elapsed_s > config.baseline_expiry_s { + return Err(FieldModelError::BaselineExpired { + elapsed_s, + max_s: config.baseline_expiry_s, + }); + } + + let mut model = Self::new(snapshot.config)?; + model.status = if elapsed_s > model.config.baseline_expiry_s * 0.5 { + CalibrationStatus::Stale + } else { + CalibrationStatus::Fresh + }; + model.last_calibration_us = modes.calibrated_at_us; + model.modes = Some(snapshot.modes); + Ok(model) + } + /// Number of calibration frames collected so far. pub fn calibration_frame_count(&self) -> u64 { self.link_stats @@ -454,6 +927,32 @@ impl FieldModel { self.config.min_calibration_frames } + /// Minimum wall-clock duration (s) of the calibration window required + /// before `finalize_calibration` will succeed (#1756). + pub fn min_calibration_duration_s(&self) -> f64 { + self.config.min_calibration_duration_s + } + + /// Wall-clock seconds elapsed since the first accepted calibration frame + /// of the current session, or 0.0 if collection has not started (#1756). + pub fn calibration_elapsed_s(&self) -> f64 { + self.calibration_started + .map(|t| t.elapsed().as_secs_f64()) + .unwrap_or(0.0) + } + + /// Effective aggregate frames per second since collection started, or 0.0 + /// if collection has not started (#1756). With N nodes streaming, this is + /// roughly N times the single-node rate. + pub fn calibration_frames_per_second(&self) -> f64 { + let elapsed = self.calibration_elapsed_s(); + if elapsed > 0.0 { + self.calibration_frame_count() as f64 / elapsed + } else { + 0.0 + } + } + /// Feed a calibration frame (one CSI observation per link during empty room). /// /// `observations` is `[n_links][n_subcarriers]` amplitude data. @@ -467,6 +966,11 @@ impl FieldModel { for (link_stat, obs) in self.link_stats.iter_mut().zip(observations.iter()) { link_stat.update(obs)?; } + // Start the wall-clock calibration window on the first accepted frame + // (#1756): finalize gates on elapsed duration as well as frame count. + if self.calibration_started.is_none() { + self.calibration_started = Some(std::time::Instant::now()); + } if self.status == CalibrationStatus::Uncalibrated { self.status = CalibrationStatus::Collecting; } @@ -496,12 +1000,46 @@ impl FieldModel { // Count once per frame (not per link) for correct MP ratio self.covariance_count += 1; + if self.calibration_reference_tail.len() == CALIBRATION_REFERENCE_FRAMES { + self.calibration_reference_tail.pop_front(); + } + self.calibration_reference_tail + .push_back(observations.to_vec()); + + for (link_sum, observation) in self + .calibration_window_sum + .iter_mut() + .zip(observations.iter()) + { + for (sum, value) in link_sum.iter_mut().zip(observation.iter()) { + *sum += value; + } + } + self.calibration_window_count += 1; + if self.calibration_window_count == RUNTIME_OCCUPANCY_WINDOW { + let divisor = self.calibration_window_count as f64; + let means: Vec> = self + .calibration_window_sum + .iter() + .map(|link_sum| link_sum.iter().map(|value| value / divisor).collect()) + .collect(); + if self.calibration_window_means.len() == CALIBRATION_BACKGROUND_WINDOWS_MAX { + self.calibration_window_means.pop_front(); + } + self.calibration_window_means.push_back(means); + for link_sum in &mut self.calibration_window_sum { + link_sum.fill(0.0); + } + self.calibration_window_count = 0; + } + Ok(()) } /// Finalize calibration: compute SVD to extract environmental modes. /// - /// Requires at least `min_calibration_frames` observations. + /// Requires at least `min_calibration_frames` observations collected over + /// at least `min_calibration_duration_s` of wall-clock time (#1756). /// `timestamp_us` is the current timestamp in microseconds. /// `geometry_hash` identifies the mesh geometry at calibration time. pub fn finalize_calibration( @@ -516,6 +1054,19 @@ impl FieldModel { got: count as usize, }); } + // #1756: the frame gate encodes "duration at the single-node rate", + // but every accepted node packet advances this model, so a fleet of N + // nodes satisfies it in ~1/N of the intended window. Gate on the + // wall-clock duration as well so the baseline covers the slow + // environmental variation the Welford statistics are meant to absorb. + let elapsed_s = self.calibration_elapsed_s(); + let need_s = self.config.min_calibration_duration_s; + if need_s > 0.0 && elapsed_s < need_s { + return Err(FieldModelError::InsufficientCalibrationDuration { + needed_s: need_s, + got_s: elapsed_s, + }); + } let n_sc = self.config.n_subcarriers; let n_modes = self.config.n_modes.min(n_sc); @@ -616,8 +1167,7 @@ impl FieldModel { } Err(_) => { // Fallback to diagonal approximation on SVD failure - let (e, m, b) = - diagonal_fallback(&self.link_stats, n_sc, n_modes); + let (e, m, b) = diagonal_fallback(&self.link_stats, n_sc, n_modes); (e, m, b, 0.0_f64) } } @@ -669,7 +1219,7 @@ impl FieldModel { 0.0 }; - let field_mode = FieldNormalMode { + let mut field_mode = FieldNormalMode { baseline, environmental_modes, mode_energies, @@ -678,15 +1228,157 @@ impl FieldModel { geometry_hash, baseline_eigenvalue_count: baseline_eig_count, baseline_noise_var, + baseline_runtime_eigenvalue_count: None, + baseline_runtime_window_size: None, + empty_room_residual_energy_threshold: None, + empty_room_residual_energy_reference: Vec::new(), + empty_room_residual_refinement_count: 0, }; + // The full calibration covariance and a fifty frame runtime covariance + // have different Marcenko Pastur aspect ratios. This first version is + // deliberately single link: combining link specific residual scales + // requires a separate multistatic calibration model. + if self.config.n_links == 1 { + let reference_frames: Vec> = self + .calibration_reference_tail + .iter() + .filter_map(|observation| observation.first().cloned()) + .collect(); + let complete_windows: Vec<&[Vec]> = reference_frames + .chunks(RUNTIME_OCCUPANCY_WINDOW) + .filter(|window| window.len() == RUNTIME_OCCUPANCY_WINDOW) + .collect(); + + #[cfg(feature = "eigenvalue")] + { + let counts: Vec = complete_windows + .iter() + .filter_map(|window| { + significant_eigenvalue_count(window, n_sc, baseline_noise_var) + }) + .collect(); + field_mode.baseline_runtime_eigenvalue_count = percentile_95_usize(counts); + } + + let mut residual_energies: Vec = self + .calibration_window_means + .iter() + .filter_map(|means| means.first()) + .filter_map(|mean| residual_energy_for_link(&field_mode, 0, mean)) + .filter(|value| value.is_finite() && *value >= 0.0) + .collect(); + if residual_energies.is_empty() { + residual_energies = complete_windows + .iter() + .filter_map(|window| { + let mut mean = vec![0.0_f64; n_sc]; + for frame in *window { + for (sum, value) in mean.iter_mut().zip(frame.iter()) { + *sum += value; + } + } + for value in &mut mean { + *value /= window.len() as f64; + } + residual_energy_for_link(&field_mode, 0, &mean) + }) + .collect(); + } + residual_energies.sort_by(|left, right| { + left.partial_cmp(right).unwrap_or(std::cmp::Ordering::Equal) + }); + field_mode.empty_room_residual_energy_threshold = + percentile_95(residual_energies.clone()).map(|value| { + (value * EMPTY_ROOM_RESIDUAL_MARGIN).max(EMPTY_ROOM_RESIDUAL_ENERGY_MAX) + }); + field_mode.empty_room_residual_energy_reference = residual_energies; + if field_mode.baseline_runtime_eigenvalue_count.is_some() + || field_mode.empty_room_residual_energy_threshold.is_some() + { + field_mode.baseline_runtime_window_size = Some(RUNTIME_OCCUPANCY_WINDOW); + } + } + self.modes = Some(field_mode); self.status = CalibrationStatus::Fresh; self.last_calibration_us = timestamp_us; + self.calibration_reference_tail.clear(); + self.calibration_window_means.clear(); + for link_sum in &mut self.calibration_window_sum { + link_sum.fill(0.0); + } + self.calibration_window_count = 0; Ok(self.modes.as_ref().unwrap()) } + /// Compare a runtime sized single link window with the learned empty room + /// distribution. The score is empirical conformance while the learned + /// boundary holds. Crossing the boundary returns zero. Legacy snapshots + /// without residual references retain binary suppression but no score. + pub fn empty_room_match(&self, recent_frames: &[Vec]) -> Option { + let modes = self.modes.as_ref()?; + if self.config.n_links != 1 { + return None; + } + let window_size = modes.baseline_runtime_window_size?; + if recent_frames.len() < window_size { + return None; + } + let mut mean = vec![0.0_f64; self.config.n_subcarriers]; + for frame in recent_frames.iter().take(window_size) { + if frame.len() < self.config.n_subcarriers { + return None; + } + for (sum, value) in mean.iter_mut().zip(frame.iter()) { + *sum += value; + } + } + for value in &mut mean { + *value /= window_size as f64; + } + let residual_energy = residual_energy_for_link(modes, 0, &mean)?; + let residual_energy_threshold = modes + .empty_room_residual_energy_threshold? + .max(EMPTY_ROOM_RESIDUAL_ENERGY_MAX); + let reference_window_count = modes.empty_room_residual_energy_reference.len(); + let maturity = (reference_window_count as f64 + / MIN_BACKGROUND_REFERENCE_WINDOWS as f64) + .clamp(0.0, 1.0); + let reliable = reference_window_count >= 10 + && modes + .empty_room_residual_energy_reference + .iter() + .all(|value| value.is_finite() && *value >= 0.0); + let matches_empty = reliable && residual_energy <= residual_energy_threshold; + let score = if !reliable { + None + } else if !matches_empty { + Some(0.0) + } else { + let at_or_below = modes + .empty_room_residual_energy_reference + .partition_point(|value| *value <= residual_energy); + let empirical_quantile = at_or_below as f64 / reference_window_count as f64; + Some((1.0 - empirical_quantile * 0.5).clamp(0.5, 1.0)) + }; + Some(EmptyRoomMatch { + matches_empty, + score, + normalized_residual_z: robust_residual_z( + &modes.empty_room_residual_energy_reference, + residual_energy, + ), + maturity, + reliable, + residual_energy, + residual_energy_threshold, + window_size, + reference_window_count, + }) + } + /// Extract body perturbation from a runtime observation. /// /// Subtracts baseline, projects out environmental modes, returns residual. @@ -797,6 +1489,22 @@ impl FieldModel { *m /= count as f64; } + // Rank alone is not person evidence. A short continuation window can + // expose more significant eigenvalues while its mean remains on the + // learned empty room manifold. Use the hardware adaptive residual + // boundary when available. + let empty_room_residual_energy_max = modes + .empty_room_residual_energy_threshold + .unwrap_or(EMPTY_ROOM_RESIDUAL_ENERGY_MAX) + .max(EMPTY_ROOM_RESIDUAL_ENERGY_MAX); + let mean_residual_energy = self + .extract_perturbation(&[mean.clone()]) + .ok() + .map(|perturbation| perturbation.total_energy); + if mean_residual_energy.is_some_and(|energy| energy <= empty_room_residual_energy_max) { + return Ok(0); + } + let mut cov = Array2::::zeros((n, n)); for frame in recent_frames { if frame.len() >= n { @@ -857,7 +1565,23 @@ impl FieldModel { let mp_threshold = noise_var * (1.0 + ratio.sqrt()).powi(2); let significant = eigenvalues.iter().filter(|&&ev| ev > mp_threshold).count(); - let occupancy = significant.saturating_sub(modes.baseline_eigenvalue_count); + let reference_count = if modes.baseline_runtime_window_size == Some(count) { + modes + .baseline_runtime_eigenvalue_count + .unwrap_or(modes.baseline_eigenvalue_count) + } else { + modes.baseline_eigenvalue_count + }; + let rank_occupancy = significant.saturating_sub(reference_count); + // A stable person can shift the window mean without increasing its + // covariance rank. Preserve at least one occupant after the learned + // empty room residual boundary is exceeded. + let occupancy = + if mean_residual_energy.is_some_and(|energy| energy > empty_room_residual_energy_max) { + rank_occupancy.max(1) + } else { + rank_occupancy + }; Ok(occupancy.min(10)) // Cap at 10 persons } @@ -895,6 +1619,13 @@ impl FieldModel { self.status = CalibrationStatus::Uncalibrated; self.covariance_sum = None; self.covariance_count = 0; + self.calibration_started = None; + self.calibration_reference_tail.clear(); + self.calibration_window_count = 0; + for link_sum in &mut self.calibration_window_sum { + link_sum.fill(0.0); + } + self.calibration_window_means.clear(); } } @@ -912,6 +1643,8 @@ mod tests { n_subcarriers: n_sc, n_modes: 3, min_calibration_frames: min_frames, + // Tests feed frames instantly; disable the wall-clock gate (#1756). + min_calibration_duration_s: 0.0, baseline_expiry_s: 86_400.0, } } @@ -1114,6 +1847,7 @@ mod tests { n_subcarriers: 8, n_modes: 2, min_calibration_frames: 5, + min_calibration_duration_s: 0.0, baseline_expiry_s: 86_400.0, }; let mut model = FieldModel::new(config).unwrap(); @@ -1307,6 +2041,7 @@ mod tests { n_subcarriers: 8, n_modes: 3, min_calibration_frames: 20, + min_calibration_duration_s: 0.0, baseline_expiry_s: 86_400.0, }; let mut model = FieldModel::new(config).unwrap(); @@ -1369,6 +2104,7 @@ mod tests { n_subcarriers: 8, n_modes: 3, min_calibration_frames: 20, + min_calibration_duration_s: 0.0, baseline_expiry_s: 86_400.0, }; let mut model = FieldModel::new(config).unwrap(); @@ -1410,6 +2146,217 @@ mod tests { assert_eq!(occupancy, 0, "Noise-only frames should yield 0 occupancy"); } + #[cfg(feature = "eigenvalue")] + #[test] + fn runtime_sized_empty_reference_scores_held_out_background() { + let config = FieldModelConfig { + n_links: 1, + n_subcarriers: 56, + n_modes: 3, + min_calibration_frames: 500, + min_calibration_duration_s: 0.0, + baseline_expiry_s: 86_400.0, + }; + let mut model = FieldModel::new(config).unwrap(); + let mut calibration = Vec::new(); + for frame_index in 0..600 { + let time = frame_index as f64 * 0.071; + let frame: Vec = (0..56) + .map(|subcarrier| { + let carrier = subcarrier as f64; + 18.0 + carrier * 0.08 + + (time + carrier * 0.13).sin() * 0.8 + + (time * 0.37 + carrier * 0.031).cos() * 0.35 + }) + .collect(); + model.feed_calibration(&[frame.clone()]).unwrap(); + calibration.push(frame); + } + model.finalize_calibration(1_000_000, 0).unwrap(); + + let modes = model.modes().unwrap(); + assert_eq!( + modes.baseline_runtime_window_size, + Some(RUNTIME_OCCUPANCY_WINDOW) + ); + assert!(modes.baseline_runtime_eigenvalue_count.is_some()); + assert!(modes.empty_room_residual_energy_threshold.is_some()); + assert_eq!(modes.empty_room_residual_energy_reference.len(), 12); + + let held_out_empty = calibration[550..600].to_vec(); + assert_eq!(model.estimate_occupancy(&held_out_empty).unwrap(), 0); + let background = model + .empty_room_match(&held_out_empty) + .expect("background match"); + assert!(background.matches_empty); + assert_eq!(background.reference_window_count, 12); + assert!(background.reliable); + assert_eq!(background.maturity, 0.6); + assert!(background.normalized_residual_z.is_some()); + assert!(background + .score + .is_some_and(|score| (0.5..=1.0).contains(&score))); + } + + #[test] + fn held_out_empty_refinement_is_bounded_and_single_use() { + let config = FieldModelConfig { + n_links: 1, + n_subcarriers: 8, + n_modes: 2, + min_calibration_frames: 100, + min_calibration_duration_s: 0.0, + baseline_expiry_s: 86_400.0, + }; + let mut model = FieldModel::new(config).unwrap(); + for frame_index in 0..1_000 { + let phase = frame_index as f64 * 0.071; + let frame = (0..8) + .map(|index| 20.0 + index as f64 * 0.1 + (phase + index as f64).sin()) + .collect(); + model.feed_calibration(&[frame]).unwrap(); + } + model.finalize_calibration(1_000_000, 0).unwrap(); + + let before = model.empty_room_residual_energy_threshold().unwrap(); + let residuals = vec![before * 1.4; 12]; + let receipt = model + .refine_empty_room_residual_boundary(&residuals) + .unwrap(); + assert_eq!(receipt.threshold_before, before); + assert!(receipt.threshold_after > before); + assert!(receipt.threshold_after <= before * EMPTY_ROOM_REFINEMENT_MAX_LIFT); + assert_eq!(receipt.held_out_sample_count, 12); + assert_eq!( + model.modes().unwrap().empty_room_residual_refinement_count, + 1 + ); + assert!(model + .refine_empty_room_residual_boundary(&residuals) + .is_err()); + } + + #[cfg(feature = "eigenvalue")] + #[test] + fn runtime_reference_rejects_shifted_held_out_window() { + let config = FieldModelConfig { + n_links: 1, + n_subcarriers: 56, + n_modes: 3, + min_calibration_frames: 500, + min_calibration_duration_s: 0.0, + baseline_expiry_s: 86_400.0, + }; + let mut model = FieldModel::new(config).unwrap(); + for frame_index in 0..600 { + let time = frame_index as f64 * 0.071; + let frame: Vec = (0..56) + .map(|subcarrier| { + let carrier = subcarrier as f64; + 18.0 + carrier * 0.08 + + (time + carrier * 0.13).sin() * 0.8 + + (time * 0.37 + carrier * 0.031).cos() * 0.35 + }) + .collect(); + model.feed_calibration(&[frame]).unwrap(); + } + model.finalize_calibration(1_000_000, 0).unwrap(); + + let shifted: Vec> = (0..RUNTIME_OCCUPANCY_WINDOW) + .map(|frame_index| { + let time = (600 + frame_index) as f64 * 0.071; + (0..56) + .map(|subcarrier| { + let carrier = subcarrier as f64; + let body_shift = if subcarrier % 3 == 0 { 8.0 } else { -5.0 }; + 18.0 + carrier * 0.08 + + (time + carrier * 0.13).sin() * 0.8 + + (time * 0.37 + carrier * 0.031).cos() * 0.35 + + body_shift + }) + .collect() + }) + .collect(); + assert!(model.estimate_occupancy(&shifted).unwrap() >= 1); + let background = model.empty_room_match(&shifted).expect("shifted match"); + assert!(!background.matches_empty); + assert_eq!(background.score, Some(0.0)); + assert!(background.reliable); + assert!(background.normalized_residual_z.is_some()); + } + + #[cfg(feature = "eigenvalue")] + #[test] + fn sparse_background_reference_cannot_suppress_runtime_change() { + let config = FieldModelConfig { + n_links: 1, + n_subcarriers: 56, + n_modes: 3, + min_calibration_frames: 500, + min_calibration_duration_s: 0.0, + baseline_expiry_s: 86_400.0, + }; + let mut model = FieldModel::new(config).unwrap(); + let mut calibration = Vec::new(); + for frame_index in 0..600 { + let time = frame_index as f64 * 0.071; + let frame: Vec = (0..56) + .map(|subcarrier| { + let carrier = subcarrier as f64; + 18.0 + carrier * 0.08 + + (time + carrier * 0.13).sin() * 0.8 + + (time * 0.37 + carrier * 0.031).cos() * 0.35 + }) + .collect(); + model.feed_calibration(&[frame.clone()]).unwrap(); + calibration.push(frame); + } + model.finalize_calibration(1_000_000, 0).unwrap(); + model + .modes + .as_mut() + .unwrap() + .empty_room_residual_energy_reference + .truncate(9); + + let result = model + .empty_room_match(&calibration[550..600]) + .expect("background comparison"); + assert!(!result.reliable); + assert!(!result.matches_empty); + assert_eq!(result.score, None); + assert_eq!(result.maturity, 0.45); + } + + #[test] + fn runtime_background_reference_is_single_link_only() { + let config = FieldModelConfig { + n_links: 2, + n_subcarriers: 8, + n_modes: 2, + min_calibration_frames: 100, + min_calibration_duration_s: 0.0, + baseline_expiry_s: 86_400.0, + }; + let mut model = FieldModel::new(config).unwrap(); + for frame_index in 0..100 { + let phase = frame_index as f64 * 0.1; + model + .feed_calibration(&[ + (0..8).map(|index| phase + index as f64).collect(), + (0..8).map(|index| phase - index as f64).collect(), + ]) + .unwrap(); + } + model.finalize_calibration(1_000_000, 0).unwrap(); + + let modes = model.modes().unwrap(); + assert_eq!(modes.baseline_runtime_window_size, None); + assert_eq!(modes.baseline_runtime_eigenvalue_count, None); + assert_eq!(modes.empty_room_residual_energy_threshold, None); + assert!(modes.empty_room_residual_energy_reference.is_empty()); + } + #[test] fn test_baseline_eigenvalue_count_stored() { let config = FieldModelConfig { @@ -1417,6 +2364,7 @@ mod tests { n_subcarriers: 8, n_modes: 3, min_calibration_frames: 20, + min_calibration_duration_s: 0.0, baseline_expiry_s: 86_400.0, }; let mut model = FieldModel::new(config).unwrap(); @@ -1445,6 +2393,48 @@ mod tests { ); } + #[test] + fn snapshot_round_trip_preserves_aggregate_modes_without_raw_frames() { + let config = make_config(1, 4, 10); + let mut model = FieldModel::new(config).unwrap(); + for i in 0..10 { + model + .feed_calibration(&[vec![1.0 + i as f64 * 0.01, 2.0, 3.0, 4.0]]) + .unwrap(); + } + model.finalize_calibration(1_000_000, 0xCA1).unwrap(); + + let snapshot = model.export_snapshot().unwrap(); + let restored = FieldModel::from_snapshot(snapshot.clone(), 2_000_000).unwrap(); + + assert_eq!(restored.status(), CalibrationStatus::Fresh); + assert_eq!(restored.calibration_frame_count(), 0); + assert_eq!(restored.export_snapshot().unwrap(), snapshot); + } + + #[test] + fn snapshot_restore_rejects_expired_and_malformed_images() { + let config = make_config(1, 4, 10); + let mut model = FieldModel::new(config).unwrap(); + for _ in 0..10 { + model.feed_calibration(&[vec![1.0, 2.0, 3.0, 4.0]]).unwrap(); + } + model.finalize_calibration(1_000_000, 0).unwrap(); + let snapshot = model.export_snapshot().unwrap(); + + assert!(matches!( + FieldModel::from_snapshot(snapshot.clone(), 90_000_000_000), + Err(FieldModelError::BaselineExpired { .. }) + )); + + let mut malformed = snapshot; + malformed.modes.baseline[0].pop(); + assert!(matches!( + FieldModel::from_snapshot(malformed, 2_000_000), + Err(FieldModelError::InvalidConfig(_)) + )); + } + #[test] fn test_environmental_projection_removes_drift() { let config = make_config(1, 4, 10); @@ -1473,4 +2463,72 @@ mod tests { "Environmental projection should be non-zero for drifting subcarrier" ); } + + // ----------------------------------------------------------------- + // Wall-clock calibration gate (#1756) + // ----------------------------------------------------------------- + + #[test] + fn test_default_frame_target_covers_background_reference_windows() { + let cfg = FieldModelConfig::default(); + assert_eq!(cfg.min_calibration_duration_s, CALIBRATION_DURATION_S); + assert_eq!( + cfg.min_calibration_frames, MIN_CALIBRATION_FRAMES, + "frame target must cover the minimum independent runtime windows" + ); + assert_eq!(MIN_CALIBRATION_FRAMES, 1_000); + assert_eq!(MIN_BACKGROUND_REFERENCE_WINDOWS, 20); + } + + #[test] + fn test_finalize_requires_wall_clock_window() { + // Enough frames, but fed instantly: the fleet-fast scenario from #1756. + let mut config = make_config(1, 4, 5); + config.min_calibration_duration_s = 600.0; + let mut model = FieldModel::new(config).unwrap(); + for _ in 0..10 { + model + .feed_calibration(&make_observations(1, 4, 1.0)) + .unwrap(); + } + assert!(model.calibration_frame_count() >= 5); + match model.finalize_calibration(1_000_000, 0) { + Err(FieldModelError::InsufficientCalibrationDuration { needed_s, got_s }) => { + assert!((needed_s - 600.0).abs() < 1e-9); + assert!(got_s < 1.0, "test feeds frames instantly, got {got_s}s"); + } + other => panic!("expected InsufficientCalibrationDuration, got {other:?}"), + } + } + + #[test] + fn test_duration_gate_disabled_with_zero() { + // min_calibration_duration_s = 0 keeps the legacy frame-only gate. + let mut model = FieldModel::new(make_config(1, 4, 5)).unwrap(); + for _ in 0..5 { + model + .feed_calibration(&make_observations(1, 4, 1.0)) + .unwrap(); + } + assert!(model.finalize_calibration(1_000_000, 0).is_ok()); + } + + #[test] + fn test_calibration_clock_accessors() { + let mut model = FieldModel::new(make_config(1, 4, 5)).unwrap(); + // No frames yet: the session clock has not started. + assert_eq!(model.calibration_elapsed_s(), 0.0); + assert_eq!(model.calibration_frames_per_second(), 0.0); + + model + .feed_calibration(&make_observations(1, 4, 1.0)) + .unwrap(); + assert!(model.calibration_elapsed_s() >= 0.0); + assert!(model.calibration_frames_per_second() >= 0.0); + + // Reset clears the session clock. + model.reset_calibration(); + assert_eq!(model.calibration_elapsed_s(), 0.0); + assert_eq!(model.calibration_frames_per_second(), 0.0); + } }