# ADR 0051: `@derive(MsgPack)` — deriving a MessagePack decoder
**Status:** Proposed (2026-10-18); needs engineering review (#44). It
extends [ADR 0019](0019-user-defined-derives.md) (issue #140, part 3).
## Context
- **The codec landed.** `std/msgpack` is the MessagePack format in pure
Louise over `Bytes`: `Value`, `write`/`read` (untrusted-input safe:
length prefixes checked before reads, nesting capped at 512, `str`
UTF-8-checked, `float 32` widened), and the traits `ToMsgPack` and
`FromMsgPack` with impls for `Int`, `Float`, `String`, `Bytes`,
`Bool`, `List`, `Array`, `Option`, `Result` and `Dict` (tuples and
`()` wait for #106). No new externs: [ADR 0035](0035-numbers-bits-and-u64.md)'s
bit operations and float bits (#143) were what the codec needed.
- **Encoding is derivable today.** `ToMsgPack` is
`@derivable(to_msgpack_of_constructor)`: its one method
`fn to_msgpack(x: a) -> Value` fits ADR 0019's consumer shape, and the
derive function writes the wire format below. `@derive(ToMsgPack)`
works now; this ADR ratifies its format.
- **Decoding is not.** `fn from_msgpack(Value) -> Result`
is a *producer*: no `x: a` to fold over. ADR 0019 allows only
single-method consumer derives and names "producers" and "several
derivable methods per trait" as alternatives not taken.
- **Opaque types** ([ADR 0033](0033-opaque-types.md), #139): a derived
decoder builds values through constructors, so on a `pub opaque type`
it would forge values the module's smart constructor exists to refuse.
- Decoding errors should say **where** decoding failed (a path such as
`lines[2].qty`), not only what was expected.
## Decision
### The mechanism: a producer shape for `@derivable`
ADR 0019 gains a second derivable shape. Next to the consumer
`fn m(x: a) -> R`, a derivable trait may have one method
```louise
fn m :: x: R -> F // the result is one type constructor applied to `a`
```
(`Result`, `Option`, `List`; `R` mentions no type variable).
The prelude gains the producer's structural view, dual to `Constructor`:
```louise
pub type Builder
{ Builder
( name: String
, index: Int
, labels: List
, make: fn(List) -> F
)
}
```
`@derive(T)` on a type generates `m(x) = f(builders, x)`, where each
constructor's `make` is compiler-generated: it takes the fields **raw**
(as `r`; for MessagePack, as `Value`s), runs the trait's own method of
each field's type on them (E0410 when a field has no impl, as today), and
builds the value. The derive function `f` is ordinary polymorphic Louise,
`fn(List>, R) -> F`, checked once where it is written —
every property ADR 0019 bought (no templates, no expansion, both
backends) is kept. For `FromMsgPack`, `std/msgpack` defines:
```louise
@derivable from_msgpack_of_builders
pub trait FromMsgPack {
fn from_msgpack :: v: Value -> Result
}
```
and `from_msgpack_of_builders` implements the reading rules below in
Louise, not in the compiler.
### One trait or two: two
`@derive(ToMsgPack, FromMsgPack)` derives both directions; the two
traits already exist. "Several derivable methods per trait" (one
`@derive(MsgPack)` on a two-method trait) is **rejected**: two derives
compose today, and a multi-method `@derivable(to: f, from: g)` would
tangle two shapes into one validation for no new power. If a single name
is wanted later, that is sugar (`@derive(MsgPack)` expanding to both),
not a new mechanism.
### A compiler derive: rejected
Deriving `MsgPack` in Rust, as `Eq`, `Ord`, `Hash` and `Default` are, is
simpler to specify but is what ADR 0019 moved away from: derives are
written in Louise, type-checked once, and reviewed as Louise. Those four
stay the only compiler derives, for the reasons ADR 0019 gave (a binary
consumer's eagerness; hashing on lookup paths). Nothing about MessagePack
is special enough to reopen that.
### The wire format of user types (ratifies `@derive(ToMsgPack)`)
Chosen for schema evolution:
- **A record (labelled fields) is a map keyed by field label:**
`{"id": 7, "lines": [...]}`. Fields can be added and reordered.
- **A positional constructor is a one-entry map from its name to its
fields' array:** `{"Circle": [3]}`. Tagged by *name*, not index, so
constructors can be reordered.
- **A constructor without fields is its name:** `"Dot"`.
- **Reading:** unknown keys are ignored; a missing field of type
`Option` is `None`; any other missing field is an error. A field's
error carries its path (`lines[2].qty`): `DecodeError` gains the path
on `Mismatch` (the codec's v0 errors carry the empty path; std is
pre-1.0, the change is an edit, not a new type).
### `Int` outside 64 bits
MessagePack integers are uint64/int64; Louise's `Int` promotes to a big
integer ([ADR 0014](0014-arrays-and-bigint.md)). v0: `write` **panics**
with a clear message on an `Int` above 2^64 − 1 or below −2^63 (writing
is of local, trusted data; a `Result` there would tax every call site
for a case that is a bug). Reading a uint64 above 2^62 yields a big
integer, as it should. An `Ext`-encoded bignum (a Louise-reserved tag,
two's-complement big-endian payload) is the alternative, deferred until
a use case asks — it wants a small registry of Louise extension tags,
which the timestamp type (−1) will also want once std has a time type.
### Opaque types
`@derive(FromMsgPack)` is **refused on `pub opaque type`s** (an error
naming the rule): the derived decoder would forge values past the smart
constructor. The impl is written by hand, through the validating
function. A `via:` form (`@derive(FromMsgPack(via: parse))`, decoding
through the named function) is a possible later addition; `@derive`
takes only trait names today.
### Where the codec lives
Louise over `Bytes` (both backends for free). If a benchmark shows the
codec hot, a pure kernel in `runtime/louise_rt_prims` — shared by the
runtime and the interpreter, like the hashes — can replace the insides
with no API change; the byte-exact vectors in `tests/run/msgpack.lse`
pin the format either way. Measured when it matters, not before.
### HTTP (part 4 of the issue, decided here, built there)
`http.msgpack(value) -> Response` and a request body decoder, next to
`http.html` and `http.form_body`; the content type is
**`application/msgpack`** (the registered MIME type). The body size
limit (`http.Settings`) applies before decoding, as for any body.
## Consequences
- E0412's validation grows the producer shape; the prelude gains
`Builder`; the derive generator grows the per-constructor `make`
functions (the same order of size as ADR 0019's consumer generator).
- `DecodeError` gains a path; `std/msgpack`'s hand-written impls fill it
in as the derive does.
- Tests (issue #140, part 5, with the derive): derived records, sum,
recursive and generic types in `tests/run` on both backends;
byte-exact vectors against another implementation (the `rmp` crate) in
a Rust test; `tests/ui` for a derive on a fieldless-impl type (E0410)
and on an opaque type.
- `@derive(ToMsgPack)` is already accepted and writing this format;
rejecting this ADR's format means changing `to_msgpack_of_constructor`
and its tests, not any user code (none can exist yet).
## Alternatives considered
- **Several derivable methods per trait** — rejected above (two derives
compose; the validation stays one shape per trait).
- **A compiler derive** — rejected above (ADR 0019's direction).
- **Decoding through the consumer view** (folding a `Constructor` back
into a value): impossible — the view cannot build an `a`; a producer
needs construction capability, which is exactly `Builder.make`.
- **An `Ext` bignum for big integers** — deferred above (a tag registry
first; no use case yet).
- **Deriving through opaque types' validators (`via:`)** — later;
refusal plus a hand-written impl covers v0.