agent-envoy 0.2.0

Message/coordination server for AI coding agents using sqlitegraph pub/sub
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# Envoy API Reference

Server: `http://localhost:9876` (default)
Binary: `ENVOY_DB=/path/envoy.db target/release/envoy`
Protocol: JSON over HTTP + WebSocket

---

## Authentication

None. Envoy is a local coordination server.

**IMPORTANT: Agent IDs vs Agent Names.** Agents are identified by `agent_id` (server-assigned, e.g. `"id1"`), NOT by display name (e.g. `"hermes"`). The `from`, `to`, `heartbeat.agent_id`, and `GET /messages?to=` fields all require `agent_id`. To resolve a name to an ID, call `GET /agents` and match on the `name` field. Clients should cache the name→ID mapping.

---

## Error Format

All errors return JSON:

```json
{
  "error": {
    "code": "ERROR_CODE",
    "message": "human-readable description"
  }
}
```

| HTTP | Code | When |
|------|------|------|
| 400 | `INVALID_MESSAGE` | Missing required fields, empty parts |
| 400 | `SERIALIZATION_ERROR` | Malformed JSON body |
| 400 | `TOO_MANY_PARTS` | More than 20 parts in message |
| 400 | `MESSAGE_TOO_LARGE` | Single text part exceeds 1MB |
| 404 | `AGENT_NOT_FOUND` | Agent ID does not exist |
| 404 | `MESSAGE_NOT_FOUND` | Message ID does not exist |
| 404 | `TASK_NOT_FOUND` | Task ID does not exist |
| 404 | `DEPENDENCY_NOT_FOUND` | Dependency ID does not exist |
| 404 | `PROJECT_CONFIG_NOT_FOUND` | No config for project |
| 404 | `SUBSCRIPTION_NOT_FOUND` | Agent not subscribed to project |
| 403 | `NOT_TASK_CLAIMANT` | Agent does not own the task |
| 409 | `AGENT_ALREADY_EXISTS` | Agent name already registered |
| 409 | `AGENT_OFFLINE` | Sender is not online |
| 409 | `TASK_ALREADY_CLAIMED` | Task taken by another agent |
| 409 | `INVALID_TASK_STATE` | Disallowed state transition |
| 409 | `DUPLICATE_DEPENDENCY` | Dependency already exists |
| 500 | `INTERNAL_ERROR` | Graph/DB error |

---

## Agents

### Register Agent

```
POST /agents
```

```json
{ "name": "claude2", "kind": "worker", "parent_id": null }
```

Response (`201`):
```json
{
  "agent_id": "id1",
  "name": "claude2",
  "kind": "worker",
  "online": true,
  "parent_id": null,
  "registered_at": "2026-05-08T06:00:00Z",
  "last_heartbeat_at": "2026-05-08T06:00:00Z",
  "status": { "state": "working", "working_on": "...", "checkpoint": "..." }
}
```

`agent_id` is server-assigned (auto-increment). Use this ID for all subsequent calls.

### List Agents

```
GET /agents
```

Response:
```json
{ "agents": [ { "agent_id": "id1", "name": "claude2", ... } ] }
```

### Get Agent

```
GET /agents/{agent_id}
```

Response includes `children` array of sub-agent IDs.

### Disconnect Agent

```
DELETE /agents/{agent_id}
```

Response:
```json
{ "disconnected": true, "affected": ["dep_id_1"] }
```

---

## Messages

### Send Message

```
POST /messages
```

```json
{
  "type": "direct",
  "from": "id1",
  "to": "id2",
  "task_id": null,
  "context_id": "status_update",
  "parts": [
    { "text": "WS client dogfood complete" }
  ]
}
```

| Field | Required | Description |
|-------|----------|-------------|
| `type` | yes | `direct`, `handoff`, `heartbeat`, `system` |
| `from` | yes | Sender agent_id (must be registered and online) |
| `to` | yes | Recipient agent_id (must be registered) |
| `task_id` | no | Task this message relates to |
| `context_id` | no | Thread/subject for grouping messages |
| `parts` | yes | Array of 1-20 content parts |

Part types:
- `{ "text": "body text" }` — text content (max 1MB each)
- `{ "data": { "key": "value" } }` — structured JSON
- `{ "url": "https://..." }` — URL reference

