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

+59
View File
@@ -62,3 +62,62 @@ def test_a_second_finish_raises_and_keeps_the_first_result(tmp_path):
row = db.query_device_commands()[0]
assert (row["outcome"], row["verified"], row["ack"], row["error"]) == \
("failed", "mismatch", {"subs": ["0x69"]}, "unit reads idle")
def test_a_finish_records_whether_a_frame_went_out_and_the_states(tmp_path):
db = SeismoDb(tmp_path / "s.db")
states = {"before": "idle", "seen": ["idle", "monitoring"], "after": "monitoring"}
cid = db.begin_device_command(**ROW)
db.finish_device_command(cid, outcome="sent", verified="ok", ack={"subs": ["0x69"]},
error=None, sent=1, states=states)
row = db.query_device_commands()[0]
assert (row["sent"], row["states"]) == (1, states)
def test_sent_and_states_default_to_null(tmp_path):
db = SeismoDb(tmp_path / "s.db")
cid = db.begin_device_command(**ROW)
db.finish_device_command(cid, outcome="failed", verified="not_checked", ack=None,
error="no clean ack")
row = db.query_device_commands()[0]
assert (row["sent"], row["states"]) == (None, None)
def test_db_audit_passes_sent_and_states_through(tmp_path):
db = SeismoDb(tmp_path / "s.db")
audit = DbAudit(lambda: db)
cid = audit.begin(**ROW)
audit.finish(cid, outcome="skipped", verified="not_checked", ack=None, error="already idle",
sent=0, states={"before": "idle", "seen": ["idle"], "after": None})
row = db.query_device_commands()[0]
assert row["sent"] == 0 and row["states"]["before"] == "idle"
# The device_commands table as Task 3 created it: no `sent`, no `states`.
_TASK3_TABLE = """
CREATE TABLE device_commands (
id TEXT PRIMARY KEY, ts TEXT NOT NULL, finished_ts TEXT, serial TEXT NOT NULL,
series INTEGER NOT NULL, address TEXT NOT NULL, action TEXT NOT NULL, tier TEXT NOT NULL,
request TEXT, ack TEXT, verified TEXT, outcome TEXT NOT NULL, error TEXT, caller TEXT
);
INSERT INTO device_commands (id, ts, serial, series, address, action, tier, outcome)
VALUES ('old', '2026-10-22T00:00:00+00:00', 'UM20147', 4, 'tcp:x', 'stop monitoring',
'change', 'pending');
"""
def test_an_existing_task3_table_gains_sent_and_states(tmp_path):
import sqlite3
path = tmp_path / "s.db"
with sqlite3.connect(path) as conn:
conn.executescript(_TASK3_TABLE)
db = SeismoDb(path)
with sqlite3.connect(path) as conn:
cols = {r[1] for r in conn.execute("PRAGMA table_info(device_commands)")}
assert {"sent", "states"} <= cols
old = db.query_device_commands()[0]
assert (old["id"], old["sent"], old["states"]) == ("old", None, None)
db.finish_device_command("old", outcome="failed", verified="not_checked", ack=None,
error="x", sent=0, states={"before": "idle", "seen": ["idle"], "after": None})
assert db.query_device_commands()[0]["sent"] == 0
SeismoDb(path) # a second open is a no-op