feat(micromate): one opcode table and a send-side gate -- writes refused unless allowed, 0xA2 always

micromate/opcodes.py holds WRITE_SUBS and the cross-series collision 0xA2.
MicromateProtocol._send reads the SUB of the bytes about to go out
(framing.request_sub) and refuses any write SUB not in allowed_writes, and
0xA2 unconditionally.  Docstrings now say the reads-only policy is enforced.

request_sub also refuses any frame not in canonical form
(stuff(unstuff(body)) == body): an interior unescaped 02/03/04, a 10 XX
outside {02,03,04,10}, or a trailing lone DLE.  What the gate checks is then
exactly what the unit parses.

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-22 17:10:23 -04:00
1 parent 151ba1546f
commit a3e4e85dfd
6 files changed
+178 -9

No files matched your search

+20 -8
View File
@@ -4,13 +4,16 @@ protocol.py — one method per Micromate (Series IV) wire command.
Returns raw payload bytes. Interpretation belongs in ``client.py``; this layer
knows frames, offsets and sequencing, and nothing about what a field means.
Scope: **reads only.** Nothing here writes, erases, or changes monitoring
state. That is deliberate and worth keeping — no command has ever been
originated against a unit by this project; every write in
``docs/micromate_protocol_reference.md`` was performed 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 effect of a client
that grew a method.
Scope: **reads only on the wire, enforced.** Every outgoing frame passes
`micromate.opcodes.check_transmit()` in `_send`: a write opcode is refused
unless this connection was constructed with it in `allowed_writes` (nothing in
the codebase does so yet), and `0xA2` -- CONFIRM ERASE on Series III -- is
refused always. No command has ever been originated against a unit by this
project; every write in ``docs/micromate_protocol_reference.md`` was performed
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
effect of a client that grew a method. Write frames are *built* in
``micromate.writes`` as reviewable plans; building one sends nothing.
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
@@ -43,7 +46,8 @@ from typing import Optional
from minimateplus.transport import BaseTransport
from .framing import MicromateFrame, MicromateFrameParser, build_request
from .framing import MicromateFrame, MicromateFrameParser, build_request, request_sub
from .opcodes import check_transmit
log = logging.getLogger(__name__)
@@ -243,9 +247,13 @@ class MicromateProtocol:
transport: BaseTransport,
recv_timeout: float = DEFAULT_RECV_TIMEOUT,
strict_checksums: bool = True,
allowed_writes: frozenset = frozenset(),
) -> None:
"""
Args:
allowed_writes: write SUBs this connection may transmit. Empty by
default -- every write opcode in `micromate.opcodes` is refused
at `_send`, and `0xA2` is refused whatever this says.
strict_checksums: raise on a bad checksum. **Defaults to True,
unlike the Series III sibling**, which logs and continues
because its parser cannot reliably tell an inner-frame
@@ -262,6 +270,7 @@ class MicromateProtocol:
self._transport = transport
self._recv_timeout = recv_timeout
self._strict = strict_checksums
self._allowed_writes = frozenset(allowed_writes)
self._parser = MicromateFrameParser()
self._pending: list[MicromateFrame] = []
@@ -509,6 +518,9 @@ class MicromateProtocol:
reset_parser=False)
def _send(self, frame: bytes) -> None:
# The gate sees the bytes that are about to go out, not the caller's
# intent: every transmit in this package passes through here.
check_transmit(request_sub(frame), self._allowed_writes)
log.debug("TX %d bytes: %s", len(frame), frame.hex())
self._transport.write(frame)