fix(sfm): a stale Series-3 id is refused before the full download, not after

Series3Adapter.download_event compared the timestamp only after
download_event_by_key had run the whole 5A.  A stale id cost a full
download of the wrong event (minutes on cellular), could surface the
64 KB-page 502 instead of 409, and a lost 0C reply was reported as "the
unit was erased".

- get_events() gains only_key_timestamp: with only_key, if the target's 0C
  says another time (or none), the target takes the existing bypass --
  no 1F(arm), no POLL, no 5A.  A new branch only; it adds no command.
- download_event_by_key(key4, *, expected_timestamp=None) passes it on.
- The adapter converts the id's stamp to that ISO form and checks, in
  order: no event -> 404; no readable 0C timestamp -> 502 MalformedReply
  ("could not read ... record (0C)"); a different time -> 409 StaleEvent;
  no frames -> 502.
- The fake's download mirrors the real walk (no frames on a mismatch) and
  records what it was asked for.
- Legacy get_events() (none of the new options) is pinned by a test of
  its exact call sequence and 5A arguments, written before the change.

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:38:34 -04:00
1 parent 157fcc51a7
commit 01b9188f25
5 files changed
+141 -12

No files matched your search

+23 -3
View File
@@ -450,7 +450,7 @@ class MiniMateClient:
proto.confirm_erase_all()
log.info("delete_all_events: erase confirmed — device memory cleared")
def get_events(self, full_waveform: bool = False, debug: bool = False, stop_after_index: Optional[int] = None, skip_waveform_for_keys: Optional[set] = None, skip_waveform_for_events: Optional[dict] = None, extra_chunks_after_metadata: int = 1, headers_only: bool = False, only_key: Optional[bytes] = None) -> list[Event]:
def get_events(self, full_waveform: bool = False, debug: bool = False, stop_after_index: Optional[int] = None, skip_waveform_for_keys: Optional[set] = None, skip_waveform_for_events: Optional[dict] = None, extra_chunks_after_metadata: int = 1, headers_only: bool = False, only_key: Optional[bytes] = None, only_key_timestamp: Optional[str] = None) -> list[Event]:
"""
Download all stored events from the device using the confirmed
1E → 0A → 0C → 5A → 1F event-iterator protocol.
@@ -483,6 +483,11 @@ class MiniMateClient:
only_key: download only the event with this key (in full, per
full_waveform); walk the others without their 5A, and stop once
that event is read.
only_key_timestamp: with only_key, the ISO timestamp (as
`_event_timestamp_iso` formats it, e.g. "2026-10-01T08:00:00") the
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.
"""
proto = self._require_proto()
@@ -586,6 +591,14 @@ class MiniMateClient:
# read; 1F(arm) + POLL + 5A are not sent, and 1F(browse) below is
# safe because no 5A was ever started.
skip_5a = True
elif (only_key is not None and only_key_timestamp is not None
and actual_ts != only_key_timestamp):
# The event at this key is not the one asked for -- erased or
# wrapped since it was listed, or its 0C reply was lost. Do
# not download it; the caller reports which.
log.info("get_events: key=%s is from %r, not %r -- no 5A",
cur_key.hex(), actual_ts, only_key_timestamp)
skip_5a = True
arm_key4: Optional[bytes] = None
a5_ok = False
@@ -765,14 +778,21 @@ class MiniMateClient:
"""
return self.get_events(headers_only=True)
def download_event_by_key(self, key4: bytes) -> Optional[Event]:
def download_event_by_key(self, key4: bytes, *, expected_timestamp: Optional[str] = None) -> Optional[Event]:
"""Download ONE event in full; walk past the events before it without
their 5A. None if no full record has that key.
`get_events(stop_after_index=N)` downloads events 0..N in full and keeps
one; this downloads only the one asked for.
`expected_timestamp` (ISO, as `_event_timestamp_iso` writes it): the
time the event was listed with. If the 0C read at that key says
otherwise -- or could not be read -- the event comes back without its
5A (`_a5_frames` None) and nothing is downloaded; the caller compares
the timestamp and says which.
"""
for ev in self.get_events(full_waveform=True, only_key=key4):
for ev in self.get_events(full_waveform=True, only_key=key4,
only_key_timestamp=expected_timestamp):
if ev._waveform_key == key4:
return ev
return None