Files
LLeMbas-CLI/harness/connection.md
T
HomerandClaude Fable 5.2 99081c4ed4 The terminal is LLeMbas TERM: terminal contract 4, harness spec 3.15.0
LLEMBAS_TERM, LLEMBAS_TERM_PANE, LLEMBAS_TERM_EXECUTABLE, LLEMBAS_TERM_STATUS_DIR,
llembas-term-status=1, llembas-term-input=1, the llembas-term directory and the
llembas_term_status user var are the names. The ones from before the rename are
also read — a whole family at a time, the new one first — so the terminal's
releases before it work unchanged. One place decides: src/terminal/env.ts
(terminalNames, namesOf, isMagic). lembas --version --json: terminal_contract 4,
capability terminal_names_2. connection.json: product term.

The terminal's tests run in both spellings (scripts/test-terminal-names.sh, in
scripts/ci.sh).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-12-02 20:15:36 +00:00

16 KiB

LLeMbas connection 1

How one LLeMbas product connects to another: the server, LLeMbas CLI, the desktop and Android apps, the editor plugins and the terminal. One way, so that the same server answer, the same typed address and the same dead connection mean the same thing in every one of them.

This page is the rule. The numbers, the names and the words are data in connection.json; the parts that are pure functions have conformance cases every implementation runs: conformance/address.json, conformance/version.json, conformance/errors.json. What travels over the link and over stdio is acp.md; this page says how the connection under it is made, kept and lost.

"Client" is the side that connects: an app to the server, the CLI to the server, an editor to lembas serve --stdio. Must is a rule; a product that cannot follow one says so in its own documentation and in words to the person, never by silently doing something else.

1. Address

A person types an address once. Every product turns it into the same base address with the same rule (conformance/address.json):

  1. Trim it. Empty, or with white space inside, is invalid_address.
  2. No scheme typed: https:// is put in front. A typed scheme is read in any case of letters.
  3. Only http and https; a scheme with no host after it is invalid_address.
  4. Keep the scheme, the host in lower case, and the port unless it is the scheme's default. Drop the user name, path, query, fragment and any trailing slash.

A product that will not speak plain http (an app whose platform forbids cleartext) must say so in words at this step, before any request.

Two addresses are one server when their base addresses are equal — and an address typed without a scheme is the same server as one on either scheme, because its https was a default and not the person's word (kind: same). Every comparison of addresses anywhere in a product — "am I already signed in there", "is this computer linked to my server" — is this comparison, with both sides normalised first. No product compares raw strings.

What is stored is the normalised typed address. The base_url in the server's discovery is advisory: a product may show it and write it to a log, and never stores it and never compares with it. It is built from the headers of whatever stands in front of the server, so two products that stored it could disagree about the name of one server, and a product that followed it would be talking to an address nobody typed.

Sign-ins kept from before this rule are not lost and not doubled:

  • an existing sign-in keeps working untouched; its stored address is read through the address rule above from now on, and compared like any other;
  • only a new sign-in stores the typed address;
  • a sign-in whose stored address is the one the server's discovery names — it was stored when that was what a product kept — is the same sign-in as one now made by typing the server's real address: it is replaced, never given a twin (kind: login). The server's word is taken only for this, and only about an address already stored: discovery naming an address nobody stored claims nothing. And it is not proof. A token is sent only to the server it belongs to: a sign-in recognised as the same on the server's word alone has its old token signed out at the address that token was stored with, and the new token is never named there — so a server that claims another's address in its discovery gets neither the other's token nor a twin of it.

2. Discover

GET <base>/.well-known/lembas.json, answered within discovery.timeout_s. It is asked at every start and before every reconnect — never only at sign-in — because the server may have been updated since. The outcomes, each with its own code and words (§11):

What happened Code
name not found, refused, reset unreachable
no answer in time timeout
the certificate is not trusted tls_untrusted — the sentence names what that product's person can do about the CA
404, or a body that is not JSON, or service is not lembas not_lembas
any other status that is not 2xx server_error, saying the status — never not_lembas
no version in common, or this product below the server's minimum server_too_old or client_too_old (§5)
an endpoint it names is on another origin other_origin

Endpoints from discovery are used only on the stored base's origin — the same scheme, host and port (conformance/address.json, kind: origin). Where discovery names an address anywhere else for something the client is about to call — where to sign in, where its API is — the client does not call it and says other_origin, naming the address; what the client reaches by a path of its own on the stored base needs no word from discovery and is not affected. This is also a safety rule: the token goes only to the server whose address a person typed.

