# The manifest protocol

A monitoring product shows a request's spans by the names in your source
when it holds the manifest of the build that sent them. The manifest
protocol is how every product is given it, in one published form, so
that every product decodes `jadpo.build`, `jadpo.identity` and
`jadpo.source` the same way. A vendor still needs nothing but an
endpoint and a header: the product serves the protocol below the
telemetry endpoint, and reads the same credential.

Run `jadpo manifest send` as you deploy
([`jadpo manifest`](/docs/commands-for-services-calls-email-and-stored-data.md#jadpo-manifest)). It sends each
deployable's manifest, reading the endpoint and credential as the
deployed app does. The deployed app itself carries no manifest and
serves nothing about its structure
([Production builds](/docs/production-builds.md)).

| Request | Answers |
|---|---|
| `PUT jadpo/v1/builds/{build}` | `201` with `{ "build": "b_9a1f03c6d2e4" }` when the product didn't hold the build, `200` when it did. The body is the build's manifest |
| `GET jadpo/v1/builds/{build}` | `200` with the build's manifest, as it was first sent |

Each path is added to the endpoint as `/v1/traces` is, so with the
endpoint `https://api.honeycomb.io` a build is sent to
`https://api.honeycomb.io/jadpo/v1/builds/b_9a1f03c6d2e4`. The body is
JSON, and so is every answer. A product keeps the first manifest it is
sent for a build and never replaces it, so sending again is safe and
can't change how an old build's telemetry reads. A failure has the body
`{ "error": { "code": "…", "message": "…" } }`:

- `401 unauthorised`: the credential is missing or wrong;
- `404 build_not_found`: no manifest is held for that build;
- `413 too_large`: a manifest over 16 MiB;
- `422 manifest_invalid`: the body isn't this protocol's manifest, or it
  names another build than its path.

A manifest holds only what decoding needs. It has the build's id,
deployable, target and compiler, and a node for each declaration the
deployable reaches that has a place in your source:

```json
{
  "build": "b_9a1f03c6d2e4",
  "compiler": "X.Y.Z",
  "deployable": "api",
  "nodes": [
    {
      "domain": "planning",
      "file": "domains/planning/actions/complete_todo.jadpo",
      "id": "action complete_todo",
      "kind": "action",
      "lines": [4, 22],
      "name": "complete_todo"
    }
  ],
  "protocol": 1,
  "target": "wasm"
}
```

Each node is the build manifest's node of the same key
([What a node holds](/docs/the-manifest.md#what-a-node-holds))
without its description, digest or edges. An app with roles held across
the whole application also has `roles`, what each may see
([Who may read the telemetry](/docs/manifest-protocol.md#who-may-read-the-telemetry)). The JSON is canonical:
members sorted, nodes in key order, no timestamps. So everything in it
is decided by the build id. `protocol` changes only when a member is
removed or changes meaning, as the build manifest's `format` does, and a
product leaves alone a member it doesn't know.

The attributes decode like this:

- **`jadpo.build`**, on every payload's resource, is the build whose
  manifest is at `builds/{build}`.
- **`jadpo.identity`**, on each action's and query's span, is a node's
  `id`. It stays the same across builds while the declaration keeps its
  name, and a rename keeps it too
  ([Stable identity](/docs/stable-identity.md)).
- **`jadpo.source`** is `file:line`, a line of `file` in that build. The
  node it falls in is the node of that `file` whose `lines` hold it, the
  narrowest when several do.
- **`jadpo.path`**, on a request's root span, is a path through the
  build ([Path IDs](/docs/path-ids.md)), read against the manifest's `routes`
  and its callables' `flow`, below. `jadpo.path_key` needs no decoding:
  requests that went the same way through the same code share it, in
  any build.

A manifest also holds the control flow `jadpo.path` is read against, an
addition, so `protocol` stays 1: `routes`, the build's route table in
order, each with its `id`, `file`, `line` and the action it `runs`, and
on each callable's node its `flow`. Here is `welcome_guest`'s, from the
lobby above ([Path IDs](/docs/path-ids.md)):

```json
{
  "blocks": [
    [
      {
        "arms": [{ "add": 1, "block": 2, "branch": 0, "condition": 1, "line": 14 }],
        "else": { "block": 3, "branch": 1, "line": 14 },
        "if": 14
      },
      { "kind": "function", "line": 18, "names": "function lobby.salutation_for", "site": 9 },
      { "line": 17, "return": 7 }
    ],
    [],
    [{ "kind": "reject", "line": 15, "names": "type lobby.NoGuest", "site": 6 }],
    []
  ],
  "calls": [[9, "function lobby.salutation_for"]],
  "identity": "41b3b106ac39f9cd",
  "sites": 12,
  "ways": 2
}
```

A path is a number in mixed radix: its first digit is its remainder by
the first radix, and the quotient holds the rest, read the same way. In
order, the digits are the route's place in `routes` (its radix is how
many routes there are), whether its action ran (2), then its action's
frame. A frame's digits are how it ended (`sites` + 2: 0 ran to its end
or returned, 1 plus the site it stopped at, `sites` + 1 its entry); the
way through its body (`ways`, unless that is 0), then, for each of its
`cuts`, whether that segment was reached (2) and its way (the cut's
ways); for each of its `turns`, the set of ways its turns took as bits
(2 to the region's ways) when it has at most 63 ways, or else its first
turn's way plus one, or 0 (its ways plus one); for each of its `calls`,
whether it ran (2); and then each call that ran, in that order, as a
frame of its callee, but for a call to a callable already above it,
which has none. `blocks` holds the body's steps in the order they run,
block 0 the body's, each step naming the blocks it holds by number, to
draw the way. A step is a `site` (its `line`, its `kind` and what it
`names`), an `if` (each arm's `condition` block and way, and its
`else`), a `match` (its `subject` block and a way for each arm), a
`right` (an `and` or `or` whose right side holds a choice: its `left`
block, then the `add` working out its right side's `block` adds), a
`loop` (a `for`'s body, or an `inline` function's, with its `region`,
its `block` and what a `return` inside it adds as it `leave`s through
it), a `return` or a `cut`; a way has its `branch`, `line`, `add` and
`block`. At each choice, the way taken is the one whose `add` is the
largest at most what is left of the body's way, and its `add` is taken
off. Counts past 2^53 are written as decimal text.

`jadpo manifest protocol <endpoint>` is the protocol's conformance test.
A monitoring product runs it in its own CI against its own endpoint,
needing only `jadpo` at the release it targets. It sends a sample build
whose id no build has, `b_000000000000`, and checks every answer above:
the first manifest is kept, one naming another build and one that isn't
a manifest are refused, and an unknown build isn't found. With
`OTEL_EXPORTER_OTLP_HEADERS` set, it sends that credential, and a request
without it must be refused.

## Who may read the telemetry

A monitoring product may show an app's telemetry to the app's own
people, its staff, each seeing what their role may. It needs no
permission system of its own for that: the manifest it is given carries
the app's roles and what each may see, read from the app's own
[policy](/docs/policy.md).

Here a library's staff hold a role across the whole application. Its
admins may add books, and its front desk may only read them:

```jadpo
use desk/entities { Patron }
use desk/types { StaffRole }

/// A patron who works at the library.
entity StaffMember {
    id: Uuid
    patron_id: Patron.id {
        on_delete: OnDelete.cascade
    }
    role: StaffRole

    membership: {
        scope: MembershipScope.application
        member: patron_id
        role: role
    }

    policy: {
        create: [StaffRole.admin]
        read: [StaffRole.admin, patron_id]
    }
}
```

```jadpo
use desk/types { StaffRole, Title }

/// A book on the shelves.
entity Book {
    id: Uuid
    title: Title

    policy: {
        create: [StaffRole.admin]
        read: [StaffRole.admin, StaffRole.front_desk]
    }
}
```

With its action `add_book` and query `books`, the manifest's `roles` are:

```json
"roles": [
  {
    "role": "desk.StaffRole.admin",
    "sees": ["action add_book", "query books"]
  },
  {
    "role": "desk.StaffRole.front_desk",
    "sees": ["query books"]
  }
]
```

Each role is its enum, with its domain before it, and its value, in name
order, and `sees` lists the keys of what its holders may see, in key
order:

- **An action or a query it may run on every request.** Each create,
  read, update and delete it reaches, in its own body and through what it
  calls, is granted to the role by name, to `Access.authenticated` or to
  `Access.public`, and its line under `actions:`, when it is listed
  ([Permissions on actions](/docs/action-permissions.md)), lets
  the role in. A grant to a user field, such as `patron_id`, depends on
  the record, so it lets no role in.
- **A route that runs one it sees,** as `route GET /books`: a request's
  span is named after its route, `GET /books`, as a threshold names it.
- **Nothing that reaches a tenant's records,** since telemetry spans
  every tenant ([Tenancy](/docs/tenancy.md)).

A product shows a holder of a role the spans whose `jadpo.identity` it
sees, the metrics naming those (`jadpo.name`, or the route's
`http.route`), and the logs of the requests whose routes it sees; what
names nothing it sees is for the product's own operators. A role held in
a record, a company's editor, sees nothing and isn't listed: telemetry
carries no record or tenant, so it would see every company's requests.
How a reader signs in to the product, and which role they hold there,
is the product's: the manifest carries no one's membership.
