Skip to content

Structured Message Content

Structured Message Content (SMC) is the one event format of the Sobamail platform. Every event an app handles arrives as an SMC document: a request from the app's own user interface, a mutation replayed from another replica, an email from the outside world. It is also what rides inside a Structured Message, the email form that carries structured data between mailboxes.

SMC is a very simple format that works on top of JSON-like serialization formats with loose schemas. It slightly extends the self-description features of such encodings with a functionality similar to XML Namespaces. Every document says, in a globally unique way, what it is, so that the receiving side can decide who handles it before it looks at the content.

SMC fields

Consider the following JSON document:

{value: 1}

It is simple and compact, but it does not say what it is. The above is not a valid SMC document. With the missing pieces added:

{
    namespace: "https://github.com/sobamail/counter/model/v1",
    name: "IncrementRequest",
    content: {value: 1}
}

An SMC document is a map with three keys:

Key Meaning
namespace A URI that identifies the vocabulary the object belongs to. Must be a valid XML namespace.
name The object's name within that namespace. Must be a valid JavaScript class name.
content The payload. Its shape belongs to whoever owns the namespace. SMC says nothing about it.

That is the whole format. A response that reports a failure also carries an error key (see Errors). The documents the runtime exchanges between replicas add fields of their own (see Replication SMC). Neither concerns an app that sends or receives objects.

The object key

The object key is the namespace and the name concatenated in the form {namespace}name. For the document above that is:

