needs-you

Guides

needs-you hub API (v1): wire contract

This is the exact contract implemented by hub/needs_you_hub.py. The design rationale is in adr/0007-founding-design.md; how senders should behave is in AGENT-GUIDE.md; running hubs is in HUB.md.

Conventions #

The item #

{
  "id": "01M492BJ7AYVYAA9X7WTP13JWG",      // ULID minted once by the hub that created it; same on every hub
  "key": "work:ACME-123:redo-blocked",       // sender's dedupe key; = id when the sender gave none
  "context": "work",                         // work | personal
  "kind": "needs",                           // needs | done | info
  "priority": "normal",                      // urgent | normal | low
  "title": "ACME-123: push blocked on the migration fork",
  "body": "Choose: **merge** or **bypass**.", // markdown, or null when empty
  "links": [{"label": "Jira", "url": "https://example.atlassian.net/browse/ACME-123"}],
  "steps": [                                 // the person's checklist, in order; [] when none
    {"text": "Pick **merge** or **bypass** in the PR thread", "done": false,
     "link": {"label": "PR #42", "url": "https://github.com/example/app/pull/42"}},
    {"text": "Re-run the push", "done": false}
  ],
  "question": null,                          // what an agent asked and the choices it offered; null when none
  "answer": null,                            // the person's answer to `question`: [{"selected": ["Postgres"]}], or null
  "answered_at": null,                       // when it was answered
  "answered_by": null,                       // the name of the token that answered (a person's Mac)
  "source": {"host": "my-server", "agent": "orca:redo-fixer", "project": "app"}, // optional "event" too (below)
  "status": "open",                          // open | resolved | dismissed
  "created_at": "2026-10-06T17:04:05.123Z",
  "updated_at": "2026-10-06T18:04:05.456Z",  // moves on EVERY write (re-post, resolve, patch)
  "content_updated_at": "2026-10-06T17:04:05.123Z", // moves only when title, body, priority, steps or question change
  "seen_at": null,
  "expires_at": null,                        // done/info default to created/re-posted + 24 h
  "superseded_by": null,                     // set when this item lost a same-key merge (see Replication)
  "tombstone": false                         // true: closed, and its text is gone (Housekeeping)
}

Change detection: content_updated_at #