Response (`201`):
```json
{
  "message_id": "15",
  "type": "direct",
  "from": "id1",
  "to": "id2",
  "task_id": null,
  "context_id": "status_update",
  "timestamp": "2026-05-08T06:00:00Z",
  "sequence_id": 1,
  "parts": [ { "text": "WS client dogfood complete" } ]
}
```

If the recipient has an active WebSocket, the message is also pushed as:
```json
{ "event": "message", "data": { <full message envelope> } }
```

### Poll Messages

```
GET /messages?to={agent_id}&since={seq}&limit={n}
```

| Param | Required | Default | Description |
|-------|----------|---------|-------------|
| `to` | yes || Recipient agent_id |
| `since` | no | `0` | Return messages with sequence_id > this value |
| `limit` | no | `50` | Max messages (capped at 100) |

Response:
```json
{
  "messages": [ { "message_id": "15", ... } ],
  "latest_sequence": 15
}
```

**Polling pattern:**
1. First poll: `GET /messages?to=id2&since=0`
2. Store `latest_sequence` from response (e.g. `15`)
3. Next poll: `GET /messages?to=id2&since=15`
4. Repeat

### Get Message

```
GET /messages/{message_id}
```

Returns the full `MessageEnvelope`.

### Pending Messages (offline agent)

```
GET /agents/{agent_id}/messages/pending
```

Returns up to 100 undelivered messages for a disconnected agent. Useful for catch-up after reconnect.

---

## Heartbeat

### Send Heartbeat

```
POST /heartbeat
```

```json
{
  "agent_id": "id1",
  "status": {
    "state": "working",
    "task_id": null,
    "blocked_reason": null,
    "waiting_on_agent": null,
    "checkpoint": "implementation",
    "working_on": "building WS client"
  }
}
```

Agent states: `working`, `blocked`, `waiting_review`, `idle`

Response:
```json
{
  "accepted": true,
  "nudges": [
    { "reason": "Dependent claude1 may now be unblocked", "severity": "info" }
  ]
}
```

If no status is provided, a default `working` heartbeat is recorded.

Agents without heartbeat for `stale_threshold_minutes` (default: 5) are flagged as stale. The nudge loop sends warnings and reclaims their tasks.

**Note:** Envoy loads persisted agents as `online: false` on server restart. Clients should not blindly reuse an offline `agent_id` found by name after restart. Either re-register (creates new online agent) or send a heartbeat to bring the existing agent back online.

---

## WebSocket

### Connect

```
GET /ws/{agent_id}
```

Upgrade to WebSocket. The agent is marked online immediately.

### Server → Client Messages

All server pushes follow:
```json
{ "event": "<event_type>", "data": { ... } }
```

| Event | When |
|-------|------|
| `agent_connected` | On connect: `{ "agent_id": "id1" }` |
| `message` | New message for this agent |
| `hook_event` | Hook result event |
| `gate_event` | Quality gate event |
| `ci_event` | CI status event |
| `doc_event` | Doc sync event |
| `task_proposed` | New task created |
| `task_claimed` | Task claimed by agent |
| `task_state_changed` | Task state updated |
| `event_catchup` | Undelivered events replayed on reconnect |
| `channel_lagged` | Broadcast channel overflowed |
| `nudge` | Stale agent nudge |
| `blocker_stale` | A blocking agent may be stalled |
| `blocker_updated` | A blocking agent sent heartbeat |
| `dependency_resolved` | A dependency was resolved |
| `task_reclaimed` | Task reclaimed from stale agent |

### Client → Server Messages

**Heartbeat:**
```json
{
  "type": "heartbeat",
  "data": {
    "state": "working",
    "working_on": "building plugin",
    "checkpoint": "implementation"
  }
}
```
Response: `{ "type": "heartbeat_ack", "data": { "accepted": true, "timestamp": "..." } }`

**Ping:**
```json
{ "type": "ping" }
```
Response: `{ "type": "pong" }`

### Catch-up on Reconnect

When a WS connection opens for an agent that was previously connected:

1. Pending messages (from `MessageStore`) are replayed first
2. Undelivered events (from `DeliveryTracker`) are replayed as `event_catchup`
3. `agent_connected` event is sent last

---

## Event Bus

### Ingest Hook Event

```
POST /events/hook
```

```json
{
  "project": "envoy",
  "hook_name": "stub-check",
  "exit_code": 2,
  "output": "Found todo!() in src/lib.rs"
}
```

Severity mapping: exit_code 2 → `blocking`, non-zero → `warning`, 0 → `info`

