RightNow-AI/openfang · error · BrowserResponse

Back failed: {e}

Error message

Back failed: {e}

What it means

cmd_back first runs history.back() through CDP. If that evaluation itself returns Err — before any navigation is even attempted successfully — the library short-circuits with this error. Unlike error 32, navigation did NOT happen; the failure is in issuing/executing the history.back() JS on the current page (connection or context problem), or the JS threw.

Source

Thrown at crates/openfang-runtime/src/browser.rs:617

        match self.cdp.run_js(expression).await {
            Ok(val) => BrowserResponse::ok(serde_json::json!({"result": val})),
            Err(e) => BrowserResponse::err(format!("JS execution failed: {e}")),
        }
    }

    async fn cmd_back(&self) -> BrowserResponse {
        match self.cdp.run_js("history.back(); 'ok'").await {
            Ok(_) => {
                tokio::time::sleep(Duration::from_millis(500)).await;
                self.wait_for_load().await;
                match self.page_info().await {
                    Ok(info) => BrowserResponse::ok(info),
                    Err(e) => {
                        BrowserResponse::err(format!("Back succeeded but page info failed: {e}"))
                    }
                }
            }
            Err(e) => BrowserResponse::err(format!("Back failed: {e}")),
        }
    }

    // ── Helpers ────────────────────────────────────────────────────────

    /// Poll until document.readyState is 'complete' or 'interactive'.
    async fn wait_for_load(&self) {
        for _ in 0..PAGE_LOAD_MAX_POLLS {
            if let Ok(val) = self.cdp.run_js("document.readyState").await {
                let state = val.as_str().unwrap_or("");
                if state == "complete" || state == "interactive" {
                    return;
                }
            }
            tokio::time::sleep(Duration::from_millis(PAGE_LOAD_POLL_INTERVAL_MS)).await;
        }
    }

View on GitHub (pinned to acf2587e46)

Solutions

  1. Check the browser/CDP session health and reconnect or relaunch the browser if the connection is gone.
  2. Inspect the embedded {e}: 'Execution context was destroyed' → wait for the pending navigation to finish, then retry back.
  3. If the main thread is blocked by page script, force navigation via CDP Page.navigateTo (history entry) instead of history.back().
  4. Retry cmd_back after wait_for_load-style polling shows an evaluable readyState.
  5. Verify the history stack actually has a previous entry (history.length > 1) to rule out a no-op followed by a context error.

Example fix

// before: unconditional back
browser.cmd_back();
// after: ensure evaluable page, retry once
if browser.cmd_back().is_err() {
    browser.wait_for_load().await;
    browser.cmd_back().or_else(|_| browser.cmd_run_js("location.href = document.referrer"));
}
Defensive patterns

Strategy: retry

Validate before calling

let probe = browser.cmd_run_js("history.length");
if probe.is_err() || probe.result.as_i64().unwrap_or(1) < 2 { /* no history to go back to, or session is dead */ }

Type guard

fn is_back_failure(resp: &BrowserResponse) -> bool {
    resp.error.as_deref().map(|e| e.starts_with("Back failed:")).unwrap_or(false)
}

Try / catch

match browser.cmd_back() {
    Ok(r) => r,
    Err(e) => {
        if !session_alive(browser) { reconnect_browser()?; }
        wait_for_load();
        browser.cmd_back().map_err(|e2| format!("back retry failed: {e} / {e2}"))
    }
}

Prevention

When it happens

Trigger: CDP connection closed or browser tab crashed when cmd_back is called; the current execution context was destroyed by an in-flight navigation or page crash so Runtime.evaluate fails; the current page's script environment is unresponsive (blocked main thread), making the evaluation time out.

Common situations: Calling back on a page that just triggered its own navigation; long-running automation where the Chrome session died; pages with a hung synchronous script freezing the main thread; browser closed by an earlier timeout while the tool session persists.

Related errors


AI-assisted analysis of RightNow-AI/openfang@acf2587e46 (2026-09-02). Data as JSON: /api/errors/9e39ac9cf50b701b. Report an issue: GitHub.