@jocax/agent-coms0.3.1
Peer-to-peer agent communication: Unix socket (local) and HTTP/SSE (networked) with Bun hub server
Untested on omp
This is a pi package. No one has reported whether it works on omp yet. If you try it, add your result below.
Reports 0
No reports yet.
Readme
@jocax/agent-coms
Peer-to-peer agent communication over Unix sockets (same machine) and HTTP/SSE (networked). Enables bidirectional information flow between equal pi agents — no orchestrator, no parent/child hierarchy.
What
Two transports, one unified tool surface. coms (local) uses Unix sockets with a file-based discovery registry for agents on the same machine. coms-net (networked) uses HTTP + Server-Sent Events via a Bun hub server for cross-machine communication. Both variants share the same four-tool pattern (list, send, get, await), a live peer pool widget, self-healing lifecycle management, and hop-limit enforcement.
Includes a standalone Bun-based hub server (scripts/coms-net-server.ts) for the networked transport.
Why
Flat team hierarchies beat orchestrator patterns. Instead of a central dispatcher routing messages, agents communicate directly as peers. Two variants cover both local development (fast, no auth) and cross-machine collaboration (LAN or remote via reverse proxy). The tool surface stays consistent regardless of transport — agents use coms_send / coms_net_send the same way.
Install
Install the package from npm:
{
"source": "npm:@jocax/agent-coms"
}
# From a local checkout
pi install -l .
The coms-net server requires Bun. Install it separately:
curl -fsSL https://bun.sh/install | bash
Usage
Local Coms (Unix Sockets)
Launch two pi agents on the same machine. Each must share the same --project to discover each other.
Terminal 1 (planner):
pi --coms-name planner --coms-purpose "Plan and review work" --coms-project my-project --coms-color "#36F9F6"
Terminal 2 (coder):
pi --coms-name coder --coms-purpose "Implement approved plans" --coms-project my-project --coms-color "#FF7EDB"
Each agent auto-discovers peers via the file-based registry at ~/.pi/coms/projects/<project>/agents/. The live pool widget shows all connected peers with names, models, and context usage bars.
In the planner agent, prompt the LLM:
Use coms_send to send the implementation plan to coder and ask them to review it.
Wait for the response with coms_await.
Networked Coms (HTTP/SSE)
Step 1: Start the hub server (Bun required):
bun run software/packages/agent-coms/scripts/coms-net-server.ts
The server prints a boot banner with the URL, project, and PID. It writes ~/.pi/coms-net/server.json for auto-discovery.
Step 2: Connect clients — each in its own terminal:
# Terminal 1 (planner)
pi --coms-net-name planner --project my-project --color "#36F9F6"
# Terminal 2 (coder)
pi --coms-net-name coder --project my-project --color "#FF7EDB"
Clients auto-discover the server from server.json. For remote clients (different machine):
pi --coms-net-name remote-agent --server-url http://192.168.1.100:3000 --auth-token <token> --project my-project
Step 3: Communicate — agents use the coms_net_* tools to send messages, poll for responses, and await replies.
Environment Variables
| Variable | Default | Description |
|---|---|---|
AE_PI_AGENT_COMS_ENABLED |
true |
Enable local coms transport (set false or 0 to disable) |
AE_PI_AGENT_COMS_NET_ENABLED |
true |
Enable networked coms-net transport (set false or 0 to disable) |
AE_PI_AGENT_COMS_MAX_HOPS |
5 |
Max message forwarding hops for local coms |
AE_PI_AGENT_COMS_NET_MAX_HOPS |
5 |
Max message forwarding hops for coms-net |
AE_PI_AGENT_COMS_SEQUENTIAL_IDS |
false |
Set to true to count message ids up from sender and target (alpha-beta-1 locally, msg_alpha_beta_1 on the hub) instead of drawing them at random, for reproducible demos and replays |
AE_PI_AGENT_COMS_POOL_REFRESH_MS |
10000 |
Peer pool ping interval in milliseconds; 0 keeps the pool event-driven, refreshed by coms_list, /coms and inbound messages |
AE_PI_AGENT_COMS_NET_HEARTBEAT_MS |
10000 |
Heartbeat interval in milliseconds (10s default) |
AE_PI_AGENT_COMS_NET_MESSAGE_TTL_MS |
1800000 |
Message time-to-live in milliseconds (30 min default) |
AE_PI_AGENT_COMS_NET_SERVER_URL |
— | Server base URL (auto-discovered from server.json if not set) |
AE_PI_AGENT_COMS_NET_AUTH_TOKEN |
— | Bearer auth token (required for non-localhost hub binds) |
AE_PI_AGENT_COMS_NET_PROJECT |
default |
Default project namespace for coms-net |
AE_PI_AGENT_COMS_NET_LOG_QUIET |
0 |
Set to 1 to suppress hub server event logs (except startup/shutdown) |
AE_PI_AGENT_COMS_NET_LOG_HEARTBEAT |
0 |
Set to 1 to show heartbeat logs in hub server |
Tools
Local Coms (coms_*)
| Tool | Description |
|---|---|
coms_list |
List peer agents on the same machine. Pings peers for live context usage and model info. Supports project and include_explicit filters. |
coms_send |
Send a prompt to a named peer agent. Returns a msg_id for tracking. Supports conversation_id and response_schema for structured responses. |
coms_get |
Non-blocking poll for message status by msg_id. Returns pending, complete (with response), or error. |
coms_await |
Block until a reply arrives for a previously sent message. Races the pending promise against an optional timeout_ms (default 30s). Returns the response text. |
Networked Coms (coms_net_*)
| Tool | Description |
|---|---|
coms_net_list |
List all agents connected to the coms-net hub for the current project. Shows names, models, status, and context usage. |
coms_net_send |
Send a message to another agent via the hub server. Returns a msg_id for tracking. Supports conversation_id and response_schema. |
coms_net_get |
Check message status by msg_id. Checks local SSE-resolved results first, falls back to server query. |
coms_net_await |
Wait for a response, blocking until arrival or timeout. Races the local SSE promise against a server long-poll endpoint. |
CLI Flags
The two transports register different flag names. Local coms prefixes every
flag with coms-. The networked transport only prefixes its name flag,
--coms-net-name — pi itself owns the unprefixed --name, so an agent named
with --name falls back to agent-<6chars> on the hub too.
Local Coms
| Flag | Type | Default | Description |
|---|---|---|---|
--coms-name |
string | agent-<6chars> |
Override agent name for peer discovery |
--coms-purpose |
string | "general purpose agent" |
Override agent purpose description |
--coms-project |
string | default |
Project namespace for peer discovery |
--coms-color |
string | (auto-assigned) | Hex color for agent display (e.g. #FFB86C) |
--coms-explicit |
boolean | false |
Hide agent from auto-discovery lists |
Networked Coms
| Flag | Type | Default | Description |
|---|---|---|---|
--coms-net-name |
string | agent-<6chars> |
Override agent name for coms-net identity |
--purpose |
string | "coms-net agent" |
Override agent purpose description |
--project |
string | default |
Project namespace for peer discovery |
--color |
string | (auto-assigned) | Hex color for agent display (e.g. #FFB86C) |
--explicit |
boolean | false |
Hide agent from auto-discovery lists |
--server-url |
string | (auto-discovered) | Override hub server URL |
--auth-token |
string | (auto-discovered) | Override auth token |
Flags can also be set via system prompt frontmatter. Create a markdown file with YAML frontmatter:
---
name: code-reviewer
description: Expert code reviewer for TypeScript projects
color: "#5E9EFF"
---
# Code Reviewer Agent
You are an expert code reviewer.
Then pass it with --system-prompt path/to/agent.md. CLI flags override frontmatter.
Commands
| Command | Description |
|---|---|
/coms |
Refresh the coms peer pool. Flags: --all (include explicit agents), --project <name> (filter by project) |
/coms-net |
Manage coms-net connection. Flags: --all, --reconnect (force SSE reconnect), --server (show server info), --project <name> |
Hub Server
The coms-net hub server is a standalone Bun script at scripts/coms-net-server.ts.
# Start with defaults (localhost, OS-assigned port, project "default")
bun run scripts/coms-net-server.ts
# Custom project and port
bun run scripts/coms-net-server.ts --project my-project --port 3000
# Bind to all interfaces (requires AE_PI_AGENT_COMS_NET_AUTH_TOKEN env var)
AE_PI_AGENT_COMS_NET_AUTH_TOKEN=my-secret-token bun run scripts/coms-net-server.ts --host 0.0.0.0 --port 3000
Server lifecycle:
- Loopback bind without an env token → auto-generates a random token, writes
server.secret.json(0600 permissions) - Non-loopback bind without an env token → fails at startup (exit 1)
- Auth token is never printed in logs, the boot banner, or
server.json - Clean shutdown on SIGINT/SIGTERM: closes all SSE streams, unlinks state files, stops server
Server endpoints (all /v1/* routes require Bearer auth):
| Method | Path | Description |
|---|---|---|
GET |
/health |
Health check (no auth) |
POST |
/v1/agents/register |
Register or re-register an agent |
GET |
/v1/events |
Open SSE event stream |
POST |
/v1/agents/:sid/heartbeat |
Agent heartbeat with context/queue stats |
GET |
/v1/agents |
List agents (filter by project, include_explicit) |
DELETE |
/v1/agents/:sid |
Deregister an agent |
POST |
/v1/messages |
Send a message to a target agent |
GET |
/v1/messages/:id |
Get message status and response |
GET |
/v1/messages/:id/await |
Long-poll until message reaches terminal status |
POST |
/v1/messages/:id/response |
Submit a response for a message |
Safety Notes
- One writer agent at a time. Multiple agents writing files simultaneously risks conflicts. Coordinate writes via messages.
- Use
coms_await, not polling loops. Blocking awaits are more efficient than repeatedcoms_getcalls. - Avoid ping-pong loops. Do NOT call
coms_send/coms_net_sendto reply to inbound messages. Just answer the prompt normally — your final assistant response is automatically captured and sent back to the sender. - Use the same
--projectfor agents that should discover each other. - Auth tokens never appear in logs or error messages. The
safeError()helper strips tokens from all user-visible output. - Hop limit is 5 by default. Messages forwarded through a chain of agents will be rejected at the 6th hop. This prevents forwarding loops.
Examples
See the examples/ directory for annotated demo scripts:
local-coms.ts— Two pi agents communicating over Unix socketscoms-net.ts— Hub server + two clients communicating over HTTP/SSE
Run the demo
just demo agent-coms ["Two peers over the socket" | "Three peers through the hub"]
A herdr workspace opens with one pane per peer — alpha and beta in the
socket scenario, plus gamma in the hub scenario — over a disposable copy of
the small-app sandbox. The demo pins each peer's coms name and colour to
its pane label with a per-agent launch flag, so the names coms_list /
coms_net_list report are the names on screen. Each scenario loads only the
transport it exercises.
| Step | Do this | Watch |
|---|---|---|
| 1 | In the hub scenario, wait for the hub pane |
it prints service hub: pane w1:p1, listening on 45801 before any agent pane starts |
| 2 | Type /coms (socket scenario) or /coms-net (hub scenario) |
the peer pool widget lists every other peer by its pinned name — the same as its pane label |
| 3 | Let alpha's turn run | it calls coms_send/coms_net_send (sent · …), then coms_await/coms_net_await (… complete) once beta replies in its own response text |
| 4 | In the hub scenario, watch the hub pane |
it logs ↓ response msg_… from agent-… for each reply it relays |
Two limitations:
coms_net_listlists the caller among the peers without marking it, so with the kit's small default model alpha sometimes messages itself and waits for its own reply — rerun the scenario or nudge it if that happens.- The server URL and auth token are passed to every pane explicitly, because
the hub writes
server.jsonto a path its own clients do not look at.
The hub scenario also needs bun on PATH and port 45801 free.
Needs herdr 0.9.3 or newer and one reachable model provider — see demo-kit.
Development
# Type-check
npx tsc --noEmit -p software/packages/agent-coms/tsconfig.json
# Run tests
npx vitest run software/packages/agent-coms/tests/