fix(sfm): a change that fails after the send says so; the audit row records sent + states; S3 unknown states refused

Final fix wave for phase 3 (start/stop monitoring), per the whole-phase review.

- I1 + M2: every failure after the send has started is a ChangeUnconfirmed
  (audit_id, sent = "acknowledged" | "unknown", the cause's HTTP status) whose
  message says the frame was sent and names the audit record -- the ring in
  place of the ack, the four lost-ack follow-ups, and the three read-back
  paths.  Series4Session releases the line on it.  Every audit finish goes
  through one helper; a finish that fails is AuditIncomplete (500) and still
  says what happened to the unit.  The line is kept.
- I2: device_commands gains `sent` (1 acknowledged, 0 nothing sent, NULL
  unknown) and `states` (before / seen / after), with an idempotent ADD COLUMN
  in _migrate for a table created earlier on this branch.
- M1: a RADIO RING already buffered is caught before 0x96/0x97 goes out
  (pre_send = read_unsolicited, one 10 ms transport read).  That is a
  CallHomeInProgress with sent=0, not ChangeUnconfirmed.
- M3: Series 3 reads the raw 0x1C flag (new MonitorStatus.state_flag); a byte
  other than 0x00/0x10, or a short section, is unknown(...) and refused.
- M8: even_if_already on a unit already in the state notes that the read-back
  proves nothing.
- M10: a scan pins `.protocol.execute(` to sfm/device_access/s4.py; both
  scans fail closed on a file that does not parse.
- M11: docs corrected (CLAUDE.md phase-2 sentence, phase-3 bullet, REST list;
  micromate/protocol.py scope and the 0x49 read_state docstring; s4.py; route
  docstrings).  "No command has ever been originated" is NOT retracted.
- M12: GET /db/device_commands normalises the serial.
- Minors: unused imports, S3 dry-run frames and 120 s timeout pinned, test
  rename, serial-cache and short-0x49 tests, _RUN_STATES comment.

Schema: two NULLable columns on the branch-new device_commands table, added
by _migrate on open; no backfill.  No codec change, TOOL_VERSION unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
serversdownandClaude Opus 5.5 committed 2026-10-23 19:16:45 -04:00
1 parent ac33c8ffa4
commit 8f68b09f5e
18 files changed
+806 -107

No files matched your search

+1
View File
@@ -385,6 +385,7 @@ class MicromateClient:
self._serial = _cstring(_content(self._proto.read_serial()))
return self._serial
# Anything else reads unknown(0x..): the safe default, which the change path refuses.
_RUN_STATES = {0x00: "idle", 0x02: "monitoring", 0x03: "calling_home"}
def read_run_state(self) -> MicromateRunState:
+21 -9
View File
@@ -4,14 +4,17 @@ protocol.py — one method per Micromate (Series IV) wire command.
Returns raw payload bytes. Interpretation belongs in ``client.py``; this layer
knows frames, offsets and sequencing, and nothing about what a field means.
Scope: **reads only on the wire, enforced** (in this package: the Series-3
`minimateplus` stack and raw-socket bench bridges are outside the gate). Every outgoing frame passes
Scope: **reads, plus start/stop monitoring through `execute()` only --
enforced** (in this package: the Series-3 `minimateplus` stack and raw-socket
bench bridges are outside the gate). Every outgoing frame passes
`micromate.opcodes.check_transmit()` in `_send`: a write opcode is refused
unless it arrives through `execute()` (the connection itself holds no write
permission), and `0xA2` -- CONFIRM ERASE on Series III -- is
refused always. The gate is an allowlist: a SUB not catalogued in
`opcodes` (read or write) is refused as unknown. No command has ever been originated against a unit by this
project; every write in ``docs/micromate_protocol_reference.md`` was performed
permission), `execute()` can permit only SUBs under `ENABLEABLE_WRITES`
(`0x96` / `0x97`), and `0xA2` -- CONFIRM ERASE on Series III -- is refused
always. The one caller of `execute()` is SFM's change path,
`sfm/device_access/s4.py` (a test pins that). The gate is an allowlist: a SUB
not catalogued in `opcodes` (read or write) is refused as unknown. No command
has ever been originated against a unit by this project; every write in ``docs/micromate_protocol_reference.md`` was performed
by THOR while we recorded. The first thing this codebase ever sends to a
customer's instrument should be a decision someone made on purpose, not a side
effect of a client that grew a method. Write frames are *built* in
@@ -305,10 +308,19 @@ class MicromateProtocol:
return self._read(SUB_DEVICE_INFO)
def read_state(self) -> bytes:
"""`0x49` → `0xB6`. A cheap monitoring check; 16 B.
"""`0x49` → `0xB6`. A cheap state check; 16 B of data.
⚠ Test `data[11]` for **non-zero**, never against a constant — it has
read both `0x0E` and `0x0C` while monitoring.
`data[11]` (content[0]) is the run state. Measured: `0x00` idle,
`0x02` monitoring (every monitoring sample so far), and `0x03` while
calling home (ten reads of ten in the captured 2026-10-14 ACH
session). No other value has been seen.
`MicromateClient.read_run_state` matches those three exactly and names
anything else `unknown(0x..)`, which the change path refuses;
`connect()`'s `monitoring` tests non-zero, so `0x03` reads True there.
⚠ `0x0E` and `0x0C` (both read while monitoring, so "test for
non-zero") belong to `SUB 0x1C` `data[12]`, a separate field -- not to
this byte. See the reference's `0x49` and `0x1C` sections.
"""
return self._read(SUB_STATE)