diff --git a/CHANGELOG.md b/CHANGELOG.md index fee8c00b..733ee8ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,15 @@ and listed with their assets on the ### Added +- **Links to a stack's or a lane's dashboard page, for agents to hand on**: + `devstack status`, `devstack up` and `devstack lane start` end with + `dashboard: http://127.0.0.1:4250/#/s/?lane=`; the MCP tools + `status`, `up` and `lane_start` too, and `lanes` names each lane's page. + `devstack ui` opens the dashboard on the stack it runs in, in a worktree on + its lane (`--lane ` for another); `devstack ui --link` prints the page + and the `devstack://open` link that pairs the browser first. The `devstack` + skill tells agents to give the user that page after starting a stack or a + lane, or creating a worktree (run `devstack skills install` again). - **Open a stack's folder from the dashboard** ([ADR-0046](docs/adr/0046-the-hub-opens-a-worktree-in-the-users-programs.md)): the path in the stack header is a menu. It copies the path, or opens the diff --git a/README.md b/README.md index 95a2b072..5cb54b5a 100644 --- a/README.md +++ b/README.md @@ -197,7 +197,7 @@ Every command and flag: `devstack --help`, `devstack --help`. | `devstack builds `, `logs`, `top`, `env [--export dotenv]` | what ran, what it printed, what it uses, what it gets | | `devstack run ` | a command of the stack file | | `devstack lane list\|start\|stop\|reset\|prune\|move\|share` | the lanes (worktrees), see above; `reset` throws a stopped lane's data away, `prune` removes what gone lanes left behind | -| `devstack ui [--browser ] [--print]` | open and pair the dashboard; the browser is `--browser`, else `DEVSTACK_BROWSER`, else `BROWSER`, else the system's default | +| `devstack ui [--lane ] [--browser ] [--link\|--print]` | open and pair the dashboard, on this stack (in a worktree on its lane, or on `--lane`); the browser is `--browser`, else `DEVSTACK_BROWSER`, else `BROWSER`, else the system's default. `--link` prints the lasting links to the stack or lane instead (the page, and the `devstack://open` link that pairs first); `--print` the one-time pairing link. `status`, `up` and `lane start` end with the page as `dashboard: ` | | `devstack hub [status\|stop\|start\|install\|uninstall]` | the hub that serves the dashboard: without an action in the foreground; `stop` leaves the stacks running; `install` registers it at logon and the `devstack://` link (`--no-autostart`: the link only), `uninstall` removes both, and `devstack up` then leaves them alone | | `devstack skills install [--dir] [--force]`, `skills check` | write the agent skills; check that they match this devstack and stack file (exits 1 when not) | | `devstack mcp` | the MCP server (above) | diff --git a/crates/devstack/src/client.rs b/crates/devstack/src/client.rs index 5c58b81b..f03074ad 100644 --- a/crates/devstack/src/client.rs +++ b/crates/devstack/src/client.rs @@ -677,20 +677,24 @@ pub fn print_status(stack: &Value) { print!("{}", status_text(stack)); } -/// `devstack status`: the table, plus a note when the generated agent skills -/// no longer match. +/// `devstack status`: the table, a note when the generated agent skills no +/// longer match, and where the dashboard shows this tree. pub fn print_status_for(stack: &Value, stack_file: &Path) { print_status(stack); - if let Some(n) = Client::probe(&state_dir(stack_file).unwrap_or_default()) + let c = Client::probe(&state_dir(stack_file).unwrap_or_default()) .ok() - .flatten() - .and_then(|c| lanes_note(&c)) - { + .flatten(); + if let Some(n) = c.as_ref().and_then(lanes_note) { println!("\n{n}"); } if let Some(n) = crate::skills::outdated_note(stack_file) { println!("\n{n}"); } + let lane = c.as_ref().and_then(Client::lane).filter(|l| *l != "-"); + println!( + "\n{}", + crate::hubcmd::dashboard_line(&daemon_file(stack_file), lane) + ); } /// The service table and the infrastructure, as `devstack status` shows them. @@ -1575,6 +1579,10 @@ pub fn lane_start( UpOutcome::Ready(stack) => { println!(); print!("{}", status_text(&stack)); + println!( + "\n{}", + crate::hubcmd::dashboard_line(&daemon_file(stack_file), Some(&started.name)) + ); if run && let Some(cmd) = wanted.and_then(|p| p.command.as_deref()) { println!( "\nrunning `{cmd}` in lane `{}`, the profile's command", @@ -1592,6 +1600,23 @@ pub fn lane_start( } } +/// The lane of the worktree the CLI runs in, started or not, by the +/// daemon's list; `None` in the main working tree, or without a daemon. +pub fn worktree_lane(stack_file: &Path) -> Option { + let c = Client::probe(&state_dir(stack_file).ok()?).ok().flatten()?; + let wt = worktree()?; + let lanes: Vec = serde_json::from_str(&c.get("/v1/lanes").ok()?).ok()?; + let me = devstack_proc::long_path(&wt.path); + lanes + .iter() + .find(|l| { + l["path"] + .as_str() + .is_some_and(|p| same_dir(&devstack_proc::long_path(Path::new(p)), &me)) + }) + .and_then(|l| l["name"].as_str().map(str::to_string)) +} + /// One line on the lanes for `status` in the main working tree (#103): that /// worktrees exist, which run, and how to start one beside the main tree. pub fn lanes_note(c: &Client) -> Option { diff --git a/crates/devstack/src/dispatch/hub.rs b/crates/devstack/src/dispatch/hub.rs index b3b86469..0d055bc3 100644 --- a/crates/devstack/src/dispatch/hub.rs +++ b/crates/devstack/src/dispatch/hub.rs @@ -3,10 +3,16 @@ use crate::{Cmd, HubAction, NotifyAction, hubcmd, integration}; use anyhow::Result; +use std::path::Path; -pub(crate) fn run(cmd: Cmd) -> Result<()> { +pub(crate) fn run(file: &Path, cmd: Cmd) -> Result<()> { match cmd { - Cmd::Ui { print, browser } => hubcmd::ui(print, browser.as_deref()), + Cmd::Ui { + lane, + link, + print, + browser, + } => hubcmd::ui(file, lane.as_deref(), link, print, browser.as_deref()), Cmd::Hub { action } => hub(action), Cmd::OpenUrl { url } => integration::open_url(&url), _ => super::misrouted(), diff --git a/crates/devstack/src/hubcmd.rs b/crates/devstack/src/hubcmd.rs index 41e5f5eb..a40d022e 100644 --- a/crates/devstack/src/hubcmd.rs +++ b/crates/devstack/src/hubcmd.rs @@ -2,6 +2,7 @@ use anyhow::{Result, anyhow, bail}; use serde_json::Value; +use std::path::Path; use std::time::Duration; fn agent() -> ureq::Agent { @@ -262,6 +263,41 @@ pub fn pairing_url() -> Result { .ok_or_else(|| anyhow!("no url in the hub's answer")) } +/// Where the dashboard shows a stack, on a lane or its main working tree. +pub struct DashboardLinks { + /// The hash route: `/#/s/?lane=`. + pub route: String, + /// The page, for a browser paired before. + pub page: String, + /// `devstack://open?stack=&lane=`: pairs the browser that + /// opens it first, then shows the page (ADR-0045 point 6). + pub link: String, +} + +/// The dashboard's links for the stack whose daemon runs from `main_file` +/// (in a lane, the main working tree's stack file); `lane: None` is the +/// main working tree. The id is the one the daemon registers. +pub fn dashboard_links(main_file: &Path, lane: Option<&str>) -> DashboardLinks { + let abs = std::path::absolute(main_file).unwrap_or_else(|_| main_file.to_path_buf()); + let id = devstack_daemon::registry::id_for(&devstack_proc::long_path(&abs)); + let lane = crate::client::enc(lane.unwrap_or("")); + let route = format!("/#/s/{id}?lane={lane}"); + DashboardLinks { + page: format!("http://127.0.0.1:{}{route}", devstack_hub::port()), + link: if lane.is_empty() { + format!("devstack://open?stack={id}") + } else { + format!("devstack://open?stack={id}&lane={lane}") + }, + route, + } +} + +/// `dashboard: `: the line `status`, `up` and `lane start` end with. +pub fn dashboard_line(main_file: &Path, lane: Option<&str>) -> String { + format!("dashboard: {}", dashboard_links(main_file, lane).page) +} + /// Open the dashboard at `route` (a hash route, `/#/s/?lane=`), paired /// like `devstack ui`: a click on a notification (`devstack://open`). pub fn open_dashboard(route: &str) -> Result<()> { @@ -285,8 +321,39 @@ fn chosen_browser(explicit: Option<&str>) -> Option { .filter(|b| !b.trim().is_empty()) } -pub fn ui(print_only: bool, browser: Option<&str>) -> Result<()> { - let url = pairing_url()?; +/// `devstack ui`: the dashboard in the browser, paired, on the stack of +/// `stack_file` when there is one, on `lane` or this worktree's lane. `link` +/// prints the lasting links instead (an agent hands them on), `print_only` +/// the one-time pairing link. +pub fn ui( + stack_file: &Path, + lane: Option<&str>, + link: bool, + print_only: bool, + browser: Option<&str>, +) -> Result<()> { + let here = stack_file.is_file().then(|| { + let lane = lane + .map(str::to_string) + .or_else(|| crate::client::worktree_lane(stack_file)); + dashboard_links(&crate::client::daemon_file(stack_file), lane.as_deref()) + }); + if here.is_none() && (link || lane.is_some()) { + bail!( + "no stack file at {}: run it in the repository, or name the file with -f", + stack_file.display() + ); + } + if link { + let l = here.expect("checked above"); + println!("{}", l.page); + println!("{}", l.link); + return Ok(()); + } + let mut url = pairing_url()?; + if let Some(l) = &here { + url = format!("{url}&next={}", devstack_hub::next_param(&l.route)); + } if print_only { println!("{url}"); return Ok(()); @@ -431,6 +498,23 @@ pub fn ensure_quietly() { #[cfg(test)] mod tests { + #[test] + fn the_dashboard_links_name_the_stack_the_daemon_registers() { + let file = std::env::temp_dir().join("shop").join("devstack.toml"); + let id = devstack_daemon::registry::id_for(&devstack_proc::long_path(&file)); + let port = devstack_hub::port(); + let main = super::dashboard_links(&file, None); + assert_eq!(main.route, format!("/#/s/{id}?lane=")); + assert_eq!(main.page, format!("http://127.0.0.1:{port}/#/s/{id}?lane=")); + assert_eq!(main.link, format!("devstack://open?stack={id}")); + let lane = super::dashboard_links(&file, Some("feat-x")); + assert_eq!( + lane.page, + format!("http://127.0.0.1:{port}/#/s/{id}?lane=feat-x") + ); + assert_eq!(lane.link, format!("devstack://open?stack={id}&lane=feat-x")); + } + #[test] fn browsers_by_name_or_path() { let b = super::browser_program; diff --git a/crates/devstack/src/main.rs b/crates/devstack/src/main.rs index b7bb8639..915ebe14 100644 --- a/crates/devstack/src/main.rs +++ b/crates/devstack/src/main.rs @@ -219,8 +219,18 @@ enum Cmd { }, /// MCP server on stdio for coding agents; a client of the daemon like the CLI. Mcp, - /// Open the dashboard: starts the hub if needed and pairs the browser (ADR-0021). + /// Open the dashboard: starts the hub if needed and pairs the browser + /// (ADR-0021). In a repository it opens on this stack, in a worktree on + /// its lane. Ui { + /// Open on this lane of the stack (default: the worktree's own). + #[arg(long, value_name = "LANE")] + lane: Option, + /// Print the lasting links to this stack (or lane) instead: the page, + /// for a browser paired before, and the `devstack://open` link, which + /// pairs the browser first. + #[arg(long, conflicts_with = "print")] + link: bool, /// Print the one-time pairing link instead of opening a browser. #[arg(long)] print: bool, @@ -672,7 +682,9 @@ fn main() -> Result<()> { cmd @ (Cmd::Node { .. } | Cmd::Join { .. } | Cmd::Pool { .. } | Cmd::Open { .. }) => { dispatch::pool::run(&file, cmd) } - cmd @ (Cmd::Ui { .. } | Cmd::Hub { .. } | Cmd::OpenUrl { .. }) => dispatch::hub::run(cmd), + cmd @ (Cmd::Ui { .. } | Cmd::Hub { .. } | Cmd::OpenUrl { .. }) => { + dispatch::hub::run(&file, cmd) + } cmd @ (Cmd::Update { .. } | Cmd::Myself { .. }) => dispatch::myself::run(cmd), Cmd::Skills { action } => dispatch::skills::run(&file, action), cmd @ (Cmd::Daemon | Cmd::ServeStatic { .. } | Cmd::Shim { .. }) => { @@ -730,6 +742,27 @@ mod tests { assert_eq!(on.as_deref(), Some("pool")); } + #[test] + fn ui_opens_on_a_lane_or_prints_its_links() { + assert!(matches!( + parse(&["ui", "--lane", "feat-x", "--link"]).cmd, + Cmd::Ui { lane: Some(l), link: true, print: false, .. } if l == "feat-x" + )); + assert!(matches!( + parse(&["ui"]).cmd, + Cmd::Ui { + lane: None, + link: false, + print: false, + .. + } + )); + assert_eq!( + refused(&["ui", "--link", "--print"]), + ErrorKind::ArgumentConflict + ); + } + #[test] fn promote_takes_a_service_or_a_lane() { assert!(matches!( diff --git a/crates/devstack/src/mcp.rs b/crates/devstack/src/mcp.rs index 43c3d144..4d228c6e 100644 --- a/crates/devstack/src/mcp.rs +++ b/crates/devstack/src/mcp.rs @@ -465,6 +465,12 @@ fn checked(status: u16, body: String) -> Result { Ok(body) } +/// `dashboard: ` for the stack of `file`, on `lane` (`None`: the main +/// working tree): the line an agent hands the user on. +fn dashboard_line(file: &Path, lane: Option<&str>) -> String { + crate::hubcmd::dashboard_line(&client::daemon_file(file), lane) +} + fn call(name: &str, args: &Value, file: &Path) -> Result { match name { "status" => { @@ -476,6 +482,8 @@ fn call(name: &str, args: &Value, file: &Path) -> Result { if let Some(n) = crate::skills::outdated_note(file) { text.push_str(&format!("\n{n}\n")); } + let lane = c.lane().filter(|l| *l != "-"); + text.push_str(&format!("\n{}\n", dashboard_line(file, lane))); Ok(text) } "up" => { @@ -551,7 +559,11 @@ fn call(name: &str, args: &Value, file: &Path) -> Result { } match client::start_and_wait(&c, file, Some(left()), |_, _| {})? { UpOutcome::Ready(stack) => { - let mut text = format!("{notes}ready\n\n{}", client::status_text(&stack)); + let mut text = format!( + "{notes}ready\n\n{}\n{}\n", + client::status_text(&stack), + dashboard_line(file, c.lane().filter(|l| *l != "-")) + ); if args["run"] == true && let Some(cmd) = wanted.as_ref().and_then(|p| p.command.as_deref()) { @@ -654,7 +666,11 @@ fn call(name: &str, args: &Value, file: &Path) -> Result { match client::start_and_wait(&started.client, &started.file, Some(timeout), |_, _| {})? { UpOutcome::Ready(stack) => { - let mut text = format!("{notes}ready\n\n{}", client::status_text(&stack)); + let mut text = format!( + "{notes}ready\n\n{}\n{}\n", + client::status_text(&stack), + dashboard_line(file, Some(&started.name)) + ); if run && let Some(cmd) = wanted.and_then(|p| p.command.as_deref()) { let log = client::run_command_on(&started.client, cmd, false)?; text.push_str(&format!( @@ -735,9 +751,9 @@ fn call(name: &str, args: &Value, file: &Path) -> Result { } else { format!("{}/{} running", l["running"], l["services"]) }; + let name = l["name"].as_str().unwrap_or(""); out.push_str(&format!( - "{} (branch {}): ports +{}, {running}, {}{}\n", - l["name"].as_str().unwrap_or(""), + "{name} (branch {}): ports +{}, {running}, {}{}; dashboard {}\n", l["branch"].as_str().unwrap_or("detached"), l["port_offset"], l["path"].as_str().unwrap_or(""), @@ -745,7 +761,8 @@ fn call(name: &str, args: &Value, file: &Path) -> Result { ", serves the main ports" } else { "" - } + }, + crate::hubcmd::dashboard_links(&client::daemon_file(file), Some(name)).page )); } Ok(out) diff --git a/skills/devstack/SKILL.md b/skills/devstack/SKILL.md index 55fb00f7..ed0d7917 100644 --- a/skills/devstack/SKILL.md +++ b/skills/devstack/SKILL.md @@ -56,7 +56,7 @@ Run from the repository root (devstack looks for `{{devstack:stack_file}}` there | `devstack builds ` | the service's builds, newest first: live, failed (with the reason), superseded by a newer change, held; answers "did my change ever go live" and "since when is it broken" | | `devstack hot-reload on\|off` | for a debugging session: the toolchain's watcher (e.g. `dotnet watch`) from the source tree instead of builds and candidates; needs `hot_reload` in the stack file. Switch it off again afterwards | | `devstack up` in a `git worktree` | starts that worktree's **lane** (every worktree is one) in the main tree's daemon: its own builds, ports moved by +100 (+200, ...), its own containers (a Compose project per lane, ports moved alike; `shared` services come from the main tree), no background workers (`--workers` turns them on; `--force` skips the memory check). Every command in the worktree then acts on the lane (`devstack lane list` shows all) | -| `git worktree add -b ` | a new worktree is a lane at once, not started (devstack never creates or removes worktrees; use git or your tool). A lane runs from a copy of its worktree, so `git worktree remove` works while it runs and stops it. `devstack lane stop ` stops a lane and keeps the worktree | +| `git worktree add -b ` | a new worktree is a lane at once, not started (devstack never creates or removes worktrees; use git or your tool); tell the user its dashboard page (`devstack ui --link` in it). A lane runs from a copy of its worktree, so `git worktree remove` works while it runs and stops it. `devstack lane stop ` stops a lane and keeps the worktree | | `devstack lane start [--on ]` | start a lane from the main tree (or anywhere in the repository), as `devstack up` in its worktree does; `status` in the main tree lists the lanes in one line. **To run another branch or commit beside the main stack** (an end-to-end run against a given commit, a review): `git worktree add `, `devstack lane start ` (its ports are the main ports plus its offset, `devstack lane list`), test against those, `devstack lane stop `, `git worktree remove `. **For an acceptance run** use the stack file's profile if it has one (`status` lists none; `[profiles]` in the file): `devstack lane start --profile

