feat(mcp): open and close one book while the server runs, and say who is connected
The desktop app is a window with a tab per company (plan/p15-app-and-relay.md 3.1, D-77, D-78): a company must be reachable by agents exactly while its tab is open, so the books server can now open and close ONE book at run time instead of opening every book at start and closing only with the process. Close is ordered so that a call either ran completely (and is in the journal) or was refused unrun: the book's door (a per-book gate every route enters before it touches the book, its access log included) is shut and the book is listed closed, so no new call gets in; SharedStore::close stops tools starting and waits for those that did; the gate is waited empty for the upload, download, export and MCP handlers inside; sessions end; the last backup of the server's own close runs; and the store is let go, releasing its writer lock. Only then is 'closed' answered. Another book's door, store and sessions are never touched or waited on. A closed book is a state of its own (not a problem): listed closed, and a call for it is told apart from one for a book that does not exist, on the MCP, upload, download and export routes, in words an agent can relay. Start gains --open all|none|IDS (default all). Sessions: a small in-memory board per book (sessions.rs) keeps what the client said at initialize, the agent's self-report, calls running and the last call, joined with the sessions the server already binds to tokens; the admin 'sessions' request lists the token's NAME, never its text or hash, and doctor prints it. Backups of a book let go push and record nothing more. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
526c27233e
commit
f7f75fb380
10 files changed
+1414
-97
No files matched your search
@@ -474,10 +474,18 @@ fn books_usage(prog: &str) {
|
||||
eprintln!(" {prog} back-up ID [--new-disk]");
|
||||
eprintln!(" {prog} upgrade ID");
|
||||
eprintln!(" {prog} extract BOOK ATTACHMENT");
|
||||
eprintln!(" {prog} open ID");
|
||||
eprintln!(" {prog} close ID");
|
||||
eprintln!(
|
||||
" {prog} put-aside ID DATA-DIR [--vault DIR --vault-key FILE] [PATH...] [--yes]"
|
||||
);
|
||||
eprintln!(" list the books, and whether each is open");
|
||||
eprintln!(" list the books, and whether each is open or closed");
|
||||
eprintln!(
|
||||
" open open one closed book while the server runs, checked as at its start (agents can reach it from then)"
|
||||
);
|
||||
eprintln!(
|
||||
" close close one book while the server runs: its running calls finish, its sessions end, it is backed up and let go; the others keep serving"
|
||||
);
|
||||
eprintln!(" create start new, empty books (base currency USD unless given)");
|
||||
eprintln!(" adopt copy existing books in, verified; the original is left as it was");
|
||||
eprintln!(
|
||||
@@ -604,6 +612,18 @@ pub fn books(
|
||||
};
|
||||
return put_aside(&common, book, old, yes);
|
||||
}
|
||||
["open", book] => with_server(
|
||||
&common,
|
||||
Request::OpenBook {
|
||||
id: (*book).to_string(),
|
||||
},
|
||||
),
|
||||
["close", book] => with_server(
|
||||
&common,
|
||||
Request::CloseBook {
|
||||
id: (*book).to_string(),
|
||||
},
|
||||
),
|
||||
["back-up", book] => {
|
||||
return match back_up_now(&common, book, new_disk) {
|
||||
Ok(lines) => {
|
||||
@@ -666,6 +686,44 @@ pub fn books(
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Ok(Response::OpenBook { id, outcome }) => match outcome {
|
||||
admin::OpenOutcome::Opened => {
|
||||
println!("Opened the books \"{id}\": agents can reach them now.");
|
||||
Ok(())
|
||||
}
|
||||
admin::OpenOutcome::AlreadyOpen => {
|
||||
println!("The books \"{id}\" are open already.");
|
||||
Ok(())
|
||||
}
|
||||
admin::OpenOutcome::NoSuchBook => {
|
||||
eprintln!(
|
||||
"error: there are no books called \"{id}\": `wanderbooks books list` lists them"
|
||||
);
|
||||
Err(exit(1))
|
||||
}
|
||||
admin::OpenOutcome::CouldNotOpen(why) => {
|
||||
eprintln!("error: the books \"{id}\" couldn't be opened: {why}");
|
||||
Err(exit(1))
|
||||
}
|
||||
},
|
||||
Ok(Response::CloseBook { id, outcome }) => match outcome {
|
||||
admin::CloseOutcome::Closed => {
|
||||
println!(
|
||||
"Closed the books \"{id}\": nothing is running on them, their sessions ended, and they are let go."
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
admin::CloseOutcome::WasNotOpen => {
|
||||
println!("The books \"{id}\" weren't open.");
|
||||
Ok(())
|
||||
}
|
||||
admin::CloseOutcome::NoSuchBook => {
|
||||
eprintln!(
|
||||
"error: there are no books called \"{id}\": `wanderbooks books list` lists them"
|
||||
);
|
||||
Err(exit(1))
|
||||
}
|
||||
},
|
||||
Ok(Response::Refused(why)) | Err(why) => {
|
||||
eprintln!("error: {why}");
|
||||
Err(exit(1))
|
||||
@@ -679,6 +737,12 @@ pub fn books(
|
||||
|
||||
/// One book, as `books list` and `doctor` show it.
|
||||
fn book_line(b: &admin::BookStatus) -> String {
|
||||
if b.closed {
|
||||
return format!(
|
||||
"{} closed (`wanderbooks books open {}` opens it)",
|
||||
b.id, b.id
|
||||
);
|
||||
}
|
||||
match (&b.problem, &b.format_upgrade) {
|
||||
(Some(why), _) => format!("{} didn't open: {why}", b.id),
|
||||
(None, Some(_)) => format!(
|
||||
@@ -689,6 +753,54 @@ fn book_line(b: &admin::BookStatus) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// `doctor`'s lines for who is connected to each open book: the token's
|
||||
/// name, the client and agent as they said, when it connected, the calls
|
||||
/// running and the last call.
|
||||
fn session_lines(open: &[admin::BookSessions]) -> Vec<String> {
|
||||
let mut lines = Vec::new();
|
||||
if open.iter().all(|b| b.sessions.is_empty()) {
|
||||
lines.push("Assistants connected: none.".to_string());
|
||||
return lines;
|
||||
}
|
||||
lines.push("Assistants connected:".to_string());
|
||||
for b in open {
|
||||
if b.sessions.is_empty() {
|
||||
lines.push(format!(" {}: none", b.book));
|
||||
continue;
|
||||
}
|
||||
lines.push(format!(" {}:", b.book));
|
||||
for s in &b.sessions {
|
||||
let client = match (&s.client_name, &s.client_version) {
|
||||
(Some(n), Some(v)) => format!("{n} {v}"),
|
||||
(Some(n), None) => n.clone(),
|
||||
_ => "a client that hasn't said who it is".to_string(),
|
||||
};
|
||||
let mut line = format!(" token {:?}, {client}", s.token);
|
||||
if let Some(a) = &s.agent {
|
||||
let said: Vec<&str> = [&a.model, &a.reasoning_effort, &a.harness]
|
||||
.into_iter()
|
||||
.filter_map(|v| v.as_deref())
|
||||
.collect();
|
||||
if !said.is_empty() {
|
||||
line.push_str(&format!(" (says: {})", said.join(", ")));
|
||||
}
|
||||
}
|
||||
line.push_str(&format!(", connected {}", s.connected_at));
|
||||
line.push_str(&match s.running {
|
||||
0 => ", no call running".to_string(),
|
||||
1 => ", 1 call running".to_string(),
|
||||
n => format!(", {n} calls running"),
|
||||
});
|
||||
match &s.last {
|
||||
Some(l) => line.push_str(&format!(", last {} ({}) at {}", l.tool, l.outcome, l.at)),
|
||||
None => line.push_str(", no call yet"),
|
||||
}
|
||||
lines.push(line);
|
||||
}
|
||||
}
|
||||
lines
|
||||
}
|
||||
|
||||
/// The projection database a book uses, and the other schemas' files
|
||||
/// beside it (left as they are for the versions that wrote them, never
|
||||
/// deleted), as `doctor` says them; None when it didn't open.
|
||||
@@ -2499,6 +2611,18 @@ pub fn doctor(
|
||||
Ok(admin::Response::Refused(why)) => println!("Agents' tokens: {why}"),
|
||||
Ok(_) | Err(_) => {}
|
||||
}
|
||||
// Who is connected to each open book, and what it is doing.
|
||||
match admin::call_within(&socket, Request::Sessions, Some(DOCTOR_WAIT)) {
|
||||
Ok(admin::Response::Sessions(open)) => {
|
||||
for line in session_lines(&open) {
|
||||
println!("{line}");
|
||||
}
|
||||
}
|
||||
Ok(admin::Response::Refused(why)) => {
|
||||
println!("Assistants connected: {why}")
|
||||
}
|
||||
Ok(_) | Err(_) => {}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("The books are opening (the books server isn't ready yet).");
|
||||
@@ -3516,6 +3640,7 @@ mod tests {
|
||||
|problem: Option<&str>, waiting: Option<admin::FormatWaiting>| admin::BookStatus {
|
||||
id: "acme".into(),
|
||||
open: problem.is_none(),
|
||||
closed: false,
|
||||
problem: problem.map(str::to_string),
|
||||
format_upgrade: waiting,
|
||||
database: None,
|
||||
@@ -3566,6 +3691,7 @@ mod tests {
|
||||
let mut b = admin::BookStatus {
|
||||
id: "acme".into(),
|
||||
open: true,
|
||||
closed: false,
|
||||
problem: None,
|
||||
format_upgrade: None,
|
||||
database: Some("wanderbooks.v13.db".into()),
|
||||
|
||||
Reference in new issue
Block a user