### Ingest Gate Event

```
POST /events/gate
```

```json
{
  "project": "envoy",
  "gates_passed": 7,
  "gates_total": 8,
  "failures": ["wiring-check"]
}
```

### Ingest CI Event

```
POST /events/ci
```

```json
{
  "project": "envoy",
  "run_id": "25459445036",
  "status": "completed",
  "conclusion": "success",
  "head_branch": "master",
  "display_title": "CI run"
}
```

Conclusion `success` → `info`, `failure` → `blocking`, other → `info`

### Ingest Doc Event

```
POST /events/doc
```

```json
{
  "project": "envoy",
  "doc_files": ["CHANGELOG.md"],
  "last_updated_seconds": 3600
}
```

Over 86400s (24h) → `warning`, otherwise → `info`

### Query Events

```
GET /events?project={name}&since={timestamp}&limit={n}
```

| Param | Required | Default | Description |
|-------|----------|---------|-------------|
| `project` | yes || Project to filter by |
| `since` | no || RFC3339 timestamp, return events after this |
| `limit` | no | `50` | Max events (capped at 100) |

Response:
```json
{ "events": [ { "id": "5", "project": "envoy", ... } ], "count": 1 }
```

---

## Task Board

### Propose Task

```
POST /tasks/propose
```

```json
{ "project": "envoy", "description": "Build WS client", "blocked_by": [] }
```

### Claim Next Available

```
POST /tasks/claim-next
```

```json
{ "project": "envoy", "agent_id": "id1" }
```

Claims the oldest `proposed` task in the project, ordered by `created_at`.

### Claim Specific Task

```
POST /tasks/{id}/claim
```

```json
{ "agent_id": "id1" }
```

### Update Task State

```
POST /tasks/{id}/state
```

```json
{ "state": "in_progress", "checkpoint": "implementation" }
```

States: `proposed` → `claimed` → `in_progress` → `waiting_review` → `done`
Also: `claimed` → `proposed` (release), `in_progress` → `proposed` (release)

When a task moves to `done`, all tasks blocked by it receive a `dependency_resolved` event.

### Get Task

```
GET /tasks/{id}
```

### List Tasks

```
GET /tasks?project={name}&state={state}
```

---

## Subscriptions

### Subscribe Agent to Project

```
POST /subscriptions
```

```json
{ "agent_id": "id1", "project": "envoy" }
```

Subscribed agents receive project events via WebSocket broadcast.

### Unsubscribe

```
DELETE /subscriptions/{agent_id}/{project}
```

### List Subscriptions

```
GET /subscriptions/{agent_id}
```

Response:
```json
{ "agent_id": "id1", "subscriptions": ["envoy", "magellan"] }
```

---

## Project Config

### Get Config

```
GET /projects/{name}/config
```

### Set Config

```
POST /projects/{name}/config
```

```json
{
  "project": "envoy",
  "ci_poll_seconds": 60,
  "doc_poll_seconds": 300,
  "ci_repo_owner": "oldnordic/envoy",
  "doc_files": ["CHANGELOG.md"]
}
```

---

## Dependencies

### Create Dependency

```
POST /dependencies
```

```json
{
  "dependent_agent": "id1",
  "blocker_agent": "id2",
  "reason": "waiting for WS reconnect fix"
}
```

### Get Dependencies

```
GET /dependencies/blocker/{agent_id}    — what this agent blocks
GET /dependencies/dependent/{agent_id}  — what blocks this agent
```

### Resolve Dependency

```
POST /dependencies/{dep_id}/resolve
```

---

## Health & Stats

### Health Check

```
GET /health
```

```json
{ "status": "ok", "uptime_seconds": 3600, "agents_online": 3 }
```

### Stats

```
GET /stats
```

```json
{ "messages_total": 42, "agents_registered": 4 }
```

### Nudge Config

```
GET /nudge-config
POST /nudge-config
```

```json
{ "stale_threshold_minutes": 5, "check_interval_seconds": 30 }
```

---

## Proposed Additions (not yet implemented)

These endpoints are needed for proper plugin support:

### Acknowledge Message

```
POST /messages/{id}/ack
```

Marks a message as delivered/read. Prevents re-delivery on polling. Returns the updated message with a `delivered_at` timestamp.

### List Online Agents

```
GET /agents/online
```

Returns only agents currently marked online (useful for `envoy_who` tool).