WeKnora/patches/browserskill/06-navigation-lifecycle.patch

157 lines
6.8 KiB
Diff

diff --git a/apps/extension/src/tools/__tests__/navigation.test.ts b/apps/extension/src/tools/__tests__/navigation.test.ts
index 921771e..ca82df7 100644
--- a/apps/extension/src/tools/__tests__/navigation.test.ts
+++ b/apps/extension/src/tools/__tests__/navigation.test.ts
@@ -376,6 +376,43 @@ describe("handleNavigate", () => {
expect(res.reached).toBe("load");
});
+ it("follows a committed main-frame redirect without accepting stale or child-frame events", async () => {
+ const sm = new SessionManager({ agentWindow: fakeAgentWindow([100]) });
+ await sm.start("aa11");
+ const fake = makeFakeCdp({ beforeLoaderId: "old", navigateLoaderId: "initial" });
+ let finished = false;
+ const navP = handleNavigate(
+ sm,
+ {
+ session_id: "aa11",
+ url: "https://example.com/redirect",
+ wait_until: "domcontentloaded",
+ timeout_ms: 1000,
+ },
+ { cdp: fake.cdp, tabsApi: fake.tabsApi },
+ ).then((result) => {
+ finished = true;
+ return result;
+ });
+ await vi.waitFor(() =>
+ expect(fake.sent.some((call) => call.method === "Page.navigate")).toBe(true),
+ );
+ fake.fireFrameNavigated("child", "child-loader");
+ fake.fireLifecycle("DOMContentLoaded", "frame-1", "child-loader");
+ fake.fireLifecycle("DOMContentLoaded", "frame-1", "redirect");
+ await Promise.resolve();
+ expect(finished).toBe(false);
+ fake.fireFrameNavigated("frame-1", "redirect");
+ fake.fireLifecycle("DOMContentLoaded", "frame-1", "initial");
+ fake.fireFrameNavigated("frame-1", "old");
+ fake.fireLifecycle("DOMContentLoaded", "frame-1", "old");
+ await Promise.resolve();
+ expect(finished).toBe(false);
+ fake.fireLifecycle("DOMContentLoaded", "frame-1", "redirect");
+ expect(await navP).toMatchObject({ reached: "domcontentloaded" });
+ expect(fake.listeners).toHaveLength(0);
+ });
+
it("returns reached=timeout when no matching lifecycle fires", async () => {
const sm = new SessionManager({ agentWindow: fakeAgentWindow([100]) });
await sm.start("aa11");
diff --git a/apps/extension/src/tools/navigation.ts b/apps/extension/src/tools/navigation.ts
index e40d724..58d3ea6 100644
--- a/apps/extension/src/tools/navigation.ts
+++ b/apps/extension/src/tools/navigation.ts
@@ -236,6 +236,13 @@ function startLifecycleWait(
const promise = new Promise<WaitOutcome>((resolve) => {
let settled = false;
let sawRelevantLifecycle = false;
+ // The Page.navigate reply describes the initial loader. A client-side
+ // redirect may commit a different document before that loader finishes.
+ // Only a top-level frameNavigated event may advance the expected loader.
+ let committedLoaderId = "";
+ const activeGuard = guard
+ ? { ...guard, loaderId: () => committedLoaderId || currentLoaderId(guard) }
+ : undefined;
let lastLifecycle: string | undefined;
let pendingLifecycle: { name: string; frameId?: string; loaderId?: string } | null = null;
let listenerSub: { dispose(): void } | null = null;
@@ -260,7 +267,7 @@ function startLifecycleWait(
eventFrameId?: string,
eventLoaderId?: string,
): boolean => {
- if (!eventLoaderIsRelevant(eventLoaderId, guard)) return false;
+ if (!eventLoaderIsRelevant(eventLoaderId, activeGuard)) return false;
if (!lifecycleEventMatchesFrame(eventFrameId)) return false;
noteRelevantLifecycle(name);
return true;
@@ -331,16 +338,19 @@ function startLifecycleWait(
if (settled) return;
if (source.tabId !== expectedTabId) return;
- if (targetName === "commit" && method === "Page.frameNavigated") {
+ if (method === "Page.frameNavigated") {
const p = params as { frame?: { id?: string; parentId?: string; loaderId?: string } };
+ if (p.frame?.parentId) return;
const expectedFrameId = currentFrameId();
if (expectedFrameId.length > 0) {
if (p.frame?.id !== expectedFrameId) return;
- } else if (p.frame?.parentId) {
- return;
+ }
+ if (guard && p.frame?.loaderId) {
+ if (p.frame.loaderId === guard.beforeLoaderId) return;
+ committedLoaderId = p.frame.loaderId;
}
if (!acceptLifecycleEvent("commit", p.frame?.id, p.frame?.loaderId)) return;
- finish({ reached: "match", lastLifecycle: "commit" });
+ if (targetName === "commit") finish({ reached: "match", lastLifecycle: "commit" });
return;
}
@@ -362,7 +372,7 @@ function startLifecycleWait(
}
return;
}
- if (!eventLoaderIsRelevant(p.loaderId, guard)) return;
+ if (!eventLoaderIsRelevant(p.loaderId, activeGuard)) return;
if (!lifecycleEventMatchesFrame(p.frameId)) return;
pendingLifecycle = { name: p.name, frameId: p.frameId, loaderId: p.loaderId };
return;
diff --git a/crates/bsk-cli/src/daemon/ipc.rs b/crates/bsk-cli/src/daemon/ipc.rs
index 69daf58..4695d48 100644
--- a/crates/bsk-cli/src/daemon/ipc.rs
+++ b/crates/bsk-cli/src/daemon/ipc.rs
@@ -793,7 +793,14 @@ fn tool_dispatch_transport_timeout(method: &Method, params: &Value) -> Result<Du
timeout.map(|timeout| {
if matches!(
method,
- Method::ToolUpload | Method::ToolDownload | Method::ToolRequestHelp
+ Method::ToolUpload
+ | Method::ToolDownload
+ | Method::ToolRequestHelp
+ | Method::ToolNavigate
+ | Method::ToolNavigateBack
+ | Method::ToolNavigateForward
+ | Method::ToolReload
+ | Method::ToolWaitForNavigation
) {
timeout.saturating_add(EXTENSION_RESPONSE_GRACE)
} else {
@@ -1820,12 +1827,27 @@ mod tests {
Method::ToolUpload,
Method::ToolDownload,
Method::ToolRequestHelp,
+ Method::ToolNavigate,
+ Method::ToolNavigateBack,
+ Method::ToolNavigateForward,
+ Method::ToolReload,
+ Method::ToolWaitForNavigation,
] {
let dispatch_timeout = tool_dispatch_transport_timeout(&method, &params).unwrap();
assert_eq!(dispatch_timeout, Duration::from_secs(62));
}
}
+ #[test]
+ fn default_navigation_deadline_allows_extension_timeout_response() {
+ for method in [Method::ToolNavigate, Method::ToolWaitForNavigation] {
+ assert_eq!(
+ tool_dispatch_transport_timeout(&method, &serde_json::json!({})).unwrap(),
+ DEFAULT_TOOL_TIMEOUT + EXTENSION_RESPONSE_GRACE
+ );
+ }
+ }
+
#[test]
fn extension_response_grace_does_not_change_other_tools() {
let params = serde_json::json!({