fix(minimateplus): size the 5A chunk cap from STRT, refuse a misread STRT, TERM only a tail

Follow-ups from the review of 887e75d (verdict: keep, with fixes; no critical).

- The cap: a caller's max_chunks (client.py passes 128) now bounds only a walk
  whose end is unknown.  With STRT it cut every event past ~66 KB short --
  2,545 of 63,500 archived histograms are bigger (the review's count, DL2).
  Strict raised; legacy sent a TERM for most of the event, or a bare ValueError.
- A STRT claiming less than one 0x200 chunk, or more than 4 MiB (a sanity bound,
  not a measured limit), is refused as a misread, in both modes -- once a bare
  ValueError and a 500.
- TERM is sent only for a true tail (< 0x200).  After an early end the legacy
  walk returns what arrived and sends none.
- build_5a_request wraps build_bw_frame; the old 5A helpers are marked
  superseded (kept for tests/test_5a_protocol.py) and their imports dropped.

Tests: all 8 recorded events (were 4) walk exactly as Blastware and rebuild its
own files; an end key holding escaped bytes; a 300 KB event past a 128 cap; both
misread-STRT refusals; the 1A frames BE9441 answered post-fix; the checksum
vectors retargeted.  Mutants the review saw survive (no STRT un-escape; a
standard-request escape revert) and 3 new ones are each caught.

Measured on the way: a reply's checksum is SUM8 of its unescaped payload,
748/748.  The 'S3 checksums frequently fail' note was the parser summing its
own kept escape bytes; noted, parser unchanged.

Docs: the crossing count corrected to four (events 1, 3, 5, 7); the remaining
stale 5A sections in CLAUDE.md annotated; the write-frame suspicion recorded as
inferred only.  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-26 15:16:10 -04:00
1 parent f61fc3cc78
commit 8acb9512a9
15 files changed
+285 -73

No files matched your search

+11 -1
View File
@@ -9,7 +9,7 @@ to disk. Both directions, raw:
| `blastware_requests.bin` | Blastware → unit (748 requests, each after a `41 03` SESSION_RESET) | 17,757 B |
| `unit_replies.bin` | unit → Blastware | 364,972 B |
Events (key → end), from Blastware's 5A walks. Three cross a 64 KB page:
Events (key → end), from Blastware's 5A walks. Four cross a 64 KB page (1, 3, 5, 7):
| event | start key | end (STRT) | 5A requests |
|---|---|---|---|
@@ -35,8 +35,18 @@ its chunks at 0x0200 and 0x0400, escaped (`10 02 00`, `10 04 00`).
|---|---|---|
| `K441LTOF.ZI0` | 0 (`01110000`) | no |
| `K441LTOF.ZQ0` | 1 (`01118a2c`) | yes, into 0x12 |
| `K441LTOF.ZV0` | 2 (`011216c4`) | no |
| `K441LTOG.000` | 3 (`0112a0b6`) | yes, into 0x13 |
| `K441LTOG.050` | 4 (`011329d2`) | no |
| `K441LTOG.0A0` | 5 (`0113b072`) | yes, into 0x14 |
| `K441LTOG.0L0` | 6 (`01143046`) | no |
| `K441LTOG.0S0` | 7 (`0114b0b2`) | yes, into 0x15 |
After the fix (887e75d), SFM's own live downloads of all 8 events were byte-identical to
Blastware's files (sha256), the 4 crossings included.
The unit's replies (measured on all 748): every 02/03/04/10 is escaped with a `10`, exactly as
in requests, and the checksum is SUM8 of the **unescaped** payload, 748/748. `S3FrameParser`
keeps the escape bytes of `10 02/03/04` in its output and sums those too, which is why its
`checksum_valid` is False on 502 of the 540 A5 replies. The "S3 checksums frequently fail"
note in `protocol._validate_frame` describes the parser, not the unit.
+3 -2
View File
@@ -11,8 +11,9 @@ import minimateplus.protocol as mp
from minimateplus.framing import S3FrameParser
_DIR = Path(__file__).parent / "fixtures" / "minimate-bench-10-25-26"
FIX = _DIR / "BE9441_event1_0111646a_rx.bin" # event 1 (non-first): no metadata pages
FIX_EVENT0 = _DIR / "BE9441_event0_01110000_rx.bin" # event 0: metadata pages 0x1002 / 0x1004
FIX = _DIR / "BE9441_event1_0111646a_rx.bin" # event 1 (non-first)
FIX_EVENT0 = _DIR / "BE9441_event0_01110000_rx.bin" # event 0 (its chunks at 0x0200 / 0x0400 were
# once read as "metadata pages 0x1002 / 0x1004")
KEY = bytes.fromhex("0111646a")
STAMP = "2026-10-25T14:10:49"
UNIT_PEAKS = {"Tran": 0.030, "Vert": 0.095, "Long": 0.020} # the unit's own 0C peaks, in/s
+125 -5
View File
@@ -1,6 +1,6 @@
"""SFM's MiniMate requests and 5A walk, against Blastware's own (recorded 2026-10-26).
Blastware downloaded all 8 of BE9441's events -- three of which cross a 64 KB page
Blastware downloaded all 8 of BE9441's events -- four of which cross a 64 KB page
-- while a bridge recorded both directions (fixture README). Every one of its 748
requests is the standard 16-byte payload with a SUM8 checksum and every 02 / 03 /
04 / 10 byte DLE-escaped; a 5A request carries a length (0x0200, or the remainder
@@ -23,7 +23,8 @@ import minimateplus.protocol as mp # noq
from minimate_replay import fast_clock # noqa: E402
from minimateplus.blastware_file import extract_body_bytes, write_blastware_file # noqa: E402
from minimateplus.models import Event # noqa: E402
from minimateplus.framing import S3FrameParser, build_5a_request, build_bw_frame, waveform_key_params # noqa: E402
from minimateplus.framing import (S3FrameParser, build_5a_request, build_bw_frame, # noqa: E402
parse_strt_end_key, parse_strt_end_offset, waveform_key_params)
from minimateplus.waveform_codec import STREAM_END_ID, decode_waveform_v2, walk_records # noqa: E402
FIX = Path(__file__).parent / "fixtures" / "minimate-bench-10-26-26-blastware"
@@ -140,7 +141,19 @@ def _fast(monkeypatch):
fast_clock(monkeypatch)
@pytest.mark.parametrize("key", ["01110000", "01118a2c", "0112a0b6", "0114b0b2"])
# BE9441's 8 events; 1, 3, 5 and 7 cross into the next 64 KB page.
EVENTS = [("01110000", "K441LTOF.ZI0"), ("01118a2c", "K441LTOF.ZQ0"), ("011216c4", "K441LTOF.ZV0"),
("0112a0b6", "K441LTOG.000"), ("011329d2", "K441LTOG.050"), ("0113b072", "K441LTOG.0A0"),
("01143046", "K441LTOG.0L0"), ("0114b0b2", "K441LTOG.0S0")]
def test_the_crossing_events_are_the_ones_named():
ends = {k: addr_len(blastware_walk(int(k, 16))[-1]) for k, _ in EVENTS}
crossing = [k for k, (a, n) in ends.items() if (a + n) >> 16 != int(k, 16) >> 16]
assert crossing == ["01118a2c", "0112a0b6", "0113b072", "0114b0b2"]
@pytest.mark.parametrize("key", [k for k, _ in EVENTS])
def test_the_walk_sends_exactly_blastwares_requests_and_the_event_is_whole(key):
t = ByAddress()
frames = mp.MiniMateProtocol(t).read_bulk_waveform_stream(
@@ -155,8 +168,7 @@ def test_the_walk_sends_exactly_blastwares_requests_and_the_event_is_whole(key):
assert len({len(dec[c]) for c in ("Tran", "Vert", "Long")}) == 1
@pytest.mark.parametrize("key,bw", [("01110000", "K441LTOF.ZI0"), ("01118a2c", "K441LTOF.ZQ0"),
("0112a0b6", "K441LTOG.000"), ("0114b0b2", "K441LTOG.0S0")])
@pytest.mark.parametrize("key,bw", EVENTS)
def test_the_file_built_from_the_walk_is_blastwares_own(key, bw, tmp_path):
frames = mp.MiniMateProtocol(ByAddress()).read_bulk_waveform_stream(
bytes.fromhex(key), stop_after_metadata=False, include_terminator=True, strict=True)
@@ -165,3 +177,111 @@ def test_the_file_built_from_the_walk_is_blastwares_own(key, bw, tmp_path):
path = tmp_path / bw
write_blastware_file(ev, frames, path)
assert path.read_bytes() == (FIX / "blastware" / bw).read_bytes()
# -- the requests BE9441 answered after the escape fix ---------------------------
def test_the_compliance_reads_be9441_answered_with_the_escaped_0x04():
# SUB 1A frames B and C are the only fixed requests whose bytes the fix changed
# (their 0x0400 length went out bare). These exact frames got full replies,
# 2,125 bytes, decoded correctly (bench 2026-10-26 14:38:43).
assert build_bw_frame(0x1A, 0x0400, bytes(7) + b"\x64\x00\x00").hex() == \
"41021010001a00100400000000000000006400009203"
assert build_bw_frame(0x1A, 0x0400, bytes([0, 0, 4, 0, 0, 0, 0, 0x64, 0, 0])).hex() == \
"41021010001a0010040000001004000000006400009603"
# -- synthetic replies: the unit's own encoding, with a chosen STRT end ----------
def _escape(b: bytes) -> bytes:
"""The unit's encoding (measured on 748 replies): 10 before 02, 03, 04 and 10."""
return b"".join(bytes([0x10, x]) if x in (0x02, 0x03, 0x04, 0x10) else bytes([x]) for x in b)
def _parse(wire: bytes):
frames = S3FrameParser().feed(wire)
assert len(frames) == 1
return frames[0]
def _logical(reply: bytes) -> bytes:
"""A recorded reply's payload, unescaped and without its checksum. Each
recorded reply opens with the unit's `41 03 41` before its `10 02`."""
return _unstuff(reply[reply.index(b"\x10\x02") + 2:-1])[:-1]
def first_reply_ending_at(end: int, event: int = 1) -> bytes:
"""A recorded first-chunk reply with STRT's end key set to `end`, re-encoded."""
q = FIVE_A.index(blastware_walk(int(EVENTS[event][0], 16))[0])
payload = bytearray(_logical(unit_a5_replies()[q]))
i = payload.find(b"STRT")
payload[i + 6:i + 10] = end.to_bytes(4, "big")
return b"\x10\x02" + _escape(bytes(payload) + bytes([sum(payload) & 0xFF])) + b"\x03"
def test_the_reply_encoder_is_the_units():
q = FIVE_A.index(blastware_walk(0x01118a2c)[0])
recorded = unit_a5_replies()[q]
payload = _logical(recorded)
i = payload.find(b"STRT")
end = int.from_bytes(payload[i + 6:i + 10], "big")
assert _parse(first_reply_ending_at(end)).data == _parse(recorded).data
def test_an_end_key_holding_escaped_bytes_reads_whole():
# None of the 8 recorded end keys holds a 02/03/04/10; this one holds 03 and 10.
data = _parse(first_reply_ending_at(0x01120310)).data
assert parse_strt_end_key(data) == 0x01120310
assert parse_strt_end_offset(data) != 0x0310 # the old reader's misread
class Synthetic:
"""Answers the first 5A request with `first`, each chunk with a recorded chunk
reply and the TERM with a recorded TERM reply; records (address, length)."""
def __init__(self, first: bytes):
replies = unit_a5_replies()
walk = blastware_walk(0x01118a2c)
self.first, self.sent, self._buf = first, [], b""
self.chunk = replies[FIVE_A.index(walk[1])]
self.term = replies[FIVE_A.index(walk[-1])]
def write(self, data):
a, n = addr_len(bytes(data))
self.sent.append((a, n))
self._buf += self.first if len(self.sent) == 1 else (self.chunk if n == 0x200 else self.term)
def read(self, n):
out, self._buf = self._buf[:n], self._buf[n:]
return out
def walk(first: bytes, start: int = 0x01118a2c, **kw):
t = Synthetic(first)
frames = mp.MiniMateProtocol(t).read_bulk_waveform_stream(
start.to_bytes(4, "big"), stop_after_metadata=False, include_terminator=True, **kw)
return t.sent, frames
def test_the_walk_ends_on_an_escaped_end_key():
sent, _ = walk(first_reply_ending_at(0x01120310), strict=True)
# chunks step from the start (...8a2c), so the TERM is at 0x0112022C for 0xE4
assert sent[-1] == (0x0112022C, 0x01120310 - 0x0112022C) # TERM: the tail, exactly
def test_a_large_event_walks_whole_past_the_callers_cap():
# 2,545 of 63,500 archived histograms exceed the ~66 KB a 128-chunk cap allowed.
start, end = 0x01118a2c, 0x01118a2c + 300_000
sent, frames = walk(first_reply_ending_at(end), start, strict=True, max_chunks=128)
n = (end - start) // 0x200
assert sent[:-1] == [(start + 0x200 * k, 0x200) for k in range(n)]
assert sent[-1] == (start + 0x200 * n, (end - start) % 0x200)
assert len(frames) == len(sent)
@pytest.mark.parametrize("strict", [True, False])
@pytest.mark.parametrize("size,why", [(0x100, "less than one 0x200 chunk"),
(0x500000, "more than any unit holds")])
def test_a_misread_strt_is_refused_in_both_modes(size, why, strict):
with pytest.raises(mp.ProtocolError, match=why):
walk(first_reply_ending_at(0x01118a2c + size), strict=strict)
+3 -5
View File
@@ -17,14 +17,12 @@ import pytest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from minimateplus.framing import build_5a_frame, bulk_waveform_params # noqa: E402
KEY = bytes.fromhex("01122f06")
CHUNK_OFFSET = 0x1002
from minimateplus.framing import build_5a_request # noqa: E402
def chunk(counter: int) -> str:
return build_5a_frame(CHUNK_OFFSET, bulk_waveform_params(KEY, counter)).hex()
# Event 01122f06's chunk reads, length 0x0200 (once read as an "offset 0x1002")
return build_5a_request(0x01120000 + counter, 0x0200).hex()
@pytest.mark.parametrize("counter,wire", [
+21 -3
View File
@@ -84,9 +84,17 @@ def test_strict_raises_on_a_terminator_mid_stream():
stream(r, strict=True)
def test_strict_raises_at_the_chunk_cap():
with pytest.raises(mp.ProtocolError, match="chunk cap"):
stream(a5_replies(), max_chunks=5, strict=True)
def test_a_known_end_is_not_cut_short_by_the_callers_chunk_cap():
# STRT bounds the walk; a cap (client.py passes 128) once cut every event past
# ~66 KB short -- a large histogram, say. It now bounds only a walk with no end.
assert len(stream(a5_replies(), max_chunks=5, strict=True)) == 51
def test_the_cap_still_bounds_a_legacy_walk_with_no_strt():
r = a5_replies()
r[0] = r[0].replace(b"STRT", b"XXXX")
frames = stream(r, max_chunks=5)
assert len(frames) == 1 + 5 + 1 # first chunk, 5 capped chunks, TERM
def test_strict_raises_when_the_probe_has_no_strt():
@@ -114,3 +122,13 @@ def test_strict_refuses_a_strt_whose_end_is_not_after_its_start():
p.read_bulk_waveform_stream(KEY, stop_after_metadata=False, include_terminator=True,
strict=True)
assert p._transport.sent == 1 # the first chunk, and nothing after
def test_the_legacy_walk_sends_no_term_for_most_of_an_event():
# After an early end, the "remainder" is most of the event, not a tail; a TERM
# for it once asked for kilobytes (or, past 64 KB, raised a bare ValueError).
t = Replay(a5_replies(), silent_from=40)
frames = mp.MiniMateProtocol(t).read_bulk_waveform_stream(
KEY, stop_after_metadata=False, include_terminator=True)
assert len(frames) == 40
assert t.sent == 41 # 40 answered, the one met by silence, and no TERM