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

+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