diff --git a/crates/ledger-host/src/bridge.rs b/crates/ledger-host/src/bridge.rs index 91b25088..6cda67f8 100644 --- a/crates/ledger-host/src/bridge.rs +++ b/crates/ledger-host/src/bridge.rs @@ -61,8 +61,10 @@ //! one). With nothing running, every call is answered "Wanderbooks //! isn't running: start Wanderbooks and open BOOK (on a computer //! with no screen, run `wanderbooks serve`), then try again." -//! (`not_running_words`); with the app running but BOOK not open in -//! it, "Company BOOK isn't open: open it in Wanderbooks." +//! (`not_running_words`); with a server holding the books and no +//! 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 //! door). Neither is final: the call after the app starts, or the //! 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 /// in it (INVARIANT 12). fn not_open_words(book: &str) -> String { @@ -1091,7 +1102,7 @@ impl Bridge { async fn keep_in_app(self: Arc, run_dir: PathBuf) { 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 _ = self.token_file.set((account, run_dir)); + let _ = self.token_file.set((account, run_dir.clone())); let mut epoch = 0u64; loop { if matches!(&*self.link.borrow(), Link::Opening) { @@ -1101,8 +1112,14 @@ impl Bridge { return; } Err(Owner::Unreachable) => { - self.link - .send_replace(Link::Refused(not_running_words(&self.book))); + // A server holding the books with no socket yet is + // 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(up) => { @@ -2691,6 +2708,26 @@ mod against_a_server { 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] async fn with_the_app_running_a_company_that_is_not_open_is_said_in_words() { let server = Server::start(); diff --git a/crates/ledger-host/src/commands.rs b/crates/ledger-host/src/commands.rs index 107cf8dd..c54c8285 100644 --- a/crates/ledger-host/src/commands.rs +++ b/crates/ledger-host/src/commands.rs @@ -7,17 +7,27 @@ //! //! ```text //! 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 -//! connects agents, without ever naming or touching the books' -//! private folder. +//! open (`serve`), closes them, makes, adopts, backs up and restores +//! books and connects agents, without ever naming or touching the +//! 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 -//! any command that needs the server, holds a lease; "Close the -//! books" ends every lease), 3.1 (the CLI talks to admin.sock and -//! never reads the books), 5.5 (adopt; preview and confirmation at -//! the terminal). +//! any command that needs a host, holds a lease), 3.1 (the CLI +//! talks to admin.sock and never reads the books), 5.5 (adopt; +//! preview and confirmation at 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: -//! 1. A command that needs the server holds a lease while it runs, and -//! lets go when it ends. +//! 1. A command that needs the server finds what holds the books: a +//! 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. //! 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 @@ -31,8 +41,7 @@ //! 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 //! reads its token from this file). -//! 5. The viewer's hold lets go only when the owner closes the books; -//! books that stop, or won't open, are tried again. +//! 5. (Retired with the viewer's lease: the app is the host.) //! 6. Books waiting for a format upgrade (plan 3.9, D-39): the owner's //! commands that open the books (`serve`, `books`, `token`, //! `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 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 -//! 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}; @@ -163,17 +173,17 @@ enum Direct { fn direct_for(request: &Request) -> Direct { match request { // Opening and closing a company in a running server mean nothing - // with none running; the sessions and the backfill are of a - // running server's. - Request::OpenBook { .. } - | Request::CloseBook { .. } - | Request::Sessions - | Request::Extract { .. } => Direct::No, - // Work on an open company. - Request::BackUpNow { id, .. } | Request::UpgradeFormat { id, .. } => { - Direct::With(vec![id.clone()]) - } - // The rest read the list, or make and move files and tokens. + // with none running, and the sessions are of a running server's. + Request::OpenBook { .. } | Request::CloseBook { .. } | Request::Sessions => Direct::No, + // Work on one company, which the server opens (so the owner is + // told of a format upgrade it waits for, INVARIANT 6). + Request::BackUpNow { id, .. } + | Request::UpgradeFormat { id, .. } + | Request::Extract { id, .. } => Direct::With(vec![id.clone()]), + Request::TokenCreate { book, .. } + | Request::TokenCheck { book, .. } + | Request::TokenRevoke { book, .. } => Direct::With(vec![book.clone()]), + // The rest read the list, or make and move files. _ => Direct::With(Vec::new()), } } @@ -303,12 +313,9 @@ fn with_server_as( let reached = reach(&mut opener, common, &direct).await?; let run_dir = reached.run_dir.clone(); // The books are open: asked about a format upgrade first - // (INVARIANT 6). (A server of the command's own has only what - // the command named open: nothing waits there to be offered.) - if common.ask_upgrades - && reached.own.is_none() - && !ASKED_UPGRADES.swap(true, std::sync::atomic::Ordering::SeqCst) - { + // (INVARIANT 6). (A server of the command's own has only the + // company the command names open.) + if common.ask_upgrades && !ASKED_UPGRADES.swap(true, std::sync::atomic::Ordering::SeqCst) { let run = run_dir.clone(); let _ = tokio::task::spawn_blocking(move || offer_upgrades(&run)).await; } diff --git a/crates/ledger-host/src/lib.rs b/crates/ledger-host/src/lib.rs index e49ba2d4..bd6bfb2a 100644 --- a/crates/ledger-host/src/lib.rs +++ b/crates/ledger-host/src/lib.rs @@ -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 -//! bridge, `wanderbooks serve`, or a command that needs the books) starts -//! the **host**, which starts the books server and holds it open while -//! any lease on its socket lasts, and closes it 10 s after the last one -//! ends. Each part is a mode of the one `wanderbooks` executable: -//! [`host::run`] (`wanderbooks host`), [`bridge::run`] (`wanderbooks -//! mcp`), and the owner's commands in [`commands`] (`serve`, `close`, -//! `books`, `token`, `connect`, `doctor`, `passphrase`, and what the -//! viewer's Books menu does). Agents' tokens are kept in files under the data root -//! ([`tokens`]). +//! On the desktop the app is the host: [`app_server`] starts the books +//! server as the window's child and closes it by closing its input. +//! Nothing else on the desktop starts a detached host. The headless +//! `wanderbooks serve` keeps one, as before: its host starts the books +//! server and holds it open while any lease on its socket lasts, and +//! closes it 10 s after the last one ends. Each part is a mode of the one +//! `wanderbooks` executable: [`host::run`] (`wanderbooks host`), +//! [`bridge::run`] (`wanderbooks mcp`, a line to whatever runs, in words +//! when nothing does), and the owner's commands in [`commands`] (`serve`, +//! `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 bridge;