Doc Envelope Protocol
The X07 ecosystem commonly represents fallible outcomes as bytes that wrap either:
- an ok payload (bytes)
- an error payload (bytes)
This document defines the canonical doc envelope format so stdlib/ext packages can interoperate.
Format
A doc envelope is a bytes value with the following layout:
tag(1 byte)1= ok0= err
payload(0 or more bytes)- ok payload: interpretation is defined by the producing API
- err payload: interpretation is defined by the producing API, but see the recommended error payload below
Doc envelopes are not self-describing beyond the tag byte. The caller must know which API produced the doc and how to interpret its payload.
Recommended Error Payload (v1)
For error docs (tag=0), stdlib uses this stable payload encoding:
code_u32le(4 bytes, little-endian)msg_len_u32le(4 bytes, little-endian)msg_bytes(msg_len_u32lebytes, UTF-8 text or empty)
This encoding is used by helpers in std.doc:
std.doc.err_from_code_v1(code) -> bytesstd.doc.err_v1(msg) -> bytes(usescode=0)std.doc.err_from_code_msg_v1(code, msg) -> bytesstd.doc.error_code_v1(doc) -> i32std.doc.error_msg_v1(doc) -> bytes
Helpers
Use std.doc for construction and inspection:
std.doc.ok_v1(payload: bytes_view) -> bytesstd.doc.is_ok_v1(doc: bytes_view) -> i32std.doc.payload_v1(doc: bytes_view) -> bytesstd.doc.err_payload_v1(doc: bytes_view) -> bytes
Propagation Sugar: try_doc
The language provides ["try_doc", expr] for doc envelopes:
exprmust evaluate tobytesorbytes_viewrepresenting a doc envelope- if ok (
tag=1): yields the payload asbytes - if err (
tag=0or invalid): returns the original doc from the enclosingdefn
The enclosing defn return type must be bytes (the doc envelope shape).