Skip to content

docs: broker HTTP / WS API reference#865

Merged
willwashburn merged 3 commits into
mainfrom
docs/broker-api-reference
May 17, 2026
Merged

docs: broker HTTP / WS API reference#865
willwashburn merged 3 commits into
mainfrom
docs/broker-api-reference

Conversation

@willwashburn
Copy link
Copy Markdown
Member

Summary

  • New doc page reference-broker-api covering the broker's listen API surface (port 3888 by default): auth model, all 22 routes grouped by purpose, full request/response shapes for the ones a caller would compose by hand, and the /ws protocol (?sinceSeq= resume, replay_gap frame, full inventory of durable + ephemeral event kinds).
  • Worked end-to-end curl + websocat example so you can spawn → observe → drive → message → release an agent without going through the dashboard.
  • Mirrors to docs/reference-broker-api.md per .claude/rules/docs-sync.md.
  • Added the slug to the CLI nav group in web/lib/docs-nav.ts.

Why

None of the broker's HTTP/WS surface was documented anywhere outside the source. That made it invisible to anyone trying to build a custom integration, debug a flow, or even understand what the dashboard talks to. It also blocks the work in #864 (view / drive subcommands) — the clients are thin wrappers around this API, and a doc reviewer needs to be able to see the surface they wrap.

