fix(sfm): a ring or a refusal is never mistaken for a send; fresh serial pinned; unmeasured states refused

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 16:39:55 -04:00
1 parent eada7ba767
commit 14ea80f5a4
6 files changed
+166 -21

No files matched your search

+1 -1
View File
@@ -195,7 +195,7 @@ class FakeMicromateClient(_Base):
def serial(self, *, refresh=False):
self._step("serial")
return self.serial_reported
return self.serial_reported if refresh else self.unit_serial
def read_run_state(self):
from micromate.models import MicromateRunState
+102 -4
View File
@@ -9,10 +9,12 @@ import pytest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import micromate.protocol as mm_proto # noqa: E402
from micromate.opcodes import OpcodeRefused # noqa: E402
from sfm.device_access.s4 import RingDetected # noqa: E402
from device_access_fakes import FakeMicromateClient # noqa: E402
from sfm.device_access.access import DeviceAccess # noqa: E402
from sfm.device_access.errors import ( # noqa: E402
CallHomeInProgress, NotSupported, WrongUnit,
AuditUnavailable, CallHomeInProgress, DeviceError, NoAnswer, NotSupported, UnitStateUnknown, WrongUnit,
)
from sfm.device_access.models import UnitAddress # noqa: E402
@@ -56,7 +58,7 @@ def test_start_sends_once_and_verifies_by_reading_back():
assert len(fake.executed) == 1
row = da._audit.rows[r.audit_id]
assert (row["outcome"], row["verified"], row["action"]) == ("sent", "ok", "start monitoring")
assert r.detail["states_seen"][-1] == "monitoring"
assert r.detail["states_seen"] == ["idle", "idle", "monitoring"]
assert r.plan["sends_nothing"] is False
assert row["request"]["plan"]["sends_nothing"] is False
@@ -83,6 +85,7 @@ def test_a_unit_calling_home_is_refused_and_nothing_is_sent():
assert fake.executed == []
assert fake.is_open() is False # the line was released for THOR
assert da.held_serial(ADDR) is None # and the session dropped
assert ADDR.key not in da._sessions
def test_a_different_unit_on_the_fresh_serial_read_is_refused_before_writing():
@@ -93,13 +96,14 @@ def test_a_different_unit_on_the_fresh_serial_read_is_refused_before_writing():
assert fake.executed == []
assert fake.is_open() is False
assert da.held_serial(ADDR) is None
assert ADDR.key not in da._sessions
def test_a_live_change_without_an_audit_sink_is_refused():
fake = FakeMicromateClient()
with pytest.raises(NotSupported, match="audit"):
access(fake, audit=None).start_monitoring(ADDR, "UM20147")
assert fake.executed == []
assert fake.executed == [] and fake.opened == 0 and fake.calls == []
def test_a_clean_ack_but_no_state_change_is_a_failed_change():
@@ -109,9 +113,10 @@ def test_a_clean_ack_but_no_state_change_is_a_failed_change():
assert (r.outcome, r.verified, r.state_after) == ("failed", "mismatch", "idle")
assert len(r.detail["states_seen"]) == 4 # 6 s window / 2 s + the first read
assert da._audit.rows[r.audit_id]["verified"] == "mismatch"
assert da._audit.rows[r.audit_id]["outcome"] == "failed"
def test_a_lost_ack_is_never_retried():
def test_a_lost_ack_is_recorded_once_and_not_resent():
fake = FakeMicromateClient(run_states=[0x00, 0x02],
execute_error=mm_proto.TimeoutError("no ack"))
da = access(fake)
@@ -148,4 +153,97 @@ def test_a_read_back_that_fails_still_finishes_the_audit_row():
da.start_monitoring(ADDR, "UM20147")
(row,) = da._audit.rows.values()
assert len(fake.executed) == 1
assert (row["outcome"], row["verified"]) == ("sent", "not_checked")
assert row["ack"] is not None
assert "reading the state back failed" in row["error"]
assert fake.is_open() is False and ADDR.key not in da._sessions
def test_a_ring_in_place_of_the_ack_is_a_call_home_not_a_send():
fake = FakeMicromateClient(run_states=[0x00], execute_error=RingDetected("RADIO RING"))
da = access(fake)
with pytest.raises(CallHomeInProgress):
da.start_monitoring(ADDR, "UM20147")
assert len(fake.executed) == 1
assert fake.is_open() is False and ADDR.key not in da._sessions
(row,) = da._audit.rows.values()
assert (row["outcome"], row["verified"]) == ("failed", "not_checked")
assert "call-home began in place of the ack" in row["error"]
def test_calling_home_seen_while_polling_releases_the_line():
fake = FakeMicromateClient(run_states=[0x00, 0x03])
da = access(fake)
with pytest.raises(CallHomeInProgress):
da.start_monitoring(ADDR, "UM20147")
assert len(fake.executed) == 1
assert fake.is_open() is False and ADDR.key not in da._sessions
(row,) = da._audit.rows.values()
assert (row["outcome"], row["verified"]) == ("sent", "not_checked")
assert row["ack"] is not None
def test_calling_home_after_a_lost_ack_releases_the_line():
fake = FakeMicromateClient(run_states=[0x00, 0x03],
execute_error=mm_proto.TimeoutError("no ack"))
da = access(fake)
with pytest.raises(CallHomeInProgress):
da.start_monitoring(ADDR, "UM20147")
assert fake.is_open() is False
(row,) = da._audit.rows.values()
assert (row["outcome"], row["verified"]) == ("failed", "not_checked")
def test_a_refusal_by_execute_is_not_a_send():
fake = FakeMicromateClient(execute_error=OpcodeRefused("nope"))
da = access(fake)
with pytest.raises(DeviceError, match="nope"):
da.start_monitoring(ADDR, "UM20147")
(row,) = da._audit.rows.values()
assert (row["outcome"], row["verified"]) == ("failed", "not_checked")
assert "refused before sending" in row["error"]
def test_a_failed_audit_write_sends_nothing():
class Broken(Audit):
def begin(self, **f):
raise OSError("disk full")
fake = FakeMicromateClient()
with pytest.raises(AuditUnavailable):
access(fake, audit=Broken()).start_monitoring(ADDR, "UM20147")
assert fake.executed == []
def test_lost_ack_and_failed_follow_up_read_on_a_held_line_releases_it():
fake = FakeMicromateClient(run_states=[0x00], execute_error=mm_proto.TimeoutError("no ack"))
da = access(fake)
da.status(ADDR, "UM20147") # hold the line
orig, n = fake.read_run_state, []
def flaky():
n.append(1)
if len(n) == 2: # the follow-up after the lost ack
raise mm_proto.TimeoutError("gone")
return orig()
fake.read_run_state = flaky
with pytest.raises(NoAnswer):
da.start_monitoring(ADDR, "UM20147")
assert len(fake.executed) == 1
assert fake.is_open() is False and ADDR.key not in da._sessions
def test_a_live_change_in_an_unmeasured_state_is_refused_but_a_dry_run_is_not():
fake = FakeMicromateClient(run_states=[0x07])
da = access(fake)
with pytest.raises(UnitStateUnknown):
da.start_monitoring(ADDR, "UM20147")
assert fake.executed == [] and da._audit.rows == {}
r = da.start_monitoring(ADDR, "UM20147", dry_run=True)
assert r.state_before == "unknown(0x07)"
def test_bad_verify_timings_are_rejected():
with pytest.raises(ValueError):
DeviceAccess(start_reaper=False, verify_interval_s=0)
with pytest.raises(ValueError):
DeviceAccess(start_reaper=False, verify_interval_s=5, verify_timeout_s=2)