feat(host): nothing starts a detached host but wanderbooks serve
Plan P15 3.1: the desktop app is the host. The bridge and every command that needs the books now find what runs (a host held by serve, the app's server, or nothing) and never start a host. The permanent run/app-hosted marker is gone: a root is the app's only while a server runs on it with no host. - opener: no host and no server is HoldError::NotRunning for every holder but serve; a server holding run/server.lock with no admin socket yet is waited for, in words (an opening or finishing server); the admin socket answering is the leaseless hold on the app's server, marker or none. - commands: with nothing running, a command that manages the books' files or reads (list, create, adopt, restore, back-up, upgrade, token, connect) runs a server of its own, the one the app starts, for the length of the work and closes it, taking the same server.lock and books.lock; a command that needs an open company held by Wanderbooks (books open/close, upload, download) says "Start Wanderbooks first". The format upgrade goes to the server as an admin request when no host is there. doctor and close say what runs. hold_for_viewer and close_books are removed. - bridge: the line to the app is the default with no host; a bridge whose host went away becomes it. The words an assistant gets name both ways to start the books (the app, or wanderbooks serve). - app server: the child is its own process group on Unix (a terminal Ctrl+C reaches the window alone) and breaks away from a launcher's job on Windows (a kill-on-close job no longer kills it mid-backup); a stale marker from an earlier build is removed. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
8d2187bae7
commit
22f447c0ba
5 files changed
+573
-285
No files matched your search
@@ -54,19 +54,22 @@
|
||||
//! refusal, which says how the human makes one; but when the token
|
||||
//! file is there and couldn't be read, with that instead: the token
|
||||
//! is not said to be missing. The token is never written to the agent or to stderr.
|
||||
//! 12. For a root the desktop app serves (app_server.rs INVARIANT 7) with
|
||||
//! no host running, the bridge is a thin line to the app's server
|
||||
//! and nothing more: it holds no lease and starts no host (a
|
||||
//! detached host is not how the desktop runs). With the app not
|
||||
//! running, every call is answered "Wanderbooks isn't running:
|
||||
//! start Wanderbooks and open BOOK." (`not_running_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 running. A root no app has served,
|
||||
//! and any root a host holds, behave as in 1 to 11 (`wanderbooks
|
||||
//! serve`, the headless host).
|
||||
//! 12. With no host running (every root but a headless `wanderbooks
|
||||
//! serve`'s), the bridge is a thin line to whatever server runs and
|
||||
//! nothing more: it holds no lease and starts no host (a detached
|
||||
//! host is not how the desktop runs; only `wanderbooks serve` starts
|
||||
//! 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_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
|
||||
//! running. A root a host holds (`wanderbooks serve`, the headless
|
||||
//! host) behaves as in 1 to 11: the bridge holds a lease on it; and
|
||||
//! when that host goes and nothing takes its place, the bridge
|
||||
//! becomes the line above, in words, instead of starting another.
|
||||
//! ```
|
||||
|
||||
use crate::knobs::{FLAG, Knobs};
|
||||
@@ -100,7 +103,9 @@ const MAYBE_RAN: &str = "the connection to the books server broke while this cal
|
||||
|
||||
/// What the agent is told when the app is not running (INVARIANT 12).
|
||||
fn not_running_words(book: &str) -> String {
|
||||
format!("Wanderbooks isn't running: start Wanderbooks and open {book}.")
|
||||
format!(
|
||||
"Wanderbooks isn't running: start Wanderbooks and open {book} (on a computer with no screen, run `wanderbooks serve`), then try again."
|
||||
)
|
||||
}
|
||||
|
||||
/// What the agent is told when the app runs but the company is not open
|
||||
@@ -191,8 +196,9 @@ struct Bridge {
|
||||
/// The server's channel, for its owner check (INVARIANT 9).
|
||||
endpoint: ledger_mcp::ipc::Endpoint,
|
||||
/// Whether this is the line to the desktop app's server, with no host
|
||||
/// and no lease (INVARIANT 12).
|
||||
app: bool,
|
||||
/// and no lease (INVARIANT 12). It turns true when a host the bridge
|
||||
/// held is gone and nothing took its place.
|
||||
app: std::sync::atomic::AtomicBool,
|
||||
}
|
||||
|
||||
/// Who has the server's socket, when it isn't plainly this account.
|
||||
@@ -923,7 +929,7 @@ impl Bridge {
|
||||
Some(Link::Failed(why)) => return self.answer(&id, error(&id, why)),
|
||||
// The desktop app can't be reached, or the company isn't open
|
||||
// in it: said now, not "opening" (INVARIANT 12).
|
||||
Some(Link::Refused(why)) if self.app => return self.answer(&id, error(&id, why)),
|
||||
Some(Link::Refused(why)) if self.is_app() => return self.answer(&id, error(&id, why)),
|
||||
_ => None,
|
||||
};
|
||||
let result = result.unwrap_or_else(|| {
|
||||
@@ -955,7 +961,7 @@ impl Bridge {
|
||||
let run_dir = opener.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 _ = self.token_file.set((account, run_dir));
|
||||
let _ = self.token_file.set((account, run_dir.clone()));
|
||||
let mut epoch = 0u64;
|
||||
loop {
|
||||
let mut lease = match opener.hold().await {
|
||||
@@ -965,6 +971,15 @@ impl Bridge {
|
||||
self.link.send_replace(Link::Closed);
|
||||
return;
|
||||
}
|
||||
// The host this bridge held is gone (`wanderbooks serve`
|
||||
// ended) and nothing runs: the bridge becomes the line to
|
||||
// whatever runs next, and says so in words until then
|
||||
// (INVARIANT 12).
|
||||
Err(crate::opener::HoldError::NotRunning) => {
|
||||
self.app.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||
self.link.send_replace(Link::Opening);
|
||||
return self.keep_in_app(run_dir).await;
|
||||
}
|
||||
Err(crate::opener::HoldError::Failed(why)) => {
|
||||
self.link.send_replace(Link::Failed(why));
|
||||
return;
|
||||
@@ -1051,11 +1066,16 @@ impl Bridge {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether this is the line to the app's (or any host-less) server.
|
||||
fn is_app(&self) -> bool {
|
||||
self.app.load(std::sync::atomic::Ordering::SeqCst)
|
||||
}
|
||||
|
||||
/// The words for a refusal at the server's door: for the desktop app's
|
||||
/// `closed` refusal, the bridge's own (INVARIANT 12); else the
|
||||
/// server's.
|
||||
fn refusal_words(&self, closed: bool, body: &str) -> String {
|
||||
if self.app && closed {
|
||||
if self.is_app() && closed {
|
||||
not_open_words(&self.book)
|
||||
} else {
|
||||
body.trim().chars().take(500).collect()
|
||||
@@ -1382,11 +1402,11 @@ async fn skip_line<R: tokio::io::AsyncBufRead + Unpin>(input: &mut R) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the bridge is the line to the desktop app's server for the
|
||||
/// root whose `run/` folder this is (INVARIANT 12): an app has served the
|
||||
/// root, and no host holds it.
|
||||
/// Whether the bridge is the line to the app's server for the root whose
|
||||
/// `run/` folder this is (INVARIANT 12): no host holds it. The server
|
||||
/// answering, or not running at all, is looked at call by call.
|
||||
fn serves_the_app(run: &std::path::Path) -> bool {
|
||||
crate::app_server::app_hosted(run) && !crate::opener::host_lock_held(run)
|
||||
!crate::opener::host_lock_held(run)
|
||||
}
|
||||
|
||||
fn usage(prog: &str) {
|
||||
@@ -1470,7 +1490,7 @@ pub fn run(
|
||||
next_id: std::sync::atomic::AtomicU64::new(1),
|
||||
pending: Mutex::new(std::collections::HashMap::new()),
|
||||
endpoint,
|
||||
app,
|
||||
app: std::sync::atomic::AtomicBool::new(app),
|
||||
});
|
||||
let keeper = if app {
|
||||
tokio::spawn(bridge.clone().keep_in_app(run_dir))
|
||||
@@ -1834,7 +1854,7 @@ mod against_a_server {
|
||||
next_id: std::sync::atomic::AtomicU64::new(1),
|
||||
pending: Mutex::new(std::collections::HashMap::new()),
|
||||
endpoint,
|
||||
app,
|
||||
app: std::sync::atomic::AtomicBool::new(app),
|
||||
});
|
||||
(bridge, rx)
|
||||
}
|
||||
@@ -2605,7 +2625,7 @@ mod against_a_server {
|
||||
fn the_two_messages_are_plain_and_name_the_company() {
|
||||
assert_eq!(
|
||||
not_running_words("acme"),
|
||||
"Wanderbooks isn't running: start Wanderbooks and open acme."
|
||||
"Wanderbooks isn't running: start Wanderbooks and open acme (on a computer with no screen, run `wanderbooks serve`), then try again."
|
||||
);
|
||||
assert_eq!(
|
||||
not_open_words("acme"),
|
||||
@@ -2614,12 +2634,13 @@ mod against_a_server {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_root_is_the_apps_only_when_marked_and_no_host_holds_it() {
|
||||
fn a_root_is_the_line_to_the_app_when_no_host_holds_it() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
assert!(!serves_the_app(dir.path()), "no app has served it");
|
||||
std::fs::write(dir.path().join(crate::app_server::APP_MARKER), b"").unwrap();
|
||||
assert!(serves_the_app(dir.path()), "no host holds it");
|
||||
// A mark an earlier build left changes nothing.
|
||||
std::fs::write(dir.path().join(crate::app_server::RETIRED_MARKER), b"").unwrap();
|
||||
assert!(serves_the_app(dir.path()));
|
||||
// A host holding the root is the headless path, marked or not.
|
||||
// A host holding the root is the headless path.
|
||||
let lock = std::fs::File::create(dir.path().join(crate::lease::HOST_LOCK)).unwrap();
|
||||
lock.lock().unwrap();
|
||||
assert!(!serves_the_app(dir.path()));
|
||||
|
||||
Reference in new issue
Block a user