From eecabc76ebecb310b53ec65fd0e0ea4912afa07f Mon Sep 17 00:00:00 2001 From: serversdown Date: Wed, 28 Oct 2026 23:20:26 -0400 Subject: [PATCH] docs(plan): phase 1c -- the webapp by serial, and the wrong-series safeguards Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44 --- .../plans/2026-10-28-sfm-phase1c-webapp.md | 207 ++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 docs/superpowers/plans/2026-10-28-sfm-phase1c-webapp.md diff --git a/docs/superpowers/plans/2026-10-28-sfm-phase1c-webapp.md b/docs/superpowers/plans/2026-10-28-sfm-phase1c-webapp.md new file mode 100644 index 0000000..094d15e --- /dev/null +++ b/docs/superpowers/plans/2026-10-28-sfm-phase1c-webapp.md @@ -0,0 +1,207 @@ +# SFM phase 1c — the webapp on the serial-based endpoints, plus wrong-series safeguards + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** The SFM standalone page (`sfm/sfm_webapp.html`) drives both series through the serial-based device endpoints. In addition, no MiniMate-only write or erase can reach a unit that has not identified itself as Series 3. + +**Architecture:** +- **Server:** one helper refuses the legacy Series-3 write/erase routes unless the unit's own serial (read on the same connection) says Series 3. +- **Page:** the series routing and button rules live in a marked block of pure functions, tested through Node. The DOM wiring calls those functions and nothing else to build a `/device/` URL or decide a button's state. + +**Tech Stack:** Python 3.10 / FastAPI (`sfm/server.py`), vanilla JS in one HTML file, pytest. Node 20 (`~/.local/bin/node`) runs the page-logic tests. + +**Spec:** `docs/superpowers/specs/2026-10-20-sfm-series4-parity-design.md` covers "The webapp", "Scope: the parity table" rows 1–5 and 7, and "Build phases" 1c. The design agreed with Brian on 2026-10-28 is recorded in the Global Constraints below. + +## Global Constraints + +- Work in worktree `~/seismo-relay-parity`, on branch `feat/series4-sfm-parity`. Never commit to `main`. Check with `git branch --show-current` before every commit. +- Run tests with `/home/serversdown/seismo-relay/.venv/bin/python -m pytest -q -p no:cacheprovider`. The baseline is **16 known failures** (15 in `test_bw_ascii_report.py`, 1 in `test_event_file_io.py`). Read the failure list before committing. A `pytest | tail` exits 0. +- **Serial optional:** with a serial, the Live tabs use the serial-based endpoints: `/device/info`, `/device/monitor/status`, `/device/settings`, `/device/events`, `POST /device/events/download`, and `POST /device/monitor/start|stop` (all with `serial=`), plus `POST /device/hangup?host=&tcp_port=`. With the serial blank, every read behaves exactly as today. +- **Series comes from the unit's reply.** The badge and every series-specific control follow the `series` field of the last `/device/info` reply, and only if its `serial` equals the typed one. A typed serial alone never enables a series-specific control. +- **Write buttons:** + - Start/stop use the audited serial path for both series. + - Config write and Call-Home write are **disabled for both series**. Their tooltips read exactly "Coming in phase 5 (#46)" and "Coming in phase 4". + - Diagnostics (erase, rescue, slow drip, blind stop) is enabled only when the confirmed series is 3. + - Hang up is shown only when the confirmed series is 4. +- **Start feedback:** while a start is in flight, show "Starting — the unit runs its sensor check first (about 35 s)". The legacy 60-second `_pollMonitorConfirm` loop is not used in serial mode. +- **Results are reported as SFM returned them:** outcome, verified, state before → after, and the first 8 characters of the audit id. HTTP error `detail` strings are shown verbatim. +- **Speed (Brian):** no new request may be added to a path unless the plan says why. The server identity check reuses the serial that `connect()` already reads. On `rescue` and `POST /device/call_home` it adds exactly one SUB 0x15 read. +- No codec, store or DB change. The legacy `/device/*` routes keep their URLs and parameters (a new optional `serial` is added). + +## Review Focus + +1. **A typed `BE…` serial at an address that holds a Micromate.** The server must refuse every guarded route with 409 and send no write. In the page, Diagnostics stays disabled until `/device/info` answers Series 3. Pinned by Task 1 (`test_guarded_route_refuses_a_unit_that_answers_as_series4`) and Task 2 (`controls: typed serial is not confirmation`). +2. **The serial is edited after a successful identify.** The confirmation must stop counting the moment the typed serial no longer matches. Pinned by Task 2 (`controls: confirmation drops when the serial changes`). +3. **Lower-case or space-padded serials** (`" be9441 "`) are normalised the same way the server normalises them. Pinned by Task 2 (`seriesFromSerial` and `unitQuery` cases). +4. **A start that times out (504) or reads back a mismatch (409).** The page must show the server's detail and the audit id, never "started". Pinned by Task 2 (`changeSummary` cases for `failed`/`mismatch`) and Task 5 (an error-path render). +5. **A download that is already stored** (`inserted: false`) is reported as "already stored" and still opens the stored event. Pinned by Task 2 (`downloadSummary`). + +--- + +### Task 1: Server — legacy Series-3 writes refuse a unit that isn't Series 3 + +**Files:** +- Modify: `sfm/server.py`: + - `device_config` (~1107), `device_events_erase` (~1427), `device_rescue` (~1763), the `device_call_home` POST (~2066); + - `device_config_project` passes `serial` through. +- Modify: `minimateplus/client.py`: add `MiniMateClient.read_serial()`. +- Test: create `tests/test_server_series3_guard.py`. + +**Interfaces:** +- Produces: `MiniMateClient.read_serial(self) -> str`. Requires an open, polled session. It runs the SUB 0x15 read (`self._require_proto().read(SUB_SERIAL_NUMBER)`) and returns `_decode_serial_number(...).serial`. +- Produces: `sfm.server._require_series3(unit_serial: Optional[str], expected: Optional[str]) -> None`. It raises `HTTPException(409, detail)` when either: + - `series_for_serial(unit_serial)` is not 3 (an unknown serial counts as not 3), with detail `f"the unit at this address answers as {unit_serial!r}, not a Series-3 unit -- nothing sent"`; or + - `expected` is given and `normalise_serial(expected) != normalise_serial(unit_serial)`, with detail `f"the unit at this address is {unit_serial}, not {expected} -- nothing sent"`. +- Each guarded route gains `serial: Optional[str] = Query(None, description=_SERIAL_DOC)`. It calls the guard right after it knows the unit's serial, and before its first write: + - `device_config` and `device_events_erase`: `info = client.connect()` → `_require_series3(info.serial, serial)`; + - `device_rescue` and `device_call_home` (POST): after `client.poll()` → `_require_series3(client.read_serial(), serial)`. +- Not guarded, and each gets a one-line comment saying why: legacy `monitor/start|stop`, `stop_monitoring_blind`, `stop_monitoring_slow_drip`, `stop_monitoring_spam`. They send only `0x96`/`0x97`, which mean the same on both series (measured 2026-10-25/27), and the rescue tools must work on a unit too wedged to answer. + +- [ ] **Step 1: Write the failing tests** in `tests/test_server_series3_guard.py`. Use `monkeypatch.setattr(srv, "_build_client", factory)` as `tests/test_server_device_access.py:398` does. The fake client records every call: + - `test_guarded_route_refuses_a_unit_that_answers_as_series4[route]`: parametrise over the four routes. The fake's `connect()` returns a `DeviceInfo(serial="UM20147")`, or its `read_serial()` returns `"UM20147"`. Assert `HTTPException.status_code == 409`, and that the recorded calls contain no write method (`delete_all_events`, `apply_config`, `set_call_home_config`, `stop_monitoring`). + - `test_guarded_route_refuses_a_different_series3_unit`: the unit answers `"BE9441"`, the request names `serial="BE12599"`, so the response is 409 with `"not BE12599"` in the detail. + - `test_guarded_route_proceeds_for_the_named_series3_unit`: the unit answers `"BE9441"` with `serial="be9441 "` (normalised), and the write method is called once. + - `test_unguarded_stop_routes_send_without_an_identity_read`: `stop_monitoring_blind` (it writes through a bare `TcpTransport`; stub `srv.TcpTransport`) sends its stop frames, and nothing builds a client or reads a serial. +- [ ] **Step 2: Run them.** `…pytest tests/test_server_series3_guard.py -q` → FAIL (`_require_series3` / `read_serial` don't exist). +- [ ] **Step 3: Implement** `read_serial`, `_require_series3`, the `serial` query parameter, and the four guard calls as specified. +- [ ] **Step 4: Run** the new tests plus `tests/test_server_device_access.py` → PASS. Then the full suite → only the 16 known failures. +- [ ] **Step 5: Commit** `fix(sfm): legacy Series-3 writes and erase refuse a unit that does not answer as Series 3`. + +### Task 2: Page — the device-routing block (pure functions) and its Node-run tests + +**Files:** +- Modify: `sfm/sfm_webapp.html`. Add one `