From 9e03b3f0abae73013a79ddaf67061353c3c154f3 Mon Sep 17 00:00:00 2001 From: serversdown Date: Mon, 2 Nov 2026 17:23:59 -0500 Subject: [PATCH] fix(sfm): a Micromate in an unknown run state gets no call-home write, and a lost ack keeps its snapshot (phase-4 review) - s4_change_call_home's read_state now returns an unknown(0xNN) run state as it is, so the change core refuses it (UnitStateUnknown, nothing sent), as start/stop already does; it checked only for calling_home. - run_change records the snapshot on the lost-ack path too: after an ack is lost, what the unit now holds (and, on Series 4, whether [118:120] moved) is what tells whether the write landed. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44 --- sfm/device_access/change.py | 9 +++++++-- sfm/device_access/s4.py | 5 +++-- tests/test_device_access_s4_call_home.py | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/sfm/device_access/change.py b/sfm/device_access/change.py index ebabd18..17b2660 100644 --- a/sfm/device_access/change.py +++ b/sfm/device_access/change.py @@ -207,13 +207,18 @@ def run_change(*, kind: ChangeKind, serial: str, series: int, dry_run: bool, eve outcome="failed", verified="not_checked", ack=None, error=f"no clean ack: {exc}; the unit is now calling home", states=states([before], after)) + lost_detail: Dict[str, Any] = {"error": f"no clean ack: {exc}"} + lost_states = states([before], after) + snap = _take_snapshot(snapshot) # what the unit holds is what tells whether it landed + if snap is not None: + lost_detail["snapshot"] = lost_states["snapshot"] = snap finish(f"{action} was sent but the acknowledgement was lost ({exc}); the unit " f"now reads {after}", outcome="failed", verified="not_checked", ack=None, - error=f"no clean ack: {exc}", sent=SENT_UNKNOWN, states=states([before], after)) + error=f"no clean ack: {exc}", sent=SENT_UNKNOWN, states=lost_states) return ChangeResult(dry_run=False, sent=True, outcome="failed", verified="not_checked", state_after=after, audit_id=cid, - detail={"error": f"no clean ack: {exc}"}, **base) + detail=lost_detail, **base) seen = [before] diff --git a/sfm/device_access/s4.py b/sfm/device_access/s4.py index fcf1ebb..b0a881a 100644 --- a/sfm/device_access/s4.py +++ b/sfm/device_access/s4.py @@ -403,8 +403,9 @@ def s4_change_call_home(client: Any, serial: str, *, enabled: bool, dry_run: boo planned: list = [] def read_state() -> str: - if client.read_run_state().name == "calling_home": - return "calling_home" + run = client.read_run_state().name + if run == "calling_home" or run.startswith("unknown"): + return run # the core refuses both; a write waits for a known state block = client.get_call_home_content() reads.append(block) return "enabled" if block[5] & 0x01 else "disabled" diff --git a/tests/test_device_access_s4_call_home.py b/tests/test_device_access_s4_call_home.py index 564d27f..6711810 100644 --- a/tests/test_device_access_s4_call_home.py +++ b/tests/test_device_access_s4_call_home.py @@ -94,3 +94,23 @@ def test_s4_even_if_already_sends_the_no_op_write(): r = change(fake, enabled=True, even_if_already=True) assert len(fake.executed) == 1 assert any("changes nothing" in w for w in r.plan["warnings"]) + + + +def test_s4_unknown_run_state_sends_nothing(): + # Final review #3: a run state never measured is refused, as start/stop does. + from sfm.device_access.errors import UnitStateUnknown + fake = FakeMicromateClient(run_states=[0x07]) + with pytest.raises(UnitStateUnknown): + change(fake, enabled=False) + assert fake.executed == [] + + +def test_a_lost_ack_still_records_what_the_unit_holds(): + # Final review #4: the lost-ack path is where the operator most needs it. + import micromate.protocol as mm_proto + fake = FakeMicromateClient(execute_error=mm_proto.TimeoutError("lost")) + r = change(fake, enabled=False) + assert (r.outcome, r.sent) == ("failed", True) + snap = r.detail["snapshot"] + assert snap["changed_offsets"] == [] and snap["before"] == snap["after"] # the write did not land