diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e1d9aa6668..97b05eab93 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -405,6 +405,7 @@ jobs: # workflow, so it doesn't gate the rest of the pipeline. docker-build: name: Docker Build & Test + if: github.event_name == 'push' && github.ref == 'refs/heads/main' runs-on: ubuntu-latest needs: [code-quality, test, rust-tests] continue-on-error: true @@ -525,7 +526,7 @@ jobs: name: Notify runs-on: ubuntu-latest needs: [code-quality, test, rust-tests, ui-tests, performance-test, docker-build, docs] - if: always() + if: always() && github.event_name == 'push' && github.ref == 'refs/heads/main' permissions: contents: write # required by softprops/action-gh-release # GitHub Actions does not allow `secrets.X` directly in step-level `if:` diff --git a/.github/workflows/firmware-qemu.yml b/.github/workflows/firmware-qemu.yml index 1c295760c4..d792ed9ab6 100644 --- a/.github/workflows/firmware-qemu.yml +++ b/.github/workflows/firmware-qemu.yml @@ -22,49 +22,32 @@ on: - 'scripts/swarm_presets/**' - '.github/workflows/firmware-qemu.yml' +defaults: + run: + shell: bash + env: IDF_VERSION: "v5.4" - QEMU_REPO: "https://github.com/espressif/qemu.git" - QEMU_BRANCH: "esp-develop" jobs: build-qemu: name: Build Espressif QEMU - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 + # This SDK image installs Espressif's checksum-verified QEMU package and + # its runtime libraries. Match the image used by both consumers below. + # Avoid APT DNS and an unpinned floating QEMU source build on cold caches. + container: + image: espressif/idf:v5.4@sha256:f1e9f69dc052b9afc7801ca884e0ef40c17e014bb05ce73d9c09d29290bd17fb steps: - - name: Cache QEMU build - id: cache-qemu - uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 - with: - path: /opt/qemu-esp32 - # Include date component so cache refreshes monthly when branch updates - key: qemu-esp32s3-${{ env.QEMU_BRANCH }}-v5 - restore-keys: | - qemu-esp32s3-${{ env.QEMU_BRANCH }}- - - - name: Install QEMU build dependencies - if: steps.cache-qemu.outputs.cache-hit != 'true' + - name: Prepare SDK-provided Espressif QEMU run: | - sudo apt-get update - sudo apt-get install -y \ - git build-essential ninja-build pkg-config \ - libglib2.0-dev libpixman-1-dev libslirp-dev \ - libgcrypt20-dev \ - python3 python3-venv - - - name: Clone and build Espressif QEMU - if: steps.cache-qemu.outputs.cache-hit != 'true' - run: | - git clone --depth 1 -b "$QEMU_BRANCH" "$QEMU_REPO" /tmp/qemu-esp - cd /tmp/qemu-esp - mkdir build && cd build - ../configure \ - --target-list=xtensa-softmmu \ - --prefix=/opt/qemu-esp32 \ - --enable-slirp \ - --disable-werror - ninja -j$(nproc) - ninja install + . "$IDF_PATH/export.sh" + QEMU_BIN="$(command -v qemu-system-xtensa)" + QEMU_ROOT="$(dirname "$(dirname "$QEMU_BIN")")" + "$QEMU_BIN" --version + "$QEMU_BIN" -machine help | grep -E '^esp32s3[[:space:]]' + mkdir -p /opt/qemu-esp32 + cp -a "$QEMU_ROOT/." /opt/qemu-esp32/ - name: Verify QEMU binary run: | @@ -84,7 +67,7 @@ jobs: needs: build-qemu runs-on: ubuntu-latest container: - image: espressif/idf:v5.4 + image: espressif/idf:v5.4@sha256:f1e9f69dc052b9afc7801ca884e0ef40c17e014bb05ce73d9c09d29290bd17fb strategy: fail-fast: false @@ -117,26 +100,26 @@ jobs: - name: Install Python dependencies run: | - . $IDF_PATH/export.sh + . "$IDF_PATH/export.sh" pip install esptool esp-idf-nvs-partition-gen - name: Set target ESP32-S3 working-directory: firmware/esp32-csi-node run: | - . $IDF_PATH/export.sh + . "$IDF_PATH/export.sh" idf.py set-target esp32s3 - name: Build firmware (mock CSI mode) working-directory: firmware/esp32-csi-node run: | - . $IDF_PATH/export.sh + . "$IDF_PATH/export.sh" idf.py \ -D SDKCONFIG_DEFAULTS="sdkconfig.defaults;sdkconfig.qemu" \ build - name: Generate NVS matrix run: | - . $IDF_PATH/export.sh + . "$IDF_PATH/export.sh" python3 scripts/generate_nvs_matrix.py \ --output-dir firmware/esp32-csi-node/build/nvs_matrix \ --only ${{ matrix.nvs_config }} @@ -144,12 +127,12 @@ jobs: - name: Create merged flash image working-directory: firmware/esp32-csi-node run: | - . $IDF_PATH/export.sh + . "$IDF_PATH/export.sh" # Determine merge_bin arguments - OTA_ARGS="" + OTA_ARGS=() if [ -f build/ota_data_initial.bin ]; then - OTA_ARGS="0xf000 build/ota_data_initial.bin" + OTA_ARGS=(0xf000 build/ota_data_initial.bin) fi python3 -m esptool --chip esp32s3 merge_bin \ @@ -158,7 +141,7 @@ jobs: --fill-flash-size 8MB \ 0x0 build/bootloader/bootloader.bin \ 0x8000 build/partition_table/partition-table.bin \ - $OTA_ARGS \ + "${OTA_ARGS[@]}" \ 0x20000 build/esp32-csi-node.bin file_size() { stat -c%s "$1" 2>/dev/null || stat -f%z "$1" 2>/dev/null || wc -c < "$1"; } @@ -213,32 +196,35 @@ jobs: fuzz-test: name: Fuzz Testing (ADR-061 Layer 6) - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 + env: + CC: clang-18 steps: - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 with: submodules: recursive - - name: Install clang + - name: Verify hosted Clang + # Ubuntu 24.04 hosted runners already contain Clang 18 and libFuzzer. + # Do not fetch the redundant distro meta-package before testing. run: | - sudo apt-get update - sudo apt-get install -y clang + "$CC" --version - name: Build fuzz targets working-directory: firmware/esp32-csi-node/test - run: make all CC=clang + run: make all CC="$CC" - name: Run serialize fuzzer (60s) working-directory: firmware/esp32-csi-node/test - run: make run_serialize FUZZ_DURATION=60 || echo "FUZZER_CRASH=serialize" >> "$GITHUB_ENV" + run: make run_serialize CC="$CC" FUZZ_DURATION=60 || echo "FUZZER_CRASH=serialize" >> "$GITHUB_ENV" - name: Run edge enqueue fuzzer (60s) working-directory: firmware/esp32-csi-node/test - run: make run_edge FUZZ_DURATION=60 || echo "FUZZER_CRASH=edge" >> "$GITHUB_ENV" + run: make run_edge CC="$CC" FUZZ_DURATION=60 || echo "FUZZER_CRASH=edge" >> "$GITHUB_ENV" - name: Run NVS config fuzzer (60s) working-directory: firmware/esp32-csi-node/test - run: make run_nvs FUZZ_DURATION=60 || echo "FUZZER_CRASH=nvs" >> "$GITHUB_ENV" + run: make run_nvs CC="$CC" FUZZ_DURATION=60 || echo "FUZZER_CRASH=nvs" >> "$GITHUB_ENV" - name: Check for crashes working-directory: firmware/esp32-csi-node/test @@ -299,7 +285,7 @@ jobs: echo "::error::$f has unexpected size $SIZE (expected 24576)" exit 1 fi - echo " OK: $(basename $f) ($SIZE bytes)" + echo " OK: $(basename "$f") ($SIZE bytes)" done # --------------------------------------------------------------------------- @@ -319,7 +305,7 @@ jobs: needs: [build-qemu] runs-on: ubuntu-latest container: - image: espressif/idf:v5.4 + image: espressif/idf:v5.4@sha256:f1e9f69dc052b9afc7801ca884e0ef40c17e014bb05ce73d9c09d29290bd17fb steps: - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 @@ -337,13 +323,13 @@ jobs: - name: Install Python dependencies run: | - . $IDF_PATH/export.sh + . "$IDF_PATH/export.sh" pip install pyyaml esptool esp-idf-nvs-partition-gen - name: Build firmware for swarm working-directory: firmware/esp32-csi-node run: | - . $IDF_PATH/export.sh + . "$IDF_PATH/export.sh" idf.py set-target esp32s3 idf.py -D SDKCONFIG_DEFAULTS="sdkconfig.defaults;sdkconfig.qemu" build python3 -m esptool --chip esp32s3 merge_bin \ @@ -356,7 +342,7 @@ jobs: - name: Run swarm smoke test run: | - . $IDF_PATH/export.sh + . "$IDF_PATH/export.sh" EXIT_CODE=0 python3 scripts/qemu_swarm.py --preset ci_matrix \ --qemu-path /opt/qemu-esp32/bin/qemu-system-xtensa \ diff --git a/.github/workflows/mqtt-integration.yml b/.github/workflows/mqtt-integration.yml index 3a488dbe72..a11f52680c 100644 --- a/.github/workflows/mqtt-integration.yml +++ b/.github/workflows/mqtt-integration.yml @@ -33,11 +33,8 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 20 - # NB: we don't use a `services:` mosquitto container here because the - # eclipse-mosquitto:2.x image rejects anonymous connections by default - # and GH Actions `services` doesn't easily support mounting a custom - # config file. We start mosquitto manually in a step below with an - # inline `allow_anonymous true` config. + # Explicit vendor broker config; publish only the CI loopback port. + # No APT package install or production MQTT security policy is involved. env: RUVIEW_RUN_INTEGRATION: "1" @@ -50,29 +47,27 @@ jobs: with: submodules: recursive - - name: Install mosquitto + clients and start with allow_anonymous + - name: Start pinned Mosquitto broker with CI-only anonymous listener run: | - sudo apt-get update -qq - sudo apt-get install -y mosquitto mosquitto-clients - sudo systemctl stop mosquitto || true - # Inline config: anon listener on 11883 only — no TLS, no auth, - # OK for CI because we test the wire shape, not security. - # Production deployments enable mTLS per ADR-115 §3.9. - cat > /tmp/mosquitto-ci.conf <<'EOF' - listener 11883 + cat > "$RUNNER_TEMP/mosquitto-ci.conf" <<'CONFIG' + listener 1883 allow_anonymous true persistence false log_dest stdout - EOF - mosquitto -c /tmp/mosquitto-ci.conf -d + CONFIG + docker run --detach --user mosquitto --name ruview-mqtt-ci \ + --publish 127.0.0.1:11883:1883 \ + --mount "type=bind,source=$RUNNER_TEMP/mosquitto-ci.conf,target=/mosquitto/config/mosquitto.conf,readonly" \ + eclipse-mosquitto:2.0.18@sha256:d12c8f80dfc65b768bb9acecc7ef182b976f71fb681640b66358e5e0cf94e9e9 for i in {1..20}; do - if mosquitto_pub -h 127.0.0.1 -p 11883 -t healthcheck -m ok -q 0 2>/dev/null; then - echo "mosquitto reachable on 11883"; exit 0 + if docker exec ruview-mqtt-ci mosquitto_pub -h 127.0.0.1 -p 1883 -t healthcheck -m ok -q 0 \ + && python3 -c 'import socket; socket.create_connection(("127.0.0.1", 11883), timeout=3).close()'; then + exit 0 fi - sleep 2 + echo "Waiting for broker ($i/20)" + sleep 1 done - echo "mosquitto never became reachable" >&2 - tail -50 /var/log/mosquitto/*.log 2>/dev/null || true + docker logs ruview-mqtt-ci exit 1 - name: Install Rust toolchain @@ -90,14 +85,11 @@ jobs: python -m pip install --quiet pyyaml python scripts/validate-ha-blueprints.py - - name: Verify unit tests still pass under --features mqtt + - name: Verify MQTT-feature unit tests working-directory: v2 - # `cargo test` accepts a single TESTNAME filter, so we run the - # whole --lib suite here. That gives us the full 410-test green - # bar under --features mqtt (which is more reassuring than - # filtering anyway). + # Preserve this PR base's full MQTT-feature library suite. run: >- - cargo test -p wifi-densepose-sensing-server + cargo test --locked -p wifi-densepose-sensing-server --features mqtt --no-default-features --lib --no-fail-fast @@ -114,5 +106,8 @@ jobs: - name: Dump broker logs on failure if: failure() run: | - docker ps -a - docker logs $(docker ps -aqf "ancestor=eclipse-mosquitto:2.0.18") || true + docker logs ruview-mqtt-ci || true + + - name: Stop the owned CI broker + if: always() + run: docker rm --force ruview-mqtt-ci diff --git a/.github/workflows/npm-packages.yml b/.github/workflows/npm-packages.yml index ac7864f5a1..4244a75e9e 100644 --- a/.github/workflows/npm-packages.yml +++ b/.github/workflows/npm-packages.yml @@ -45,8 +45,10 @@ jobs: # flashing, training runner/gate, doctor, device access, and SSH # hosts. ADR-375 adds the MCP Apps console widget (~12 KB), the # Streamable HTTP transport and the terminal UI (384 KiB); ADR-378 the - # ruview-live showcase views (~37 KB): 448 KiB reviewed ceiling. - unpacked_budget: 458752 + # ruview-live showcase views (~37 KB): prior 448 KiB ceiling. + # Declarative node configuration adds a 44 KB runtime module. + # Measured pack: 479863 B; retain a 480 KiB hard feature ceiling. + unpacked_budget: 491520 - dir: harness/homecore build: false publishable: true diff --git a/.github/workflows/ruview-kernel-npm.yml b/.github/workflows/ruview-kernel-npm.yml index 8e850926ee..b7c32fdf90 100644 --- a/.github/workflows/ruview-kernel-npm.yml +++ b/.github/workflows/ruview-kernel-npm.yml @@ -77,6 +77,13 @@ jobs: with: node-version: '22' + # The ruv-FANN git dependency contains paths longer than Windows' + # 260-character limit. On a cold cargo cache, cargo's checkout fails + # with "path too long" unless git long-path support is on. + - name: Allow long paths for cargo git checkouts + if: runner.os == 'Windows' + run: git config --global core.longpaths true + - name: Rust ABI tests run: cargo test --manifest-path v2/Cargo.toml -p ruview-kernel --locked diff --git a/.github/workflows/security-scan.yml b/.github/workflows/security-scan.yml index 4b406ed775..897fda29de 100644 --- a/.github/workflows/security-scan.yml +++ b/.github/workflows/security-scan.yml @@ -38,7 +38,8 @@ jobs: working-directory: v2 run: | set -o pipefail - cargo audit --file Cargo.lock --json | tee cargo-audit.json + # Direct invocation avoids project-defined Cargo aliases. + cargo-audit audit --file Cargo.lock --json | tee cargo-audit.json - name: Upload Rust advisory report uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 diff --git a/docs/security/wasmtime-audit-20261119.md b/docs/security/wasmtime-audit-20261119.md new file mode 100644 index 0000000000..77d09f9aa4 --- /dev/null +++ b/docs/security/wasmtime-audit-20261119.md @@ -0,0 +1,57 @@ +# Wasmtime 36.x patch and advisory metadata reconciliation + +## Problem and actual patch + +RustSec database7133866a reported five entries against core wasmtime36.0.20. +Primary upstream advisory HTML and repository advisory API distinguish the actual +package/version scopes from those raw matches: + +| RustSec ID | Upstream scope | Result for this workspace | +|---|---|---| +|0569|[Core below36.0.21](https://github.com/bytecodealliance/wasmtime/security/advisories/GHSA-hj27-mpw9-ff4c)|Version-applicable; patch to36.0.21| +|0567|[Core branches starting at39](https://github.com/bytecodealliance/wasmtime/security/advisories/GHSA-2674-w29h-wqvj)|36.x outside affected ranges| +|0573|[Core branches starting at47](https://github.com/bytecodealliance/wasmtime/security/advisories/GHSA-wvgq-g5hf-6gm6)|36.x outside affected ranges| +|0566|[wasmtime-wasi](https://github.com/bytecodealliance/wasmtime/security/advisories/GHSA-5m8g-xhq4-4h93)|Package absent; RustSec matched the wrong package| +|0571|[wasmtime-wasi-http](https://github.com/bytecodealliance/wasmtime/security/advisories/GHSA-f8mm-ggr3-89wh)|Package absent; RustSec matched the wrong package| + +The compatible36.0.21 patch updates32 Wasmtime/Cranelift family lock entries, +retains Rust1.89 compatibility and keeps the optional backend default-off. The +internal cache is re-vendored from its actual36.0.21 archive, preserving the +reviewed zstd0.14.3 manifest correction and byte-identical upstream executable +source/tests/licenses. Existing audit.toml ignores are unchanged. + +An independent controlled imported/re-exported host-function regression panics in +36.0.20's get_module_export implementation. The same test passes with36.0.21. +It is retained under the optional Wasmtime feature. This tests the dependency +patch; it is not a claim that the existing application invokes that faulty API or +that a deployed exploit was demonstrated. + +## Upstream correction and native audit — 2026-11-20 + +RustSec commit `074ea9a7ed619f511b5b7374c7a97e98d1e98be4` corrected both +range errors by recording `<39.0.0` and `<47.0.0` as unaffected for0567/0573. +With the patched lockfile, the CI-pinned cargo-audit0.22.2 now exits0 with zero +native registry findings under the unchanged audit policy. + +The temporary selector reconciliation, fixtures and live GitHub advisory API +calls have therefore been removed. CI again retains only the unmodified native +JSON report and uses its exit status, with pipefail preserving auditor failure. +No custom classification, extra token, offline database or advisory exception +is needed. Any future native advisory failure remains a job failure. + +Invoke `cargo-audit audit --file Cargo.lock --json` directly from `v2/`. +Direct binary invocation avoids executing a project-defined Cargo audit alias; +the installed tool remains pinned to0.22.2. An actual controlled Cargo-alias +negative probe established that the old dispatcher could execute a benign marker +and return artificial clean data; the direct auditor ignored that alias. Those +negative-control reports are not real security evidence. + +The dependency patch remains necessary:36.x before36.0.21 is still affected by +0569. The controlled API regression and archive provenance checks above remain. +Historical raw reports and reconciliation receipts describe the earlier database, +not the current native result. cargo-audit0.22.1 separately reports local path ZIP +packages as registry matches; it is not the pinned CI tool. Actual registry ZIP9 +and adapter tests remain separately relevant to the held server integration. + +No plugin is enabled, helper replaced, app installed or physical sensor qualified +by this change. Merge and release remain separate maintainer decisions. diff --git a/firmware/esp32-csi-node/nodes.example.yaml b/firmware/esp32-csi-node/nodes.example.yaml new file mode 100644 index 0000000000..5420541415 --- /dev/null +++ b/firmware/esp32-csi-node/nodes.example.yaml @@ -0,0 +1,65 @@ +# RuView declarative node config — read by `ruview node` (harness/ruview/src/node-config.js). +# +# cp nodes.example.yaml ~/.ruview/nodes.yaml # keep the real one OUT of git +# ruview node plan # validate + show what would happen +# ruview node apply --watch # apply to each board as you plug it in +# ruview node status # read-only health of every node +# +# Supported YAML: block maps/lists, [a, b] lists, quoted or plain scalars and +# comments. Anchors, tags, multi-line strings and unknown keys are rejected. + +wifi: + ssid: "MyHome-2G" # 2.4 GHz network + # Path only: ruview never reads it, provision.py does. Inline passwords are refused. + password_file: ~/.ruview/wifi-pass # chmod 600; the password, one line + +# OTA key (64 hex chars, chmod 600). Without it, provisioning DISABLES remote +# OTA updates and config_push on the node. Create one with: +# (umask 077; set -C; openssl rand -hex 32 > ~/.ruview-ota-psk) +# ota: +# psk_file: ~/.ruview-ota-psk + +aggregator: + ip: auto # auto = this machine's private LAN IPv4 + port: 5005 + +firmware: + source: auto # auto: repo build/ unless sources changed | reuse: build/ as-is | bundle: release dir + variant: s3-8mb # s3-8mb | s3-4mb | c6 + # bundle: ~/Downloads/esp32-csi-node-firmware-4mb # source: bundle (needs SHA256SUMS) + +steps: [device, driver, flash, wifi, validate] # used by `node apply` + +validate: + wait_secs: 60 # max wait for the node's first CSI packet after provisioning + udp_secs: 15 # measurement window + min_pps: 20 # CSI rate floor (firmware README acceptance floor) + # max_loss: 0.4 # optional ceiling on CSI sequence loss (0-1) + +# Applied to every node unless the node overrides it. +defaults: + edge_tier: 2 # 0=raw, 1=stats, 2=vitals + # channel: 6 + # pres_thresh: 50 + +# Boards that are gone for good. Their node_id (kept in the registry) is +# released so a replacement can take it; plugging a retired board back in is refused. +# retired: +# - "aa:bb:cc:dd:ee:09" + +# Boards not listed get the next free node_id; the registry +# (~/.ruview/esp32-setup/devices.jsonl) keeps it stable across runs. +# Set false to refuse unlisted boards. +auto_assign: true + +# Matched by MAC (`ruview node apply` prints it). tdm_total = nodes with a tdm_slot. +nodes: + - mac: "aa:bb:cc:dd:ee:01" + node_id: 1 + zone: living-room + tdm_slot: 0 + # - mac: "aa:bb:cc:dd:ee:02" + # node_id: 2 + # zone: bedroom + # tdm_slot: 1 + # edge_tier: 1 diff --git a/harness/ruview/.harness/manifest.json b/harness/ruview/.harness/manifest.json index af7bb1f28b..2bfff649f0 100644 --- a/harness/ruview/.harness/manifest.json +++ b/harness/ruview/.harness/manifest.json @@ -23,7 +23,7 @@ ".mcp/servers.json": "fec6075400f8350d8075beac8306690355c4b015425bfd0e5f52966234e9d66f", "CLAUDE.md": "25f9306b6caae22078cb9375108b6f6bb813d83e617739f6d9372f116b0ede5d", "LICENSE": "631f94984f626818d42ecf717aa6e8e0afd4f9f355ca706bd2effafbd1416d06", - "README.md": "d761e75678ac48c391b86e9094c207193f01d351235b8c77fc709983e57accd7", + "README.md": "14ff0b70ea6b02e63d960a32523299a63e560d31b5fccd292220915e5a9ccf97", "bin/cli.js": "8ebf6ca4b6d9529a5e338374c19329c1a2fc2f9e52f13b4341928b7168ee461b", "brain/corpus/core.jsonl": "c0fb7b079ded157059b91601361429944697dae3cc42abc00dfe1a680986b0f4", "flywheel/evaluations.json": "ac4ff1f897a2444870cd2b8ae8aee8b1578e61467aeca4db57893f41be98a572", @@ -32,7 +32,7 @@ "flywheel/genome.json": "75db44a3cab70d9459fc8c07863f640ac1214bfaa243483939e1506d63f51214", "flywheel/replay.mjs": "0670ca0b03701f4afe0b4bca8a3d58d481676b61a94a5b98c6a425aefb1159ab", "flywheel/run.mjs": "6d4f97db16900c45367b6538848cbe1915af999e663720dfc51f2bb1698f1cd0", - "package.json": "d6ba81ba3cad035c5ddd17d2d84d1f02a273c6ac588b77c1592497d720803f3c", + "package.json": "96014281a1c3be5c43cb13f0a79bcf2ec425271c37f832ac55e97b2bfb7c104a", "scripts/sync-skills.mjs": "43715dab61e204dc91bbd61755810e8fdb2f66e2b0c0bd791b4bf48a2e293565", "scripts/update-manifest.mjs": "8f56764b8f70aed55da0c7e2417ae875b0d58d781d839b6db7f115f08af61e6b", "scripts/verify-manifest.mjs": "6491a221762efcfeb3e749ecab243b204f17fd5bc871f3d4025597f31b8f0f10", @@ -45,8 +45,8 @@ "skills/verify.md": "2d38d240e9810a7827e2ebd3717dc0f85c646cc92e46c3812fe77c5b9eb40b76", "src/brain.js": "0f16a75aea943acdacc430ff11d5df7ecdec9cca2ab497795ff6f33eaebdfab6", "src/cli-ui.js": "5ac04c5790f03df708c24a6dd366b51ff33a643691642b689284bcada25c9d9a", - "src/cli.js": "9b604f5c05a9d3338bd56f4386272a2624e7c5ee9676fcbc4bbfff6b86916571", - "src/devices/esp32.js": "21b8c74d9b0a47f52538b5d902faa32ac6f4c58dab94d6612d7fbc89237d8f80", + "src/cli.js": "94fd0631944bec2386bf8f3b2e59abd7058fa4f68bd05943eb2a3cff101dcc74", + "src/devices/esp32.js": "587e875cdaddb7b942c4e2c0521595456894762b842446e553b8c69d2c1ad32f", "src/devices/esphome.js": "d4429fd09c8d93fdc2e5926305d8193c98e074affc48deabda4b16bb9b620056", "src/devices/lidar.js": "d9a91e2b6eec21463bbb950fc333e9d0ebd2699a7c533d534323bb4d6a55bd97", "src/devices/mmwave.js": "2ac0be6c4be3c71d147d4bc57b8fd516d051234a8897295bc9e7f9c36c8ae710", @@ -63,6 +63,7 @@ "src/kernel.js": "48dfd218a747dd158f82ae108e7e8ed89a82cd68649214d23266112dc9033f5d", "src/mcp-http.js": "b72e021ae00d01831ef7184db824f5e85a2b8747e1d8e994b6a1cb1cb4d08945", "src/mcp-server.js": "5eb8c3431768932c3a85476df032d0608fc7a732ede3d6bdcc8467ca47552666", + "src/node-config.js": "7d76efae4d6214b8f635fb2d9c7efbec0520892b959b7bb035c3b1ad9d22f09e", "src/policy.js": "baad5eb8c3eb2e416e0a71b342ad850d7c8413d7d976571ebf596937f69da8e2", "src/process-runner.js": "49533b038044dfb8bc76ed01c030d06a9856ead0836157fb693e2a7d40f786d6", "src/redact.js": "ebf1afff46341078706b0401838c53db043603586e280d51ece5cf1feba35189", @@ -75,7 +76,7 @@ "src/training.js": "bd8a6c12eba4f9d2f8990fa9692cd04c743ea247f9b13e10f8717ceefcbd09ac", "src/ui/console-widget.js": "0d2f707d57b5ec54968dee1dc80f31c7db9152dcadfc48700c18379dabb55462" }, - "filesDigest": "c946d18daae8b4cf29a5d79a39d266e2c6b37754d262ba7584c2268f55e5c76f", + "filesDigest": "06f83e927733f8964056cce94d0790c5631155e9198f31ecb5a9d50ae91cee54", "brainDigest": "c0fb7b079ded157059b91601361429944697dae3cc42abc00dfe1a680986b0f4", "gateFingerprint": "6e53c784eee38310188948fc75fb49e6b4ebc04e247d01b903fa8c8a92d67bdd", "developmentPins": { diff --git a/harness/ruview/.harness/manifest.sha256 b/harness/ruview/.harness/manifest.sha256 index 298a252ba9..dc31eaaa9a 100644 --- a/harness/ruview/.harness/manifest.sha256 +++ b/harness/ruview/.harness/manifest.sha256 @@ -1 +1 @@ -baa345cff1caafc8d2bf686db32dfd00a812aaf2ccb786d1e6afc54d1c908a07 manifest.json +a0dbdf2751dd2ec155c88e99ecb45436ebecd01c5e41eeb7933fd06c77f3128b manifest.json diff --git a/harness/ruview/README.md b/harness/ruview/README.md index 2e107e1637..f57fa9952f 100644 --- a/harness/ruview/README.md +++ b/harness/ruview/README.md @@ -140,6 +140,44 @@ panic. A successful write alone is not hardware validation. WiFi provisioning stays with `provision.py` (see the `provision-node` skill) so credentials never pass through the harness. +## Declarative node config (`ruview node`) + +One file, `~/.ruview/nodes.yaml` (template: +`firmware/esp32-csi-node/nodes.example.yaml`), declares the WiFi network, the +aggregator, the firmware source and every node by MAC: node ID, zone, TDM slot +and provisioning overrides. `ruview node apply` brings an attached board to its +declared state. + +```bash +ruview node plan # validate config; nodes, firmware, secrets plan +ruview node status # read-only: per-node CSI rate/loss/RSSI + :8032 status +ruview node apply # read MAC, show the plan, ask "yes" before writing +ruview node apply --watch # each board plugged in: plan → yes → apply +ruview node apply --firmware-only --all --confirm # no questions (scripts); flash + validate, settings kept +``` + +- **Confirmation:** at a terminal every write is shown first and needs a + typed `yes`. `--confirm` skips the question. Without a terminal (pipes, + `--json`, MCP) and without `--confirm`, only the plan is printed. +- **Matching:** a board is identified by `esptool read_mac`. Listed MACs take + their configured node. Unlisted MACs reuse the node ID they had before, from + `~/.ruview/esp32-setup/devices.jsonl`, or get the next free ID unless + `auto_assign: false`. Two boards may never share an ID: `plan` and `apply` + both refuse a conflict. A board that is gone for good goes under + `retired:`, which releases its retained ID for a replacement. +- **Secrets:** `wifi.password_file` and `ota.psk_file` are paths (owner-only + files) passed to `provision.py`. The harness never reads them. Inline + passwords and env-var sources are refused. +- **Flashing:** reuses `flash` (checksums, chip check, boot log). `source: + auto|reuse` flashes the repo's ESP-IDF `build/` as an unverified local + build. `auto` refuses a build older than the firmware sources. +- **Validation:** waits for the node's CSI on the aggregator UDP port, then + checks `validate.min_pps` and the optional `validate.max_loss`. This is a + live measurement on this host, so stop the sensing server first (UDP port in + use otherwise). +- **YAML:** a strict subset (no dependency). Anchors, tags, multi-line strings, + flow maps, tabs, duplicate and unknown keys are errors. + ## Training (ADR-371) ```bash diff --git a/harness/ruview/package.json b/harness/ruview/package.json index 896f498efa..f9d3be4c40 100644 --- a/harness/ruview/package.json +++ b/harness/ruview/package.json @@ -47,7 +47,7 @@ ], "scripts": { "test": "node --test --test-concurrency=2 test/*.test.mjs", - "test:security": "node --test test/hosts.test.mjs test/brain.test.mjs test/policy.test.mjs test/spaces.test.mjs test/kernel.test.mjs test/firmware.test.mjs test/training.test.mjs test/devices.test.mjs test/csi-nodes.test.mjs test/remote.test.mjs test/mcp-apps.test.mjs test/cli-ui.test.mjs test/esphome.test.mjs", + "test:security": "node --test test/hosts.test.mjs test/brain.test.mjs test/policy.test.mjs test/spaces.test.mjs test/kernel.test.mjs test/firmware.test.mjs test/training.test.mjs test/devices.test.mjs test/csi-nodes.test.mjs test/remote.test.mjs test/mcp-apps.test.mjs test/cli-ui.test.mjs test/esphome.test.mjs test/node-config.test.mjs", "doctor": "node ./bin/cli.js doctor", "mcp": "node ./bin/cli.js mcp start", "brain:verify": "node ./bin/cli.js brain verify", diff --git a/harness/ruview/src/cli.js b/harness/ruview/src/cli.js index 91c5ba58b4..1663bbe424 100644 --- a/harness/ruview/src/cli.js +++ b/harness/ruview/src/cli.js @@ -182,6 +182,15 @@ Firmware (ADR-370, cross-platform esptool): flash --port

