fix(minimateplus): the boot-aware first POLL resets the parser; a drained or long-trailed banner is seen

Review of d73a27d: _await_poll_or_boot no longer reset the parser and pending
frames the way _recv_one(reset_parser=True) did, so a reused protocol object
with half a frame left over failed startup (UnexpectedResponse A4 vs A5).
Restored.  Also: the banner check now runs before the tail is trimmed (a read
carrying >48 bytes after the banner lost it), and a banner caught during the
200 ms drain is reported, so startup goes straight to the re-poll loop instead
of the full timeout.  Tests for all three.

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-25 18:43:49 -04:00
1 parent 151782365b
commit f6166b4b04
2 files changed
+52 -6

No files matched your search

+22 -4
View File
@@ -212,7 +212,7 @@ class MiniMateProtocol:
ProtocolError: if either POLL step fails.
"""
log.debug("startup: draining boot string")
self._drain_boot_string()
booting = self._drain_boot_string()
# Send session-reset signal (ACK+ETX) before the first POLL probe.
# Confirmed from 4-8-26 BW TX captures: Blastware always sends this
@@ -223,7 +223,15 @@ class MiniMateProtocol:
log.debug("startup: POLL probe")
self._send(POLL_PROBE)
probe_rsp = self._await_poll_or_boot(self._recv_timeout)
try:
# A banner already drained means the unit is booting: a short wait,
# then the re-poll loop, rather than the full timeout.
probe_rsp = self._await_poll_or_boot(
self._BOOT_POLL_S if booting else self._recv_timeout)
except TimeoutError:
if not booting:
raise
probe_rsp = None
if probe_rsp is None:
probe_rsp = self._poll_until_booted()
log.debug(
@@ -1581,6 +1589,8 @@ class MiniMateProtocol:
"""The first POLL probe's reply -- or None the moment the unit sends its
boot banner, because a POLL sent before the banner is never answered."""
expected = _expected_rsp_sub(SUB_POLL)
self._parser.reset() # as _recv_one(reset_parser=True) did here
self._pending_frames.clear()
deadline = time.monotonic() + timeout
tail = b""
while time.monotonic() < deadline:
@@ -1594,9 +1604,10 @@ class MiniMateProtocol:
self._pending_frames.extend(frames[1:])
self._validate_frame(frames[0], expected)
return frames[0]
tail = (tail + chunk)[-64:]
tail += chunk
if self._BOOT_BANNER in tail:
return None
tail = tail[-64:] # trimmed after the check, so a long read keeps it
raise TimeoutError(
f"No S3 frame received within {timeout:.1f}s (expected SUB 0x{expected:02X})")
@@ -1618,7 +1629,7 @@ class MiniMateProtocol:
f"the unit sent its boot message but did not answer a POLL within "
f"{self._BOOT_GRACE_S:.0f} s")
def _drain_boot_string(self, drain_ms: int = 200) -> None:
def _drain_boot_string(self, drain_ms: int = 200) -> bool:
"""
Read and discard any boot-string bytes ("Operating System") the device
may send before entering binary protocol mode.
@@ -1626,14 +1637,21 @@ class MiniMateProtocol:
We simply read with a short timeout and throw the bytes away. The
S3FrameParser's IDLE state already handles non-frame bytes gracefully,
but it's cleaner to drain them explicitly before the first real frame.
Returns whether the boot banner was among them (the unit is booting).
"""
deadline = time.monotonic() + (drain_ms / 1000)
discarded = 0
tail, saw_banner = b"", False
while time.monotonic() < deadline:
chunk = self._transport.read(256)
if chunk:
discarded += len(chunk)
tail += chunk
saw_banner = saw_banner or self._BOOT_BANNER in tail
tail = tail[-len(self._BOOT_BANNER):] # a banner split across reads still matches
else:
time.sleep(0.005)
if discarded:
log.debug("drain_boot_string: discarded %d bytes", discarded)
return saw_banner