# Advanced host compatibility

> How ClayJS discovers host capabilities through GET /_/meta, extension names, blockeditor-state, and root attributes.

Source: https://clayjs.com/docs/advanced/compatibility/

For server authors implementing a ClayJS host or inspecting runtime discovery metadata; for what each built-in host supports, see [Host compatibility](https://clayjs.com/docs/compatibility/).

## How ClayJS learns what a host supports

ClayJS never infers host capabilities from the hostname, port, or trial requests. Instead, it reads capabilities from the `GET /_/meta` discovery endpoint and from attributes the host stamps on `<html>` when serving the page.

### Capability discovery (`GET /_/meta`)

When a feature needs to know what the host supports, ClayJS sends a `GET` request to `/_/meta` (or `/_/meta/{token}` when `<html savetoken>` is present) with the `Document-URL` header set to `window.location.href`, `cache: "no-store"`, `redirect: "manual"`, and a 6-second timeout. In [visitor draft mode](https://clayjs.com/docs/guide/free-documents/), ClayJS skips discovery and treats the host as a bare core host immediately.

Only a `2xx` JSON response with a numeric `spec` property is accepted as a capability document:

```json
{
  "spec": 1,
  "extensions": ["conditional", "save-history", "sync", "upload", "data-read", "data-write", "wire", "people"],
  "document": {
    "etag": "\"8f02c16e7b4d9a30\"",
    "saveId": "b3f1c8e2-...",
    "upload": { "maxBytes": 26214400 },
    "helpers": [
      { "name": "ai-edit", "state": "ready" },
      { "name": "blockeditor-tool", "state": "ready" }
    ],
    "people": { "me": { "id": "u1", "name": "Ada" } }
  }
}
```

ClayJS also reads these `document` fields when present: `writable` (a boolean: `false` takes edit permission away and `true` grants it unless the document rules editing out; a change fires `clay:capabilitychange`), `versions` (`true` when the host keeps a version history a person can go back to, read by `clay.host.versions`), `maxBytes` (the largest save the host stores, a positive integer, read by `clay.save.limits`), `maxBytesMeasures` (`"file"` when that limit is judged on the stored file rather than the request body), and `awareness` (`{ transport, scope }`, see `blockeditor-awareness-v1` below).

If `/_/meta` returns `404`, a redirect, an HTML error page, or any non-JSON body, ClayJS treats the server as a bare core host (`{ spec: null, extensions: [], document: null }`) that supports only `POST /_/save`. Discovery never throws or rejects.

### Extension names in `extensions`

| Extension | What it enables in ClayJS |
|---|---|
| `conditional` | Sends `If-Match` on saves and handles HTTP `412` conflicts (see [Conditional saves and receipts](https://clayjs.com/docs/conditional-saves/)). |
| `save-history` | Queries `/_/meta` with `Save-ID` and `Save-Request-Hash` (`document.saveOutcome`) after a timed-out save, and enables history for `clay.live`. |
| `data-read` | Allows [`clay.readData()`](https://clayjs.com/docs/plugins/data/) to read the saved file as JSON. |
| `data-write` | Allows [`clay.writeData()`](https://clayjs.com/docs/plugins/data/) to write JSON into the saved file. |
| `upload` | Allows [`clay.upload()`](https://clayjs.com/docs/plugins/upload/) to post files to `/_/upload` (when `document.upload` is also present, pre-checking `document.upload.maxBytes`). |
| `sync`, `sync-worker`, `sync-worker-data` | Selects `/_/sync` as the [live sync](https://clayjs.com/docs/plugins/live-sync/) transport (without a `spec` response, `sync` falls back to legacy `/_/live-sync/save` and `/_/live-sync/stream`). |
| `presence`, `presence-editing` | Enables the presence avatar stack and active-block editing outlines in `sync`. |
| `record-awareness-v1`, `blockeditor-awareness-v1` | Lets a page publish what it is working on through `clay.presence.setTarget`: a record and field, or a position in the block editor. With `document.awareness` set to `{ transport: "broadcast", scope }`, targets travel between this browser's tabs over a BroadcastChannel and the host relays nothing. |
| `live-data`, `live-provenance` | Enables host relay and provenance for `clay.live` and [`blockeditor`](https://clayjs.com/docs/plugins/blockeditor/). |
| `blockeditor` | Enables host-backed merge state for the [`blockeditor`](https://clayjs.com/docs/plugins/blockeditor/) plugin. |
| `blockeditor-recovery-v1` | With `document.blockeditorState.href`, reads the block editor state stored for the exact saved bytes and may retire journaled block editor work a proven checkpoint covers; without either, the records stay. |
| `blockeditor-format-v1` | Lets an editor raise a document's block editor format floor through one save, which is what the four newer formats (underline, text color, highlight and quote) need before they write. A host announces it only once its floor enforcement and tolerant checkpoint renderer are installed; without it a newer format command is refused with `blockeditor-format-unsupported` and writes nothing (see [The block editor format floor](https://clayjs.com/docs/guide/live-sync/#the-page-format-floor)). |
| `people` | Reads `document.people` for [`clay.me` and `clay.people`](https://clayjs.com/docs/plugins/people/). |
| `wire` | Enables [`clay.wire`](https://clayjs.com/docs/plugins/wire/) and reads `document.helpers` (`{ name, state }` with state `ready`, `denied`, or `unavailable`). [`ai-edit`](https://clayjs.com/docs/plugins/ai-edit/) activates when `document.helpers` lists `"ai-edit"` as `ready` or `unavailable`, and `/ai` and **Revise…** in [`blockeditor`](https://clayjs.com/docs/plugins/blockeditor/) activate when `document.helpers` lists `"blockeditor-tool"` as `ready` or `unavailable`. |

### The blockeditor-state resource

`document.blockeditorState.href` names the route that returns the block editor's stored Yjs state for the exact bytes a client accepted. ClayJS asks it only when an accepted save's own capture cannot prove a checkpoint, and it asks conditionally:

- The request is a `GET` with `If-Match` set to the ETag that save carried, `Document-URL` set to `window.location.href`, `cache: "no-store"` and `redirect: "manual"`.
- `428` means no condition was sent, `412` means another version of the document is stored, and `404` means the host keeps no page state for these bytes. Each of them, and every other failure, keeps the journal exactly as it was.
- A `2xx` answer carries `version` at 1, `documentId`, `generation`, `etag`, `stateHash` and `tag`. ClayJS accepts it only when the document identity, the generation and the ETag match the ones it accepted, when the digest of the decoded state matches `stateHash`, and when that state projects to the article the save sent.

```json
{
  "version": 1,
  "documentId": "b3f1c8e2-4d1a-4f8e-9c2b-7a6d5e4f3c21",
  "generation": "9c1f4a7e2b8d4c6f0a3e5d7b9c1f2a4e",
  "etag": "8f02c16e7b4d9a30",
  "stateHash": "5f0c1d8a3b7e94f2c6d05a1e8b3f7c9d2e4a6b8c0d1e3f5a7b9c1d2e3f4a5b6c",
  "tag": "{\"v\":1,\"g\":0,\"y\":{\"seed\":\"...\"}}"
}
```

Hyperclay and Hyperclay Local answer `/_/blockeditor-state` under the document lock, and HTML Clay answers `/_/blockeditor-state/{token}` for the token in `<html savetoken>`. Hyperclay names its document row id and HTML Clay the id behind its history key; Hyperclay Local mints no document id at all and answers `documentId: null`.

### Host-injected attributes on `<html>`

A host can also stamp three attributes onto the root `<html>` element when serving a document. ClayJS reads them once at boot and strips all three before every save:

- **`savetoken`**: Grants permission to edit (`clay.canEdit`) and sends requests to `/_/save/{token}`, `/_/meta/{token}`, and `/_/upload/{token}`. The document still opens in view mode; see [edit mode](https://clayjs.com/docs/edit-mode/). (If a host sends only the legacy pre-1.9.0 `htmlclaytoken` attribute, ClayJS logs a warning and keeps edit mode off.)
- **`documentetag`**: Seeds the initial `ETag` stamp for the version of the file that was served.
- **`sitevisibility`**: Sets `clay.siteVisibility` to `"private"` or `"public"` (or `null` when absent).
