Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<id>?lane=<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 <name>` 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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ Every command and flag: `devstack --help`, `devstack <command> --help`.
| `devstack builds <service>`, `logs`, `top`, `env <service> [--export dotenv]` | what ran, what it printed, what it uses, what it gets |
| `devstack run <command>` | 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 <name\|path>] [--print]` | open and pair the dashboard; the browser is `--browser`, else `DEVSTACK_BROWSER`, else `BROWSER`, else the system's default |
| `devstack ui [--lane <name>] [--browser <name\|path>] [--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: <url>` |
| `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) |
Expand Down
37 changes: 31 additions & 6 deletions crates/devstack/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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",
Expand All @@ -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<String> {
let c = Client::probe(&state_dir(stack_file).ok()?).ok().flatten()?;
let wt = worktree()?;
let lanes: Vec<Value> = 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<String> {
Expand Down
10 changes: 8 additions & 2 deletions crates/devstack/src/dispatch/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
88 changes: 86 additions & 2 deletions crates/devstack/src/hubcmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -262,6 +263,41 @@ pub fn pairing_url() -> Result<String> {
.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/<id>?lane=<lane>`.
pub route: String,
/// The page, for a browser paired before.
pub page: String,
/// `devstack://open?stack=<id>&lane=<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: <page>`: 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/<id>?lane=`), paired
/// like `devstack ui`: a click on a notification (`devstack://open`).
pub fn open_dashboard(route: &str) -> Result<()> {
Expand All @@ -285,8 +321,39 @@ fn chosen_browser(explicit: Option<&str>) -> Option<String> {
.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(());
Expand Down Expand Up @@ -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;
Expand Down
37 changes: 35 additions & 2 deletions crates/devstack/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// 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,
Expand Down Expand Up @@ -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 { .. }) => {
Expand Down Expand Up @@ -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!(
Expand Down
27 changes: 22 additions & 5 deletions crates/devstack/src/mcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,12 @@ fn checked(status: u16, body: String) -> Result<String> {
Ok(body)
}

/// `dashboard: <page>` 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<String> {
match name {
"status" => {
Expand All @@ -476,6 +482,8 @@ fn call(name: &str, args: &Value, file: &Path) -> Result<String> {
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" => {
Expand Down Expand Up @@ -551,7 +559,11 @@ fn call(name: &str, args: &Value, file: &Path) -> Result<String> {
}
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())
{
Expand Down Expand Up @@ -654,7 +666,11 @@ fn call(name: &str, args: &Value, file: &Path) -> Result<String> {
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!(
Expand Down Expand Up @@ -735,17 +751,18 @@ fn call(name: &str, args: &Value, file: &Path) -> Result<String> {
} 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(""),
if l["owns_main"] == true {
", serves the main ports"
} else {
""
}
},
crate::hubcmd::dashboard_links(&client::daemon_file(file), Some(name)).page
));
}
Ok(out)
Expand Down
Loading