Make roaming an opt-in goose-cli feature (default-off, stays in-tree) (#11516)
Signed-off-by: Michael Neale <michael.neale@gmail.com> Co-authored-by: Michael Neale <14976+michaelneale@users.noreply.github.com> Co-authored-by: pstayets <philip@vulturelabs.com> Co-authored-by: pstayet <philipstayetski2416@gmail.com> Co-authored-by: Colin Gauvin <colin@gauvin.id>
This commit is contained in:
@@ -18,6 +18,8 @@ use crate::commands::configure::handle_configure;
|
||||
use crate::commands::info::handle_info;
|
||||
use crate::commands::plugin::{handle_plugin_install, handle_plugin_update};
|
||||
use crate::commands::recipe::{handle_deeplink, handle_list, handle_open, handle_validate};
|
||||
#[cfg(feature = "roaming")]
|
||||
use crate::commands::roam::{handle_roam_command, RoamCommand};
|
||||
use crate::commands::term::{
|
||||
handle_term_info, handle_term_init, handle_term_log, handle_term_run, Shell,
|
||||
};
|
||||
@@ -840,6 +842,14 @@ enum Command {
|
||||
enable_scheduler: bool,
|
||||
},
|
||||
|
||||
/// Share or connect to agents peer-to-peer over iroh
|
||||
#[cfg(feature = "roaming")]
|
||||
#[command(about = "Share or connect to agents peer-to-peer (roaming)")]
|
||||
Roam {
|
||||
#[command(subcommand)]
|
||||
command: RoamCommand,
|
||||
},
|
||||
|
||||
/// Start ACP server over HTTP and WebSocket
|
||||
#[command(about = "Start ACP server over HTTP and WebSocket")]
|
||||
Serve {
|
||||
@@ -887,6 +897,14 @@ enum Command {
|
||||
|
||||
#[arg(long, help = "Enable scheduled recipe execution")]
|
||||
enable_scheduler: bool,
|
||||
|
||||
/// Also expose this server over goose roam (p2p) so paired devices can connect remotely
|
||||
#[cfg(feature = "roaming")]
|
||||
#[arg(
|
||||
long,
|
||||
help = "Also expose this server over goose roam (p2p) so paired devices can connect remotely"
|
||||
)]
|
||||
roam: bool,
|
||||
},
|
||||
|
||||
/// Start or resume interactive chat sessions
|
||||
@@ -1356,6 +1374,8 @@ fn get_command_name(command: &Option<Command>) -> &'static str {
|
||||
Some(Command::Info { .. }) => "info",
|
||||
Some(Command::Mcp { .. }) => "mcp",
|
||||
Some(Command::Acp { .. }) => "acp",
|
||||
#[cfg(feature = "roaming")]
|
||||
Some(Command::Roam { .. }) => "roam",
|
||||
Some(Command::Serve { .. }) => "serve",
|
||||
Some(Command::Session { .. }) => "session",
|
||||
Some(Command::Run { .. }) => "run",
|
||||
@@ -1606,6 +1626,127 @@ struct ServeCommandArgs {
|
||||
dangerously_unauthenticated: bool,
|
||||
allowed_origins: Vec<String>,
|
||||
enable_scheduler: bool,
|
||||
#[cfg(feature = "roaming")]
|
||||
roam: bool,
|
||||
}
|
||||
|
||||
#[cfg(feature = "roaming")]
|
||||
type RoamShareSlot =
|
||||
std::sync::Arc<tokio::sync::RwLock<Option<std::sync::Arc<goose_roaming::RoamingNode>>>>;
|
||||
|
||||
#[cfg(feature = "roaming")]
|
||||
fn spawn_roam_share(
|
||||
server: std::sync::Arc<goose::acp::server_factory::AcpServer>,
|
||||
) -> RoamShareSlot {
|
||||
use crate::commands::roam::try_acquire_roam_lock_owner;
|
||||
|
||||
let slot = RoamShareSlot::default();
|
||||
let task_slot = slot.clone();
|
||||
tokio::spawn(async move {
|
||||
let mut standing_by = false;
|
||||
loop {
|
||||
match try_acquire_roam_lock_owner() {
|
||||
Ok(Some(lock)) => match start_roam_share(server.clone()).await {
|
||||
Ok(node) => {
|
||||
*task_slot.write().await = Some(node);
|
||||
let _lock = lock;
|
||||
std::future::pending::<()>().await;
|
||||
}
|
||||
Err(error) => {
|
||||
tracing::error!("roam share failed to start: {error}");
|
||||
drop(lock);
|
||||
}
|
||||
},
|
||||
Ok(None) => {
|
||||
if !standing_by {
|
||||
standing_by = true;
|
||||
eprintln!(
|
||||
"another goose process owns the roaming endpoint; standing by to take over if it exits"
|
||||
);
|
||||
}
|
||||
}
|
||||
// A real failure (unwritable data dir, filesystem error) will
|
||||
// not fix itself: surface it and stop instead of retrying as
|
||||
// if the endpoint were merely busy.
|
||||
Err(error) => {
|
||||
eprintln!("roaming disabled: cannot acquire the endpoint lock: {error:#}");
|
||||
return;
|
||||
}
|
||||
}
|
||||
tokio::time::sleep(std::time::Duration::from_secs(3)).await;
|
||||
}
|
||||
});
|
||||
slot
|
||||
}
|
||||
|
||||
#[cfg(feature = "roaming")]
|
||||
async fn start_roam_share(
|
||||
server: std::sync::Arc<goose::acp::server_factory::AcpServer>,
|
||||
) -> Result<std::sync::Arc<goose_roaming::RoamingNode>> {
|
||||
use crate::commands::roam::{
|
||||
directory_path, load_identity, resolve_relay_settings, trust_path,
|
||||
};
|
||||
use crate::commands::roam_full_bridge::FullAcpBridge;
|
||||
use goose::config::paths::Paths;
|
||||
use goose_roaming::{RoamingConfig, RoamingNode, TrustBook};
|
||||
use std::sync::Arc;
|
||||
|
||||
let status_path = Paths::data_dir().join("roam/serve.json");
|
||||
let _ = std::fs::remove_file(&status_path);
|
||||
|
||||
let identity = load_identity()?;
|
||||
let node = RoamingNode::bind(RoamingConfig {
|
||||
identity,
|
||||
relay: resolve_relay_settings()?,
|
||||
trust: TrustBook::new(),
|
||||
trust_path: Some(trust_path()),
|
||||
directory: goose_roaming::Directory::persistent_owned(directory_path()),
|
||||
bind_addr: None,
|
||||
relay_tls: None,
|
||||
})
|
||||
.await?;
|
||||
|
||||
let agent_id = node.endpoint_id().to_string();
|
||||
// Roaming sessions run where `goose serve` was started: the connector's
|
||||
// machine-local path is meaningless on this host, and the serve-wide
|
||||
// server keeps `session_cwd: None` for local ACP clients.
|
||||
let session_cwd =
|
||||
std::env::current_dir().map_err(|e| anyhow::anyhow!("could not determine cwd: {e}"))?;
|
||||
node.share(Arc::new(FullAcpBridge::new(server, agent_id, session_cwd)))
|
||||
.await?;
|
||||
|
||||
if !node.wait_online(std::time::Duration::from_secs(15)).await {
|
||||
tracing::warn!(
|
||||
"roaming endpoint did not come online; the card may lack a reachable address"
|
||||
);
|
||||
}
|
||||
|
||||
let card = node.card();
|
||||
let card_encoded = card.encode()?;
|
||||
let started_at = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_millis() as u64)
|
||||
.unwrap_or(0);
|
||||
let status = serde_json::json!({
|
||||
"card": card_encoded,
|
||||
"endpointId": card.endpoint_id.to_string(),
|
||||
"fingerprint": card.fingerprint(),
|
||||
"startedAt": started_at,
|
||||
});
|
||||
if let Some(dir) = status_path.parent() {
|
||||
std::fs::create_dir_all(dir)?;
|
||||
}
|
||||
let tmp_path = status_path.with_extension("json.tmp");
|
||||
std::fs::write(&tmp_path, serde_json::to_vec_pretty(&status)?)?;
|
||||
std::fs::rename(&tmp_path, &status_path)?;
|
||||
|
||||
eprintln!("roam is enabled for this server");
|
||||
eprintln!(" endpoint id : {}", card.endpoint_id);
|
||||
eprintln!(" fingerprint : {}", card.fingerprint());
|
||||
eprintln!("your connection card (share with a peer so it can reach you):");
|
||||
eprintln!("{card_encoded}");
|
||||
|
||||
Ok(node)
|
||||
}
|
||||
|
||||
async fn handle_serve_command(args: ServeCommandArgs) -> Result<()> {
|
||||
@@ -1629,6 +1770,8 @@ async fn handle_serve_command(args: ServeCommandArgs) -> Result<()> {
|
||||
dangerously_unauthenticated,
|
||||
allowed_origins,
|
||||
enable_scheduler,
|
||||
#[cfg(feature = "roaming")]
|
||||
roam,
|
||||
} = args;
|
||||
|
||||
let builtins = AcpBuiltinSelection::from_requested(builtins);
|
||||
@@ -1651,6 +1794,7 @@ async fn handle_serve_command(args: ServeCommandArgs) -> Result<()> {
|
||||
config_dir: Paths::config_dir(),
|
||||
goose_platform: platform.into(),
|
||||
additional_source_roots,
|
||||
session_cwd: None,
|
||||
enable_scheduler,
|
||||
}));
|
||||
let env_secret = std::env::var(GOOSE_SERVER_SECRET_KEY_ENV)
|
||||
@@ -1684,6 +1828,12 @@ async fn handle_serve_command(args: ServeCommandArgs) -> Result<()> {
|
||||
if let Err(error) = server.start_scheduler().await {
|
||||
warn!("Scheduler failed to start; scheduled jobs will not run until a client connects: {error}");
|
||||
}
|
||||
#[cfg(feature = "roaming")]
|
||||
let roam_share = if roam {
|
||||
Some(spawn_roam_share(server.clone()))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let router = create_router(
|
||||
server,
|
||||
secret_key,
|
||||
@@ -1741,6 +1891,13 @@ async fn handle_serve_command(args: ServeCommandArgs) -> Result<()> {
|
||||
.await?;
|
||||
}
|
||||
|
||||
#[cfg(feature = "roaming")]
|
||||
if let Some(slot) = roam_share {
|
||||
if let Some(node) = slot.write().await.take() {
|
||||
let _ = node.shutdown().await;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -2652,6 +2809,8 @@ pub async fn cli() -> anyhow::Result<()> {
|
||||
builtins,
|
||||
enable_scheduler,
|
||||
}) => goose::acp::server::run(builtins, enable_scheduler).await,
|
||||
#[cfg(feature = "roaming")]
|
||||
Some(Command::Roam { command }) => handle_roam_command(command).await,
|
||||
Some(Command::Serve {
|
||||
host,
|
||||
port,
|
||||
@@ -2663,6 +2822,8 @@ pub async fn cli() -> anyhow::Result<()> {
|
||||
dangerously_unauthenticated,
|
||||
allowed_origins,
|
||||
enable_scheduler,
|
||||
#[cfg(feature = "roaming")]
|
||||
roam,
|
||||
}) => {
|
||||
handle_serve_command(ServeCommandArgs {
|
||||
host,
|
||||
@@ -2675,6 +2836,8 @@ pub async fn cli() -> anyhow::Result<()> {
|
||||
dangerously_unauthenticated,
|
||||
allowed_origins,
|
||||
enable_scheduler,
|
||||
#[cfg(feature = "roaming")]
|
||||
roam,
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user