# Overview
What EDGE is, and where ECHO, HALO, MESH, SAGE and PLEX use it.
EDGE is an always-online peer for local-first apps, run by DXOS on Cloudflare Workers.
In a client-server app, the data lives on the server. EDGE is not where the data lives. The devices are.
A **peer** is anything a device syncs a space's data with, and EDGE is the one that never goes offline. It does four
jobs for the devices:
- It keeps a copy of each space that replicates through it.
- It indexes that copy, if the space lets EDGE read it.
- It recovers identities when every device is lost.
- It runs operations and agents while every device is offline, on the spaces it can read.
EDGE can read a space only when the space gives it more than `pull` access. `pull` is the lowest access level, and it
lets a member store and relay the space's encrypted data. See HALO's
[Spaces and membership](/docs/halo/concepts/spaces-membership/).
Each space and each identity gets its own Durable Objects. A **Durable Object** is a small stateful server on
Cloudflare, and these hold that space's or identity's state.
## What goes over each link
In the diagram, each box is a device or one of EDGE's services, and each arrow is a link or a call. Each device's
WebSocket ends at the Router, which reaches the DB, identity and compute services. The DB service fires triggers in
compute, and compute sends prompts to the AI service.
A device talks to EDGE two ways:
- The WebSocket carries signaling, which introduces the device to its peers, and the changes of each of its spaces
that replicates through EDGE.
- HTTP requests carry the rest, such as a query over objects the device has not loaded, or the recovery of an
identity.
The arrow between the two devices is a direct link. Devices that are online together open one through
[MESH](/docs/mesh/overview/).
## Where the rest of DXOS uses EDGE
| Section | Uses EDGE for | Page |
| ------- | ------------------------------------------------------------------------------ | ------------------------------------------------------------ |
| ECHO | An always-online copy of each space that replicates through it, and backups | [Replication and backup](/docs/edge/concepts/replication/) |
| ECHO | Queries over objects this device has not loaded | [Indexing and queries](/docs/edge/concepts/indexing/) |
| HALO | Recovering an identity, accounts, invitations, service access | [Identity services](/docs/edge/concepts/identity-services/) |
| MESH | The WebSocket each device keeps, and signaling between peers | [Connecting](/docs/edge/concepts/connecting/) |
| SAGE | Agent processes, and models through the AI service | [Compute](/docs/edge/concepts/compute/) |
| PLEX | Operations, triggers, sandbox builds and the plugin registry | [Compute](/docs/edge/concepts/compute/) |
## Running it
DXOS runs EDGE. You point your app at it with `runtime.services.edge.url`. In production, the value is
`https://dxos.network/`. The `runtime.client.edgeFeatures` flags then turn on each thing the client uses EDGE for, and
they are off by default.
There is no self-hosted EDGE. See [Configuration](/docs/edge/resources/configuration/).
## How these pages are organized
Your app reaches EDGE through the DXOS client, not through an API you call. These pages explain how EDGE works, so
there is no learning path, API group or guides.
- [Why EDGE?](/docs/edge/why-edge/) argues for servers in a local-first system.
- [Architecture](/docs/edge/concepts/architecture/) lists the services and the objects behind them. The other concept
pages take one job each.
---
# Why EDGE?
Why a local-first system runs servers at all.
A local-first app keeps its data on the user's devices and works offline. It still needs a **peer**, something its
devices sync with, that is always online, because the user's devices are rarely online together.
EDGE is that peer. It holds a copy of each space that replicates through it, not the original. Every device can sync
and recover through that copy. Devices can also query through it when EDGE can read the space.
You can turn this off for a space with `Space.setEdgeReplication`. The space then syncs only between devices online at
the same time.
## The pattern
Edit a space on a laptop, close the lid, then open the space on a phone. The two devices were never online at the same
time, so no direct link could carry the change. The laptop sent it to EDGE before it slept, and the phone gets it from
EDGE when it wakes.
## An app server against an always-online peer
An app server owns the data, resolves conflicts and decides access. EDGE is a peer that holds a copy. The table sets
the two side by side.
| | App server | EDGE |
| ------------------------- | ----------------------------------- | ------------------------------------ |
| Where the data lives | On the server. Devices hold a cache | On the devices. EDGE holds a copy |
| Works offline | Read-only, at best | Fully. Changes sync later |
| Who resolves conflicts | The server | CRDT merging, the same on every peer |
| If the service disappears | The data goes with it | The devices still have everything |
| Who decides access | The server, on every request | The space's members, through HALO |
EDGE can read a space only when the space gives it more than `pull` access. `pull` is the lowest access level, and it
lets a member store and relay the space's encrypted data. See HALO's
[Spaces and membership](/docs/halo/concepts/spaces-membership/).
## Practical problems
### Devices that are rarely online together
A phone and a laptop that never overlap need a peer in between. EDGE keeps every change it receives, so each device
syncs with it whenever it is online. See [Replication and backup](/docs/edge/concepts/replication/).
### Queries over objects this device has not loaded
A device loads only the documents it needs, so a query that reads the device alone can miss objects it has not loaded.
When EDGE can read the space, a query that should match the whole space also asks EDGE, which has indexed all of it.
A space that gives EDGE only `pull` access is relayed unread and not indexed. See
[Indexing and queries](/docs/edge/concepts/indexing/).
### Recovery when every device is lost
If every device of an identity is gone, the user still holds a recovery key, but no device is left to let a new one
in. EDGE keeps the identity's recovery credentials, the signed records that name its recovery keys, and checks a
recovery challenge against them. See [Identity services](/docs/edge/concepts/identity-services/).
### Work that runs while you are offline
A schedule, an incoming email and an agent partway through a task all need a computer that is on. A user's devices
are often off.
EDGE runs operations, the typed actions an app defines, and agents against the space. See
[Compute](/docs/edge/concepts/compute/).
## Next
[Architecture](/docs/edge/concepts/architecture/) shows the services and the objects behind them.
---
# Architecture
EDGE's services, the objects it keeps per space and per identity, and its environments.
EDGE is a set of Cloudflare Workers. Each Worker is a service, and each service keeps its state in Durable Objects, one
per space, identity or other key.
## Model
**Entry points.** EDGE has two. Each device's WebSocket ends at the Router. HTTP requests go to the `edge` Worker,
which authenticates the caller and forwards each request to the service that owns its path.
**Services.** Every service except the Router answers at a path:
| Service | Path | Does |
| --------- | ------------ | -------------------------------------------------------------------------------------------- |
| Router | (WebSocket) | Holds each device's WebSocket, routes messages between an identity's devices, and runs signaling |
| DB | `/db` | Replicates spaces, indexes them, keeps each space's state, and answers identity recovery |
| Identity | `/identity` | Keeps the agent device of each identity that grants EDGE a key |
| Compute | `/compute` | Runs operations, triggers, the scheduler and agent processes, and creates SAGE agents' identities |
| KMS | `/kms` | Keeps secrets for a space, such as OAuth tokens for the services it connects to |
| Hub | `/hub` | Keeps accounts, invitations and email verification |
| AI | `/ai` | Gives agents and Composer's assistant access to models |
| Blob | `/blob` | Uploads and downloads files |
| Registry | `/registry` | Keeps the plugin registry and the bundles it hosts |
**Durable Objects.** A **Durable Object** is a small server with its own storage, created on first use and named by a
key. EDGE keys them like this:
In the diagram, each box in the top row is a kind of key, and each box in the group below is a Durable Object. An
arrow joins a key to the objects EDGE keeps for it. The table lists the same objects with what each keeps.
| Key | Object | Keeps |
| ----------- | ------------------------------- | -------------------------------------------- |
| Identity | `RouterObject` | The identity's devices and their WebSockets |
| Swarm topic | `SwarmObject` | Which devices are on the topic |
| Space | `SubductionAutomergeReplicator` | The space's Automerge documents |
| Space | `FeedReplicator`, `FeedSpace` | The space's feeds |
| Space | `SpaceStateMachine` | Members, credentials and other space state |
| Space | `Indexer` | The space's index for queries |
| Agent | Agent process | The agent's process and memory, in compute |
A swarm topic is the key MESH gives the group of devices that share one space. See MESH's
[Signaling and discovery](/docs/mesh/concepts/signaling/).
## Environments
| Environment | URL | For |
| ----------- | -------------------------------- | ---------------------------- |
| Production | `https://dxos.network/` | Apps, Composer |
| Preview | `https://preview.dxos.network/` | Testing the next release |
| Dev | `https://dev.dxos.network/` | DXOS's own development |
Your app chooses one with `runtime.services.edge.url`. Every service answers under that one URL, at its path. See
[Configuration](/docs/edge/resources/configuration/).
## Behavior
- A space's Durable Objects run in one place at a time. All writes to a space's copy go through one server, so they
need no coordination between servers.
- EDGE authenticates every request with the caller's HALO identity. See [Connecting](/docs/edge/concepts/connecting/).
## Limits
- DXOS operates every environment. There is no self-hosted EDGE, and its source is not public.
- EDGE serves only identities linked to a DXOS account. For now, you create an account through
[Composer](https://composer.space).
---
# Connecting
The one WebSocket each device keeps to EDGE, and how EDGE knows who is on it.
Each device keeps one WebSocket to EDGE. The EDGE services that stream to the device share it, and EDGE knows which
identity and device are on the other end.
## Model
A web app usually sends each request on its own and proves who is calling with a session cookie or a token. A DXOS
device holds one WebSocket open and proves who it is with its keys.
**One WebSocket, many channels.** `@dxos/edge-client` opens the WebSocket when the client starts and multiplexes it. A
**channel** is one service's stream inside the WebSocket. Each service, such as signaling or a space's replication,
gets a channel with its own flow control, so a large sync does not hold up signaling.
**Authentication.** When the WebSocket opens, EDGE sends a challenge. The device signs it with its device key and sends
the signature with the HALO credential chain, the signed records that admit that device to its identity. EDGE checks
the chain, so it knows the identity behind the device without a password or a session cookie. HTTP requests to EDGE's
services carry the same proof.
**The Router.** The **Router** is the EDGE service where each WebSocket ends. It keeps one
[Durable Object](/docs/edge/concepts/architecture/), a small server with its own storage, per identity. That object
holds the WebSockets of the identity's online devices and delivers messages addressed to the identity or to one of its
devices.
**Feature flags.** Use `runtime.client.edgeFeatures` to turn on what the device uses EDGE for:
| Flag | Turns on |
| ---------------------- | -------------------------------------------------------------------------------------------------- |
| `signaling` | EDGE as the signaling server. See MESH's [Signaling and discovery](/docs/mesh/concepts/signaling/) |
| `subductionReplicator` | Replicating Automerge documents with EDGE's copy |
| `feedReplicator` | Replicating feeds with EDGE's copy |
| `agents` | The identity's agent device, whose key EDGE holds |
An agent device is a device of the identity, and it is not a SAGE agent. See
[Identity services](/docs/edge/concepts/identity-services/).
## Behavior
- When the WebSocket drops, the client reconnects with backoff and authenticates again. Channels resume where they
left off, and replication catches up from what each side has.
- While the device is offline, everything local works: reads, writes and queries over loaded objects. Changes go to EDGE
when the device reconnects.
- Swarms, signaling between peers and ICE/TURN belong to MESH. See MESH's
[Signaling and discovery](/docs/mesh/concepts/signaling/). This page covers only the link between a device and EDGE.
## Limits
- Without the WebSocket, a device cannot meet new peers or replicate with EDGE. Without any route to EDGE, it also
cannot recover an identity or query EDGE's index.
- EDGE accepts only identities linked to a DXOS account. Without one, requests return `403`.
---
# Replication and backup
How EDGE keeps an always-online copy of each space.
EDGE keeps a copy of every space that replicates through it. Devices sync with that copy whenever they are online, so
two devices that never meet still converge.
## Model
The data lives on the devices, and EDGE holds one more copy that is always online.
**Two replicators per space.** A **replicator** is a [Durable Object](/docs/edge/concepts/architecture/), a small
server with its own storage, that keeps EDGE's copy of one kind of data in sync with the devices. A space holds two
kinds of data, so it has two:
| Data | Replicator | Protocol |
| --------------------- | ---------------------------------- | -------------------------------------------------------------------------- |
| Automerge documents | `SubductionAutomergeReplicator` | Subduction, which syncs a space's documents by comparing compact summaries of their history |
| Feeds | `FeedReplicator` | Appends each feed's new blocks |
**A copy, not the authority.** EDGE merges changes the same way every device does. A conflict between two devices
resolves the same on EDGE as on each device, so EDGE never has to decide who wins.
The diagram follows one change from a laptop to a phone. Each box holds a copy of the space, and each arrow is a route
the change takes. The solid arrows pass through EDGE's copy. The dashed arrow is the direct link that MESH opens while
both devices are online.
## Turning it off
`Space.setEdgeReplication` turns replication through EDGE on or off for one space. With it off:
- The space syncs only between devices online at the same time.
- EDGE stops receiving the space's changes, so they are not backed up there and EDGE cannot index or run compute on
them.
- A device that recovers the identity gets the space only from another device.
## Archives, backups and deletion
- `Space.exportSpace` returns a space as an archive, and `Space.importSpace` creates a new space from one. Both work
with or without EDGE.
- EDGE's copy is the backup for a space whose devices are all lost. A recovered identity syncs its spaces from it.
- When you delete a space, EDGE's copy is queued for deletion too.
## Behavior
- A change that reaches EDGE reaches every member's device that later connects.
- The same change may arrive both from EDGE and over a direct link. It is applied once.
## Limits
- EDGE's copy trails the devices by however long they take to send it. A change made offline is on EDGE only after the
device reconnects.
- With only `pull` access, the lowest access level, EDGE relays a space without reading it. Indexing and compute need
more. See HALO's [Spaces and membership](/docs/halo/concepts/spaces-membership/).
---
# Indexing and queries
EDGE's index of each space, and how a query uses it.
EDGE keeps an index of every space it replicates and can read. A query on a device reads its local index and EDGE's
together, so your query can match objects the device has not loaded.
A space that grants EDGE only `pull` access, the lowest access level, is relayed unread and has no index there. See
HALO's [Spaces and membership](/docs/halo/concepts/spaces-membership/).
## Model
A device loads only the documents it needs. How does a query match an object the device has never loaded? A query that
should match the whole space also asks EDGE.
**The index.** An **index** is a lookup from a type, a property or a relation to the objects that have it. Each space
has an `Indexer` [Durable Object](/docs/edge/concepts/architecture/) on EDGE. It reads the space's documents as they
reach EDGE and indexes them the way a device indexes its own.
**Merging results.** A query runs against the device's own index first and returns those results. If the device is
connected, the query also asks EDGE, and adds EDGE's matches as they arrive.
ECHO treats EDGE's answer as evidence, not authority:
- If the device holds an object EDGE matched, the device checks the match against its own copy and drops it if the two
disagree. A slow index therefore cannot bring back an object you deleted.
- If the device does not hold the object, the match is shown as EDGE reported it.
See [Queries](/docs/echo/concepts/queries/) for how a query combines its sources.
## Behavior
- Results from the device's own data come back without waiting for EDGE.
- Queries are live. A query never completes. It updates as new matches arrive, from the device or from EDGE.
## Limits
- EDGE's index trails the space by however long changes take to reach it. A change made on another device a moment ago
may not match yet.
- If the device is offline, a query sees only what the device holds.
- If a space has EDGE replication off, it has no EDGE index.
---
# Identity services
What EDGE does for HALO: recovery, accounts, invitations and service access.
A HALO identity's keys are made on its devices, and most of HALO needs no server. EDGE handles the parts that do:
recovering an identity when every device is gone, the DXOS account an identity is linked to, and access to EDGE's own
services.
## Model
A user loses every device. Who can let a new one in? The answer starts with which keys EDGE holds.
**Keys.** By default, no server holds a key that can act for an identity. An identity can grant a server access to
hold one, and EDGE provides this as a service.
When an identity makes the grant, EDGE runs an **agent device** for it. The agent device is one of the identity's
devices, of kind `agent-managed`, and EDGE holds its key. Like any of the identity's devices, it can sign for the
identity. It runs no logic of its own. It keeps its key in its own store and signs a credential when another EDGE
service asks. EDGE asks when a user recovers the identity, and when a member's device needs a credential written into
a space while no other member is online. See HALO's [Identities and devices](/docs/halo/concepts/identities-devices/).
**Recovery.** While they still have a device, a user sets up a **recovery credential**. It is a signed record that
names a recovery key, and the key comes from a recovery code or a passkey. The record is part of the identity's own
data, which replicates to EDGE.
To recover, a new device asks EDGE for a challenge and signs it with the recovery key. EDGE checks the signature
against the identity's recovery credentials. If it matches, the identity's agent device signs a credential that admits
the new device to the identity, and EDGE answers with it.
Recovery needs EDGE because, with every device lost, the user has no device left to admit the new one. EDGE can admit
it only for an identity that has an agent device. See
[Recovery and credentials](/docs/halo/concepts/recovery-credentials/).
**Accounts.** The **Hub** is the EDGE service that keeps DXOS accounts. An identity linked to an account can use EDGE.
The Hub also handles email verification and the invitations that bring people to DXOS.
**Service access.** `Identity.grantServiceAccess` writes a credential that lets the identity use an EDGE or Hub service.
`Identity.createApiToken` mints a token. Use it when a CLI or a server has to act as the identity.
A SAGE agent is a different thing from an agent device. It is a model in a loop that works in a space, and it
can have an identity of its own, which EDGE creates. See SAGE's [Agents](/docs/sage/concepts/agents/).
## Behavior
- The recovered device is a new device of the same identity, and the identity keeps its DID. The device syncs the
identity's spaces from EDGE and from any other members online.
- EDGE checks credentials. It can act for an identity only with a key that the identity granted it.
## Limits
- EDGE stores recovery credentials only for identities linked to an account. For now, you create an account through
[Composer](https://composer.space).
- Recovery needs a connection to EDGE.
---
# Compute
How EDGE runs work over ECHO objects, with operations, processes and triggers, and where agents and plugins run.
Compute runs typed work wherever it fits: in the app, in a browser worker, or on an always-online server. Each piece of
work is an **operation**, a typed action with a key, an input schema and an output schema.
Three kinds of caller start an operation:
- A person runs one from a button or the command palette.
- An agent calls one as a tool.
- A trigger runs one when something happens in the space or outside it.
The same operation reads and writes the space's objects the same way wherever it runs. Each run is a **process**, and
the **process manager** starts it in the app or on EDGE.
In the diagram, the top row holds the three callers, and their arrows meet at the process manager. It starts a local
process in the app or a remote process on EDGE. Either process reads and writes the space's ECHO objects, and writes
what it did to the trace feed.
## Model
Compute has four parts, and each has a page of its own:
- [Operations](/docs/edge/concepts/operations/) covers the unit of work. You define an operation once, by its key and
its schemas, and a handler does the work. Callers find it by key, and the invoker checks the input and output
against the schemas.
- [Processes](/docs/edge/concepts/processes/) covers one run of an operation. A process gets the services its
operation declares, such as the space's database, storage of its own and the space's secrets. It has a state that
the app can watch, and it can hibernate while it waits and resume later, on the same device or on EDGE.
- [Triggers](/docs/edge/concepts/triggers/) covers what starts an operation without anyone asking. A trigger is an
ECHO object. It fires on a schedule, when objects matching a query change, or when a feed gets new items. It also
fires when an email or an HTTP request arrives, or when something invokes it directly.
- [Traces](/docs/edge/concepts/traces/) covers the record of what a process did. Durable trace events are kept in a
feed in the space. Live progress goes to the devices watching.
**Agents.** An **agent** is a process too, a model in a loop that calls operations as tools. By default an agent's
chats run on EDGE, so the agent keeps working while every device is offline. It reaches models through EDGE's AI
service. See SAGE's [Agents](/docs/sage/concepts/agents/) and [Models](/docs/sage/concepts/models/).
**Sandbox builds and the plugin registry.** When you publish a plugin, EDGE can build it in a sandbox with the
`@dxos/*` packages installed. The **plugin registry** is the EDGE service that indexes published plugins and hosts
their bundles, which Composer loads. See [Publishing a plugin](/docs/plex/publishing-plugins/).
## Where compute runs
Compute runs in three places:
| Where | What runs there | While |
| --------------------------- | ---------------------------------------------------------------------------- | ----------------------------- |
| The app | Operations people invoke, local triggers, and chats that are not an agent's | The app is open |
| A browser worker in the app | Code that agents write in code mode, on its own ECHO client | The app is open |
| EDGE | Remote triggers, operations deployed to EDGE, and agents' chats | Always, whether a device is online or not |
The app shows local and EDGE processes in one list. See [Processes](/docs/edge/concepts/processes/).
## Behavior
- Operations and agents act through the same APIs as a device. Their changes replicate to members like anyone else's.
- On EDGE, operations run in Cloudflare Workers that EDGE keeps for them, and each agent's process runs in a
[Durable Object](/docs/edge/concepts/architecture/) of its own.
- A process on EDGE that hibernates is written to storage. It resumes when its alarm fires or its child process
finishes.
## Limits
- Compute on EDGE reads space content, so it runs only for spaces that give EDGE more than `pull` access, the lowest
access level. See HALO's [Spaces and membership](/docs/halo/concepts/spaces-membership/).
- The AI service sends prompts and space content to the model provider a request names.
- An operation that runs on EDGE needs every ECHO type it uses to be known there. Its definition lists them.
---
# Operations
A typed action with an input and output schema, invoked by key.
An operation is the unit of compute in DXOS. It is a typed action that you define once. Whoever needs it runs it: a
person through the UI, an agent as a tool, or a trigger. A trigger is an object that starts an operation on a schedule
or an event.
## Model
An HTTP endpoint has a path, a request body and a response body. An operation has the same three parts: a key, an input
schema and an output schema.
### Definition
A **definition** is the value that `Operation.make` builds from metadata and two schemas:
- The metadata, `meta`, holds the key, a name, a description and an icon. The **key** is a DXN, such as
`com.example.operation.stashedLink.markRead`, and it names the operation everywhere it runs. The description is
what an agent reads when it decides whether to call the operation.
- The input and output schemas are Effect schemas. Their field descriptions tell people and agents what to pass.
- A definition can also list the ECHO types it uses, so the types are known where it runs remotely, and the
`services` its handler needs.
**Example** (Defining an operation)
```ts
import * as Schema from 'effect/Schema';
import * as Operation from '@dxos/compute/Operation';
import { Database, DXN, Ref } from '@dxos/echo';
import { StashedLink } from './StashedLink.ts';
export const MarkRead = Operation.make({
meta: {
key: DXN.make('com.example.operation.stashedLink.markRead'),
name: 'Mark as read',
description: 'Mark a link as read.',
icon: 'ph--check-circle--regular',
},
// The handler resolves the reference in the link's space.
services: [Database.Service],
input: Schema.Struct({
link: Ref.Ref(StashedLink).annotate({
description: 'The link that was read.',
}),
}),
output: Schema.Void,
});
```
`MarkRead` has no handler yet. Its one input field, `link`, is a reference to the link, not the link itself, and its
output is `Schema.Void`, so a caller gets nothing back.
### Handler
A **handler** takes the input and returns an Effect that produces the output. `Operation.withHandler` attaches one to a
definition.
**Example** (Attaching a handler)
```ts
import * as Effect from 'effect/Effect';
import * as Operation from '@dxos/compute/Operation';
import { Database, Obj } from '@dxos/echo';
import { StashedLink } from '../types/StashedLink.ts';
import { MarkRead } from '../types/StashedLinkOperation.ts';
const handler: Operation.WithHandler = MarkRead.pipe(
Operation.withHandler(({ link: ref }) =>
Effect.gen(function* () {
// The assistant passes a reference by id, so look it up in the space.
const link = yield* Database.resolve(ref, StashedLink);
Obj.update(link, (link) => {
link.read = true;
});
}),
),
);
export default handler;
```
`handler` is the `MarkRead` definition with its handler attached. The handler can call `Database.resolve` because the
definition lists `Database.Service`. It then sets `read` to `true` inside `Obj.update`.
The definition and the handler are separate values, so a package can publish the definition for others to call
without shipping how it runs.
`Operation.lazyHandler` names the module that holds the handler. Use it when the code should load only on the first
call.
A **handler set** is a group of handlers that a runtime loads together. A PLEX plugin contributes one through a
capability. See PLEX's [Operations](/docs/plex/concepts/operations/).
### Invoker
The **invoker** runs an operation for a caller. It does four things in order:
1. It finds the handler by the definition's key.
2. It checks the input against the input schema.
3. It runs the handler with the services it declared.
4. It checks the output.
### Registry
The **operation registry** holds each definition as an ECHO object, so agents and the MCP server can search the
operations a space offers. A definition whose metadata sets `skipRegistry` is left out of it.
### Annotations
A definition carries hints for its callers:
| Annotation | Says |
| ---------- | ---- |
| Mutation | Whether the operation changes nothing, writes, or destroys data |
| Idempotent | Whether running it twice with the same input has the same effect as once |
| Visible | Whether people can pick it, as in a trigger's operation picker. Unmarked operations are hidden |
## Behavior
- One definition serves every caller. A button, the command palette, an agent and a trigger invoke the same key with
the same input.
- Errors are typed. A handler fails with tagged errors that the caller catches by tag. If you invoke a key that no
handler set serves, the call fails with `NoHandlerError`.
- A handler can stop early when its run is cancelled. It reads the run's `AbortSignal` from `Cancellation.signal` in
`@dxos/compute`, and hands the signal to work that can stop, such as a `fetch`.
- The runtime that runs the operation fires the signal. In the app, it fires when the
[process](/docs/edge/concepts/processes/) is terminated, and the runtime then interrupts the handler. On EDGE, it
fires when the client asks to cancel the run, and the handler is not interrupted.
- If the runtime provides no cancellation, the signal never fires.
- An operation can invoke other operations. The call is linked to the caller's [process](/docs/edge/concepts/processes/),
so the trace shows which call started it.
- When you deploy an operation to EDGE, EDGE assigns it a deployment id. A client can invoke it there by that id or by
its key.
- Every call writes its start and end to the [trace](/docs/edge/concepts/traces/).
## Limits
- Input and output must be serializable, because a call may cross to a browser worker or to EDGE. Pass ECHO objects
by reference.
- A handler gets only the services its definition declares and the ones every process has. It does not get the UI.
- An operation that sets `skipRegistry` is not in the operation registry, so agents and the MCP server cannot find it.
Use it for an operation that only makes sense in the UI.
---
# Processes
One running invocation, with its own services, lifetime and trace.
A process is one run of an [operation](/docs/edge/concepts/operations/). It holds the services the operation needs, it
has a state that the app can watch, and it keeps a trace of what it did. A process can run in the app, in a browser
worker or on EDGE, and the app sees them all in one list.
## Model
An operating system runs each program as a process, with a parent, a state and a way to stop it. Compute does the same
for each run of an operation.
The **process manager** spawns processes and keeps their tree. A process that starts other work spawns it as a child,
linked to itself.
### Lifetime
How long a process lives depends on its operation:
- A short operation becomes a process that takes one input, runs the handler and exits.
- A durable operation, such as an agent, is a process that takes input many times, is idle between inputs and resumes
where it left off.
### States
A process is in one of these states:
| State | Means |
| ----------- | ------------------------------------------------------------------------- |
| Starting | The spawn was sent to a remote runtime, which has not acknowledged it yet |
| Running | It is doing work |
| Hibernating | It is waiting for a child process or an alarm |
| Idle | It is waiting for input |
| Terminating | It is being stopped |
| Terminated | Something stopped it |
| Succeeded | It finished |
| Failed | It failed |
These pages say a process **hibernates** when it waits on a child or an alarm, and is idle when it waits for input.
### Layer stack
A **layer** is a recipe that builds one or more services, and the **layer stack** is the set of layers a process gets
its services from. Each layer has an **affinity**, which says how widely one instance is shared: across the whole
application, per space, or per process. The services include:
- The space's ECHO database. The process reads and writes objects through it like a device.
- Storage of its own. This is a key-value store scoped to the process, and it survives hibernation.
- The space's **secrets**, which are API keys and tokens for outside services.
- The trace, and `AiService` for operations that call a model.
A service the runtime cannot provide is left out, and a layer that needs a missing service is not built.
### Local and remote
The same operation can run in the app, in a browser worker or on EDGE. The app spawns a process on EDGE by naming the
operation's key, or its deployment id when it was deployed.
The process list merges the local tree with the one EDGE reports. Each control, such as cancel, goes to the runtime
that holds the process.
### Deployed operations
You can keep operation code as an ECHO object in the space and deploy it to EDGE. EDGE builds it and gives it a
deployment id. The deployed operation runs with the space's secrets. EDGE can hold a secret on the space's behalf, so
the secret is not replicated to every device.
### Pipelines
A **pipeline** is a chain of stages over an Effect stream, with back pressure. An operation that works through a long
source, such as a mailbox or a chat history, can stream it with `@dxos/pipeline`.
A pipeline saves a cursor to the process's storage as it goes, so a process that is stopped and run again resumes
where it left off. It runs inside one process and is not a separate kind of compute.
## Behavior
- A process that hibernates can be written to storage and dropped from memory. When its alarm fires or its child
exits, the runtime loads it again and it continues. On EDGE, this lets a
[Durable Object](/docs/edge/concepts/architecture/) stop running between an agent's turns.
- An operation can declare requirements, such as permission to write to a space. A process runs such an operation only
if its **guard** admits it. A guard is a check the process is spawned with, and a child process inherits its
parent's.
- A finished process clears its storage.
## Limits
- An operation's input reaches a remote process as serialized data, so it must not hold live objects or functions.
- Use process storage for a process's own state, such as a cursor. Put data that people or other processes need in
ECHO objects.
---
# Triggers
What starts an operation without a person asking.
A trigger starts an [operation](/docs/edge/concepts/operations/) when something happens: a schedule comes round, objects
change, a feed gets new items, or a message arrives from outside. A trigger is an ECHO object in the space, so it syncs
to every member and you edit it like any other object.
## Model
On a server, you would write a cron job for a schedule, a webhook handler for a request from outside, and a database
hook for a change. A trigger covers each of these with one kind of object.
A trigger names the operation to run, a spec for when to run it, and an input template. It also holds whether it is
enabled, whether it runs on EDGE, and how many runs may be in flight at once. By default, one run is in flight at a
time.
**Kinds.** The spec's kind says what fires the trigger:
| Kind | Fires when | Settings |
| -------------- | ---------------------------------------------------- | ------------------------------------------------------------- |
| `timer` | A cron schedule comes round | The cron expression |
| `subscription` | Objects matching an ECHO query are created or change | The query, whether nested changes count, and a debounce delay |
| `feed` | A feed receives new items | The feed, and how many items each run gets, up to 100 |
| `email` | An email arrives at the space's address | None |
| `webhook` | An HTTP request arrives at the trigger's URL | The HTTP method |
| `direct` | Something invokes it | None |
**Input template.** The trigger builds the operation's input from its template:
- A value can name part of the event that fired the trigger, such as `{{event.item}}` for the new feed item.
- A fixed value, such as a reference to a mailbox, is passed as it is.
**Routines.** In Composer, a **routine** pairs an operation with the triggers that start it. It is an app-level object,
and EDGE sees only the triggers.
## Behavior
- A trigger runs locally unless you mark it remote. A local trigger fires only while an app with the space open is
running. A remote trigger fires on EDGE whether or not any device is online.
- Every run is a [process](/docs/edge/concepts/processes/) and writes a [trace](/docs/edge/concepts/traces/), so the
app can show when a trigger last fired and whether the run succeeded.
- You can also run a `timer` or `direct` trigger by hand from the app. A disabled trigger refuses to run.
- An agent can have triggers of its own. Turning the agent off turns its triggers off.
## Limits
- A `subscription` trigger fires on changes that reach the runtime it runs in. A local one sees only what the device
has synced.
- A remote trigger reads space content on EDGE, so it needs a space that gives EDGE more than `pull` access, the
lowest access level. See HALO's [Spaces and membership](/docs/halo/concepts/spaces-membership/).
- A feed trigger reads each batch in one query, which is why a batch holds at most 100 items.
---
# Traces
What ran, when and why, kept so people and agents can see it.
Every [process](/docs/edge/concepts/processes/), which is one run of an operation, records what it does as trace events.
The durable ones are kept in the space, so a member can see what a trigger or an agent did while they were away. The
live ones show progress on the devices watching.
## Model
A trigger fired overnight. What did it run, and did the run succeed? A trace is the record that answers, and its
durable part is data in the space.
**Events.** A **trace event** has a type, a timestamp and a payload, and it names the process that wrote it. An
operation writes its own events with `Trace.write`. The runtime writes the rest. Some of the types:
| Event | Written when | Kept |
| ------------------------------------- | ---------------------------------------------------- | ------- |
| `operation.start`, `operation.end` | An operation starts, and when it ends or fails | Durable |
| `operation.input`, `operation.output` | With the start and the end, holding the values | Live |
| `status.update` | A process reports what it is doing | Live |
| `task.statusChanged` | A task in an agent's plan changes state | Durable |
| `question.asked`, `question.answered` | An agent asks a person something, and gets an answer | Durable |
| `assistant.*` | An agent's turn begins, streams and ends | Both |
**Durable events** go to the space's trace feed, a [feed](/docs/echo/concepts/feeds/) like any other. They replicate to
members and to EDGE, and the app and agents query them like other ECHO data.
**Live events** go to the devices in the space's swarm that are watching the process, and are not stored. The swarm is
the set of devices that MESH links for the space. A device that starts watching a running process gets the events
buffered so far, then new ones as they come.
## Behavior
- The app's process list and an agent's activity view both read the trace. They read the durable feed for history
and the live events for what is happening now.
- A process on EDGE writes to the same trace feed as one in the app, so the history does not depend on where it ran.
- Operation inputs and outputs are live only, so the values an operation handled are not kept in the space's history.
## Limits
- Live events reach only devices that are online and watching. A device that opens the process later sees only the
durable events.
- Trace events are appended in batches about half a second apart, so the durable history trails a running process
slightly.
---
# Configuration
Every client setting that points at or turns on part of EDGE.
This page lists every client setting that points at EDGE or turns on part of it. The settings go in the client's
`Config`, as in [Installation](/docs/echo/learn/installation/):
```ts
const config = new Config({
runtime: {
client: {
edgeFeatures: {
signaling: true,
subductionReplicator: true,
feedReplicator: true,
},
},
services: {
edge: { url: 'https://dxos.network/' },
iceProviders: [{ urls: 'https://dxos.network/ice' }],
},
},
});
```
| Setting | Type | Default in a bare `Config` | Effect |
| -------------------------------------------------- | ---------------- | -------------------------- | -------------------------------------------------------------------------------- |
| `runtime.services.edge.url` | URL | none | Sets the EDGE environment the client uses. Without it, the client never connects |
| `runtime.services.iceProviders` | `{ urls }[]` | none | Lists URLs that return ICE servers for WebRTC |
| `runtime.services.ice` | `RTCIceServer[]` | none | Gives WebRTC a fixed list of ICE servers |
| `runtime.client.edgeFeatures.signaling` | `boolean` | off | Uses EDGE as the [signaling server](/docs/mesh/concepts/signaling/) |
| `runtime.client.edgeFeatures.subductionReplicator` | `boolean` | off | Replicates Automerge documents with EDGE |
| `runtime.client.edgeFeatures.feedReplicator` | `boolean` | off | Replicates feeds with EDGE |
| `runtime.client.edgeFeatures.agents` | `boolean` | off | Turns on the identity's agent device, whose key EDGE holds |
Composer and the `dx` CLI turn on all four `edgeFeatures` flags.
The agent device is a device of the identity that EDGE holds a key for. It is not a SAGE agent. See
[Identity services](/docs/edge/concepts/identity-services/).
## Environments
| `edge.url` | Environment |
| ------------------------------- | ----------- |
| `https://dxos.network/` | Production |
| `https://preview.dxos.network/` | Preview |
| `https://dev.dxos.network/` | Dev |
---
# Glossary
Every EDGE term, defined once.
MESH's terms, such as swarm, signaling and TURN, are in MESH's [glossary](/docs/mesh/resources/glossary/).
### Agent device
An agent device is a device of an identity, of kind `agent-managed`, whose key EDGE holds. An identity has one only
if it grants EDGE that access. It signs credentials when another EDGE service asks, and it is not a SAGE agent. See
[Identity services](/docs/edge/concepts/identity-services/).
### [Always-online peer](/docs/edge/why-edge/)
An always-online peer is a server that devices sync a space with, as they sync with each other. EDGE is the
always-online peer for every space that replicates through it.
### [Durable Object](/docs/edge/concepts/architecture/)
A Durable Object is a small Cloudflare server with its own storage, named by a key and created on first use. EDGE
keeps one or more per space, per identity and per agent.
### Environment
An environment is one deployment of EDGE: production, preview or dev, each at its own URL.
### Guard
A guard is a check a [process](#process) is spawned with. It admits or refuses the operations the process invokes
that declare requirements. A child process inherits its parent's guard.
### [Handler](/docs/edge/concepts/operations/)
A handler is the code that does an operation's work. It takes the input and returns an Effect that produces the
output. A handler set groups the handlers a runtime loads together.
### Hibernation
Hibernation is the state of a [process](#process) that waits on a child process or an alarm. A hibernating process
can be written to storage and dropped from memory. A process that waits for input is idle instead.
### Hub
Hub is the EDGE service that keeps DXOS accounts, email verification and the invitations that bring people to DXOS.
### [Index](/docs/edge/concepts/indexing/)
An index is a lookup from a type, a property or a relation to the objects that have it. EDGE keeps one for each space
it can read, and queries read it beside the device's own.
### Invoker
The invoker finds an operation's handler by key, checks the input and output against the schemas, and runs it.
### Layer stack
A layer stack is the set of layers that give a process its services. A layer builds one or more services, and its
affinity says whether one instance serves the application, a space or one process. See
[Processes](/docs/edge/concepts/processes/).
### [Operation](/docs/edge/concepts/operations/)
An operation is a typed action over ECHO objects, with a key, an input schema and an output schema. People, agents
and triggers run it the same way.
### Operation registry
The operation registry is the set of operation definitions kept as ECHO objects, which agents and the MCP server
search. An operation that sets `skipRegistry` is not in it.
### Pipeline
A pipeline is a chain of stages over a stream. An operation uses one to work through a long source, with a cursor to
resume.
### Plugin registry
The plugin registry is the EDGE service that indexes published plugins and hosts their bundles. See
[Compute](/docs/edge/concepts/compute/).
### Process
A process is one run of an operation, with its own services, state and trace, in the app or on EDGE. See
[Processes](/docs/edge/concepts/processes/).
### [Replicator](/docs/edge/concepts/replication/)
A replicator is a Durable Object that keeps EDGE's copy of one space's data in sync with devices. A space has one for
Automerge documents and one for feeds.
### Router
The Router is the EDGE service where each device's WebSocket ends. It keeps a Durable Object per identity, which
holds the identity's WebSockets and delivers messages between its devices. See
[Connecting](/docs/edge/concepts/connecting/).
### Secret
A secret is an API key or a token for an outside service, kept for a space. A process gets the space's secrets as a
service, and EDGE can hold one so that it is not replicated to every device.
### Service access
Service access is a credential that lets an identity use an EDGE or Hub service, written with `Identity.grantServiceAccess`.
### [Trace](/docs/edge/concepts/traces/)
A trace is the set of events a process records. Durable events go to the space's trace feed, and live events go to
the devices watching.
### Trigger
A trigger is an ECHO object that starts an operation on a schedule, on a change, or on a message from outside. See
[Triggers](/docs/edge/concepts/triggers/).
### Worker
A Worker is a Cloudflare Worker. Each EDGE service is one, deployed on its own and reached through the `edge` Worker
at its path. A browser worker, the thread a web app runs code in beside its page, is a different thing, and these
pages always call it a browser worker.
---
# llms.txt
The EDGE site map and full text for agents, in the llms.txt format.
Point an agent at these files before asking it to work with EDGE. They follow the [llms.txt](https://llmstxt.org/) format and cover every written page of this section.
| File | Contents |
| -------------------------------------------- | ---------------------------------------------------------- |
| [`/edge/llms.txt`](/edge/llms.txt) | Each EDGE page with its summary and a link to its Markdown |
| [`/edge/llms-full.txt`](/edge/llms-full.txt) | Every EDGE page in one file |
Any page is also served as Markdown at its URL with `.md` in place of the trailing slash.