--run` starts the lane as the profile says (a build instead of a dev server, workers, fresh data, the suite's env) and runs its suite command. A worktree another tool or script created is a lane too: starting it gives its own ports, containers and database without building a separate environment | | `devstack lane reset ` / `devstack lane prune [--older-than 7d] [--dry-run]` | throw a stopped lane's data away (it starts fresh, seeded again); remove what lanes whose worktree is gone left behind, here and on the pool's nodes. A lane on a node keeps its data there between starts. Ask the user before either: data goes | | `devstack promote --lane ` | the lane's services, dev servers included, answer on the main ports; ask the user first, `--lane main` gives them back. A lane on a node may take them only on a dedicated node (a server) | @@ -75,7 +75,7 @@ Run from the repository root (devstack looks for `{{devstack:stack_file}}` there | `devstack start ` / `stop ` | one service | | `devstack run ` | a command of the stack file (list below) | | `devstack top` | CPU, memory, disk per service, container, group and lane | -| `devstack ui` | dashboard in the browser (pairs the browser once); `--browser chrome\|edge\|firefox\|brave\|`, else `DEVSTACK_BROWSER`, else `BROWSER` picks the browser, `--print` only prints the link | +| `devstack ui` | dashboard in the browser, on this stack (in a worktree on its lane; `--lane ` on another), and pairs the browser once; `--browser chrome\|edge\|firefox\|brave\|`, else `DEVSTACK_BROWSER`, else `BROWSER` picks the browser. `--link` prints the lasting links instead (see "Tell the user where to look"), `--print` the one-time pairing link | | `devstack hub notify` | the desktop notifications the hub shows (a build failed, a candidate held, a service crashed, a lane's start on a node failed) and whether they show; `on\|off …\|all`, `test`. The user's own choice: change it only when asked | | `devstack update` | install the newest devstack (`--check` only asks); stacks switch at their next restart | | `devstack down` | stop the services and the daemon; containers keep running. `down --infra` removes them too. In the main tree, lanes that run keep the daemon and the infrastructure (only the main tree's services stop); `down --all` stops the lanes too: ask first, they may be someone else's | @@ -121,6 +121,19 @@ devstack status # every service healthy / running / completed, no "n Check, do not assume: if something "does not run", `devstack status` and the port tell within a second, and they are the evidence to quote. +### Tell the user where to look + +Whenever you started the stack or a lane, or created a worktree, give the user +its dashboard page: one link to its services, logs, builds and events. +`devstack status`, `devstack up` and `devstack lane start` end with it +(`dashboard: http://127.0.0.1:/#/s/?lane=`, an empty +`lane=` is the main working tree); for a worktree not started yet, +`devstack ui --link` in it (or `--lane ` from the main tree) prints it. +Quote it as printed, never build it yourself. The page opens in a browser that +was paired before; `devstack ui --link` also prints a +`devstack://open?stack=…&lane=…` link that pairs the browser first (where the +chat or terminal makes it clickable), and `devstack ui` opens the page paired. + ## 5. Agents: MCP `devstack mcp` is an MCP server (stdio) with `status`, `up` (`on` for a lane on a @@ -128,7 +141,8 @@ node), `down`, `start_service`, `stop_service`, `restart_service`, `promote_service`, `rollback_service`, `hot_reload`, `logs`, `builds`, `diagnostics`, `doctor`, `run_command`, `usage`, `lanes`, `lane_start`, `lane_stop`, `lane_move`, `lane_share`, `promote_lane`, `placement`, -`hub_status`, `hub_start`. `lane_start` and `lane_stop` work from the main tree too: an +`hub_status`, `hub_start`. `status`, `up` and `lane_start` end with the +`dashboard:` page, `lanes` names each lane's. `lane_start` and `lane_stop` work from the main tree too: an agent there runs another worktree's lane without moving to it. `up` also takes `profile`, `run` and `yes` (as `--profile`, `--run`, `--yes`) and `timeout_s` (how long it waits for the services; the stack keeps starting);