fix(settings): final-review fixes -- revision, resulting limits, logical writes

From the whole-branch review of phase 5:
- The preview's revision now covers the changes asked for, so a live
  change must be exactly what was previewed (any API caller, not only the
  page).  It covers what the preview showed -- the settings block (and a
  Micromate's setup name) -- not the blocks a write only echoes, which are
  re-read at send and sent as read (a MiniMate's trigger block byte 5
  differs by session, #46).
- The units' limits hold on the block that would result: a range, rate or
  mode change alone can no longer leave a held trigger, alarm or record
  time over them.
- Every MiniMate block read is logical, so push_config_raw no longer
  unescapes again (a job number of 4098, 10 02, was refused at Send); the
  preview runs the write's own block check, so a block it would refuse is
  shown as blocked.
- A MiniMate's monitoring refusal says a stop makes it call in (spec).

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-11-03 22:10:49 -05:00
1 parent 385716d8e1
commit 08d0a6c0d4
9 files changed
+209 -43

No files matched your search

+2 -1
View File
@@ -233,7 +233,8 @@ class FakeMiniMateClient(_Base):
blocks["compliance"] = s3_device_info_answer()
return blocks
def push_config_raw(self, event_index, compliance, trigger, waveform) -> None:
def push_config_raw(self, event_index, compliance, trigger, waveform, *, escaped=False) -> None:
self.pushed_escaped = escaped
self.pushed = (bytes(event_index), bytes(compliance), bytes(trigger), bytes(waveform))
self._write("push_config_raw")
self.settings_blocks["compliance"] = bytes(compliance)[:2090] # read back as the unit stores it
+77
View File
@@ -154,3 +154,80 @@ def test_push_config_raw_precheck_raises_image_refused():
with pytest.raises(ImageRefused):
c.push_config_raw(b["event_index"], b["compliance"][:2089], b["trigger"], b["waveform"])
assert t.sent == []
# Final review #1: the revision covers the changes previewed, not only the unit.
def test_s3_a_revision_covers_the_changes_previewed():
fake = FakeMiniMateClient()
rev = change(fake, {"job_number": 7}, dry_run=True).plan["revision"]
with pytest.raises(SettingsChanged):
change(fake, {"geo_trigger_ips": 0.05}, revision=rev)
assert fake.writes == []
# Ruling: the trigger block's byte 5 "differs by unit and session" (#46); the three
# echoed blocks are re-read at send and sent as read, so they are not in the revision.
def test_s3_an_echoed_block_moving_does_not_stale_the_preview():
fake = FakeMiniMateClient()
rev = change(fake, {"geo_trigger_ips": 0.05}, dry_run=True).plan["revision"]
trig = bytearray(fake.settings_blocks["trigger"])
trig[5] = 0x03
fake.settings_blocks["trigger"] = bytes(trig)
assert change(fake, {"geo_trigger_ips": 0.05}, revision=rev).verified == "ok"
assert fake.pushed[2] == bytes(trig) # sent as read at send time
# Final review #4 (spec): the refusal says a stop makes the unit call in.
def test_s3_monitoring_refusal_says_a_stop_calls_in():
with pytest.raises(UnitStateUnknown) as e:
change(FakeMiniMateClient(monitoring_settings=True), {"geo_trigger_ips": 0.05}, dry_run=True)
assert "call in" in str(e.value) and "call-home" in str(e.value)
# Final review #3: the blocks are logical bytes; a block holding a literal
# 10 02 / 10 03 / 10 04 (job number 4098 = 0x1002) must be written intact, and a
# block the write would refuse must show in the preview, not only at Send.
def test_s3_a_bad_echoed_block_is_blocked_in_the_preview():
fake = FakeMiniMateClient()
fake.settings_blocks["event_index"] = fake.settings_blocks["event_index"][:-1]
r = change(fake, {"geo_trigger_ips": 0.05}, dry_run=True)
assert r.plan["blocked"] and r.plan["steps"] == []
from sfm.device_access.errors import ChangeRefused
with pytest.raises(ChangeRefused):
change(fake, {"geo_trigger_ips": 0.05}, revision=r.plan["revision"])
assert fake.writes == []
def _acking():
from minimateplus.framing import build_bw_write_frame # noqa: F401 (frames are parsed below)
from test_minimate_write_frames import _payload
class Acks:
def __init__(self):
self.sent, self._buf = [], b""
def write(self, data):
data = bytes(data)
self.sent.append(data)
sub = _payload(data)[2]
body = bytes([0x00, 0x10, 0xFF - sub, 0x00, 0x00]) + bytes(11)
body += bytes([sum(body) & 0xFF])
self._buf += b"\x10\x02" + body.replace(b"\x10", b"\x10\x10") + b"\x03"
def read(self, n):
out, self._buf = self._buf[:n], self._buf[n:]
return out
return Acks()
def test_push_config_raw_writes_a_logical_block_with_a_literal_escape_byte_intact():
from test_minimate_write_frames import _payload
t = _acking()
c = MiniMateClient("unused")
c._proto = mp.MiniMateProtocol(t, recv_timeout=0.5)
b = s3_settings_blocks()
block = encode_setup(b["compliance"], 3, {"job_number": 4098}) # [40:42] = 10 02
assert block[40:42] == b"\x10\x02"
c.push_config_raw(b["event_index"], block, b["trigger"], b["waveform"])
sent = b"".join(_payload(f)[16:] for f in t.sent if _payload(f)[2] == 0x71)
assert sent == block + b"\x00\x00"
+21
View File
@@ -169,3 +169,24 @@ def test_s4_unseen_block_size_is_blocked():
with pytest.raises(ChangeRefused):
change(fake, {"geo_trigger_ips": 0.3}, revision=r.plan["revision"])
assert fake.executed == []
# Final review #1: the revision covers the changes previewed, not only the unit.
def test_s4_a_revision_covers_the_changes_previewed():
fake = FakeMicromateClient()
rev = change(fake, {"job_number": 7}, dry_run=True).plan["revision"]
with pytest.raises(SettingsChanged):
change(fake, {"geo_trigger_ips": 0.3}, revision=rev)
assert fake.executed == []
# Ruling (final review, declined-to-judge): the blocks a push only echoes are
# re-read at send and sent as read, so they are not part of the revision.
def test_s4_an_echoed_block_moving_does_not_stale_the_preview():
fake = FakeMicromateClient()
rev = change(fake, {"geo_trigger_ips": 0.3}, dry_run=True).plan["revision"]
from micromate.setup_block import with_crc
prefs = bytearray(fake.prefs)
prefs[31] ^= 0x01
fake.prefs = with_crc(bytes(prefs))
assert change(fake, {"geo_trigger_ips": 0.3}, revision=rev).verified == "ok"
+17
View File
@@ -288,3 +288,20 @@ def test_editable_fields_describe_both_series():
assert s3["note_1"]["label"] == "Project" and s3["note_1"]["maxlength"] == 41
assert s4["note_1"]["label"] == "Location"
assert s4["record_time_s"]["max"] == 90.0 and s3["record_time_s"]["max"] == 26.0
# Final review #2: the limits hold on the block that would result, not only on the
# fields in the request -- a one-field change must not leave the unit over them.
def test_limits_hold_on_the_resulting_block():
hot = encode_setup(S3_BLOCK, 3, {"geo_trigger_ips": 2.0})
_refused(hot, 3, {"geo_range": "sensitive"}) # trigger 2.0 > 1.25
alarm = bytearray(S3_BLOCK)
struct.pack_into(">f", alarm, 1708 + 38, 2.0)
for k in (1, 2):
struct.pack_into(">f", alarm, 1708 + 48 * k + 38, 2.0)
_refused(bytes(alarm), 3, {"geo_range": "sensitive"}) # alarm 2.0 > 1.25
_refused(S3_BLOCK, 3, {"record_mode": "histogram_continuous"}) # it holds 20 s; combo max 13
slow = encode_setup(S3_BLOCK, 3, {"sample_rate": 1024, "record_time_s": 100.0})
_refused(slow, 3, {"sample_rate": 4096}) # 100 s > 26 at 4096
assert check_changes(S3_BLOCK, 3, {"note_1": "x"}) # nothing limit-related touched
assert check_changes(hot, 3, {"geo_range": "normal"}) # 2.0 <= 10