3. Identify

Every product names itself the same way, llembas-<product>/<version>, with <product> one of connection.json products: cli, desktop, android, nvim, vscode, term, server — and tty, what the terminal named itself before it was renamed LLeMbas TERM (its releases 1.0.0-beta.1 to 5), still known so that such a terminal is told what to update by its own name.

  • HTTP: User-Agent: llembas-<product>/<version> on every request to a LLeMbas server, the link's upgrade request included.
  • Device code: client_id is the same string.
  • stdio: clientInfo: { name: "llembas-<product>", version } in initialize; the agent answers agentInfo: { name: "LLeMbas CLI", version }, reads the client's and writes it to its log.

The server records the User-Agent with a token's last use. Nothing is ever decided by parsing it: it is for people and for logs.

4. Authenticate

Device code (RFC 8628). The token is a bearer in the Authorization header and nowhere else: never in an address, a command's arguments or the environment. A client polls at interval, adds 5 s on slow_down, and stops at expires_in (expired). A token at rest is a file only its owner can read (mode 0600), or the platform's keystore.

A client that reads its token from a file reads it each time it connects, not once at start: a sign-in made again by another process is then picked up without restarting anything.

5. Versions

Each surface has its own integer, and a client checks only the surfaces it uses: protocols (the link, and _lembas over stdio), app_api (the apps' API). harness_spec is information.

The rule is one function, both ways (conformance/version.json): what both list in common, the highest is used. With nothing in common, the side whose highest number is lower is the old one — server_too_old ("Update the server.") or client_too_old ("Update ."). The sentence always says which side to update, and names the other side's version.

stdio. The client says clientCapabilities._meta.lembas.protocol, the agent answers _meta.lembas.protocol with its own (stdio.protocol), and both use the lower. A client that says a higher one than the agent has is also told so in words: _meta.lembas.notice in the answer, one sentence. Within a protocol a feature is used only if its capability is listed (acp.md Versions and discovery). A client that needs a newer CLI than it found compares agentInfo.version with its stated minimum once, at connect, and says server_too_old in its stdio wording, with both versions.

min_clients. Discovery may carry "min_clients": { "<product>": "<version>" }, the lowest version of each product the server still serves — products cli, desktop, android, nvim, vscode, term (and tty, the terminal before its rename). Absent, or without the product: no minimum. A client below its minimum says client_too_old in the minimum wording — both versions, and how to update — at discovery, and does not go on. Versions are compared as numbers and know a pre-release (2.0.0-beta.10 is newer than 2.0.0-beta.9, and 2.0.0 newer than 2.0.0-beta.99); a value that is not a version is no minimum (conformance/version.json, kind: min).

A client of lembas serve --stdio does not ask the server itself, so the agent tells it: in initialize's answer, _meta.lembas.min_client is the minimum the logged-in server names for the product the client said it is in clientInfo.name (llembas-nvim → min_clients.nvim) — a version string, or absent when the CLI is not logged in, has not heard the server's discovery, the client named no LLeMbas product, or there is no minimum for it. The client compares its own version and says the same sentence.

6. A refused token

One event, one behaviour, in every product.

  • Signed out is a 401 that carries the server's own challenge, WWW-Authenticate: Bearer realm="lembas" (connection.json challenge), or the link closed as refused (the server sends 4403; link.close.refused): signed_out. It concerns the token that was sent — the client marks the account that token belongs to, not whichever account is in front, as signed out by the server, stops that account's streams and its link, keeps everything else, and offers the sign-in in one step. A refusal that arrives late, for a token since replaced, signs out nothing: the newer token was not the one refused.
  • A 401 without that challenge is not a sign-out. The server's /v1 routes pass a model provider's status through, so a provider's 401 reaches the client: it is shown as the server's own sentence (failed), and nobody is signed out.
  • 403: not_allowed, with the server's own sentence where it gave one. Nobody is signed out.
  • 429: busy. The client waits what Retry-After says — whole seconds, at least 1 and at most retry_after.ceiling_s (five minutes) — before the request is made again, and its own schedule (§9) where the header is absent or is not a number of seconds.

wss://<base>/api/devices/link (ws:// for an http base), one JSON-RPC message per frame (acp.md).

  • Connect and handshake together take at most link.handshake_s: the upgrade, the device's _lembas/hello, and the server's initialize answered by the device. A dial that has not got that far in time is closed by the device (timeout) and dialled again on the schedule.
  • linked is said only after initialize was answered. An open socket is not a link: the server accepts the socket before it judges the token. Until then the state is connecting.
  • Close codes. link.close.protocol (4400): no protocol in common — final. One of link.close.refused (4401, 4403, 1008): the token is refused (§6) — final for that token. link.close.replaced (4409): a newer link of the same device took this one's place — the device does not redial by itself and says replaced. Any other code: dropped; dial again.
  • A refusal after the token changed is not final. A device whose token file holds another token than the one it dialled with, when it is refused, dials again at once with the new one.
  • A stopped link starts again when the sign-in does. A device that stopped for signed_out, replaced or not_logged_in and keeps running watches its own sign-in and dials again when that changes; nobody has to restart it.
  • Liveness. The device sends a WebSocket ping every link.ping_s. Anything heard — a message, a pong, a ping — is life; after link.silence_s with nothing heard the device closes the socket and dials again. The server needs nothing beyond answering pings, which every WebSocket implementation does.

8. Streams

A long-lived HTTP stream (server-sent events) carries something at least every streams.keepalive_s, a comment line where there is no news. A client that hears nothing for streams.silence_s closes it and reconnects on the schedule. A stream is never read without a timeout.

9. Reconnect

One schedule, everywhere something is connected again — the link, a stream, a supervised child:

  • wait reconnect.first_s, doubling to reconnect.ceiling_s;
  • each wait changed by a random ±reconnect.jitter of itself, so that clients cut off together do not come back together;
  • the wait starts over only after a connection stayed healthy for reconnect.healthy_s. A connection that opens and fails — a handshake that never finishes, a socket closed a moment after it was accepted — counts as one more failure, not as a success.

A stop that will not heal by itself is not retried: it carries its code (§10), and waits for the thing that changes it.

10. State between programs

One program tells another its state in codes, never in sentences. Nothing parses English.

  • LLeMbas CLI's service.json: state (connecting, linked, waiting, stopped), code — one of connection.json codes — and detail, the sentence for a person.
  • lembas login --json: its error event carries code beside message.
  • JSON-RPC refusals carry data.reason (acp.md).

A program that meets a code it does not know treats it by state alone and shows detail.

11. The same words

connection.json codes is the table: a condition, its code, its English sentence (conformance/errors.json). Every product uses it — translated where the product is — and adds only its own step from steps, the one thing a person can do there.

Code When Tries again by itself
linked the link is up: initialize was answered —
connecting dialling, or waiting for the handshake —
unreachable name not found, refused, reset; a link that dropped yes
timeout no answer in time; a handshake that did not finish yes
tls_untrusted the certificate is not trusted yes
not_lembas it answers and is not LLeMbas yes
server_error discovery or a request answered a status that is an error yes
busy 429 yes, after Retry-After
server_too_old no version in common; the other side is the old one no
client_too_old no version in common; this product is the old one no
signed_out a 401 with the server's challenge; the link closed as refused no — until signed in again
replaced the link closed with 4409 no — until signed in again
not_logged_in there is no sign-in on this computer no — until signed in
not_allowed 403 no
other_origin discovery names an endpoint on another origin (§2) no
disabled the link is turned off on this device no
invalid_address §1 no
expired a device code ran out before it was approved no
stopped stopped on purpose no
failed anything else — a 401 without the server's challenge among it; the sentence is the server's or the product's own no

A failed turn is reported in the prompt's answer — _meta.lembas.error = { code, message } (acp.md, session/prompt) — as well as by the error event, so a client that draws only what the answer says still shows why a turn ended with no reply.

12. A client of lembas serve --stdio

An editor plugin, the desktop app's helper, the terminal:

  • finds the binary — its setting, then a bundled and verified one, then PATH;
  • runs lembas serve --stdio and sends initialize (§3, §5), which must be answered within stdio.initialize_s;
  • answers every request it does not implement with -32601, never with silence;
  • reads lines of at most stdio.line_bytes and gives up the connection on a longer one — as the agent does with what the client sends;
  • keeps the child's last stdio.stderr_lines lines of stderr, and says the last when it stops;
  • stops it by closing its stdin, then SIGTERM, then SIGKILL after stdio.kill_after_s;
  • after a crash says why once and starts it again at the next use — at most once every stdio.quiet_restart_s when nobody asked.

13. Several accounts

An account is the pair of a base address (§1) and the account's id on that server. LLeMbas CLI holds one sign-in. A client with several accounts shows which account the computer is linked as, and never replaces that sign-in without saying whose it replaces.