feat(host,gui): the command that reaches a company's books is made in one place

The Connect page of P15 unit A4 shows an assistant the exact command it
must run to reach a company (`<this program> mcp --book ID`, with --root
when a root was named). `connect` already built that command, so it is
now one function (`bridge_serve_args`/`command_line`) used by both, and
the step `connect` does first (the company's default key exists and its
copy is in the token file) is `ensure_key`, which the page offers by
itself for assistants it has no button for.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-21 10:03:35 -07:00
1 parent 016f1b2afc
commit 1d1a2a5a88
3 files changed
+64 -40

No files matched your search

+6 -2
View File
@@ -155,8 +155,12 @@ impl ledger_gui::owner::Owner for ViewerOwner {
self.server.revoke_token(book, name)
}
fn bridge_command(&self, book: &str) -> Result<String, String> {
ledger_host::commands::bridge_command_line(&self.common, book)
fn command_line(&self, words: &[&str]) -> Result<String, String> {
ledger_host::commands::command_line(&self.common, words)
}
fn make_key(&self, book: &str) -> Result<bool, String> {
ledger_host::commands::ensure_key(&self.common, book)
}
fn close_books(&self) -> Result<String, String> {
+10 -4
View File
@@ -267,10 +267,16 @@ pub trait Owner: Send + Sync {
fn serve_revoke(&self, _book: &str, _name: &str) -> Result<String, String> {
Err("not here".into())
}
/// The command an assistant runs to reach the company `book`, as text
/// to type (the program and `mcp --book ID`, quoted for a terminal):
/// what the Connect page shows for an assistant it has no button for.
fn bridge_command(&self, _book: &str) -> Result<String, String> {
/// This program run with `words`, as a line to type in a terminal (the
/// program's own path, the words, the data root if one was named): what
/// the Connect page shows for an assistant it has no button for.
fn command_line(&self, _words: &[&str]) -> Result<String, String> {
Err("not here".into())
}
/// The key an assistant needs to reach the company `book` exists (made
/// now if it did not): `Ok(true)` when it was made now. What `connect`
/// does first, for an assistant set up by hand. Blocks.
fn make_key(&self, _book: &str) -> Result<bool, String> {
Err("not here".into())
}
/// Close the books: everything holding them lets go.
+48 -34
View File
@@ -1402,42 +1402,43 @@ pub fn connect(
}
}
/// The command an assistant runs to reach the books `book`: this program's
/// own executable and `mcp --book <book>`, with `--root` when the data root
/// was named and the knobs this run was given. The one place it is made:
/// `connect_agent` registers it, and the window's Connect page shows it to
/// be typed into an assistant that has no `connect` of its own.
pub fn bridge_serve_args(common: &Common, book: &str) -> Result<Vec<std::ffi::OsString>, String> {
let book = ledger_store::root::BookId::parse(book)
.map_err(|e| e.to_string())?
.to_string();
/// This program's own executable followed by `words`, then `--root` when
/// the data root was named and the knobs this run was given: what an
/// assistant, or a person at a terminal, runs to reach these books. The one
/// place it is made.
fn program_args(common: &Common, words: &[&str]) -> Result<Vec<std::ffi::OsString>, String> {
// A translocated macOS app's path (P11a step 6) is not a place to point
// an agent: the copy goes away with the app.
let exe = crate::start::this_executable()?;
if let Some(why) = crate::start::translocation_refusal(&exe) {
return Err(why.to_string());
}
let mut serve: Vec<std::ffi::OsString> = vec![
exe.into_os_string(),
"mcp".into(),
"--book".into(),
book.into(),
];
let mut args: Vec<std::ffi::OsString> = vec![exe.into_os_string()];
args.extend(words.iter().map(std::ffi::OsString::from));
if common.root.is_some() {
let found =
ledger_store::root::Root::find(common.root.as_deref()).map_err(|e| e.to_string())?;
serve.push("--root".into());
serve.push(found.path().as_os_str().to_os_string());
args.push("--root".into());
args.push(found.path().as_os_str().to_os_string());
}
serve.extend(common.knobs.args().into_iter().map(Into::into));
Ok(serve)
args.extend(common.knobs.args().into_iter().map(Into::into));
Ok(args)
}
/// [`bridge_serve_args`] as text, for an assistant that is set up by hand:
/// a part with a space in it is quoted for a terminal.
pub fn bridge_command_line(common: &Common, book: &str) -> Result<String, String> {
let args = bridge_serve_args(common, book)?;
Ok(args
/// The command an assistant runs to reach the books `book`: this program
/// and `mcp --book <book>`. `connect_agent` registers it, and the window's
/// Connect page shows it for an assistant that has no `connect` of its own.
pub fn bridge_serve_args(common: &Common, book: &str) -> Result<Vec<std::ffi::OsString>, String> {
let book = ledger_store::root::BookId::parse(book)
.map_err(|e| e.to_string())?
.to_string();
program_args(common, &["mcp", "--book", &book])
}
/// This program run with `words`, as a line to type in a terminal: a part
/// with a space in it is quoted.
pub fn command_line(common: &Common, words: &[&str]) -> Result<String, String> {
Ok(program_args(common, words)?
.iter()
.map(|a| {
let a = a.to_string_lossy();
@@ -1451,6 +1452,28 @@ pub fn bridge_command_line(common: &Common, book: &str) -> Result<String, String
.join(" "))
}
/// The default token of the books `book` exists and its copy is in the token
/// file (made now if it is not): what `connect` does first. `Ok(true)` when
/// it was made now.
pub fn ensure_key(common: &Common, book: &str) -> Result<bool, String> {
let book = ledger_store::root::BookId::parse(book)
.map_err(|e| e.to_string())?
.to_string();
let found =
ledger_store::root::Root::find(common.root.as_deref()).map_err(|e| e.to_string())?;
let run = found.run_dir().map_err(|e| e.to_string())?;
let account = crate::tokens::Account::new(found.path(), &book, crate::tokens::DEFAULT_NAME);
if crate::tokens::keeper(&run).get(&account)?.is_some() {
return Ok(false);
}
let scopes = ledger_store::auth::Scope::ALL
.iter()
.map(|s| s.as_str().to_string())
.collect();
make_token(common, &book, crate::tokens::DEFAULT_NAME, scopes, true)?;
Ok(true)
}
/// A revoked token's copy in the token file goes with it (what `token
/// revoke` does after the server has ended the token). `run_dir` is the
/// root's `run/` folder.
@@ -1483,16 +1506,7 @@ pub fn connect_agent(common: &Common, agent: &str, book: &str) -> Result<Vec<Str
let serve = bridge_serve_args(common, &book)?;
let mut lines = Vec::new();
// The token: kept in its file, or made now.
let found =
ledger_store::root::Root::find(common.root.as_deref()).map_err(|e| e.to_string())?;
let run = found.run_dir().map_err(|e| e.to_string())?;
let account = crate::tokens::Account::new(found.path(), &book, crate::tokens::DEFAULT_NAME);
if crate::tokens::keeper(&run).get(&account)?.is_none() {
let scopes = ledger_store::auth::Scope::ALL
.iter()
.map(|s| s.as_str().to_string())
.collect();
make_token(common, &book, crate::tokens::DEFAULT_NAME, scopes, true)?;
if ensure_key(common, &book)? {
lines.push(format!(
"Made the agent token of the books {book}, kept in a token file in the data root."
));