mcl_core_wire (mcl_core v1.0.0)

View Source

Tolerant field lookup on a decoded mesh payload map (RPC/stream args, or a pubsub event payload), instead of every provider desk re-solving the same two gotchas independently.

Gotcha one -- KEYS: under macula 12 (D26), the frame decoder is strict and never turns a key into an atom. Every text key arrives as {text, Bin} (macula encodes every map key as CBOR text), and a byte-string key is refused at decode as bad_key. The only atom key a handler sees is caller, which with_caller/2 in the macula_station_link module merges in after decode (from macula 12.11.1 it also removes a caller-sent text "caller"). A map handed over in process, however, may carry atom or binary keys, so a lookup must cope with all three. Before macula 12 the decoder also atomised keys the receiving VM happened to know, and three incompatible ways of coping with that were live in the earlier services; this module replaced them.

Gotcha two -- VALUES, found live 2026-09-01 fixing a RAG service: a JSON string sent as an RPC arg is encoded as a CBOR text string (major type 3), which macula_record_cbor's own documented value representation decodes to {text, binary()}, NOT a bare binary() -- a plain binary is reserved for a CBOR BYTE string (major type 2), a different wire type. Every is_binary/1 guard and :: binary() field spec in that service (and any other consumer) assumed the wire delivers plain binaries for a text field; every one of them silently failed to match a real caller's payload instead, indistinguishable from a missing field. This one recurses: a list of strings decodes to a list of {text, _} tuples, and a list of maps (e.g. a caller round-tripping a prior response's hits back in) decodes to maps whose OWN values need the identical unwrap.

field/2,3 accepts either an atom or a binary key literal, whichever the caller naturally reaches for. Either way the atom form is tried first (the confirmed-live shape for both RPC/stream args and pubsub payloads, piece C/D's own live tests), then the binary form, then {text, Bin}. Whatever value is found (or the caller's own Default) is run through unwrap/1 before returning, so both gotchas are resolved in one call regardless of which one a given field happens to hit.

Use field/2,3 and unwrap/1 to READ a field. Never unwrap a payload that is about to be re-encoded for a signature check: {text, B} and B encode as different CBOR types (text and bytes). See mcl_core_ownership_proof.

retryable/1 (piece G) is the response-side counterpart: whether a failed RPC/stream call outcome is worth retrying, per macula's own published BOLT#4 retry policy. The tom-player service's tom_wire_macula.erl was, before this, the one place in the workspace doing this at all -- asking macula_bolt4:is_retryable/1 rather than keeping a second copy of its code table locally, which would rot the moment BOLT#4 grows a code.

Summary

Functions

The identity that made this RPC call, or undefined if the connected macula doesn't thread it yet (pre-10.15.0 -- macula's own CHANGELOG explains why it took until then) or if this payload didn't arrive via an RPC path at all (a pubsub event's caller-equivalent is publisher, delivered via a separate Meta argument, not the payload).

caller/1 as lowercase 64-character hex, or undefined.

Look up Key in Payload, trying its atom, binary and {text, Bin} forms in that order regardless of which one the caller passed in, and unwrapping whatever value is found (see unwrap/1). Returns unwrap(Default) if no form is present, a no-op for the plain Erlang term a caller's own literal Default almost always already is.

Whether a failed RPC/stream call outcome is worth retrying.

Recursively unwrap macula_record_cbor's wire-level value representation into the plain Erlang terms a handler actually wants to pattern-match or guard against. {text, Bin} (CBOR text string, major type 3 -- see that module's own moduledoc for the full value() table) unwraps to Bin — through list elements and map values, so a topics :: [binary()] field or a hits :: [map()] field round-trips correctly, not just a flat top-level field. null (CBOR major 7/22) unwraps to undefined, matching every existing "absent field" convention in this codebase rather than leaking a wire-protocol atom no caller ever chose. A plain binary() (CBOR BYTE string, major type 2) passes through untouched -- it is already the shape a caller wants; unwrapping only ever undoes major type 3's wrapping. Exported as a pure helper: usable directly on an already-extracted nested value field/2,3 never sees (e.g. a hit map recovered from inside a list), not just internally by this module.

Types

caller_hex_id/0

-type caller_hex_id() :: <<_:512>>.

