feat(host): a command that names a company opens it in its own server; a bridge says a server is still opening
A command with nothing running opens in its own server the one company it names (token create, check, revoke, back-up, upgrade, extract), so the owner is still told of a format upgrade that company waits for and the work that needs the company open can run. A bridge facing a server that holds the books but has no socket yet (the app opening large companies, or finishing a last backup) says it is still opening, not that Wanderbooks isn't running. The lib and commands spec blocks follow (WB-app-5). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
22f447c0ba
commit
d81f6d5c83
3 files changed
+94
-45
No files matched your search
@@ -61,8 +61,10 @@
|
|||||||
//! one). With nothing running, every call is answered "Wanderbooks
|
//! one). With nothing running, every call is answered "Wanderbooks
|
||||||
//! isn't running: start Wanderbooks and open BOOK (on a computer
|
//! isn't running: start Wanderbooks and open BOOK (on a computer
|
||||||
//! with no screen, run `wanderbooks serve`), then try again."
|
//! with no screen, run `wanderbooks serve`), then try again."
|
||||||
//! (`not_running_words`); with the app running but BOOK not open in
|
//! (`not_running_words`); with a server holding the books and no
|
||||||
//! it, "Company BOOK isn't open: open it in Wanderbooks."
|
//! socket yet (the app opening large companies), "Wanderbooks is
|
||||||
|
//! still opening its companies…: try again shortly."
|
||||||
|
//! (`opening_words`); with the app running but BOOK not open in it, "Company BOOK isn't open: open it in Wanderbooks."
|
||||||
//! (`not_open_words`, for the server's `closed` refusal at the
|
//! (`not_open_words`, for the server's `closed` refusal at the
|
||||||
//! door). Neither is final: the call after the app starts, or the
|
//! door). Neither is final: the call after the app starts, or the
|
||||||
//! company opens, goes through, in a session that was already
|
//! company opens, goes through, in a session that was already
|
||||||
@@ -108,6 +110,15 @@ fn not_running_words(book: &str) -> String {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// What the agent is told when a server holds the books and is not
|
||||||
|
/// answering yet: the app opening its companies (a large one takes a
|
||||||
|
/// while), or finishing a last backup (INVARIANT 12).
|
||||||
|
fn opening_words(book: &str) -> String {
|
||||||
|
format!(
|
||||||
|
"Wanderbooks is still opening its companies, or finishing the last backup of the last session: {book} can be reached in a moment. Try again shortly."
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// What the agent is told when the app runs but the company is not open
|
/// What the agent is told when the app runs but the company is not open
|
||||||
/// in it (INVARIANT 12).
|
/// in it (INVARIANT 12).
|
||||||
fn not_open_words(book: &str) -> String {
|
fn not_open_words(book: &str) -> String {
|
||||||
@@ -1091,7 +1102,7 @@ impl Bridge {
|
|||||||
async fn keep_in_app(self: Arc<Self>, run_dir: PathBuf) {
|
async fn keep_in_app(self: Arc<Self>, run_dir: PathBuf) {
|
||||||
let root = run_dir.parent().unwrap_or(&run_dir).to_path_buf();
|
let root = run_dir.parent().unwrap_or(&run_dir).to_path_buf();
|
||||||
let account = crate::tokens::Account::new(&root, &self.book, &self.token_name);
|
let account = crate::tokens::Account::new(&root, &self.book, &self.token_name);
|
||||||
let _ = self.token_file.set((account, run_dir));
|
let _ = self.token_file.set((account, run_dir.clone()));
|
||||||
let mut epoch = 0u64;
|
let mut epoch = 0u64;
|
||||||
loop {
|
loop {
|
||||||
if matches!(&*self.link.borrow(), Link::Opening) {
|
if matches!(&*self.link.borrow(), Link::Opening) {
|
||||||
@@ -1101,8 +1112,14 @@ impl Bridge {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Err(Owner::Unreachable) => {
|
Err(Owner::Unreachable) => {
|
||||||
self.link
|
// A server holding the books with no socket yet is
|
||||||
.send_replace(Link::Refused(not_running_words(&self.book)));
|
// opening (or finishing), not absent.
|
||||||
|
let words = if crate::opener::server_lock_held(&run_dir) {
|
||||||
|
opening_words(&self.book)
|
||||||
|
} else {
|
||||||
|
not_running_words(&self.book)
|
||||||
|
};
|
||||||
|
self.link.send_replace(Link::Refused(words));
|
||||||
}
|
}
|
||||||
Ok(()) => match self.open_session(epoch + 1).await {
|
Ok(()) => match self.open_session(epoch + 1).await {
|
||||||
Ok(up) => {
|
Ok(up) => {
|
||||||
@@ -2691,6 +2708,26 @@ mod against_a_server {
|
|||||||
keeper.abort();
|
keeper.abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn with_a_server_still_opening_the_answer_says_so_and_not_that_nothing_runs() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
// A server holds the books and has bound no socket yet.
|
||||||
|
let lock = std::fs::File::create(dir.path().join(ledger_mcp::local::SERVER_LOCK)).unwrap();
|
||||||
|
lock.lock().unwrap();
|
||||||
|
let (b, mut rx) = app_bridge_at(&dir.path().join("mcp.sock"), knobs(3000, 3000));
|
||||||
|
let keeper = tokio::spawn(b.clone().keep_in_app(dir.path().to_path_buf()));
|
||||||
|
let call: Value = serde_json::from_str(CALL).unwrap();
|
||||||
|
b.waiting(&json!(7));
|
||||||
|
b.clone().request_on(call, json!(7), "tools/call").await;
|
||||||
|
let said = told(&mut rx, 3000).await.unwrap()["error"]["message"]
|
||||||
|
.as_str()
|
||||||
|
.unwrap()
|
||||||
|
.to_string();
|
||||||
|
assert_eq!(said, opening_words("acme"));
|
||||||
|
assert!(!said.contains("isn't running"));
|
||||||
|
keeper.abort();
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn with_the_app_running_a_company_that_is_not_open_is_said_in_words() {
|
async fn with_the_app_running_a_company_that_is_not_open_is_said_in_words() {
|
||||||
let server = Server::start();
|
let server = Server::start();
|
||||||
|
|||||||
@@ -7,17 +7,27 @@
|
|||||||
//!
|
//!
|
||||||
//! ```text
|
//! ```text
|
||||||
//! GOAL: from a terminal, or the viewer's menu, the owner holds the books
|
//! GOAL: from a terminal, or the viewer's menu, the owner holds the books
|
||||||
//! open, closes them, makes, adopts, backs up and restores books and
|
//! open (`serve`), closes them, makes, adopts, backs up and restores
|
||||||
//! connects agents, without ever naming or touching the books'
|
//! books and connects agents, without ever naming or touching the
|
||||||
//! private folder.
|
//! books' private folder, and without any command leaving a host or a
|
||||||
|
//! server running that nothing manages.
|
||||||
//! SOURCE: plan/served.md 3.9 (a terminal running `wanderbooks serve`, and
|
//! SOURCE: plan/served.md 3.9 (a terminal running `wanderbooks serve`, and
|
||||||
//! any command that needs the server, holds a lease; "Close the
|
//! any command that needs a host, holds a lease), 3.1 (the CLI
|
||||||
//! books" ends every lease), 3.1 (the CLI talks to admin.sock and
|
//! talks to admin.sock and never reads the books), 5.5 (adopt;
|
||||||
//! never reads the books), 5.5 (adopt; preview and confirmation at
|
//! preview and confirmation at the terminal);
|
||||||
//! the terminal).
|
//! plan/p15-app-and-relay.md 3.1 (the app is the host; only `serve`
|
||||||
|
//! keeps a detached host), WB-app-5.
|
||||||
//! INVARIANTS:
|
//! INVARIANTS:
|
||||||
//! 1. A command that needs the server holds a lease while it runs, and
|
//! 1. A command that needs the server finds what holds the books: a
|
||||||
//! lets go when it ends.
|
//! host (it holds a lease while it runs, and lets go when it ends),
|
||||||
|
//! the app's server (no lease: the app decides when the books
|
||||||
|
//! close), or nothing. With nothing running it starts no host: a
|
||||||
|
//! command that only reads, or manages the books' files, runs a
|
||||||
|
//! server of its own for the length of the work and closes it
|
||||||
|
//! (waiting for its last backup); one that needs a company held open
|
||||||
|
//! says "Start Wanderbooks first" ([`Direct`], [`reach`]; WB-app-5).
|
||||||
|
//! A server of its own takes the locks the app's server takes, so
|
||||||
|
//! two programs never hold one set of books.
|
||||||
//! 2. Nothing printed names the data root.
|
//! 2. Nothing printed names the data root.
|
||||||
//! 3. Adoption and restoring are previewed first and go ahead only on a
|
//! 3. Adoption and restoring are previewed first and go ahead only on a
|
||||||
//! yes at the terminal, or --yes; a vault beside the books must be
|
//! yes at the terminal, or --yes; a vault beside the books must be
|
||||||
@@ -31,8 +41,7 @@
|
|||||||
//! makes the books' agent token only when there is no token file for
|
//! makes the books' agent token only when there is no token file for
|
||||||
//! it, replacing the server's (no one could be using it: the bridge
|
//! it, replacing the server's (no one could be using it: the bridge
|
||||||
//! reads its token from this file).
|
//! reads its token from this file).
|
||||||
//! 5. The viewer's hold lets go only when the owner closes the books;
|
//! 5. (Retired with the viewer's lease: the app is the host.)
|
||||||
//! books that stop, or won't open, are tried again.
|
|
||||||
//! 6. Books waiting for a format upgrade (plan 3.9, D-39): the owner's
|
//! 6. Books waiting for a format upgrade (plan 3.9, D-39): the owner's
|
||||||
//! commands that open the books (`serve`, `books`, `token`,
|
//! commands that open the books (`serve`, `books`, `token`,
|
||||||
//! `connect`) ask for the go-ahead once each time they run, at a
|
//! `connect`) ask for the go-ahead once each time they run, at a
|
||||||
@@ -40,7 +49,8 @@
|
|||||||
//! the foreground: the question, then the owner passphrase, which only
|
//! the foreground: the question, then the owner passphrase, which only
|
||||||
//! the server checks); elsewhere they only say so, on stderr. Only a passphrase typed
|
//! the server checks); elsewhere they only say so, on stderr. Only a passphrase typed
|
||||||
//! at a terminal (or in the viewer) is sent, through the lease
|
//! at a terminal (or in the viewer) is sent, through the lease
|
||||||
//! socket's consent_format_upgrade; declining changes nothing.
|
//! socket's consent_format_upgrade (a host), or as the admin request
|
||||||
|
//! `UpgradeFormat` (no host); declining changes nothing.
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
use crate::knobs::{FLAG, Knobs};
|
use crate::knobs::{FLAG, Knobs};
|
||||||
@@ -163,17 +173,17 @@ enum Direct {
|
|||||||
fn direct_for(request: &Request) -> Direct {
|
fn direct_for(request: &Request) -> Direct {
|
||||||
match request {
|
match request {
|
||||||
// Opening and closing a company in a running server mean nothing
|
// Opening and closing a company in a running server mean nothing
|
||||||
// with none running; the sessions and the backfill are of a
|
// with none running, and the sessions are of a running server's.
|
||||||
// running server's.
|
Request::OpenBook { .. } | Request::CloseBook { .. } | Request::Sessions => Direct::No,
|
||||||
Request::OpenBook { .. }
|
// Work on one company, which the server opens (so the owner is
|
||||||
| Request::CloseBook { .. }
|
// told of a format upgrade it waits for, INVARIANT 6).
|
||||||
| Request::Sessions
|
Request::BackUpNow { id, .. }
|
||||||
| Request::Extract { .. } => Direct::No,
|
| Request::UpgradeFormat { id, .. }
|
||||||
// Work on an open company.
|
| Request::Extract { id, .. } => Direct::With(vec![id.clone()]),
|
||||||
Request::BackUpNow { id, .. } | Request::UpgradeFormat { id, .. } => {
|
Request::TokenCreate { book, .. }
|
||||||
Direct::With(vec![id.clone()])
|
| Request::TokenCheck { book, .. }
|
||||||
}
|
| Request::TokenRevoke { book, .. } => Direct::With(vec![book.clone()]),
|
||||||
// The rest read the list, or make and move files and tokens.
|
// The rest read the list, or make and move files.
|
||||||
_ => Direct::With(Vec::new()),
|
_ => Direct::With(Vec::new()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -303,12 +313,9 @@ fn with_server_as(
|
|||||||
let reached = reach(&mut opener, common, &direct).await?;
|
let reached = reach(&mut opener, common, &direct).await?;
|
||||||
let run_dir = reached.run_dir.clone();
|
let run_dir = reached.run_dir.clone();
|
||||||
// The books are open: asked about a format upgrade first
|
// The books are open: asked about a format upgrade first
|
||||||
// (INVARIANT 6). (A server of the command's own has only what
|
// (INVARIANT 6). (A server of the command's own has only the
|
||||||
// the command named open: nothing waits there to be offered.)
|
// company the command names open.)
|
||||||
if common.ask_upgrades
|
if common.ask_upgrades && !ASKED_UPGRADES.swap(true, std::sync::atomic::Ordering::SeqCst) {
|
||||||
&& reached.own.is_none()
|
|
||||||
&& !ASKED_UPGRADES.swap(true, std::sync::atomic::Ordering::SeqCst)
|
|
||||||
{
|
|
||||||
let run = run_dir.clone();
|
let run = run_dir.clone();
|
||||||
let _ = tokio::task::spawn_blocking(move || offer_upgrades(&run)).await;
|
let _ = tokio::task::spawn_blocking(move || offer_upgrades(&run)).await;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,20 @@
|
|||||||
//! The books open while something holds them (plan 3.9, D-21, D-23).
|
//! The books open while something holds them (plan 3.9, D-21, D-23; plan
|
||||||
|
//! P15 3.1, D-77, D-84).
|
||||||
//!
|
//!
|
||||||
//! Nothing runs in the background: the first holder (the app, an agent's
|
//! On the desktop the app is the host: [`app_server`] starts the books
|
||||||
//! bridge, `wanderbooks serve`, or a command that needs the books) starts
|
//! server as the window's child and closes it by closing its input.
|
||||||
//! the **host**, which starts the books server and holds it open while
|
//! Nothing else on the desktop starts a detached host. The headless
|
||||||
//! any lease on its socket lasts, and closes it 10 s after the last one
|
//! `wanderbooks serve` keeps one, as before: its host starts the books
|
||||||
//! ends. Each part is a mode of the one `wanderbooks` executable:
|
//! server and holds it open while any lease on its socket lasts, and
|
||||||
//! [`host::run`] (`wanderbooks host`), [`bridge::run`] (`wanderbooks
|
//! closes it 10 s after the last one ends. Each part is a mode of the one
|
||||||
//! mcp`), and the owner's commands in [`commands`] (`serve`, `close`,
|
//! `wanderbooks` executable: [`host::run`] (`wanderbooks host`),
|
||||||
//! `books`, `token`, `connect`, `doctor`, `passphrase`, and what the
|
//! [`bridge::run`] (`wanderbooks mcp`, a line to whatever runs, in words
|
||||||
//! viewer's Books menu does). Agents' tokens are kept in files under the data root
|
//! when nothing does), and the owner's commands in [`commands`] (`serve`,
|
||||||
//! ([`tokens`]).
|
//! `close`, `books`, `token`, `connect`, `doctor`, `passphrase`, and what
|
||||||
|
//! the viewer's Books menu does), which talk to the running app's (or
|
||||||
|
//! host's) server, or with nothing running run a server of their own for
|
||||||
|
//! the length of the work, or say "Start Wanderbooks first". Agents'
|
||||||
|
//! tokens are kept in files under the data root ([`tokens`]).
|
||||||
|
|
||||||
pub mod app_server;
|
pub mod app_server;
|
||||||
pub mod bridge;
|
pub mod bridge;
|
||||||
|
|||||||
Reference in new issue
Block a user