diff --git a/CHANGELOG.md b/CHANGELOG.md index f958d1fa..51919b26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,15 @@ and listed with their assets on the ### Fixed +- **`devstack up --on` moves a started lane**: a lane started on a node + stayed there after `devstack down` (its lease and place stay, only its + services stop), and `devstack up --on local` started it there again. + `--on local` now brings it here, and `--on ` takes it to that node, + as `devstack lane move` does; without `--on` it stays where it runs. +- **The MCP tool `up` does as `devstack up`**: in a worktree without `on` + it starts the lane where this machine starts lanes by default (*Where + lanes start*), not always here, and an `on` that names another place + than where a started lane runs moves it there. - **A lane on a node picks up edits**: its dev servers ran from a copy of the worktree that its daemon there never brought up to date, so hot reload showed the code of the lane's start; the file watcher took the diff --git a/crates/devstack/src/client.rs b/crates/devstack/src/client.rs index 269cbe24..5611ec71 100644 --- a/crates/devstack/src/client.rs +++ b/crates/devstack/src/client.rs @@ -993,6 +993,9 @@ pub fn up( // In a worktree, no `--on`: where this machine starts lanes by default // (ADR-0025); `--on local` keeps it here. The main tree runs here. let explicit = on.filter(|o| !devstack_node::member::is_here(o)); + // `--on local` (or this machine's own name) said in so many words: a + // lane started on a node comes here, as `lane move --to local` does. + let asked_here = names_here(on); let resolved = if worktree().is_some() { devstack_daemon::remote::effective_on(on) } else { @@ -1045,6 +1048,15 @@ pub fn up( ); } } + // Started already somewhere else than `--on` names (here, or another + // node): it moves there, a cold start there that takes over once ready, + // as `devstack lane move` does. Without `--on`, a started lane stays + // where it runs. + let mut moved = false; + if let Some((name, to)) = move_wanted(&c, on, asked_here)? { + lane_move(stack_file, &name, &to)?; + moved = true; + } // Started already, here or on a node: nothing restarts it (#314). if c.lane().is_some_and(|l| l != "-") && (workers || force) { eprintln!( @@ -1052,6 +1064,7 @@ pub fn up( ); } if let Some(wt) = worktree() + && !moved && (on.is_some() || c.lane().is_none_or(|l| l == "-")) { if let Some(node) = on { @@ -1778,6 +1791,67 @@ fn follow_steps( (done, handle) } +/// `on` as given names this machine (`local`, `here`, its own member name). +pub(crate) fn names_here(on: Option<&str>) -> bool { + on.is_some_and(|o| { + devstack_node::member::is_here(o.trim()) + || devstack_daemon::remote::is_this_machine(o.trim()) + }) +} + +/// The worktree's lane is started (here or on a node), and `on` (resolved: +/// `None` here) or `asked_here` names another place than where it runs: +/// the lane and where it is to move. A pool's own choice (`pool`) is no +/// other place; without either, a started lane stays where it runs. +pub(crate) fn move_wanted( + c: &Client, + on: Option<&str>, + asked_here: bool, +) -> Result> { + let Some(name) = c.lane().filter(|l| *l != "-").map(str::to_string) else { + return Ok(None); + }; + let now = lane_node(c, &name)?; + let node = on.and_then(|o| { + devstack_node::member::resolve_on(&devstack_daemon::remote::state_dir(), o) + .ok() + .filter(|(_, n)| n != "pool") + .map(|(_, n)| n) + }); + let to = match (node, asked_here) { + (Some(n), _) if now.as_deref() != Some(n.as_str()) => on.map(str::to_string), + (None, true) if now.is_some() => Some("local".to_string()), + _ => None, + }; + Ok(to.map(|t| (name, t))) +} + +/// Move the lane `name` to `to` and wait until it runs there, without a +/// word (for `devstack mcp`, whose output is its protocol). +pub(crate) fn lane_move_quiet(stack_file: &Path, name: &str, to: &str) -> Result<()> { + let c = main_client(stack_file)?; + let (status, body) = c.call_raw( + "POST", + &format!("/v1/lanes/{name}/move"), + Some(serde_json::json!({ "to": to })), + )?; + if status >= 400 { + bail!("{}", error_text(&body)); + } + Ok(()) +} + +/// The node the lane `name` runs on; `None`: this machine. +fn lane_node(c: &Client, name: &str) -> Result> { + let (_, body) = c.call_raw("GET", "/v1/lanes", None)?; + let lanes: Vec = serde_json::from_str(&body).unwrap_or_default(); + Ok(lanes + .iter() + .find(|l| l["name"] == name) + .and_then(|l| l["node"].as_str()) + .map(str::to_string)) +} + pub fn lane_move(stack_file: &Path, name: &str, to: &str) -> Result<()> { let c = main_client(stack_file)?; println!("moving lane `{name}` to {to}: starting it there; it takes over once it is ready..."); diff --git a/crates/devstack/src/mcp.rs b/crates/devstack/src/mcp.rs index 87df6436..135f0960 100644 --- a/crates/devstack/src/mcp.rs +++ b/crates/devstack/src/mcp.rs @@ -566,14 +566,23 @@ fn call(name: &str, args: &Value, file: &Path) -> Result { "the profile starts the main working tree with fresh data (its volumes go): ask the user, then `yes`, or run it in a lane" ); } - let on = args["on"].as_str().filter(|o| !o.trim().is_empty()); - if on.is_some_and(|o| !devstack_node::member::is_here(o)) + let asked = args["on"].as_str().filter(|o| !o.trim().is_empty()); + if asked.is_some_and(|o| !devstack_node::member::is_here(o)) && client::worktree().is_none() { anyhow::bail!( "`on` runs a worktree's lane on a node: this is the main working tree" ); } + // As `devstack up`: in a worktree, no `on` is where this machine + // starts lanes by default (ADR-0025); `local` keeps it here. + let asked_here = client::names_here(asked); + let resolved = if client::worktree().is_some() { + devstack_daemon::remote::effective_on(asked) + } else { + None + }; + let on = resolved.as_deref(); // Like `devstack up`: start Docker Desktop if the stack needs it // (a lane on a node needs no Docker here), and say so. let mut notes = String::new(); @@ -600,7 +609,16 @@ fn call(name: &str, args: &Value, file: &Path) -> Result { ); } } + // Started already elsewhere than `on` names: it moves there, as + // `devstack up --on` and `lane move` do. + let mut moved = false; + if let Some((name, to)) = client::move_wanted(&c, on, asked_here)? { + client::lane_move_quiet(file, &name, &to)?; + notes.push_str(&format!("lane `{name}` moved to {to}\n")); + moved = true; + } if let Some(wt) = client::worktree() + && !moved && (on.is_some() || c.lane().is_none_or(|l| l == "-")) { // A start on a node is followed within the same timeout.