caller_id/0

-type caller_id() :: <<_:256>>.

Functions

caller(Payload)

-spec caller(map()) -> caller_id() | binary() | undefined.

The identity that made this RPC call, or undefined if the connected macula doesn't thread it yet (pre-10.15.0 -- macula's own CHANGELOG explains why it took until then) or if this payload didn't arrive via an RPC path at all (a pubsub event's caller-equivalent is publisher, delivered via a separate Meta argument, not the payload).

THIS IS THE RAW 32-BYTE NODE ID, NOT TEXT. macula_station_link injects the caller as node-id bytes; 32 random bytes are not valid UTF-8, so a service that stores this value and sends it back as CBOR text has its reply refused by macula's reply check, and the caller sees unknown_error instead of an answer (mcl-chess#10, caught in review before it shipped). For anything that is stored, logged, compared with another node's id or sent back on the wire, use caller_hex/1: it is the same identity in the one shape a reply can carry.

Just field(caller, Payload) under a name every desk that wants provenance can reach for instead of re-deciding the field name -- the same reasoning field/2,3's own moduledoc gives for existing as a shared helper rather than N per-desk reimplementations.

caller_hex(Payload)

-spec caller_hex(map()) -> caller_hex_id() | undefined.

caller/1 as lowercase 64-character hex, or undefined.

The form a service STORES and ANSWERS WITH (mcl-om#30): a reply's caller field travels as text, and the raw node id is not text. The case is fixed here rather than left to each caller, because two services that disagree about case for the same node cannot match each other's stored ids -- mcl-rag encodes uppercase today and mcl-graph, mcl-sec-guard and mcl_core_guard encode lowercase, each by hand.

undefined covers the same cases as caller/1, and also a value that is not a 32-byte node id at all (a payload built in process): hex of something that was never a node id is not a caller, and returning one would hand the service a plausible-looking id no node on the mesh answers to. macula has injected the caller as raw bytes since 10.15.0, the first version to inject one at all, so no other shape reaches this module from the mesh.

field(Key, Payload)

-spec field(atom() | binary(), map()) -> term().

Equivalent to field(Key, Payload, undefined).

field(Key, Payload, Default)

-spec field(atom() | binary(), map(), term()) -> term().

Look up Key in Payload, trying its atom, binary and {text, Bin} forms in that order regardless of which one the caller passed in, and unwrapping whatever value is found (see unwrap/1). Returns unwrap(Default) if no form is present, a no-op for the plain Erlang term a caller's own literal Default almost always already is.

retryable(_)

-spec retryable({ok, term()} | {error, term()} | {'EXIT', term()}) -> boolean().

Whether a failed RPC/stream call outcome is worth retrying.

{error, {call_error, Code, _Name}} is macula's own documented outcome shape for a CALL failure (macula_station_link.erl's own doc table), not a caller-specific convention -- covers macula: call/5, call_station/6,7,8, and mcl_core_capabilities: call_capability/5,7 uniformly. A success, or an error macula didn't code-classify at all (a raw catch, a timeout), has nothing for the BOLT#4 table to say, so those are decided directly here rather than delegated. The 11.x call outcomes: {error, {call_error, C, Detail}} with a BINARY wire code (temporary_relay_failure, unauthorized, unknown_error, ...) or the atom unknown_next_peer for a station that could not route; handler_error arrives as {error, Detail}.

unwrap(List)

-spec unwrap(term()) -> term().

Recursively unwrap macula_record_cbor's wire-level value representation into the plain Erlang terms a handler actually wants to pattern-match or guard against. {text, Bin} (CBOR text string, major type 3 -- see that module's own moduledoc for the full value() table) unwraps to Bin — through list elements and map values, so a topics :: [binary()] field or a hits :: [map()] field round-trips correctly, not just a flat top-level field. null (CBOR major 7/22) unwraps to undefined, matching every existing "absent field" convention in this codebase rather than leaking a wire-protocol atom no caller ever chose. A plain binary() (CBOR BYTE string, major type 2) passes through untouched -- it is already the shape a caller wants; unwrapping only ever undoes major type 3's wrapping. Exported as a pure helper: usable directly on an already-extracted nested value field/2,3 never sees (e.g. a hit map recovered from inside a list), not just internally by this module.