--bundle

[--variant ...] [--baud 460800] --confirm [--boot-log-seconds 15] [--allow-unverified] write flash, then capture boot evidence +Node config (YAML: ~/.ruview/nodes.yaml, see firmware/esp32-csi-node/nodes.example.yaml): + node [plan] validate the config, list nodes, firmware + secrets plan + node status read-only: live CSI per node + each node's :8032 status + node apply [--port P | --all | --watch] [--confirm] bring the board (by MAC) to its config: config steps, + default flash → provision WiFi/NVS → validate + node apply --firmware-only [...] flash + validate; keeps WiFi/node settings + at a terminal: shows the plan and asks "yes" per board; --confirm skips the + question; pipes/--json without --confirm only print the plan + Devices (ADR-373) — run on the machine the hardware is attached to: devices classify USB serial devices (ESP32, Realtek, mmWave, RPLIDAR) esp32 [--udp-port 5005] [--seconds 10] [--bind 0.0.0.0] receive + summarize ESP32 / Realtek RAC1 node UDP streams @@ -224,6 +233,122 @@ Hosts implemented and tested locally: claude-code (-p), codex (exec)`); return 0; } +/** `ruview node [plan|status|apply]` — declarative node config (node-config.js). */ +async function nodeConfig(rest, flags) { + const action = rest[0] && !rest[0].startsWith('--') ? rest[0] : 'plan'; + if (!['plan', 'status', 'apply'].includes(action)) { + console.error('Usage: ruview node [plan|status|apply] [--config ~/.ruview/nodes.yaml] [--port P | --all | --watch] [--firmware-only] [--confirm] [--json]'); + return 2; + } + const flow = action === 'apply' && flags['firmware-only'] === true ? 'firmware' : 'apply'; + const [fl, { OPERATOR_DEPS }, { captureEsp32 }] = await Promise.all([import('./node-config.js'), tools(), import('./devices/esp32.js')]); + const asJson = flags.json === true || !process.stdout.isTTY; + const out = (kind, res) => (asJson ? pjson(res) : console.log(fl.formatNodes(kind, res))); + let config; + try { + config = fl.loadNodeConfig(typeof flags.config === 'string' ? flags.config : fl.DEFAULT_CONFIG_PATH); + } catch (e) { + if (!(e instanceof fl.NodeConfigError)) throw e; + out('error', { ok: false, reason: e.reason, detail: e.message, errors: e.errors }); + return 2; + } + // cwd first (an explicit checkout wins), then the checkout this CLI ships in, + // so `ruview node` works from any directory. + const repoRoot = OPERATOR_DEPS.findRepoRoot() || OPERATOR_DEPS.findRepoRoot(ROOT); + const tty = process.stderr.isTTY && !asJson; + const deps = { + exec: OPERATOR_DEPS.exec, python: OPERATOR_DEPS.python, fetch: OPERATOR_DEPS.fetch, + capture: (args) => captureEsp32(args), repoRoot, env: process.env, + log: (msg) => { if (tty) process.stderr.write(` … ${msg}\n`); }, + }; + + if (action === 'plan') { const res = fl.planNodes(config, { repoRoot }); out('plan', res); return res.ok ? 0 : 1; } + if (action === 'status') { + const res = await withProgress(`Listening on UDP :${config.aggregator.port}`, config.validate.udp_secs, () => fl.nodeStatus(config, deps)); + out('status', res); return res.ok ? 0 : 1; + } + + // apply: one port, every ESP32 candidate (--all), or each new one (--watch). + // --confirm writes without asking. Otherwise a person at a terminal sees the + // plan and must type yes; pipes/--json/MCP only ever get the plan (fail closed). + const confirm = flags.confirm === true; + const interactive = !confirm && !asJson && process.stdin.isTTY === true && process.stderr.isTTY === true; + const askYes = async (question) => { + const { createInterface } = await import('node:readline/promises'); + const rl = createInterface({ input: process.stdin, output: process.stderr }); + try { return /^(y|yes)$/i.test((await rl.question(question)).trim()); } + catch { return false; } // Ctrl+C / closed stdin = no + finally { rl.close(); } + }; + const one = async (port) => { + if (confirm) { + const res = await fl.applyBoard(config, { port, flow, confirm: true }, deps); + out(action, res); + return res.ok; + } + const plan = await fl.applyBoard(config, { port, flow, confirm: false }, deps); + if (interactive && plan.ok) delete plan.next; + out(action, plan); + if (!plan.ok || !interactive) return plan.ok; + const parts = plan.steps.filter((x) => ['flash', 'wifi'].includes(x)).map((x) => (x === 'flash' ? 'firmware' : 'WiFi/node settings')); + const what = `write ${parts.join(' + ') || 'nothing'} to`; + if (!(await askYes(` ? ${what} ${plan.mac} (node ${plan.node_id}) on ${port}? Type yes to continue [y/N] `))) { + console.error(' skipped — nothing was written'); + return true; + } + const res = await fl.applyBoard(config, { port, flow, confirm: true, + expectedIdentity: { mac: plan.mac, nodeId: plan.node_id } }, deps); + out(action, res); + return res.ok; + }; + const candidates = async () => { + const { scanDevices } = await import('./devices/registry.js'); + const scan = await scanDevices(OPERATOR_DEPS); + if (!scan.ok) { + // Name the interpreter: an unrelated active venv shadowing python3 is the usual cause. + const py = OPERATOR_DEPS.python() || 'python3'; + throw new fl.NodeConfigError(scan.reason, scan.reason === 'pyserial_missing' + ? `${py} has no pyserial; run \`${py} -m pip install pyserial esptool\` or put a venv that has them first on PATH` + : (scan.remedy || 'serial scan failed')); + } + return scan.devices.filter((d) => d.likelyRoles.includes('esp32')).map((d) => d.port); + }; + try { + if (typeof flags.port === 'string') return (await one(flags.port)) ? 0 : 1; + if (flags.watch === true) { + if (!confirm && !interactive) { console.error('node: --watch needs a terminal (asks per board) or --confirm'); return 2; } + const done = new Set(); + let stop = false; let failures = 0; + process.once('SIGINT', () => { stop = true; }); + console.error(`Watching for ESP32 boards (${flow}). Plug them in one at a time; Ctrl+C to finish.`); + while (!stop) { + const ports = await candidates(); + for (const p of done) if (!ports.includes(p)) done.delete(p); // unplugged → socket reusable + const next = ports.find((p) => !done.has(p)); + if (!next) { await new Promise((r) => setTimeout(r, 1000)); continue; } + await new Promise((r) => setTimeout(r, 1000)); // let the port settle + if (!(await one(next))) failures += 1; + done.add(next); + console.error('→ unplug it and plug in the next board'); + } + return failures ? 1 : 0; + } + const ports = await candidates(); + if (flags.all === true) { + if (!ports.length) { console.error('node: no ESP32 candidates found (ruview devices)'); return 1; } + let ok = true; + for (const p of ports) ok = (await one(p)) && ok; + return ok ? 0 : 1; + } + if (ports.length !== 1) { console.error(`node: found ${ports.length} ESP32 candidates (${ports.join(', ') || 'none'}); pass --port, --all or --watch`); return 2; } + return (await one(ports[0])) ? 0 : 1; + } catch (e) { + if (!(e instanceof fl.NodeConfigError)) throw e; + out('error', { ok: false, reason: e.reason, detail: e.message }); + return 1; + } +} + /** tiny flag parser: --k v / --k=v / --flag (boolean) */ function parseFlags(rest) { const f = {}; @@ -291,6 +416,7 @@ export async function run(args, opts = {}) { switch (cmd) { case 'doctor': return doctor(flags); + case 'node': return nodeConfig(rest, flags); case 'skills': console.log(listSkills().join('\n') || '(none)'); return 0; case 'skill': { const n = rest[0]; diff --git a/harness/ruview/src/devices/esp32.js b/harness/ruview/src/devices/esp32.js index a49fd3c407..98f71c3b8b 100644 --- a/harness/ruview/src/devices/esp32.js +++ b/harness/ruview/src/devices/esp32.js @@ -258,6 +258,7 @@ export class NodeStats { this.nodes.set(key, n); } n.packets[pkt.kind] = (n.packets[pkt.kind] || 0) + 1; + if (sender) n.address = sender; // last sender: lets callers reach the node (e.g. :8032) if (pkt.kind === 'csi') { n.rssiSum += pkt.rssi; n.rssiCount += 1; trackSequence(n, pkt.seq); @@ -290,6 +291,7 @@ export class NodeStats { return { source: n.source, nodeId: n.nodeId, + ...(n.address ? { address: n.address } : {}), packets: n.packets, csiRateHz: Number((csi / seconds).toFixed(2)), csiLossFraction: received ? Number((n.seqGaps / received).toFixed(4)) : null, diff --git a/harness/ruview/src/node-config.js b/harness/ruview/src/node-config.js new file mode 100644 index 0000000000..c387eed82c --- /dev/null +++ b/harness/ruview/src/node-config.js @@ -0,0 +1,737 @@ +// SPDX-License-Identifier: MIT +// Declarative node config for ESP32 CSI nodes: one YAML file describes the +// WiFi, the aggregator, the firmware and every node (matched by MAC). +// `ruview node plan|apply|status` brings attached boards to that state. +// +// Zero runtime dependencies: the YAML reader below accepts a strict subset +// (block maps, block lists, flow lists of scalars, quoted/plain scalars, +// comments) and rejects everything else (anchors, tags, multi-line scalars, +// flow maps, tabs, duplicate keys) instead of guessing. +// +// Secrets: the WiFi password and OTA key are referenced by file path only and +// handed to provision.py as paths, so credential values never pass through the +// harness (README "Firmware flashing"). Inline passwords and env-var sources +// are refused. + +import { + appendFileSync, existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, statSync, +} from 'node:fs'; +import { homedir, networkInterfaces } from 'node:os'; +import { dirname, join, resolve } from 'node:path'; +import { FIRMWARE_VARIANTS, FirmwareError, flashFirmware, resolveBundle, validatePort } from './firmware.js'; + +export class NodeConfigError extends Error { + constructor(reason, message) { super(message); this.reason = reason; } +} + +// ───────────────────────────────────────────────────────── YAML subset ──── + +function stripComment(line) { + let quote = null; + for (let i = 0; i < line.length; i++) { + const c = line[i]; + if (quote) { if (c === quote) quote = null; else if (c === '\\' && quote === '"') i++; continue; } + if (c === '"' || c === "'") quote = c; + else if (c === '#' && (i === 0 || /\s/.test(line[i - 1]))) return line.slice(0, i); + } + return line; +} + +function parseScalar(raw, lineNo) { + const v = raw.trim(); + const fail = (what) => { throw new NodeConfigError('yaml_unsupported', `line ${lineNo}: ${what} is not supported in node-config YAML`); }; + if (v === '' || v === '~' || v === 'null') return null; + if (/^[&*!|>{@`%]/.test(v)) fail(`"${v[0]}" (anchors, tags, block scalars, flow maps)`); + if (v.startsWith('[')) { + if (!v.endsWith(']')) fail('a multi-line flow list'); + const inner = v.slice(1, -1).trim(); + if (!inner) return []; + if (/[[\]{}]/.test(inner)) fail('a nested flow collection'); + return inner.split(',').map((item) => parseScalar(item, lineNo)); + } + if (v.startsWith('"')) { + if (!/^"(?:[^"\\]|\\.)*"$/.test(v)) throw new NodeConfigError('yaml_invalid', `line ${lineNo}: unterminated double-quoted string`); + return v.slice(1, -1).replace(/\\(.)/g, (_, ch) => ({ n: '\n', t: '\t', '"': '"', '\\': '\\' })[ch] ?? fail(`escape \\${ch}`)); + } + if (v.startsWith("'")) { + if (!/^'(?:[^']|'')*'$/.test(v)) throw new NodeConfigError('yaml_invalid', `line ${lineNo}: unterminated single-quoted string`); + return v.slice(1, -1).replaceAll("''", "'"); + } + if (v === 'true') return true; + if (v === 'false') return false; + if (/^-?\d+$/.test(v)) return Number(v); + if (/^-?\d+\.\d+$/.test(v)) return Number(v); + return v; +} + +const KEY_RE = /^([A-Za-z_][A-Za-z0-9_-]*):(?:\s+(.*))?$/; +const isItem = (text) => text === '-' || text.startsWith('- '); + +/** Parse the node-config YAML subset into plain objects/arrays/scalars. */ +export function parseNodeYaml(text) { + const lines = []; + String(text).split(/\r?\n/).forEach((raw, idx) => { + const lineNo = idx + 1; + const body = stripComment(raw).replace(/\s+$/, ''); + if (!body.trim() || body.trim() === '---') return; + const lead = /^[ \t]*/.exec(body)[0]; + if (lead.includes('\t')) throw new NodeConfigError('yaml_invalid', `line ${lineNo}: tabs are not allowed for indentation`); + lines.push({ indent: lead.length, text: body.slice(lead.length), lineNo }); + }); + if (!lines.length) return {}; + + function block(i, indent) { + return isItem(lines[i].text) ? list(i, indent) : map(i, indent); + } + function map(i, indent) { + const out = {}; + while (i < lines.length && lines[i].indent === indent && !isItem(lines[i].text)) { + const { text, lineNo } = lines[i]; + const m = KEY_RE.exec(text); + if (!m) throw new NodeConfigError('yaml_invalid', `line ${lineNo}: expected "key: value"`); + const key = m[1]; + if (key === '__proto__' || key === 'constructor' || key === 'prototype') throw new NodeConfigError('yaml_invalid', `line ${lineNo}: reserved key "${key}"`); + if (Object.hasOwn(out, key)) throw new NodeConfigError('yaml_invalid', `line ${lineNo}: duplicate key "${key}"`); + i += 1; + if (m[2] !== undefined && m[2].trim() !== '') { + out[key] = parseScalar(m[2], lineNo); + } else if (i < lines.length && (lines[i].indent > indent || (lines[i].indent === indent && isItem(lines[i].text)))) { + [out[key], i] = block(i, lines[i].indent); + } else { + out[key] = null; + } + } + if (i < lines.length && lines[i].indent > indent) throw new NodeConfigError('yaml_invalid', `line ${lines[i].lineNo}: unexpected indentation`); + return [out, i]; + } + function list(i, indent) { + const out = []; + while (i < lines.length && lines[i].indent === indent && isItem(lines[i].text)) { + const { text, lineNo } = lines[i]; + const rest = text === '-' ? '' : text.slice(2); + if (rest.startsWith(' ')) throw new NodeConfigError('yaml_invalid', `line ${lineNo}: use exactly one space after "-"`); + if (!rest) { + i += 1; + if (i < lines.length && lines[i].indent > indent) [out[out.length], i] = block(i, lines[i].indent); + else out.push(null); + } else if (KEY_RE.test(rest)) { + lines[i] = { indent: indent + 2, text: rest, lineNo }; // "- key: v" opens a map at indent+2 + const [value, next] = map(i, indent + 2); + out.push(value); i = next; + } else { + out.push(parseScalar(rest, lineNo)); i += 1; + } + } + if (i < lines.length && lines[i].indent > indent) throw new NodeConfigError('yaml_invalid', `line ${lines[i].lineNo}: unexpected indentation`); + return [out, i]; + } + + const [value, end] = block(0, lines[0].indent); + if (end < lines.length) throw new NodeConfigError('yaml_invalid', `line ${lines[end].lineNo}: unexpected content at lower indentation`); + if (Array.isArray(value)) throw new NodeConfigError('yaml_invalid', 'node config must be a mapping at the top level'); + return value; +} + +// ──────────────────────────────────────────────────────── config schema ─── + +export const DEFAULT_CONFIG_PATH = join(homedir(), '.ruview', 'nodes.yaml'); +export const DEFAULT_REGISTRY_PATH = join(homedir(), '.ruview', 'esp32-setup', 'devices.jsonl'); +export const STEPS = Object.freeze(['device', 'driver', 'flash', 'wifi', 'validate']); +const MAC_RE = /^([0-9a-f]{2}:){5}[0-9a-f]{2}$/; +const IPV4_RE = /^(25[0-5]|2[0-4]\d|1?\d?\d)(\.(25[0-5]|2[0-4]\d|1?\d?\d)){3}$/; + +/** Provisioning keys a node (or `defaults`) may set → provision.py flag + check. */ +const PROVISION_KEYS = Object.freeze({ + edge_tier: { flag: '--edge-tier', check: (v) => [0, 1, 2].includes(v), want: '0, 1 or 2' }, + channel: { flag: '--channel', check: (v) => Number.isInteger(v) && ((v >= 1 && v <= 14) || (v >= 36 && v <= 177)), want: '1-14 or 36-177' }, + filter_mac: { flag: '--filter-mac', check: (v) => typeof v === 'string' && MAC_RE.test(v.toLowerCase()), want: 'AA:BB:CC:DD:EE:FF' }, + hop_channels: { flag: '--hop-channels', check: (v) => Array.isArray(v) && v.length > 0 && v.every((c) => Number.isInteger(c) && c > 0 && c <= 177), want: 'a list of channels', format: (v) => v.join(',') }, + hop_dwell: { flag: '--hop-dwell', check: (v) => Number.isInteger(v) && v >= 10 && v <= 60000, want: '10-60000 ms' }, + pres_thresh: { flag: '--pres-thresh', check: (v) => Number.isInteger(v) && v >= 0, want: 'a non-negative integer' }, + fall_thresh: { flag: '--fall-thresh', check: (v) => Number.isInteger(v) && v >= 0, want: 'a non-negative integer' }, + vital_win: { flag: '--vital-win', check: (v) => Number.isInteger(v) && v > 0, want: 'a positive integer' }, + vital_int: { flag: '--vital-int', check: (v) => Number.isInteger(v) && v > 0, want: 'a positive integer' }, + subk_count: { flag: '--subk-count', check: (v) => Number.isInteger(v) && v > 0, want: 'a positive integer' }, + zone: { flag: '--zone', check: (v) => typeof v === 'string' && /^[\w .-]{1,32}$/.test(v), want: '1-32 chars of letters, digits, space, . _ -' }, +}); +const NODE_KEYS = new Set(['mac', 'node_id', 'tdm_slot', ...Object.keys(PROVISION_KEYS)]); +const SECTIONS = Object.freeze({ + wifi: new Set(['ssid', 'password_file', 'password_env', 'password']), + aggregator: new Set(['ip', 'port']), + firmware: new Set(['source', 'bundle', 'variant', 'allow_unverified', 'erase', 'baud']), + validate: new Set(['wait_secs', 'udp_secs', 'min_pps', 'max_loss']), + ota: new Set(['psk_file']), +}); +const TOP_KEYS = new Set([...Object.keys(SECTIONS), 'defaults', 'nodes', 'retired', 'steps', 'auto_assign', 'registry']); + +const expandHome = (p, home = homedir()) => (typeof p === 'string' && (p === '~' || p.startsWith('~/')) ? join(home, p.slice(2)) : p); + +/** + * Validate a parsed node-config document and fill defaults. Collects every error + * (fail closed) rather than stopping at the first one. + */ +export function normalizeNodeConfig(doc, { home = homedir() } = {}) { + const errors = []; + const err = (path, msg) => errors.push(`${path}: ${msg}`); + const obj = (v) => v !== null && typeof v === 'object' && !Array.isArray(v); + if (!obj(doc)) throw new NodeConfigError('config_invalid', 'node config must be a mapping'); + for (const k of Object.keys(doc)) if (!TOP_KEYS.has(k)) err(k, `unknown key (allowed: ${[...TOP_KEYS].join(', ')})`); + const section = (name) => { + const v = doc[name] ?? {}; + if (!obj(v)) { err(name, 'must be a mapping'); return {}; } + for (const k of Object.keys(v)) if (!SECTIONS[name].has(k)) err(`${name}.${k}`, `unknown key (allowed: ${[...SECTIONS[name]].join(', ')})`); + return v; + }; + const wifi = section('wifi'); + const agg = section('aggregator'); + const fw = section('firmware'); + const val = section('validate'); + const ota = section('ota'); + + // WiFi — secrets by reference only. + if (wifi.password !== undefined) err('wifi.password', 'inline passwords are refused; use password_file (chmod 600)'); + if (wifi.password_env !== undefined) err('wifi.password_env', 'not supported: credentials never pass through the harness; use password_file (chmod 600)'); + if (wifi.ssid !== undefined && (typeof wifi.ssid !== 'string' || !wifi.ssid || wifi.ssid.length > 32)) err('wifi.ssid', 'must be a 1-32 character string'); + if (wifi.password_file !== undefined && typeof wifi.password_file !== 'string') err('wifi.password_file', 'must be a path'); + if (ota.psk_file !== undefined && typeof ota.psk_file !== 'string') err('ota.psk_file', 'must be a path'); + + // Aggregator. + const ip = agg.ip ?? 'auto'; + if (ip !== 'auto' && !(typeof ip === 'string' && IPV4_RE.test(ip))) err('aggregator.ip', 'must be "auto" or an IPv4 address'); + const port = agg.port ?? 5005; + if (!Number.isInteger(port) || port < 1024 || port > 65535) err('aggregator.port', 'must be an integer 1024-65535'); + + // Firmware. + const source = fw.source ?? 'auto'; + if (!['auto', 'reuse', 'build', 'bundle'].includes(source)) err('firmware.source', 'must be auto, reuse, build or bundle'); + if (source === 'bundle' && typeof fw.bundle !== 'string') err('firmware.bundle', 'required when source is bundle'); + const variant = fw.variant ?? 's3-8mb'; + if (!FIRMWARE_VARIANTS[variant]) err('firmware.variant', `must be one of ${Object.keys(FIRMWARE_VARIANTS).join(', ')}`); + if (fw.erase === true) err('firmware.erase', 'erase is not supported by `ruview node` (it would wipe NVS); erase manually with esptool if you must'); + for (const b of ['allow_unverified', 'erase']) if (fw[b] !== undefined && typeof fw[b] !== 'boolean') err(`firmware.${b}`, 'must be true or false'); + const baud = fw.baud ?? 460800; + if (![115200, 230400, 460800, 921600].includes(baud)) err('firmware.baud', 'must be 115200, 230400, 460800 or 921600'); + + // Validation thresholds. + const num = (path, v, lo, hi, dflt) => { + if (v === undefined || v === null) return dflt; + if (typeof v !== 'number' || !Number.isFinite(v) || v < lo || v > hi) { err(path, `must be a number ${lo}-${hi}`); return dflt; } + return v; + }; + const validate = { + wait_secs: num('validate.wait_secs', val.wait_secs, 5, 300, 60), + udp_secs: num('validate.udp_secs', val.udp_secs, 1, 300, 15), + min_pps: num('validate.min_pps', val.min_pps, 0, 10000, 20), + max_loss: val.max_loss === undefined ? null : num('validate.max_loss', val.max_loss, 0, 1, null), + }; + + // Steps. + let steps = doc.steps ?? STEPS; + if (!Array.isArray(steps) || steps.some((s) => !STEPS.includes(s))) { err('steps', `must be a list of ${STEPS.join(', ')}`); steps = STEPS; } + + // Defaults + nodes. + const checkProvision = (path, entry) => { + for (const [k, spec] of Object.entries(PROVISION_KEYS)) { + if (entry[k] !== undefined && entry[k] !== null && !spec.check(entry[k])) err(`${path}.${k}`, `must be ${spec.want}`); + } + }; + const defaults = doc.defaults ?? {}; + if (!obj(defaults)) err('defaults', 'must be a mapping'); + else { + for (const k of Object.keys(defaults)) if (!PROVISION_KEYS[k]) err(`defaults.${k}`, `unknown key (allowed: ${Object.keys(PROVISION_KEYS).join(', ')})`); + checkProvision('defaults', defaults); + } + const rawNodes = doc.nodes ?? []; + const nodes = []; + if (!Array.isArray(rawNodes)) err('nodes', 'must be a list'); + else { + const seen = { mac: new Map(), node_id: new Map(), tdm_slot: new Map() }; + rawNodes.forEach((n, i) => { + const path = `nodes[${i}]`; + if (!obj(n)) { err(path, 'must be a mapping'); return; } + for (const k of Object.keys(n)) if (!NODE_KEYS.has(k)) err(`${path}.${k}`, `unknown key (allowed: ${[...NODE_KEYS].join(', ')})`); + const mac = typeof n.mac === 'string' ? n.mac.toLowerCase() : null; + if (!mac || !MAC_RE.test(mac)) err(`${path}.mac`, 'required, like aa:bb:cc:dd:ee:01'); + if (!Number.isInteger(n.node_id) || n.node_id < 0 || n.node_id > 255) err(`${path}.node_id`, 'required integer 0-255'); + if (n.tdm_slot !== undefined && !(Number.isInteger(n.tdm_slot) && n.tdm_slot >= 0 && n.tdm_slot <= 255)) err(`${path}.tdm_slot`, 'must be an integer 0-255'); + checkProvision(path, n); + for (const [k, v] of [['mac', mac], ['node_id', n.node_id], ['tdm_slot', n.tdm_slot]]) { + if (v === undefined || v === null) continue; + if (seen[k].has(v)) err(`${path}.${k}`, `duplicates ${seen[k].get(v)}`); + else seen[k].set(v, path); + } + nodes.push({ ...n, mac }); + }); + } + if (doc.auto_assign !== undefined && typeof doc.auto_assign !== 'boolean') err('auto_assign', 'must be true or false'); + // Boards that are gone for good: their retained registry IDs are released. + const retired = []; + if (doc.retired !== undefined && doc.retired !== null) { + if (!Array.isArray(doc.retired)) err('retired', 'must be a list of MAC addresses'); + else doc.retired.forEach((m, i) => { + const mac = typeof m === 'string' ? m.toLowerCase() : null; + if (!mac || !MAC_RE.test(mac)) err(`retired[${i}]`, 'must be a MAC like aa:bb:cc:dd:ee:01'); + else if (nodes.some((n) => n.mac === mac)) err(`retired[${i}]`, `${mac} is also listed under nodes; a board is either active or retired`); + else if (!retired.includes(mac)) retired.push(mac); + }); + } + if (doc.registry !== undefined && typeof doc.registry !== 'string') err('registry', 'must be a path'); + + if (errors.length) { + const e = new NodeConfigError('config_invalid', `node config has ${errors.length} problem(s)`); + e.errors = errors; + throw e; + } + return { + wifi: { + ssid: wifi.ssid ?? null, + password_file: wifi.password_file ? expandHome(wifi.password_file, home) : null, + }, + aggregator: { ip, port }, + firmware: { source, bundle: fw.bundle ? expandHome(fw.bundle, home) : null, variant, allow_unverified: fw.allow_unverified === true, baud }, + ota: { psk_file: ota.psk_file ? expandHome(ota.psk_file, home) : null }, + validate, + steps, + defaults: obj(defaults) ? defaults : {}, + nodes, + retired, + auto_assign: doc.auto_assign !== false, + registry: expandHome(doc.registry, home) || DEFAULT_REGISTRY_PATH, + tdm_total: nodes.filter((n) => n.tdm_slot !== undefined).length, + }; +} + +/** Read + parse + validate a node config file. */ +export function loadNodeConfig(path = DEFAULT_CONFIG_PATH, opts = {}) { + const file = resolve(expandHome(path, opts.home)); + if (!existsSync(file)) throw new NodeConfigError('config_missing', `node config not found: ${file} (copy firmware/esp32-csi-node/nodes.example.yaml)`); + const config = normalizeNodeConfig(parseNodeYaml(readFileSync(file, 'utf8')), opts); + return { ...config, path: file }; +} + +// ─────────────────────────────────────────────────────── node resolution ── + +export function readRegistry(path) { + if (!path || !existsSync(path)) return []; + const out = []; + for (const line of readFileSync(path, 'utf8').split('\n')) { + if (!line.trim()) continue; + try { out.push(JSON.parse(line)); } catch { /* skip a torn line */ } + } + return out; +} + +/** Append one event; never pass secrets in `fields`. */ +export function appendRegistry(path, event, fields = {}, now = () => new Date()) { + mkdirSync(dirname(path), { recursive: true, mode: 0o700 }); + const rec = { ts: now().toISOString(), event }; + for (const [k, v] of Object.entries(fields)) if (v !== undefined && v !== null && v !== '') rec[k] = v; + appendFileSync(path, `${JSON.stringify(rec)}\n`, { mode: 0o600 }); + return rec; +} + +// The last valid record for a MAC is its retained assignment. Explicit YAML +// entries supersede that MAC's history, but must not reuse another board's ID. +// MACs under `retired:` are dropped, which releases their IDs. +function registryNodes(registry, retired = []) { + const gone = new Set(retired); + const nodes = []; + for (const record of registry) { + const id = typeof record?.node_id === 'number' ? record.node_id + : typeof record?.node_id === 'string' && /^[0-9]{1,3}$/.test(record.node_id) ? Number(record.node_id) : null; + if (!record || typeof record.mac !== 'string' || !MAC_RE.test(record.mac) + || !Number.isInteger(id) || id < 0 || id > 255) continue; + const mac = record.mac.toLowerCase(); + if (gone.has(mac)) continue; + nodes.push({ mac, node_id: id, source: 'registry' }); + } + return nodes; +} + +function effectiveNodes(config, registry) { + const nodes = new Map(registryNodes(registry, config.retired).map((node) => [node.mac, node])); + for (const node of config.nodes) nodes.set(node.mac, { ...node, source: 'config' }); + return [...nodes.values()]; +} + +/** + * Map a board MAC to its node entry: the config wins; otherwise the node_id + * this MAC got before (registry); otherwise the next free id if auto_assign. + */ +export function resolveNode(config, mac, registry = []) { + const m = String(mac || '').toLowerCase(); + if (!MAC_RE.test(m)) throw new NodeConfigError('invalid_mac', `not a MAC address: ${mac}`); + if ((config.retired || []).includes(m)) throw new NodeConfigError('node_retired', `MAC ${m} is listed under retired:; remove it there (and add it under nodes:) to use this board again`); + const listed = config.nodes.find((n) => n.mac === m); + let entry; let source; + if (listed) { entry = listed; source = 'config'; } + else { + const prior = effectiveNodes(config, registry).find((r) => r.mac === m); + if (prior) { entry = { mac: m, node_id: Number(prior.node_id) }; source = 'registry'; } + else if (config.auto_assign) { + const used = new Set([...config.nodes.map((n) => n.node_id), ...registryNodes(registry, config.retired).map((r) => r.node_id)]); + let id = 1; while (used.has(id) && id < 256) id += 1; + if (id > 255) throw new NodeConfigError('node_ids_exhausted', 'no free node_id left (0-255)'); + entry = { mac: m, node_id: id }; source = 'auto-assigned'; + } else { + throw new NodeConfigError('node_unlisted', `MAC ${m} is not in the node config and auto_assign is false; add it under nodes:`); + } + } + const conflict = effectiveNodes(config, registry).find((other) => other.mac !== m && other.node_id === entry.node_id); + if (conflict) throw new NodeConfigError('node_id_conflict', `node_id ${entry.node_id} is also assigned to ${conflict.mac}; give each board a unique ID in the node config, or list ${conflict.mac} under retired: if that board is gone`); + const node = { ...config.defaults, ...entry }; + if (node.tdm_slot !== undefined) node.tdm_total = config.tdm_total; + return { node, source }; +} + +/** Host IPv4 for the aggregator: the configured one, or the first private LAN address. */ +export function resolveTargetIp(config, nets = networkInterfaces()) { + if (config.aggregator.ip !== 'auto') return { ip: config.aggregator.ip, candidates: [config.aggregator.ip] }; + const candidates = Object.values(nets).flat() + .filter((a) => a && (a.family === 'IPv4' || a.family === 4) && !a.internal && /^(10\.|192\.168\.|172\.(1[6-9]|2\d|3[01])\.)/.test(a.address)) + .map((a) => a.address); + if (!candidates.length) throw new NodeConfigError('no_lan_ip', 'no private IPv4 address on this host; set aggregator.ip'); + return { ip: candidates[0], candidates }; +} + +/** provision.py argv (no secrets — only file paths). */ +export function provisionArgs(config, node, { port, targetIp, passwordFile }) { + validatePort(port); + if (!config.wifi.ssid) throw new NodeConfigError('ssid_missing', 'wifi.ssid is required to provision'); + if (!passwordFile) throw new NodeConfigError('password_missing', 'wifi.password_file is required to provision'); + const chip = FIRMWARE_VARIANTS[config.firmware.variant].chip; + const argv = ['--port', port, '--chip', chip, '--ssid', config.wifi.ssid, '--password-file', passwordFile, + '--target-ip', targetIp, '--target-port', String(config.aggregator.port), '--node-id', String(node.node_id)]; + if (node.tdm_slot !== undefined) argv.push('--tdm-slot', String(node.tdm_slot), '--tdm-total', String(node.tdm_total)); + for (const [k, spec] of Object.entries(PROVISION_KEYS)) { + if (node[k] === undefined || node[k] === null) continue; + argv.push(spec.flag, spec.format ? spec.format(node[k]) : String(node[k])); + } + if (config.ota.psk_file) argv.push('--ota-psk-file', config.ota.psk_file); + return argv; +} + +/** Owner-only regular file check for secret files (POSIX). */ +function checkSecretFile(path, what) { + if (!existsSync(path)) throw new NodeConfigError('secret_missing', `${what} not found: ${path}`); + const st = lstatSync(path); + if (!st.isFile()) throw new NodeConfigError('secret_untrusted', `${what} must be a regular file (not a symlink): ${path}`); + if (process.platform !== 'win32' && (st.mode & 0o077)) throw new NodeConfigError('secret_permissions', `${what} must be owner-only (chmod 600): ${path}`); +} + +/** The WiFi password file path, after an owner-only regular-file check. Never read. */ +export function passwordFile(config) { + if (!config.wifi.password_file) throw new NodeConfigError('password_missing', 'configure wifi.password_file (chmod 600)'); + checkSecretFile(config.wifi.password_file, 'wifi.password_file'); + return config.wifi.password_file; +} + +/** + * Firmware images for the configured source. `reuse`/`auto` use the repo's + * ESP-IDF build/ (a local build you produced → unverified by design); + * `bundle` requires SHA256SUMS unless allow_unverified. + */ +export function firmwareSource(config, repoRoot) { + const { source, variant } = config.firmware; + if (source === 'bundle') return { dir: config.firmware.bundle, allowUnverified: config.firmware.allow_unverified, kind: 'bundle' }; + if (source === 'build') { + throw new NodeConfigError('build_unsupported', '`ruview node` does not build firmware. Build with Docker (firmware/esp32-csi-node/README.md "Building"), then use source: reuse'); + } + if (!repoRoot) throw new NodeConfigError('repo_missing', 'source reuse/auto needs a RuView checkout (run inside the repo) or use source: bundle'); + const dir = join(repoRoot, 'firmware', 'esp32-csi-node', 'build'); + if (!existsSync(join(dir, 'esp32-csi-node.bin'))) { + throw new NodeConfigError('build_missing', `no firmware build at ${dir}; build it first (see firmware/esp32-csi-node/README.md) or use source: bundle`); + } + const sdk = join(repoRoot, 'firmware', 'esp32-csi-node', 'sdkconfig'); + const want = FIRMWARE_VARIANTS[variant].chip; + if (existsSync(sdk) && !readFileSync(sdk, 'utf8').includes(`CONFIG_IDF_TARGET="${want}"`)) { + throw new NodeConfigError('build_target_mismatch', `build/ was not built for ${want} (sdkconfig CONFIG_IDF_TARGET); rebuild or fix firmware.variant`); + } + const app = statSync(join(dir, 'esp32-csi-node.bin')).mtimeMs; + const stale = ['main', 'components', 'CMakeLists.txt', 'sdkconfig.defaults'].some((p) => newerThan(join(repoRoot, 'firmware', 'esp32-csi-node', p), app)); + if (stale && source === 'auto') { + throw new NodeConfigError('build_stale', 'firmware sources changed after the last build; rebuild, or set source: reuse to flash the existing build anyway'); + } + return { dir, allowUnverified: true, kind: 'local-build', stale }; +} + +function newerThan(path, t, depth = 0) { + if (!existsSync(path) || depth > 6) return false; + const st = statSync(path); + if (st.isFile()) return st.mtimeMs > t; + if (!st.isDirectory()) return false; + for (const name of readdirSync(path)) if (newerThan(join(path, name), t, depth + 1)) return true; + return false; +} + +// ───────────────────────────────────────────────────────────── flows ────── + +const ESPTOOL_MAC_RE = /MAC:\s*((?:[0-9a-f]{2}:){5}[0-9a-f]{2})/i; + +/** Plan only: validate the config, resolve every listed node, never touch hardware. */ +export function planNodes(config, { repoRoot, nets } = {}) { + const warnings = []; + let target = null; + try { target = resolveTargetIp(config, nets); if (target.candidates.length > 1) warnings.push(`aggregator.ip auto picked ${target.ip} of ${target.candidates.join(', ')}; set it explicitly if wrong`); } catch (e) { warnings.push(e.message); } + let firmware = null; + try { + const src = firmwareSource(config, repoRoot); + const bundle = resolveBundle(src.dir, config.firmware.variant, { requireChecksums: !src.allowUnverified }); + firmware = { source: config.firmware.source, kind: src.kind, dir: bundle.dir, variant: bundle.variant, chip: bundle.chipName, flashSize: bundle.flashSize, images: bundle.images.map((i) => ({ role: i.role, offset: i.offset, file: i.relative, integrity: i.integrity })) }; + if (src.stale) warnings.push('build/ is older than the firmware sources (source: reuse flashes it anyway)'); + } catch (e) { + if (!(e instanceof NodeConfigError || e instanceof FirmwareError)) throw e; + firmware = { source: config.firmware.source, error: e.reason, detail: e.message }; + } + const secrets = { + wifi_password: config.wifi.password_file ? `file ${config.wifi.password_file}` : 'not configured', + ota_psk: config.ota.psk_file ? `file ${config.ota.psk_file}` : 'not configured — remote OTA and config_push stay disabled', + }; + if (!config.ota.psk_file) warnings.push('no ota.psk_file: provisioning disables OTA updates and remote config_push on these nodes'); + // Resolve against the same registry `apply` uses, so plan reports what apply + // would refuse (e.g. node_id_conflict) instead of passing and failing later. + const registry = readRegistry(config.registry); + const conflicts = []; + const nodes = config.nodes.map((n) => { + try { + const { node } = resolveNode(config, n.mac, registry); + return { mac: n.mac, node_id: node.node_id, zone: node.zone ?? null, tdm: node.tdm_slot !== undefined ? `${node.tdm_slot}/${node.tdm_total}` : null, edge_tier: node.edge_tier ?? null }; + } catch (e) { + if (!(e instanceof NodeConfigError)) throw e; + conflicts.push({ mac: n.mac, node_id: n.node_id, reason: e.reason, detail: e.message }); + return { mac: n.mac, node_id: n.node_id, zone: n.zone ?? null, tdm: null, edge_tier: null, error: e.reason }; + } + }); + return { + ok: !firmware.error && conflicts.length === 0, + config: config.path, + ssid: config.wifi.ssid, + target: target ? `${target.ip}:${config.aggregator.port}` : null, + steps: config.steps, + secrets, + firmware, + validate: config.validate, + auto_assign: config.auto_assign, + registry: config.registry, + nodes, + retired: config.retired, + conflicts, + warnings, + }; +} + +/** Wait until `nodeId` shows up in short captures, then measure it once. */ +async function measureNode(config, nodeId, deps) { + const deadline = Date.now() + config.validate.wait_secs * 1000; + let seen = null; + while (Date.now() < deadline) { + const probe = await deps.capture({ udp_port: config.aggregator.port, seconds: 3 }); + if (probe.reason === 'port_in_use') return { ok: false, reason: 'port_in_use', detail: probe.detail }; + seen = (probe.nodes || []).find((n) => n.nodeId === nodeId && (n.packets?.csi || 0) > 0); + if (seen) break; + } + if (!seen) return { ok: false, reason: 'node_silent', detail: `node ${nodeId} sent no CSI to UDP ${config.aggregator.port} within ${config.validate.wait_secs}s` }; + const res = await deps.capture({ udp_port: config.aggregator.port, seconds: config.validate.udp_secs }); + const n = (res.nodes || []).find((x) => x.nodeId === nodeId); + if (!n) return { ok: false, reason: 'node_silent', detail: `node ${nodeId} stopped sending during the ${config.validate.udp_secs}s measurement` }; + return judgeNode(config, n); +} + +function judgeNode(config, n) { + const problems = []; + if (n.csiRateHz < config.validate.min_pps) problems.push(`CSI ${n.csiRateHz} Hz < ${config.validate.min_pps}`); + if (config.validate.max_loss !== null && n.csiLossFraction !== null && n.csiLossFraction > config.validate.max_loss) { + problems.push(`loss ${(n.csiLossFraction * 100).toFixed(1)}% > ${(config.validate.max_loss * 100).toFixed(1)}%`); + } + return { + ok: problems.length === 0, + reason: problems.length ? 'below_threshold' : undefined, + problems, + address: n.address ?? null, + csiRateHz: n.csiRateHz, + csiLossFraction: n.csiLossFraction, + rssiMean: n.rssiMean, + freqMhz: n.csi?.freqMhz ?? null, + evidence: 'MEASURED: live UDP CSI from this node decoded on this host', + }; +} + +/** + * apply (config steps: flash → provision → validate) or firmware-only + * (flash → validate) to one board. Plans only unless `confirm`. deps = { exec, python, capture, repoRoot, now, nets, log }. + */ +export async function applyBoard(config, { port, flow = 'apply', confirm = false, expectedIdentity }, deps) { + validatePort(port); + const steps = new Set(flow === 'firmware' ? ['flash', 'validate'] : config.steps); + const registry = readRegistry(config.registry); + const python = deps.python(); + if (!python) return { ok: false, reason: 'python_missing', remedy: 'Install Python 3 and `pip install esptool pyserial`.' }; + + const log = deps.log || (() => {}); + // Identify the board (read-only). + log(`reading MAC on ${port}`); + const macProbe = await deps.exec(python, ['-m', 'esptool', '--port', port, 'read_mac'], { timeoutMs: 60_000 }); + const mac = (ESPTOOL_MAC_RE.exec(`${macProbe.stdout}\n${macProbe.stderr}`) || [])[1]?.toLowerCase(); + if (!mac) return { ok: false, reason: 'mac_probe_failed', port, detail: (macProbe.stderr || macProbe.error || '').slice(-600), remedy: 'Check the cable/port, close serial monitors, or hold BOOT while pressing RESET.' }; + let resolved; + try { resolved = resolveNode(config, mac, registry); } catch (e) { if (e instanceof NodeConfigError) return { ok: false, reason: e.reason, port, mac, detail: e.message }; throw e; } + const { node, source } = resolved; + const result = { ok: false, flow, port, mac, node_id: node.node_id, zone: node.zone ?? null, node_source: source, steps: [...steps], stages: {} }; + if (expectedIdentity !== undefined && (expectedIdentity?.mac !== mac || expectedIdentity?.nodeId !== node.node_id)) { + return { ...result, reason: 'confirmation_changed', detail: 'The selected board or node assignment changed. Read and confirm a new plan before applying.' }; + } + if (source === 'auto-assigned' && confirm) appendRegistry(config.registry, 'node_assigned', { mac, node_id: node.node_id }, deps.now); + + let target; + try { target = resolveTargetIp(config, deps.nets).ip; } catch (e) { return { ...result, reason: e.reason, detail: e.message }; } + + // Plan. + let fwSrc = null; + if (steps.has('flash')) { + try { fwSrc = firmwareSource(config, deps.repoRoot); } catch (e) { if (e instanceof NodeConfigError) return { ...result, reason: e.reason, detail: e.message }; throw e; } + } + const provisionScript = deps.repoRoot ? join(deps.repoRoot, 'firmware', 'esp32-csi-node', 'provision.py') : null; + if (steps.has('wifi') && !(provisionScript && existsSync(provisionScript))) { + return { ...result, reason: 'provision_missing', detail: 'provision.py not found; run inside a RuView checkout' }; + } + + if (!confirm) { + const pw = config.wifi.password_file; + const plan = { target: `${target}:${config.aggregator.port}` }; + if (fwSrc) { + const fwPlan = await flashFirmware({ port, bundle: fwSrc.dir, variant: config.firmware.variant, baud: config.firmware.baud, allow_unverified: fwSrc.allowUnverified }, deps); + plan.flash = fwPlan.ok ? fwPlan.plan : { error: fwPlan.reason, detail: fwPlan.detail }; + } + if (steps.has('wifi')) { + try { plan.provision = ['python', 'provision.py', ...provisionArgs(config, node, { port, targetIp: target, passwordFile: pw || '' })]; } catch (e) { plan.provision = { error: e.reason, detail: e.message }; } + } + if (steps.has('validate')) plan.validate = { udp_port: config.aggregator.port, ...config.validate }; + return { ...result, ok: true, dryRun: true, plan, next: 'Re-run with --confirm (or answer yes at a terminal) to write. Nothing was written.' }; + } + + const reg = (event, fields) => appendRegistry(config.registry, event, { flow, port, mac, node_id: node.node_id, zone: node.zone, ...fields }, deps.now); + reg('chip_probed', { node_src: source }); + + // Flash (NVS preserved by the image set). + if (steps.has('flash')) { + log(`flashing ${fwSrc.kind} (${config.firmware.variant}) and capturing the boot log — ~1 min`); + const fw = await flashFirmware({ port, bundle: fwSrc.dir, variant: config.firmware.variant, baud: config.firmware.baud, allow_unverified: fwSrc.allowUnverified, confirm: true, boot_log_seconds: 15 }, deps); + result.stages.flash = { ok: fw.ok, reason: fw.reason, firmwareVersion: fw.bootLog?.firmwareVersion ?? null, panic: fw.bootLog?.panic ?? null, detail: fw.detail }; + if (!fw.ok || fw.bootLog?.panic) { + reg('flash_failed', { reason: fw.reason || 'boot_panic' }); + return { ...result, reason: fw.reason || 'boot_panic', detail: fw.detail || 'boot log shows a panic' }; + } + reg('flashed', { fw_version: fw.bootLog?.firmwareVersion, variant: config.firmware.variant, firmware_kind: fwSrc.kind }); + } + + // Provision NVS. + if (steps.has('wifi')) { + log(`provisioning WiFi "${config.wifi.ssid}" → ${target}:${config.aggregator.port}, node ${node.node_id}`); + let pw; + try { + pw = passwordFile(config); + if (config.ota.psk_file) checkSecretFile(config.ota.psk_file, 'ota.psk_file'); + } catch (e) { if (e instanceof NodeConfigError) return { ...result, reason: e.reason, detail: e.message }; throw e; } + const run = await deps.exec(python, [provisionScript, ...provisionArgs(config, node, { port, targetIp: target, passwordFile: pw })], { timeoutMs: 180_000 }); + result.stages.wifi = { ok: run.ok, target: `${target}:${config.aggregator.port}`, ota: Boolean(config.ota.psk_file) }; + if (!run.ok) { + reg('provision_failed', {}); + return { ...result, reason: 'provision_failed', detail: `${run.stdout}\n${run.stderr}`.slice(-800) }; + } + reg('provisioned', { target: `${target}:${config.aggregator.port}`, ota: config.ota.psk_file ? 'enabled' : 'disabled' }); + } + + // Validate on the network. + if (steps.has('validate')) { + log(`waiting up to ${config.validate.wait_secs}s for node ${node.node_id} on UDP ${config.aggregator.port}, then measuring ${config.validate.udp_secs}s`); + const v = await measureNode(config, node.node_id, deps); + result.stages.validate = v; + reg('validated', { ip: v.address, csi_pps: v.csiRateHz, csi_loss: v.csiLossFraction, rssi: v.rssiMean, health: v.ok ? 'healthy' : (v.reason || 'degraded') }); + if (!v.ok) return { ...result, reason: v.reason, detail: v.detail || v.problems?.join('; ') }; + } + return { ...result, ok: true }; +} + +/** Read-only node health: one UDP capture + each node's :8032/ota/status. */ +export async function nodeStatus(config, deps) { + const cap = await deps.capture({ udp_port: config.aggregator.port, seconds: config.validate.udp_secs }); + if (cap.reason === 'port_in_use' || cap.reason === 'invalid_port') return { ok: false, reason: cap.reason, detail: cap.detail, remedy: cap.remedy }; + const heard = new Map((cap.nodes || []).filter((n) => n.source === 'esp32').map((n) => [n.nodeId, n])); + const registry = readRegistry(config.registry); + const lastIp = new Map(); + for (const r of registry) if (r?.ip && r.node_id !== undefined) lastIp.set(Number(r.node_id), r.ip); + // Nodes `node apply` auto-assigned are known too (registry), even if not in the YAML. + const assigned = effectiveNodes(config, registry); + const known = (id) => assigned.find((node) => node.node_id === id); + const ids = new Set([...assigned.map((node) => node.node_id), ...heard.keys()]); + const rows = []; + for (const id of [...ids].sort((a, b) => a - b)) { + const listed = known(id); + const conflict = assigned.filter((node) => node.node_id === id).length > 1; + const n = heard.get(id); + const address = n?.address || lastIp.get(id) || null; + let http = null; + if (address && /^(10\.|192\.168\.|172\.(1[6-9]|2\d|3[01])\.|169\.254\.)/.test(address)) { + try { + const r = await deps.fetch(`http://${address}:8032/ota/status`, { signal: AbortSignal.timeout(2500) }); + const j = r.ok ? await r.json() : null; + http = j ? { version: j.version ?? null, ota_state: j.ota_state ?? null } : { error: `HTTP ${r.status}` }; + } catch { http = { error: 'unreachable' }; } + } + const judged = n ? judgeNode(config, n) : null; + const health = conflict ? 'id-conflict' : !listed ? 'unlisted' : judged?.ok ? 'healthy' : n ? 'degraded' : http && !http.error ? 'no-csi-here' : 'offline'; + rows.push({ + node_id: id, zone: conflict ? null : listed?.zone ?? null, mac: conflict ? null : listed?.mac ?? null, source: conflict ? null : listed ? (listed.source || 'config') : null, address, + firmware: http?.version ?? null, ota_state: http?.ota_state ?? null, http: http ? (http.error || 'ok') : null, + csiRateHz: n?.csiRateHz ?? 0, csiLossFraction: n?.csiLossFraction ?? null, rssiMean: n?.rssiMean ?? null, + health, problems: conflict ? ['Multiple boards have this node ID; reconcile their config/registry assignments before applying.'] : judged?.problems ?? [], + }); + } + const bad = rows.filter((r) => !['healthy', 'unlisted'].includes(r.health)); + return { + ok: bad.length === 0, + listen: cap.listen, seconds: cap.seconds, nodes: rows, + summary: `${rows.filter((r) => r.health === 'healthy').length}/${rows.filter((r) => r.health !== 'unlisted').length} known nodes healthy (min ${config.validate.min_pps} Hz${config.validate.max_loss !== null ? `, max loss ${config.validate.max_loss * 100}%` : ''})`, + notes: ['no-csi-here = node answers HTTP but streams to another host', 'unlisted = node heard on UDP but neither in the node config nor assigned by `node apply`'], + evidence: cap.packets ? 'MEASURED: live UDP CSI decoded on this host' : null, + }; +} + +/** Terminal rendering (JSON is the default for pipes). */ +export function formatNodes(kind, res) { + const lines = []; + const table = (rows, cols) => { + const w = cols.map(([h, f]) => Math.max(h.length, ...rows.map((r) => String(f(r) ?? '-').length))); + lines.push(` ${cols.map(([h], i) => h.padEnd(w[i])).join(' ')}`); + for (const r of rows) lines.push(` ${cols.map(([, f], i) => String(f(r) ?? '-').padEnd(w[i])).join(' ')}`); + }; + if (!res.ok && res.reason && !res.nodes) { + lines.push(`✘ ${res.reason}${res.detail ? `: ${res.detail}` : ''}`); + for (const e of res.errors || []) lines.push(` - ${e}`); + if (res.remedy) lines.push(` → ${res.remedy}`); + return lines.join('\n'); + } + if (kind === 'plan') { + lines.push(`Node plan — ${res.config}`, ` WiFi: ${res.ssid ?? '(none)'} · target ${res.target ?? '?'} · steps ${res.steps.join(',')}`); + lines.push(` Secrets: wifi ${res.secrets.wifi_password}; ota ${res.secrets.ota_psk}`); + lines.push(res.firmware.error ? ` Firmware: ✘ ${res.firmware.error} — ${res.firmware.detail}` : ` Firmware: ${res.firmware.kind} ${res.firmware.dir} (${res.firmware.chip} ${res.firmware.flashSize}; ${res.firmware.images.map((i) => `${i.role}:${i.integrity}`).join(', ')})`); + lines.push(` Validate: wait ${res.validate.wait_secs}s, measure ${res.validate.udp_secs}s, min ${res.validate.min_pps} Hz${res.validate.max_loss !== null ? `, max loss ${res.validate.max_loss}` : ''}`, ''); + table(res.nodes, [['NODE', (r) => r.node_id], ['MAC', (r) => r.mac], ['ZONE', (r) => r.zone], ['TDM', (r) => r.tdm], ['TIER', (r) => r.edge_tier], ['STATE', (r) => r.error || 'ok']]); + for (const c of res.conflicts || []) lines.push(` ✘ ${c.mac}: ${c.detail}`); + if (res.retired?.length) lines.push(` retired (IDs released): ${res.retired.join(', ')}`); + for (const w of res.warnings) lines.push(` ! ${w}`); + } else if (kind === 'status') { + table(res.nodes, [['NODE', (r) => r.node_id], ['ZONE', (r) => r.zone], ['MAC', (r) => r.mac], ['IP', (r) => r.address], ['FW', (r) => r.firmware], ['HTTP', (r) => r.http], ['CSI Hz', (r) => r.csiRateHz], ['LOSS', (r) => (r.csiLossFraction === null ? null : `${(r.csiLossFraction * 100).toFixed(1)}%`)], ['RSSI', (r) => r.rssiMean], ['HEALTH', (r) => r.health]]); + lines.push('', ` ${res.summary}`); + for (const r of res.nodes) for (const p of r.problems) lines.push(` ! node ${r.node_id}: ${p}`); + } else { + lines.push(`${res.ok ? '✔' : '✘'} ${res.flow} ${res.port} · MAC ${res.mac ?? '?'} · node ${res.node_id ?? '?'}${res.zone ? ` (${res.zone})` : ''} [${res.node_source ?? '-'}]`); + if (res.dryRun) { lines.push(JSON.stringify(res.plan, null, 2).split('\n').map((l) => ` ${l}`).join('\n')); if (res.next) lines.push(` → ${res.next}`); return lines.join('\n'); } + for (const [stage, s] of Object.entries(res.stages || {})) { + const extra = stage === 'flash' ? `fw ${s.firmwareVersion ?? '?'}` : stage === 'wifi' ? `→ ${s.target} (OTA ${s.ota ? 'on' : 'off'})` : `${s.csiRateHz ?? 0} Hz, loss ${s.csiLossFraction === null || s.csiLossFraction === undefined ? '?' : `${(s.csiLossFraction * 100).toFixed(1)}%`}, RSSI ${s.rssiMean ?? '?'}, ip ${s.address ?? '?'}`; + lines.push(` ${s.ok ? '✔' : '✘'} ${stage}: ${extra}`); + } + if (!res.ok) lines.push(` ✘ ${res.reason}${res.detail ? `: ${res.detail}` : ''}`); + } + return lines.join('\n'); +} diff --git a/harness/ruview/test/node-config.test.mjs b/harness/ruview/test/node-config.test.mjs new file mode 100644 index 0000000000..0693aa0bf9 --- /dev/null +++ b/harness/ruview/test/node-config.test.mjs @@ -0,0 +1,455 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, utimesSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { + NodeConfigError, firmwareSource, nodeStatus, normalizeNodeConfig, parseNodeYaml, passwordFile, planNodes, + provisionArgs, readRegistry, resolveNode, resolveTargetIp, applyBoard, +} from '../src/node-config.js'; + +const EXAMPLE = fileURLToPath(new URL('../../../firmware/esp32-csi-node/nodes.example.yaml', import.meta.url)); +const NETS = { en0: [{ family: 'IPv4', internal: false, address: '192.168.1.20' }] }; +const MAC = 'aa:bb:cc:dd:ee:01'; + +// Never read the operator's real ~/.ruview registry from tests. +const NO_REGISTRY = join(tmpdir(), `ruview-node-test-no-registry-${process.pid}.jsonl`); +const base = (extra = {}) => ({ + wifi: { ssid: 'Lab-2G', password_file: '/tmp/none' }, + registry: NO_REGISTRY, + nodes: [{ mac: MAC, node_id: 1, zone: 'living-room', tdm_slot: 0 }, { mac: 'aa:bb:cc:dd:ee:02', node_id: 2, tdm_slot: 1 }], + ...extra, +}); +const cfg = (extra) => normalizeNodeConfig(base(extra)); +const problems = (doc) => { try { normalizeNodeConfig(doc); return []; } catch (e) { assert.ok(e instanceof NodeConfigError); return e.errors || [e.message]; } }; + +// ─── YAML subset ─────────────────────────────────────────────────────────── + +test('YAML subset: maps, lists of maps, flow lists, scalars, comments', () => { + const doc = parseNodeYaml(` +# comment +wifi: + ssid: "Lab # 2G" # hash inside quotes is data + password_file: ~/.ruview/wifi-pass +steps: [device, flash, validate] +validate: + min_pps: 20 + max_loss: 0.25 +auto_assign: false +nodes: + - mac: "AA:BB:CC:DD:EE:01" + node_id: 1 + zone: 'living room' + - mac: aa:bb:cc:dd:ee:02 + node_id: 2 +empty: +`); + assert.equal(doc.wifi.ssid, 'Lab # 2G'); + assert.deepEqual(doc.steps, ['device', 'flash', 'validate']); + assert.equal(doc.validate.max_loss, 0.25); + assert.equal(doc.auto_assign, false); + assert.equal(doc.nodes.length, 2); + assert.deepEqual(doc.nodes[0], { mac: 'AA:BB:CC:DD:EE:01', node_id: 1, zone: 'living room' }); + assert.equal(doc.nodes[1].mac, 'aa:bb:cc:dd:ee:02'); + assert.equal(doc.empty, null); +}); + +test('YAML subset fails closed on unsupported or ambiguous syntax', () => { + const bad = { + tabs: 'wifi:\n\tssid: x', + anchor: 'a: &x 1', + alias: 'a: *x', + tag: 'a: !!str 1', + block_scalar: 'a: |\n text', + flow_map: 'a: {b: 1}', + duplicate: 'a: 1\na: 2', + proto: '__proto__: 1', + bad_indent: 'a:\n b: 1\n c: 2', + unterminated: 'a: "x', + top_list: '- a\n- b', + }; + for (const [name, text] of Object.entries(bad)) assert.throws(() => parseNodeYaml(text), NodeConfigError, name); +}); + +test('the shipped nodes.example.yaml parses and validates', () => { + const config = normalizeNodeConfig(parseNodeYaml(readFileSync(EXAMPLE, 'utf8'))); + assert.ok(config.nodes.length >= 1); + assert.equal(config.firmware.source, 'auto'); +}); + +// ─── config validation ───────────────────────────────────────────────────── + +test('config: defaults are filled and paths expand ~', () => { + const c = normalizeNodeConfig({ wifi: { ssid: 'x', password_file: '~/p' }, nodes: [] }, { home: '/home/u' }); + assert.equal(c.wifi.password_file, join('/home/u', 'p')); + assert.deepEqual(c.aggregator, { ip: 'auto', port: 5005 }); + assert.equal(c.firmware.variant, 's3-8mb'); + assert.deepEqual(c.validate, { wait_secs: 60, udp_secs: 15, min_pps: 20, max_loss: null }); + assert.equal(c.auto_assign, true); +}); + +test('config: inline passwords, duplicates, bad MACs and unknown keys are all reported', () => { + const errs = problems({ + wifi: { ssid: 'x', password: 'hunter2' }, + color: 'blue', + firmware: { erase: true, variant: 's2' }, + nodes: [{ mac: MAC, node_id: 1, tdm_slot: 0 }, { mac: MAC.toUpperCase(), node_id: 1, tdm_slot: 0 }, { mac: 'nope', node_id: 300, flavor: 1 }], + }); + const text = errs.join('\n'); + for (const want of ['wifi.password', 'color: unknown key', 'firmware.erase', 'firmware.variant', 'nodes[1].mac: duplicates', 'nodes[1].node_id: duplicates', 'nodes[1].tdm_slot: duplicates', 'nodes[2].mac', 'nodes[2].node_id', 'nodes[2].flavor']) { + assert.ok(text.includes(want), `missing "${want}" in:\n${text}`); + } + assert.ok(!text.includes('hunter2'), 'secret value must never appear in errors'); +}); + +test('config: provisioning values are range-checked', () => { + const text = problems(base({ defaults: { edge_tier: 3, channel: 15, hop_channels: [], zone: 'x;rm -rf /' } })).join('\n'); + for (const k of ['defaults.edge_tier', 'defaults.channel', 'defaults.hop_channels', 'defaults.zone']) assert.ok(text.includes(k), k); +}); + +// ─── node resolution + provisioning argv ─────────────────────────────────── + +test('resolveNode: config first, then registry, then next free id', () => { + const c = cfg({ defaults: { edge_tier: 2 } }); + const listed = resolveNode(c, MAC.toUpperCase()); + assert.equal(listed.source, 'config'); + assert.deepEqual([listed.node.node_id, listed.node.tdm_slot, listed.node.tdm_total, listed.node.edge_tier], [1, 0, 2, 2]); + const prior = resolveNode(c, '11:22:33:44:55:66', [{ mac: '11:22:33:44:55:66', node_id: 7 }]); + assert.deepEqual([prior.source, prior.node.node_id], ['registry', 7]); + const fresh = resolveNode(c, '11:22:33:44:55:77', [{ mac: '11:22:33:44:55:66', node_id: 3 }]); + assert.deepEqual([fresh.source, fresh.node.node_id], ['auto-assigned', 4]); + assert.equal(fresh.node.tdm_total, undefined, 'no tdm_total without a slot'); + assert.throws(() => resolveNode(cfg({ auto_assign: false }), '11:22:33:44:55:77'), /not in the node config/); +}); + +test('provisionArgs carries paths, never secret values', () => { + const c = cfg({ ota: { psk_file: '/keys/ota' }, defaults: { hop_channels: [1, 6, 11] } }); + const { node } = resolveNode(c, MAC); + const argv = provisionArgs(c, node, { port: '/dev/cu.usbmodem1', targetIp: '192.168.1.20', passwordFile: '/run/pw' }); + const at = (flag) => argv[argv.indexOf(flag) + 1]; + assert.equal(at('--password-file'), '/run/pw'); + assert.equal(at('--ota-psk-file'), '/keys/ota'); + assert.equal(at('--hop-channels'), '1,6,11'); + assert.equal(at('--tdm-total'), '2'); + assert.ok(!argv.includes('--password')); + assert.throws(() => provisionArgs(c, node, { port: 'COM7;x', targetIp: '1.2.3.4', passwordFile: '/p' }), /port must look like/); +}); + +test('passwordFile: path only, owner-only regular files; env sources are refused', () => { + assert.ok(problems({ wifi: { ssid: 'x', password_env: 'RV_PW' } }).join('\n').includes('credentials never pass through the harness')); + const dir = mkdtempSync(join(tmpdir(), 'rv-pw-')); + try { + const f = join(dir, 'pw'); writeFileSync(f, 'x'); chmodSync(f, 0o644); + const loose = normalizeNodeConfig({ wifi: { ssid: 'x', password_file: f } }); + if (process.platform !== 'win32') assert.throws(() => passwordFile(loose), /owner-only/); + chmodSync(f, 0o600); + assert.equal(passwordFile(loose), f); + assert.throws(() => passwordFile(normalizeNodeConfig({ wifi: { ssid: 'x' } })), /password_file/); + } finally { rmSync(dir, { recursive: true, force: true }); } +}); + +test('resolveTargetIp picks a private LAN address or the configured one', () => { + assert.equal(resolveTargetIp(cfg(), { lo: [{ family: 'IPv4', internal: true, address: '127.0.0.1' }], ...NETS }).ip, '192.168.1.20'); + assert.equal(resolveTargetIp(cfg({ aggregator: { ip: '10.0.0.5' } }), {}).ip, '10.0.0.5'); + assert.throws(() => resolveTargetIp(cfg(), { en0: [{ family: 'IPv4', internal: false, address: '8.8.8.8' }] }), /no private IPv4/); +}); + +// ─── firmware source ─────────────────────────────────────────────────────── + +function fakeRepo({ target = 'esp32s3', staleSource = false } = {}) { + const root = mkdtempSync(join(tmpdir(), 'rv-repo-')); + const fw = join(root, 'firmware', 'esp32-csi-node'); + const b = join(fw, 'build'); + mkdirSync(join(b, 'bootloader'), { recursive: true }); + mkdirSync(join(b, 'partition_table'), { recursive: true }); + mkdirSync(join(fw, 'main'), { recursive: true }); + writeFileSync(join(fw, 'main', 'main.c'), 'int x;'); + writeFileSync(join(fw, 'provision.py'), '# fake'); + writeFileSync(join(fw, 'sdkconfig'), `CONFIG_IDF_TARGET="${target}"\n`); + const old = new Date(Date.now() - 60_000); + utimesSync(join(fw, 'main', 'main.c'), old, old); + for (const [f, body] of [['bootloader/bootloader.bin', 'B'], ['partition_table/partition-table.bin', 'P'], ['ota_data_initial.bin', 'O'], ['esp32-csi-node.bin', 'A']]) writeFileSync(join(b, f), body); + if (staleSource) { const later = new Date(Date.now() + 60_000); utimesSync(join(fw, 'main', 'main.c'), later, later); } + return root; +} + +test('firmwareSource: reuse local build, refuse stale auto, wrong target, and build', () => { + const root = fakeRepo(); + const stale = fakeRepo({ staleSource: true }); + const wrong = fakeRepo({ target: 'esp32c6' }); + try { + assert.equal(firmwareSource(cfg(), root).kind, 'local-build'); + assert.throws(() => firmwareSource(cfg(), stale), /changed after the last build/); + assert.equal(firmwareSource(cfg({ firmware: { source: 'reuse' } }), stale).stale, true); + assert.throws(() => firmwareSource(cfg(), wrong), /not built for esp32s3/); + assert.throws(() => firmwareSource(cfg({ firmware: { source: 'build' } }), root), /does not build firmware/); + assert.throws(() => firmwareSource(cfg(), null), /RuView checkout/); + } finally { for (const d of [root, stale, wrong]) rmSync(d, { recursive: true, force: true }); } +}); + +// ─── flows with fake hardware ────────────────────────────────────────────── + +function fakeDeps({ repoRoot, macOut = `MAC: ${MAC}`, provisionOk = true, rate = 30, loss = 0.01, silent = false } = {}) { + const calls = []; + return { + calls, + repoRoot, + nets: NETS, + now: () => new Date('2026-11-12T00:00:00Z'), + python: () => '/usr/bin/python3', + exec: async (cmd, args) => { + calls.push(args); + if (args.includes('read_mac')) return { ok: true, stdout: macOut, stderr: '' }; + if (args.includes('chip_id')) return { ok: true, stdout: 'Chip type: ESP32-S3 (QFN56)', stderr: '' }; + if (args.includes('write_flash')) return { ok: true, stdout: 'Hash of data verified.', stderr: '' }; + if (args[0] === '-c') return { ok: true, stdout: 'I (1) main: esp32-csi-node v0.8.15\nI (2) csi_collector: CSI cb #1\n', stderr: '' }; + if (String(args[0]).endsWith('provision.py')) return { ok: provisionOk, stdout: provisionOk ? 'ok' : '', stderr: provisionOk ? '' : 'NVS write failed' }; + return { ok: false, stdout: '', stderr: 'unexpected' }; + }, + capture: async () => (silent + ? { ok: false, reason: 'no_packets', nodes: [] } + : { ok: true, packets: 400, listen: '0.0.0.0:5005', seconds: 15, nodes: [{ source: 'esp32', nodeId: 1, address: '192.168.1.40', packets: { csi: 450 }, csiRateHz: rate, csiLossFraction: loss, rssiMean: -50, csi: { freqMhz: 2462 } }] }), + fetch: async () => ({ ok: true, json: async () => ({ version: '0.8.15', ota_state: 'valid' }) }), + }; +} + +function withSecrets(extra = {}) { + const dir = mkdtempSync(join(tmpdir(), 'rv-sec-')); + const pw = join(dir, 'pw'); writeFileSync(pw, 'pw'); chmodSync(pw, 0o600); + const reg = join(dir, 'devices.jsonl'); + return { dir, reg, config: cfg({ wifi: { ssid: 'Lab-2G', password_file: pw }, registry: reg, ...extra }) }; +} + +test('apply without --confirm reads the MAC, plans, and writes nothing', async () => { + const repo = fakeRepo(); const s = withSecrets(); + try { + const deps = fakeDeps({ repoRoot: repo }); + const res = await applyBoard(s.config, { port: '/dev/cu.usbmodem1' }, deps); + assert.equal(res.ok, true); assert.equal(res.dryRun, true); + assert.deepEqual([res.mac, res.node_id, res.node_source], [MAC, 1, 'config']); + assert.ok(res.plan.provision.includes('--password-file')); + assert.ok(!deps.calls.some((a) => a.includes('write_flash') || String(a[0]).endsWith('provision.py')), 'no writes in a plan'); + assert.equal(existsSync(s.reg), false, 'no registry writes in a plan'); + } finally { rmSync(repo, { recursive: true, force: true }); rmSync(s.dir, { recursive: true, force: true }); } +}); + +test('apply --confirm: flash → provision → validate, recorded in the registry without secrets', async () => { + const repo = fakeRepo(); const s = withSecrets(); + try { + const deps = fakeDeps({ repoRoot: repo }); + const res = await applyBoard(s.config, { port: '/dev/cu.usbmodem1', confirm: true }, deps); + assert.equal(res.ok, true, JSON.stringify(res)); + assert.deepEqual(Object.keys(res.stages), ['flash', 'wifi', 'validate']); + assert.equal(res.stages.flash.firmwareVersion, '0.8.15'); + assert.equal(res.stages.validate.address, '192.168.1.40'); + const events = readRegistry(s.reg).map((r) => r.event); + assert.deepEqual(events, ['chip_probed', 'flashed', 'provisioned', 'validated']); + assert.ok(!readFileSync(s.reg, 'utf8').includes('"pw"')); + } finally { rmSync(repo, { recursive: true, force: true }); rmSync(s.dir, { recursive: true, force: true }); } +}); + +test('firmware-only skips provisioning; failures stop the flow with a reason', async () => { + const repo = fakeRepo(); const s = withSecrets({ validate: { max_loss: 0.2 } }); + try { + const upd = await applyBoard(s.config, { port: '/dev/cu.usbmodem1', flow: 'firmware', confirm: true }, fakeDeps({ repoRoot: repo })); + assert.deepEqual(Object.keys(upd.stages), ['flash', 'validate']); + + const lossy = await applyBoard(s.config, { port: '/dev/cu.usbmodem1', flow: 'firmware', confirm: true }, fakeDeps({ repoRoot: repo, loss: 0.37 })); + assert.equal(lossy.ok, false); assert.equal(lossy.reason, 'below_threshold'); assert.match(lossy.detail, /loss 37\.0%/); + + const nvs = await applyBoard(s.config, { port: '/dev/cu.usbmodem1', confirm: true }, fakeDeps({ repoRoot: repo, provisionOk: false })); + assert.equal(nvs.reason, 'provision_failed'); + + const nomac = await applyBoard(s.config, { port: '/dev/cu.usbmodem1', confirm: true }, fakeDeps({ repoRoot: repo, macOut: 'Failed to connect' })); + assert.equal(nomac.reason, 'mac_probe_failed'); + } finally { rmSync(repo, { recursive: true, force: true }); rmSync(s.dir, { recursive: true, force: true }); } +}); + +test('status joins live CSI, HTTP and config; flags offline and unlisted nodes', async () => { + const s = withSecrets(); + try { + const deps = fakeDeps(); + deps.capture = async () => ({ ok: true, packets: 10, listen: '0.0.0.0:5005', seconds: 15, nodes: [ + { source: 'esp32', nodeId: 1, address: '192.168.1.40', packets: { csi: 450 }, csiRateHz: 30, csiLossFraction: 0.01, rssiMean: -50 }, + { source: 'esp32', nodeId: 9, address: '8.8.8.8', packets: { csi: 30 }, csiRateHz: 2, csiLossFraction: 0, rssiMean: -80 }, + ] }); + const fetched = []; + deps.fetch = async (url) => { fetched.push(url); return { ok: true, json: async () => ({ version: '0.8.15', ota_state: 'valid' }) }; }; + const res = await nodeStatus(s.config, deps); + const by = Object.fromEntries(res.nodes.map((n) => [n.node_id, n])); + assert.equal(by[1].health, 'healthy'); assert.equal(by[1].firmware, '0.8.15'); + assert.equal(by[2].health, 'offline'); + assert.equal(by[9].health, 'unlisted'); + assert.deepEqual(fetched, ['http://192.168.1.40:8032/ota/status'], 'never probes a public address'); + assert.equal(res.ok, false); + writeFileSync(s.reg, `${JSON.stringify({ event: 'node_assigned', mac: '11:22:33:44:55:09', node_id: 9 })}\n`); + const again = Object.fromEntries((await nodeStatus(s.config, deps)).nodes.map((n) => [n.node_id, n])); + assert.deepEqual([again[9].mac, again[9].source], ['11:22:33:44:55:09', 'registry']); + assert.notEqual(again[9].health, 'unlisted', 'auto-assigned nodes are judged, not reported as unlisted'); + } finally { rmSync(s.dir, { recursive: true, force: true }); } +}); + +test('plan resolves every node and warns when OTA is not configured', () => { + const repo = fakeRepo(); + try { + const res = planNodes({ ...cfg(), path: '/x/nodes.yaml' }, { repoRoot: repo, nets: NETS }); + assert.equal(res.ok, true); + assert.equal(res.target, '192.168.1.20:5005'); + assert.deepEqual(res.nodes.map((n) => [n.node_id, n.tdm]), [[1, '0/2'], [2, '1/2']]); + assert.ok(res.warnings.some((w) => w.includes('ota.psk_file'))); + } finally { rmSync(repo, { recursive: true, force: true }); } +}); + +// Review regressions: identity binding, assignment collisions and retained inventory. +test('interactive confirmation rejects a replacement board before writes or registry changes', async () => { + const repo = fakeRepo(); const s = withSecrets({ steps: ['flash', 'wifi'] }); + try { + const plan = await applyBoard(s.config, { port: '/dev/cu.usbmodem1' }, fakeDeps({ repoRoot: repo })); + const deps = fakeDeps({ repoRoot: repo, macOut: 'MAC: 11:22:33:44:55:66' }); + const result = await applyBoard(s.config, { port: plan.port, confirm: true, + expectedIdentity: { mac: plan.mac, nodeId: plan.node_id } }, deps); + assert.equal(result.reason, 'confirmation_changed'); + assert.equal(deps.calls.length, 1, 'only the identity probe may run'); + assert.equal(existsSync(s.reg), false, 'replacement cannot reserve a node ID'); + } finally { rmSync(repo, { recursive: true, force: true }); rmSync(s.dir, { recursive: true, force: true }); } +}); + +test('interactive confirmation rejects a changed assignment for the same board', async () => { + const repo = fakeRepo(); const s = withSecrets(); + try { + const deps = fakeDeps({ repoRoot: repo }); + const result = await applyBoard(s.config, { port: '/dev/cu.usbmodem1', confirm: true, + expectedIdentity: { mac: MAC, nodeId: 7 } }, deps); + assert.equal(result.reason, 'confirmation_changed'); + assert.equal(deps.calls.length, 1); + assert.equal(existsSync(s.reg), false); + } finally { rmSync(repo, { recursive: true, force: true }); rmSync(s.dir, { recursive: true, force: true }); } +}); + +test('interactive confirmation proceeds for the same board and node assignment', async () => { + const repo = fakeRepo(); const s = withSecrets(); + try { + const result = await applyBoard(s.config, { port: '/dev/cu.usbmodem1', confirm: true, + expectedIdentity: { mac: MAC, nodeId: 1 } }, fakeDeps({ repoRoot: repo })); + assert.equal(result.ok, true); + } finally { rmSync(repo, { recursive: true, force: true }); rmSync(s.dir, { recursive: true, force: true }); } +}); + +test('registry restoration refuses an ID now configured for a different MAC', () => { + const other = '11:22:33:44:55:66'; + assert.throws(() => resolveNode(cfg(), other, [{ event: 'node_assigned', mac: other, node_id: 1 }]), + error => error.reason === 'node_id_conflict'); +}); + +test('explicit config also refuses an ID retained by another unlisted board', () => { + const other = '11:22:33:44:55:66'; + assert.throws(() => resolveNode(cfg(), MAC, [{ event: 'node_assigned', mac: other, node_id: 1 }]), + error => error.reason === 'node_id_conflict'); +}); + +test('latest registry assignments detect duplicate IDs between unlisted boards', () => { + const a = '11:22:33:44:55:66', b = '11:22:33:44:55:77'; + assert.throws(() => resolveNode(cfg(), a, [{ mac: a, node_id: 7 }, { mac: b, node_id: 7 }]), + error => error.reason === 'node_id_conflict'); +}); + +test('explicit reassignment supersedes a historical registry ID for the same MAC', () => { + const other = '11:22:33:44:55:66'; + const registry = [{ mac: MAC, node_id: 7 }, { mac: other, node_id: 7 }]; + assert.equal(resolveNode(cfg(), other, registry).node.node_id, 7); +}); + +test('status retains offline auto-assigned nodes and observes their return', async () => { + const s = withSecrets({ nodes: [] }); + try { + writeFileSync(s.reg, JSON.stringify({ event: 'node_assigned', mac: MAC, node_id: 1 }) + '\n'); + const offline = await nodeStatus(s.config, fakeDeps({ silent: true })); + assert.equal(offline.ok, false); + assert.equal(offline.nodes.length, 1); + assert.deepEqual([offline.nodes[0].node_id, offline.nodes[0].mac, offline.nodes[0].health], [1, MAC, 'offline']); + assert.match(offline.summary, /^0\/1 known nodes healthy/); + const online = await nodeStatus(s.config, fakeDeps()); + assert.equal(online.nodes[0].health, 'healthy'); + assert.equal(online.ok, true); + } finally { rmSync(s.dir, { recursive: true, force: true }); } +}); + +test('malformed registry assignments cannot restore invalid IDs', () => { + const other = '11:22:33:44:55:66'; + for (const bad of [null, { mac: other, node_id: null }, { mac: other, node_id: true }, + { mac: other, node_id: -1 }, { mac: other, node_id: 256 }]) { + const result = resolveNode(cfg(), other, [bad]); + assert.equal(result.source, 'auto-assigned'); + assert.equal(result.node.node_id, 3); + } +}); + +test('status reports conflicting board assignments instead of a healthy source identity', async () => { + const s = withSecrets(); + try { + writeFileSync(s.reg, JSON.stringify({ event: 'node_assigned', mac: '11:22:33:44:55:66', node_id: 1 }) + '\n'); + const result = await nodeStatus(s.config, fakeDeps()); + const node = result.nodes.find(row => row.node_id === 1); + assert.equal(node.health, 'id-conflict'); + assert.equal(node.mac, null); + assert.equal(result.ok, false); + } finally { rmSync(s.dir, { recursive: true, force: true }); } +}); + +test('status uses an explicit reassignment without retaining the same MAC under its old ID', async () => { + const s = withSecrets(); + try { + writeFileSync(s.reg, JSON.stringify({ event: 'node_assigned', mac: MAC, node_id: 7 }) + '\n'); + const result = await nodeStatus(s.config, fakeDeps({ silent: true })); + assert.deepEqual(result.nodes.map(row => row.node_id), [1, 2]); + } finally { rmSync(s.dir, { recursive: true, force: true }); } +}); + +// Plan/apply parity and retired boards. +test('plan reads the registry and reports the conflict apply would refuse', () => { + const repo = fakeRepo(); const s = withSecrets(); + try { + const old = '11:22:33:44:55:66'; + writeFileSync(s.reg, `${JSON.stringify({ event: 'node_assigned', mac: old, node_id: 1 })}\n`); + const res = planNodes({ ...s.config, path: '/x/nodes.yaml' }, { repoRoot: repo, nets: NETS }); + assert.equal(res.ok, false, 'plan must not pass when apply would refuse'); + assert.equal(res.conflicts.length, 1); + assert.equal(res.conflicts[0].reason, 'node_id_conflict'); + assert.match(res.conflicts[0].detail, /retired:/, 'the error points at the way out'); + assert.equal(res.nodes.find((n) => n.node_id === 1).error, 'node_id_conflict'); + } finally { rmSync(repo, { recursive: true, force: true }); rmSync(s.dir, { recursive: true, force: true }); } +}); + +test('retired: releases a dead board\'s retained ID for its replacement', () => { + const old = '11:22:33:44:55:66'; + const registry = [{ event: 'node_assigned', mac: old, node_id: 1 }]; + assert.throws(() => resolveNode(cfg(), MAC, registry), (e) => e.reason === 'node_id_conflict'); + const freed = resolveNode(cfg({ retired: [old.toUpperCase()] }), MAC, registry); + assert.deepEqual([freed.source, freed.node.node_id], ['config', 1]); + // auto-assign may reuse the released ID too + const fresh = resolveNode(normalizeNodeConfig({ wifi: { ssid: 'x' }, retired: [old] }), '11:22:33:44:55:77', registry); + assert.deepEqual([fresh.source, fresh.node.node_id], ['auto-assigned', 1]); +}); + +test('a retired board plugged back in is refused, not silently re-used', () => { + const old = '11:22:33:44:55:66'; + assert.throws(() => resolveNode(cfg({ retired: [old] }), old, [{ mac: old, node_id: 9 }]), (e) => e.reason === 'node_retired'); +}); + +test('retired: is validated and cannot overlap active nodes', () => { + const text = problems(base({ retired: ['nope', MAC] })).join('\n'); + assert.ok(text.includes('retired[0]: must be a MAC'), text); + assert.ok(text.includes('retired[1]') && text.includes('also listed under nodes'), text); + assert.ok(problems(base({ retired: 'aa:bb:cc:dd:ee:09' })).join('\n').includes('retired: must be a list')); +}); + +test('status drops retired boards from the known inventory', async () => { + const s = withSecrets({ nodes: [], retired: [MAC] }); + try { + writeFileSync(s.reg, `${JSON.stringify({ event: 'node_assigned', mac: MAC, node_id: 1 })}\n`); + const res = await nodeStatus(s.config, fakeDeps({ silent: true })); + assert.deepEqual(res.nodes, [], 'a retired, silent board is not reported offline'); + assert.equal(res.ok, true); + } finally { rmSync(s.dir, { recursive: true, force: true }); } +}); diff --git a/harness/ruview/test/training-capture-collisions.test.mjs b/harness/ruview/test/training-capture-collisions.test.mjs index c6c4ac5282..3f49553b10 100644 --- a/harness/ruview/test/training-capture-collisions.test.mjs +++ b/harness/ruview/test/training-capture-collisions.test.mjs @@ -15,9 +15,27 @@ async function record(dir, port, sequence) { const tx = dgram.createSocket('udp4'); try { await new Promise((resolve, reject) => { - let text = ''; timer = setTimeout(() => reject(new Error('collector startup timeout')), 2000); - child.stderr.on('data', b => { text += b; if (text.includes("Collecting 'walking'")) { clearTimeout(timer); resolve(); } }); - child.once('error', reject); + // This is a session-collision test, not a Python startup benchmark. + // Cold Windows workers can take over two seconds to start the child. + let text = ''; + const cleanup = () => { + clearTimeout(timer); + child.stderr.off('data', onData); + child.off('exit', onExit); + child.off('error', onError); + }; + const onError = error => { cleanup(); reject(error); }; + const onExit = (code, signal) => onError(new Error( + `collector exited before readiness (code=${code}, signal=${signal}): ${text}`)); + const onData = chunk => { + text += chunk; + if (text.includes("Collecting 'walking'")) { cleanup(); resolve(); } + else text = text.slice(-8192); + }; + timer = setTimeout(() => onError(new Error(`collector startup timeout: ${text}`)), 10000); + child.stderr.on('data', onData); + child.once('exit', onExit); + child.once('error', onError); }); const packet = Buffer.alloc(24); packet.writeUInt32LE(0xC5110001); packet[4]=1; packet[5]=1; packet.writeUInt16LE(2,6); packet.writeUInt32LE(2437,8); packet.writeUInt32LE(sequence,12); packet.writeInt8(-50,16); Buffer.from([3,4,6,8]).copy(packet,20); await new Promise((resolve,reject)=>tx.send(packet,port,'127.0.0.1',e=>e?reject(e):resolve())); diff --git a/v2/Cargo.lock b/v2/Cargo.lock index 3ed1589569..9c481c692f 100644 --- a/v2/Cargo.lock +++ b/v2/Cargo.lock @@ -1825,36 +1825,36 @@ dependencies = [ [[package]] name = "cranelift-assembler-x64" -version = "0.123.20" +version = "0.123.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40d4018626c25bb153a72738b261bdfed97ad6b7f21b100d556c0b8457fa9b3b" +checksum = "142696a120a5dc298df74bcfb8c5ec43f0f8c53ff2f3607b3cffe67c1bc3fe0b" dependencies = [ "cranelift-assembler-x64-meta", ] [[package]] name = "cranelift-assembler-x64-meta" -version = "0.123.20" +version = "0.123.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c69b47b7ffbd42f8d162ee958886e685b374b102b3bd116b0470c911153428b4" +checksum = "24a12a121860c44ba6dc9d0bd2b4b9284f409c2f305aa04093184a7878148726" dependencies = [ "cranelift-srcgen", ] [[package]] name = "cranelift-bforest" -version = "0.123.20" +version = "0.123.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d1994514a2e498d42a7173804fb0618472c94176b72c54ef8d64acdada05d0f" +checksum = "26e0347ec8886cfe409616aeeeb3d1ee4ae0ec6ae0e053fc111e0214f67c830c" dependencies = [ "cranelift-entity", ] [[package]] name = "cranelift-bitset" -version = "0.123.20" +version = "0.123.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "019bfe57e970cf56fd9ad9ea45e4290457eb1ceb9bea954646abbe9d657a4a61" +checksum = "b8a8d504f1c49cf5b3c59166fa77c990d6618987baa16095a8adbc125218a710" dependencies = [ "serde", "serde_derive", @@ -1862,9 +1862,9 @@ dependencies = [ [[package]] name = "cranelift-codegen" -version = "0.123.20" +version = "0.123.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "397c0a3cc8a67f9f85dbe47fbba2c1e88430293b130f91f3c98411f6ae4e0e80" +checksum = "0826e7db0453c2e4def6cc16e661309b88acfe5c688727d2bd14f210c5979228" dependencies = [ "bumpalo", "cranelift-assembler-x64", @@ -1889,9 +1889,9 @@ dependencies = [ [[package]] name = "cranelift-codegen-meta" -version = "0.123.20" +version = "0.123.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5bc504c51980615d594a685367584d1fa95d36c32c14a15b0eb4b1d9cc0dc2e" +checksum = "5466899a46c1b457a2c40e62c0db595ead52ce070283349f24f1ea3e3ab95e59" dependencies = [ "cranelift-assembler-x64-meta", "cranelift-codegen-shared", @@ -1902,24 +1902,24 @@ dependencies = [ [[package]] name = "cranelift-codegen-shared" -version = "0.123.20" +version = "0.123.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc8ae2c52ca44f49719cd78e48480c076e115a490b4e51ae28a89535ec4d76df" +checksum = "4cb2af0ddf040bc688bb1c7c3f98026caf1f7be9735a90abe5210fb3ed61b31e" [[package]] name = "cranelift-control" -version = "0.123.20" +version = "0.123.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d94159e930a391bb589c8c4832769c3e2f9c81d8f1cd1266083a33a1787d41a9" +checksum = "23928359f722201147e559d1ad68b26e04f542df2da609615883373ca95488f9" dependencies = [ "arbitrary", ] [[package]] name = "cranelift-entity" -version = "0.123.20" +version = "0.123.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cc0257dc68c7d282dabc1b3c9effec3f5d00fff56a964a80e7c08af7a3aa4dc" +checksum = "544a5be17891f159c85d2a1200cbd1846d5ffdc9fbb26fa9140f4c0040cde8fe" dependencies = [ "cranelift-bitset", "serde", @@ -1928,9 +1928,9 @@ dependencies = [ [[package]] name = "cranelift-frontend" -version = "0.123.20" +version = "0.123.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a60aa092dbffc0b8e501845afc3f67a3c987d780d22dd302829aa9f0255738e" +checksum = "41a1775f555a016d16b212b38b7f4b4759f258ae90c7139ffc198bb3cb63829c" dependencies = [ "cranelift-codegen", "log", @@ -1940,15 +1940,15 @@ dependencies = [ [[package]] name = "cranelift-isle" -version = "0.123.20" +version = "0.123.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e62bea43dd7767887c6b7861021846b0f0a03d9cc72bafddd0827e79b5f5deb5" +checksum = "3cc4649f050a50eaf0ee29b47d797e76a7dbd6f4a1fa1247e21667fd50ccc197" [[package]] name = "cranelift-native" -version = "0.123.20" +version = "0.123.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f410a5e75d4758d0564106931a1809d50d17681385ea1686518d029593a06b2a" +checksum = "90b5fbff9fd5b0fef29865a393f77d1706f9ed50ff90ca40f2768ea2f53ccc7c" dependencies = [ "cranelift-codegen", "libc", @@ -1957,9 +1957,9 @@ dependencies = [ [[package]] name = "cranelift-srcgen" -version = "0.123.20" +version = "0.123.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5f98d0e96c17d2654a95820c77403050754561f2b2cd737dbf59462c64896f3" +checksum = "d2877cb00084918ff719184b88e247a2b46ebd0dca27fac3569a168590053ae1" [[package]] name = "crc" @@ -8102,9 +8102,9 @@ dependencies = [ [[package]] name = "pulley-interpreter" -version = "36.0.20" +version = "36.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f6a7699ba3c24456c4235f6578a5f182386a1a9230653e8783f165ea1085bbe" +checksum = "0781f03b600890175c17522913366e8543c96acd76d074b39a27835879f89702" dependencies = [ "cranelift-bitset", "log", @@ -8114,9 +8114,9 @@ dependencies = [ [[package]] name = "pulley-macros" -version = "36.0.20" +version = "36.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1efdd17eb6ed2d844c29350fffb7230c550ddb0cbe93e1fd2b9cfcc345d77450" +checksum = "dc7ad6f328adda4f2d169d9338402fad5f726630e9c9fdc26660771b13d1a788" dependencies = [ "proc-macro2", "quote", @@ -8213,7 +8213,7 @@ dependencies = [ "quinn-udp", "rustc-hash 2.1.1", "rustls", - "socket2 0.5.10", + "socket2 0.6.5", "thiserror 2.0.18", "tokio", "tracing", @@ -8253,9 +8253,9 @@ dependencies = [ "cfg_aliases", "libc", "once_cell", - "socket2 0.5.10", + "socket2 0.6.5", "tracing", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -12643,9 +12643,9 @@ dependencies = [ [[package]] name = "wasmtime" -version = "36.0.20" +version = "36.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a32143e651d97f1b8d749713d242f70a72c9a0d7e16fd498a7e5a3f5574ed10b" +checksum = "c58d2cbba3817454af39ec9f0b39279870a9a98ef4c52bbbcdd9c1ffc1f5f17f" dependencies = [ "addr2line", "anyhow", @@ -12698,9 +12698,9 @@ dependencies = [ [[package]] name = "wasmtime-environ" -version = "36.0.20" +version = "36.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0501ad47df09ae57fbb76e3bdc6513b1f6d9dd0eadf3a115bb39360798890c7" +checksum = "2e64cbeb0aa73682360f848c931ec7e024a59bb15e855fc461c21c6ec665775e" dependencies = [ "anyhow", "cpp_demangle", @@ -12725,16 +12725,16 @@ dependencies = [ [[package]] name = "wasmtime-internal-asm-macros" -version = "36.0.20" +version = "36.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de4c70c3fb098b9b28b73f7fc2b0147c5624621fb017a5354cdedb660f273e07" +checksum = "ead251813500a15bb1fc31db43f8d2e612fcc0c723f3efff69c5d7bedd9edaf9" dependencies = [ "cfg-if", ] [[package]] name = "wasmtime-internal-cache" -version = "36.0.20" +version = "36.0.21" dependencies = [ "anyhow", "base64 0.22.1", @@ -12755,9 +12755,9 @@ dependencies = [ [[package]] name = "wasmtime-internal-component-macro" -version = "36.0.20" +version = "36.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28729d33f5731506bcfe774a4b9eaf9e7e713449e1e3d848748f7de9978bea53" +checksum = "c6aba235e8bfa25cf76312a8b8295964968164cbd4e3fb6e2d9be167128ae3d7" dependencies = [ "anyhow", "proc-macro2", @@ -12770,15 +12770,15 @@ dependencies = [ [[package]] name = "wasmtime-internal-component-util" -version = "36.0.20" +version = "36.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91abf6b751d0f135cab1e78448f402af00a0f3e0638be858d18cc93170ab1009" +checksum = "0e1b1657d77558b86ace8e85e0f016655f7b68b8a8c5b8ad875515f4880aece3" [[package]] name = "wasmtime-internal-cranelift" -version = "36.0.20" +version = "36.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3840e7bb2d2593f3178b13a6717f215ad63e233ef54b61892f3cee80582f098" +checksum = "0dc2e39983f07c0e85ba5f83df2dfb8a4e8607a5cab01c761a3a5deb15abc5bd" dependencies = [ "anyhow", "cfg-if", @@ -12803,9 +12803,9 @@ dependencies = [ [[package]] name = "wasmtime-internal-fiber" -version = "36.0.20" +version = "36.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "458d24ab006050e9b5fcf79f2d83ccc236f69846685fb27a7bf4c50a1583f1ea" +checksum = "3d8f744e4eea04b2b1070b941d32138c1c34e50887b27d9f11c47f9ce8187cd6" dependencies = [ "anyhow", "cc", @@ -12819,9 +12819,9 @@ dependencies = [ [[package]] name = "wasmtime-internal-jit-debug" -version = "36.0.20" +version = "36.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eca1ce583227e82e6370939f48395ae902acdcd4c91ca32ea1ae7abbc86232a7" +checksum = "7e12bcf0d25fa4e1c4346d0ecea325d0ef850505ca1fabec5711b7a86679ae3e" dependencies = [ "cc", "object", @@ -12831,9 +12831,9 @@ dependencies = [ [[package]] name = "wasmtime-internal-jit-icache-coherence" -version = "36.0.20" +version = "36.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c110f07b222a8eb29d957eabf90dce38f41461f883099ab28dc50861913ba17" +checksum = "147bdb5ff208cf503adb480a9bd9a3c9012b3289007e69d46117ca31cef51ed6" dependencies = [ "anyhow", "cfg-if", @@ -12843,24 +12843,24 @@ dependencies = [ [[package]] name = "wasmtime-internal-math" -version = "36.0.20" +version = "36.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a273dd4ec37457d435538c00b30f3ec9ae99e04a4f6ba89a737048e8de6095df" +checksum = "4e77a4cd77f1aa48a23ef9cc544cdc8732de512f2ad50f628a39092fb53cb34c" dependencies = [ "libm", ] [[package]] name = "wasmtime-internal-slab" -version = "36.0.20" +version = "36.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d12c7b3ae65639ab90d8d29135348dfd16de6d96eb36ab5b63861f3e12cf8e0" +checksum = "2652fc800965c03acb4e5a2a2bb0ce52650a1f1b3d23e91cd3f65ebafbd68ff0" [[package]] name = "wasmtime-internal-unwinder" -version = "36.0.20" +version = "36.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6da6131cb87700e06ffb7a83cc933658f521176f04d6d73511f1ee60db58c515" +checksum = "cb5796bfab805bdb0f13e8a8502c0ebd643f11bc209c9199da71c97c17234da3" dependencies = [ "anyhow", "cfg-if", @@ -12871,9 +12871,9 @@ dependencies = [ [[package]] name = "wasmtime-internal-versioned-export-macros" -version = "36.0.20" +version = "36.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71efc44d34bb86408d7f620d308dad19457f34543e5c44ace680bdbe2d819917" +checksum = "f534370e1ccf59b3d903c6f4db72036c43b5d2b94a77ccd39c43239e90b07e61" dependencies = [ "proc-macro2", "quote", @@ -12882,9 +12882,9 @@ dependencies = [ [[package]] name = "wasmtime-internal-winch" -version = "36.0.20" +version = "36.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af4da8c46ddb215374f023c0e3ec8917185b878b3d9874fbfaaefdc2d2df38e0" +checksum = "f286941452a9d79b9e681f7272354280129ef38c55497b757bfc9d5810277afd" dependencies = [ "anyhow", "cranelift-codegen", @@ -12899,9 +12899,9 @@ dependencies = [ [[package]] name = "wasmtime-internal-wit-bindgen" -version = "36.0.20" +version = "36.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "356a1ae3cfc0534a84df35d9df214a241ca454983c0d311f8169b1674b4979a3" +checksum = "f0f67e6dc46acb465d17902b302f19bf29018d05b7cc00c8d6eeb2e8aa703399" dependencies = [ "anyhow", "bitflags 2.11.0", @@ -13825,9 +13825,9 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "winch-codegen" -version = "36.0.20" +version = "36.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09aff830750e48ca9828e9df5ba4589688f49f2748e4a1d43fd543d6a673d272" +checksum = "48ff4619adfdac753592c13508b49067f857cb41a1e5c2579e12580ad7470ce6" dependencies = [ "anyhow", "cranelift-assembler-x64", diff --git a/v2/Cargo.toml b/v2/Cargo.toml index d546e97b36..e0ec3b0d89 100644 --- a/v2/Cargo.toml +++ b/v2/Cargo.toml @@ -3,7 +3,7 @@ resolver = "2" members = [ # Checksum-verified dependency patches; keep their upstream tests in CI. "vendor/security/zip-0.6.6", - "vendor/security/wasmtime-internal-cache-36.0.20", + "vendor/security/wasmtime-internal-cache-36.0.21", "vendor/security/sqlx-sqlite-0.8.6", "crates/wifi-densepose-core", "crates/wifi-densepose-physics", # ADR-323 bounded pose assessment/correction @@ -357,5 +357,5 @@ zip-legacy = { package = "zip", path = "vendor/security/zip-0.6.6" } # Candle 0.9 expects ZIP 7's filename access. The private adapter validates # filename decoding and delegates ZIP operations to patched upstream ZIP 9. zip-candle = { package = "zip", path = "vendor/security/zip-candle-compat" } -wasmtime-internal-cache = { path = "vendor/security/wasmtime-internal-cache-36.0.20" } +wasmtime-internal-cache = { path = "vendor/security/wasmtime-internal-cache-36.0.21" } sqlx-sqlite = { path = "vendor/security/sqlx-sqlite-0.8.6" } diff --git a/v2/crates/homecore-plugins/Cargo.toml b/v2/crates/homecore-plugins/Cargo.toml index 5adb536849..f9815fde03 100644 --- a/v2/crates/homecore-plugins/Cargo.toml +++ b/v2/crates/homecore-plugins/Cargo.toml @@ -64,7 +64,7 @@ base64 = "0.22" # Upgraded from 25.0.3 to remediate the Cranelift/Winch sandbox advisories; # 40.0.4 was subsequently replaced because of RUSTSEC-2026-0114. # Patched 36.x retains HOMECORE's Rust 1.89 MSRV. -wasmtime = { version = "36.0.20", optional = true } +wasmtime = { version = "36.0.21", optional = true } [dev-dependencies] diff --git a/v2/crates/homecore-plugins/tests/wasmtime_advisory_regression.rs b/v2/crates/homecore-plugins/tests/wasmtime_advisory_regression.rs new file mode 100644 index 0000000000..b218bebf4b --- /dev/null +++ b/v2/crates/homecore-plugins/tests/wasmtime_advisory_regression.rs @@ -0,0 +1,23 @@ +//! RUSTSEC-2026-0569: a re-exported host function has no calling Wasm module. +#![cfg(feature = "wasmtime")] + +#[test] +fn reexported_host_function_has_no_calling_module() { + use wasmtime::{Caller, Engine, Linker, Module, Store}; + let engine = Engine::default(); + let module = Module::new(&engine, "(module (import \"host\" \"inspect\" (func)) (memory (export \"memory\") 1) (export \"run\" (func 0)))").unwrap(); + let memory = module.get_export_index("memory").unwrap(); + let mut linker = Linker::new(&engine); + linker + .func_wrap("host", "inspect", move |mut caller: Caller<'_, ()>| { + assert!(caller.get_module_export(&memory).is_none()); + }) + .unwrap(); + let mut store = Store::new(&engine, ()); + let instance = linker.instantiate(&mut store, &module).unwrap(); + instance + .get_typed_func::<(), ()>(&mut store, "run") + .unwrap() + .call(&mut store, ()) + .unwrap(); +} diff --git a/v2/vendor/security/wasmtime-internal-cache-36.0.20/RUVIEW-PROVENANCE.md b/v2/vendor/security/wasmtime-internal-cache-36.0.20/RUVIEW-PROVENANCE.md deleted file mode 100644 index cb51c691fb..0000000000 --- a/v2/vendor/security/wasmtime-internal-cache-36.0.20/RUVIEW-PROVENANCE.md +++ /dev/null @@ -1,13 +0,0 @@ -# Dependency compatibility patch - -Source: crates.io `wasmtime-internal-cache` 36.0.20. -Published archive SHA-256: `65f09c789d1bb2e7ab1618ac8336d783c1ff0babc38430c7f1940d01c546722b` (verified against crates.io version metadata). - -Executable source, tests, fixtures and licenses are byte-for-byte upstream. -Only the normalized Cargo.toml dependency requirement changes: -`zstd` from `0.13.0` to `0.14.3`, retaining the upstream feature selection. -Registry bookkeeping, the nested lockfile, the unconsumed original manifest -and upstream CI files are omitted. No advisory suppression is added. - -This preserves the parent API while selecting the patched registry dependency. -Remove this override when a compatible parent release includes the fixed dependency. diff --git a/v2/vendor/security/wasmtime-internal-cache-36.0.20/Cargo.toml b/v2/vendor/security/wasmtime-internal-cache-36.0.21/Cargo.toml similarity index 99% rename from v2/vendor/security/wasmtime-internal-cache-36.0.20/Cargo.toml rename to v2/vendor/security/wasmtime-internal-cache-36.0.21/Cargo.toml index 9b86a5661b..b41f628f52 100644 --- a/v2/vendor/security/wasmtime-internal-cache-36.0.20/Cargo.toml +++ b/v2/vendor/security/wasmtime-internal-cache-36.0.21/Cargo.toml @@ -13,7 +13,7 @@ edition = "2024" rust-version = "1.86.0" name = "wasmtime-internal-cache" -version = "36.0.20" +version = "36.0.21" authors = ["The Wasmtime Project Developers"] build = "build.rs" autolib = false diff --git a/v2/vendor/security/wasmtime-internal-cache-36.0.20/LICENSE b/v2/vendor/security/wasmtime-internal-cache-36.0.21/LICENSE similarity index 100% rename from v2/vendor/security/wasmtime-internal-cache-36.0.20/LICENSE rename to v2/vendor/security/wasmtime-internal-cache-36.0.21/LICENSE diff --git a/v2/vendor/security/wasmtime-internal-cache-36.0.21/RUVIEW-PROVENANCE.md b/v2/vendor/security/wasmtime-internal-cache-36.0.21/RUVIEW-PROVENANCE.md new file mode 100644 index 0000000000..e9d78a01ff --- /dev/null +++ b/v2/vendor/security/wasmtime-internal-cache-36.0.21/RUVIEW-PROVENANCE.md @@ -0,0 +1,13 @@ +# Dependency compatibility patch + +Source: crates.io `wasmtime-internal-cache` 36.0.21. +Published archive SHA-256: `1ff2377df26271c42d9649ac4ee13656ef89a51f02f0daa7917e67c635d9bc1d`. + +Executable source, tests, fixtures and licenses are byte-for-byte upstream. +Only the normalized Cargo.toml zstd requirement changes from0.13.0 to0.14.3, +retaining upstream features and the previously reviewed compatibility repair. +Registry bookkeeping, nested lockfile, unconsumed original manifest and upstream +CI files are omitted. No source backport or advisory ignore is added here. + +This version matches the compatible Wasmtime36.0.21 core security update. +Remove the override when a compatible parent selects the fixed zstd dependency. diff --git a/v2/vendor/security/wasmtime-internal-cache-36.0.20/build.rs b/v2/vendor/security/wasmtime-internal-cache-36.0.21/build.rs similarity index 100% rename from v2/vendor/security/wasmtime-internal-cache-36.0.20/build.rs rename to v2/vendor/security/wasmtime-internal-cache-36.0.21/build.rs diff --git a/v2/vendor/security/wasmtime-internal-cache-36.0.20/src/config.rs b/v2/vendor/security/wasmtime-internal-cache-36.0.21/src/config.rs similarity index 100% rename from v2/vendor/security/wasmtime-internal-cache-36.0.20/src/config.rs rename to v2/vendor/security/wasmtime-internal-cache-36.0.21/src/config.rs diff --git a/v2/vendor/security/wasmtime-internal-cache-36.0.20/src/config/tests.rs b/v2/vendor/security/wasmtime-internal-cache-36.0.21/src/config/tests.rs similarity index 100% rename from v2/vendor/security/wasmtime-internal-cache-36.0.20/src/config/tests.rs rename to v2/vendor/security/wasmtime-internal-cache-36.0.21/src/config/tests.rs diff --git a/v2/vendor/security/wasmtime-internal-cache-36.0.20/src/lib.rs b/v2/vendor/security/wasmtime-internal-cache-36.0.21/src/lib.rs similarity index 100% rename from v2/vendor/security/wasmtime-internal-cache-36.0.20/src/lib.rs rename to v2/vendor/security/wasmtime-internal-cache-36.0.21/src/lib.rs diff --git a/v2/vendor/security/wasmtime-internal-cache-36.0.20/src/tests.rs b/v2/vendor/security/wasmtime-internal-cache-36.0.21/src/tests.rs similarity index 100% rename from v2/vendor/security/wasmtime-internal-cache-36.0.20/src/tests.rs rename to v2/vendor/security/wasmtime-internal-cache-36.0.21/src/tests.rs diff --git a/v2/vendor/security/wasmtime-internal-cache-36.0.20/src/worker.rs b/v2/vendor/security/wasmtime-internal-cache-36.0.21/src/worker.rs similarity index 100% rename from v2/vendor/security/wasmtime-internal-cache-36.0.20/src/worker.rs rename to v2/vendor/security/wasmtime-internal-cache-36.0.21/src/worker.rs diff --git a/v2/vendor/security/wasmtime-internal-cache-36.0.20/src/worker/tests.rs b/v2/vendor/security/wasmtime-internal-cache-36.0.21/src/worker/tests.rs similarity index 100% rename from v2/vendor/security/wasmtime-internal-cache-36.0.20/src/worker/tests.rs rename to v2/vendor/security/wasmtime-internal-cache-36.0.21/src/worker/tests.rs diff --git a/v2/vendor/security/wasmtime-internal-cache-36.0.20/src/worker/tests/system_time_stub.rs b/v2/vendor/security/wasmtime-internal-cache-36.0.21/src/worker/tests/system_time_stub.rs similarity index 100% rename from v2/vendor/security/wasmtime-internal-cache-36.0.20/src/worker/tests/system_time_stub.rs rename to v2/vendor/security/wasmtime-internal-cache-36.0.21/src/worker/tests/system_time_stub.rs diff --git a/v2/vendor/security/wasmtime-internal-cache-36.0.20/tests/cache_write_default_config.rs b/v2/vendor/security/wasmtime-internal-cache-36.0.21/tests/cache_write_default_config.rs similarity index 100% rename from v2/vendor/security/wasmtime-internal-cache-36.0.20/tests/cache_write_default_config.rs rename to v2/vendor/security/wasmtime-internal-cache-36.0.21/tests/cache_write_default_config.rs