Notes

  • A handful of routes (/api/spawn, /api/release, /api/metrics, …) delegate to the broker over an internal channel; their full success payload isn't easily extracted from the handler code, so the doc gives the body shapes and statuses but stays vague on success-response fields. Fine for now; can tighten as we use them.
  • Event kinds came from grepping json!({\"kind\": ...}) across src/. The durable vs ephemeral split is enforced in broadcast_if_relevant (src/listen_api.rs:1291).

Test plan

  • web/lib/docs-nav.ts change renders the new page in the docs sidebar under "CLI"
  • docs/reference-broker-api.md and web/content/docs/reference-broker-api.mdx stay in sync (no content drift between them)
  • The worked example actually works against a local broker (smoke-checked)

Related: #864 (view/drive subcommands — depends on this surface being discoverable).

The broker's listen API (port 3888 by default) is what the dashboard,
the CLI, the SDKs, and any custom integration call to spawn agents,
inject PTY input, subscribe to events, and shut the broker down. None
of that surface was documented anywhere outside the source.

Adds `reference-broker-api` covering:

  - base URL / port / `--api-bind` flag
  - auth model: `X-API-Key` or `Bearer`, `RELAY_BROKER_API_KEY` env,
    unauthenticated fallback when unset, `/health` exempt
  - all 22 routes grouped as Health/Config, Agent lifecycle, PTY
    interaction, Event stream — with body shapes for the ones a
    caller would actually compose by hand (`/api/spawn`, `/api/send`,
    `/api/input/{name}`)
  - `/ws` protocol: ping cadence, `?sinceSeq=` resume, `replay_gap`
    frame, full inventory of durable + ephemeral event `kind`s
  - worked end-to-end curl + websocat example to spawn, observe,
    drive, message, and release an agent
  - error envelope and common codes

Mirrors to `docs/reference-broker-api.md` per `.claude/rules/docs-sync.md`.
Adds the slug to the CLI nav group in `web/lib/docs-nav.ts`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 16, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 202ee8e1-2394-4525-b118-d32eaa6406b2

📥 Commits

Reviewing files that changed from the base of the PR and between 633aa2a and 3896309.

📒 Files selected for processing (1)
  • .claude/rules/docs-sync.md

📝 Walkthrough

Walkthrough

This PR adds comprehensive API reference documentation for the agent-relay-broker, consisting of a new MDX page detailing the HTTP and WebSocket listen API surface with authentication, endpoint specifications, event streaming, and error handling. It integrates the documentation into the CLI navigation menu and updates the documentation governance policy to establish MDX as the single source of truth.

Changes

Broker API Documentation and Governance

Layer / File(s) Summary
Broker API reference page: authentication and endpoint specifications
web/content/docs/reference-broker-api.mdx
MDX documentation introduces the broker listen API, base URL/port binding, API-key authentication with RELAY_BROKER_API_KEY environment variable and unauthenticated /health exception, structured route catalog, and detailed endpoint specifications for agent spawning (POST /api/spawn), PTY interaction (POST /api/input/{name}), and relay message injection (POST /api/send) with field aliasing and mode semantics.
Event streaming, worked example, and error documentation
web/content/docs/reference-broker-api.mdx
Event streaming API documentation covers WebSocket (/ws) and HTTP replay (/api/events/replay) with replay resumption via sinceSeq, gap frame format, durable vs ephemeral event kind enumeration. Includes end-to-end worked example sequence (start broker, spawn agent, stream events, send keystrokes, inject relay message, release agent) with shell commands, and standardized error envelope with HTTP status codes.
Navigation integration and documentation governance
web/lib/docs-nav.ts, .claude/rules/docs-sync.md
Adds "Broker HTTP / WS API" entry to CLI navigation pointing to reference-broker-api slug. Updates documentation rules to establish web/content/docs/*.mdx as single source of truth for published docs, designates top-level docs/ directory as legacy read-only, and removes prior sync/mirror requirements between MDX and markdown files.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Suggested reviewers

  • khaliqgant
  • barryollama

Poem

🐰 A broker's API, now in writing clear,
With routes and events for all to hear,
WebSocket streams and spawn commands flow,
Through documented paths we now all know! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding comprehensive documentation for the broker's HTTP and WebSocket API reference.
Description check ✅ Passed The description covers the required Summary section with detailed context, includes Why and Notes sections for additional context, and has a Test plan with specific checklist items.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch docs/broker-api-reference

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 1 additional finding.

Open in Devin Review

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/reference-broker-api.md`:
- Around line 29-32: Update the two fenced code blocks so they include a
language identifier to satisfy MD040: change the credentials/header block
containing "X-API-Key: <token> Authorization: Bearer <token>" to use a ```http
fence, and change the websocket example "ws://localhost:3888/ws?sinceSeq=12345"
to use a ```text (or ```http) fence; also apply the same change to the other
identical fenced block later in the file so all similar fences include a
language tag.

In `@web/content/docs/reference-broker-api.mdx`:
- Around line 30-33: The MDX code fences lack explicit fence languages; update
the blocks containing "X-API-Key: <token>\nAuthorization: Bearer <token>" to use
a language like http (```http) and the block with
"ws://localhost:3888/ws?sinceSeq=12345" to use a neutral text language (```text)
so the doc linter and markdown source stay in sync; locate these exact blocks in
reference-broker-api.mdx and replace the opening triple backticks with the
appropriate fenced language tokens.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3ab98c70-7630-4725-9cc4-75516389533b

📥 Commits

Reviewing files that changed from the base of the PR and between ae04694 and 633aa2a.

📒 Files selected for processing (3)
  • docs/reference-broker-api.md
  • web/content/docs/reference-broker-api.mdx
  • web/lib/docs-nav.ts

Comment thread docs/reference-broker-api.md Outdated
Comment on lines +29 to +32
```
X-API-Key: <token>
Authorization: Bearer <token>
```
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Add fence languages to satisfy markdown linting (MD040).

Both fenced blocks are missing a language identifier, which matches the lint warnings and can fail docs quality gates.

Suggested patch
-```
+```http
 X-API-Key: <token>
 Authorization: Bearer <token>

@@
- +text
ws://localhost:3888/ws?sinceSeq=12345

Also applies to: 153-155

🧰 Tools
🪛 markdownlint-cli2 (0.22.1)

[warning] 29-29: Fenced code blocks should have a language specified

(MD040, fenced-code-language)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/reference-broker-api.md` around lines 29 - 32, Update the two fenced
code blocks so they include a language identifier to satisfy MD040: change the
credentials/header block containing "X-API-Key: <token> Authorization: Bearer
<token>" to use a ```http fence, and change the websocket example
"ws://localhost:3888/ws?sinceSeq=12345" to use a ```text (or ```http) fence;
also apply the same change to the other identical fenced block later in the file
so all similar fences include a language tag.

Comment on lines +30 to +33
```
X-API-Key: <token>
Authorization: Bearer <token>
```
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Mirror file has the same missing fence-language issue.

These code fences also need explicit languages to keep the MDX page lint-clean and in sync with the markdown source.

Suggested patch
-```
+```http
 X-API-Key: <token>
 Authorization: Bearer <token>

@@
- +text
ws://localhost:3888/ws?sinceSeq=12345

Also applies to: 154-156

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@web/content/docs/reference-broker-api.mdx` around lines 30 - 33, The MDX code
fences lack explicit fence languages; update the blocks containing "X-API-Key:
<token>\nAuthorization: Bearer <token>" to use a language like http (```http)
and the block with "ws://localhost:3888/ws?sinceSeq=12345" to use a neutral text
language (```text) so the doc linter and markdown source stay in sync; locate
these exact blocks in reference-broker-api.mdx and replace the opening triple
backticks with the appropriate fenced language tokens.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 633aa2a52b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +233 to +236
Failed responses return a consistent envelope:

```json
{
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Remove claim of a single failed-response envelope

This section says all failures use the nested error.code/message/statusCode envelope, but many protected endpoints actually return {"success": false, "error": "..."} instead (for example /api/send and /api/spawn paths in src/listen_api.rs). Consumers implementing error parsing from this doc will mis-handle common non-2xx responses unless the docs describe both formats (or scope this envelope to the endpoints that really use it).

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6 issues found across 3 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="docs/reference-broker-api.md">

<violation number="1" location="docs/reference-broker-api.md:93">
P2: `cli` is documented as required, but the server defaults it to `"claude"` when omitted; only `name` is required.</violation>

<violation number="2" location="docs/reference-broker-api.md:111">
P2: The JSON curl examples omit `Content-Type: application/json`, so copied requests can fail with 415 before reaching these handlers.</violation>

<violation number="3" location="docs/reference-broker-api.md:232">
P2: The docs currently overstate error-shape consistency; multiple endpoints still return `{ success: false, error: "..." }` instead of the nested `error` envelope.</violation>
</file>

<file name="web/content/docs/reference-broker-api.mdx">

<violation number="1" location="web/content/docs/reference-broker-api.mdx:117">
P2: `/api/input/{name}` is documented as returning 404 for missing agents, but the handler currently returns 400 (`agent_not_found`).</violation>

<violation number="2" location="web/content/docs/reference-broker-api.mdx:207">
P2: POST examples with JSON bodies are missing `Content-Type: application/json`, which can cause Axum to reject the requests.</violation>

<violation number="3" location="web/content/docs/reference-broker-api.mdx:233">
P2: The docs claim a single consistent error envelope, but the API currently returns multiple error formats across endpoints.</violation>
</file>

Tip: instead of fixing issues one by one fix them all with cubic
Re-trigger cubic

Comment thread docs/reference-broker-api.md Outdated
Comment thread docs/reference-broker-api.md Outdated
Comment thread docs/reference-broker-api.md Outdated
-d '{"data":"hello\n"}'
```

Returns 404 if the agent isn't found.
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot May 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: /api/input/{name} is documented as returning 404 for missing agents, but the handler currently returns 400 (agent_not_found).

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At web/content/docs/reference-broker-api.mdx, line 117:

<comment>`/api/input/{name}` is documented as returning 404 for missing agents, but the handler currently returns 400 (`agent_not_found`).</comment>

<file context>
@@ -0,0 +1,247 @@
+  -d '{"data":"hello\n"}'
+```
+
+Returns 404 if the agent isn't found.
+
+#### `POST /api/send`
</file context>
Suggested change
Returns 404 if the agent isn't found.
Returns 400 with code `agent_not_found` if the agent isn't found.
Fix with Cubic


## Error envelope

Failed responses return a consistent envelope:
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot May 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The docs claim a single consistent error envelope, but the API currently returns multiple error formats across endpoints.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At web/content/docs/reference-broker-api.mdx, line 233:

<comment>The docs claim a single consistent error envelope, but the API currently returns multiple error formats across endpoints.</comment>

<file context>
@@ -0,0 +1,247 @@
+
+## Error envelope
+
+Failed responses return a consistent envelope:
+
+```json
</file context>
Fix with Cubic

agent-relay up --port 3888 &

# 2. Spawn Alice running claude
curl -sX POST localhost:3888/api/spawn \
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot May 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: POST examples with JSON bodies are missing Content-Type: application/json, which can cause Axum to reject the requests.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At web/content/docs/reference-broker-api.mdx, line 207:

<comment>POST examples with JSON bodies are missing `Content-Type: application/json`, which can cause Axum to reject the requests.</comment>

<file context>
@@ -0,0 +1,247 @@
+agent-relay up --port 3888 &
+
+# 2. Spawn Alice running claude
+curl -sX POST localhost:3888/api/spawn \
+  -H "X-API-Key: $KEY" \
+  -d '{"name":"Alice","cli":"claude"}'
</file context>
Fix with Cubic

`docs/*.md` is a partial mirror that's gone stale long ago (5 pages
there vs 40+ in `web/content/docs/`) and is no longer maintained.
The Next.js site under `web/` reads `web/content/docs/*.mdx`
directly — that's the source of truth.

This PR followed the old `.claude/rules/docs-sync.md` rule and
created a `docs/reference-broker-api.md` mirror. Dropping it here
along with rewriting the rule so future doc PRs aren't sent down
the same wrong path. The canonical reference at
`web/content/docs/reference-broker-api.mdx` is untouched.

Cleaning up the existing legacy pages in `docs/` is a separate task.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
willwashburn added a commit that referenced this pull request May 17, 2026
The previous version of `web/content/docs/reference-broker-api.mdx`
on this branch only contained the new Snapshots section — it
assumed the full broker API reference (which exists on PR #865's
branch as 247 lines covering auth, routes, /ws, error envelope)
was already on main. It isn't.

If this PR merges first, the published docs page would have only a
header + the snapshot section, missing the rest of the API surface.

Folds PR #865's full reference content in and slots the snapshot
section under "Routes" between "PTY interaction" and "Event stream"
(promoted from `##` to `###` to match the section's nesting). Adds
a snapshot step to the end-to-end worked example so the new
endpoint is exercised there too.

Also brings the `.claude/rules/docs-sync.md` rewrite from PR #865
onto this branch — the old "mirror to /docs/*.md" rule was what
sent the first version of this doc down the wrong path.

Either merge order now works: if PR #865 lands first there's a
trivial conflict on the duplicated base content; if this PR lands
first the doc is already complete.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@willwashburn willwashburn merged commit 18852c4 into main May 17, 2026
39 of 40 checks passed
@willwashburn willwashburn deleted the docs/broker-api-reference branch May 17, 2026 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant