← Back to NIP Index

Unified Agent State Synchronization

NIP: 6 | Author: jolestar(@jolestar) | Status: Draft | Created: May 13, 2025


Abstract

This NIP defines a peer-to-peer (P2P) protocol for synchronizing an agent’s state across multiple devices and application contexts. This allows an agent to maintain a consistent state and operate seamlessly regardless of which device or application is currently in use. The protocol aims to support nested JSON schema-like data structures, multi-platform compatibility, offline operation with eventual consistency, and optional end-to-end privacy.

Motivation

Users often interact with agents from various devices (desktop, mobile, web) and through multiple applications on those devices. A robust synchronization mechanism is crucial to ensure that the agent’s knowledge, capabilities, and context are consistent across all these devices and applications. This NIP builds upon existing NIPs for identity (NIP-1), authentication (NIP-2), and payments (NIP-3) to provide a comprehensive solution for unified agent state.

Specification

Core Concepts

  1. Agent Identity: Each agent is uniquely identified by its DID (as defined in NIP-1). All devices and applications associated with an agent share this common identity. The DID document lists authorized operational keys (for devices, applications, etc.), which are crucial for verifying the authenticity of discovery messages and synchronization updates.
  2. Device/Application Identity & Authorization:
    • Each device or application instance will have its own unique cryptographic key pair. Its public key is registered in the Agent’s DID document as a verificationMethod entry, as per NIP-1.
    • OperationalContextID: Throughout this NIP, OperationalContextID (e.g., DeviceID, ApplicationID) refers to the unique fragment identifier of an operational key’s verificationMethod entry within the Agent’s DID document (e.g., if a verificationMethod.id is did:agent:123#op-key-xyz, then the OperationalContextID is op-key-xyz). This OperationalContextID is used in discovery and synchronization messages to uniquely identify a device or application instance within the context of its AgentDID.
    • A device or application instance must be authorized by the agent’s primary key (or a designated master device/application) to participate in state synchronization. This authorization can leverage mechanisms similar to those in NIP-2 for A2A authentication.
    • Consideration for secure key sharing: New devices or applications, once authorized via NIP-1, need to securely obtain a shared secret or a specific decryption key (e.g., an encrypted epriv using a master password or temporary authorization token via QR code/OTP) to access and decrypt the agent’s synchronized state. This process is detailed in the “Device Authorization Flow”.
  3. State Representation:
    • The agent’s state will be represented using Conflict-free Replicated Data Types (CRDTs). CRDTs are well-suited for P2P environments as they allow for concurrent updates from multiple devices and guarantee eventual consistency without complex conflict resolution logic.
    • The state should support nested JSON schema-like data structures.
    • Recommended CRDT Libraries: Libraries like Automerge (good for JSON-like nested states) or Yjs (performant for fine-grained collaborative editing and rich data types) are strong candidates due to their robust CRDT implementations and support for complex data structures.
  4. Peer Discovery:
    • Devices and application instances belonging to the same agent need to discover each other’s current connection information (e.g., IP address, port, transport protocol, or inter-process communication channels). The Agent’s DID document (NIP-1) serves as the authoritative source for verifying that a discovered peer (device or application) is legitimate, but not for storing its dynamic connection information.
    • Possible mechanisms:
      • Decentralized Hash Table (DHT): See “Detailed Protocol”.
      • Rendezvous Server: See “Detailed Protocol”.
      • Local Network/Inter-Process Discovery (mDNS/DNS-SD, IPC mechanisms): See “Detailed Protocol”.
      • Discovery via Synchronization Layer: See “Detailed Protocol”.
  5. Communication Protocol & Synchronization Layer:
    • Secure P2P communication channels will be established between devices and applications.
    • Underlying P2P transport: WebRTC (for browser-based agents) or libp2p (for more general applications).
    • All communication must be end-to-end encrypted using keys derived during the device/application authorization/authentication phase.
    • Synchronization Layer Options:
      • GunDB: Lightweight, easy to deploy, with built-in SEA for identity and encryption. Suitable for simpler state and rapid deployment.
      • OrbitDB: Builds on IPFS and Merkle-CRDTs, offering decentralized and persistent state. Requires IPFS.
      • Matrix: Provides strong E2EE, user identity, and can act as a secure message/state synchronization bus using its event DAG as a form of CRDT.
      • Custom sync protocol built directly over Yjs or Automerge providers.
  6. Synchronization Process:
    • Initial Sync: When a new device or application is authorized or an existing one comes online, it will attempt to connect to known peers and fetch the current state.
    • State Exchange: Once connected, devices and applications will exchange their CRDT state. CRDTs inherently handle the merging of different versions of the state.
    • Delta Updates: After the initial sync, devices and applications will only transmit changes (deltas) to the state to minimize bandwidth usage. Libraries like Yjs and Automerge support this.
    • Gossip Protocol: Updates can be propagated through the network of connected peers using a gossip-like mechanism to ensure all devices and applications eventually receive all updates.
  7. Offline Support and Resilience: (Moved to its own sub-section under Specification for clarity, as it’s a key feature)
    • Devices and applications should be able to operate offline and accumulate state changes.
    • When a device or application reconnects, it will sync its changes with its peers.
    • The system should be resilient to devices and applications frequently joining and leaving the network.

Detailed Protocol

Rationale

(Placeholder: To be filled in with design choices, alternatives considered, and community consensus.)

Backwards Compatibility

(Placeholder: To be filled in. This NIP defines a new protocol, so initial implementations would not have backwards compatibility issues with older versions of this specific protocol. Compatibility with other NIPs like NIP-1, NIP-2, NIP-3 is by design.)

Test Cases

(Placeholder: To be filled in with specific test cases for device/application authorization, peer discovery, state synchronization, conflict resolution with CRDTs, offline operation, and security checks.)

Reference Implementation

(Placeholder: To be provided once a reference implementation is available.)

Security Considerations

The security of this NIP relies heavily on the underlying NIPs for identity (NIP-1) and authentication (NIP-2).

Copyright and related rights waived via CC0.