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

+46 -8
View File
@@ -231,17 +231,55 @@ def test_the_scanner_ignores_ordinary_calls():
assert scan_source("p._send(frame)\nf(a, b=1)") == []
def test_only_protocol_py_grants_write_permission():
def scan_file(scanner, src: str, rel: str) -> list[str]:
"""Fails closed: a file the scanner cannot parse is a hit, not a pass."""
try:
return scanner(src, rel)
except SyntaxError as exc:
return [f"{rel}: does not parse, so it cannot be scanned ({exc})"]
def scan_tracked(scanner, skip) -> list[str]:
root = pathlib.Path(__file__).resolve().parent.parent
files = subprocess.run(["git", "ls-files", "*.py"], cwd=root, capture_output=True,
text=True, check=True).stdout.split()
hits = []
for rel in files:
if rel.startswith("tests/") or rel == "micromate/protocol.py":
if rel.startswith("tests/") or skip(rel):
continue
try:
src = (root / rel).read_text(errors="ignore")
hits += scan_source(src, rel)
except SyntaxError:
pass
assert hits == []
hits += scan_file(scanner, (root / rel).read_text(errors="ignore"), rel)
return hits
def test_a_file_that_does_not_parse_fails_the_scan():
assert scan_file(scan_source, "def broken(:\n", "x.py")
assert scan_file(scan_execute_callers, "def broken(:\n", "x.py")
def test_only_protocol_py_grants_write_permission():
assert scan_tracked(scan_source, lambda rel: rel == "micromate/protocol.py") == []
# -- nothing outside the safety layer calls protocol.execute( -------------------
def scan_execute_callers(src: str, name: str = "<s>") -> list[str]:
"""Hits: any `<x>.protocol.execute(...)` call -- the Series-4 write entry."""
hits = []
for node in ast.walk(ast.parse(src)):
if not isinstance(node, ast.Call):
continue
fn = node.func
if (isinstance(fn, ast.Attribute) and fn.attr == "execute"
and isinstance(fn.value, ast.Attribute) and fn.value.attr == "protocol"):
hits.append(f"{name}:{node.lineno} .protocol.execute call")
return hits
def test_the_execute_scanner_catches_a_protocol_execute_and_not_sqlite():
assert scan_execute_callers("client.protocol.execute(plan)")
assert scan_execute_callers("conn.execute('SELECT 1')") == []
def test_only_the_safety_layer_calls_protocol_execute():
assert scan_tracked(scan_execute_callers,
lambda rel: rel == "sfm/device_access/s4.py") == []