Skip to content
Open
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
7 changes: 2 additions & 5 deletions cli/src/native/stream/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,11 +548,8 @@ pub(crate) async fn execute_chat_tool(session: &str, command: &str, was_open: bo
Err(e) => return format!("Failed to resolve executable: {}", e),
};

let mut cmd = tokio::process::Command::new(&exe);
cmd.args(&args)
.env_remove("AGENT_BROWSER_DASHBOARD")
.env_remove("AGENT_BROWSER_DASHBOARD_PORT")
.env_remove("AGENT_BROWSER_STREAM_PORT");
let mut cmd = super::dashboard::cli_command(&exe);
cmd.args(&args);

match cmd.output().await {
Ok(output) => {
Expand Down
35 changes: 28 additions & 7 deletions cli/src/native/stream/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,17 @@ async fn read_post_body(stream: &mut tokio::net::TcpStream, initial: &[u8], n: u
String::from_utf8(body).unwrap_or_default()
}

/// Runs the CLI as a client of a session daemon. The dashboard server runs
/// with `AGENT_BROWSER_DASHBOARD` set, and a child that inherits it starts a
/// second dashboard server instead of running its command.
pub(super) fn cli_command(exe: &std::path::Path) -> tokio::process::Command {
let mut cmd = tokio::process::Command::new(exe);
cmd.env_remove("AGENT_BROWSER_DASHBOARD")
.env_remove("AGENT_BROWSER_DASHBOARD_PORT")
.env_remove("AGENT_BROWSER_STREAM_PORT");
cmd
}

async fn exec_cli(body: &str) -> Result<String, String> {
let parsed: Value = serde_json::from_str(body).map_err(|e| format!("Invalid JSON: {}", e))?;
let args: Vec<String> = parsed
Expand All @@ -912,12 +923,8 @@ async fn exec_cli(body: &str) -> Result<String, String> {

let exe = std::env::current_exe().map_err(|e| format!("Cannot resolve executable: {}", e))?;

let mut cmd = tokio::process::Command::new(&exe);
cmd.args(&args)
.arg("--json")
.env_remove("AGENT_BROWSER_DASHBOARD")
.env_remove("AGENT_BROWSER_DASHBOARD_PORT")
.env_remove("AGENT_BROWSER_STREAM_PORT");
let mut cmd = cli_command(&exe);
cmd.args(&args).arg("--json");

let output = cmd
.output()
Expand Down Expand Up @@ -992,7 +999,7 @@ pub(super) async fn spawn_session(body: &str) -> Result<String, String> {

let exe = std::env::current_exe().map_err(|e| format!("Cannot resolve executable: {}", e))?;

let mut cmd = tokio::process::Command::new(&exe);
let mut cmd = cli_command(&exe);
cmd.arg("open")
.arg("about:blank")
.arg("--session")
Expand Down Expand Up @@ -1048,6 +1055,20 @@ mod tests {
);
}

#[test]
fn cli_command_clears_dashboard_mode_for_the_child() {
let cmd = cli_command(std::path::Path::new("agent-browser"));
let removed: Vec<_> = cmd
.as_std()
.get_envs()
.filter_map(|(key, value)| value.is_none().then_some(key))
.collect();

for name in ["AGENT_BROWSER_DASHBOARD", "AGENT_BROWSER_DASHBOARD_PORT"] {
assert!(removed.contains(&std::ffi::OsStr::new(name)), "{name}");
}
}

async fn send_request_to_dashboard_handler(request: &str) -> String {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
Expand Down