fix(micromate): the write ceiling is enforced at the wire; allowed_writes retired; execute takes only real plans

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-23 16:06:24 -04:00
1 parent 8daf413fe3
commit a90b8158b6
5 files changed
+150 -40

No files matched your search

+98 -3
View File
@@ -2,7 +2,10 @@
from __future__ import annotations
import os
import pathlib
import re
import sys
from types import SimpleNamespace
import pytest
@@ -10,9 +13,10 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from micromate.framing import FLAGS_THOR, build_request # noqa: E402
from micromate.opcodes import ENABLEABLE_WRITES, OpcodeRefused # noqa: E402
from micromate.protocol import ACK_DATA_LEN, UnexpectedResponse # noqa: E402
from micromate.protocol import ACK_DATA_LEN, TimeoutError as MmTimeout, UnexpectedResponse # noqa: E402
from micromate.writes import ( # noqa: E402
plan_call_home_enabled, plan_start_monitoring, plan_stop_monitoring,
WritePlan, WriteStep, plan_call_home_enabled, plan_start_monitoring,
plan_stop_monitoring,
)
from test_micromate_protocol import frame, proto # noqa: E402
@@ -63,7 +67,7 @@ def test_execute_refuses_before_any_micromate_reply_has_been_seen():
def test_execute_refuses_after_a_non_micromate_flags_reply():
p, t = proto([frame(0xA4, bytes(48), flags=0x10), ACK_START], strict_checksums=False)
p, t = proto([frame(0xA4, bytes(48), flags=0x10), ACK_START])
p.poll()
t.written.clear()
with pytest.raises(OpcodeRefused):
@@ -83,3 +87,94 @@ def test_thor_flags_are_accepted_too():
p.poll()
p.execute(plan_stop_monitoring())
assert t.written[-1] == build_request(0x97, 0xFFFF)
# -- fix round 1: the ceiling is enforced where frames leave --------------------
def test_a_permit_beyond_the_ceiling_cannot_send():
p, t = _ready([])
with pytest.raises(OpcodeRefused):
p._send(build_request(0x8E, 0), permit=frozenset({0x8E}))
assert t.written == []
def test_the_constructor_no_longer_grants_anything():
with pytest.raises(TypeError):
proto([], allowed_writes={0xA8})
def test_a_duck_typed_plan_is_refused():
p, t = _ready([])
fake = SimpleNamespace(subs=frozenset({0x96}), steps=(WriteStep(0x7E, 0),))
with pytest.raises(TypeError):
p.execute(fake)
assert t.written == []
def test_a_forged_step_in_a_real_plan_is_refused_at_the_step():
p, t = _ready([])
plan = plan_start_monitoring()
object.__setattr__(plan, "steps", (WriteStep(0x7E, 0),)) # subs now reads 0x7E too
with pytest.raises(OpcodeRefused):
p.execute(plan)
assert t.written == []
def test_a_forged_step_hidden_behind_a_good_plan_is_refused():
class Lying(WritePlan):
@property
def subs(self):
return frozenset({0x96})
plan = Lying("x", (WriteStep(0x96, 0xFFFF), WriteStep(0x7E, 0)))
p, t = _ready([ACK_START])
with pytest.raises(OpcodeRefused):
p.execute(plan)
assert t.written == []
# -- a lost ack is never retried, inside the real execute ------------------------
def test_a_lost_ack_raises_after_exactly_one_send():
p, t = _ready([])
with pytest.raises(MmTimeout):
p.execute(plan_start_monitoring())
assert len(t.written) == 1
def test_a_lost_first_ack_stops_a_two_step_plan():
plan = WritePlan("start then stop",
(WriteStep(0x96, 0xFFFF), WriteStep(0x97, 0xFFFF)))
p, t = _ready([])
with pytest.raises(MmTimeout):
p.execute(plan)
assert t.written == [build_request(0x96, 0xFFFF)]
def test_trust_is_the_most_recent_request_not_an_earlier_one():
p, t = _ready([])
with pytest.raises(MmTimeout):
p.poll() # sent, never answered
t.written.clear()
with pytest.raises(OpcodeRefused):
p.execute(plan_start_monitoring())
assert t.written == []
def test_execute_works_right_after_a_valid_read():
p, t = _ready([ACK_START])
p.execute(plan_start_monitoring())
assert len(t.written) == 1
# -- nothing outside protocol.py passes permit= to _send ---------------------------
def test_only_protocol_py_passes_permit():
root = pathlib.Path(__file__).resolve().parent.parent
offenders = []
for d in ("micromate", "sfm", "bridges", "scratch", "scripts"):
for f in (root / d).rglob("*.py"):
if f == root / "micromate" / "protocol.py":
continue
if re.search(r"_send\([^)]*permit\s*=", f.read_text(errors="ignore"), re.S):
offenders.append(str(f))
assert offenders == []