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

+56 -7
View File
@@ -1869,9 +1869,31 @@ def device_monitor_start(
With `serial`: the unit's identity is read first (a different unit is a 409,
nothing sent), then its state; `dry_run` returns the planned frames and sends
nothing; a live change is audited, verified by reading the state back, and
never retried. The response is the ChangeResult (409 if the unit never
reached the state, 502 if the ack was lost).
nothing; a live change is audited (`GET /db/device_commands`), verified by
reading the state back, and never retried. `even_if_already` sends even
when the unit is already in the target state.
Statuses with `serial`:
200 the ChangeResult: a dry run, a skip (already in the state), or a send
read back as made
409 ChangeResult: sent and acknowledged, but the unit never reached the
state (`verified: mismatch`, every state seen kept); or, as an
error, a different unit answered or its state was never measured
(nothing sent either way)
502 ChangeResult: the ack was lost but the unit could be read afterwards
422 a malformed serial or address; 423 the unit is busy;
501 no audit log is configured (nothing sent)
503 the unit is calling home, or a call-home was already waiting before
sending (nothing sent); or the audit log could not be written
(nothing sent)
500 the audit row could not be finished -- the detail says first what
happened to the unit (sent and verified, nothing sent, ...)
504 / 502 / 503 a link failure before anything was sent
A failure AFTER the frame went out (or may have) -- the read-back failed,
the ack was lost and the unit could not be read, or a call-home began --
takes the status its cause would have had (503 for a call-home, 504 for
silence, 502 for a bad reply, 503 for a lost connection), and its detail
says the frame was sent and names the audit record.
Without `serial`: the legacy behaviour above, unchanged.
"""
@@ -1912,9 +1934,31 @@ def device_monitor_stop(
With `serial`: the unit's identity is read first (a different unit is a 409,
nothing sent), then its state; `dry_run` returns the planned frames and sends
nothing; a live change is audited, verified by reading the state back, and
never retried. The response is the ChangeResult (409 if the unit never
reached the state, 502 if the ack was lost).
nothing; a live change is audited (`GET /db/device_commands`), verified by
reading the state back, and never retried. `even_if_already` sends even
when the unit is already in the target state.
Statuses with `serial`:
200 the ChangeResult: a dry run, a skip (already in the state), or a send
read back as made
409 ChangeResult: sent and acknowledged, but the unit never reached the
state (`verified: mismatch`, every state seen kept); or, as an
error, a different unit answered or its state was never measured
(nothing sent either way)
502 ChangeResult: the ack was lost but the unit could be read afterwards
422 a malformed serial or address; 423 the unit is busy;
501 no audit log is configured (nothing sent)
503 the unit is calling home, or a call-home was already waiting before
sending (nothing sent); or the audit log could not be written
(nothing sent)
500 the audit row could not be finished -- the detail says first what
happened to the unit (sent and verified, nothing sent, ...)
504 / 502 / 503 a link failure before anything was sent
A failure AFTER the frame went out (or may have) -- the read-back failed,
the ack was lost and the unit could not be read, or a call-home began --
takes the status its cause would have had (503 for a call-home, 504 for
silence, 502 for a bad reply, 503 for a lost connection), and its detail
says the frame was sent and names the audit record.
Without `serial`: the legacy behaviour above, unchanged.
"""
@@ -3137,8 +3181,13 @@ def db_device_commands(
) -> dict:
"""
The audit trail of changes SFM attempted on units, newest first.
Each row's `sent` says whether a frame went out (1 acknowledged, 0 nothing
sent, null unknown) and `states` holds every state read (before / seen /
after). `serial` is matched case-insensitively.
"""
return {"commands": _get_db().query_device_commands(serial, limit)}
serial = normalise_serial(serial) if serial else None
return {"commands": _get_db().query_device_commands(serial or None, limit)}
# ── Entry point ────────────────────────────────────────────────────────────────