feat(micromate): execute(plan) -- the only write path, one call's permission, a start/stop ceiling
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
1 parent
144d3779a1
commit
8daf413fe3
3 files changed
+132
-7
No files matched your search
@@ -80,6 +80,12 @@ WRITE_SUBS: Mapping[int, str] = MappingProxyType(_WRITE_SUBS)
|
|||||||
CROSS_SERIES_COLLISIONS: Mapping[int, str] = MappingProxyType(_CROSS_SERIES_COLLISIONS)
|
CROSS_SERIES_COLLISIONS: Mapping[int, str] = MappingProxyType(_CROSS_SERIES_COLLISIONS)
|
||||||
READ_SUBS: Mapping[int, str] = MappingProxyType(_READ_SUBS)
|
READ_SUBS: Mapping[int, str] = MappingProxyType(_READ_SUBS)
|
||||||
|
|
||||||
|
# The write SUBs any code may transmit at all -- through MicromateProtocol.execute,
|
||||||
|
# one call at a time. Raising this is a spec change, reviewed like one: delete
|
||||||
|
# (0xA8/0xAA), raw file writes (0x8D/0x8E -- they could reach the modem
|
||||||
|
# settings) and 0xA2 never belong here without one.
|
||||||
|
ENABLEABLE_WRITES = frozenset({0x96, 0x97})
|
||||||
|
|
||||||
|
|
||||||
class OpcodeRefused(RuntimeError):
|
class OpcodeRefused(RuntimeError):
|
||||||
"""A frame was stopped before it reached the wire."""
|
"""A frame was stopped before it reached the wire."""
|
||||||
|
|||||||
+41
-7
@@ -16,6 +16,9 @@ by THOR while we recorded. The first thing this codebase ever sends to a
|
|||||||
customer's instrument should be a decision someone made on purpose, not a side
|
customer's instrument should be a decision someone made on purpose, not a side
|
||||||
effect of a client that grew a method. Write frames are *built* in
|
effect of a client that grew a method. Write frames are *built* in
|
||||||
``micromate.writes`` as reviewable plans; building one sends nothing.
|
``micromate.writes`` as reviewable plans; building one sends nothing.
|
||||||
|
``execute(plan)`` is the only write path: one call's permission, at most
|
||||||
|
``ENABLEABLE_WRITES``, and only on a connection that has had Micromate-flagged
|
||||||
|
replies.
|
||||||
|
|
||||||
Every offset and params layout below was read off THOR's own frames in
|
Every offset and params layout below was read off THOR's own frames in
|
||||||
``bridges/captures/9-24-26 - micromate2/`` rather than taken from the spec
|
``bridges/captures/9-24-26 - micromate2/`` rather than taken from the spec
|
||||||
@@ -48,8 +51,11 @@ from typing import Optional
|
|||||||
|
|
||||||
from minimateplus.transport import BaseTransport
|
from minimateplus.transport import BaseTransport
|
||||||
|
|
||||||
from .framing import MicromateFrame, MicromateFrameParser, build_request, request_sub
|
from .framing import (
|
||||||
from .opcodes import WRITE_SUBS, check_transmit
|
FLAGS_BLASTWARE, FLAGS_THOR, MicromateFrame, MicromateFrameParser,
|
||||||
|
build_request, request_sub,
|
||||||
|
)
|
||||||
|
from .opcodes import ENABLEABLE_WRITES, WRITE_SUBS, OpcodeRefused, check_transmit
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -281,6 +287,7 @@ class MicromateProtocol:
|
|||||||
+ ", ".join(f"0x{x:02X}" for x in sorted(unknown)))
|
+ ", ".join(f"0x{x:02X}" for x in sorted(unknown)))
|
||||||
self._parser = MicromateFrameParser()
|
self._parser = MicromateFrameParser()
|
||||||
self._pending: list[MicromateFrame] = []
|
self._pending: list[MicromateFrame] = []
|
||||||
|
self.last_flags: Optional[int] = None
|
||||||
|
|
||||||
# ── Identity and state ────────────────────────────────────────────────────
|
# ── Identity and state ────────────────────────────────────────────────────
|
||||||
|
|
||||||
@@ -525,11 +532,37 @@ class MicromateProtocol:
|
|||||||
return self._recv_one(expected_sub=0xFF - sub, timeout=timeout,
|
return self._recv_one(expected_sub=0xFF - sub, timeout=timeout,
|
||||||
reset_parser=False)
|
reset_parser=False)
|
||||||
|
|
||||||
def _send(self, frame: bytes) -> None:
|
def execute(self, plan) -> list:
|
||||||
# The gate sees the bytes that are about to go out, not the caller's
|
"""Transmit a `micromate.writes.WritePlan` -- the only write path.
|
||||||
# intent: every transmit in this package passes through here.
|
|
||||||
frame = bytes(frame) # checked bytes == written bytes
|
Permission is granted for this call alone, one step at a time, and only
|
||||||
check_transmit(request_sub(frame), self._allowed_writes)
|
for SUBs under `ENABLEABLE_WRITES`. Refuses unless the last reply on
|
||||||
|
this connection carried Micromate flags (0xC5 / 0x03). Each step's ack
|
||||||
|
is awaited before the next is sent. Never retries: a timeout raises,
|
||||||
|
and what happened is the caller's to find out by reading the unit back.
|
||||||
|
"""
|
||||||
|
beyond = plan.subs - ENABLEABLE_WRITES
|
||||||
|
if beyond:
|
||||||
|
raise OpcodeRefused(
|
||||||
|
"plan holds SUB(s) above the write ceiling: "
|
||||||
|
+ ", ".join(f"0x{s:02X}" for s in sorted(beyond)))
|
||||||
|
if self.last_flags not in (FLAGS_BLASTWARE, FLAGS_THOR):
|
||||||
|
raise OpcodeRefused(
|
||||||
|
"no Micromate-flagged reply seen on this connection -- "
|
||||||
|
"refusing to write")
|
||||||
|
acks = []
|
||||||
|
for step in plan.steps:
|
||||||
|
self._parser.reset()
|
||||||
|
self._pending.clear()
|
||||||
|
self._send(step.frame, permit=frozenset({step.sub}))
|
||||||
|
acks.append(self._recv_one(expected_sub=step.ack_sub, reset_parser=False))
|
||||||
|
return acks
|
||||||
|
|
||||||
|
def _send(self, frame: bytes, *, permit: frozenset = frozenset()) -> None:
|
||||||
|
# The gate sees the bytes about to go out, not the caller's intent.
|
||||||
|
# `permit` is passed only by execute(), for exactly one step.
|
||||||
|
frame = bytes(frame)
|
||||||
|
check_transmit(request_sub(frame), self._allowed_writes | permit)
|
||||||
log.debug("TX %d bytes: %s", len(frame), frame.hex())
|
log.debug("TX %d bytes: %s", len(frame), frame.hex())
|
||||||
self._transport.write(frame)
|
self._transport.write(frame)
|
||||||
|
|
||||||
@@ -583,4 +616,5 @@ class MicromateProtocol:
|
|||||||
raise UnexpectedResponse(
|
raise UnexpectedResponse(
|
||||||
f"expected SUB 0x{expected_sub:02X}, got 0x{frame.sub:02X}"
|
f"expected SUB 0x{expected_sub:02X}, got 0x{frame.sub:02X}"
|
||||||
)
|
)
|
||||||
|
self.last_flags = frame.flags
|
||||||
return frame
|
return frame
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
"""execute(plan): the only write path -- exact frames, one call's permission, a ceiling."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
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.writes import ( # noqa: E402
|
||||||
|
plan_call_home_enabled, plan_start_monitoring, plan_stop_monitoring,
|
||||||
|
)
|
||||||
|
from test_micromate_protocol import frame, proto # noqa: E402
|
||||||
|
|
||||||
|
ACK_START = frame(0x69, bytes(ACK_DATA_LEN))
|
||||||
|
POLL_RSP = frame(0xA4, bytes(48))
|
||||||
|
|
||||||
|
|
||||||
|
def _ready(responses):
|
||||||
|
"""A protocol that has already seen one Micromate-flagged reply (a POLL)."""
|
||||||
|
p, t = proto([POLL_RSP] + responses)
|
||||||
|
p.poll()
|
||||||
|
t.written.clear()
|
||||||
|
return p, t
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_ceiling_is_start_and_stop_only():
|
||||||
|
assert ENABLEABLE_WRITES == frozenset({0x96, 0x97})
|
||||||
|
|
||||||
|
|
||||||
|
def test_execute_sends_exactly_the_plans_frame_and_returns_the_ack():
|
||||||
|
p, t = _ready([ACK_START])
|
||||||
|
acks = p.execute(plan_start_monitoring())
|
||||||
|
assert t.written == [build_request(0x96, 0xFFFF)]
|
||||||
|
assert [a.sub for a in acks] == [0x69]
|
||||||
|
|
||||||
|
|
||||||
|
def test_permission_ends_with_the_call():
|
||||||
|
p, t = _ready([ACK_START])
|
||||||
|
p.execute(plan_start_monitoring())
|
||||||
|
with pytest.raises(OpcodeRefused):
|
||||||
|
p._send(build_request(0x96, 0xFFFF))
|
||||||
|
assert len(t.written) == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_plan_beyond_the_ceiling_is_refused_and_nothing_is_sent():
|
||||||
|
content = bytes(5) + b"\x05" + bytes(120)
|
||||||
|
p, t = _ready([])
|
||||||
|
with pytest.raises(OpcodeRefused, match="ceiling"):
|
||||||
|
p.execute(plan_call_home_enabled(content, False))
|
||||||
|
assert t.written == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_execute_refuses_before_any_micromate_reply_has_been_seen():
|
||||||
|
p, t = proto([ACK_START])
|
||||||
|
with pytest.raises(OpcodeRefused, match="Micromate"):
|
||||||
|
p.execute(plan_start_monitoring())
|
||||||
|
assert t.written == []
|
||||||
|
|
||||||
|
|
||||||
|
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.poll()
|
||||||
|
t.written.clear()
|
||||||
|
with pytest.raises(OpcodeRefused):
|
||||||
|
p.execute(plan_stop_monitoring())
|
||||||
|
assert t.written == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_wrong_ack_sub_raises_after_one_send():
|
||||||
|
p, t = _ready([frame(0x68, bytes(ACK_DATA_LEN))]) # stop's ack, for a start
|
||||||
|
with pytest.raises(UnexpectedResponse):
|
||||||
|
p.execute(plan_start_monitoring())
|
||||||
|
assert len(t.written) == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_thor_flags_are_accepted_too():
|
||||||
|
p, t = proto([frame(0xA4, bytes(48), flags=FLAGS_THOR), frame(0x68, bytes(ACK_DATA_LEN))])
|
||||||
|
p.poll()
|
||||||
|
p.execute(plan_stop_monitoring())
|
||||||
|
assert t.written[-1] == build_request(0x97, 0xFFFF)
|
||||||
Reference in new issue
Block a user