# webrtc p2p agent swarm

- Status: `experiment`
- Track: [peer-to-peer coordination, pip surfaces & malleable plugins](https://aifoc.us/labs/agent-meshes/)
- Layer: 6. Peer-to-Peer, Persistent Chrome & Agent Meshes
- Updated: 1 December 2026
- Live demo: https://webrtc-agent-swarm.kinlan-test.workers.dev
- Source: https://github.com/PaulKinlan/webrtc-agent-swarm

## Inspired by

- [agent-do: my agent loop](https://aifoc.us/agent-do-my-agent-loop/)

## Hypothesis

- **Claim:** Using Cloudflare Durable Objects solely for ephemeral SDP/ICE handshakes while pushing CRDT task logs directly peer-to-peer over `RTCDataChannel` + IndexedDB keeps multi-tab and collaborator state private and functional off-server.
- **Confirms it:** Two peers exchange CRDT deltas end-to-end with zero task bytes sent to the signaling server (`422` rejects payload writes on `SwarmSignaling`), maintaining consistent order across reconnects via `navigator.locks`.
- **Falsifies or bounds it:** Restrictive NATs/firewalls block direct UDP peer connections frequently enough to require TURN relay fallback for consumer networks.

> **Why this exists:** Coordinating state across two open tabs or two collaborators usually means routing every update and note through a central backend database even when both browsers are online simultaneously. Using a server strictly for the brief WebRTC SDP handshake and pushing task state directly peer-to-peer over `RTCDataChannel` keeps application data off the signaling server and resilient offline in IndexedDB.


## What this lab tests

[WebRTC Agent Swarm](https://webrtc-agent-swarm.kinlan-test.workers.dev) ([GitHub](https://github.com/PaulKinlan/webrtc-agent-swarm)) connects two browser peers over [`RTCDataChannel`](https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel) using a Durable Object (`SwarmSignaling`) solely for ephemeral SDP/ICE handshakes while persisting CRDT logs locally in IndexedDB with `navigator.locks` leader coordination.


## Open questions to iterate on

- How well does `BroadcastChannel` + `navigator.locks` handle same-origin multi-tab election without dropping WebRTC data channels on tab close?
- What compact CRDT tombstoning keeps IndexedDB storage bounded across long agent swarms?

## Iteration log

- **2026-12-01:** Built Peer A/B WebRTC `RTCDataChannel` CRDT workbench with SQLite Durable Object signaling (`422` data guard) and IndexedDB persistence.
- **2026-12-01:** Reordered workbench above setup docs, added live HTTP 422 signaling-guard probe and CSP-safe DOM syntax highlighting, hardened 700px embed responsiveness, and added no-cache static headers.

## Primitives tested

- `RTCDataChannel`
- `IndexedDB`
- `Web Locks API`
- `Durable Object SDP Signaling`

## Connected labs in other tracks

- [reactive agent statecharts, blackboards & worker actors](https://aifoc.us/labs/reactive-prompts/reactive-agent-machines/) — Explores deterministic local statecharts vs. distributed actor state.
- [document pip web coprocessor](https://aifoc.us/labs/agent-meshes/pip-web-coprocessor/) — Shares cross-window synchronization primitives over `BroadcastChannel`.

---
Canonical: https://aifoc.us/labs/agent-meshes/webrtc-agent-swarm/
