Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

28p address-bar-own-tab: a stream's address bar shows its own page (#810)

Oleksandr Ivanchenkoc8bc37d25d7d
diff --git a/src/server/browser_stream.rs b/src/server/browser_stream.rs
index c2319abc..f8a02955 100644
--- a/src/server/browser_stream.rs
+++ b/src/server/browser_stream.rs
@@ -535,13 +535,19 @@ fn refresh_location(cdp: &Cdp, ctrl: &BrowserController, shared: &Arc<Shared>, c
});
}
-/// The `location` message for the page `cdp` is on (its address and title, also kept on the
-/// controller); `None` when the page doesn't answer within `PROBE_WAIT` (a dialog is open).
+/// The `location` message for the page `cdp` is on: the address and title this read got from
+/// that page, also kept on the controller. The controller's fields are shared with the bot's own
+/// page (its navigation writes them too), so the message is never built from them: a stream on
+/// the Shared tab would show the bot's page's address. `None` when the page doesn't answer within
+/// `PROBE_WAIT` (a dialog is open) or answers without an address; a missing title is sent empty.
async fn read_location(cdp: &Cdp, ctrl: &BrowserController) -> Option<Value> {
let result = cdp.call("Runtime.evaluate", json!({"expression": "({url: location.href, title: document.title})", "returnByValue": true}), PROBE_WAIT).await.ok()?;
- if let Some(url) = result["result"]["value"]["url"].as_str() { *ctrl.current_url.lock().await = Some(url.to_string()); }
- if let Some(title) = result["result"]["value"]["title"].as_str() { *ctrl.current_title.lock().await = Some(title.to_string()); }
- Some(json!({"type":"location","url":ctrl.current_url.lock().await.clone(),"title":ctrl.current_title.lock().await.clone()}))
+ let read = &result["result"]["value"];
+ let url = read["url"].as_str()?.to_string();
+ *ctrl.current_url.lock().await = Some(url.clone());
+ let title = read["title"].as_str().map(str::to_string);
+ if let Some(title) = &title { *ctrl.current_title.lock().await = Some(title.clone()); }
+ Some(json!({"type":"location","url":url,"title":title.unwrap_or_default()}))
}
impl Shared {
@@ -2250,6 +2256,76 @@ mod tests {
/// Take over from a second window while the first has control is `held_by_person`, never
/// the busy bot's error; a bot mid-step is `bot_busy`; once the first window hands back (its
/// lease dropped) the second can take over.
+ /// A fake page on its own DevTools socket: it answers `Runtime.evaluate` with whatever
+ /// `answer` holds at that moment (its address and title).
+ async fn fake_page(answer: Arc<std::sync::Mutex<Value>>) -> Cdp {
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let addr = listener.local_addr().unwrap();
+ tokio::spawn(async move {
+ let (tcp, _) = listener.accept().await.unwrap();
+ let mut ws = tokio_tungstenite::accept_async(tcp).await.unwrap();
+ while let Some(Ok(CdpMessage::Text(text))) = ws.next().await {
+ let call: Value = serde_json::from_str(&text).unwrap();
+ let value = answer.lock().unwrap().clone();
+ let reply = json!({"id": call["id"], "result": {"result": {"type": "object", "value": value}}});
+ if ws.send(CdpMessage::Text(reply.to_string())).await.is_err() { break; }
+ }
+ });
+ let (ws, _) = tokio_tungstenite::connect_async(format!("ws://{addr}/devtools/page/x")).await.unwrap();
+ Cdp::open(ws).0
+ }
+
+ /// 28p: the Shared tab's stream and the bot's own page share one controller, and the bot's
+ /// navigation writes the controller's address and title. Each stream's `location` is the
+ /// address and title its own page answered, even when the bot's page moves between this
+ /// stream's write and its message, and a read without an address sends nothing.
+ #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+ async fn each_stream_shows_its_own_pages_address_while_the_other_page_navigates() {
+ let ctrl = BrowserController::new(0);
+ let shared = Arc::new(std::sync::Mutex::new(json!({"url": "https://shared.example/doc", "title": "Shared doc"})));
+ let bots = Arc::new(std::sync::Mutex::new(json!({"url": "https://bot.example/start", "title": "Bot start"})));
+ let shared_tab = fake_page(shared.clone()).await;
+ let bot_page = fake_page(bots.clone()).await;
+ let location = |url: &str, title: &str| json!({"type": "location", "url": url, "title": title});
+
+ assert_eq!(read_location(&shared_tab, &ctrl).await, Some(location("https://shared.example/doc", "Shared doc")));
+ assert_eq!(read_location(&bot_page, &ctrl).await, Some(location("https://bot.example/start", "Bot start")));
+
+ // the bot's page navigates (browser.rs writes the controller), then the Shared tab reads again
+ *bots.lock().unwrap() = json!({"url": "chrome-error://chromewebdata/", "title": "bot.example"});
+ *ctrl.current_url.lock().await = Some("chrome-error://chromewebdata/".into());
+ *ctrl.current_title.lock().await = Some("bot.example".into());
+ assert_eq!(read_location(&shared_tab, &ctrl).await, Some(location("https://shared.example/doc", "Shared doc")));
+ assert_eq!(read_location(&bot_page, &ctrl).await, Some(location("chrome-error://chromewebdata/", "bot.example")));
+
+ // the bot's navigation lands while the Shared tab's read is between its write and its message
+ let title_held = ctrl.current_title.lock().await;
+ let read = tokio::spawn({
+ let (cdp, ctrl) = (shared_tab.clone(), ctrl.clone());
+ async move { read_location(&cdp, &ctrl).await }
+ });
+ let deadline = Instant::now() + Duration::from_secs(5);
+ while ctrl.current_url.lock().await.as_deref() != Some("https://shared.example/doc") {
+ assert!(Instant::now() < deadline, "the Shared tab's read never wrote its address");
+ tokio::time::sleep(Duration::from_millis(5)).await;
+ }
+ *ctrl.current_url.lock().await = Some("https://bot.example/next".into());
+ drop(title_held);
+ assert_eq!(read.await.unwrap(), Some(location("https://shared.example/doc", "Shared doc")));
+
+ // a page that answers without a title shows an empty one, never the bot's page's title
+ *ctrl.current_title.lock().await = Some("Bot next".into());
+ *shared.lock().unwrap() = json!({"url": "https://shared.example/doc"});
+ assert_eq!(read_location(&shared_tab, &ctrl).await, Some(location("https://shared.example/doc", "")));
+
+ // a page that answers without an address sends nothing, not the bot's page's address
+ *shared.lock().unwrap() = json!({"title": "Shared doc"});
+ assert_eq!(read_location(&shared_tab, &ctrl).await, None);
+ *shared.lock().unwrap() = Value::Null;
+ assert_eq!(read_location(&shared_tab, &ctrl).await, None);
+ assert_eq!(read_location(&bot_page, &ctrl).await, Some(location("chrome-error://chromewebdata/", "bot.example")));
+ }
+
#[tokio::test]
async fn takeover_refused_says_whether_the_person_or_a_bot_has_the_browser() {
let ctrl = BrowserController::new(0);