{https://github.com/sobamail/counter/model/v1}IncrementRequest

The braces are part of the string. This convention is sometimes called Clark notation. It is what the platform routes on. When an event arrives, the runtime looks the key up to find the applications that declared an interest in it through the objects map of their Mutator module. An app that never listed a key never sees the event, with one exception. A Replication SMC names the instance it is for under instanceId. A mutation addressed to an instance of the receiving mailbox reaches that instance whether or not its app listed the key.

Because a namespace is a URI, there is no registry. Whoever controls a URL controls the vocabulary under it. The convention is to append /model/v1 to wherever the project lives, and to bump the version segment when the shape of the content changes in a way old handlers cannot read. Two objects with the same name in different namespaces are unrelated objects.

The platform's own objects live under https://sobamail.com/module/base/v1 (messages, rows, blobs, errors) and https://sobamail.com/module/appmanager/v1 (deployment and instantiation).

Content

SMC fixes its own fields and leaves the content to the owner of the namespace. In practice, content takes one of two shapes:

  • A map, as in the counter example above. This is the natural shape for objects defined in JavaScript, where a class instance is serialized with its property names as keys.
  • A positional list, used by the runtime's own objects. For example an AppInfoGet carries content: [appId] and the AppInfo that answers it carries [appId, appName, verName, icon, desc, root, assets, imports]. Positional lists are smaller on the wire and are what the C++ side of the platform prefers.

Values are restricted to what the serialization can carry: null, booleans, numbers, strings, binary buffers, arrays and maps of those. Nested SMC documents are fine. A Structured Message, for one, is an SMC document whose content holds the files of the email, and each file may hold another SMC document.

The runtime writes the booleans true and false as the integers 1 and 0. Every document the runtime emits (a response, a mutation) carries a number where the app wrote a boolean, and a reader of that document finds a number. An app that wants a boolean back compares the number against 1.

Encodings

SMC is defined over three encodings: JSON, MessagePack and XML. All carry the same document. The runtime reads JSON and MessagePack, so a Structured Message carries its document in either of the two. Reading XML is on the roadmap.

JSON

The string-keyed form shown above, with Content-Type: application/json. Binary values have no native JSON representation, so objects that carry bytes (blobs, digests) normally travel as MessagePack.

MessagePack

For MessagePack, the same string-keyed serialization works. But MessagePack is a binary format, so the document can shrink further when predefined integer keys stand in for the string keys:

{
    0: "https://github.com/sobamail/counter/model/v1",
    1: "IncrementRequest",
    3: {value: 1}
}

The above is also a valid SMC in the msgpack encoding, with Content-Type: application/msgpack. The keys substitute as follows:

Integer Key String Key
0 "namespace"
1 "name"
2 "error"
3 "content"

Senders may use either key style. The Sobamail runtime always emits the integer-keyed form, so a client reading a response finds the content under key 3. The integer keys apply to the SMC fields only. Whatever is inside content keeps the keys its owner gave it.

The runtime's MessagePack encoder is canonical: a value has exactly one byte representation. Booleans are part of that: true and false encode as the MessagePack integers 1 and 0, never as MessagePack's own boolean type. Key order is part of a JavaScript object's value, because objects keep their assignment order and it survives a round trip. So {a: 1, b: 2} and {b: 2, a: 1} are two values with two encodings. This matters because a mutation is an SMC document hashed into the mutation graph, and those two would hash differently. See soba.pack.msgpack.write().

XML

The object key was designed to be an XML qualified name, and in the XML form it is exactly that, with Content-Type: application/xml. The document's root element is the object: its XML namespace is the SMC namespace, its local name is the SMC name, and its children are the content. The counter example reads:

<IncrementRequest xmlns="https://github.com/sobamail/counter/model/v1">
    <value>1</value>
</IncrementRequest>

A receiver forms the object key from the root element's namespace URI and local name, so a document whose root carries no namespace is not a valid SMC. Like JSON, XML has no native binary type, so objects that carry bytes go as MessagePack.

TODO

A Structured Message whose document is XML reaches no app: the runtime reads the JSON and MessagePack forms. Reading the XML form is on the roadmap.

Errors

A failure report is an SMC document too. The error field carries a code, and the content is a StructuredError object from the base namespace. This is, for instance, what the app runtime answers when it refuses a request:

{
    0: "https://sobamail.com/module/base/v1",
    1: "StructuredError",
    2: "Client.Validation",
    3: ["https://sobamail.com/module/base/v1", "StructuredError", 3, ["App 'x' carries 33554559 bytes of blobs, over the 33554432 byte deployment limit"]]
}

The content is a positional list: the namespace and name of the error, a numeric code, and a list of arguments. The arguments hold the text meant for a human.

The runtime does not read the value process() returns. A request succeeds or fails by the result the runtime records once the call is over. A process() that throws fails its request, and the web interface answers that 500 Internal error with a StructuredError of the code Server.InternalError.

Where SMC travels

The same document shape appears at three places in the platform. Only the first is a public contract: a Structured Message is email, and anything that can send and receive email can take part. The other two are inside the platform and may change between versions.

Inside an email: the Structured Message

A Structured Message is an ordinary MIME email that carries an SMC document as one of its attachments and points at it with a header:

Message-Structure: <content-id of the attachment>

The platform accepts the older spelling X-Message-Structure as well. The attachment is of type application/msgpack or application/json and holds the SMC document. application/xml is the type of the XML form, which is on the roadmap. Everything else about the message is regular email. It has a sender, recipients, a subject and a date, and it passes through any SMTP server and lands in any IMAP mailbox. A client that knows nothing about Sobamail shows it as a message with an attachment. A client that does know reads the header, opens the attachment and routes the object by its key.

This is how data crosses mailbox boundaries. When an app instance is shared between users, each mutation travels to the other mailboxes as a Structured Message. The platform recognizes such a message by the object key {https://sobamail.com/module/base/v1}SMessage, as opposed to {https://sobamail.com/module/base/v1}Message for free-form mail. See Events.

Note

Clients that access the mailbox over IMAP or POP3 still see processed Structured Messages. They are harmless. See Troubleshooting before you delete one.

Between an app's UI and its backend

An app's web interface talks to its Mutator with a POST of an SMC document to /api/v2/object, as JSON or MessagePack with the matching Content-Type. There are no methods, paths or headers to design: the object key says what the request is. The document may also be a whole Structured Message. The Mutator then receives it as it would receive the same message arriving as email: a Message whose body object is the root document (runtime.md), keyed by the root's object key. The reply is an SMC document as well, emitted with integer keys as MessagePack. It carries Content-Type: application/msgpack whatever its status: the app's response, the app's own refusal and a StructuredError the platform builds alike. The platform refuses a request it cannot read (a body that is no SMC document, or a Structured Message with a malformed field) with status 400 Bad request before any app sees it. Its log names the field that failed. A page thus reads a reply like this:

const reply = decode(await response.arrayBuffer());
const content = reply[3];

The collaborative counter tutorial walks through a request and its reply end to end.

In the mutation log

A mutation is a Replication SMC document, described next: serialized as MessagePack, hashed, and placed in the instance's mutation graph. The hash is the mutation's digest (key 9): the SHA-512 of its parents' digests (key 11's bytes) followed by the object's document bytes, sha512 = SHA-512( psha512 || data). Replicas exchange these documents to converge. The log holds each mutation's object alone. That is keys 0, 1 and 3 for an app's object, or keys 6 and 3 for a well-known object. Key 4 sits beside them when the object carries user-defined fields, which a DeleteRow never does. The log holds no other key. Within a mailbox the mutations travel over the platform's own sync protocol. Across mailboxes they travel as Structured Messages.

Replication SMC

When an app's backend records a change (through the soba.data functions), the runtime turns it into a document for the other replicas to replay. That document is a Replication SMC: the plain SMC of the object being recorded, extended with what the mutation graph needs to place it. Unlike plain SMC it is exclusively MessagePack, because it carries binary data (the digests and the replica uuid) that JSON and XML cannot hold natively. It is always integer-keyed, and it rides inside a Structured Message like any other SMC. Its keys:

Key Name Meaning
0 namespace The object's namespace, for an app-defined object.
1 name The object's name, for an app-defined object.
6 wellKnownObject In place of keys 0 and 1, for the two runtime-defined objects (see below).
3 content The object itself, as the app's table definition describes it.
4 contentAux Optional, user-defined fields beside the object, for an app-defined object or an UpdateRow (the extra fields of soba.data.update()). Never for a DeleteRow.
64 format The format of the bytes under key 65, one of the values below.
65 contentBytes In place of keys 0, 1, 3, 4 and 6: the object's whole document as opaque bytes, exactly as its producer wrote them, a binary value.
16 owner The address of the account that owns the app instance.
13 mailbox The uuid of the mailbox replica that produced the mutation, as 16 raw bytes.
7 instanceId The app instance the mutation belongs to. A mailbox that holds an instance of this id hands the mutation to it whether or not the instance's app listed the object's key.
15 database The instance's database the mutation applies to.
14 digestMutator The digest of the Mutator module version that produced it, so that every replica replays it with the same frozen code.
12 mutationId The mutation's sequence number in the producing replica.
10 digestAlgo The digest algorithm, SHA-512.
9 digest The mutation's own digest: its identity in the graph.
11 digestParent The digests of the mutations this one depends on: its parents in the graph, as one byte string of concatenated 64-byte SHA-512 digests, zero or more of them, at most 1024.

The document itself is always a map. A mutation leaves the mutation log as key 65: the log's own document of the object, byte for byte. Inside the bytes are its keys 0, 1 and 3, or 6 and 3, and 4 when it has one. A well-known object travels the same way. Everything else the mutation carries, the keys from 7 up, sits at the top level beside it. A document carrying key 65 therefore carries none of the keys 0, 1, 3, 4 and 6 at the top level, and states the format of the bytes under key 64. A reader rejects a document that:

  • carries key 65 beside any of those keys,
  • lacks key 64,
  • carries a value of key 64 that the table below marks unknown or does not list, or
  • holds bytes that are not of their stated format.

The bytes name the object as any document does, so a well-known object inside them is named by key 6 alone.

Format value The bytes under key 65
0 Unknown
1 A MessagePack map
2 A MessagePack array
3 A JSON object
4 A JSON array

The mutation log writes format 1. Formats 2 to 4 are reserved for forward compatibility, and readers reject them today.

Because the bytes under key 65 are the log's document unchanged, a receiver checks a mutation's digest without rebuilding anything. Key 9 is the SHA-512 of key 11's bytes followed by key 65's bytes. When the mutation has no parents, it is the SHA-512 of key 65's bytes alone.

A boolean the app wrote into the object is in content as the integer 1 or 0, as in every document the runtime emits. The digest under key 9 is computed over that integer form. Every replica replays the mutation with the integer, and a reader of the mutation log or of a Structured Message finds the integer.

The digests are what make the mutation log a Merkle DAG. A mutation's digest identifies it, and the mutation names the mutations it depends on by their digests. So every edge of the graph is a hash of the node it points to. The parents travel as a single binary value whose length is a multiple of 64: an empty value means no parents, and the value holds at most 1024 digests. A mutation can have several parents: one for each happens-before relationship the backend's calls established. Examples are the row handle passed to delete() or update(), the row a foreign key or a last-write-wins constraint resolved to, and a handle from find(). A mutation with no parents is a root of the graph.

Well-known objects

The runtime, not any app, defines two of the objects a Replication SMC carries. They are so frequent that the document names them with a small integer under key 6 instead of a namespace and a name. A document carrying key 6 must not carry keys 0 or 1, and the deserializer rejects one that does. In an integer-keyed document, keys 0 and 1 never name a well-known object. The deserializer rejects such a document whose namespace and name are those of a well-known object, so each well-known object has one serialized form and one digest.

WKO value Object Key
1 {https://sobamail.com/module/base/v1}DeleteRow
2 {https://sobamail.com/module/base/v1}UpdateRow

Both only make sense inside the graph. A DeleteRow is a tombstone whose parents include the insertion it removes. There is one per soba.data.delete() call. A row that a foreign key cascade removes along with the deleted row has no DeleteRow of its own, and the deleted row's tombstone stands for the whole cascade. An UpdateRow's parents include the row it changes, and its content is a commutative operation on one of that row's columns. Neither is an event an app sends or declares an interest in. The runtime emits them from soba.data.delete() and soba.data.update() and replays them on every replica.

Apps never build a Replication SMC by hand and never see one as an event. A replica that replays the graph hands the Mutator the object under content, as it would any other event. This page documents the layout so that a reader of the raw mutation log, or of a Structured Message in a mailbox, can make sense of it.

A complete example

The counter app's IncrementRequest as its UI sends it, as JSON:

{
    "namespace": "https://github.com/sobamail/counter/model/v1",
    "name": "IncrementRequest",
    "content": {"value": 1}
}

The same document as the runtime emits it, as MessagePack shown in JSON notation:

{
    0: "https://github.com/sobamail/counter/model/v1",
    1: "IncrementRequest",
    3: {"value": 1}
}

Its object key, and the entry the counter's Mutator declares in its objects map to receive it:

{https://github.com/sobamail/counter/model/v1}IncrementRequest