Skip to content

Add a typed codec that rebuilds a message without running code - #462

Draft
johnsabath wants to merge 1 commit into
mainfrom
feat/typed-codec
Draft

johnsabath wants to merge 1 commit into
mainfrom
feat/typed-codec

Conversation

@johnsabath

@johnsabath johnsabath commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Why

A split model's link pickles every message, and unpickling runs whatever code a
frame names. This PR adds a reader to reactor_runtime.ipc that builds data and
runs no code, so a fault in a session runtime cannot run code in the model
server. The next PR lets the link use it.

What Changed

reactor_runtime.ipc.typed encodes an object as typed values: None, bool,
int, float, str, bytes, numpy arrays and scalars of a fixed dtype list,
list, tuple, dict, and dataclasses and enums registered with
payload_type. Arrays go into the writer's SharedSlot, as with pack().

from reactor_runtime.ipc import payload_type

@payload_type
@dataclass(frozen=True)
class Tick:
    tick: int
    latent: np.ndarray

The decoder runs no code from the types it builds: records are filled field
by field without __init__, enums are looked up by member name, dict keys are
plain values (by exact type, to a bounded depth), and registration refuses a
field that is a data descriptor. It checks each length against the frame, each
array's shape against its bytes, each span against the block, the nesting
depth, and that one frame copies no more bytes out of the block than the block
holds. A malformed frame raises TypedFrameError.

Pickle carries some values that this codec refuses at encode with a
TypeError that names the type: torch tensors, object or structured arrays,
sets, subclasses of builtins, and a combined Flag value (send its int). A
bytearray arrives as bytes, and a non-contiguous array arrives as a
C-contiguous copy.

Interface: new module reactor_runtime.ipc.typed; new exports
reactor_runtime.ipc.payload_type and TypedFrameError; new property
SlotReader.size. Nothing calls the codec yet. About 535 lines are source and
496 are tests.

How it was proved

Round trips of nested records, tuples, optional arrays, enums, dicts of lists of
arrays and numpy scalars compare types as well as values. Fifteen hostile
frames, a pickle that writes a file, and a frame that names one span three
times are each refused, and no file appears. Every prefix of two sample frames
and 6,000 randomly changed frames decode or raise one of the documented errors.
With the shape check, tuple decoding or the depth check broken, 3, 2 and 1
tests fail.

reactor_runtime.ipc.typed encodes an object as a tree of typed values:
None, bool, int, float, str, bytes, numpy arrays and scalars of a fixed
dtype list, list, tuple, dict, and the dataclasses and enums registered
with @payload_type. Arrays go into the writer's SharedSlot as they do
with pack(), or inline when there is no slot.

The reader builds only those types. It checks every length against the
frame, every array's shape against its bytes, every span against the
block, and the nesting depth, and it refuses an unregistered type by
name. pickle imports any class a frame names and calls what the frame
tells it to, so a reader that must not trust its writer cannot use it.

Nothing uses the codec yet; the model server's link takes it in the
next change.

Signed-off-by: John Sabath <john@reactor.inc>

johnsabath commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor Author

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant