From 59cb729a728a9e54fee1ceda8e4368451970b9fa Mon Sep 17 00:00:00 2001 From: matthewxfz3 Date: Thu, 12 Nov 2026 17:44:58 -0800 Subject: [PATCH 01/10] =?UTF-8?q?feat(harness):=20ruview=20node=20?= =?UTF-8?q?=E2=80=94=20set=20up=20ESP32=20CSI=20nodes=20from=20one=20YAML?= =?UTF-8?q?=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces the per-board esptool/provision.py chain with a declarative config applied by MAC. Co-Authored-By: Claude Opus 5.5 --- firmware/esp32-csi-node/nodes.example.yaml | 60 ++ harness/ruview/.harness/manifest.json | 13 +- harness/ruview/.harness/manifest.sha256 | 2 +- harness/ruview/README.md | 36 ++ harness/ruview/package.json | 2 +- harness/ruview/src/cli.js | 125 ++++ harness/ruview/src/devices/esp32.js | 2 + harness/ruview/src/firmware.js | 3 +- harness/ruview/src/node-config.js | 682 +++++++++++++++++++++ harness/ruview/test/firmware.test.mjs | 5 + harness/ruview/test/node-config.test.mjs | 299 +++++++++ 11 files changed, 1220 insertions(+), 9 deletions(-) create mode 100644 firmware/esp32-csi-node/nodes.example.yaml create mode 100644 harness/ruview/src/node-config.js create mode 100644 harness/ruview/test/node-config.test.mjs diff --git a/firmware/esp32-csi-node/nodes.example.yaml b/firmware/esp32-csi-node/nodes.example.yaml new file mode 100644 index 0000000000..d231efe86f --- /dev/null +++ b/firmware/esp32-csi-node/nodes.example.yaml @@ -0,0 +1,60 @@ +# 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 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 1350e0f9cd..6bfbe199d2 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": "ae06aa31b2e1f93902a8a05070b5248327bc3aa17219ea4611d29a95ea7efc11", + "README.md": "a5b814493c3f6fd847de7dc0a8f66e95f4f5c2f7c6715e5957aa4cb5a4ac1855", "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": "d1d313a9e57244c817d688918c150c46aaf7aeb50ca65c0d982690e302ef7fdd", + "package.json": "46fc4d0bf96b782164bc9c24e9fbe6a8c176495cd1e83fd03a1766cc88850732", "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": "bb7989f4660566cd016a0cb3c44b6e816d19618ed94337398488732056e8202b", - "src/devices/esp32.js": "21b8c74d9b0a47f52538b5d902faa32ac6f4c58dab94d6612d7fbc89237d8f80", + "src/cli.js": "9f2fcb67faa9540c6335d36edb284dcb1e9a22e185445307eaf6695efc635f01", + "src/devices/esp32.js": "587e875cdaddb7b942c4e2c0521595456894762b842446e553b8c69d2c1ad32f", "src/devices/esphome.js": "d4429fd09c8d93fdc2e5926305d8193c98e074affc48deabda4b16bb9b620056", "src/devices/lidar.js": "d9a91e2b6eec21463bbb950fc333e9d0ebd2699a7c533d534323bb4d6a55bd97", "src/devices/mmwave.js": "2ac0be6c4be3c71d147d4bc57b8fd516d051234a8897295bc9e7f9c36c8ae710", @@ -54,7 +54,7 @@ "src/devices/serial-pump.js": "86fd00708c7f74ca004eee1b729821aa10a70bfd394cc556dc0ec4b64b43b149", "src/doctor.js": "e5feccab91da3fdad16316ed7fd9090218a55b6de48772219ced96a930b87e75", "src/exec.js": "8889d13b8a8277f322856e743c79ac8f29b707251eaf9757c46d3f745743ae22", - "src/firmware.js": "a38e9ea3da8b60ffff662c73525418b4e6ffe58f7c1a0797b82defdda4908457", + "src/firmware.js": "5a9b8d38e92280b1e46dd16b19222d9529e16b2bed9134fbb0d9dea44b033c12", "src/guardrails.js": "aacc8fa6088f7f1ccea3a0b02171a5c516b95d3416ee3ba87add3879a1d6aaad", "src/guidance.js": "1e0ccee808a736319b3f8257a3fc4fc0f8d9f8956970887d597870bb8d615949", "src/hosts/claude-code.js": "2212bc39b49822018800dfe33a471e56bbb4c5233d716bfa7aa4fff77aa23edb", @@ -63,6 +63,7 @@ "src/kernel.js": "48dfd218a747dd158f82ae108e7e8ed89a82cd68649214d23266112dc9033f5d", "src/mcp-http.js": "b72e021ae00d01831ef7184db824f5e85a2b8747e1d8e994b6a1cb1cb4d08945", "src/mcp-server.js": "5eb8c3431768932c3a85476df032d0608fc7a732ede3d6bdcc8467ca47552666", + "src/node-config.js": "ed7e9e83668dace777b71af2fa475f153a2793a826ae446047e9103aba95539e", "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": "edc6be28115ad21f18954066e989a6b4aaae2137b2f4e6d6571312beef610288", + "filesDigest": "c5afe0760a50ee0be0c086f1c92138f10b7937b416d23468a6559c943573eeea", "brainDigest": "c0fb7b079ded157059b91601361429944697dae3cc42abc00dfe1a680986b0f4", "gateFingerprint": "6e53c784eee38310188948fc75fb49e6b4ebc04e247d01b903fa8c8a92d67bdd", "developmentPins": { diff --git a/harness/ruview/.harness/manifest.sha256 b/harness/ruview/.harness/manifest.sha256 index 054df24f9a..6319f5b572 100644 --- a/harness/ruview/.harness/manifest.sha256 +++ b/harness/ruview/.harness/manifest.sha256 @@ -1 +1 @@ -0517e22827b8acc7d8550e1e560e4f13ca79397977c52c30cf23770dead2327a manifest.json +729e0fb4349c429ea448e867eb203329cb0f314affeb85e87b0b88990450ad60 manifest.json diff --git a/harness/ruview/README.md b/harness/ruview/README.md index e0abaa348c..d5f4e27173 100644 --- a/harness/ruview/README.md +++ b/harness/ruview/README.md @@ -129,6 +129,42 @@ 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`. +- **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 06d44e5eda..8bbe43b804 100644 --- a/harness/ruview/package.json +++ b/harness/ruview/package.json @@ -47,7 +47,7 @@ ], "scripts": { "test": "node --test 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 7b99d9e76b..7dc69a7c8f 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,121 @@ 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 }, 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 +415,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/firmware.js b/harness/ruview/src/firmware.js index 71da155665..d079bbb500 100644 --- a/harness/ruview/src/firmware.js +++ b/harness/ruview/src/firmware.js @@ -143,7 +143,8 @@ export function summarizeBootLog(text) { const lines = String(text).split(/\r?\n/); const csi = lines.filter((l) => /CSI cb|csi_collector/.test(l)).length; const panic = lines.some((l) => /Guru Meditation|abort\(\) was called|Backtrace:/.test(l)); - const version = (/(?:RuView|esp32-csi-node)[^\n]*?v?(\d+\.\d+\.\d+)/i.exec(text) || [])[1] || null; + // Banners: "esp32-csi-node v0.8.1", "RuView … 0.8.1", "ESP32-S3 CSI Node (ADR-018 / ADR-110) — v0.8.15". + const version = (/(?:RuView|esp32-csi-node|CSI Node)[^\n]*?v?(\d+\.\d+\.\d+)/i.exec(text) || [])[1] || null; return { lines: lines.filter(Boolean).length, csiCallbacks: csi, diff --git a/harness/ruview/src/node-config.js b/harness/ruview/src/node-config.js new file mode 100644 index 0000000000..51b0cf633b --- /dev/null +++ b/harness/ruview/src/node-config.js @@ -0,0 +1,682 @@ +// 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', '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'); + 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, + 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; +} + +/** + * 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}`); + const listed = config.nodes.find((n) => n.mac === m); + let entry; let source; + if (listed) { entry = listed; source = 'config'; } + else { + const prior = registry.filter((r) => r.mac === m && Number.isInteger(Number(r.node_id))).at(-1); + 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), ...registry.map((r) => Number(r.node_id)).filter(Number.isInteger)]); + 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 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'); + const nodes = config.nodes.map((n) => { + const { node } = resolveNode(config, n.mac, []); + 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 }; + }); + return { + ok: !firmware.error, + 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, + 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 }, 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 (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 = new Map(); + for (const r of registry) if (r.event === 'node_assigned' && r.mac && Number.isInteger(Number(r.node_id))) assigned.set(Number(r.node_id), r.mac); + const known = (id) => config.nodes.find((n) => n.node_id === id) + || (assigned.has(id) && !config.nodes.some((n) => n.mac === assigned.get(id)) ? { node_id: id, mac: assigned.get(id), zone: null, source: 'registry' } : null); + const ids = new Set([...config.nodes.map((n) => n.node_id), ...heard.keys()]); + const rows = []; + for (const id of [...ids].sort((a, b) => a - b)) { + const listed = known(id); + 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 = !listed ? 'unlisted' : judged?.ok ? 'healthy' : n ? 'degraded' : http && !http.error ? 'no-csi-here' : 'offline'; + rows.push({ + node_id: id, zone: listed?.zone ?? null, mac: listed?.mac ?? null, source: 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: 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]]); + 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/firmware.test.mjs b/harness/ruview/test/firmware.test.mjs index f2786aaa00..54283ee462 100644 --- a/harness/ruview/test/firmware.test.mjs +++ b/harness/ruview/test/firmware.test.mjs @@ -223,3 +223,8 @@ test('boot-log capture survives non-ASCII firmware logs on a cp1252 stdout', (t) rmSync(fake, { recursive: true, force: true }); } }); + +test('boot log version parsing covers the current CSI Node banner', () => { + assert.equal(summarizeBootLog('I (406) main: ESP32-S3 CSI Node (ADR-018 / ADR-110) \u2014 v0.8.15 \u2014 Node ID: 2\n').firmwareVersion, '0.8.15'); + assert.equal(summarizeBootLog('I (1) main: esp32-csi-node v0.8.1\n').firmwareVersion, '0.8.1'); +}); diff --git a/harness/ruview/test/node-config.test.mjs b/harness/ruview/test/node-config.test.mjs new file mode 100644 index 0000000000..618722d1f3 --- /dev/null +++ b/harness/ruview/test/node-config.test.mjs @@ -0,0 +1,299 @@ +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'; + +const base = (extra = {}) => ({ + wifi: { ssid: 'Lab-2G', password_file: '/tmp/none' }, + 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, '/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 }); } +}); From 3e1af4f94f3f5d7f52823eeacacaacb78648c844 Mon Sep 17 00:00:00 2001 From: Dragan Spiridonov Date: Fri, 13 Nov 2026 09:38:03 +0100 Subject: [PATCH 02/10] fix(harness): bind node confirmation and reconcile registry inventory --- .github/workflows/ci.yml | 3 +- harness/ruview/.harness/manifest.json | 6 +- harness/ruview/.harness/manifest.sha256 | 2 +- harness/ruview/src/cli.js | 3 +- harness/ruview/src/node-config.js | 49 ++++++++--- harness/ruview/test/node-config.test.mjs | 105 +++++++++++++++++++++++ 6 files changed, 150 insertions(+), 18 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f6b6b9a7b5..6fe951ba5b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -385,6 +385,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 @@ -505,7 +506,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/harness/ruview/.harness/manifest.json b/harness/ruview/.harness/manifest.json index 6bfbe199d2..f7bda01d6b 100644 --- a/harness/ruview/.harness/manifest.json +++ b/harness/ruview/.harness/manifest.json @@ -45,7 +45,7 @@ "skills/verify.md": "2d38d240e9810a7827e2ebd3717dc0f85c646cc92e46c3812fe77c5b9eb40b76", "src/brain.js": "0f16a75aea943acdacc430ff11d5df7ecdec9cca2ab497795ff6f33eaebdfab6", "src/cli-ui.js": "5ac04c5790f03df708c24a6dd366b51ff33a643691642b689284bcada25c9d9a", - "src/cli.js": "9f2fcb67faa9540c6335d36edb284dcb1e9a22e185445307eaf6695efc635f01", + "src/cli.js": "90d3d2dfeb44ddadb51455d2cd336e3834faaf5d2a733332900ac372a4dad2a5", "src/devices/esp32.js": "587e875cdaddb7b942c4e2c0521595456894762b842446e553b8c69d2c1ad32f", "src/devices/esphome.js": "d4429fd09c8d93fdc2e5926305d8193c98e074affc48deabda4b16bb9b620056", "src/devices/lidar.js": "d9a91e2b6eec21463bbb950fc333e9d0ebd2699a7c533d534323bb4d6a55bd97", @@ -63,7 +63,7 @@ "src/kernel.js": "48dfd218a747dd158f82ae108e7e8ed89a82cd68649214d23266112dc9033f5d", "src/mcp-http.js": "b72e021ae00d01831ef7184db824f5e85a2b8747e1d8e994b6a1cb1cb4d08945", "src/mcp-server.js": "5eb8c3431768932c3a85476df032d0608fc7a732ede3d6bdcc8467ca47552666", - "src/node-config.js": "ed7e9e83668dace777b71af2fa475f153a2793a826ae446047e9103aba95539e", + "src/node-config.js": "6f53ae40b9ff70a65a350c9379c993cf1227c0998fecc6fa86c3354521f7cab8", "src/policy.js": "baad5eb8c3eb2e416e0a71b342ad850d7c8413d7d976571ebf596937f69da8e2", "src/process-runner.js": "49533b038044dfb8bc76ed01c030d06a9856ead0836157fb693e2a7d40f786d6", "src/redact.js": "ebf1afff46341078706b0401838c53db043603586e280d51ece5cf1feba35189", @@ -76,7 +76,7 @@ "src/training.js": "bd8a6c12eba4f9d2f8990fa9692cd04c743ea247f9b13e10f8717ceefcbd09ac", "src/ui/console-widget.js": "0d2f707d57b5ec54968dee1dc80f31c7db9152dcadfc48700c18379dabb55462" }, - "filesDigest": "c5afe0760a50ee0be0c086f1c92138f10b7937b416d23468a6559c943573eeea", + "filesDigest": "5311fa218f92c3617a488a83ef1e00642cf0549b0bb9757c1d4bc9a90009738b", "brainDigest": "c0fb7b079ded157059b91601361429944697dae3cc42abc00dfe1a680986b0f4", "gateFingerprint": "6e53c784eee38310188948fc75fb49e6b4ebc04e247d01b903fa8c8a92d67bdd", "developmentPins": { diff --git a/harness/ruview/.harness/manifest.sha256 b/harness/ruview/.harness/manifest.sha256 index 6319f5b572..db0fe70602 100644 --- a/harness/ruview/.harness/manifest.sha256 +++ b/harness/ruview/.harness/manifest.sha256 @@ -1 +1 @@ -729e0fb4349c429ea448e867eb203329cb0f314affeb85e87b0b88990450ad60 manifest.json +ace9b6697527d3feaf9fbbd2a90e113911653588b164743127b2a95b7c2eb125 manifest.json diff --git a/harness/ruview/src/cli.js b/harness/ruview/src/cli.js index 7dc69a7c8f..0ca281358b 100644 --- a/harness/ruview/src/cli.js +++ b/harness/ruview/src/cli.js @@ -296,7 +296,8 @@ async function nodeConfig(rest, flags) { console.error(' skipped — nothing was written'); return true; } - const res = await fl.applyBoard(config, { port, flow, confirm: true }, deps); + 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; }; diff --git a/harness/ruview/src/node-config.js b/harness/ruview/src/node-config.js index 51b0cf633b..0279af43d0 100644 --- a/harness/ruview/src/node-config.js +++ b/harness/ruview/src/node-config.js @@ -318,6 +318,27 @@ export function appendRegistry(path, event, fields = {}, now = () => new Date()) 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. +function registryNodes(registry) { + 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(); + nodes.push({ mac, node_id: id, source: 'registry' }); + } + return nodes; +} + +function effectiveNodes(config, registry) { + const nodes = new Map(registryNodes(registry).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. @@ -329,10 +350,10 @@ export function resolveNode(config, mac, registry = []) { let entry; let source; if (listed) { entry = listed; source = 'config'; } else { - const prior = registry.filter((r) => r.mac === m && Number.isInteger(Number(r.node_id))).at(-1); + 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), ...registry.map((r) => Number(r.node_id)).filter(Number.isInteger)]); + const used = new Set([...config.nodes.map((n) => n.node_id), ...registryNodes(registry).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'; @@ -340,6 +361,8 @@ export function resolveNode(config, mac, registry = []) { 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 before applying`); const node = { ...config.defaults, ...entry }; if (node.tdm_slot !== undefined) node.tdm_total = config.tdm_total; return { node, source }; @@ -509,7 +532,7 @@ function judgeNode(config, n) { * 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 }, deps) { +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); @@ -526,6 +549,9 @@ export async function applyBoard(config, { port, flow = 'apply', confirm = false 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; @@ -605,16 +631,15 @@ export async function nodeStatus(config, deps) { 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); + 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 = new Map(); - for (const r of registry) if (r.event === 'node_assigned' && r.mac && Number.isInteger(Number(r.node_id))) assigned.set(Number(r.node_id), r.mac); - const known = (id) => config.nodes.find((n) => n.node_id === id) - || (assigned.has(id) && !config.nodes.some((n) => n.mac === assigned.get(id)) ? { node_id: id, mac: assigned.get(id), zone: null, source: 'registry' } : null); - const ids = new Set([...config.nodes.map((n) => n.node_id), ...heard.keys()]); + 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; @@ -626,12 +651,12 @@ export async function nodeStatus(config, deps) { } catch { http = { error: 'unreachable' }; } } const judged = n ? judgeNode(config, n) : null; - const health = !listed ? 'unlisted' : judged?.ok ? 'healthy' : n ? 'degraded' : http && !http.error ? 'no-csi-here' : 'offline'; + 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: listed?.zone ?? null, mac: listed?.mac ?? null, source: listed ? (listed.source || 'config') : null, address, + 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: judged?.problems ?? [], + 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)); diff --git a/harness/ruview/test/node-config.test.mjs b/harness/ruview/test/node-config.test.mjs index 618722d1f3..fb7056a97a 100644 --- a/harness/ruview/test/node-config.test.mjs +++ b/harness/ruview/test/node-config.test.mjs @@ -297,3 +297,108 @@ test('plan resolves every node and warns when OTA is not configured', () => { 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 }); } +}); From 578ca7b2232a6e20441717b9bfd25d2c738dd3a3 Mon Sep 17 00:00:00 2001 From: matthewxfz3 Date: Fri, 13 Nov 2026 14:03:56 -0800 Subject: [PATCH 03/10] fix(harness): plan reports node-ID conflicts; retired: releases a dead board's ID plan resolved against an empty registry, so it passed configs apply then refused, and a gone board's ID could only be freed by hand-editing devices.jsonl. Co-Authored-By: Claude Opus 5.5 --- firmware/esp32-csi-node/nodes.example.yaml | 5 +++ harness/ruview/.harness/manifest.json | 8 ++-- harness/ruview/.harness/manifest.sha256 | 2 +- harness/ruview/README.md | 4 +- harness/ruview/src/firmware.js | 3 +- harness/ruview/src/node-config.js | 48 ++++++++++++++++---- harness/ruview/test/firmware.test.mjs | 5 --- harness/ruview/test/node-config.test.mjs | 51 ++++++++++++++++++++++ 8 files changed, 104 insertions(+), 22 deletions(-) diff --git a/firmware/esp32-csi-node/nodes.example.yaml b/firmware/esp32-csi-node/nodes.example.yaml index d231efe86f..5420541415 100644 --- a/firmware/esp32-csi-node/nodes.example.yaml +++ b/firmware/esp32-csi-node/nodes.example.yaml @@ -42,6 +42,11 @@ defaults: # 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. diff --git a/harness/ruview/.harness/manifest.json b/harness/ruview/.harness/manifest.json index 6991c31241..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": "de24e91dc1570896d512cd69da60a3bedca86edb8a8bd5c081881f4533bbe861", + "README.md": "14ff0b70ea6b02e63d960a32523299a63e560d31b5fccd292220915e5a9ccf97", "bin/cli.js": "8ebf6ca4b6d9529a5e338374c19329c1a2fc2f9e52f13b4341928b7168ee461b", "brain/corpus/core.jsonl": "c0fb7b079ded157059b91601361429944697dae3cc42abc00dfe1a680986b0f4", "flywheel/evaluations.json": "ac4ff1f897a2444870cd2b8ae8aee8b1578e61467aeca4db57893f41be98a572", @@ -54,7 +54,7 @@ "src/devices/serial-pump.js": "86fd00708c7f74ca004eee1b729821aa10a70bfd394cc556dc0ec4b64b43b149", "src/doctor.js": "e5feccab91da3fdad16316ed7fd9090218a55b6de48772219ced96a930b87e75", "src/exec.js": "8889d13b8a8277f322856e743c79ac8f29b707251eaf9757c46d3f745743ae22", - "src/firmware.js": "92f95153df552e82a4e101d41c3fbf6405c2f28b6b394c065862572c1fa92b6f", + "src/firmware.js": "b0aa0a1e49a75072cb9d0422572152c6552a451e41df264e0489eda22a5d1c62", "src/guardrails.js": "aacc8fa6088f7f1ccea3a0b02171a5c516b95d3416ee3ba87add3879a1d6aaad", "src/guidance.js": "bd92e27280099246353926390df6913f2577f3811de48e16ac28b59253b55b1f", "src/hosts/claude-code.js": "2212bc39b49822018800dfe33a471e56bbb4c5233d716bfa7aa4fff77aa23edb", @@ -63,7 +63,7 @@ "src/kernel.js": "48dfd218a747dd158f82ae108e7e8ed89a82cd68649214d23266112dc9033f5d", "src/mcp-http.js": "b72e021ae00d01831ef7184db824f5e85a2b8747e1d8e994b6a1cb1cb4d08945", "src/mcp-server.js": "5eb8c3431768932c3a85476df032d0608fc7a732ede3d6bdcc8467ca47552666", - "src/node-config.js": "6f53ae40b9ff70a65a350c9379c993cf1227c0998fecc6fa86c3354521f7cab8", + "src/node-config.js": "7d76efae4d6214b8f635fb2d9c7efbec0520892b959b7bb035c3b1ad9d22f09e", "src/policy.js": "baad5eb8c3eb2e416e0a71b342ad850d7c8413d7d976571ebf596937f69da8e2", "src/process-runner.js": "49533b038044dfb8bc76ed01c030d06a9856ead0836157fb693e2a7d40f786d6", "src/redact.js": "ebf1afff46341078706b0401838c53db043603586e280d51ece5cf1feba35189", @@ -76,7 +76,7 @@ "src/training.js": "bd8a6c12eba4f9d2f8990fa9692cd04c743ea247f9b13e10f8717ceefcbd09ac", "src/ui/console-widget.js": "0d2f707d57b5ec54968dee1dc80f31c7db9152dcadfc48700c18379dabb55462" }, - "filesDigest": "5b527e8f22c5f082e5b61da1d0d6be54778ad40b02012fe3b878b6db5bd280cb", + "filesDigest": "06f83e927733f8964056cce94d0790c5631155e9198f31ecb5a9d50ae91cee54", "brainDigest": "c0fb7b079ded157059b91601361429944697dae3cc42abc00dfe1a680986b0f4", "gateFingerprint": "6e53c784eee38310188948fc75fb49e6b4ebc04e247d01b903fa8c8a92d67bdd", "developmentPins": { diff --git a/harness/ruview/.harness/manifest.sha256 b/harness/ruview/.harness/manifest.sha256 index f8f6f80439..dc31eaaa9a 100644 --- a/harness/ruview/.harness/manifest.sha256 +++ b/harness/ruview/.harness/manifest.sha256 @@ -1 +1 @@ -c4dba7eb06e89bc52b74a77e9a4fc0e7754c10130dcfba8f2fa9fe1d2f163cc5 manifest.json +a0dbdf2751dd2ec155c88e99ecb45436ebecd01c5e41eeb7933fd06c77f3128b manifest.json diff --git a/harness/ruview/README.md b/harness/ruview/README.md index eabcdfe103..f57fa9952f 100644 --- a/harness/ruview/README.md +++ b/harness/ruview/README.md @@ -162,7 +162,9 @@ ruview node apply --firmware-only --all --confirm # no questions (scripts); fl - **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`. + `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. diff --git a/harness/ruview/src/firmware.js b/harness/ruview/src/firmware.js index 124069de3b..2a82616f02 100644 --- a/harness/ruview/src/firmware.js +++ b/harness/ruview/src/firmware.js @@ -146,8 +146,7 @@ export function summarizeBootLog(text) { const lines = String(text).split(/\r?\n/); const csi = lines.filter((l) => /\bCSI cb #[1-9]\d*\b/.test(l)).length; const panic = lines.some((l) => /Guru Meditation|abort\(\) was called|Backtrace:|SPI flash busy detected|TG[01]_WDT/.test(l)); - // Banners: "esp32-csi-node v0.8.1", "RuView … 0.8.1", "ESP32-S3 CSI Node (ADR-018 / ADR-110) — v0.8.15". - const version = (/(?:RuView|esp32-csi-node|CSI Node)[^\n]*?v?(\d+\.\d+\.\d+)/i.exec(text) || [])[1] || null; + const version = (/(?:RuView|esp32-csi-node|ESP32(?:-[A-Z0-9]+)? CSI Node)[^\n]*?v?(\d+\.\d+\.\d+)/i.exec(text) || [])[1] || null; return { lines: lines.filter(Boolean).length, csiCallbacks: csi, diff --git a/harness/ruview/src/node-config.js b/harness/ruview/src/node-config.js index 0279af43d0..c387eed82c 100644 --- a/harness/ruview/src/node-config.js +++ b/harness/ruview/src/node-config.js @@ -162,7 +162,7 @@ const SECTIONS = Object.freeze({ 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', 'steps', 'auto_assign', 'registry']); +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); @@ -264,6 +264,17 @@ export function normalizeNodeConfig(doc, { home = homedir() } = {}) { }); } 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) { @@ -283,6 +294,7 @@ export function normalizeNodeConfig(doc, { home = homedir() } = {}) { 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, @@ -320,7 +332,9 @@ export function appendRegistry(path, event, fields = {}, now = () => new Date()) // 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. -function registryNodes(registry) { +// 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 @@ -328,13 +342,14 @@ function registryNodes(registry) { 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).map((node) => [node.mac, node])); + 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()]; } @@ -346,6 +361,7 @@ function effectiveNodes(config, registry) { 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'; } @@ -353,7 +369,7 @@ export function resolveNode(config, mac, registry = []) { 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).map((r) => r.node_id)]); + 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'; @@ -362,7 +378,7 @@ export function resolveNode(config, mac, registry = []) { } } 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 before applying`); + 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 }; @@ -472,12 +488,22 @@ export function planNodes(config, { repoRoot, nets } = {}) { 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) => { - const { node } = resolveNode(config, n.mac, []); - 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 }; + 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, + ok: !firmware.error && conflicts.length === 0, config: config.path, ssid: config.wifi.ssid, target: target ? `${target.ip}:${config.aggregator.port}` : null, @@ -488,6 +514,8 @@ export function planNodes(config, { repoRoot, nets } = {}) { auto_assign: config.auto_assign, registry: config.registry, nodes, + retired: config.retired, + conflicts, warnings, }; } @@ -688,7 +716,9 @@ export function formatNodes(kind, res) { 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]]); + 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]]); diff --git a/harness/ruview/test/firmware.test.mjs b/harness/ruview/test/firmware.test.mjs index c30ffe8a87..8c3cd58c3a 100644 --- a/harness/ruview/test/firmware.test.mjs +++ b/harness/ruview/test/firmware.test.mjs @@ -286,8 +286,3 @@ test('boot-log capture survives non-ASCII firmware logs on a cp1252 stdout', (t) rmSync(fake, { recursive: true, force: true }); } }); - -test('boot log version parsing covers the current CSI Node banner', () => { - assert.equal(summarizeBootLog('I (406) main: ESP32-S3 CSI Node (ADR-018 / ADR-110) \u2014 v0.8.15 \u2014 Node ID: 2\n').firmwareVersion, '0.8.15'); - assert.equal(summarizeBootLog('I (1) main: esp32-csi-node v0.8.1\n').firmwareVersion, '0.8.1'); -}); diff --git a/harness/ruview/test/node-config.test.mjs b/harness/ruview/test/node-config.test.mjs index fb7056a97a..6bef83a7ea 100644 --- a/harness/ruview/test/node-config.test.mjs +++ b/harness/ruview/test/node-config.test.mjs @@ -13,8 +13,11 @@ const EXAMPLE = fileURLToPath(new URL('../../../firmware/esp32-csi-node/nodes.ex 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, }); @@ -402,3 +405,51 @@ test('status uses an explicit reassignment without retaining the same MAC under 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 }); } +}); From 25d867fb45944d3c85a3291ff1c75a5da0a19301 Mon Sep 17 00:00:00 2001 From: Justin Wold Date: Thu, 19 Nov 2026 01:58:20 -0800 Subject: [PATCH 04/10] ci: fix three checks broken by new runner images Run the MQTT test broker from the eclipse-mosquitto:2.0.18 image, build Espressif QEMU on ubuntu-24.04, and enable git long paths before cargo on Windows. Plan item 0.6. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01T8hca9bTB4puaeBstXR9CE --- .github/workflows/firmware-qemu.yml | 8 +++++++- .github/workflows/mqtt-integration.yml | 27 ++++++++++++++++--------- .github/workflows/ruview-kernel-npm.yml | 7 +++++++ 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/.github/workflows/firmware-qemu.yml b/.github/workflows/firmware-qemu.yml index 1c295760c4..254d2afcf5 100644 --- a/.github/workflows/firmware-qemu.yml +++ b/.github/workflows/firmware-qemu.yml @@ -30,7 +30,13 @@ env: jobs: build-qemu: name: Build Espressif QEMU - runs-on: ubuntu-latest + # Pinned: on ubuntu-26.04 (GCC 15) the bundled dtc subproject fails with + # -Werror=discarded-qualifiers in libfdt/fdt_overlay.c, which QEMU's + # --disable-werror does not reach. The binary also runs inside the older + # espressif/idf container below, so building on the older image keeps + # its glibc requirement low. Revisit when Espressif's QEMU builds clean + # on the newer image. + runs-on: ubuntu-24.04 steps: - name: Cache QEMU build id: cache-qemu diff --git a/.github/workflows/mqtt-integration.yml b/.github/workflows/mqtt-integration.yml index 3a488dbe72..c7fcf85874 100644 --- a/.github/workflows/mqtt-integration.yml +++ b/.github/workflows/mqtt-integration.yml @@ -36,7 +36,7 @@ jobs: # 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 + # config file. We start the container manually in a step below with an # inline `allow_anonymous true` config. env: @@ -50,29 +50,36 @@ jobs: with: submodules: recursive - - name: Install mosquitto + clients and start with allow_anonymous + - name: Start mosquitto with allow_anonymous run: | - sudo apt-get update -qq - sudo apt-get install -y mosquitto mosquitto-clients - sudo systemctl stop mosquitto || true + # Run the pinned upstream image instead of the distro package. On + # ubuntu-26.04 runners the packaged mosquitto 2.0.22 exits with + # "Unable to open config file /tmp/mosquitto-ci.conf" before + # listening, and the apt install alone took over five minutes. # 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' + cat > "$RUNNER_TEMP/mosquitto-ci.conf" <<'EOF' listener 11883 allow_anonymous true persistence false log_dest stdout EOF - mosquitto -c /tmp/mosquitto-ci.conf -d + docker run -d --name mosquitto-ci -p 127.0.0.1:11883:11883 \ + -v "$RUNNER_TEMP/mosquitto-ci.conf:/mosquitto/config/mosquitto.conf:ro" \ + eclipse-mosquitto:2.0.18 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 + # The first check proves the broker accepts an anonymous + # publish; the second proves the port is reachable from the + # host, where the tests run. + if docker exec mosquitto-ci mosquitto_pub -h 127.0.0.1 -p 11883 -t healthcheck -m ok -q 0 2>/dev/null \ + && (exec 3<>/dev/tcp/127.0.0.1/11883) 2>/dev/null; then echo "mosquitto reachable on 11883"; exit 0 fi sleep 2 done echo "mosquitto never became reachable" >&2 - tail -50 /var/log/mosquitto/*.log 2>/dev/null || true + docker logs mosquitto-ci || true exit 1 - name: Install Rust toolchain @@ -115,4 +122,4 @@ jobs: if: failure() run: | docker ps -a - docker logs $(docker ps -aqf "ancestor=eclipse-mosquitto:2.0.18") || true + docker logs mosquitto-ci || 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 From 6bff4df903e4475dc08120b8d2d58b5aeab50005 Mon Sep 17 00:00:00 2001 From: Dragan Spiridonov Date: Thu, 19 Nov 2026 13:26:51 +0100 Subject: [PATCH 05/10] fix(ci): reuse pinned QEMU runtime and non-root broker bootstrap --- .github/workflows/firmware-qemu.yml | 104 ++++++++++--------------- .github/workflows/mqtt-integration.yml | 60 ++++++-------- 2 files changed, 66 insertions(+), 98 deletions(-) diff --git a/.github/workflows/firmware-qemu.yml b/.github/workflows/firmware-qemu.yml index 254d2afcf5..d792ed9ab6 100644 --- a/.github/workflows/firmware-qemu.yml +++ b/.github/workflows/firmware-qemu.yml @@ -22,55 +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 - # Pinned: on ubuntu-26.04 (GCC 15) the bundled dtc subproject fails with - # -Werror=discarded-qualifiers in libfdt/fdt_overlay.c, which QEMU's - # --disable-werror does not reach. The binary also runs inside the older - # espressif/idf container below, so building on the older image keeps - # its glibc requirement low. Revisit when Espressif's QEMU builds clean - # on the newer image. 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' - 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' + - name: Prepare SDK-provided Espressif QEMU 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: | @@ -90,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 @@ -123,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 }} @@ -150,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 \ @@ -164,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"; } @@ -219,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 @@ -305,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 # --------------------------------------------------------------------------- @@ -325,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 @@ -343,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 \ @@ -362,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 c7fcf85874..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 the container 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,36 +47,27 @@ jobs: with: submodules: recursive - - name: Start mosquitto with allow_anonymous + - name: Start pinned Mosquitto broker with CI-only anonymous listener run: | - # Run the pinned upstream image instead of the distro package. On - # ubuntu-26.04 runners the packaged mosquitto 2.0.22 exits with - # "Unable to open config file /tmp/mosquitto-ci.conf" before - # listening, and the apt install alone took over five minutes. - # 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 > "$RUNNER_TEMP/mosquitto-ci.conf" <<'EOF' - listener 11883 + cat > "$RUNNER_TEMP/mosquitto-ci.conf" <<'CONFIG' + listener 1883 allow_anonymous true persistence false log_dest stdout - EOF - docker run -d --name mosquitto-ci -p 127.0.0.1:11883:11883 \ - -v "$RUNNER_TEMP/mosquitto-ci.conf:/mosquitto/config/mosquitto.conf:ro" \ - eclipse-mosquitto:2.0.18 + 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 - # The first check proves the broker accepts an anonymous - # publish; the second proves the port is reachable from the - # host, where the tests run. - if docker exec mosquitto-ci mosquitto_pub -h 127.0.0.1 -p 11883 -t healthcheck -m ok -q 0 2>/dev/null \ - && (exec 3<>/dev/tcp/127.0.0.1/11883) 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 - docker logs mosquitto-ci || true + docker logs ruview-mqtt-ci exit 1 - name: Install Rust toolchain @@ -97,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 @@ -121,5 +106,8 @@ jobs: - name: Dump broker logs on failure if: failure() run: | - docker ps -a - docker logs mosquitto-ci || true + docker logs ruview-mqtt-ci || true + + - name: Stop the owned CI broker + if: always() + run: docker rm --force ruview-mqtt-ci From 73640844e0b8acff47c9a75bb5d9a7b6cf9794a9 Mon Sep 17 00:00:00 2001 From: Dragan Spiridonov Date: Thu, 19 Nov 2026 13:30:59 +0100 Subject: [PATCH 06/10] fix(ci): use native path expectations and account for node-config package footprint --- .github/workflows/npm-packages.yml | 6 ++++-- harness/ruview/test/node-config.test.mjs | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) 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/harness/ruview/test/node-config.test.mjs b/harness/ruview/test/node-config.test.mjs index 6bef83a7ea..0693aa0bf9 100644 --- a/harness/ruview/test/node-config.test.mjs +++ b/harness/ruview/test/node-config.test.mjs @@ -82,7 +82,7 @@ test('the shipped nodes.example.yaml parses and validates', () => { 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, '/home/u/p'); + 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 }); From b53e2ec27f5957767d16fc083185b5ef9e3a9fd3 Mon Sep 17 00:00:00 2001 From: Dragan Spiridonov Date: Thu, 19 Nov 2026 14:36:13 +0100 Subject: [PATCH 07/10] fix(security): patch Wasmtime36 and verify exact advisory range corrections --- .github/workflows/ci.yml | 3 +- .github/workflows/security-scan.yml | 11 +- docs/security/wasmtime-audit-20261119.md | 80 ++++++++ scripts/ci/audit_rust_advisories.py | 175 ++++++++++++++++++ .../wasmtime-primary-scopes-20261119.json | 60 ++++++ .../wasmtime-rustsec-metadata-20261119.json | 68 +++++++ scripts/ci/test_audit_rust_advisories.py | 125 +++++++++++++ v2/Cargo.lock | 132 ++++++------- v2/Cargo.toml | 4 +- v2/crates/homecore-plugins/Cargo.toml | 2 +- .../tests/wasmtime_advisory_regression.rs | 23 +++ .../RUVIEW-PROVENANCE.md | 13 -- .../Cargo.toml | 2 +- .../LICENSE | 0 .../RUVIEW-PROVENANCE.md | 13 ++ .../build.rs | 0 .../src/config.rs | 0 .../src/config/tests.rs | 0 .../src/lib.rs | 0 .../src/tests.rs | 0 .../src/worker.rs | 0 .../src/worker/tests.rs | 0 .../src/worker/tests/system_time_stub.rs | 0 .../tests/cache_write_default_config.rs | 0 24 files changed, 624 insertions(+), 87 deletions(-) create mode 100644 docs/security/wasmtime-audit-20261119.md create mode 100644 scripts/ci/audit_rust_advisories.py create mode 100644 scripts/ci/fixtures/wasmtime-primary-scopes-20261119.json create mode 100644 scripts/ci/fixtures/wasmtime-rustsec-metadata-20261119.json create mode 100644 scripts/ci/test_audit_rust_advisories.py create mode 100644 v2/crates/homecore-plugins/tests/wasmtime_advisory_regression.rs delete mode 100644 v2/vendor/security/wasmtime-internal-cache-36.0.20/RUVIEW-PROVENANCE.md rename v2/vendor/security/{wasmtime-internal-cache-36.0.20 => wasmtime-internal-cache-36.0.21}/Cargo.toml (99%) rename v2/vendor/security/{wasmtime-internal-cache-36.0.20 => wasmtime-internal-cache-36.0.21}/LICENSE (100%) create mode 100644 v2/vendor/security/wasmtime-internal-cache-36.0.21/RUVIEW-PROVENANCE.md rename v2/vendor/security/{wasmtime-internal-cache-36.0.20 => wasmtime-internal-cache-36.0.21}/build.rs (100%) rename v2/vendor/security/{wasmtime-internal-cache-36.0.20 => wasmtime-internal-cache-36.0.21}/src/config.rs (100%) rename v2/vendor/security/{wasmtime-internal-cache-36.0.20 => wasmtime-internal-cache-36.0.21}/src/config/tests.rs (100%) rename v2/vendor/security/{wasmtime-internal-cache-36.0.20 => wasmtime-internal-cache-36.0.21}/src/lib.rs (100%) rename v2/vendor/security/{wasmtime-internal-cache-36.0.20 => wasmtime-internal-cache-36.0.21}/src/tests.rs (100%) rename v2/vendor/security/{wasmtime-internal-cache-36.0.20 => wasmtime-internal-cache-36.0.21}/src/worker.rs (100%) rename v2/vendor/security/{wasmtime-internal-cache-36.0.20 => wasmtime-internal-cache-36.0.21}/src/worker/tests.rs (100%) rename v2/vendor/security/{wasmtime-internal-cache-36.0.20 => wasmtime-internal-cache-36.0.21}/src/worker/tests/system_time_stub.rs (100%) rename v2/vendor/security/{wasmtime-internal-cache-36.0.20 => wasmtime-internal-cache-36.0.21}/tests/cache_write_default_config.rs (100%) 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/security-scan.yml b/.github/workflows/security-scan.yml index 4b406ed775..4cdd250c73 100644 --- a/.github/workflows/security-scan.yml +++ b/.github/workflows/security-scan.yml @@ -36,16 +36,21 @@ jobs: # reviewed ignores in v2/.cargo/audit.toml. Run inside the v2 workspace # so that policy file is actually loaded. working-directory: v2 + env: + GH_TOKEN: ${{ github.token }} run: | - set -o pipefail - cargo audit --file Cargo.lock --json | tee cargo-audit.json + python3 ../scripts/ci/test_audit_rust_advisories.py + python3 ../scripts/ci/audit_rust_advisories.py --lockfile Cargo.lock - name: Upload Rust advisory report uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 if: always() with: name: cargo-audit-report - path: v2/cargo-audit.json + path: | + v2/cargo-audit.json + v2/cargo-audit-classification.json + v2/cargo-audit-upstream.json if-no-files-found: error # Static Application Security Testing (SAST) diff --git a/docs/security/wasmtime-audit-20261119.md b/docs/security/wasmtime-audit-20261119.md new file mode 100644 index 0000000000..24b7eed33b --- /dev/null +++ b/docs/security/wasmtime-audit-20261119.md @@ -0,0 +1,80 @@ +# 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. + +## Why native cargo-audit still reports two matches + +With36.0.21, the native report drops from five to two. Its remaining0567/0573 +entries omit the upstream lower bounds and therefore incorrectly match36.x. +The two wrongly named WASI entries stop matching after the patch; no special +coverage for those entries is implemented. + +The existing security workflow uses audit_rust_advisories.py to retain the exact +native report and reconcile only these two reviewed range errors. This is not a +native audit-clean result: native exit1/two findings remain recorded separately +from zero unresolved findings after verified metadata reconciliation. + +A correction applies only when ALL of the following hold: + +- The RustSec selector fingerprint matches the checked-in ID/package/aliases/URL, + affected-function/platform selectors and patched/unaffected ranges. +- The finding is registry core wasmtime exactly36.0.21, with its checksum matching + both the verified registry archive and current Cargo.lock. Other versions, sources or checksums are not covered. +- Neither wasmtime-wasi nor wasmtime-wasi-http appears in the reviewed lockfile. + A different graph requires reassessment; those packages cannot gain coverage. +- A fresh read of the fixed official GitHub repository advisory endpoint matches + the reviewed primary package/version-range fingerprint. Network failure, + changed identity/ranges or missing evidence cannot fall back to the fixture. +- The report's existing ignore settings match the unchanged local audit policy. + +All unknown findings remain unresolved, including the version-applicable0569 if +it reappears. Malformed or incomplete reports fail closed. Fingerprints deliberately +cover scope selectors, not cosmetic prose. The primary scopes are checked live; +fixtures are used only by offline tests and never as a successful CI fallback. + +## Evidence and operating boundary + +The job retains cargo-audit.json (unmodified native output), +cargo-audit-classification.json (including native exit and unresolved findings), +and cargo-audit-upstream.json (live scopes and retrieval time). The existing +read-only GitHub Actions token is used by gh to read public published advisories; +no extra token, permission, secret or write operation is introduced. + +Offline negative tests exercise unknown advisories, changed RustSec/primary +selectors, affected/future versions, wrong registry/checksum, missing lock records, +WASI additions, policy injection, malformed reports, unreviewed network IDs and +live-provider failure. Existing ignores remain explicit; no blanket ignore, +severity-threshold change, raw report rewrite or advisory database edit is made. + +Native audit reproduction on snapshot7133866a after the patch reports two entries; +live primary verification classifies those two scope mismatches and leaves zero +unresolved entries. Remove this reconciliation when RustSec's ranges are corrected. +A future version or altered scope must be reassessed, not silently covered. + +No plugin is enabled, helper replaced, application installed, physical sensor +qualified or production configuration changed by this source repair. Merge/release +remain the maintainers' separate decisions. diff --git a/scripts/ci/audit_rust_advisories.py b/scripts/ci/audit_rust_advisories.py new file mode 100644 index 0000000000..78fa10b987 --- /dev/null +++ b/scripts/ci/audit_rust_advisories.py @@ -0,0 +1,175 @@ +#!/usr/bin/env python3 +"""Preserve cargo-audit output and reconcile two reviewed upstream range errors. + +This is not an advisory ignore list. Only the exact reviewed RustSec selectors, +registry Wasmtime 36.0.21, matching lockfile checksum and absent WASI packages can +qualify. Corrected/new metadata or other versions remain unresolved. See the +docs/security/wasmtime-audit-20261119.md assessment for authoritative package/range evidence. +""" +import argparse +import datetime +import hashlib +import json +from pathlib import Path +import subprocess +import sys +import tomllib + +REGISTRY = "registry+https://github.com/rust-lang/crates.io-index" +WASMTIME_ARCHIVE_SHA256 = "c58d2cbba3817454af39ec9f0b39279870a9a98ef4c52bbbcdd9c1ffc1f5f17f" +REVIEWED = { + "RUSTSEC-2026-0567": "99da727f7424b8b1f70c570e7347f7c2ad1d23ac7f7d1534654324e12297ee4c", + "RUSTSEC-2026-0573": "1716fc0fdcf2acae357882632f7ee3a3b6551116e6c18ba32f2f79ddf140ad08", +} + +PRIMARY = { + 'RUSTSEC-2026-0567': ('GHSA-2674-w29h-wqvj', + 'fe62715ac8a8ae324420af60d2ba36d55be25ede888c045cdfdbf5b18ffbd9ca'), + 'RUSTSEC-2026-0573': ('GHSA-wvgq-g5hf-6gm6', + '4a1af44d4f625d4efdf6585593ec47fb73e89fc162fe1ba525090ca9d43f5b70'), +} + + +def primary_scope(document): + if not isinstance(document, dict) or not isinstance(document.get("vulnerabilities"), list): + raise ValueError("invalid upstream advisory metadata") + values = [] + for value in document["vulnerabilities"]: + package = value["package"] + values.append({"package": {"name": package["name"], "ecosystem": package["ecosystem"].lower()}, + "vulnerable_version_range": "".join(value["vulnerable_version_range"].split()), + "patched_versions": "".join(value["patched_versions"].split())}) + values.sort(key=lambda value: json.dumps(value, sort_keys=True)) + return {"ghsa_id": document["ghsa_id"], "vulnerabilities": values} + + +def primary_digest(document): + return hashlib.sha256(json.dumps(primary_scope(document), sort_keys=True, separators=(",", ":")).encode()).hexdigest() + + +def fetch_primary(ghsa): + # IDs come only from PRIMARY constants, never from an untrusted report URL. + if ghsa not in {value[0] for value in PRIMARY.values()}: + raise ValueError("unreviewed upstream advisory identifier") + completed = subprocess.run(["gh", "api", "--hostname", "github.com", + "repos/bytecodealliance/wasmtime/security-advisories/" + ghsa], + capture_output=True, text=True, timeout=45) + if completed.returncode != 0 or len(completed.stdout) > 512 * 1024: + raise ValueError(f"live upstream verification failed for {ghsa} (gh exit {completed.returncode}); no offline fallback") + scope = primary_scope(json.loads(completed.stdout)) + if scope["ghsa_id"] != ghsa: + raise ValueError("upstream advisory identity changed") + return scope + + +def selector_digest(finding): + advisory = finding.get("advisory", {}) + selectors = {key: finding.get(key) for key in ("affected", "versions")} + selectors["advisory"] = {key: advisory.get(key) for key in ("id", "package", "aliases", "url")} + return hashlib.sha256(json.dumps(selectors, sort_keys=True, separators=(",", ":")).encode()).hexdigest() + + +def metadata_mismatch(finding, packages, primary): + advisory = finding.get("advisory", {}) + expected = REVIEWED.get(advisory.get("id")) + if expected is None or selector_digest(finding) != expected: + return False + ghsa, expected_primary = PRIMARY[advisory["id"]] + if ghsa not in primary or primary_digest(primary[ghsa]) != expected_primary: + return False + package = finding.get("package", {}) + if (package.get("name"), package.get("version"), package.get("source")) != ("wasmtime", "36.0.21", REGISTRY): + return False + checksum = package.get("checksum") + if checksum != WASMTIME_ARCHIVE_SHA256: + return False + identity = ("name", "version", "source", "checksum") + if not any(all(p.get(key) == package.get(key) for key in identity) for p in packages): + return False + if any(p.get("name") in {"wasmtime-wasi", "wasmtime-wasi-http"} for p in packages): + return False + return True + + +def classify(report, lock, ignored=(), primary=None): + primary = primary or {} + if not isinstance(report, dict) or report.get("error") or not isinstance(lock.get("package"), list): + raise ValueError("invalid cargo-audit report or lockfile") + vulnerability = report.get("vulnerabilities", {}) + findings = vulnerability.get("list") + count = vulnerability.get("count") + if (not isinstance(findings, list) or type(count) is not int or count < 0 + or count != len(findings) or type(vulnerability.get("found")) is not bool + or vulnerability["found"] != bool(findings)): + raise ValueError("cargo-audit omitted or malformed vulnerability findings") + configured = report.get("settings", {}).get("ignore", []) + if not isinstance(configured, list) or set(configured) != set(ignored): + raise ValueError("cargo-audit policy does not match the existing local policy") + result = {"raw_vulnerability_count": count, "metadata_corrections": [], "existing_policy_ignored": [], "unresolved": []} + for finding in findings: + if not isinstance(finding, dict) or not isinstance(finding.get("advisory"), dict): + raise ValueError("invalid advisory finding") + advisory_id = finding["advisory"].get("id") + if advisory_id in ignored: + result["existing_policy_ignored"].append(advisory_id) + elif metadata_mismatch(finding, lock["package"], primary): + result["metadata_corrections"].append({"id": advisory_id, "package": "wasmtime", "version": "36.0.21", "selector_sha256": REVIEWED[advisory_id], "upstream_advisory": PRIMARY[advisory_id][0], "upstream_scope_sha256": PRIMARY[advisory_id][1]}) + else: + result["unresolved"].append(finding) + return result + + +def decision(native_exit, result, policy): + if native_exit not in (0, 1): + raise ValueError("cargo-audit execution failed") + if native_exit == 1 and result["raw_vulnerability_count"] == 0: + raise ValueError("unexplained native failure without vulnerability findings") + # Never turn an explicitly denied native warning into success. + denied_output = bool(policy.get("output", {}).get("deny", [])) + return 1 if result["unresolved"] or (native_exit == 1 and denied_output) else 0 + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--lockfile", type=Path, default=Path("Cargo.lock")) + parser.add_argument("--raw-output", type=Path, default=Path("cargo-audit.json")) + parser.add_argument("--classified-output", type=Path, default=Path("cargo-audit-classification.json")) + parser.add_argument("--upstream-output", type=Path, default=Path("cargo-audit-upstream.json")) + parser.add_argument("--database", type=Path, help="explicit local advisory snapshot for reproducible local verification") + args = parser.parse_args() + lockfile = args.lockfile.resolve() + lock = tomllib.loads(lockfile.read_text()) + policy_file = lockfile.parent / ".cargo/audit.toml" + policy = tomllib.loads(policy_file.read_text()) if policy_file.exists() else {} + ignored = policy.get("advisories", {}).get("ignore", []) + command = ["cargo", "audit", "--file", str(lockfile), "--json"] + if args.database is not None: + command += ["--db", str(args.database.resolve()), "--no-fetch"] + completed = subprocess.run(command, cwd=lockfile.parent, capture_output=True, text=True, timeout=300) + args.raw_output.write_text(completed.stdout) + sys.stdout.write(completed.stdout) + sys.stderr.write(completed.stderr) + if completed.returncode not in (0, 1) or len(completed.stdout) > 16 * 1024 * 1024: + raise ValueError("cargo-audit execution failed or returned an oversized report") + report = json.loads(completed.stdout) + primary = {} + for finding in report.get("vulnerabilities", {}).get("list", []): + advisory_id = finding.get("advisory", {}).get("id") + if advisory_id in PRIMARY and advisory_id not in ignored: + ghsa = PRIMARY[advisory_id][0] + primary[ghsa] = fetch_primary(ghsa) + args.upstream_output.write_text(json.dumps({"retrieved_at": datetime.datetime.now(datetime.timezone.utc).isoformat(), "scopes": primary}, indent=2) + "\n") + result = classify(report, lock, ignored, primary) + exit_code = decision(completed.returncode, result, policy) + result["native_exit_code"] = completed.returncode + args.classified_output.write_text(json.dumps(result, indent=2) + "\n") + print(json.dumps({key: value for key, value in result.items() if key != "unresolved"} | {"unresolved_count": len(result["unresolved"])})) + return exit_code + + +if __name__ == "__main__": + try: + sys.exit(main()) + except (ValueError, OSError, subprocess.SubprocessError) as error: + print(f"RUST AUDIT FAILED: {error}", file=sys.stderr) + sys.exit(2) diff --git a/scripts/ci/fixtures/wasmtime-primary-scopes-20261119.json b/scripts/ci/fixtures/wasmtime-primary-scopes-20261119.json new file mode 100644 index 0000000000..1aae735d11 --- /dev/null +++ b/scripts/ci/fixtures/wasmtime-primary-scopes-20261119.json @@ -0,0 +1,60 @@ +{ + "GHSA-2674-w29h-wqvj": { + "ghsa_id": "GHSA-2674-w29h-wqvj", + "vulnerabilities": [ + { + "package": { + "name": "wasmtime", + "ecosystem": "rust" + }, + "vulnerable_version_range": ">=39.0.0,<48.0.9", + "patched_versions": "48.0.9" + }, + { + "package": { + "name": "wasmtime", + "ecosystem": "rust" + }, + "vulnerable_version_range": ">=49.0.0,<49.0.6", + "patched_versions": "49.0.6" + }, + { + "package": { + "name": "wasmtime", + "ecosystem": "rust" + }, + "vulnerable_version_range": ">=50.0.0,<50.0.2", + "patched_versions": "50.0.2" + } + ] + }, + "GHSA-wvgq-g5hf-6gm6": { + "ghsa_id": "GHSA-wvgq-g5hf-6gm6", + "vulnerabilities": [ + { + "package": { + "name": "wasmtime", + "ecosystem": "rust" + }, + "vulnerable_version_range": ">=47.0.0,<=48.0.8", + "patched_versions": "48.0.9" + }, + { + "package": { + "name": "wasmtime", + "ecosystem": "rust" + }, + "vulnerable_version_range": ">=49.0.0,<=49.0.5", + "patched_versions": "49.0.6" + }, + { + "package": { + "name": "wasmtime", + "ecosystem": "rust" + }, + "vulnerable_version_range": ">=50.0.0,<=50.0.1", + "patched_versions": "50.0.2" + } + ] + } +} diff --git a/scripts/ci/fixtures/wasmtime-rustsec-metadata-20261119.json b/scripts/ci/fixtures/wasmtime-rustsec-metadata-20261119.json new file mode 100644 index 0000000000..b328e152e3 --- /dev/null +++ b/scripts/ci/fixtures/wasmtime-rustsec-metadata-20261119.json @@ -0,0 +1,68 @@ +[ + { + "advisory": { + "id": "RUSTSEC-2026-0567", + "package": "wasmtime", + "title": "Abort in Wasmtime builds without the `threads` cargo feature", + "description": "This is an entry in the RustSec database for the Wasmtime security advisory\nlocated at\nhttps://github.com/bytecodealliance/wasmtime/security/advisories/GHSA-2674-w29h-wqvj\nFor more information see the GitHub-hosted security advisory.", + "date": "2026-11-16", + "aliases": [ + "GHSA-2674-w29h-wqvj" + ], + "related": [], + "collection": "crates", + "categories": [], + "keywords": [], + "cvss": null, + "informational": null, + "references": [], + "source": null, + "url": "https://github.com/bytecodealliance/wasmtime/pull/15314", + "withdrawn": null, + "license": "CC0-1.0", + "expect-deleted": false + }, + "versions": { + "patched": [ + ">=48.0.9, <49.0.0", + ">=49.0.6, <50.0.0", + ">=50.0.2" + ], + "unaffected": [] + }, + "affected": null + }, + { + "advisory": { + "id": "RUSTSEC-2026-0573", + "package": "wasmtime", + "title": "Process abort when a GC heap's reservation isn't a multiple of the page size", + "description": "This is an entry in the RustSec database for the Wasmtime security advisory\nlocated at\nhttps://github.com/bytecodealliance/wasmtime/security/advisories/GHSA-wvgq-g5hf-6gm6\nFor more information see the GitHub-hosted security advisory.", + "date": "2026-11-16", + "aliases": [ + "GHSA-wvgq-g5hf-6gm6" + ], + "related": [], + "collection": "crates", + "categories": [], + "keywords": [], + "cvss": null, + "informational": null, + "references": [], + "source": null, + "url": "https://github.com/bytecodealliance/wasmtime/pull/15320", + "withdrawn": null, + "license": "CC0-1.0", + "expect-deleted": false + }, + "versions": { + "patched": [ + ">=48.0.9, <49.0.0", + ">=49.0.6, <50.0.0", + ">=50.0.2" + ], + "unaffected": [] + }, + "affected": null + } +] diff --git a/scripts/ci/test_audit_rust_advisories.py b/scripts/ci/test_audit_rust_advisories.py new file mode 100644 index 0000000000..ba6304110b --- /dev/null +++ b/scripts/ci/test_audit_rust_advisories.py @@ -0,0 +1,125 @@ +import copy +import json +from pathlib import Path +import unittest +from unittest import mock +from audit_rust_advisories import classify as classify_report, REGISTRY, fetch_primary, decision, WASMTIME_ARCHIVE_SHA256 + +def classify(report, lock, ignored=(), primary=None): + if primary is None: + primary = json.loads((Path(__file__).parent / "fixtures/wasmtime-primary-scopes-20261119.json").read_text()) + return classify_report(report, lock, ignored, primary) + + +class AuditMetadataTests(unittest.TestCase): + def setUp(self): + self.findings = json.loads((Path(__file__).parent / "fixtures/wasmtime-rustsec-metadata-20261119.json").read_text()) + self.package = {"name": "wasmtime", "version": "36.0.21", "source": REGISTRY, "checksum": WASMTIME_ARCHIVE_SHA256} + for finding in self.findings: + finding["package"] = self.package.copy() + self.lock = {"package": [self.package.copy()]} + + def report(self, findings=None): + findings = self.findings if findings is None else findings + return {"vulnerabilities": {"found": bool(findings), "count": len(findings), "list": findings}, "settings": {"ignore": []}} + + def test_exact_reviewed_false_matches_are_reported_separately(self): + result = classify(self.report(), self.lock) + self.assertEqual(len(result["metadata_corrections"]), 2) + self.assertEqual(result["raw_vulnerability_count"], 2) + self.assertEqual(result["unresolved"], []) + + def test_old_or_future_affected_versions_cannot_use_the_correction(self): + for version in ("36.0.20", "36.0.22", "39.0.0", "47.0.0", "50.0.1"): + with self.subTest(version=version): + findings = copy.deepcopy(self.findings) + for finding in findings: + finding["package"]["version"] = version + lock = {"package": [{**self.package, "version": version}]} + self.assertEqual(len(classify(self.report(findings), lock)["unresolved"]), 2) + + def test_wasi_presence_prevents_wrong_package_coverage(self): + for name in ("wasmtime-wasi", "wasmtime-wasi-http"): + with self.subTest(package=name): + lock = {"package": [self.package.copy(), {"name": name, "version": "36.0.20"}]} + self.assertEqual(len(classify(self.report(), lock)["unresolved"]), 2) + + def test_actual_wasi_findings_remain_unresolved(self): + findings = copy.deepcopy(self.findings) + findings[0]["package"]["name"] = "wasmtime-wasi" + self.assertEqual(len(classify(self.report(findings), self.lock)["unresolved"]), 1) + + def test_changed_advisory_selectors_fail_closed(self): + for change in ("versions", "affected", "aliases", "url"): + findings = copy.deepcopy(self.findings) + if change == "versions": findings[0]["versions"]["patched"] = [">=99.0.0"] + elif change == "affected": findings[0]["affected"] = {"os": ["macos"]} + elif change == "aliases": findings[0]["advisory"]["aliases"] = ["GHSA-new-advisory"] + else: findings[0]["advisory"]["url"] = "https://example.invalid/unreviewed" + self.assertEqual(len(classify(self.report(findings), self.lock)["unresolved"]), 1) + + def test_unknown_or_version_applicable_advisory_is_not_covered(self): + for advisory in ("RUSTSEC-2099-0001", "RUSTSEC-2026-0569"): + findings = copy.deepcopy(self.findings) + findings[0]["advisory"]["id"] = advisory + self.assertEqual(len(classify(self.report(findings), self.lock)["unresolved"]), 1) + + def test_registry_identity_and_checksum_must_match_the_lockfile(self): + for change in ("checksum", "source"): + findings = copy.deepcopy(self.findings) + findings[0]["package"][change] = "unreviewed" + self.assertEqual(len(classify(self.report(findings), self.lock)["unresolved"]), 1) + self.assertEqual(len(classify(self.report(), {"package": []})["unresolved"]), 2) + + def test_coordinated_report_and_lock_checksum_tamper_is_rejected(self): + findings = copy.deepcopy(self.findings) + for finding in findings: finding["package"]["checksum"] = "b" * 64 + lock = {"package": [{**self.package, "checksum": "b" * 64}]} + self.assertEqual(len(classify(self.report(findings), lock)["unresolved"]), 2) + + def test_malformed_or_omitted_findings_fail_closed(self): + for report in ({}, {"vulnerabilities": {"found": False, "count": 0, "list": self.findings}}): + with self.assertRaises(ValueError): classify(report, self.lock) + + def test_existing_policy_is_honored_but_cannot_be_broadened_by_report(self): + findings = copy.deepcopy(self.findings) + findings[0]["advisory"]["id"] = "RUSTSEC-2099-0001" + report = self.report(findings) + report["settings"]["ignore"] = ["RUSTSEC-2099-0001"] + with self.assertRaises(ValueError): classify(report, self.lock) + result = classify(report, self.lock, ["RUSTSEC-2099-0001"]) + self.assertEqual(result["unresolved"], []) + self.assertEqual(result["existing_policy_ignored"], ["RUSTSEC-2099-0001"]) + + def test_missing_primary_evidence_does_not_cover_any_finding(self): + self.assertEqual(len(classify(self.report(), self.lock, primary={})["unresolved"]), 2) + + def test_changed_primary_package_or_range_revokes_coverage(self): + for field in ("package", "vulnerable_version_range"): + primary = json.loads((Path(__file__).parent / "fixtures/wasmtime-primary-scopes-20261119.json").read_text()) + target = primary["GHSA-2674-w29h-wqvj"]["vulnerabilities"][0] + if field == "package": target["package"]["name"] = "other-package" + else: target[field] = ">=36.0.0,<48.0.9" + self.assertEqual(len(classify(self.report(), self.lock, primary=primary)["unresolved"]), 1) + + def test_unknown_advisory_cannot_choose_a_network_location(self): + with mock.patch("audit_rust_advisories.subprocess.run") as execute: + with self.assertRaises(ValueError): fetch_primary("https://untrusted.invalid/advisory") + execute.assert_not_called() + + def test_live_primary_failure_has_no_offline_success_fallback(self): + with mock.patch("audit_rust_advisories.subprocess.run", return_value=mock.Mock(returncode=1, stdout="")): + with self.assertRaises(ValueError): fetch_primary("GHSA-2674-w29h-wqvj") + + def test_native_warning_denial_cannot_be_overridden(self): + result = classify(self.report(), self.lock) + self.assertEqual(decision(1, result, {"output": {"deny": ["warnings"]}}), 1) + self.assertEqual(decision(1, result, {}), 0) + with self.assertRaises(ValueError): decision(1, classify(self.report([]), self.lock), {}) + + def test_native_clean_report_remains_clean(self): + self.assertEqual(classify(self.report([]), self.lock)["unresolved"], []) + + +if __name__ == "__main__": + unittest.main() 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 From 2dd4eab1f97374f8c6c56d3b646d4153c925c87d Mon Sep 17 00:00:00 2001 From: Dragan Spiridonov Date: Thu, 19 Nov 2026 15:15:16 +0100 Subject: [PATCH 08/10] fix(security): invoke auditor directly and reject CLI argument overrides --- docs/security/wasmtime-audit-20261119.md | 11 ++++++ scripts/ci/audit_rust_advisories.py | 10 +++--- scripts/ci/test_audit_rust_advisories.py | 43 ++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/docs/security/wasmtime-audit-20261119.md b/docs/security/wasmtime-audit-20261119.md index 24b7eed33b..277abef3a6 100644 --- a/docs/security/wasmtime-audit-20261119.md +++ b/docs/security/wasmtime-audit-20261119.md @@ -78,3 +78,14 @@ A future version or altered scope must be reassessed, not silently covered. No plugin is enabled, helper replaced, application installed, physical sensor qualified or production configuration changed by this source repair. Merge/release remain the maintainers' separate decisions. + +The CLI invokes cargo-audit directly with a fixed argument vector and shell=False. +It requires a standard Cargo.lock basename and has no alternate-database switch; +Cargo aliases and CLI-selected advisory arguments do not enter the process command. +Local pinned-snapshot evidence is reproduced by calling the native tool directly, +then exercising the pure classifier with fresh upstream evidence. + +A controlled negative probe confirmed the previous Cargo dispatcher could execute +a project-defined audit alias and accept an artificial clean result. The direct +auditor ignored that alias. This is a negative-control fixture, not a real clean +audit or a claim that any repository alias was used maliciously. diff --git a/scripts/ci/audit_rust_advisories.py b/scripts/ci/audit_rust_advisories.py index 78fa10b987..afa8ecd751 100644 --- a/scripts/ci/audit_rust_advisories.py +++ b/scripts/ci/audit_rust_advisories.py @@ -135,17 +135,17 @@ def main(): parser.add_argument("--raw-output", type=Path, default=Path("cargo-audit.json")) parser.add_argument("--classified-output", type=Path, default=Path("cargo-audit-classification.json")) parser.add_argument("--upstream-output", type=Path, default=Path("cargo-audit-upstream.json")) - parser.add_argument("--database", type=Path, help="explicit local advisory snapshot for reproducible local verification") args = parser.parse_args() lockfile = args.lockfile.resolve() + if lockfile.name != "Cargo.lock": + raise ValueError("the workspace audit requires a standard Cargo.lock filename") lock = tomllib.loads(lockfile.read_text()) policy_file = lockfile.parent / ".cargo/audit.toml" policy = tomllib.loads(policy_file.read_text()) if policy_file.exists() else {} ignored = policy.get("advisories", {}).get("ignore", []) - command = ["cargo", "audit", "--file", str(lockfile), "--json"] - if args.database is not None: - command += ["--db", str(args.database.resolve()), "--no-fetch"] - completed = subprocess.run(command, cwd=lockfile.parent, capture_output=True, text=True, timeout=300) + # A fixed direct auditor invocation avoids Cargo aliases and option injection. + completed = subprocess.run(["cargo-audit", "audit", "--file", "Cargo.lock", "--json"], + cwd=lockfile.parent, capture_output=True, text=True, timeout=300, shell=False) args.raw_output.write_text(completed.stdout) sys.stdout.write(completed.stdout) sys.stderr.write(completed.stderr) diff --git a/scripts/ci/test_audit_rust_advisories.py b/scripts/ci/test_audit_rust_advisories.py index ba6304110b..8a8eba24f4 100644 --- a/scripts/ci/test_audit_rust_advisories.py +++ b/scripts/ci/test_audit_rust_advisories.py @@ -1,5 +1,9 @@ import copy import json +import os +import subprocess +import sys +import tempfile from pathlib import Path import unittest from unittest import mock @@ -117,6 +121,45 @@ def test_native_warning_denial_cannot_be_overridden(self): self.assertEqual(decision(1, result, {}), 0) with self.assertRaises(ValueError): decision(1, classify(self.report([]), self.lock), {}) + def test_cli_rejects_option_like_or_shell_like_lockfile_names(self): + script = Path(__file__).parent / "audit_rust_advisories.py" + with tempfile.TemporaryDirectory(prefix="ruview-audit-cli-") as directory: + marker = Path(directory) / "must-not-exist" + lock = Path(directory) / "Cargo.lock; touch must-not-exist" + lock.write_text("version = 4\npackage = []\n") + result = subprocess.run([sys.executable, str(script), "--lockfile", str(lock)], + cwd=directory, capture_output=True, text=True, timeout=5) + self.assertEqual(result.returncode, 2) + self.assertIn("standard Cargo.lock", result.stderr) + self.assertFalse(marker.exists()) + + def test_cli_cannot_select_an_unreviewed_advisory_database(self): + script = Path(__file__).parent / "audit_rust_advisories.py" + result = subprocess.run([sys.executable, str(script), "--database", "/unreviewed"], + capture_output=True, text=True, timeout=5) + self.assertEqual(result.returncode, 2) + self.assertIn("unrecognized arguments", result.stderr) + + def test_project_cargo_entrypoint_is_never_executed(self): + script = Path(__file__).parent / "audit_rust_advisories.py" + with tempfile.TemporaryDirectory(prefix="ruview-audit-entrypoint-") as directory: + root = Path(directory) + (root / "Cargo.lock").write_text("version = 4\npackage = []\n") + marker = root / "unexpected-cargo" + clean = json.dumps({"vulnerabilities": {"found": False, "count": 0, "list": []}, "settings": {"ignore": []}}) + for binary in ("cargo", "cargo-audit"): + text = f"#!{sys.executable}\nfrom pathlib import Path\n" + if binary == "cargo": text += f"Path({str(marker)!r}).write_text('unexpected Cargo dispatch')\n" + text += f"print({clean!r})\n" + executable = root / binary + executable.write_text(text) + executable.chmod(0o755) + result = subprocess.run([sys.executable, str(script), "--lockfile", str(root / "Cargo.lock")], + cwd=root, env={**os.environ, "PATH": str(root) + os.pathsep + os.defpath}, + capture_output=True, text=True, timeout=5) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertFalse(marker.exists()) + def test_native_clean_report_remains_clean(self): self.assertEqual(classify(self.report([]), self.lock)["unresolved"], []) From 4bae2c739836309f994e0849f3aff540c2a84b12 Mon Sep 17 00:00:00 2001 From: Dragan Spiridonov Date: Fri, 20 Nov 2026 10:03:59 +0100 Subject: [PATCH 09/10] Remove temporary advisory reconciliation after upstream range correction --- .github/workflows/security-scan.yml | 12 +- docs/security/wasmtime-audit-20261119.md | 92 +++------ scripts/ci/audit_rust_advisories.py | 175 ------------------ .../wasmtime-primary-scopes-20261119.json | 60 ------ .../wasmtime-rustsec-metadata-20261119.json | 68 ------- scripts/ci/test_audit_rust_advisories.py | 168 ----------------- 6 files changed, 33 insertions(+), 542 deletions(-) delete mode 100644 scripts/ci/audit_rust_advisories.py delete mode 100644 scripts/ci/fixtures/wasmtime-primary-scopes-20261119.json delete mode 100644 scripts/ci/fixtures/wasmtime-rustsec-metadata-20261119.json delete mode 100644 scripts/ci/test_audit_rust_advisories.py diff --git a/.github/workflows/security-scan.yml b/.github/workflows/security-scan.yml index 4cdd250c73..897fda29de 100644 --- a/.github/workflows/security-scan.yml +++ b/.github/workflows/security-scan.yml @@ -36,21 +36,17 @@ jobs: # reviewed ignores in v2/.cargo/audit.toml. Run inside the v2 workspace # so that policy file is actually loaded. working-directory: v2 - env: - GH_TOKEN: ${{ github.token }} run: | - python3 ../scripts/ci/test_audit_rust_advisories.py - python3 ../scripts/ci/audit_rust_advisories.py --lockfile Cargo.lock + set -o pipefail + # 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 if: always() with: name: cargo-audit-report - path: | - v2/cargo-audit.json - v2/cargo-audit-classification.json - v2/cargo-audit-upstream.json + path: v2/cargo-audit.json if-no-files-found: error # Static Application Security Testing (SAST) diff --git a/docs/security/wasmtime-audit-20261119.md b/docs/security/wasmtime-audit-20261119.md index 277abef3a6..77d09f9aa4 100644 --- a/docs/security/wasmtime-audit-20261119.md +++ b/docs/security/wasmtime-audit-20261119.md @@ -26,66 +26,32 @@ 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. -## Why native cargo-audit still reports two matches - -With36.0.21, the native report drops from five to two. Its remaining0567/0573 -entries omit the upstream lower bounds and therefore incorrectly match36.x. -The two wrongly named WASI entries stop matching after the patch; no special -coverage for those entries is implemented. - -The existing security workflow uses audit_rust_advisories.py to retain the exact -native report and reconcile only these two reviewed range errors. This is not a -native audit-clean result: native exit1/two findings remain recorded separately -from zero unresolved findings after verified metadata reconciliation. - -A correction applies only when ALL of the following hold: - -- The RustSec selector fingerprint matches the checked-in ID/package/aliases/URL, - affected-function/platform selectors and patched/unaffected ranges. -- The finding is registry core wasmtime exactly36.0.21, with its checksum matching - both the verified registry archive and current Cargo.lock. Other versions, sources or checksums are not covered. -- Neither wasmtime-wasi nor wasmtime-wasi-http appears in the reviewed lockfile. - A different graph requires reassessment; those packages cannot gain coverage. -- A fresh read of the fixed official GitHub repository advisory endpoint matches - the reviewed primary package/version-range fingerprint. Network failure, - changed identity/ranges or missing evidence cannot fall back to the fixture. -- The report's existing ignore settings match the unchanged local audit policy. - -All unknown findings remain unresolved, including the version-applicable0569 if -it reappears. Malformed or incomplete reports fail closed. Fingerprints deliberately -cover scope selectors, not cosmetic prose. The primary scopes are checked live; -fixtures are used only by offline tests and never as a successful CI fallback. - -## Evidence and operating boundary - -The job retains cargo-audit.json (unmodified native output), -cargo-audit-classification.json (including native exit and unresolved findings), -and cargo-audit-upstream.json (live scopes and retrieval time). The existing -read-only GitHub Actions token is used by gh to read public published advisories; -no extra token, permission, secret or write operation is introduced. - -Offline negative tests exercise unknown advisories, changed RustSec/primary -selectors, affected/future versions, wrong registry/checksum, missing lock records, -WASI additions, policy injection, malformed reports, unreviewed network IDs and -live-provider failure. Existing ignores remain explicit; no blanket ignore, -severity-threshold change, raw report rewrite or advisory database edit is made. - -Native audit reproduction on snapshot7133866a after the patch reports two entries; -live primary verification classifies those two scope mismatches and leaves zero -unresolved entries. Remove this reconciliation when RustSec's ranges are corrected. -A future version or altered scope must be reassessed, not silently covered. - -No plugin is enabled, helper replaced, application installed, physical sensor -qualified or production configuration changed by this source repair. Merge/release -remain the maintainers' separate decisions. - -The CLI invokes cargo-audit directly with a fixed argument vector and shell=False. -It requires a standard Cargo.lock basename and has no alternate-database switch; -Cargo aliases and CLI-selected advisory arguments do not enter the process command. -Local pinned-snapshot evidence is reproduced by calling the native tool directly, -then exercising the pure classifier with fresh upstream evidence. - -A controlled negative probe confirmed the previous Cargo dispatcher could execute -a project-defined audit alias and accept an artificial clean result. The direct -auditor ignored that alias. This is a negative-control fixture, not a real clean -audit or a claim that any repository alias was used maliciously. +## 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/scripts/ci/audit_rust_advisories.py b/scripts/ci/audit_rust_advisories.py deleted file mode 100644 index afa8ecd751..0000000000 --- a/scripts/ci/audit_rust_advisories.py +++ /dev/null @@ -1,175 +0,0 @@ -#!/usr/bin/env python3 -"""Preserve cargo-audit output and reconcile two reviewed upstream range errors. - -This is not an advisory ignore list. Only the exact reviewed RustSec selectors, -registry Wasmtime 36.0.21, matching lockfile checksum and absent WASI packages can -qualify. Corrected/new metadata or other versions remain unresolved. See the -docs/security/wasmtime-audit-20261119.md assessment for authoritative package/range evidence. -""" -import argparse -import datetime -import hashlib -import json -from pathlib import Path -import subprocess -import sys -import tomllib - -REGISTRY = "registry+https://github.com/rust-lang/crates.io-index" -WASMTIME_ARCHIVE_SHA256 = "c58d2cbba3817454af39ec9f0b39279870a9a98ef4c52bbbcdd9c1ffc1f5f17f" -REVIEWED = { - "RUSTSEC-2026-0567": "99da727f7424b8b1f70c570e7347f7c2ad1d23ac7f7d1534654324e12297ee4c", - "RUSTSEC-2026-0573": "1716fc0fdcf2acae357882632f7ee3a3b6551116e6c18ba32f2f79ddf140ad08", -} - -PRIMARY = { - 'RUSTSEC-2026-0567': ('GHSA-2674-w29h-wqvj', - 'fe62715ac8a8ae324420af60d2ba36d55be25ede888c045cdfdbf5b18ffbd9ca'), - 'RUSTSEC-2026-0573': ('GHSA-wvgq-g5hf-6gm6', - '4a1af44d4f625d4efdf6585593ec47fb73e89fc162fe1ba525090ca9d43f5b70'), -} - - -def primary_scope(document): - if not isinstance(document, dict) or not isinstance(document.get("vulnerabilities"), list): - raise ValueError("invalid upstream advisory metadata") - values = [] - for value in document["vulnerabilities"]: - package = value["package"] - values.append({"package": {"name": package["name"], "ecosystem": package["ecosystem"].lower()}, - "vulnerable_version_range": "".join(value["vulnerable_version_range"].split()), - "patched_versions": "".join(value["patched_versions"].split())}) - values.sort(key=lambda value: json.dumps(value, sort_keys=True)) - return {"ghsa_id": document["ghsa_id"], "vulnerabilities": values} - - -def primary_digest(document): - return hashlib.sha256(json.dumps(primary_scope(document), sort_keys=True, separators=(",", ":")).encode()).hexdigest() - - -def fetch_primary(ghsa): - # IDs come only from PRIMARY constants, never from an untrusted report URL. - if ghsa not in {value[0] for value in PRIMARY.values()}: - raise ValueError("unreviewed upstream advisory identifier") - completed = subprocess.run(["gh", "api", "--hostname", "github.com", - "repos/bytecodealliance/wasmtime/security-advisories/" + ghsa], - capture_output=True, text=True, timeout=45) - if completed.returncode != 0 or len(completed.stdout) > 512 * 1024: - raise ValueError(f"live upstream verification failed for {ghsa} (gh exit {completed.returncode}); no offline fallback") - scope = primary_scope(json.loads(completed.stdout)) - if scope["ghsa_id"] != ghsa: - raise ValueError("upstream advisory identity changed") - return scope - - -def selector_digest(finding): - advisory = finding.get("advisory", {}) - selectors = {key: finding.get(key) for key in ("affected", "versions")} - selectors["advisory"] = {key: advisory.get(key) for key in ("id", "package", "aliases", "url")} - return hashlib.sha256(json.dumps(selectors, sort_keys=True, separators=(",", ":")).encode()).hexdigest() - - -def metadata_mismatch(finding, packages, primary): - advisory = finding.get("advisory", {}) - expected = REVIEWED.get(advisory.get("id")) - if expected is None or selector_digest(finding) != expected: - return False - ghsa, expected_primary = PRIMARY[advisory["id"]] - if ghsa not in primary or primary_digest(primary[ghsa]) != expected_primary: - return False - package = finding.get("package", {}) - if (package.get("name"), package.get("version"), package.get("source")) != ("wasmtime", "36.0.21", REGISTRY): - return False - checksum = package.get("checksum") - if checksum != WASMTIME_ARCHIVE_SHA256: - return False - identity = ("name", "version", "source", "checksum") - if not any(all(p.get(key) == package.get(key) for key in identity) for p in packages): - return False - if any(p.get("name") in {"wasmtime-wasi", "wasmtime-wasi-http"} for p in packages): - return False - return True - - -def classify(report, lock, ignored=(), primary=None): - primary = primary or {} - if not isinstance(report, dict) or report.get("error") or not isinstance(lock.get("package"), list): - raise ValueError("invalid cargo-audit report or lockfile") - vulnerability = report.get("vulnerabilities", {}) - findings = vulnerability.get("list") - count = vulnerability.get("count") - if (not isinstance(findings, list) or type(count) is not int or count < 0 - or count != len(findings) or type(vulnerability.get("found")) is not bool - or vulnerability["found"] != bool(findings)): - raise ValueError("cargo-audit omitted or malformed vulnerability findings") - configured = report.get("settings", {}).get("ignore", []) - if not isinstance(configured, list) or set(configured) != set(ignored): - raise ValueError("cargo-audit policy does not match the existing local policy") - result = {"raw_vulnerability_count": count, "metadata_corrections": [], "existing_policy_ignored": [], "unresolved": []} - for finding in findings: - if not isinstance(finding, dict) or not isinstance(finding.get("advisory"), dict): - raise ValueError("invalid advisory finding") - advisory_id = finding["advisory"].get("id") - if advisory_id in ignored: - result["existing_policy_ignored"].append(advisory_id) - elif metadata_mismatch(finding, lock["package"], primary): - result["metadata_corrections"].append({"id": advisory_id, "package": "wasmtime", "version": "36.0.21", "selector_sha256": REVIEWED[advisory_id], "upstream_advisory": PRIMARY[advisory_id][0], "upstream_scope_sha256": PRIMARY[advisory_id][1]}) - else: - result["unresolved"].append(finding) - return result - - -def decision(native_exit, result, policy): - if native_exit not in (0, 1): - raise ValueError("cargo-audit execution failed") - if native_exit == 1 and result["raw_vulnerability_count"] == 0: - raise ValueError("unexplained native failure without vulnerability findings") - # Never turn an explicitly denied native warning into success. - denied_output = bool(policy.get("output", {}).get("deny", [])) - return 1 if result["unresolved"] or (native_exit == 1 and denied_output) else 0 - - -def main(): - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("--lockfile", type=Path, default=Path("Cargo.lock")) - parser.add_argument("--raw-output", type=Path, default=Path("cargo-audit.json")) - parser.add_argument("--classified-output", type=Path, default=Path("cargo-audit-classification.json")) - parser.add_argument("--upstream-output", type=Path, default=Path("cargo-audit-upstream.json")) - args = parser.parse_args() - lockfile = args.lockfile.resolve() - if lockfile.name != "Cargo.lock": - raise ValueError("the workspace audit requires a standard Cargo.lock filename") - lock = tomllib.loads(lockfile.read_text()) - policy_file = lockfile.parent / ".cargo/audit.toml" - policy = tomllib.loads(policy_file.read_text()) if policy_file.exists() else {} - ignored = policy.get("advisories", {}).get("ignore", []) - # A fixed direct auditor invocation avoids Cargo aliases and option injection. - completed = subprocess.run(["cargo-audit", "audit", "--file", "Cargo.lock", "--json"], - cwd=lockfile.parent, capture_output=True, text=True, timeout=300, shell=False) - args.raw_output.write_text(completed.stdout) - sys.stdout.write(completed.stdout) - sys.stderr.write(completed.stderr) - if completed.returncode not in (0, 1) or len(completed.stdout) > 16 * 1024 * 1024: - raise ValueError("cargo-audit execution failed or returned an oversized report") - report = json.loads(completed.stdout) - primary = {} - for finding in report.get("vulnerabilities", {}).get("list", []): - advisory_id = finding.get("advisory", {}).get("id") - if advisory_id in PRIMARY and advisory_id not in ignored: - ghsa = PRIMARY[advisory_id][0] - primary[ghsa] = fetch_primary(ghsa) - args.upstream_output.write_text(json.dumps({"retrieved_at": datetime.datetime.now(datetime.timezone.utc).isoformat(), "scopes": primary}, indent=2) + "\n") - result = classify(report, lock, ignored, primary) - exit_code = decision(completed.returncode, result, policy) - result["native_exit_code"] = completed.returncode - args.classified_output.write_text(json.dumps(result, indent=2) + "\n") - print(json.dumps({key: value for key, value in result.items() if key != "unresolved"} | {"unresolved_count": len(result["unresolved"])})) - return exit_code - - -if __name__ == "__main__": - try: - sys.exit(main()) - except (ValueError, OSError, subprocess.SubprocessError) as error: - print(f"RUST AUDIT FAILED: {error}", file=sys.stderr) - sys.exit(2) diff --git a/scripts/ci/fixtures/wasmtime-primary-scopes-20261119.json b/scripts/ci/fixtures/wasmtime-primary-scopes-20261119.json deleted file mode 100644 index 1aae735d11..0000000000 --- a/scripts/ci/fixtures/wasmtime-primary-scopes-20261119.json +++ /dev/null @@ -1,60 +0,0 @@ -{ - "GHSA-2674-w29h-wqvj": { - "ghsa_id": "GHSA-2674-w29h-wqvj", - "vulnerabilities": [ - { - "package": { - "name": "wasmtime", - "ecosystem": "rust" - }, - "vulnerable_version_range": ">=39.0.0,<48.0.9", - "patched_versions": "48.0.9" - }, - { - "package": { - "name": "wasmtime", - "ecosystem": "rust" - }, - "vulnerable_version_range": ">=49.0.0,<49.0.6", - "patched_versions": "49.0.6" - }, - { - "package": { - "name": "wasmtime", - "ecosystem": "rust" - }, - "vulnerable_version_range": ">=50.0.0,<50.0.2", - "patched_versions": "50.0.2" - } - ] - }, - "GHSA-wvgq-g5hf-6gm6": { - "ghsa_id": "GHSA-wvgq-g5hf-6gm6", - "vulnerabilities": [ - { - "package": { - "name": "wasmtime", - "ecosystem": "rust" - }, - "vulnerable_version_range": ">=47.0.0,<=48.0.8", - "patched_versions": "48.0.9" - }, - { - "package": { - "name": "wasmtime", - "ecosystem": "rust" - }, - "vulnerable_version_range": ">=49.0.0,<=49.0.5", - "patched_versions": "49.0.6" - }, - { - "package": { - "name": "wasmtime", - "ecosystem": "rust" - }, - "vulnerable_version_range": ">=50.0.0,<=50.0.1", - "patched_versions": "50.0.2" - } - ] - } -} diff --git a/scripts/ci/fixtures/wasmtime-rustsec-metadata-20261119.json b/scripts/ci/fixtures/wasmtime-rustsec-metadata-20261119.json deleted file mode 100644 index b328e152e3..0000000000 --- a/scripts/ci/fixtures/wasmtime-rustsec-metadata-20261119.json +++ /dev/null @@ -1,68 +0,0 @@ -[ - { - "advisory": { - "id": "RUSTSEC-2026-0567", - "package": "wasmtime", - "title": "Abort in Wasmtime builds without the `threads` cargo feature", - "description": "This is an entry in the RustSec database for the Wasmtime security advisory\nlocated at\nhttps://github.com/bytecodealliance/wasmtime/security/advisories/GHSA-2674-w29h-wqvj\nFor more information see the GitHub-hosted security advisory.", - "date": "2026-11-16", - "aliases": [ - "GHSA-2674-w29h-wqvj" - ], - "related": [], - "collection": "crates", - "categories": [], - "keywords": [], - "cvss": null, - "informational": null, - "references": [], - "source": null, - "url": "https://github.com/bytecodealliance/wasmtime/pull/15314", - "withdrawn": null, - "license": "CC0-1.0", - "expect-deleted": false - }, - "versions": { - "patched": [ - ">=48.0.9, <49.0.0", - ">=49.0.6, <50.0.0", - ">=50.0.2" - ], - "unaffected": [] - }, - "affected": null - }, - { - "advisory": { - "id": "RUSTSEC-2026-0573", - "package": "wasmtime", - "title": "Process abort when a GC heap's reservation isn't a multiple of the page size", - "description": "This is an entry in the RustSec database for the Wasmtime security advisory\nlocated at\nhttps://github.com/bytecodealliance/wasmtime/security/advisories/GHSA-wvgq-g5hf-6gm6\nFor more information see the GitHub-hosted security advisory.", - "date": "2026-11-16", - "aliases": [ - "GHSA-wvgq-g5hf-6gm6" - ], - "related": [], - "collection": "crates", - "categories": [], - "keywords": [], - "cvss": null, - "informational": null, - "references": [], - "source": null, - "url": "https://github.com/bytecodealliance/wasmtime/pull/15320", - "withdrawn": null, - "license": "CC0-1.0", - "expect-deleted": false - }, - "versions": { - "patched": [ - ">=48.0.9, <49.0.0", - ">=49.0.6, <50.0.0", - ">=50.0.2" - ], - "unaffected": [] - }, - "affected": null - } -] diff --git a/scripts/ci/test_audit_rust_advisories.py b/scripts/ci/test_audit_rust_advisories.py deleted file mode 100644 index 8a8eba24f4..0000000000 --- a/scripts/ci/test_audit_rust_advisories.py +++ /dev/null @@ -1,168 +0,0 @@ -import copy -import json -import os -import subprocess -import sys -import tempfile -from pathlib import Path -import unittest -from unittest import mock -from audit_rust_advisories import classify as classify_report, REGISTRY, fetch_primary, decision, WASMTIME_ARCHIVE_SHA256 - -def classify(report, lock, ignored=(), primary=None): - if primary is None: - primary = json.loads((Path(__file__).parent / "fixtures/wasmtime-primary-scopes-20261119.json").read_text()) - return classify_report(report, lock, ignored, primary) - - -class AuditMetadataTests(unittest.TestCase): - def setUp(self): - self.findings = json.loads((Path(__file__).parent / "fixtures/wasmtime-rustsec-metadata-20261119.json").read_text()) - self.package = {"name": "wasmtime", "version": "36.0.21", "source": REGISTRY, "checksum": WASMTIME_ARCHIVE_SHA256} - for finding in self.findings: - finding["package"] = self.package.copy() - self.lock = {"package": [self.package.copy()]} - - def report(self, findings=None): - findings = self.findings if findings is None else findings - return {"vulnerabilities": {"found": bool(findings), "count": len(findings), "list": findings}, "settings": {"ignore": []}} - - def test_exact_reviewed_false_matches_are_reported_separately(self): - result = classify(self.report(), self.lock) - self.assertEqual(len(result["metadata_corrections"]), 2) - self.assertEqual(result["raw_vulnerability_count"], 2) - self.assertEqual(result["unresolved"], []) - - def test_old_or_future_affected_versions_cannot_use_the_correction(self): - for version in ("36.0.20", "36.0.22", "39.0.0", "47.0.0", "50.0.1"): - with self.subTest(version=version): - findings = copy.deepcopy(self.findings) - for finding in findings: - finding["package"]["version"] = version - lock = {"package": [{**self.package, "version": version}]} - self.assertEqual(len(classify(self.report(findings), lock)["unresolved"]), 2) - - def test_wasi_presence_prevents_wrong_package_coverage(self): - for name in ("wasmtime-wasi", "wasmtime-wasi-http"): - with self.subTest(package=name): - lock = {"package": [self.package.copy(), {"name": name, "version": "36.0.20"}]} - self.assertEqual(len(classify(self.report(), lock)["unresolved"]), 2) - - def test_actual_wasi_findings_remain_unresolved(self): - findings = copy.deepcopy(self.findings) - findings[0]["package"]["name"] = "wasmtime-wasi" - self.assertEqual(len(classify(self.report(findings), self.lock)["unresolved"]), 1) - - def test_changed_advisory_selectors_fail_closed(self): - for change in ("versions", "affected", "aliases", "url"): - findings = copy.deepcopy(self.findings) - if change == "versions": findings[0]["versions"]["patched"] = [">=99.0.0"] - elif change == "affected": findings[0]["affected"] = {"os": ["macos"]} - elif change == "aliases": findings[0]["advisory"]["aliases"] = ["GHSA-new-advisory"] - else: findings[0]["advisory"]["url"] = "https://example.invalid/unreviewed" - self.assertEqual(len(classify(self.report(findings), self.lock)["unresolved"]), 1) - - def test_unknown_or_version_applicable_advisory_is_not_covered(self): - for advisory in ("RUSTSEC-2099-0001", "RUSTSEC-2026-0569"): - findings = copy.deepcopy(self.findings) - findings[0]["advisory"]["id"] = advisory - self.assertEqual(len(classify(self.report(findings), self.lock)["unresolved"]), 1) - - def test_registry_identity_and_checksum_must_match_the_lockfile(self): - for change in ("checksum", "source"): - findings = copy.deepcopy(self.findings) - findings[0]["package"][change] = "unreviewed" - self.assertEqual(len(classify(self.report(findings), self.lock)["unresolved"]), 1) - self.assertEqual(len(classify(self.report(), {"package": []})["unresolved"]), 2) - - def test_coordinated_report_and_lock_checksum_tamper_is_rejected(self): - findings = copy.deepcopy(self.findings) - for finding in findings: finding["package"]["checksum"] = "b" * 64 - lock = {"package": [{**self.package, "checksum": "b" * 64}]} - self.assertEqual(len(classify(self.report(findings), lock)["unresolved"]), 2) - - def test_malformed_or_omitted_findings_fail_closed(self): - for report in ({}, {"vulnerabilities": {"found": False, "count": 0, "list": self.findings}}): - with self.assertRaises(ValueError): classify(report, self.lock) - - def test_existing_policy_is_honored_but_cannot_be_broadened_by_report(self): - findings = copy.deepcopy(self.findings) - findings[0]["advisory"]["id"] = "RUSTSEC-2099-0001" - report = self.report(findings) - report["settings"]["ignore"] = ["RUSTSEC-2099-0001"] - with self.assertRaises(ValueError): classify(report, self.lock) - result = classify(report, self.lock, ["RUSTSEC-2099-0001"]) - self.assertEqual(result["unresolved"], []) - self.assertEqual(result["existing_policy_ignored"], ["RUSTSEC-2099-0001"]) - - def test_missing_primary_evidence_does_not_cover_any_finding(self): - self.assertEqual(len(classify(self.report(), self.lock, primary={})["unresolved"]), 2) - - def test_changed_primary_package_or_range_revokes_coverage(self): - for field in ("package", "vulnerable_version_range"): - primary = json.loads((Path(__file__).parent / "fixtures/wasmtime-primary-scopes-20261119.json").read_text()) - target = primary["GHSA-2674-w29h-wqvj"]["vulnerabilities"][0] - if field == "package": target["package"]["name"] = "other-package" - else: target[field] = ">=36.0.0,<48.0.9" - self.assertEqual(len(classify(self.report(), self.lock, primary=primary)["unresolved"]), 1) - - def test_unknown_advisory_cannot_choose_a_network_location(self): - with mock.patch("audit_rust_advisories.subprocess.run") as execute: - with self.assertRaises(ValueError): fetch_primary("https://untrusted.invalid/advisory") - execute.assert_not_called() - - def test_live_primary_failure_has_no_offline_success_fallback(self): - with mock.patch("audit_rust_advisories.subprocess.run", return_value=mock.Mock(returncode=1, stdout="")): - with self.assertRaises(ValueError): fetch_primary("GHSA-2674-w29h-wqvj") - - def test_native_warning_denial_cannot_be_overridden(self): - result = classify(self.report(), self.lock) - self.assertEqual(decision(1, result, {"output": {"deny": ["warnings"]}}), 1) - self.assertEqual(decision(1, result, {}), 0) - with self.assertRaises(ValueError): decision(1, classify(self.report([]), self.lock), {}) - - def test_cli_rejects_option_like_or_shell_like_lockfile_names(self): - script = Path(__file__).parent / "audit_rust_advisories.py" - with tempfile.TemporaryDirectory(prefix="ruview-audit-cli-") as directory: - marker = Path(directory) / "must-not-exist" - lock = Path(directory) / "Cargo.lock; touch must-not-exist" - lock.write_text("version = 4\npackage = []\n") - result = subprocess.run([sys.executable, str(script), "--lockfile", str(lock)], - cwd=directory, capture_output=True, text=True, timeout=5) - self.assertEqual(result.returncode, 2) - self.assertIn("standard Cargo.lock", result.stderr) - self.assertFalse(marker.exists()) - - def test_cli_cannot_select_an_unreviewed_advisory_database(self): - script = Path(__file__).parent / "audit_rust_advisories.py" - result = subprocess.run([sys.executable, str(script), "--database", "/unreviewed"], - capture_output=True, text=True, timeout=5) - self.assertEqual(result.returncode, 2) - self.assertIn("unrecognized arguments", result.stderr) - - def test_project_cargo_entrypoint_is_never_executed(self): - script = Path(__file__).parent / "audit_rust_advisories.py" - with tempfile.TemporaryDirectory(prefix="ruview-audit-entrypoint-") as directory: - root = Path(directory) - (root / "Cargo.lock").write_text("version = 4\npackage = []\n") - marker = root / "unexpected-cargo" - clean = json.dumps({"vulnerabilities": {"found": False, "count": 0, "list": []}, "settings": {"ignore": []}}) - for binary in ("cargo", "cargo-audit"): - text = f"#!{sys.executable}\nfrom pathlib import Path\n" - if binary == "cargo": text += f"Path({str(marker)!r}).write_text('unexpected Cargo dispatch')\n" - text += f"print({clean!r})\n" - executable = root / binary - executable.write_text(text) - executable.chmod(0o755) - result = subprocess.run([sys.executable, str(script), "--lockfile", str(root / "Cargo.lock")], - cwd=root, env={**os.environ, "PATH": str(root) + os.pathsep + os.defpath}, - capture_output=True, text=True, timeout=5) - self.assertEqual(result.returncode, 0, result.stderr) - self.assertFalse(marker.exists()) - - def test_native_clean_report_remains_clean(self): - self.assertEqual(classify(self.report([]), self.lock)["unresolved"], []) - - -if __name__ == "__main__": - unittest.main() From 8cccbe5397a2cca1397db495cc2937b5f9801e5e Mon Sep 17 00:00:00 2001 From: Dragan Spiridonov Date: Fri, 20 Nov 2026 10:11:57 +0100 Subject: [PATCH 10/10] fix(test): await collector readiness with bounded startup diagnostics --- .../test/training-capture-collisions.test.mjs | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) 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()));