fix(tabs): correct --tab scoped commands and un-break provider direct-page path (#1249)
* fix(tabs): initialize tab_id on missing PageInfo sites PR #892 added a required `tab_id: u32` field to `PageInfo` but missed two initializer sites, which broke the build on the PR branch. CI never caught this because the external-contributor workflow status was `action_required` and never ran. - `cli/src/native/browser.rs:395` — the `direct_page` branch of `connect_cdp_inner` used by the cloud providers (Browserbase, Browserless, Browser Use, Kernel, AgentCore). Use `assign_tab_id()` to get a fresh id. - `cli/src/native/browser.rs:1580` — a unit test initializer. Use `tab_id: 1` since the test doesn't exercise id assignment. * feat(tabs): restore active tab and clear per-tab state for scoped --tab Follow-up on PR #892's `--tab <id>` flag. The original implementation called `tab_switch_by_id` directly from the pre-dispatch block in `execute_command` but didn't touch the daemon's per-tab state, and never restored the previously-active tab. Two concrete issues this fixes: 1. `state.ref_map`, `state.iframe_sessions`, and `state.active_frame_id` were left intact across the pre-dispatch switch, so `--tab N click @e1` would try to resolve `@e1` against the scoped tab's DOM using a backend-node id from the outer tab. In practice the click handler's role+name fallback hid this as "element not found" errors, but on pages where both tabs have similarly-labelled elements it could click the wrong one. 2. The PR description promised scoped routing would "restore the previous active tab", but the implementation permanently switched. `--tab 3 snapshot` would leave tab 3 as the active tab even after the command returned, surprising subsequent non-scoped commands. This change: - Saves the current tab's stable `tab_id` (not its array index, which would shift if the scoped command closed other tabs) before switching. - Clears per-tab daemon state before the switch so refs/iframes/frame context can't leak between tabs. - After the action runs, restores the original active tab (also via stable id) unless that tab was closed during the scoped command, in which case we leave the scoped tab active. - Adds `BrowserManager::active_tab_id()` and `has_tab_id()` accessors to support the above without exposing the internal `pages` vector. * test(tabs): regression tests for scoped --tab state clearing and restoration Three new `#[ignore]` e2e tests pinning the fixed behavior: - `e2e_tab_scoped_command_clears_state_on_switch` — populates `ref_map` on tab 1, runs a `tabId: 2`-scoped command, asserts `ref_map`, `iframe_sessions`, and `active_frame_id` are all cleared. - `e2e_tab_scoped_command_restores_active_tab` — sets up two tabs, runs a scoped command against the non-active one, asserts a subsequent unscoped command reflects the originally-active tab. - `e2e_tab_scoped_command_handles_outer_tab_closed` — runs a scoped `tab_close` that kills the outer tab itself, asserts no error and the scoped tab becomes active. Also updates two misleading comments in the PR's existing `e2e_tab_global_targeting*` tests to reflect restoration semantics; the assertions themselves were already consistent with restoration. * docs(tabs): document stable tab IDs and --tab scoped-command flag Per AGENTS.md, changes that users or agents would need to know about must land in every doc surface. Fills the gaps PR #892 left: - `README.md` — new `--tab <id>` row in the Options table, rewrite the tab command examples to use `<id>` instead of `<n>`, add a paragraph explaining stable tab IDs and `--tab` peek semantics. - `docs/src/app/commands/page.mdx` — same command-example rewrite plus a new "Stable tab IDs and `--tab`" subsection. - `docs/src/app/configuration/page.mdx` — add `tab` row to the config options table so JSON config users can discover it. - `agent-browser.schema.json` — add `tab` property with description, matching the config schema. - `skills/agent-browser/references/commands.md` — same command-example rewrite plus a short paragraph for agents on when to use `--tab`.
This commit is contained in:
+172
-2
@@ -1093,7 +1093,8 @@ async fn e2e_tab_global_targeting() {
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
// After targeting tab 1 then tab 2, active tab is now tab 2
|
||||
// Active tab was never changed by the scoped `tabId` commands
|
||||
// (restoration semantics), so it's still tab 2 from the earlier `tab_new`.
|
||||
assert_eq!(get_data(&resp)["result"], "Page B");
|
||||
|
||||
let resp = execute_command(&json!({ "id": "99", "action": "close" }), &mut state).await;
|
||||
@@ -1167,7 +1168,8 @@ async fn e2e_tab_global_targeting_snapshot() {
|
||||
snapshot
|
||||
);
|
||||
|
||||
// Snapshot without tabId should use the last-switched tab (tab 2)
|
||||
// Snapshot without tabId uses the still-active tab 2 (restoration
|
||||
// semantics: scoped commands don't change the active tab).
|
||||
let resp = execute_command(&json!({ "id": "6", "action": "snapshot" }), &mut state).await;
|
||||
assert_success(&resp);
|
||||
let snapshot = get_data(&resp)["snapshot"].as_str().unwrap();
|
||||
@@ -1289,6 +1291,174 @@ async fn e2e_tab_global_targeting_snapshot_non_contiguous() {
|
||||
assert_success(&resp);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// `--tab` / `tabId` scoped-command regression tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// `tabId`-scoped commands must clear `state.ref_map`, `state.iframe_sessions`,
|
||||
/// and `state.active_frame_id` when they temporarily switch tabs, otherwise
|
||||
/// refs from the outer tab would resolve against the scoped tab's DOM.
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
async fn e2e_tab_scoped_command_clears_state_on_switch() {
|
||||
let mut state = DaemonState::new();
|
||||
|
||||
let resp = execute_command(
|
||||
&json!({ "id": "1", "action": "launch", "headless": true }),
|
||||
&mut state,
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
|
||||
let resp = execute_command(
|
||||
&json!({ "id": "2", "action": "navigate", "url": "data:text/html,<button>Alpha</button>" }),
|
||||
&mut state,
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
|
||||
let resp = execute_command(
|
||||
&json!({ "id": "3", "action": "tab_new", "url": "data:text/html,<p>Beta</p>" }),
|
||||
&mut state,
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
assert_eq!(get_data(&resp)["tabId"], 2);
|
||||
|
||||
let resp = execute_command(
|
||||
&json!({ "id": "4", "action": "tab_switch", "tabId": 1 }),
|
||||
&mut state,
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
|
||||
let resp = execute_command(&json!({ "id": "5", "action": "snapshot" }), &mut state).await;
|
||||
assert_success(&resp);
|
||||
assert!(
|
||||
state.ref_map.get("e1").is_some(),
|
||||
"snapshot should populate @e1 on tab 1"
|
||||
);
|
||||
|
||||
// Run a tabId-scoped command. The pre-dispatch must clear per-tab state
|
||||
// so nothing can leak into the scoped tab's context.
|
||||
let resp = execute_command(
|
||||
&json!({ "id": "6", "action": "title", "tabId": 2 }),
|
||||
&mut state,
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
|
||||
assert!(
|
||||
state.ref_map.get("e1").is_none(),
|
||||
"ref_map must be cleared when a tabId-scoped command switches tabs, \
|
||||
but @e1 is still present: {:?}",
|
||||
state.ref_map.get("e1")
|
||||
);
|
||||
assert!(state.iframe_sessions.is_empty());
|
||||
assert!(state.active_frame_id.is_none());
|
||||
|
||||
let resp = execute_command(&json!({ "id": "99", "action": "close" }), &mut state).await;
|
||||
assert_success(&resp);
|
||||
}
|
||||
|
||||
/// `tabId`-scoped commands must restore the original active tab afterward so
|
||||
/// `--tab N` is a non-intrusive peek that doesn't change the user's context.
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
async fn e2e_tab_scoped_command_restores_active_tab() {
|
||||
let mut state = DaemonState::new();
|
||||
|
||||
let resp = execute_command(
|
||||
&json!({ "id": "1", "action": "launch", "headless": true }),
|
||||
&mut state,
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
|
||||
let resp = execute_command(
|
||||
&json!({ "id": "2", "action": "navigate", "url": "data:text/html,<title>A</title>" }),
|
||||
&mut state,
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
|
||||
let resp = execute_command(
|
||||
&json!({ "id": "3", "action": "tab_new", "url": "data:text/html,<title>B</title>" }),
|
||||
&mut state,
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
assert_eq!(get_data(&resp)["tabId"], 2);
|
||||
|
||||
// Active tab is 2. Peek at tab 1 with a scoped command.
|
||||
let resp = execute_command(
|
||||
&json!({ "id": "4", "action": "title", "tabId": 1 }),
|
||||
&mut state,
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
assert_eq!(get_data(&resp)["title"], "A", "tabId should route to tab 1");
|
||||
|
||||
// No tabId: must reflect the originally active tab (tab 2).
|
||||
let resp = execute_command(&json!({ "id": "5", "action": "title" }), &mut state).await;
|
||||
assert_success(&resp);
|
||||
assert_eq!(
|
||||
get_data(&resp)["title"],
|
||||
"B",
|
||||
"active tab should be restored to tab 2 after the scoped command; \
|
||||
got the scoped tab's title instead"
|
||||
);
|
||||
|
||||
let resp = execute_command(&json!({ "id": "99", "action": "close" }), &mut state).await;
|
||||
assert_success(&resp);
|
||||
}
|
||||
|
||||
/// Restoration must be skipped (without error) if the scoped command closes
|
||||
/// the tab that was active before the switch.
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
async fn e2e_tab_scoped_command_handles_outer_tab_closed() {
|
||||
let mut state = DaemonState::new();
|
||||
|
||||
let resp = execute_command(
|
||||
&json!({ "id": "1", "action": "launch", "headless": true }),
|
||||
&mut state,
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
|
||||
let resp = execute_command(
|
||||
&json!({ "id": "2", "action": "navigate", "url": "data:text/html,<title>A</title>" }),
|
||||
&mut state,
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
|
||||
let resp = execute_command(
|
||||
&json!({ "id": "3", "action": "tab_new", "url": "data:text/html,<title>B</title>" }),
|
||||
&mut state,
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
|
||||
// Active tab is 2. Peek at tab 1 with a command that also closes tab 2.
|
||||
// The restoration path must not error when it discovers tab 2 is gone;
|
||||
// we treat "outer tab vanished" as an implicit accept of the scoped tab.
|
||||
let resp = execute_command(
|
||||
&json!({ "id": "4", "action": "tab_close", "tabId": 2 }),
|
||||
&mut state,
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
|
||||
let resp = execute_command(&json!({ "id": "5", "action": "title" }), &mut state).await;
|
||||
assert_success(&resp);
|
||||
assert_eq!(get_data(&resp)["title"], "A");
|
||||
|
||||
let resp = execute_command(&json!({ "id": "99", "action": "close" }), &mut state).await;
|
||||
assert_success(&resp);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Element queries: isvisible, isenabled, gettext, getattribute
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user