fix(mcp): a close waits for the answers on their way out, and test it under a hammer

The first run of the hammer (agents calling two books while one is closed and
reopened over and over) found a real race: an MCP request's handler returns
as soon as the session's task has the request, and the tool's answer streams
back afterwards. A close that waited only for handlers and running tools
could end the sessions while an answer was still on its way, so a call that
did not run (refused as closing) reached its client as neither answered nor
refused. The gate now also counts answers on their way out (Answering,
carried in the response body as the server's own close counts them), and a
close waits for them, for ANSWER_GRACE at most, before it ends the sessions.

The hammer (tests/book_open_close.rs) replays both journals afterwards:
every acknowledged draft is there exactly once under the id it was told, no
refused call left anything, the closed book's journal and access log never
moved between the close answer and its reopening, the other book never
refused a call and kept answering while the first was closed. Beside it: a
server started with --open, the sessions request over the real socket, the
doctor's lines, and the docs (mcp-interface, architecture, testing).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-20 09:23:39 -07:00
1 parent f7f75fb380
commit 89403a7159
10 files changed
+802 -34

No files matched your search

+9
View File
@@ -287,6 +287,15 @@ re-verifies the journal.
## The local transport and the Windows arms (`ledger-mcp::ipc`, `ledger-store::winsec`, P11c, D-62)
The books server opens the books of the data root at its start (all, or the
ones `--open` names), and opens or closes **one** while it runs (admin
requests `open_book` and `close_book`, `ledger-mcp/src/local.rs`
INVARIANT 10; plan/p15-app-and-relay.md 3.1): each open book has a gate
every request enters before it touches the book, which a close shuts and
waits empty, so the app's tab for a company can be its book's open state.
`sessions.rs` keeps the per-book board of who is connected (the admin
`sessions` request).
The books server, the host and everything that talks to them meet on
channels only this account can open (plan 3.9). `ledger_mcp::ipc` is the
one seam: `Endpoint` (a socket in the root's `run/` on Unix; the pipe
+45 -1
View File
@@ -267,7 +267,10 @@ reach through the bridge.
- **Served books** (`wanderbooks server`, started by the host; an agent
connects with `wanderbooks mcp --book ID`, the bridge: stdio to the
agent, the server's socket upstream): every book
in the data root, on a socket in the root's private `run/` folder that
in the data root that is open (all of them at the start, unless
`wanderbooks server --open none|ID[,ID…]` says otherwise; one is opened
or closed while the server runs, "Opening and closing one book" below),
on a socket in the root's private `run/` folder that
only this account can connect to. Every request carries a bearer token
live for that book (refused 401 without one, 403 with another book's).
Refusals at the door, tool calls, uploads, downloads and exports are
@@ -275,6 +278,12 @@ reach through the bridge.
book stops taking requests (an upload, download or export is then
refused, 503).
- `/b/{book}/mcp`: the book's MCP service. A request is at most 1 MiB.
- A book that exists but is **closed** is refused on every route below
and on this one: 503 with `x-wanderbooks-refused: closed` and the words
`The books "ID" aren't open: open them in Wanderbooks.` (the bridge
hands them to the agent as they are). A book that doesn't exist is 404
`no-such-books`; one that failed to open is 503 `not-open` with why.
Nothing of a closed book is written, its access log included.
- `PUT /b/{book}/u/{upload}`: an upload's bytes (`books.begin_upload`),
from the token that began it; what `wanderbooks upload --book B
[--token NAME] UPLOAD FILE` sends. Streamed to the disk, kept only if
@@ -331,6 +340,41 @@ reach through the bridge.
hidden file and hard-linked in: never a partial file under the real
name, never over another file.
### Opening and closing one book (plan/p15-app-and-relay.md 3.1, D-77, D-78)
The desktop app has one tab per company, and a company is reachable by
agents exactly while its tab is open, so the books server opens and closes
**one** book while it runs. These are requests on the admin socket (the
command line sends them: `wanderbooks books open ID`, `wanderbooks books
close ID`; `books list` and `doctor` show each book as open, closed, or
didn't open, with why). Protocol version 1: the requests are new, the
`closed` flag of a listed book defaults to false.
- `open_book { id }` opens a closed (or failed) book of the data root with
exactly the checks of the server's start (verified, a drifted database
rebuilt, a format upgrade waited for as at the start). Answers `opened`,
`already_open`, `no_such_book`, or `could_not_open` with why (the book
stays listed, with that `problem`).
- `close_book { id }` answers `closed`, `was_not_open` or `no_such_book`.
In this order: new calls on the book are refused (the door is shut and
the book listed closed); the calls running on it finish, on every route
(nothing is killed, nothing is half-done); its MCP sessions end (an agent's next request on one
gets the "isn't open" words above, and after a reopening a 404 "Session
not found", which a client takes as "open a new one"); its last backup is
made, as at the server's close; its store is let go, releasing the
writer lock, so another process could open the book. Only then is
"closed" answered. Every other book keeps serving, and none of its
calls waits for this close. For every call, either it ran completely
and is in the journal, or it was refused unrun and nothing of it is; after
the answer, no call on that book runs until it is opened again.
- `sessions` lists, for each open book, its live MCP sessions: the token's
**name** (never the token, its text or its hash), the client's name and
version from `initialize`, the agent's self-report (`books.start`'s
`agent`, if it made one), when it connected, how many of its calls are
running now, and its last tool with its outcome and time. A session
that ended is not listed; nothing is kept across a server restart.
`wanderbooks doctor` prints these lines under "Assistants connected".
## Server instructions
The MCP `initialize` response carries an `instructions` string, the
+29
View File
@@ -4290,6 +4290,35 @@ that make a group, the reversal skip, the `total > threshold` end),
`audit::check_split` (`> unit`, not `>=`), `AttestationBar::parse`'s range
(1..=64), `reconcilable::reconciliation_due` (`<=` through, `<` reconciled).
## P15 A1: open and close one book, and who is connected (2026-11-20)
The server opens and closes one book while it runs (`ledger-mcp/src/local.rs`
INVARIANTS 3, 10 to 12; `admin.rs` INVARIANTS 8 and 9; `sessions.rs`). The
rule the tests prove: for every call, either it ran completely and is in
the journal, or it was refused and nothing of it is; after a close is
answered no call on that book runs until it is opened again; and a close of
one book never refuses or holds up a call on another.
| Test | What it proves |
|---|---|
| `local::tests::a_closed_book_is_refused_as_closed_and_the_other_still_serves` | a closed book's door says `closed` with the words an agent can relay, a name that is no book says `no-such-books`, the other book still passes the door, the list shows closed (not a problem), closing twice is `was_not_open`, opening is `opened` then `already_open`, a name that is no book is `no_such_book` |
| `local::tests::a_closed_book_can_be_opened_by_another_program_and_not_by_two` | a close releases the store's writer lock (another `Store::open` succeeds); while another holds it, opening says `another program has these books open` and the book is listed with that problem; once it lets go it opens |
| `local::tests::a_close_waits_for_the_requests_inside_and_refuses_the_next` | a close answers only after a request already inside the book's gate has left; a request arriving meanwhile is refused as closed at once; another book's door is not held up |
| `local::tests::the_books_open_at_the_start_are_chosen_by_option` | `--open` takes `all`, `none`, or ids, and refuses a bad id |
| `local::tests::the_sessions_request_says_who_is_connected_and_never_their_token` | the sessions list: the token's name, the client from `initialize` (known before any call), the agent's self-report, the last call; no token text; an ended session is not listed; a book's sessions end with it and a reopened book has none |
| `sessions::tests` (3) | the running count rides in the call's guard; the last call, the client and the agent are kept, masked of any full TIN; a note for an ended session is dropped |
| `book_open_close::books_left_closed_at_the_start_say_so_and_open_when_asked` | a real server started with `--open BOOK`: the other book is listed closed and says so on the MCP, download, export and upload routes; a name that is no book is 404; opening serves |
| `book_open_close::the_open_option_takes_none_and_refuses_a_book_that_doesnt_exist` | `--open none` leaves every book closed; `--open nobody` exits 2 before opening anything |
| `book_open_close::the_sessions_request_names_each_connection_and_never_its_token` | over the real socket: two tokens, two clients, an agent's report, neither token's text nor SHA-256 in the answer, an ended session gone, a close ends the book's sessions |
| `book_open_close::closing_and_opening_a_book_over_and_over_loses_and_duplicates_nothing` | the hammer: 6 agents on one book and 4 on another make journaled drafts and reads while the first is closed and reopened 12 times; both journals are replayed afterwards: every acknowledged draft is in its journal exactly once under the id it was told, no refused call left anything, the closed book's journal and access log never moved between the close answer and the reopening, the other book never refused a call and kept answering while the first was closed, and the books replay with gapless ids |
| `commands::tests::doctor_says_who_is_connected` | the doctor's lines for sessions |
**For P12 (mutation targets).** `Gate::enter` (count before looking at `shut`),
`Books::close_one`'s order (shut and list closed, `SharedStore::close`, the
gate emptied, sessions ended, the last backup, `let_go`), `door`'s entering
before it touches the book, and the gate's riding in the upload and
download tasks.
## P12: the checklists
The **For P12** paragraphs above (kept where they were written, with their