Skip to content

Host contract

The contract splits responsibilities cleanly:

The host owns Mica owns
Routing and which page key to show Page order, and validating goToPage targets
The state store and anything seeded into it Projecting declared objects into it
Transport, credentials, tenancy, authorization Checking inputs against the declaration before a call
Its records and business rules Keeping only declared fields of each response
Rendering preflight problems and startup failures Safe error envelopes; no stack or raw response reaches a document
Its own chrome and styling Styling inside PackSurface via mica/runtime.css
type HostObjectBinding = Record<string, HostObjectImplementation>; // keyed by alias
interface HostObjectImplementation {
type: string; // must equal the declaration's type
fetch?: (inputs: Record<string, unknown>) => Promise<unknown> | unknown;
create?: (inputs: Record<string, unknown>) => Promise<unknown> | unknown;
update?: (identity: string | number, inputs: Record<string, unknown>) => Promise<unknown> | unknown;
delete?: (identity: string | number, inputs: Record<string, unknown>) => Promise<unknown> | unknown;
availability?: Partial<Record<"fetch" | "create" | "update" | "delete", { allowed?: boolean; reason?: string }>>;
fieldWritability?: () => Record<string, { writable: boolean; reason?: string }> | undefined;
}
  • An operation is implemented exactly when its function is present, and allowed unless availability says otherwise. Both land at /meta/objects/{alias}/capabilities/{op}.
  • fieldWritability is keyed by declared leaf field path (contact.email). Fields it omits, or answers without a boolean writable, follow the update capability. Unknown keys are ignored. It is read when the store is seeded or re-seeded and after every successful fetch; if it throws, the answer counts as absent.
  • fetch, create and update resolve with a record — a collection’s fetch with a list of records. update’s record must carry the same identity it was asked to change. delete’s result is ignored.
  • For a collection, update and delete receive the identity of the row the binding names with row, and create resolves with one complete row. See collection row writes.

Availability and writability are information for the person using the app, not a security boundary. A host must authorize every call it receives — the reference host’s delete re-checks permission even though its document hides the button.

A host signals a failure by throwing hostObjectError(envelope):

Field Rules
category validation, permission, not-found, conflict, unavailable, cancelled or unknown
code Stable machine-readable code, 1–128 characters
message User-safe text, 1–1000 characters
retryable true or false
fieldErrors Optional; messages keyed by the operation’s declared input paths (others are dropped), each up to 1000 characters

Anything else thrown, or an envelope that breaks these rules, becomes { category: "unknown", code: "host-error", message: "The host could not complete this request.", retryable: false }.

Mica produces three envelopes of its own:

code When
input-invalid An input failed the declaration’s rules; fieldErrors names each one. The host was not called.
no-current-record A record’s update or delete ran with no current record identity. The host was not called.
no-row A collection’s update or delete ran with a row that resolved to no usable identity (missing, empty, or not the identity’s kind). The host was not called.
host-contract The response did not match the declared fields (for fetch, fieldErrors names the record fields), or an update answered for a different record or row.
  1. The alias’s slot must be free; otherwise the action rejects as busy and nothing changes. The one exception is latest wins: a fetch that an element event dispatched — a Button’s press or a form control’s commit — while another event-dispatched fetch of the same alias is in flight supersedes it. The new fetch runs; the earlier one rejects as superseded when it settles, its answer dropped, so the rest of its event stops. Latest wins applies only between fetches a person’s element events dispatched: startup and host-called fetches are neither superseded nor superseding, so an event fetch during one of them, or during a write, and every write are still rejected as busy.
  2. Inputs are resolved once and checked: a startup entry’s $state reads by Mica against the live store, and an element event’s — a row’s $item reads too — by the element-event runner before the handler runs, so the handler takes them as they arrive and never reads them again. A failure writes phase error with input-invalid.
  3. update/delete take the identity from the current record, or for a collection from the binding’s resolved row. With none, they fail before the host is asked (no-current-record or no-row).
  4. delete, and a create or update carrying a confirmation, asks the confirm function; a decline rejects as cancelled.
  5. Phase becomes loading, and the host function is called.
  6. On success, data and phase success are written in one store update (a record’s fetch also re-reads field writability; a collection’s writes also write the row count). On failure, phase error and the envelope.
  7. If the session was re-seeded or disposed meanwhile, the result is dropped and a write rejects as stale (a fetch dropped this way resolves). If a newer event fetch superseded this fetch, the result is dropped and it rejects as superseded.

A failed write rejects as failed; a failed fetch resolves. The five rejection reasons are what objectRejectionReason(error) returns for an action observer’s settle event.

A collection (cardinality: "many") may implement create, update and delete as well as fetch, with the same signatures. On success Mica merges the answer into the current list by identity, comparing identities as text, and writes the list, /meta/objects/{alias}/count and the phase in one store update:

Function Called with Resolve with On success
create(inputs) the declared create inputs the complete new row, with its identity Replaces a listed row with that identity; otherwise appended only when the collection’s fetch declares no inputs (the host returns the list whole). A queried or paged list is left for its next fetch.
update(identity, inputs) the identity the binding’s row names, and the declared update inputs the complete saved row, with that identity Replaces the row with that identity in place; if it is no longer listed, the list is unchanged.
delete(identity, inputs) the identity the binding’s row names, and the declared delete inputs anything (ignored) Removes the row with that identity; if it is already gone, the list is unchanged.

A create or update answer that is not a row of the declared shape, or an update answer for another identity, is a host-contract error and no row changes. The merge needs a loaded list: until one of the collection’s fetches has succeeded, or the host seeded its data (through seedRuntimeObjectState or reseed), a write still calls the host and settles as usual, but the list and count stay as they are, so an unloaded list never becomes a one-row list. A later failed fetch keeps the list loaded, since its last good rows still show; reseed without that alias’s data and dispose make it unloaded again. Mica never refetches after a write; a document that wants the host’s fresh list composes [create, fetch] itself. One operation runs per alias at a time, rows included, so a second row’s write while the first is in flight is rejected as busy.

preflightObjectBinding(document, binding) returns { ok, issues }. It checks only what can be proven before any call — every operation the document dispatches is implemented, and every bound type key matches. preflightHostActions(document, handlers) is the same idea for declared host actions: every name the document actually dispatches must be a function on handlers. Unused declarations need none.

code Meaning
missing-binding The document dispatches an operation on an alias with no implementation.
type-mismatch The implementation’s type differs from the declaration.
not-implemented The document dispatches an operation the implementation lacks.

Field compatibility is checked on every response instead, because a host may change its payload at any time. Do not run startup when preflight fails.