fix(sfm): the Series-3 header walk fails loudly, and listing uses the normal timeout

One lost 1F reply made the list return 1 of 3 events as a 200, and on a
download gave a false 404.  The list also ran on the 120 s download
timeout, so each lost reply stalled the walk 120 s with the unit locked.

- get_events() strict mode (headers_only, or only_key): a lost 0A reply,
  a lost 1F(browse) reply on the success path and on the partial-record
  path all re-raise their ProtocolError; a 1F that hands back a key
  already walked raises ProtocolError("event chain did not advance ...").
  Legacy calls (none of the new options) behave exactly as before --
  pinned by tests of the same three faults under get_events().
- Series3Adapter.list_events runs on the default timeout (10 s on TCP);
  download_event keeps 120 s.
- FakeProto gains optional lost-reply injection; its default is unchanged.

The repeat-key guard sits at the loop head, which is reached only for a
non-null key -- equivalent to checking each 1F's key, in one place.

Read-only; nothing new on the wire.  No codec, store or DB change.

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-21 17:39:54 -04:00
1 parent 01b9188f25
commit 59dff06f1c
4 files changed
+97 -3

No files matched your search

+18
View File
@@ -488,8 +488,15 @@ class MiniMateClient:
event was listed with. If the 0C at only_key says another time, or
gave none, that event is returned without its 5A -- 1F(arm), POLL
and 5A are not sent -- and the caller reports which.
Strict mode (headers_only, or only_key): a list or a one-event download
must be all or nothing, so a lost 0A or 1F reply re-raises its
ProtocolError instead of skipping an event or ending the walk early,
and a 1F that returns a key already walked raises ProtocolError.
Without those options (legacy) the walk is exactly as it was.
"""
proto = self._require_proto()
strict = headers_only or only_key is not None
log.info("get_events: requesting first event (SUB 1E)")
try:
@@ -506,6 +513,7 @@ class MiniMateClient:
events: list[Event] = []
idx = 0
seen: set = set() # strict mode: keys walked, to catch a chain that does not advance
# Legacy bare-key skip set is deprecated: the device's key counter
# resets to 0x01110000 after every memory erase, so a key in this set
@@ -523,6 +531,10 @@ class MiniMateClient:
while data8[4:8] != b"\x00\x00\x00\x00":
cur_key = key4 # key for this event's 0A/1E-arm/0C/5A calls
if strict and cur_key in seen:
raise ProtocolError(
f"event chain did not advance: key {cur_key.hex()} came back twice")
seen.add(cur_key)
log.info("get_events: record %d key=%s", idx, cur_key.hex())
ev = Event(index=idx)
@@ -540,6 +552,8 @@ class MiniMateClient:
)
proceed = False
except ProtocolError as exc:
if strict:
raise
log.warning(
"get_events: 0A failed for key=%s: %s — skipping",
cur_key.hex(), exc,
@@ -702,6 +716,8 @@ class MiniMateClient:
key4.hex(), data8[4:8].hex(),
)
except ProtocolError as exc:
if strict:
raise
log.warning("get_events: 1F(browse) failed: %s — stopping", exc)
key4 = b"\x00\x00\x00\x00"
data8 = b"\x00\x00\x00\x00\x00\x00\x00\x00"
@@ -761,6 +777,8 @@ class MiniMateClient:
key4.hex(), data8[4:8].hex(),
)
except ProtocolError as exc:
if strict:
raise
log.warning("get_events: 1F failed: %s — stopping iteration", exc)
break
+3 -1
View File
@@ -172,7 +172,9 @@ class Series3Adapter:
self._check(info.serial, serial, address)
return EventList(serial=serial, series=3,
events=[_s3_ref(ev, serial) for ev in client.list_event_headers()])
return self._run(address, op, timeout=S3_EVENT_TIMEOUT)
# The default timeout: a header walk is small replies only, so a lost
# one costs 10 s (TCP) with the unit locked, not 120 s.
return self._run(address, op)
def download_event(self, address: UnitAddress, serial: str, key: bytes,
stamp: Optional[str]) -> DownloadedEvent:
+9 -2
View File
@@ -44,15 +44,22 @@ def test_listing_maps_each_header_into_the_shared_shape():
assert e.filename and e.filename.startswith("T193")
def test_listing_uses_the_header_walk_and_the_long_timeout():
def test_listing_uses_the_header_walk_and_the_default_timeout():
# A header walk is small replies only; a lost one should cost 10 s, not 120 s.
fake = FakeMiniMateClient()
a = adapter(fake)
a.list_events(TCP, "BE18193")
assert "list_event_headers" in fake.calls and "download_event_by_key" not in fake.calls
assert a.seen == [{"timeout": 120.0}]
assert a.seen == [{}]
assert fake.closed == 1
def test_downloading_uses_the_long_timeout():
a = adapter(FakeMiniMateClient())
a.download_event(TCP, "BE18193", FIRST, None)
assert a.seen == [{"timeout": 120.0}]
def test_listing_refuses_a_different_unit():
with pytest.raises(WrongUnit):
adapter(FakeMiniMateClient(serial="BE11529")).list_events(TCP, "BE18193")
+67
View File
@@ -11,6 +11,7 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import minimateplus.client as mc # noqa: E402
from minimateplus.client import MiniMateClient # noqa: E402
from minimateplus.models import Timestamp # noqa: E402
from minimateplus.protocol import ProtocolError # noqa: E402
END = b"\x00" * 8
KEYS = [bytes.fromhex(k) for k in ("01110000", "0111245a", "01114290")]
@@ -200,3 +201,69 @@ def test_a_target_whose_0c_reply_was_lost_gets_no_5a(client):
ev = client(proto).download_event_by_key(KEYS[1], expected_timestamp="2026-10-02T09:30:15")
assert [c for c in proto.calls if c[0] == "5A"] == []
assert ev is not None and ev.timestamp is None and ev._a5_frames is None
# ── the strict walk: listing and one-event download fail loudly ───────────────
# A lost reply must not become a short list (200) or a false 404.
def test_a_lost_1f_reply_fails_the_list_rather_than_shortening_it(client):
lost = ProtocolError("no reply to 1F")
with pytest.raises(ProtocolError) as ei:
client(FakeProto(fail={("1F-browse", 2): lost})).list_event_headers()
assert ei.value is lost
def test_a_lost_1f_reply_fails_the_download_rather_than_reporting_no_event(client):
lost = ProtocolError("no reply to 1F")
proto = FakeProto(fail={("1F-browse", 2): lost})
with pytest.raises(ProtocolError) as ei:
client(proto).download_event_by_key(KEYS[2])
assert ei.value is lost
assert [c for c in proto.calls if c[0] == "5A"] == []
@pytest.mark.parametrize("walk", ["list", "download"])
def test_a_lost_0a_reply_fails_the_walk(client, walk):
lost = ProtocolError("no reply to 0A")
c = client(FakeProto(fail={("0A", 2): lost}))
with pytest.raises(ProtocolError) as ei:
c.list_event_headers() if walk == "list" else c.download_event_by_key(KEYS[2])
assert ei.value is lost
def test_a_lost_1f_reply_after_a_partial_record_fails_the_list(client):
lost = ProtocolError("no reply to 1F")
with pytest.raises(ProtocolError) as ei:
client(FakeProto(partial={KEYS[1]}, fail={("1F-browse", 2): lost})).list_event_headers()
assert ei.value is lost
@pytest.mark.parametrize("walk", ["list", "download"])
def test_a_chain_that_does_not_advance_fails_the_walk(client, walk):
proto = FakeProto(keys=[KEYS[0], KEYS[1], KEYS[0]])
c = client(proto)
with pytest.raises(ProtocolError, match="did not advance.*01110000"):
c.list_event_headers() if walk == "list" else c.download_event_by_key(KEYS[2])
assert proto.calls.count(("0A", KEYS[0])) == 1 # stopped before re-reading it
# ── ... while legacy get_events() keeps doing exactly what it did ────────────
def test_legacy_get_events_still_returns_what_it_read_when_a_1f_reply_is_lost(client):
proto = FakeProto(fail={("1F-browse", 2): ProtocolError("no reply to 1F")})
events = client(proto).get_events()
assert [e._waveform_key for e in events] == KEYS[:2]
assert _subs(proto) == ["1E"] + _LEGACY_PER_EVENT * 2
def test_legacy_get_events_still_skips_an_event_whose_0a_reply_is_lost(client):
proto = FakeProto(fail={("0A", 2): ProtocolError("no reply to 0A")})
events = client(proto).get_events()
assert [e._waveform_key for e in events] == [KEYS[0], KEYS[2]]
assert _subs(proto) == ["1E"] + _LEGACY_PER_EVENT + ["0A", "1F"] + _LEGACY_PER_EVENT
def test_legacy_get_events_still_walks_a_repeated_key(client):
proto = FakeProto(keys=[KEYS[0], KEYS[1], KEYS[0]])
events = client(proto).get_events()
assert [e._waveform_key for e in events] == [KEYS[0], KEYS[1], KEYS[0]]