content_updated_at is the re-animation signal. It is set at creation and changes only when a re-post changes title, body, priority, steps (any step's text, link or done, or the list itself) or question (any part of it, or adding or removing it). Re-posting identical content, changing only links/source/kind/context, resolving, dismissing and setting seen_at all move updated_at but never content_updated_at.

A client animates an item when it is new to it, or when its content_updated_at is later than the value it last saw. POST /v1/items responses also carry two response-only booleans, created and changed (see below); they are not stored and are not part of the item.

Expiry #

expires_at is a deadline, not a write. Once it passes, the item is reported with "status": "resolved" and is no longer open: it leaves status=open lists, stops counting toward the volume guard, and a new POST with its key creates a new item. The stored record is not rewritten, so updated_at does not move when an item expires (see since below for how pollers still learn about it).

Endpoints #

GET /v1/health #

No auth needed. Always 200 while the hub is up:

{"ok": true, "hub_id": "hub-a", "version": "1.0.0", "api": "v1", "time": "2026-10-06T17:04:05.123Z",
 "stats": {"db_bytes": 98304, "items": 41, "open_items": 3, "live_invites": 1, "outbox_pending": 0}}

stats.db_bytes is the database plus its WAL file. With a valid token, stats.outbox also gives the pending outbox rows per peer URL.

If a bearer token is sent it is checked but never causes an error: a valid token adds "token": {"name": "...", "role": "sender"} and a "peers" array (per-peer url, outbox_pending, last_push_ok, last_pull_ok, last_error, and skipped_push / skipped_pull / last_skipped: how many replicated item records that peer couldn't read from us, and we from it, and the last one; blocked: a token or invite record one side can't read, which holds replication in that direction ("push ..." / "pull ..."), else null; all absent from hubs up to 0.1.2; hub_id, name, source and added_at as in GET /v1/peers, absent from hubs up to 0.2.1); an unknown or revoked token adds "token": null and "token_error": "unknown or revoked token".

POST /v1/items (sender) #

Create an item, or update the open item with the same key.

FieldTypeRuleDefault
keystring≤ 200 chars, only A-Z a-z 0-9 . _ : - / @ # + =; "" is a 400 (leave it out, or send null, for no key)the new item's id (so no dedupe)
titlestringrequired, 1–100 chars after trimming, no control characters (see below)
bodystring≤ 2,000 chars, markdown (the Mac renders bold, italic, code, strikethrough, links, - lists and # headings as bold lines; the rest as plain text); newlines (\n, \r) and tabs allowedempty
contextstringwork or personal (case-insensitive)work
kindstringneeds, done or info (case-insensitive)needs
prioritystringurgent, normal or low (case-insensitive)normal
linksarray≤ 6 of {"label": 1–80 chars, "url": ≤ 2,000 chars}[]
stepsarray≤ 10 of {"text", "link", "done"}, see below[]
questionobjectwhat an agent asked, see belownull
sourceobjectoptional host, agent, project, each ≤ 100 chars, and event (below){}
expires_attimestampany accepted timestampdone/info: now + 24 h (the hub's default_expiry_hours, see Server hubs); needs: none
statusrejected whenever the key is present, even "status": null (use resolve or PATCH)

An optional field sent as null counts as left out (it gets its default).

source.event is optional: what happened, as a short slug, so a person's alert rules can treat "the agent asked me something" differently from "the agent finished". It matches ^[a-z][a-z0-9_-]{0,31}$ after trimming and lowercasing (anything else is 400 invalid with field source.event). These values have a meaning; others are allowed and shown as sent:

eventThe sender is telling the person
questionThe agent asked them something and waits for the answer
approvalThe agent wants permission (to run a command, edit a file, fetch a URL) or approval of a plan
finishedThe agent's turn ended and it waits for the next message
failedThe turn errored, hit a rate limit, or needs a sign-in
contextThe agent's context window is nearly full

The hub stores it with the rest of source and never acts on it. Unknown source keys are dropped, so hubs that predate event drop it and it is safe to send to any hub. A replicated record with an event this hub would refuse keeps the item and drops the event.

Link URLs must use one of these schemes (case-insensitive): https, slack, vscode, cursor, figma, msteams, discord, linear. Anything else, including http, jira, file, javascript and orca, is a 400. Text strings are trimmed.

Before the scheme is even read, every link URL (the app actions below included) must fit a strict, parser-neutral subset of RFC 3986, checked on the raw string by the hub and the Mac app with the same regex: plain ASCII; <scheme>:// (so https:host and vscode:/file/x are refused); no userinfo @ before the host; no backslash, whitespace, quotes, [ ] < > ^ ` { | } or control characters anywhere; % only as %XX; at most one #. An https link also needs a host (https:///x and https://:443/ are refused). Percent-encode anything else.

vscode and cursor reach every installed extension's URI handler, so only these shapes are accepted (the scheme is case-insensitive, the rest is matched exactly, lowercase):

ShapeOpens
vscode://file/<abs path>[:line[:col]]A file or folder on the Mac. No query, no fragment, no // after file
vscode://vscode-remote/ssh-remote+<host>[/<abs path>]A Remote-SSH window. <host> is an ~/.ssh/config alias or name, optionally user@: letters, digits, ., _, -, starting with a letter or digit, no %; an all-hex value starting 7b (a hex-encoded JSON host spec) is refused
vscode://vscode-remote/tunnel+<name>[/<abs path>]A Remote Tunnel window (tunnels belong to the user's own account); same name rule
vscode://anthropic.claude-code/open?session=<id>The Claude Code extension's tab for that session. Exactly one parameter, session, 8–64 letters, digits or -

The same with cursor://. Paths take RFC 3986 path characters and %XX escapes, but no escape that decoding would turn into structure or into something to decode again (%2F, %3F, %23, %2E, %25, %5C), no escaped control character (%00–%1F, %7F), and no . or .. segment. Everything else is a 400: other authorities (vscode://<publisher.extension>/…, vscode://settings/…), wsl+, dev-container+ and other remote kinds, userinfo, ports, queries or fragments on file and remote links, other paths or parameters on the Claude link. orca:// was dropped: Orca's only link (orca://skills/share/<id>) imports a skill, which a card should never ask for, and the Orca jump is the app action below.

One exception: the Mac app's own scheme, for a fixed set of app actions (the card's "go there" button: Orca, the terminal, or the app the session runs in). The hub accepts needsyou://<host>/<path>?<query> only for these <host>/<path> pairs (case-insensitive), and nothing else under needsyou://:

ActionLinkWhat the app does
orca/terminalneedsyou://orca/terminal?handle=term_<uuid>[&environment=<name>]orca terminal switch. The handle is term_ plus 8–64 lowercase hex or -; the environment name is letters, digits, space, ., _, -, ≤ 64
terminal/focusneedsyou://terminal/focus?app=<app>&<id>Focuses one terminal tab or pane on the Mac (below)
app/activateneedsyou://app/activate?bundle=<id>Brings that app forward when the sender knows the app the session runs in but no window jump exists for it (kitty, Warp, Zed, a JetBrains IDE). Exactly one parameter, bundle, matched exactly against a fixed list of terminals and editors built into the app (below); only an app that is already running comes forward, it is never launched, and Needs You itself is never on the list

terminal/focus takes app and exactly the parameters for that app, each at most once:

appParametersMac side
weztermpane=<n> (1–6 digits, $WEZTERM_PANE)wezterm cli activate-pane --pane-id <n>, then WezTerm comes forward
tmuxpane=<n> (the pane id %<n> without the %, 1–6 digits) or target=<session>:<window>.<pane> (session: letters, digits, _, -, ≤ 64, not starting with -; window and pane: 1–4 digits); optional host=<iterm|terminal|wezterm|ghostty>, the terminal tmux runs intmux select-window -t / select-pane -t on the default tmux server, then the host terminal comes forward
itermsession=<UUID> (the part of $ITERM_SESSION_ID after :) or tty=/dev/ttys<n>AppleScript (opt-in on the Mac): selects that session's window, tab and session
terminaltty=/dev/ttys<n> (1–4 digits)AppleScript (opt-in on the Mac): selects the Terminal.app tab with that tty
ghosttynoneGhostty comes forward (no tab selection yet)

app/activate brings forward only these bundle ids (AppActivation.allowedApps in the Mac app, mirrored by the Claude Code hook; any other id, a different case, a second parameter or an extra character does nothing):

Opened from outside the panel (a web page, open), an app/activate link does nothing.

The hub checks only the action prefix; the app parses every link into a typed value and refuses anything else (an unknown parameter, a repeated one, a value outside its pattern, a value starting with -, user, port or fragment), and then does nothing. It runs only fixed executables from fixed paths with argument arrays, never a shell, and AppleScript only as fixed handlers called with the validated value as a typed parameter. Worst case for a sender: the Mac shows a different terminal tab, or brings a different listed app forward. A hub older than an action rejects the item with 400; senders that add one (the Claude Code hook) retry without their needsyou:// links.

Control characters are refused in every text field (title, body except newline and tab, link labels, source fields): C0, DEL, C1 (U+0080–U+009F), and the bidi embedding, override and isolate controls U+202A–U+202E and U+2066–U+2069, which can make text read as something it isn't. Ordinary right-to-left text, marks (U+200E/U+200F) and joiners (U+200C/U+200D) are fine. On POST /v1/items, the one-line fields (every text field except body and a question's text) also refuse U+2028 LINE SEPARATOR and U+2029 PARAGRAPH SEPARATOR, which break a line as \n does (400 invalid, field names it; ADR 0010). Link URLs may not contain whitespace or invisible format characters at all (percent-encode a space as %20). Item records that arrive by replication keep only links that pass these rules. The same goes for steps: their text and link fields follow these rules on POST, and a replicated step whose link fails keeps its text and loses the link.

A request body whose JSON has an unpaired UTF-16 surrogate escape anywhere ("\ud800", as a file name with a byte that isn't UTF-8 can produce) is a 400 invalid: it isn't text, can't be stored, and clients' JSON decoders refuse it. On /v1/replicate that applies per record: such an item is skipped, and such a token or invite record fails the batch closed.

Steps are the things the person has to do, in order. Each step is an object:

FieldTypeRuleDefault
textstringrequired, 1–200 chars after trimming, no newlines or control characters; inline markdown
linkobjectoptional {"label", "url"}, validated exactly like an entry of links (same limits and schemes)none
donebooleantrue or false (anything else is a 400)false

Unknown step fields are ignored. Errors name the step, for example steps[2].link.url. The hub returns every step as {"text", "done"} plus "link" when it has one; an item without steps has "steps": []. Steps are the sender's view: there is no endpoint to tick one, and the Mac app keeps the person's ticks locally (it offers Done once every step is ticked).

Question (ADR 0009) is what an agent asked the person and the choices it offered, so a client can show them as choices rather than as a checklist. By default it is read-only: the person answers in the agent. With "answerable": true the sender is waiting for an answer from a client too: the person's Mac posts the options they click, or the words they type where the sender takes them (allow_other) (POST /v1/items/{id}/answer), and the sender reads them back (GET /v1/items/answer).

"question": {
  "id": "toolu_01ABC",            // optional, ≤ 200 chars: the agent's own id for the request
  "items": [{                     // 1–4 questions, in order
    "header": "Database",         // optional, ≤ 30 chars, one line
    "text": "Which database should we use?",  // required, 1–500 chars; newlines allowed
    "options": [                  // 0–8; none for a free-text question
      {"label": "Postgres", "description": "Durable"}  // label 1–80 chars, description ≤ 200, one line each
    ],
    "multi_select": false,        // true: the agent takes several options
    "allow_other": false          // optional: true when the sender also takes the person's own words
  }],
  "answerable": false,            // optional: true when the sender waits for an answer (every item needs options or allow_other)
  "expires_at": "2026-10-08T17:04:05.000Z"  // optional: the sender stops waiting then; no answer is taken after it
}

Text follows the same rules as every field (trimmed, no control or bidi characters; only text may hold newlines). Unknown fields are ignored. Errors name the part, for example question.items[1].options[0].label. The hub returns it normalised: header and description as "" when left out, options as [], multi_select as a boolean, id only when given, allow_other only when true, answerable as a boolean, expires_at only when given (in the hub's timestamp form); an item without one has "question": null. allow_other: true says the sender takes a free-text answer to that question ("Other" in Claude Code and opencode): with options, in place of a single choice or next to a multi-select's picks; without options, as the only answer (a free-text question). Set it only when the agent really takes typed words for that question. An answerable question with an item that has neither options nor allow_other: true is refused (400, question.answerable): there would be nothing to answer with. So is one with two options of the same label within a question (400, the second one's question.items[i].options[j].label): an answer carries labels only, so the two couldn't be told apart. Labels are compared as stored (trimmed, case-sensitive, the way the hooks compare them): "Yes" and " Yes " are one label, "Yes" and "yes" two. A read-only question may repeat a label; different questions may share labels. Senders keep the question's text in body as well, for clients and views that don't show the field. An answer belongs to the question it answers: a re-post that changes the question (any part of it, expires_at included), or a re-post by a different token, clears the item's answer (an answer is only ever read back by the token that asked). A re-post by the same token with the same question keeps it, even when it changes title, body or priority (that moves content_updated_at, so an answer the person started to the earlier version gets 409 question_changed, but one already taken stands).

Semantics:

  1. If an item with this key is open (and not expired), it is updated **in place, keeping its id**. A re-post is the sender's full current view: title, body, context, kind, priority, links, steps, question, source and expires_at are all replaced (omitted optional fields become empty/default). updated_at always moves (strictly forward, even within one millisecond). content_updated_at moves only if title, body, priority, steps or question changed. The item's token becomes the re-posting token. The volume guard does not apply to updates.
  2. Otherwise a new item is created with a new id. If the token already owns 60 open, unexpired items, the hub returns 429 too_many_open instead.
  3. Every POST /v1/items counts toward a per-token rate, re-posts of an open key included (post_rate_limit, default 120, per post_rate_window_seconds, default 60; per hub, in memory; 0 turns it off). Past it the hub answers 429 rate_limited with a Retry-After header (seconds, also as "retry_after" in the error body) and changes nothing, so a sender stuck in a loop can't keep moving its card to the top of the panel. Resolves don't count (ADR 0010).

Response: 201 when created, 200 when an existing item was updated. The body is the item plus "created": true|false and "changed": true|false (changed is true on create and when title/body/priority/steps/question changed).

POST /v1/items/resolve (sender) #

Body: exactly one of {"key": "..."} or {"id": "..."}. Leading and trailing whitespace is trimmed, as POST /v1/items trims the key it stores.

Closes the open item with that key (or that id) as resolved. Always 200 and idempotent:

{"resolved": 1, "items": [ { ...the resolved item... } ]}

If nothing is open with that key/id (already resolved, dismissed, expired, or never existed), the response is {"resolved": 0, "items": []}. A body with neither or both of key/id is a 400, and so is a key or id that isn't a string ("id/key must be a string"). A key is checked as POST /v1/items checks it (at most 200 characters, only `A-Z a-z 0-9 . _ : - / @ #

Resolve doesn't check who posted the item. This is intended:

PATCH /v1/items/{id} (reader) #

Body: any of

At least one is required. Moves updated_at, never content_updated_at. Returns 200 with the item, or 404 for an unknown id. Patching an already-closed item is allowed.

POST /v1/items/{id}/answer (reader) #

The person's answer to the item's question, from an explicit click on one of its options, or the words they typed after an explicit click on "Other…" (reader or owner token; sender tokens get 403, so one agent machine can't answer another agent's question). Typed text is taken only from an owner token (the person's own Mac): the agent reads those words as the person's, so a reader token (another Mac, a dashboard) answers by picking options only, and an answer with text from it gets 403 forbidden.

{"question_id": "toolu_01ABC", "content_updated_at": "2026-10-06T17:04:05.123Z",
 "answers": [{"selected": ["Postgres"]}, {"selected": ["Auth", "Export"]}]}
{"question_id": "toolu_01ABC", "content_updated_at": "2026-10-06T17:04:05.123Z",
 "answers": [{"selected": [], "text": "MySQL, the team knows it"}]}

The hub stores text as given (trimmed), never redacts or rewrites it, and never logs it. The answer is returned normalised: {"selected": [...]} plus "text" only when given.

A question_id that isn't a string or null, or a content_updated_at that isn't a timestamp, is 400 invalid. Then the hub takes the answer only if, in this order: the item exists (404 not_found), is open and unexpired (409 not_open), has a question with answerable: true (409 not_answerable) whose expires_at hasn't passed (409 question_expired), question_id and content_updated_at match the item's (409 question_changed: the question changed under the person), and it has no answer yet (409 already_answered: the first answer wins). Then answers is checked (400 invalid, with field such as answers[0].selected[1]), then a text from a token that isn't an owner (403 forbidden; nothing is taken). A token may send at most 30 answer requests a minute (answer_rate_limit per answer_rate_window_seconds, see Server hubs); past that, 429 rate_limited.

On success the item's answer, answered_at and answered_by (the token's name) are set, updated_at moves (content_updated_at doesn't) and the item replicates like any write. The item stays open: the sender resolves it once it has acted on the answer. Returns 200 with the item.

GET /v1/items/answer?key=<key> (sender) #

How a sender reads back the answer to its own question: only the token that posted the item (its last re-post) may read it; for any other token, or no item with that key, the hub says 404 not_found. A sender still can't read anything else of the inbox. The item is the open item with that key, else the last one updated.

A token may read at most 120 times a minute (answer_read_rate_limit) and hold at most 4 long polls at once (answer_waits_per_token); past either, 429 rate_limited at once. A waiting request is not kept when the hub runs out of connections (it gives way like any request older than request_read_seconds), so long polls can't starve the hub.

StatusBodyWhen
200{"id", "key", "status", "question_id", "answers", "answered_at", "answered_by"}The item has an answer (also after it was resolved, until its text is purged)
204noneNo answer yet when wait ran out: ask again
404not_foundNo such item for this token (yet: a post still in the sender's outbox)
409not_open, not_answerable, question_expiredNo answer will come: the item closed, has no answerable question, or the question expired

answers has the shape posted to POST /v1/items/{id}/answer, text included. Check question_id: once nothing is open under the key, the answer read back is the last item's, which may be an earlier question's. needs-you answer-wait wraps this (--question-id keeps waiting past another question's answer; see the agent guide).

GET /v1/items (reader) #

Query parameters:

ParamMeaning
statusopen (default), resolved, dismissed or all. Ignored when cursor or since is given.
cursorA cursor: the next from this hub's previous response. Opaque; send it back unchanged.
sinceA cursor for clients that don't know next: the server_time from this hub's previous response.
limitMax items, default 500, max 2,000 (a value outside 1–2,000 is clamped; not an integer is a 400).

Response:

{"items": [ ... ], "server_time": "2026-10-06T17:04:05.122Z", "next": "01M492BJ7A...J.812.1791306245123",
 "hub_id": "hub-a", "more": false}

Without cursor or since: the full current set for status. status=open returns every item that is open and not expired. resolved includes expired items (shown as resolved). When the set is larger than limit the response has more: true, and its next continues it: a cursor poll with it returns the rest of the set as changes (in any status, like any cursor poll, so the rest comes with whatever changed since), paged by limit like any cursor poll. Send that one without since: a since fallback can't continue a full poll, so a cursor the hub can no longer read is then a 400 and the client starts over with a full poll. Hubs up to 0.1.3 gave a cut-short full poll a next that skipped the rest; a client that has to work with them sends a larger limit (up to 2,000) and treats a full poll with more as incomplete.

With cursor or since: every item that changed on this hub after the cursor, **in any status**, plus every item whose expires_at passed since then (reported as resolved). This is how a poller learns about resolves, dismissals and expiry, not only new items. "Changed on this hub" means this hub stored a new version of it after the cursor: a local write, or a replicated version arriving from a peer. That is this hub's own order, not the item's updated_at, so an item that reaches this hub late by replication is still delivered even if its updated_at is older than the cursor.

The polling loop:

  1. First poll (or after any doubt): GET /v1/items?status=open. Replace the local set. If it has more, the set continues in the next polls (step 3): replace only once it's all in, or merge without dropping anything.
  2. Keep next and server_time from the response. Next poll: GET /v1/items?cursor=<next>&since=<server_time> (adding status=open is harmless; it is ignored). Upsert every returned item by id; drop it from the open view if its status isn't open. Store the new next and server_time.
  3. If more is true, poll again immediately the same way.
  4. Both are cursors for that hub only. If hub_id in a response differs from the hub that issued the cursor (failover), discard them and go back to step 1.

next (hubs after 0.1.2) is <database epoch>.<seq>.<expiry position>: the hub's per-write sequence number, so a page always moves past what it returned and never holds more than limit items, however many writes share a millisecond. Clients must not parse it. A hub answers a cursor from another database (it was replaced, or restored from a backup older than the cursor) or one it can't read by serving since instead when that was sent, and with 400 invalid ("field": "cursor") when it wasn't; drop the cursor and do a full poll then. Sending both is also what keeps an older hub, which ignores cursor, working. next is in every response except a since-only page with more: true; keep paging with since then, and the last page carries a next.

server_time is the hub's clock 1 ms before it ran the query, so a write landing in the same millisecond is never missed; when a since page has more, it is instead the time of the page's last event, and the page holds every event up to that millisecond (so it can exceed limit when more than limit writes or expiries share it; hubs up to 0.1.2 served such a page repeated forever). The price is that an item can occasionally be delivered twice; clients must treat responses as idempotent upserts by id. With cursor, changes come back in the hub's write order, then expiries; with since, in time order.

GET /v1/items/{id} (reader) #

The item, or 404.

GET /v1/stream (reader, optional) #

Server-sent events. Each changed item (same rule as since: any version this hub stores) is sent as

id: 1234
event: item
data: {...item JSON...}

A comment line (: ping) is sent about every 15 s. Reconnect with Last-Event-ID: <id> (or ?after=<id>) to resume; without it the stream starts at "now". The event id is a per-hub sequence number, so after failover to another hub, reconnect without Last-Event-ID and do a full poll. Expiry is not an event; clients compare expires_at with the clock themselves.

Status records (usage meters) #

A status is a small keyed record that is not an item (ADR 0011): it never counts, never makes a card, never notifies, and never appears in GET /v1/items, /v1/stream or /v1/items/answer, so a client that predates statuses never sees one. Today the Mac app shows usage statuses as meters (session and weekly limits per provider and account); progress statuses are accepted and listed but not shown yet. Producers are hooks, status line helpers and pollers (needs-you status set), not agents.

{
  "id": "st_3f0c9b1e2a7d4c58b6e1f0a9d2c4b7e1", // opaque, the same on every hub (derived from the token and key)
  "key": "usage:claude",                    // the sender's key: the item key grammar, per token
  "type": "usage",                          // usage | progress
  "label": "Claude",                        // <= 60 chars, one line, plain text ("" allowed for usage)
  "state": null,                            // progress only: working | waiting | idle | done | failed
  "progress": null,                         // progress only: 0-100, or null for "no percentage"
  "detail": "",                             // <= 120 chars, one line, plain text
  "usage": {                                // usage only (null for progress)
    "provider": "claude",                   // [a-z0-9-]{1,20}
    "account": "",                          // a local label or hash, [A-Za-z0-9._-]{0,40}: never an email
    "windows": [                            // 1-4 windows
      {"name": "5h", "used_pct": 51, "resets_at": "2026-10-08T14:00:00.000Z"},
      {"name": "7d", "used_pct": 41.5, "resets_at": null}
    ]
  },
  "source": {"host": "devbox", "agent": "claude-code"},  // as on items
  "created_at": "2026-10-08T09:00:00.000Z",
  "updated_at": "2026-10-08T09:41:10.000Z",
  "expires_at": "2026-10-14T09:00:00.000Z"
}

Window name is [a-z0-9-]{1,20}; the producers send 5h (the session window) and 7d (the weekly one), and clients show other names as they are. Window names within one status must differ (400, field usage.windows). used_pct is a number from 0 to 100 (kept to one decimal). resets_at is a timestamp or null; a client shows a window whose resets_at has passed as reset (0 %), since the numbers are only as fresh as the last write.

PUT /v1/status/<key> (sender) #

Sets the status <key> of the calling token (a key is per token: another token's PUT to the same key is a different record). The body is the record above without id, key or the times, plus a required expires_at:

FieldRule
typeusage or progress (required)
labelstring <= 60, one line; required and non-empty for progress
stateprogress: one of working, waiting, idle, done, failed (default working); ignored for usage
progressprogress: integer 0-100 or null; ignored for usage
detailstring <= 120, one line, optional
usageusage: required, as above; ignored for progress
sourceas on items, optional
expires_atrequired, in the future: at most now + 1 h for progress, now + 8 days for usage

Text fields refuse control, bidi and line-separator characters like item text, and token-shaped text (400 secret_in_text, with field), as do the key and the source fields (a status is listed and replicated whole). @ in account is refused (no emails).

Response 200 with the stored record. A write to an existing key replaces it entirely.

Limits: at most 20 live (unexpired) statuses per token and 64 per hub (429 too_many_status), and one write per key every 10 s (429 too_fast, with Retry-After and retry_after). Sets and clears together have their own per-token rate, the same size as the post rate (post_rate_limit per post_rate_window_seconds): past it, 429 rate_limited with Retry-After. Statuses never count toward the item volume guard or the item post rate limit.

DELETE /v1/status/<key> (sender) #

Clears the calling token's status <key>: 200 {"ok": true, "cleared": true}, or "cleared": false when there was none (or it had expired). A clear is a write (it sets expires_at to now and replicates); it is never refused by too_fast, but it counts toward the status write rate (429 rate_limited).

GET /v1/status (reader) #

{"statuses": [ ...status records, newest updated_at first... ], "server_time": "2026-10-08T09:41:12.000Z"}

Every unexpired status on this hub, except those of a revoked token. The response carries an ETag; a request with that value in If-None-Match gets 304 with no body while nothing changed. A hub that predates statuses answers 404; clients treat that as "no statuses".

Expiry is computed, never written, as for items. Housekeeping deletes a status an hour after its expires_at; nothing is kept for history.

Invites #

An invite is a link that sets up one or more machines. Redeeming it mints a **new token per machine**, so one link with uses: 5 can set up five servers, each with its own revocable token. Codes are nyi_ plus 192 random bits (URL-safe base64); hubs store only their sha256.

POST /v1/invites (owner) #

{"name": "my-server", "role": "sender", "uses": 1, "ttl_hours": 72}
FieldRuleDefault
namerequired, 1–40 chars of letters, digits, ., _, @, -, starting with a letter or digit
rolesender, reader, owner, or peer (another hub, below)sender
usesinteger 1–100; exactly 1 for peer1
ttl_hoursnumber, more than 0 and at most 2160 (90 days); at most 24 for peer72; 1 for peer

Response 201:

{"code": "nyi_...", "join_url": "http://hub-a.example.ts.net:8765/join/nyi_...",
 "mac_url": "needsyou://connect?hub=http%3A%2F%2Fhub-a.example.ts.net%3A8765&code=nyi_...",
 "expires_at": "2026-10-09T17:04:05.123Z",
 "id": "01M...", "name": "my-server", "role": "sender", "uses": 1,
 "install_command": "curl -fsSL http://hub-a.example.ts.net:8765/join/nyi_.../install.sh | bash -s -- --yes --claude-hooks user --skill --alerts",
 "install_steps": ["cd", "umask 077", "curl -fsSLo needs-you-install.sh http://hub-a.example.ts.net:8765/join/nyi_.../install.sh",
                   "bash needs-you-install.sh --yes --claude-hooks user --skill --alerts"],
 "agent_prompt": "Set up needs-you alerts on this machine: read http://hub-a.example.ts.net:8765/join/nyi_... and follow it. If this machine runs Claude Code, use --claude-hooks user --skill --alerts. If it runs OpenAI Codex CLI, add --codex-hooks user; Gemini CLI, add --gemini-hooks user; opencode, add --opencode-plugin; GitHub Copilot CLI, add --copilot-hooks user; Kimi Code, add --kimi-hooks user; Grok Build, add --grok-hooks user; Cursor, add --cursor-hooks user; Cline, add --cline-hooks user; Aider, add --aider. Only if I ask for them: --mcp <agents> registers the needs-you MCP server (claude, codex, gemini, opencode, copilot, cursor) and --agent-instructions <agents> adds the posting rules to their instruction files (codex, gemini, opencode). Daily updates are on by default (the 5-minute flush runs needs-you update); add --no-auto-update only if I ask. Then run ~/.local/bin/needs-you doctor and, for each WARN or FAIL line, run the next step printed under it, or tell me if it needs me. If the installer says the link is unknown, expired or used up, ask me for a new one."}

agent_prompt, the curl … | bash install_command and install_steps are only present for sender invites (a peer invite has its own install_command and install_steps, below). install_steps is the same install as plain lines, to run one after another in one shell: no pipe, quote, bracket, $ or ~, so a console that types a paste key by key (a cloud provider's browser console) can't break them. The installer is saved as needs-you-install.sh, private to the user (umask 077, which stays set in that shell), and deletes that file as soon as it starts, since it holds the invite code. All three set up Claude Code alerts (--claude-hooks user --skill --alerts); the join page lists the other options. The prompt ends by having the agent run needs-you doctor and act on, or relay, the next step under each WARN or FAIL line. Its wording isn't a contract: show it as sent. The URLs use the hub's public_url (config public_url / --public-url; without it, the first bind address). A peer invite's response has no mac_url or agent_prompt. What to run on the server comes in two forms. install_steps, for a person at a terminal: ["cd", "curl -fsSLo install-hub.sh https://github.com/tayharris/needs-you/releases/download/v<hub version>/install-hub.sh", "sudo bash install-hub.sh --join -"], plain lines with no code in them; the installer then asks Paste the peer invite link: and reads join_url from the terminal. install_command, for a script or an agent: (curl -fsSL https://github.com/tayharris/needs-you/releases/download/v<hub version>/install-hub.sh && echo '<join_url>') | sudo bash -s -- --join -. The installer and the code it installs come from that GitHub release, never from a hub; the hub supplies only the link. Either way the link reaches the installer on its stdin, so the code is in no command line (not in ps, not in sudo's log). install_steps is new in 0.5.2; clients that build their own lines (the Mac app) keep working against older hubs.

GET /v1/invites (owner) #

{"invites": [{"id", "name", "role", "uses", "left", "created_at", "expires_at"}, ...]}: every invite that is neither revoked nor expired, including used-up ones (left: 0), whose installer still re-runs and uninstalls until they expire. Never includes codes. peer invites are listed only by the hub that made them.

DELETE /v1/invites/<id or name> (owner) #

Revokes the invite: it can't be redeemed, and its /join page and installer stop working. Tokens it already minted stay valid (revoke those separately). Response 200 {"revoked": [{"id", "name"}]}; 404 not_found if no unrevoked invite has that id or name.

Tokens #

GET /v1/tokens (owner) #

{"tokens": [{"id": "01M...", "name": "servers-devbox", "role": "sender",
             "created_at": "2026-10-06T17:04:05.123Z", "open_items": 2, "current": false,
             "client": {"cli": "0.1.1", "hook": "0.1.1", "skill": "0.1.1", "orca": "none"},
             "last_seen_at": "2026-10-07T09:12:00.000Z", "update_requested_at": null}]}

Active tokens only. current marks the token making the request. Never includes secrets or hashes. client is what the token's machine last reported in X-Needs-You-Client ({} when nothing yet) and last_seen_at when this hub last saw a sender call or token-checked /v1/health from it (null when never). Both are per hub: a client merging several hubs takes the newest last_seen_at per token id. Hubs before 0.1.2 omit both fields. update_requested_at is when an update request for the token was made on this hub, or null (absent from hubs that predate it).

POST /v1/tokens/<id or name>/request-update (owner) #

Asks that sender machine to update: its next requests to this hub get "update_requested": true. No body. Asking again refreshes the time. Response 200:

{"id": "01M...", "name": "servers-devbox", "update_requested_at": "2026-10-07T09:12:00.000Z"}

404 not_found if no active token has that id or name; 400 invalid for a reader or owner token (they don't run the CLI).

The hub records the CLI version the machine last reported with the request and **clears the request by itself** when a request from that token reports a different cli version in X-Needs-You-Client, or one at least the hub's own version (what its /dl serves). A request made before the machine ever reported a version takes its first report as the baseline. A request without the header changes nothing. Revoking the token drops its request.

Per hub, not replicated (like client and last_seen_at): the Mac app sends the request to every owner hub it has. A machine that only talks to another hub doesn't see it there.

DELETE /v1/tokens/<id or name>/request-update (owner) #

Withdraws the request. Idempotent: 200 {"id", "name", "update_requested_at": null} whether or not one was pending; 404 not_found if no active token has that id or name.

DELETE /v1/tokens/<id or name> (owner) #

Revokes the token on this hub (and, by replication, on its peers). Its open items stay until they are resolved, dismissed or expire. Response 200 {"revoked": [{"id", "name"}]}; 404 not_found if no active token has that id or name; 400 invalid for the token making the request (use another owner token, or needs_you_admin.py).

POST /v1/invites/redeem (no token) #

{"code": "nyi_...", "host": "build-1"}

Response 200:

{"token": "ny_...", "role": "sender", "name": "my-server-build-1",
 "hub_urls": ["http://hub-a.example.ts.net:8765", "http://hub-b.example.ts.net:8765"],
 "hub_id": "hub-a"}

Peer invites #

A peer invite pairs another hub with this one (ADR 0012): the way an always-on server joins the Mac's own hub, or one server another, without copying a secret. It has one use, lives 1 hour by default (at most 24), and stays on the hub that made it: it is never replicated (it names this hub). Redeeming it mints no token. The joining hub (needs-you-admin peer join - with the join_url on stdin, which install-hub.sh --join - runs) sends:

{"code": "nyi_...", "host": "hub-b",
 "peer": {"url": "http://hub-b.example.ts.net:8765", "hub_id": "hub-b", "schema": 11}}

The code is checked first (404 as above, counted). Then, without spending the use: a malformed peer is a 400 (field names the part); the inviting hub's own hub_id or URL is 409 self; a URL already in its config peers is 409 conflict (that peer uses the shared secret), and so is a URL or hub_id that already belongs to a link made by an earlier peer invite (remove that peer first, then redeem again); a schema below the inviting hub's is 409 peer_outdated (the older hub would drop fields the newer one writes, such as question and answer: upgrade it first). A peer object sent with a sender, reader or owner invite is a 400 ("field": "peer").

Response 200:

{"role": "peer", "name": "hub-b", "peer_secret": "nyp_...", "link_id": "pl_...",
 "hub_id": "hub-a", "hub_url": "http://hub-a.example.ts.net:8765", "schema": 11, "version": "0.5.1",
 "hub_urls": ["http://hub-a.example.ts.net:8765", "http://hub-b.example.ts.net:8765"]}

peer_secret (nyp_ plus 256 random bits) is this pair's own replication secret, and link_id (pl_ plus 96 random bits, not a secret) names the pair: every replication request over the link sends it in X-Needs-You-Peer-Link with the secret (below). The inviting hub stores peer.url, peer.hub_id, the invite's name, the link id and the secret, and starts replicating with that URL at once; the joining hub stores hub_url with the same link id and secret, and refuses a hub_url that is neither https nor on the tailnet or loopback (the rule for peer.url above), or one whose URL or hub_id belongs to a different link it already has. To pair the same hub again, or under a new URL, remove the old link first (DELETE /v1/peers/<hub id>, or needs-you-admin peer remove). The secret is sent only in this response: never replicated, listed, or logged.

GET /join/<code> for a peer invite says what to run on the server; GET /join/<code>/install.sh is the failing script (200, exit 1, X-Needs-You-Invite: unusable (HTTP 400)).

Peers #

A hub's peers are the config's peers (they use the shared peer_secret) plus the links made by peer invites (each with its own secret, kept in the database). A running hub picks up links the admin tool adds or removes within 5 seconds.

GET /v1/peers (owner) #

{"peers": [{"url": "http://hub-b.example.ts.net:8765", "hub_id": "hub-b", "name": "hub-b",
            "source": "invite", "added_at": "2026-10-08T17:04:05.123Z",
            "outbox_pending": 0, "last_push_ok": "2026-10-08T17:05:00.000Z",
            "last_pull_ok": "2026-10-08T17:05:01.000Z", "last_error": null,
            "skipped_push": 0, "skipped_pull": 0, "last_skipped": null, "blocked": null}]}

source is config (from hub.json or --peer; hub_id, name and added_at are null) or invite. The status fields are those of /v1/health's peers. Never includes a secret.

DELETE /v1/peers/<hub_id, URL or name> (owner) #

Stops replicating with a peer a peer invite added (the URL percent-encoded): its link and secret are deleted, with its queued outbox rows and replication state. Requests from that hub are refused from then on (401, or 404 once this hub has no secret at all); remove this hub on the other side too. Response 200 {"removed": [{"url", "hub_id", "name"}]}; 404 not_found if no link matches; 400 invalid for a config peer (edit hub.json and restart).

GET /join/<code> (no token) #

text/markdown, written for an agent and readable by a person: what needs-you is, the one-line install command, the installer's options, and the posting rules. For a reader or owner invite it says to open the needsyou:// link on the Mac instead. Viewing the page doesn't spend a use. A used-up invite's page is still served, with a note that it can't set up a new machine. Unknown, expired and revoked codes get a plain-text 404 (and count as a failed attempt). A sender invite's page ends with Files and checksums: each /dl file and its sha256, the same values as /dl/manifest.json.

GET /join/<code>/install.sh (no token) #

A bash script (bash, curl and python3 only) with this hub's URL, the code and the uses left baked in. See Add a sender for its flags. It is served until the invite expires or is revoked, also after its uses are spent: re-runs and --uninstall on a machine that is already set up don't redeem.

The script also carries the sha256 of every /dl file at the moment it was served (SHA256S, the page's list). It checks each file it downloads (the CLI, hooks, skill, Orca snippet) against that list and refuses a file that doesn't match or isn't listed, before installing anything from it. This is integrity, not authenticity (the list and the files come from the same hub): it catches corrupt or partial downloads and a hub whose files changed between the page and the download.

For an unknown, expired or revoked code (or a rate-limited client) the response is still 200, with header X-Needs-You-Invite: unusable (HTTP 404) (or 429) and a script that prints the reason to stderr and exits 1. A 4xx would make curl -fsSL ... | bash run an empty script and exit 0, which reads as success.

GET /dl/<file> (no token) #

Serves files from the hub's install directory (install_dir, default: the directory above hub/). Allow-list only:

FileSource in the repo layout
needs-youcli/needs-you
needs-you-hook.shintegrations/claude-code/needs-you-hook.sh
install-hooks.shintegrations/claude-code/install-hooks.sh
hooks.jsonintegrations/claude-code/hooks.json
SKILL.mdintegrations/claude-code/skill/needs-you/SKILL.md
orca-snippet.mdintegrations/orca/snippet.md (the Orca automation rules; prompts point at the installed copy)
needs_you_mcp.pyintegrations/mcp/needs_you_mcp.py (the MCP server; the installer's --mcp)
needs-you-usageintegrations/claude-code/needs-you-usage (Claude's usage-limit status line helper; the installer's --usage)
agent-instructions.mdintegrations/agent-instructions/needs-you.md (the skill's rules for Codex, Gemini CLI and opencode; --agent-instructions)
install-codex-hooks.sh, codex-hooks.jsonintegrations/codex/ (OpenAI Codex CLI hooks; --codex-hooks)
install-gemini-hooks.sh, gemini-hooks.jsonintegrations/gemini/ (Gemini CLI hooks; --gemini-hooks)
install-opencode-plugin.sh, needs-you-opencode.jsintegrations/opencode/ (needs-you-opencode.js is needs-you.js; --opencode-plugin)
install-copilot-hooks.sh, copilot-hooks.jsonintegrations/copilot/ (GitHub Copilot CLI hooks; --copilot-hooks)
install-cursor-hooks.sh, cursor-hooks.jsonintegrations/cursor/ (Cursor hooks; --cursor-hooks)
install-cline-hooks.shintegrations/cline/ (Cline hooks; --cline-hooks)
install-aider-notifications.shintegrations/aider/ (Aider's notifications command; --aider)
install-kimi-hooks.sh, kimi-hooks.tomlintegrations/kimi/ (Kimi Code hooks; --kimi-hooks)
install-grok-hooks.sh, grok-hooks.jsonintegrations/grok/ (Grok Build hooks; --grok-hooks)
manifest.jsongenerated: see below

Anything else is a 404. A file the install directory lacks is a 404 too.

GET /dl/manifest.json lists what this hub serves, for needs-you update:

{"version": "0.1.2",
 "files": {"needs-you": {"sha256": "<64 hex>", "size": 51234, "version": "0.1.2"},
           "needs-you-hook.sh": {"sha256": "...", "size": 30211, "version": "0.1.2"},
           "install-hooks.sh": {"sha256": "...", "size": 8122},
           "hooks.json": {"sha256": "...", "size": 2310, "version": "0.1.2"},
           "SKILL.md": {"sha256": "...", "size": 9876, "version": "0.1.2"},
           "orca-snippet.md": {"sha256": "...", "size": 3456, "version": "0.1.2"}}}

version at the top is the hub's; a file's version is its stamp (needs-you-version: X.Y.Z in a comment, "_needs_you_version" in hooks.json, VERSION = "X.Y.Z" in the CLI and the MCP server), absent when the file has none. Files the hub doesn't have are left out. The checksums guard a sender against truncated or mixed-version downloads; they don't make the hub more trustworthy than it already is (it minted the sender's token and served its installer).

Invite replication #

Invite records replicate like tokens (invites arrays next to tokens in /v1/replicate and /v1/replicate/changes), carrying the hash, never the code. Uses are a per-hub grow-only counter ("used": {"hub-a": 2, "hub-b": 1}), merged by taking the maximum per hub, so redemptions on different hubs add up and every hub converges on the same count. Revocation wins over an unrevoked version. peer invites are the exception: they are never replicated (they name the hub that made them; hubs up to 0.2.1 would also hold replication on the unknown role).

Double-spend window: each hub checks uses against what it has seen. If two hubs redeem the last use of the same invite within the replication delay (normally under a second; up to the retry backoff if a hub is unreachable), both succeed, so a link can set up one machine more than uses. Each extra machine still gets its own revocable token. Keep uses and ttl_hours small.

Hub updates #

A server hub installed by install-hub.sh updates itself with its checked updater (Server hubs). The owner can ask it to do so now and see how that went (ADR 0013). Local to each hub: nothing here replicates, and the Mac app sends the request to the hub it means, at that hub's own URL.

GET /v1/hub/update (owner) #

{"version": "0.5.1", "available": true, "auto_update": true, "channel": "stable", "reason": null,
 "state": "updated", "requested_at": null, "requested": true,
 "started_at": "2026-10-12T09:00:01.000Z", "finished_at": "2026-10-12T09:01:30.000Z",
 "from_version": "0.5.0", "target": "0.5.1", "message": "upgraded the hub from 0.5.0 to 0.5.1",
 "next_request_at": "2026-10-12T09:15:00.000Z"}

POST /v1/hub/update (owner) #

Asks this hub to update now. No body is read: the request carries no URL, version, channel or command, and only sets a flag on the hub. The hub's own updater does the work exactly as on its timer (newest release on the configured channel, its SHA-256 sums, manifest and build provenance checked, a failed release never retried, rollback when the new version doesn't come up), except that it doesn't wait out the soak time. It starts within seconds (the needs-you-hub-update.path unit), or on the next timer run.

Response 202 with the body of GET /v1/hub/update (state: "requested"). 200 with the same body, and nothing new recorded, while a request is pending or the updater is running. 409 unavailable when available is false. 429 rate_limited (with Retry-After) within 15 minutes of the last accepted request (update_request_interval_seconds); the updater also ignores a request within 10 minutes of the last one it honoured. Only an owner token is accepted: a sender or reader token is a 403, a peer secret a 401.

Housekeeping #

The hub cleans up after itself every 10 minutes (maintenance_seconds):

So purged items can't come back, a hub refuses to apply a replicated item that is closed (or expired) and older than its own retention cutoff, and an invite that is expired, or revoked past the 24 h grace period.

Replication between hubs #

Hubs are peers with no leader. Each hub has a hub_id, a list of peer URLs and a shared peer_secret, plus any peer links with their own secrets. Replication endpoints authenticate with Authorization: Bearer <secret>, compared in constant time:

Anything else is 401: a link's secret alone or with another link's id, the shared secret with a link id, a removed link. A hub with neither a shared secret nor links answers 404. A hub sends a link peer its link's secret and link id, a config peer the shared secret, and nobody else anything.

Records #

Replication carries full records (a tombstone too: "tombstone": true with its text empty; one that is open and unexpired is skipped as unreadable, and hubs up to 0.2.1 skip every tombstone, since its title is empty): the item JSON above with the raw stored status (expiry not applied) plus token_id, origin_hub (where it was minted) and updated_by (the hub that made this version). steps is carried as the array. A record without a steps key comes from a hub that predates steps: the receiver keeps its own steps for that id when the record's content_updated_at equals its own (a resolve, dismiss, seen or unchanged re-post on the old hub), and otherwise stores none. An empty array clears them. Older hubs ignore the field, so their own copies have no steps. question works the same way: carried as the object (or null), kept when a record without the key has the receiver's content_updated_at. A replicated question this hub would refuse on POST is dropped (the item stays), including an answerable one that repeats a label (from an older hub). answer, answered_at and answered_by work the same way (a record without an answer key keeps the receiver's answer when content_updated_at matches). An answer outlives last-writer-wins against a version written by a hub that hadn't heard of it yet, the same way a re-post keeps it on one hub: when the record's question (and token_id) equal the receiver's, a newer record without an answer keeps the receiver's answer, and an older one (or an equal version) that carries an answer the receiver lacks gives it its answer as a new write (a fresh updated_at, replicated like any other), so every hub ends up with the answer. A record with another question, or from another token, decides by LWW as usual. A replicated answer is kept only if this hub would have taken it: the record's question is answerable and the answer names offered labels (or text where the question has allow_other), one entry per question, one label or the text for a single choice. A replicated text is kept whichever token answered: a peer is a trusted hub that took it at its own edge (from an owner token, on a hub with this rule; an older hub took it from a reader too). Otherwise it is dropped with its answered_at and answered_by (the item stays). Hubs before 0.3.0 don't know allow_other or text: they drop the field from the question (and a question with an answerable item that has no options), and they drop an answer that has only text or keep only its labels. Upgrade every hub before senders set allow_other.

Status records travel in their own statuses array, in pushes and in change pages: the public status record plus token_id and updated_by. The receiver checks each as a PUT body (text, usage, no secrets; a past expires_at is fine, since a clear is an expiry, and up to a day past the future limits for clock skew) and that its id is the one derived from its token_id and key, then applies it by last writer wins on (updated_at, updated_by) like an item. One it can't read is listed in skipped with "kind": "status" and dropped (statuses are short-lived: no quarantine). A hub that already holds 256 live statuses skips new ones. Hubs that predate statuses ignore the array, so a mixed mesh keeps working and their own clients just see no meters. Token records carry id, name, role, hash (sha256 hex, never the token), created_at, updated_at, revoked_at and updated_by.

Push: POST /v1/replicate #

{"from_hub": "hub-a", "items": [ ...item records... ], "tokens": [ ...token records... ],
 "invites": [ ...invite records... ], "statuses": [ ...status records... ]}

Response {"ok": true, "applied": <n>, "hub_id": "hub-b", "skipped": [...]}; 409 self if from_hub is the receiving hub's own id (the sender then stops using that peer).

Unreadable items are skipped, not the batch. An item record the receiver can't read (a status from a newer hub, a missing or malformed field) is left out and listed in skipped as {"kind": "item", "id": "<id or null>", "reason": "<short text>"}; the rest of the batch is applied and the response is still 200. The pusher treats the listed records as delivered (it logs them and counts them in its peer status, below) instead of retrying them. The receiver keeps each skipped item record in a local quarantine (until retention_days) and applies it at its next start once it can read it, so upgrading the hub brings it in.

Unreadable tokens and invites fail closed. Token and invite records are security state (a revocation, a role, a redemption), so one the receiver can't read is never skipped: the whole batch is a 400 invalid ("field": "tokens" or "invites", the message names the record id, never its hash) and nothing in it is applied. The pusher keeps that record, and what is queued behind it, retries with backoff, and reports it in its peer status as blocked. The same holds for a pull: the puller's cursor stays before such a record. Replication resumes by itself once both hubs run a version that reads it. A body that isn't a JSON object, or a tokens/items/invites/statuses that isn't an array, is a 400 too.

Hubs up to 0.1.2 have no skipped and answer 400 for the whole batch when any record doesn't parse; against them the pusher halves the batch (without backing off) until the refused record is alone, then skips it if it is an item and holds it (as above) if it is a token or invite, and goes back to full batches. Any other failure (a timeout, 5xx, 413) is retried with backoff as before.

Every accepted write (create, upsert, resolve, patch, token add/revoke, merge, status set/clear) inserts one row per peer into a durable outbox table in the same SQLite transaction as the write. A worker thread per peer sends batches of up to 200 records (fewer when the body would pass 4 MiB), always the record's current version, and deletes the rows only after a 2xx (rows older than 7 days are dropped; anti-entropy covers them). On failure it backs off exponentially (1 s doubling to 5 min, ±20% jitter). When the wall clock jumps more than 30 s past the monotonic clock (the machine slept, as a Mac does), the worker drops its backoff and pushes and pulls at once. Outbox rows survive restarts. The admin tool writes to the same outbox, so needs-you-admin token add on one hub reaches every peer.

Applied records are not forwarded again (the mesh is full); anti-entropy covers indirect paths.

Pull (anti-entropy): GET /v1/replicate/changes?after=<seq>&limit=<n> #

Every stored version gets a per-hub sequence number. The response is

{"hub_id": "hub-b", "epoch": "01M...", "max_seq": 812, "next_after": 500, "more": true,
 "items": [ ... ], "tokens": [ ... ], "invites": [ ... ], "statuses": [ ... ]}

Each hub pulls from each peer at start-up and then every 60 s, keeping a cursor per peer and paging while more is true. An item record in a page that the puller can't read is skipped (logged, counted and quarantined, like a skipped push) and the cursor still moves past it; a token or invite it can't read, or a transient failure (the local database busy, the peer unreachable), leaves the cursor where it was. Because applying a replicated version also gives it a local sequence number, pull is transitive: a hub that was down catches up from any surviving peer. epoch is a random id minted when a database is created; if a peer's epoch changes (its database was replaced) or its max_seq is below the cursor, the puller restarts from 0.

Conflict rules #

  1. Last writer wins, per id. A record replaces the local one only if (updated_at, updated_by) is strictly greater (timestamps first, then hub id as a string tie-break). Re-applying the same version is a no-op, so delivery is idempotent and order independent. Every local write sets updated_at to max(now, previous + 1 ms), so it always beats the version it was based on.

  2. Upsert by key uses the local id. A POST whose key matches an open item on this hub updates that item, so after replication has delivered an item, every hub upserts into the same id.

  3. Two hubs mint different ids for one key (both accepted a POST for the key before hearing of the other's). When a hub applies a record that is open and finds another open, unexpired item with the same key, it merges them:

    • the lowest id wins (ULIDs start with the creation time, so the earliest-minted item survives);
    • the winner takes the freshest content: the title, body, priority, context, kind, links, steps, question (with its answer, answered_at and answered_by), source, expires_at and content_updated_at of whichever record has the greatest (content_updated_at, updated_at, updated_by); created_at becomes the earliest, seen_at the latest;
    • every loser becomes status: "resolved" with superseded_by: <winner id>;
    • both writes get a fresh updated_at and replicate like any other write.

    Hubs can see these events in different orders, so there is one more rule: whenever a hub applies a winner or a superseded record, it checks that the winner's content_updated_at is at least that of every item superseded by it, and copies the freshest content over if not. All hubs pick the same winner id and converge on the same content; only the merge timestamps differ, and LWW settles those. Clients see the loser close and the winner update.

  4. Expiry is computed, never written, so hubs can't disagree about it.

  5. Tokens replicate by id with the same LWW rule. Revocation is a write, so it wins over the older active version. An active name must be unique on the hub where it's added.

Known limits #

This page is docs/API.md in the repo. View or edit it on GitHub.