Metadata-Version: 2.4
Name: jchat
Version: 0.2.0
Summary: Decentralized signal & notification bus for coordinating AI agents (Python API, CLI, MCP).
License-Expression: MIT
Requires-Python: >=3.15
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# jchat

Decentralized signal & notification bus for coordinating AI agents (Python API, CLI, MCP). Standard library only, SQLite-backed, zero runtime dependencies.

## Install

Requires Python >= 3.15.

```bash
pip install jchat
```

Verify your environment with the built-in loopback self-test:

```bash
jchat doctor
```

## Quick Start

### CLI

```bash
jchat serve                                          # start node on 127.0.0.1:8765
jchat register agent-b --pid $$                      # register live agent
jchat ping agent-b                                   # live online handshake
jchat send agent-b "review ready" --sender agent-a --ref git:HEAD --kind verify
jchat pull agent-b                                   # lease messages (30 s default)
jchat ack <msg-id> agent-b                           # hard-delete message on read
jchat status                                         # node, agents, queue depth (no bodies)
```

### Python API

```python
from jchat import Client, NetPolicy, Node, wrap_untrusted

policy = NetPolicy(netmode="localhost")
with Node(node_name="local", db_path="/tmp/jchat.db", net_policy=policy, port=0) as node:
    client = Client(node.url)
    client.send(sender="agent-a", recipient="agent-b", kind="notify", body="done", ref="git:HEAD")
    for msg in client.pull("agent-b"):
        print(wrap_untrusted(msg))
        client.ack_read(msg["id"], "agent-b")
```

### MCP (stdio)

Run the MCP stdio server for agent harnesses:

```bash
jchat mcp
```

## Envelope Contract (`<= 1000` chars)

`jchat` is a signal and pointer bus, not a chat history store:

- `body`: strictly `<= 1000` characters (`ValidationError` / `400 payload_too_large` if exceeded).
- `ref`: optional resource pointer (`<= 512` chars, no newlines or embedded tokens/secrets) prefixed with `git:`, `gh:`, `drive:`, `file:`, or `https:`.
- `kind`: `notify`, `verify`, `claim`, `ask`, or `status`.
- `priority`: `normal` or `urgent` (`urgent` delivered first).
- `ttl_s`: `1..604800` (default `86400`), plus optional `dedup_key` (idempotent push) and `reply_to`.

## Message Lifecycle

1. **`queued`**: validated envelope persisted in SQLite (`messages`) awaiting pull or peer `/ingest`.
2. **`delivered`**: leased by `POST /pull` (`lease_s=30` default). If a consumer crashes before `ack`, the lease expires and the message returns to `queued`.
3. **`read`**: `POST /ack` hard-deletes the row (`DELETE FROM messages WHERE id = ?`) and leaves only a 1 h in-memory body-less receipt (`id -> {status, reason, ts}`).
4. **`failed`**: body-less receipt recorded on `ttl_expired`, `agent_offline` (when `require_online=True`), `node_unreachable`, or `node_out_of_mesh`.

## Netmodes (`JCHAT_NETMODE`)

Wildcard binds (`0.0.0.0`, `::`, `*`) are always refused. Non-`localhost` modes authenticate every request with HMAC-SHA256 (`X-JChat-Ts`, `X-JChat-Nonce`, `X-JChat-Sig`) using a 30 s skew window and nonce replay cache.

| Mode | Requirements | Inbound / Outbound Rule |
|---|---|---|
| `localhost` (default) | None | Binds `127.0.0.1` only; remote peer pushes refused (`node_out_of_mesh`). |
| `allowlist` | `JCHAT_KEY`, `JCHAT_ALLOW_IPS` | Loopback + listed `JCHAT_ALLOW_IPS` only (DNS-resolved outbound). |
| `tailscale` | `JCHAT_KEY` | Loopback, `100.64.0.0/10`, or `fd7a:115c:a1e0::/48` only. |
| `cloud` | `JCHAT_KEY`, `https://` `JCHAT_CLOUD_URL` | Relays via configured HTTPS endpoint. |

Key environment variables: `JCHAT_NETMODE`, `JCHAT_KEY`, `JCHAT_ALLOW_IPS`, `JCHAT_CLOUD_URL`, `JCHAT_BIND`, `JCHAT_PEERS`, `JCHAT_NODE`, `JCHAT_DB`, `JCHAT_URL`.

## Hooks & Diagnostics

- `jchat install-hooks <claude|agy|opencode|cursor>` installs turn-boundary hooks with automatic backups (`--remove` restores).
- `jchat doctor` runs an isolated loopback self-test covering send, pull, ack, and delete-on-read.

## License

MIT. See [LICENSE](LICENSE).
