fix(minimateplus): refuse a settings write whose four fields would not read back as asked (#1 review)

_encode_compliance_config patches the raw SUB 1A bytes at offsets measured at
1024 sps (April 2026), where the escape before 04 sits between the mode and the
anchor.  b320a53 moved the decoder to the measured layout (unescaped: mode -7,
rate -6, interval -4, record time +6), which exposed the encoder: on BE9441's
recorded 10-25 Continuous / 4096 block a mode change rewrote the byte in front
of the mode, and on the 10-26 Histogram+Continuous / 2048 block a sample-rate
change landed one byte off.  The old decoder read the same wrong bytes, so such
a write looked as if it had taken.

The encoder now decodes its own result with the decoder and raises ValueError
when mode, sample rate, interval or record time would not read back as asked
(or a field not asked for would change) -- before push_config_raw sends a byte.
No write offset is moved: rewriting the encoder on unescaped bytes is #46's.

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-29 23:56:44 -04:00
1 parent 3a1d34ae92
commit a72d3a98a9
2 files changed
+66 -1

No files matched your search

+37 -1
View File
@@ -2122,7 +2122,13 @@ def _encode_compliance_config(
DLE-jitter shifts):
Anchor: b'\\xbe\\x80\\x00\\x00\\x00\\x00' (confirmed stable, both BE11529 and BE18189)
recording_mode → uint8 at anchor_pos - 8 (BOTH read and write)
⚠ 2026-10-29: these RAW offsets were measured at 1024 sps, where the escape
before `04` sits between the mode and the anchor. The decoder now reads
the four fields after unescaping (mode -7, rate -6, interval -4, record
time +6), so at any other layout these patch the wrong bytes. Until the
encoder is rewritten on unescaped bytes (#46), a payload whose four fields
do not read back as asked raises ValueError -- see the end of this function.
recording_mode → uint8 at anchor_pos - 8 (raw; right at 1024 sps only)
Values: 0x00=Single Shot, 0x01=Continuous, 0x03=Histogram, 0x04=Histogram+Continuous
NOTE: The byte at anchor_pos - 7 is always 0x10 (a DLE marker regenerated by
device firmware in every E5 response). It must NOT be overwritten during
@@ -2305,10 +2311,40 @@ def _encode_compliance_config(
_set_string(b"Seis Loc:", seis_loc)
_set_string(b"Extended Notes", notes)
# ── Read-back check: the payload must decode to what was asked ────────────
# The decoder is the measured layout (every recorded reply); these offsets are
# 1024-sps raw ones. A mode change on BE9441's 10-25 Continuous / 4096 block
# rewrote the byte in front of the mode and read back unchanged. Refuse it
# here, before push_config_raw sends anything.
asked = {"recording_mode": recording_mode, "sample_rate": sample_rate,
"histogram_interval_sec": histogram_interval_sec, "record_time": record_time}
if any(v is not None for v in asked.values()):
before, after = _settings_read_back(raw), _settings_read_back(bytes(buf))
if record_time is not None:
asked["record_time"] = struct.unpack(">f", struct.pack(">f", record_time))[0]
want = {k: (v if v is not None else before[k]) for k, v in asked.items()}
wrong = {k: (after[k], want[k]) for k in asked if after[k] != want[k]}
if wrong:
raise ValueError(
"_encode_compliance_config: the patched block would not read back as asked ("
+ ", ".join(f"{k} reads {got!r}, not {exp!r}" for k, (got, exp) in wrong.items())
+ ") -- refused, nothing built or sent. The encoder's offsets were measured at "
"1024 sps and need rewriting on unescaped bytes (#46)."
)
# 2-byte footer — checksum formula unknown; \x00\x00 confirmed accepted by device
return bytes(buf) + b"\x00\x00"
def _settings_read_back(data: bytes) -> dict:
"""Mode, sample rate, interval and record time as the decoder reads `data`."""
info = DeviceInfo(serial="", firmware_minor=0)
_decode_compliance_config_into(data, info)
cc = info.compliance_config
return {k: getattr(cc, k, None) if cc is not None else None
for k in ("recording_mode", "sample_rate", "histogram_interval_sec", "record_time")}
def _decode_compliance_config_into(data: bytes, info: DeviceInfo) -> None:
"""
Decode a 2090-byte SUB 1A (COMPLIANCE_CONFIG) response into a ComplianceConfig.
+29
View File
@@ -94,3 +94,32 @@ def test_a_reply_without_the_settings_block_reads_as_unknown():
cc = _decode(fe)
assert (cc.recording_mode, cc.sample_rate, cc.histogram_interval_sec, cc.record_time) == \
(None, None, None, None)
# The write side (review finding, 2026-10-29). _encode_compliance_config patches
# the RAW bytes at offsets measured at 1024 sps (April 2026), where an escape sits
# between the mode and the anchor. At other rates the mode is one byte later, so
# a mode change rewrote the byte in front of it. A payload that would not read
# back as asked is refused before anything is sent.
from minimateplus.client import _encode_compliance_config # noqa: E402
def _block(i):
return _settings_replies()[i].ljust(2126, b"\x00") # a whole block's length
@pytest.mark.parametrize("i, change", [
(0, {"recording_mode": 0}), # 10-25 Continuous / 4096: mode sits at -7, not -8
(1, {"sample_rate": 4096}), # 10-26 Histogram+Continuous / 2048: escaped mode shifts the rate
])
def test_a_change_that_would_not_read_back_is_refused(i, change):
with pytest.raises(ValueError, match="read back"):
_encode_compliance_config(_block(i), **change)
def test_a_change_that_reads_back_as_asked_is_built():
out = _encode_compliance_config(_block(0), record_time=7.0)
cc = _decode(out)
assert (cc.recording_mode, cc.sample_rate, cc.histogram_interval_sec, cc.record_time) == \
(1, 4096, 5, 7.0)