Documentation
¶
Overview ¶
Package peers is a thin NATS JetStream client for the claude-peers v3 network. The durable event log (stream PEERS, subjects peers.>) is the source of truth; presence (KV PEERS_PRESENCE) is a projection. Every mutating op publishes an event, so any consumer subscribing peers.> sees the whole network. NATS does the queueing, durability and presence-TTL — this package only wires to it.
Index ¶
- Variables
- func ResolveIdentity(cwd, explicit string) (name, source string)
- func SanitizeName(s string) string
- func URLFromEnv() string
- type Client
- func (c *Client) Claim(ctx context.Context, p Peer) (*Peer, error)
- func (c *Client) Close()
- func (c *Client) Consumers(ctx context.Context) ([]ConsumerStatus, error)
- func (c *Client) Deregister(ctx context.Context, agent string) error
- func (c *Client) Heartbeat(ctx context.Context, agent string) error
- func (c *Client) NATS() *nats.Conn
- func (c *Client) Peers(ctx context.Context) ([]Peer, error)
- func (c *Client) Register(ctx context.Context, p Peer) error
- func (c *Client) Send(ctx context.Context, m Message) error
- func (c *Client) SetSummary(ctx context.Context, agent, summary string) error
- func (c *Client) Setup(ctx context.Context) error
- func (c *Client) Subscribe(ctx context.Context, agent string, h func(Message)) error
- func (c *Client) Watch(ctx context.Context, fromStart bool, h func(Envelope)) error
- type ConsumerStatus
- type Envelope
- type EventType
- type Message
- type Peer
Constants ¶
This section is empty.
Variables ¶
var ErrNameTaken = errors.New("agent name already held")
ErrNameTaken is returned by Claim when a live session already holds the name.
Functions ¶
func ResolveIdentity ¶
ResolveIdentity resolves this session's agent name, most explicit wins: explicit (--as flag) > CLAUDE_PEERS_AGENT > .claude-peers-agent file in cwd > sanitized basename of cwd. The dir default makes the common case zero-config: the agent working in ~/projects/pith is "pith" — which is how people already think about their sessions. Source is "flag", "env", "file", or "default".
func SanitizeName ¶
SanitizeName makes a string safe as a NATS subject token (names ride in peers.msg.<name>): lowercase, [a-z0-9_-] only, everything else collapses to a single '-', trimmed, capped at 32 chars. Returns "" if nothing survives.
func URLFromEnv ¶
func URLFromEnv() string
URLFromEnv resolves the server url the same way ConnectFromEnv does: NATS_URL env, then ~/.config/cp3/url, then localhost.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps a NATS connection + JetStream + the presence KV.
func Connect ¶
Connect dials NATS. creds is a path to a .creds file and token is a plain auth token; either may be "" (empty both = no auth). creds wins if both are set.
func ConnectFromEnv ¶
ConnectFromEnv dials using NATS_URL (default nats://127.0.0.1:4222), NATS_CREDS (a .creds file) and a token — the single place auth is resolved so every binary authenticates the same way. Token resolution: NATS_TOKEN env, then the file named by NATS_TOKEN_FILE, then ~/.config/cp3/token. The file paths keep the secret out of argv, JSON config, and dotfile-synced shell rc.
func (*Client) Claim ¶
Claim registers p only if the name is free (or already held by p's own session). Returns ErrNameTaken + the current holder otherwise. ponytail: best-effort uniqueness via the presence KV — a dead holder's key expires in one TTL (30s), then the name frees. A tighter guarantee would need a lock stream; not worth it for a fleet of agents.
func (*Client) Consumers ¶
func (c *Client) Consumers(ctx context.Context) ([]ConsumerStatus, error)
Consumers lists every consumer attached to the PEERS stream.
func (*Client) Deregister ¶
Deregister removes presence and emits a deregister event.
func (*Client) NATS ¶
NATS exposes the raw connection for advanced uses (e.g. the fleet-compat projector publishing legacy fleet.* events). Prefer the typed methods.
func (*Client) Peers ¶
Peers returns everyone currently present (KV projection = live view). A missing bucket means the network has never been set up here — that is an empty network, not an error.
func (*Client) SetSummary ¶
SetSummary updates an agent's presence summary (re-puts the KV record and emits a presence event so consumers see the change).
func (*Client) Setup ¶
Setup ensures the PEERS stream (the log) and PEERS_PRESENCE KV exist. Idempotent — safe to call on every start.
func (*Client) Subscribe ¶
Subscribe delivers messages addressed to agent via a DURABLE consumer, so messages sent while offline drain on reconnect. Blocks until ctx is done; h is called for each message (auto-acked).
type ConsumerStatus ¶
type ConsumerStatus struct {
Name string
Pending uint64 // not yet delivered
AckPending int // delivered, unacked
LastDelivery *time.Time
}
ConsumerStatus is a liveness snapshot of one consumer on the log. Pending piling up with no recent delivery = an abandoned durable (the v1 graveyard).
type Envelope ¶
type Envelope struct {
V int `json:"v"`
ID string `json:"id"`
Type EventType `json:"type"`
TS int64 `json:"ts"` // unix millis
Actor string `json:"actor"`
Data json.RawMessage `json:"data"`
}
Envelope is the versioned wire contract every consumer builds against.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
cp3
command
cp3 — CLI for the claude-peers v3 NATS-native network.
|
cp3 — CLI for the claude-peers v3 NATS-native network. |
|
internal
|
|
|
boot
Package boot connects to the peers network, bringing a local network up on demand: when the target is localhost and nothing is listening, it spawns a detached `cp3 serve` and retries.
|
Package boot connects to the peers network, bringing a local network up on demand: when the target is localhost and nothing is listening, it spawns a detached `cp3 serve` and retries. |
|
bridge
Package bridge is the shared skeleton for external-runtime adapters: connect to the network, register presence, and inject each inbound peer message into the target runtime via a runtime-specific inject func.
|
Package bridge is the shared skeleton for external-runtime adapters: connect to the network, register presence, and inject each inbound peer message into the target runtime via a runtime-specific inject func. |
|
mcp
cp3-mcp — the Claude Code injection adapter for claude-peers v3.
|
cp3-mcp — the Claude Code injection adapter for claude-peers v3. |
|
opencode
cp3-opencode — bridges the claude-peers NATS network to a running opencode server: each inbound peer message is injected as a steered prompt into one opencode session.
|
cp3-opencode — bridges the claude-peers NATS network to a running opencode server: each inbound peer message is injected as a steered prompt into one opencode session. |