Merge branch 't3/remote-mcp-oauth-signin': the remote door and sign-in for web assistants (P15 B1, D-83, D-86, D-90)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-21 12:16:48 -07:00
commit 5a98e417a1
27 files changed
+8578 -63

No files matched your search

+91
View File
@@ -477,6 +477,87 @@ didn't open, with why). Protocol version 1: the requests are new, the
that ended is not listed; nothing is kept across a server restart.
`wanderbooks doctor` prints these lines under "Assistants connected".
### The remote door (plan/p15-app-and-relay.md section 4, unit B1; WB-remote-1 to 10)
Web assistants (Claude and ChatGPT on the web; Gemini, Grok and muse.ai best
effort) call connectors from their vendor's cloud over HTTPS. The books server
has a second HTTP listener for them, on **127.0.0.1 only and off until the app
asks** (the relay, unit B2, and the app's line to it with its certificate, unit
B3, put it on the internet). The admin socket's requests (owner only) are its
whole control:
- `remote_start { public_url, port? }` turns it on as the **one public address**
(`https://name`, no path: the issuer), on a free loopback port unless one is
given, and answers where it listens and this start's `proxy_secret` (64 lowercase
hex characters, for the app only, never printed in a log); the same address
again is a no-op that answers the same secret, another restarts it with a new
one. `remote_stop` turns it off (it also stops when the server closes).
`remote_allow { book, allowed }` offers a company or withdraws it;
`remote_status` says all of it.
- `remote_pending` lists the assistants waiting to be allowed (what each calls
itself, cleaned and short; the address it is sent back to; whether that host is
a known vendor's; its six-digit code, which its browser page shows too; the
caller's address; the company; whether posting was asked; the time left);
`remote_decide { id, approve, post }` is the person's answer, and every Allow
makes a new approval with exactly the permissions given. `remote_approvals { book? }` lists the connections
allowed; `remote_set_post { approval, post }` raises or lowers one;
`remote_make_key { book, name, post }` makes a key for a product that takes only
a key, and `remote_make_client { name, redirect_uris }` a client id and secret
to type into one: each secret is shown once, in the answer, never again.
**Every connection begins with this start's secret, then the relay's PROXY
protocol version 2 header** (the exact form `crates/relay` writes: TCP over IPv4
or IPv6, no extensions). The secret is the 64 characters of `proxy_secret` from
`remote_start` or `remote_status`, written as they are, as the very first 64
bytes, before anything; only the app, which is told it over the owner-only admin
socket, can say a caller's address, not another process on this computer that
finds the port. The address in the header is the caller's, and is what the access
record (`from`) and the limits use (an IPv6 caller is counted by its /64). Anything
else, a wrong secret included, is dropped before a byte of HTTP is read; no
`X-Forwarded-For` is ever read. A request whose `Host` is not the public
address's host is 421; one with another `Origin` is 403.
Routes (all for the public address only):
| Route | What |
|---|---|
| `/.well-known/oauth-protected-resource` (root, `…/b/{book}/mcp` appended, or `/b/{book}/mcp/…` before it) | RFC 9728 metadata for a company's address (the root's only while one company is offered) |
| `/.well-known/oauth-authorization-server`, `/.well-known/openid-configuration` (and the same with a path either side) | RFC 8414 metadata; every address in it is the issuer's |
| `POST /register` | dynamic registration (RFC 7591): 1 to 20 redirect addresses, 201 |
| `GET /authorize` | always parks the request (never approves, and never because the product was allowed before: D-90) and answers a page with the request's code that says to look at Wanderbooks; an error is a page for a client nobody approved, a 302 for one the person has |
| `GET /authorize/wait/{id}` | the page's own look: the same page while waiting, then a 302 with `code` (or `error=access_denied`), once |
| `POST /token` | `authorization_code` (PKCE S256, `resource` checked) and `refresh_token` (rotated; one used twice ends its family); a client secret as Basic or in the body |
| `/b/{book}/mcp` | the company's MCP, stateless, plain JSON, for a good token only: 401 with `WWW-Authenticate: Bearer resource_metadata="…"` otherwise, `initialize` included |
A client is one of: a client ID metadata document (an https `client_id`, fetched
only when every address of its host is public, no redirect, no proxy, at most 64
KiB, five seconds, and only when a sign-in begins, never at `/token`); a dynamic
registration; or a client id and secret the app made. The redirect address must
equal a registered one exactly (at most 512 bytes each, 4 KiB together). A
registration is held in memory and reaches the file only when the person allows a
sign-in of it. Access tokens last ten
minutes and are kept in memory; refresh tokens last sixty days since last use and
are kept (hashed) in `auth/remote.json`, 0600, so a refresh after the app was
closed for days still works. Nothing is kept as text but a hash.
**An approval** is the owner's decision about one sign-in, a company and its
permissions: one token of the books' principals (named `web-…`, listed by
`token_list` and revoked by id like any, which ends every token and refresh token
made under it at that moment) plus a record naming the connection. It is never
reused (D-90): every new sign-in, of a product registering again or not, is asked
in the window, with a short code to match with the browser's page, and an Allow
makes a new approval with exactly the permissions ticked then. The window says
where an assistant will be sent back to, never that it is a known product. A web approval grants `books:read` and `books:write`
(drafts); `books:post` only when the person turns it on; `books:admin` never. A
stranger's unsigned request is a 401 and is counted, not written to the access
record, and a good token's refusal for rate or for another company's books is
counted, not written (its other refusals are recorded with the caller's address). Calls go
through the same server as local ones (`call_tool`): a write needs its
`request_key`, a repeat returns the first answer. Nothing bans an address; what
a request creates is bounded (2000 clients, 20 waiting requests and 5 per address,
which give way to a newer one rather than turn it away, 600 calls a minute per
approval, and so on).
## Server instructions
The MCP `initialize` response carries an `instructions` string, the
@@ -518,6 +599,16 @@ overrides and zero-width spaces are dropped), cut to 56 characters.
`instructions_budget` (the longest rule, every parties setting) and
`the_company_name_is_one_plain_line` check it.
Some products keep only the first 512 characters of the instructions
(ChatGPT) and one is reported to drop them (claude.ai, unverified since), so
**the company and the entry point also begin the description of the tools a
model reaches for first** (`books.start`, `books.handbook`, `books.chart`,
`books.show`, `books.digest`, `books.search_entries`, `books.draft_entry`,
`books.post_entry`): "For the books of Acme: call books.start first in every
session." (at most 160 characters, added by `list_tools` to the tool's own text
of at most 700), for local and remote callers alike.
`the_company_and_the_entry_point_are_in_the_first_tools_too` pins both.
The long guidance is the **handbook**, compiled in from
`ledger-mcp/handbook/*.md`, with the receipt rule in force rendered in:
+15 -1
View File
@@ -255,9 +255,23 @@ Three new rows, none above changed status, and the floor stands.
|---|---|---|---|
| Books home: a visible folder per book, `<Documents>/Wanderbooks/<id>`, holding `AGENTS.md`, `CLAUDE.md`, `.mcp.json` and `.claude/settings.json` and nothing of the books | A QBO/Xero company has no counterpart; the point is the agent's workplace (D-27) | partial | `books_home_holds_no_books_data`, `a_books_home_made_again_keeps_what_a_person_kept` (`crates/ledger-host/tests/books_home.rs`); `partial` until the macOS home is tried |
| Agent dock: Claude Code or Codex in a terminal along the bottom of the viewer, started in the books home; never required | QBO's assistant panel is a cross-check only | partial | `dock_runs_the_chosen_agent`, `agent_missing_offers_the_installer` (`crates/ledger-gui/tests/dock.rs`); Linux checked; macOS by the CI run; Windows not offered (ConPTY unchecked) |
| Assistants in the window: each company's state (none, connected, working) as a shape and a word, "Ask your assistant" where a button that edits would be, the Connect page (a row per local tool, the keys with who is on each, Revoke), and a warning before closing the window or a tab with an assistant connected (plan P15 3.3, D-79) | QBO's "Accountant" invitations and Xero's "Advisor" access are a cross-check only | partial | WB-assist-1 to WB-assist-4; `crates/ledger-gui/tests/assistants.rs`; local assistants only: the web half is plan units B1 and B3, the Claude desktop extension C1 |
| Assistants in the window: each company's state (none, connected, working) as a shape and a word, "Ask your assistant" where a button that edits would be, the Connect page (a row per local tool, the keys with who is on each, Revoke), and a warning before closing the window or a tab with an assistant connected (plan P15 3.3, D-79) | QBO's "Accountant" invitations and Xero's "Advisor" access are a cross-check only | partial | WB-assist-1 to WB-assist-4; `crates/ledger-gui/tests/assistants.rs`; local assistants, and (unit B1) the question to the person when a web assistant asks and what each web connection may do; the web switch, address and steps are unit B3's, the Claude desktop extension C1 |
| First run with no terminal: the viewer sets up the private folder, the owner passphrase, the first books and an agent | QBO's "set up your company" flow | partial | `first_run_onboards_with_no_terminal` (`crates/ledger-gui/tests/welcome.rs`); the AppImage's whole path is the EM's to try on a clean machine |
## P15 B1: the remote door (2026-11-21, D-83, D-86)
One new row below; none above changed status, so the floor stands. A second
HTTP door in the books server, on 127.0.0.1 and off until the app asks, is the
sign-in server web assistants knock on (WB-remote-1 to WB-remote-10,
`docs/mcp-interface.md` "The remote door"); the window asks the person when a
web assistant wants to connect. It is `partial`: the door is not on the
internet until the relay's client and certificates (unit B3) exist, and no
product's own sign-in has been tried against it yet.
| Area | Counterpart (a cross-check, not the spec) | Status | Notes |
|---|---|---|---|
| Web assistants reach a company over HTTPS and sign in with OAuth 2.1, approved by the person in the app's window; read and draft by default, posting per connection | QBO's and Xero's "connected apps" consent screens are a cross-check only | partial | WB-remote-1 to WB-remote-10, WB-assist-5; `ledger-mcp` unit tests in `src/local/remote/` (`tests.rs`, `proxy.rs`, `cimd.rs`, `pages.rs`), `ledger-gui` `tests/assistants.rs`; open: reached through the relay (B3), tried with Claude and ChatGPT on the web, uploads and downloads over this door |
## P15 T1: every write is one transaction, exactly once (2026-11-21, D-88, D-89)
| Area | Counterpart (a cross-check, not the spec) | Status | Notes |
+32 -2
View File
@@ -88,11 +88,41 @@ when it connected and last acted, and "Revoke", asked once, which calls what
`wanderbooks token revoke` calls (the server ends the key and the sessions on
it; its copy in the token file is removed). The window writes no books. The
web half of the page (the switch, the address, the steps for Claude and
ChatGPT) is plan unit B3's; `connect::State::web_section` is its place, and
the "Claude desktop app" row (a one-click extension) is unit C1's.
ChatGPT) is plan unit B3's, and the "Claude desktop app" row (a one-click
extension) is unit C1's. What unit B1 puts in `connect::State::web_section` is
the question to the person when a web assistant asks to connect (WB-remote-5):
a card that says who is asking, where it is sent back to, the address it asked
from and what it will be able to do (read and draft; posting only if ticked),
with "Allow" and "Don't allow", over any page of the company
(`connect::State::web_prompt`) and on the Connect page; and, for each web
connection allowed, what it may do, with "Let it post entries" and "Stop
letting it post". Revoke is the same row of "Who can reach these books".
*Enforced:* `ledger_gui::connect::State::show`, `ledger_host::commands`
(`bridge_serve_args`, `command_line`, `ensure_key`, `forget_token_file`),
`ledger_host::app_server::AppServer::revoke_token`.
*Checked:* `ledger-gui` `tests/assistants.rs`
(`the_top_bar_state_opens_the_connect_page_which_lists_and_revokes`,
`the_connect_buttons_do_what_connect_does`).
**WB-assist-5** The company's name and "call books.start first" stand in the first tools' own descriptions too.
```text
GOAL: a product that keeps only the first 512 characters of the server's
instructions, or drops them, still tells its model which company this is
and where to begin.
SOURCE: plan/p15-app-and-relay.md 3.4 (ChatGPT uses only the first 512
characters; claude.ai is reported to drop them, unverified since).
INVARIANTS:
1. The first 512 characters of the instructions stand alone and name the
company, books.start and books.handbook.
2. The descriptions of books.start and of the tools a model reaches for
first begin with the company and "call books.start first in every
session", for local and remote callers alike; the lead is at most 160
characters.
SCENARIOS:
1. A list of tools names the company in each of the first tools and in
no other.
```
*Enforced:* `first_tools_lead`, `connect_text`, `list_tools` (the server's `BooksServer`).
*Checked:* `ledger-mcp` unit test `the_company_and_the_entry_point_are_in_the_first_tools_too`.
+353
View File
@@ -0,0 +1,353 @@
# Claims: the remote door
The product rules of how web assistants (Claude and ChatGPT on the web; Gemini,
Grok and muse.ai best effort) reach a company, and the sign-in behind it (plan
P15, unit B1; decisions D-81, D-83, D-86, D-88 in `plan/p15-app-and-relay.md`;
the ten sign-in rules of its section 4). No authority outside this product
states them: they are wanderbooks' own (`WB-remote-<n>`). They stand on public
specifications for the protocol (OAuth 2.1, RFC 7636, 7591, 8414, 8707, 9728 and
9700, the MCP authorization specification) and on nothing else. The code is the
`remote` module of the books server (`crates/ledger-mcp/src/local/remote/`).
The door listens on 127.0.0.1 only, off until the app asks. The relay (unit B2)
and the app's line to it with its certificate (unit B3) put it on the internet;
the relay writes the caller's address in a PROXY header before the caller's own
bytes (WB-relay-6), and this door reads it.
Rules: none from bookspec.
---
## Product rules
**WB-remote-1** The door is off until asked, is loopback only, and knows each caller's address only from the relay's header.
```text
GOAL: nothing reaches the books from the internet unless the app turned the
door on, and the address of a caller is the one place no caller can
write.
SOURCE: WB-relay-6 (the caller's address), D-80, D-83 in
plan/p15-app-and-relay.md; the PROXY protocol specification version 2;
plan/p15-app-and-relay.md section 4, rule 8.
INVARIANTS:
1. The door listens only after a RemoteStart admin request, on 127.0.0.1;
it stops on RemoteStop and when the server closes. It serves a company
only while that company is offered (RemoteAllow) and open.
2. Every connection begins with this start's secret, then the relay's exact
header (a version 2 PROXY header for TCP over IPv4 or IPv6); anything
else, a cut header, or silence for five seconds is dropped before a
byte of HTTP is read. The secret is 64 lowercase hex characters made
for each start of the door, given to the app over the owner-only admin
socket (`proxy_secret` of the remote status, shown nowhere else and
never in a log) and compared in constant time: so only the app's relay
line can state a caller's address, not another process on this
computer that finds the port. What the app (unit B3) writes first on
every connection is exactly those 64 characters, then the header.
3. The address in the header is what the access record and the limits use;
no HTTP header (X-Forwarded-For and its kind) is read for it; a second
header sent inside the caller's own bytes is HTTP text, refused as such.
4. Connections are bounded, and a request's headers must arrive in time.
SCENARIOS:
1. A call through the door is recorded with the address in the header, not
with the one a caller's forwarding headers claim.
2. A connection with no secret, a wrong one, no header, or a header inside
a request, gets nothing.
3. The door is off before it is asked, and refuses connections after it is
stopped.
```
*Enforced:* `read_secret`, `new_secret`, `read_header`, `accept_loop`, `start_door`, `stop_door`, `Asked` (the server's `local` module).
*Checked:* `ledger-mcp` unit tests `the_relays_header_reads_back_its_source`, `a_connection_must_begin_with_this_starts_secret`, `a_connection_without_the_secret_is_dropped_before_anything_is_read`, `anything_but_the_exact_form_is_refused`, `the_recorded_address_is_the_relays_and_a_forged_one_changes_nothing`, `the_door_is_off_until_asked_and_stops_when_told`.
**WB-remote-2** There is one public address, and every address the door says is built from it.
```text
GOAL: a product that learns where to sign in is never told another place,
whatever it sends in its request.
SOURCE: RFC 9728 and RFC 8414 (the metadata documents and where they are
found); plan/p15-app-and-relay.md section 4, rule 1 (a year of
breakage in another project came of building these from the request).
INVARIANTS:
1. The issuer is the address given to RemoteStart (https, a host, no path)
and no other; a request for another Host is refused (421). The host is
the `Host` header's; under HTTP/2, which has none, it is the request
address's authority, and the same check is made on it.
2. The protected-resource document is served at the root, after the
well-known name with the resource's path appended, and before it; the
authorization server's at its two well-known names, with a path
appended or prepended too. Every address in them is the issuer's.
3. A token, code or refresh token made under one issuer is refused under
another.
SCENARIOS:
1. Every place a product looks answers the same document.
2. The public address changes: what was made under the old one stops.
```
*Enforced:* `start_door`, `front`, `server_document`, `resource_document`, `Remote::authenticate`.
*Checked:* `ledger-mcp` unit tests `the_metadata_is_the_pinned_address_at_every_place_products_look`, `an_expired_token_and_a_token_for_another_issuer_are_refused`, `the_host_is_read_from_the_authority_when_there_is_no_host_header`.
**WB-remote-3** An unsigned request is a 401 that says where to sign in, `initialize` included, and is never written to the books' record.
```text
GOAL: a product that was told nothing finds the sign-in; a stranger cannot
fill the disk by knocking.
SOURCE: RFC 6750 (the challenge), RFC 9728; plan/p15-app-and-relay.md section
4, rule 4 (one product shows "connected" and never signs in when its
first request is let through).
INVARIANTS:
1. A request to a company's path with no token, a wrong one or an expired
one is a 401 whose WWW-Authenticate names that company's
protected-resource metadata; a presented bad token adds
error="invalid_token". `initialize` is no exception.
2. A key made for the local socket opens nothing on this door.
3. Such a request is counted in memory and not written to the access record
(a request with a good token that is refused is).
SCENARIOS:
1. initialize, a tool call and a notification, unsigned, are each 401.
2. Twenty more unsigned requests leave the access record where it was.
```
*Enforced:* `unauthorized`, `Remote::authenticate`, `door` (the server's `local` module).
*Checked:* `ledger-mcp` unit test `an_unsigned_request_is_a_401_that_says_where_to_sign_in_initialize_included`.
**WB-remote-4** A client introduces itself in one of three ways, and is sent back only to an address it registered.
```text
GOAL: any of the products can sign in, and none can be sent to an address a
stranger chose.
SOURCE: RFC 7591 (dynamic registration), the IETF draft for OAuth client ID
metadata documents, RFC 6749 section 10.15 (open redirectors), RFC
7636 (PKCE), RFC 8707 (resource); plan/p15-app-and-relay.md section 4,
rules 2 and 3.
INVARIANTS:
1. A client is a client ID metadata document (an https address, fetched
only if every address its host has is public, with no redirect, no
proxy from the environment, over the address that was checked, at most
64 KiB, within five seconds, its client_id the address it came from),
a dynamic registration, or a client id and secret the app made. The
private forms inside IPv6 are refused too: IPv4-mapped, IPv4-compatible
(::/96), NAT64 and 6to4 (2002::/16). A document is fetched only when a
sign-in begins, never from the token endpoint; one we hold that is past
its time is used as it stands when the fetch budget is spent.
2. A redirect address is https, a loopback http address, or an app's own
scheme, with no fragment or userinfo, at most 512 bytes; a client may
carry up to twenty, at most 4 KiB together; the one asked for must
equal one registered exactly. A mismatch is a page, never a redirect;
and for a client nobody ever approved every error of a sign-in is a
page too, never a redirect to the address it registered.
3. PKCE with S256 is required, and its verifier is checked in constant
time; `resource` must be one company's address under the issuer (or
absent where one company is on offer); a code is single-use, lives
sixty seconds, and ends what it made if used twice; the consent is a
302.
4. A secret (a client's, a code, a token) is kept as its SHA-256 only.
SCENARIOS:
1. Each of the three kinds signs in end to end and reads the books.
2. Wrong redirect, no PKCE, the plain method, a wrong resource, a wrong
verifier and a reused code are each refused.
```
*Enforced:* `Remote::register`, `Remote::resolve_client`, `Remote::authorize`, `Remote::token`, `parse` (client documents), `is_public`, `valid_redirect`, `redirects_fit`.
*Checked:* `ledger-mcp` unit tests `a_registered_client_signs_in_and_reads_the_books`, `a_client_metadata_document_signs_in`, `a_typed_client_id_and_secret_signs_in`, `a_redirect_that_is_not_registered_is_never_redirected_to`, `pkce_the_resource_and_the_verifier_are_required`, `only_public_addresses_are_fetched`, `a_document_must_be_what_it_says_it_is`, `nothing_is_ever_fetched_from_the_token_endpoint`, `a_stale_client_document_is_used_when_the_fetch_budget_is_spent`, `a_redirect_is_at_most_512_bytes_and_a_clients_together_a_few_kib`.
**WB-remote-5** No assistant connects without the person, in the app's window, and every sign-in is asked.
```text
GOAL: nothing a stranger sends over the internet can approve a connection,
and the secret address alone never opens the books.
SOURCE: D-83 and D-90 in plan/p15-app-and-relay.md (a new assistant is
approved in the app's window, by whoever is at the machine; every
new sign-in is asked, an approval is never reused).
INVARIANTS:
1. /authorize never approves: it parks the request for five minutes and
answers a page that says to look at Wanderbooks (no form, no button,
no script, every outside word escaped) and looks again by itself.
Every sign-in is parked, always (D-90): the app can tell which
product asks (the redirect host is public), not whose account, so an
earlier approval of the "same product" counts for nothing.
2. Only an admin request (the owner-only admin socket) turns a waiting
request into a code, by an id that is not the browser's: the page's own
id only reads the outcome, once (302 with the code, or with
access_denied after a refusal or a timeout, or temporarily_unavailable
if a newer request made it give way).
3. The window is told who is asking in neutral words: where it will be
sent back to first ("An assistant that will be sent back to
claude.ai"), then what it calls itself in quotes (cleaned of
invisible and direction-changing characters, at most forty
characters), the caller's address, the company, whether posting was
asked for. No request is called a known or trusted product on the
strength of its redirect host: a host is public. The warning ("doesn't
know this address") is kept for hosts that are not a known vendor's.
4. Each waiting request has its own six-digit code (two groups of three),
never the same as another waiting one's, shown on the browser's page
and on the window's card; the card says to allow only if the browser
shows the same code. Requests are never merged.
5. Waiting requests are bounded: twenty, five per address (an IPv6 /64 is
one address). A new request is not turned away: an address at its limit
gives up its oldest waiting request, and a full table the oldest of the
address with the most waiting. The window draws the newest three and
counts the rest.
SCENARIOS:
1. The person allows: the browser is sent back with a code.
2. The person refuses, or no one answers: the browser is sent back with
access_denied, and nothing was made.
3. A guess of the browser's id decides nothing.
4. A stranger registers a client with the callback of a product the person
approved before, asks to sign in, and reads the 302: there is none; it
is a page, and no code exists until an Allow.
5. Eight requests from one address: five wait, the oldest three are told
so; a full table still takes a real person's request.
```
*Enforced:* `Remote::authorize`, `Remote::wait`, `Remote::decide`, `Remote::pending_list`, `Remote::make_pending_room`, `match_code`, `is_vendor_host`, `waiting` and `problem` (the page), `ledger_gui::connect::State::web_prompt`, `newest_waiting`, `ledger_host::app_server::AppServer::web_decide`.
*Checked:* `ledger-mcp` unit tests `a_stranger_who_knows_the_product_gets_no_code_without_an_allow`, `the_window_is_told_who_asks_and_the_page_only_says_to_look`, `a_refusal_and_a_timeout_send_the_browser_back_denied_and_make_nothing`, `waiting_requests_give_way_and_never_fill_the_window_against_a_real_one`, `a_name_is_shown_without_invisible_or_direction_changing_characters_and_short`, `a_strangers_words_never_become_markup`; `ledger-gui` `tests/assistants.rs` (`a_web_assistant_asking_is_put_to_the_person_over_any_page_in_plain_words`, `waiting_requests_each_show_their_own_code_and_only_a_few_are_drawn`, `refusing_and_allowing_with_posting_are_their_own_answers`, `a_request_that_ran_out_goes_away_and_posting_is_turned_on_and_off_on_the_connect_page`).
**WB-remote-6** Every Allow is a new approval with exactly what was ticked; a revoke ends it at once and for good.
```text
GOAL: the person's yes is for one sign-in; revoking ends everything made
under the approval, and nothing brings it back.
SOURCE: plan/p15-app-and-relay.md section 4, rule 6 as D-90 changed it; RFC
9700 (refresh token rotation and reuse); D-86.
INVARIANTS:
1. An approval is one token of the books' principals (so "Who can reach
these books" lists it, and revoking it by id ends it) plus a record
naming the connection and the company. Every Allow makes a new
approval with a new id and a new token, with exactly the permissions
ticked then: nothing is carried over from an older approval of the
same product, with posting or without; an older one stays as it is,
to be revoked or kept on its own. Refresh within a sign-in is
unchanged (an access token and its refresh token belong to one
approval).
2. An access token lives ten minutes and is memory only; a refresh token
lives sixty days since last use, is kept (hashed) across restarts in a
file only the owner can read (mode 0600; on Windows `set_owner_only`,
the helper the token files use), is rotated at every use, and one used twice ends its whole family. The
check that a refresh token is unused and its marking as used are one
hold of the lock, with the minting of what replaces it: two refreshes
at once, one wins and the other is a reuse that ends the family.
3. A revoked connection's access token is a 401 at once, and its refresh
token is invalid_grant: revoking (or making its token over by name)
removes the approval, its refresh tokens, access tokens and codes under
the lock at that moment, never when a list is next read. A later Allow
of the same product is a new approval; none of the old tokens return.
4. Raising or lowering posting swaps the connection's token in one step
under the door's lock: no call lands between the old token's going and
the new one's being named, the check that the connection is live and
the replacement are one hold of the principals (a revoke that came
first is not undone), and a revoke naming the token just replaced is
for the one that replaced it.
SCENARIOS:
1. A second sign-in of Claude is a new question and a new approval.
2. A refresh after ten days, and after a restart, still works.
3. Revoke in the list ends access and refresh, and a new Allow does not
revive them.
```
*Enforced:* `Remote::decide`, `Remote::make_approval`, `Remote::token_gone`, `Remote::set_post`, `Remote::successor_of`, `replace_token_if_live`, `Remote::mint`, `Remote::refresh`, `Remote::authenticate`, `Remote::save`.
*Checked:* `ledger-mcp` unit tests `an_allow_is_a_new_approval_with_exactly_the_permissions_ticked`, `revoke_then_allow_again_does_not_bring_the_old_tokens_back`, `raising_posting_is_one_step_and_a_revoke_during_it_wins`, `two_refreshes_at_once_never_both_succeed`, `a_code_used_twice_ends_what_it_made_and_so_does_a_refresh_token`, `revoking_the_connection_ends_the_access_token_and_the_refresh_token`, `refresh_tokens_survive_a_restart_and_only_their_hashes_are_kept`.
**WB-remote-7** A failure to sign in never bans an address; what a request creates is bounded.
```text
GOAL: a stranger can fill neither memory nor disk, and a vendor's shared
address is never shut out for another's failures.
SOURCE: plan/p15-app-and-relay.md section 4, rule 7 (the vendors' addresses
are shared; rate-limit per approval instead).
INVARIANTS:
1. Registered clients (2000 in all, thirty an hour from one address,
600 an hour in all), waiting requests (twenty, five per address, which
give way rather than turn a new one away), codes, access tokens,
refresh tokens, approvals, families, fetches of client documents (30 a
minute, 6 from one address) and the limiter's own table are all
bounded. An address is an IPv4 address, an IPv6 caller's /64, and an
IPv4-mapped address is the IPv4 one. When the table of per-address
limits is full (10,000) the entries that began longest ago go; a
connection's own limit (calls, token requests by client) is never
evicted. The token endpoint is limited by address (300 a minute) and,
once the client is known, by client (120 a minute).
2. A registration is held in memory only: it reaches `remote.json` when
the person allows a sign-in of it, and never for a stranger's
registration. A redirect address is at most 512 bytes and a client's
together at most 4 KiB. A registration nobody used for a sign-in goes
after an hour; the oldest unused goes first when the table is full;
none unused and full is a refusal. A revoked key frees its place among
the fifty.
3. Nothing bans an address; a refusal says to wait. An approval's own
calls are limited to 600 a minute, asked before the company is, and a
call refused for rate or for another company's books is counted, never
written to the books' record.
SCENARIOS:
1. Thirty-one registrations from one address: the thirty-first waits;
another address is not shut out.
2. Unused registrations expire.
```
*Enforced:* `Remote::allow`, `Remote::allow_address`, `address_key`, `Caller`, `Remote::make_room`, `Remote::sweep`, `Remote::make_key`, `Remote::count_refused`, `Remote::snapshot`.
*Checked:* `ledger-mcp` unit tests `registration_is_bounded_and_unused_registrations_expire`, `limits_count_an_ipv6_64_as_one_caller_and_a_mapped_address_as_its_ipv4`, `a_full_limiter_table_evicts_addresses_never_a_connections_own_limit`, `a_registration_nobody_allowed_never_reaches_the_disk`, `a_revoked_key_frees_its_place`, `refused_calls_are_counted_not_written_and_the_limit_comes_before_the_company`.
**WB-remote-8** A token for one company never reaches another, and a company not offered is not there.
```text
GOAL: the lock on the front door is per company.
SOURCE: D-78 in plan/p15-app-and-relay.md (a connection names its company and
is never redirected to another).
INVARIANTS:
1. An access token carries the company of its approval; at another
company's path it is a 403, counted and not written (a token holder
can't fill the books' record with refusals), after the call limit.
2. A company that is not offered (or not open) answers 404 on its path and
has no metadata.
3. A code and a refresh token carry their company and never make a token
for another.
SCENARIOS:
1. A token for acme at beta's path is 403; with beta not offered, 404.
```
*Enforced:* `door` (the server's `local` module), `Remote::is_allowed`, `resource_document`.
*Checked:* `ledger-mcp` unit test `a_token_for_one_company_never_reaches_another`.
**WB-remote-9** A web assistant may read and draft; posting is the owner's to turn on, per connection.
```text
GOAL: an assistant that also reads email, and can be steered by it, cannot
post by default.
SOURCE: D-86 in plan/p15-app-and-relay.md.
INVARIANTS:
1. An approval grants read and write (drafts), not post and never admin;
the access record (admin) is not reachable through this door whatever
the token says.
2. The owner raises or lowers posting per approval (RemoteSetPost): the
token that carries it is replaced by one with the new scopes in one
principals record, as one step (WB-remote-6), and the same access
tokens carry on.
3. A key made for an assistant that takes only a key (RemoteMakeKey) is a
principals token the door accepts, shown once, with the same scopes;
no other principals token is accepted here.
SCENARIOS:
1. A new connection can't post; raised, it can; lowered, it can't.
```
*Enforced:* `scopes_for`, `Remote::set_post`, `Remote::make_key`, `Remote::authenticate`.
*Checked:* `ledger-mcp` unit tests `the_default_is_read_and_draft_and_the_person_raises_it_to_post`, `an_unsigned_request_is_a_401_that_says_where_to_sign_in_initialize_included` (an agent's own key opens nothing here).
**WB-remote-10** A remote call is the same call: one write path, plain JSON, no session.
```text
GOAL: a write through the door is exactly once (D-88), and the door adds no
second way to the books.
SOURCE: D-88 in plan/p15-app-and-relay.md; WB-format-8 to 10.
INVARIANTS:
1. Every remote call is handled by the same server as a local one
(`call_tool`): a write without its request_key is refused, and a repeat
of a key returns the first answer and writes nothing. The remote door
has no tool of its own.
2. It is stateless and answers plain JSON: no session id is made, and one
a caller names is not heard.
SCENARIOS:
1. A draft without a key is refused; with one, twice, it is one draft and
the same answer.
```
*Enforced:* `mcp_via` (the server's `local` module), `call_tool` (the server's `BooksServer`).
*Checked:* `ledger-mcp` unit tests `a_remote_write_needs_its_request_key_and_a_repeat_returns_the_first_answer`, `a_registered_client_signs_in_and_reads_the_books`.