Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: fix compressed pages, router paths and early empty reads in the browser panel (#13532)

oobaboogaaea512a5a2fd
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index 5f003961b9b..764432f7053 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -36,6 +36,7 @@ import sys
import tempfile
import contextlib
import threading
+import zlib
from contextvars import ContextVar
# What a truncated result costs besides its body, charged where the cut is decided rather than held back in advance.
@@ -16192,6 +16193,80 @@ class _HTMLBodyLocator(HTMLParser):
self.handle_entityref("#" + name)
+# What _ContentDecoder undoes, and what the fetcher's Accept-Encoding names: with none, any coding is
+# acceptable (RFC 9110 12.5.3), brotli and zstd included.
+_DECODED_CODINGS = ("gzip", "x-gzip", "deflate")
+
+
+class _ContentDecoder:
+ """Undoes a gzip or deflate Content-Encoding as the body is read. Servers send one unasked (python.org
+ does even for ``Accept-Encoding: identity``). Output is bounded per read and input by ``wire_limit``,
+ so a small compressed body can't expand past the caller's budget."""
+
+ def __init__(self, coding):
+ self.gzip = coding != "deflate"
+ self.inflater = None
+ # Before the first byte, or between gzip members: an end of input here is a complete body.
+ self.at_boundary = True
+ self.members = 0
+ self.unread = b""
+ self.wire = 0
+ self.wire_limit = 0
+ self.finished = False
+
+ def read(self, read_wire, size):
+ """At most ``size`` decoded bytes. ``b""`` before ``finished`` means only input was consumed: call again."""
+ if self.finished:
+ return b""
+ data, self.unread = self.unread, b""
+ draining = False
+ if len(data) < 2 and (self.inflater is None or not data):
+ more = read_wire(65536)
+ self.wire += len(more)
+ if self.wire > self.wire_limit:
+ raise ValueError("compressed response exceeds the byte limit")
+ if not more:
+ # An empty coded body (a 204, or Content-Length: 0) has nothing to decode; after a member, a
+ # stray byte is padding.
+ if self.at_boundary and (not data or self.members):
+ self.finished = True
+ return b""
+ if self.inflater is None:
+ raise ValueError("truncated compressed response")
+ # zlib can hold output back past a full buffer with all the input consumed (raw deflate).
+ draining = True
+ data += more
+ if self.inflater is None and len(data) < 2:
+ self.unread = data
+ return b""
+ if self.inflater is None:
+ if self.gzip:
+ if self.members and not data.startswith(b"\x1f\x8b"):
+ # Bytes after the last member are padding, which browsers ignore too.
+ self.finished = True
+ return b""
+ # Concatenated gzip members are one body (RFC 1952 2.2).
+ self.inflater = zlib.decompressobj(31)
+ else:
+ # "deflate" is meant to be zlib-wrapped (RFC 9110 8.4.1.2); some servers send it raw.
+ wrapped = data[0] & 0x0F == 8 and (data[0] << 8 | data[1]) % 31 == 0
+ self.inflater = zlib.decompressobj(15 if wrapped else -15)
+ self.at_boundary = False
+ out = self.inflater.decompress(data, size)
+ if draining and not out and not self.inflater.eof:
+ raise ValueError("truncated compressed response")
+ if not self.inflater.eof:
+ self.unread = self.inflater.unconsumed_tail
+ elif self.gzip:
+ self.members += 1
+ self.unread = self.inflater.unused_data
+ self.inflater = None
+ self.at_boundary = True
+ else:
+ self.finished = True
+ return out
+
+
def _read_capped_body(
resp,
max_bytes,
@@ -16200,13 +16275,18 @@ def _read_capped_body(
cancel_event,
body_window = None,
charset = None,
+ decoder = None,
):
- """read at most ``max_bytes``, and ``body_window`` past the end of ``<head>``; returns ``(error, body)``."""
+ """read at most ``max_bytes`` (decoded, with a ``decoder``), and ``body_window`` past the end of ``<head>``; returns ``(error, body)``."""
# HTTPError exposes the socket for deadline updates; chunk checks bound test doubles without one
fp = getattr(resp, "fp", None)
sock = getattr(getattr(getattr(fp, "fp", fp), "raw", None), "_sock", None)
# read1 avoids buffered read(n) waiting for n bytes past the budget
read = getattr(resp, "read1", None) or resp.read
+ if decoder is not None:
+ read_wire = read
+ decoder.wire_limit = decoder.wire + max_bytes + 65536
+ read = lambda size: decoder.read(read_wire, size)
chunks = []
remaining = max_bytes
body_at = None
@@ -16224,8 +16304,18 @@ def _read_capped_body(
sock.settimeout(_fetch_hop_timeout(timeout, deadline))
except Exception:
pass
- chunk = read(min(65536, remaining))
+ try:
+ chunk = read(min(65536, remaining))
+ except (ValueError, zlib.error) as exc:
+ if decoder is None:
+ raise
+ if decoder.wire > decoder.wire_limit:
+ return f"(compressed content exceeds the {decoder.wire_limit} byte limit)", b""
+ return f"Failed to decode the compressed response: {exc}", b""
if not chunk:
+ # Checks the budget between compressed chunks that decode to nothing yet.
+ if decoder is not None and not decoder.finished:
+ continue
break
chunks.append(chunk)
remaining -= len(chunk)
@@ -16415,6 +16505,7 @@ def _fetch_url_raw(
headers = {
"User-Agent": ua,
"Host": validated_netloc,
+ "Accept-Encoding": "gzip, deflate",
}
if extra_headers:
headers.update(extra_headers)
@@ -16457,6 +16548,11 @@ def _fetch_url_raw(
return hop_error, "", ""
continue
+ coding = (resp.headers.get("Content-Encoding") or "").strip().lower()
+ if coding not in ("", "identity", *_DECODED_CODINGS):
+ return f"Failed to fetch URL: unsupported Content-Encoding {coding[:40]!r}.", "", ""
+ decoder = _ContentDecoder(coding) if coding in _DECODED_CODINGS else None
+
# get_content_type() maps absent headers to text/plain per RFC 2045; "" marks absence.
if resp.headers.get("Content-Type") is None:
content_type = ""
@@ -16485,6 +16581,7 @@ def _fetch_url_raw(
cancel_event,
body_window = max_bytes if declared_html else None,
charset = resp.headers.get_content_charset() if declared_html else None,
+ decoder = decoder,
)
if body_error is not None:
return body_error, "", ""
@@ -16508,6 +16605,7 @@ def _fetch_url_raw(
timeout,
deadline,
cancel_event,
+ decoder = decoder,
)
if tail_error is not None:
return tail_error, "", ""
diff --git a/studio/backend/routes/browser.py b/studio/backend/routes/browser.py
index 4028faf806e..e916132861e 100644
--- a/studio/backend/routes/browser.py
+++ b/studio/backend/routes/browser.py
@@ -36,6 +36,7 @@ from core.inference.tools import (
_sniff_meta_charset,
)
from loggers import get_logger
+from routes.browser_network import BROWSER_NETWORK_JS
# same embedders as the canvas shell.
from routes.inference import _ARTIFACT_PREVIEW_FRAME_ANCESTORS as _FRAME_ANCESTORS
@@ -170,6 +171,9 @@ _FRAME_HTML = r"""<!doctype html>
// about:srcdoc has an opaque origin, and data: and blob: URLs share it.
const shellOrigin = location.origin === "null" ? null : location.origin;
let pageUrl = cfg.url || location.href;
+ // Without a <base> of its own a document's base is its URL, so the shell's follows the router:
+ // relative links and requests after a pushState resolve as in a browser.
+ const routedBase = cfg.url && !cfg.ownBase ? document.querySelector("base") : null;
const post = (message) => parent.postMessage({ source: "unsloth-browser", ...message }, "*");
// URLs built from location point at this shell; map them to the page's site.
const toPage = (url) => {
@@ -241,6 +245,7 @@ _FRAME_HTML = r"""<!doctype html>
let jar = "";
Object.defineProperty(document, "cookie", { get: () => jar, set: () => {}, configurable: true });
} catch {}
+ /* BROWSER_NETWORK_JS */
const shellPathname = location.pathname;
// Throttle to 200 ms: some sites replaceState per scroll.
let reportedUrl = pageUrl;
@@ -256,13 +261,21 @@ _FRAME_HTML = r"""<!doctype html>
};
for (const [method, original] of [["pushState", originalPush], ["replaceState", originalReplace]]) {
history[method] = function (state, title, url) {
- let next = url != null ? resolve(String(url)) : null;
+ const raw = url != null ? String(url) : null;
+ let next = raw !== null ? resolve(raw) : null;
+ // A router that builds its URL from location.pathname gets "srcdoc": put the page's path back.
+ const fromShell = raw !== null && /^(?:about:)?srcdoc(?=[?#]|$)/.exec(raw);
+ if (fromShell && cfg.url) {
+ const page = new URL(pageUrl);
+ next = new URL(page.pathname + raw.slice(fromShell[0].length), page.origin).href;
+ }
// A router re-asserting the shell's own path is not a navigation.
if (next && new URL(next).pathname === shellPathname) next = null;
try { original.call(this, state, title, next ? shellPath(next) : undefined); }
catch { original.call(this, state, title); }
if (next) {
pageUrl = next;
+ routedBase?.setAttribute("href", pageUrl);
reportUrl();
}
};
@@ -700,7 +713,7 @@ _FRAME_HTML = r"""<!doctype html>
}
// Only the parent may drive the shell, once.
if (!data || data.type !== "unsloth:browser-html" || typeof data.html !== "string") return;
- const cfg = { url: data.url || null, refresh: data.refresh || null, zoom: Number(data.zoom) || 1, muted: data.muted === true };
+ const cfg = { url: data.url || null, ownBase: data.ownBase === true, refresh: data.refresh || null, zoom: Number(data.zoom) || 1, muted: data.muted === true };
const base = data.base ? `<base href="${escapeAttr(data.base)}">` : "";
const script = `<script>(${boot.toString()})(${JSON.stringify(cfg).replace(/</g, "\\u003c")});<\/script>`;
page = document.createElement("iframe");
@@ -724,6 +737,8 @@ _FRAME_HTML = r"""<!doctype html>
</body>
</html>"""
+_FRAME_HTML = _FRAME_HTML.replace("/* BROWSER_NETWORK_JS */", BROWSER_NETWORK_JS.strip(), 1)
+
# Annotate, the body of a function of `post` that the page shell's `install` runs on the panel's
# request: while it is on, pointer input is the panel's, and the page only reports the block under
# the pointer, what a click or drag marks, and where the marks sit as it scrolls.
@@ -1199,12 +1214,14 @@ def _join(base: str, href: str) -> Optional[str]:
return None
-def _prepare_page(page: str, url: str) -> tuple[str, str, Optional[dict]]:
- """Return ``(page, base_url, refresh)`` with <base>, CSP and refresh meta tags removed."""
- base_url = url
+def _prepare_page(page: str, url: str) -> tuple[str, Optional[str], Optional[dict]]:
+ """Return ``(page, base_url, refresh)`` with <base>, CSP and refresh meta tags removed; ``base_url``
+ is None when the page declares no base."""
+ base_url = None
for tag in _BASE_TAG_RE.findall(page):
href = _attr(_ATTR_HREF_RE.search(tag))
- if href:
+ # href="" is a base too, frozen at the document's URL (HTML: the base element).
+ if href is not None:
base_url = _join(url, href) or url
break
page = _BASE_TAG_RE.sub("", page)
@@ -1225,7 +1242,7 @@ def _prepare_page(page: str, url: str) -> tuple[str, str, Optional[dict]]:
parsed = _REFRESH_RE.match(_attr(_CONTENT_ATTR_RE.search(tag)) or "")
if parsed and parsed.group(2) and refresh is None:
delay = float(parsed.group(1))
- target = _join(base_url, parsed.group(2))
+ target = _join(base_url or url, parsed.group(2))
if delay <= _MAX_REFRESH_DELAY_S and target:
refresh = {"delay": delay, "url": target}
return ""
@@ -1518,9 +1535,16 @@ def _build_response(
status_code = 502,
detail = f"(page exceeds the {_MAX_BROWSER_HTML_BYTES} byte limit for the panel)",
)
- page, base_url, refresh = _prepare_page(_decode_html(body, meta.get("charset")), final_url)
+ page, own_base, refresh = _prepare_page(_decode_html(body, meta.get("charset")), final_url)
+ base_url = own_base or final_url
page = _inline_module_scripts(page, base_url, cancel_event)
- payload = {"url": final_url, "base": base_url, "refresh": refresh, "html": page}
+ payload = {
+ "url": final_url,
+ "base": base_url,
+ "ownBase": own_base is not None,
+ "refresh": refresh,
+ "html": page,
+ }
return Response(
content = json.dumps(payload, ensure_ascii = False).encode("utf-8"),
media_type = "application/json",
diff --git a/studio/backend/routes/browser_network.py b/studio/backend/routes/browser_network.py
new file mode 100644
index 00000000000..8147bf9ae67
--- /dev/null
+++ b/studio/backend/routes/browser_network.py
@@ -0,0 +1,51 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""The page shell's fetch and XMLHttpRequest, wrapped to count the page's requests in flight, so the
+browser agent can wait for content a page fetches after load before it reads the page.
+Runs inside ``boot`` in routes/browser.py."""
+
+BROWSER_NETWORK_JS = r"""
+ const nativeFetch = window.fetch.bind(window);
+ const NativeXHR = window.XMLHttpRequest;
+ // Start times of the page's requests in flight, so the agent can wait for content still coming.
+ // The wall clock, which the panel shares, captured before a page script can replace it.
+ const clock = Date.now.bind(Date);
+ const inflight = new Set();
+ const track = () => { const entry = { at: clock() }; inflight.add(entry); return () => inflight.delete(entry); };
+ // Only those started since the agent's action: a long poll the page already had open isn't its.
+ Object.defineProperty(window, Symbol.for("unsloth.browser.pending"), {
+ value: (since) => { let n = 0; for (const r of inflight) if (r.at >= since) n++; return n; },
+ });
+ // fetch() resolves at the headers; a list is drawn only once its body is read, so a read counts too.
+ // The page calls it in the same microtask run as fetch resolves, before any agent command arrives.
+ // A body streamed through response.body.getReader() is not counted.
+ for (const name of ["arrayBuffer", "blob", "bytes", "formData", "json", "text"]) {
+ const read = Response.prototype[name];
+ if (typeof read !== "function") continue;
+ Response.prototype[name] = function (...args) {
+ const reading = read.apply(this, args);
+ return reading.finally(track());
+ };
+ }
+ window.fetch = (input, init) => {
+ const done = track();
+ return nativeFetch(input, init).finally(done);
+ };
+ window.XMLHttpRequest = class extends NativeXHR {
+ // open() ends a request in flight without a loadend, so its count ends here too.
+ open(...args) {
+ this._done?.();
+ super.open(...args);
+ }
+ send(body) {
+ const done = track();
+ // Listening first: a synchronous request fires loadend inside send().
+ this.addEventListener("loadend", done, { once: true });
+ // A send the browser refuses (one already in flight) leaves that one counted.
+ try { super.send(body); } catch (error) { this.removeEventListener("loadend", done); done(); throw error; }
+ if (this._done !== done) this._done?.();
+ this._done = done;
+ }
+ };
+"""
diff --git a/studio/backend/tests/test_browser_routes.py b/studio/backend/tests/test_browser_routes.py
index 794f4057d93..fdf6b121926 100644
--- a/studio/backend/tests/test_browser_routes.py
+++ b/studio/backend/tests/test_browser_routes.py
@@ -87,6 +87,31 @@ def test_the_shell_guards():
assert "requestSubmit = " not in shell
+def test_the_shell_counts_the_pages_requests_and_maps_srcdoc_paths():
+ shell = browser_mod._FRAME_HTML
+ # Inside boot, where cfg and post exist, before any page script can capture fetch.
+ assert "/* BROWSER_NETWORK_JS */" not in shell
+ assert shell.index("const nativeFetch = window.fetch.bind(window);") < shell.index(
+ "const shellPathname"
+ )
+ for guard in (
+ "return nativeFetch(input, init).finally(done);",
+ "/^(?:about:)?srcdoc(?=[?#]|$)/",
+ ):
+ assert guard in shell, guard
+
+
+def test_the_shells_base_follows_the_router_only_without_a_page_base():
+ # A real browser resolves against the document URL when the page has no <base>; checked in
+ # Chromium, as this suite has no browser to run the shell in.
+ shell = browser_mod._FRAME_HTML
+ assert "ownBase: data.ownBase === true" in shell
+ assert 'cfg.url && !cfg.ownBase ? document.querySelector("base") : null' in shell
+ assert shell.index("pageUrl = next;") < shell.index(
+ 'routedBase?.setAttribute("href", pageUrl);'
+ )
+
+
def test_annotate_code_is_sent_only_when_annotating():
shell, code = browser_mod._FRAME_HTML, browser_mod._ANNOTATE_JS
# Not in the shell (so not in every page), installed once and only from the shell's own message.
@@ -204,9 +229,26 @@ def test_html_comes_back_as_json_with_its_final_url(monkeypatch):
assert response.headers["x-unsloth-browser-kind"] == "html"
payload = json.loads(response.body)
assert payload["url"] == payload["base"] == "https://example.com/final"
+ assert payload["ownBase"] is False
assert "<title>x</title>" in payload["html"]
+def test_a_page_base_equal_to_its_url_is_still_its_own(monkeypatch):
+ # <base href="/"> on the site root: the shell must keep it fixed when the router moves.
+ _fetch(
+ monkeypatch,
+ (None, b'<html><head><base href="/"></head></html>', "text/html"),
+ {"url": "https://example.com/", "charset": "utf-8"},
+ )
+ payload = json.loads(_call(url = "example.com").body)
+ assert payload["url"] == payload["base"] == "https://example.com/"
+ assert payload["ownBase"] is True
+ assert browser_mod._prepare_page('<base href="">', "https://example.com/a")[1] == (
+ "https://example.com/a"
+ )
+ assert browser_mod._prepare_page('<base target="_blank">', "https://example.com/a")[1] is None
+
+
def test_raw_text_is_transcoded_from_its_charset(monkeypatch):
body = "a,café\n".encode("latin-1")
_fetch(
diff --git a/studio/backend/tests/test_web_fetch_content_encoding.py b/studio/backend/tests/test_web_fetch_content_encoding.py
new file mode 100644
index 00000000000..59b882263c6
--- /dev/null
+++ b/studio/backend/tests/test_web_fetch_content_encoding.py
@@ -0,0 +1,235 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""The shared fetcher undoes gzip and deflate Content-Encoding. Servers send one unasked (python.org
+does even for ``Accept-Encoding: identity``), and the browser panel, web_fetch and image search all
+read through this fetcher."""
+
+from __future__ import annotations
+
+import gzip
+import sys
+import urllib.error
+import zlib
+from email.message import Message
+from pathlib import Path
+
+import pytest
+
+_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)
+if _BACKEND_DIR not in sys.path:
+ sys.path.insert(0, _BACKEND_DIR)
+
+from core.inference import tools
+
+_PAGE = (
+ b"<!doctype html><html><head><title>About</title></head><body><h1>About Python</h1>"
+ + b"x" * 200_000
+ + b"</body></html>"
+)
+
+
+class _FakeResp:
+ def __init__(
+ self,
+ body: bytes,
+ headers: dict,
+ step: int = 4096,
+ ):
+ self._body = body
+ self._pos = 0
+ self._step = step
+ self.headers = Message()
+ for name, value in headers.items():
+ self.headers[name] = value
+
+ def read1(self, n: int = -1) -> bytes:
+ chunk = self._body[self._pos : self._pos + min(n, self._step)]
+ self._pos += len(chunk)
+ return chunk
+
+ read = read1
+
+
+def _serve(monkeypatch, responses):
+ """`responses`: a list, one per request, of a _FakeResp or an exception to raise. Returns the sent headers."""
+ sent = []
+ queue = list(responses)
+
+ class _Opener:
+ def open(
+ self,
+ req,
+ timeout = None,
+ ):
+ sent.append(dict(req.header_items()))
+ item = queue.pop(0)
+ if isinstance(item, Exception):
+ raise item
+ return item
+
+ monkeypatch.setattr(
+ tools, "_validate_and_resolve_host", lambda host, port: (True, "", ["93.184.216.34"])
+ )
+ monkeypatch.setattr(tools.urllib.request, "build_opener", lambda *a, **k: _Opener())
+ return sent
+
+
+def _raw_deflate(data: bytes) -> bytes:
+ packer = zlib.compressobj(wbits = -15)
+ return packer.compress(data) + packer.flush()
+
+
+ "coding, body",
+ [
+ ("gzip", gzip.compress(_PAGE)),
+ ("x-gzip", gzip.compress(_PAGE)),
+ ("deflate", zlib.compress(_PAGE)),
+ # Some servers send raw deflate for "deflate".
+ ("deflate", _raw_deflate(_PAGE)),
+ # Concatenated members are one body (RFC 1952 2.2).
+ ("gzip", gzip.compress(_PAGE[:1000]) + gzip.compress(_PAGE[1000:])),
+ # Padding after the last member is ignored, as browsers do.
+ ("gzip", gzip.compress(_PAGE) + b"\0\0\0\0"),
+ ("GZIP ", gzip.compress(_PAGE)),
+ ],
+ ids = [
+ "gzip",
+ "x-gzip",
+ "zlib-deflate",
+ "raw-deflate",
+ "two-members",
+ "padding",
+ "case-and-space",
+ ],
+)
+def test_a_coded_page_reaches_the_browser_panel_decoded(monkeypatch, coding, body):
+ _serve(
+ monkeypatch,
+ [_FakeResp(body, {"Content-Type": "text/html", "Content-Encoding": coding}, step = 1000)],
+ )
+ error, raw, content_type = tools._fetch_url_raw(
+ "https://www.python.org/about/", raw_bytes_max = 10_000_000
+ )
+ assert error is None
+ assert raw == _PAGE
+ assert content_type == "text/html"
+
+
+def test_raw_deflate_output_zlib_held_back_at_the_end_is_not_truncation(monkeypatch):
+ # The last wire bytes fill the 64 KiB output buffer; zlib keeps the rest with no input left.
+ body = b"a" * 65_537
+ _serve(
+ monkeypatch,
+ [
+ _FakeResp(
+ _raw_deflate(body), {"Content-Type": "text/plain", "Content-Encoding": "deflate"}
+ )
+ ],
+ )
+ error, raw, _ = tools._fetch_url_raw("https://example.com/a.txt", raw_bytes_max = 10_000_000)
+ assert error is None
+ assert raw == body
+
+
+def test_web_fetch_reads_a_gzip_page_as_text(monkeypatch):
+ _serve(
+ monkeypatch,
+ [
+ _FakeResp(
+ gzip.compress(_PAGE),
+ {"Content-Type": "text/html; charset=utf-8", "Content-Encoding": "gzip"},
+ )
+ ],
+ )
+ text = tools._fetch_page_text("https://www.python.org/about/", timeout = 5)
+ assert "About Python" in text
+ assert "binary content" not in text
+
+
+def test_the_fetcher_asks_for_the_codings_it_can_undo(monkeypatch):
+ sent = _serve(
+ monkeypatch,
+ [
+ _FakeResp(_PAGE, {"Content-Type": "text/html"}),
+ _FakeResp(_PAGE, {"Content-Type": "text/html"}),
+ ],
+ )
+ assert tools._fetch_url_raw("https://example.com/", raw_bytes_max = 10_000_000)[0] is None
+ assert sent[0]["Accept-encoding"] == "gzip, deflate"
+ # A caller that asks for something else still gets its way.
+ tools._fetch_url_raw(
+ "https://example.com/",
+ raw_bytes_max = 10_000_000,
+ extra_headers = {"Accept-Encoding": "identity"},
+ )
+ assert sent[1]["Accept-encoding"] == "identity"
+
+
+def test_an_empty_coded_body_is_an_empty_page(monkeypatch):
+ _serve(monkeypatch, [_FakeResp(b"", {"Content-Type": "text/html", "Content-Encoding": "gzip"})])
+ error, raw, _ = tools._fetch_url_raw("https://example.com/", raw_bytes_max = 1000)
+ assert error is None and raw == b""
+
+
+ "coding, body, message",
+ [
+ ("br", b"\x0b\x02\x80hello\x03", "unsupported Content-Encoding 'br'"),
+ ("gzip", gzip.compress(_PAGE)[:-40], "truncated compressed response"),
+ ("gzip", b"<html>not compressed</html>", "Failed to decode the compressed response"),
+ ],
+ ids = ["brotli", "truncated", "not-gzip"],
+)
+def test_a_body_that_cannot_be_decoded_is_an_error_not_garbage(monkeypatch, coding, body, message):
+ _serve(
+ monkeypatch, [_FakeResp(body, {"Content-Type": "text/html", "Content-Encoding": coding})]
+ )
+ error, raw, _ = tools._fetch_url_raw("https://example.com/", raw_bytes_max = 10_000_000)
+ assert message in error
+ assert raw == ""
+
+
+def test_a_compressed_bomb_stops_at_the_decoded_limit(monkeypatch):
+ bomb = gzip.compress(b"\0" * 64_000_000)
+ _serve(
+ monkeypatch,
+ [
+ _FakeResp(
+ bomb,
+ {"Content-Type": "application/octet-stream", "Content-Encoding": "gzip"},
+ step = 65536,
+ )
+ ],
+ )
+ error, raw, _ = tools._fetch_url_raw("https://example.com/", raw_bytes_max = 1_000_000)
+ assert error == "(content exceeds the 1000000 byte limit)"
+
+
+def test_a_compressed_body_that_never_decodes_stops_at_the_wire_limit(monkeypatch):
+ # A gzip header with a huge comment field: input keeps coming, output never does.
+ padded = b"\x1f\x8b\x08\x10\x00\x00\x00\x00\x00\xff" + b"a" * 3_000_000
+ _serve(
+ monkeypatch,
+ [_FakeResp(padded, {"Content-Type": "text/html", "Content-Encoding": "gzip"}, step = 65536)],
+ )
+ error, raw, _ = tools._fetch_url_raw("https://example.com/", raw_bytes_max = 1_000_000)
+ assert error.startswith("(compressed content exceeds the ")
+ assert raw == ""
+
+
+def test_a_coded_error_page_is_decoded_too(monkeypatch):
+ page = gzip.compress(b"<html><body>Not Found</body></html>")
+ error_resp = _FakeResp(page, {"Content-Type": "text/html", "Content-Encoding": "gzip"})
+ http_error = urllib.error.HTTPError(
+ "https://example.com/x", 404, "Not Found", error_resp.headers, None
+ )
+ http_error.read1 = error_resp.read1
+ http_error.read = error_resp.read1
+ _serve(monkeypatch, [http_error])
+ error, raw, _ = tools._fetch_url_raw(
+ "https://example.com/x", raw_bytes_max = 1000, error_page = True
+ )
+ assert error.startswith("Failed to fetch URL: HTTP 404")
+ assert raw == b"<html><body>Not Found</body></html>"
diff --git a/studio/frontend/src/features/browser/agent/executor.ts b/studio/frontend/src/features/browser/agent/executor.ts
index eca05b5512d..c45c8fe0d34 100644
--- a/studio/frontend/src/features/browser/agent/executor.ts
+++ b/studio/frontend/src/features/browser/agent/executor.ts
@@ -17,6 +17,7 @@ import { askUser, setActiveAction, useBrowserAgentStore } from "./agent-store";
import { type Gate, type Question, type SitePolicy, gateElement, gateNavigation, hostOfUrl, navigationBlocks } from "./gate";
import { ruleFor } from "./permissions";
import { type AgentAnswer, type ElementInfo, pageAgentSource } from "./page-agent";
+import { waitForPage } from "./page-wait";
export type BrowserAction = {
id: string;
@@ -51,6 +52,7 @@ type Result = { ok: boolean; error?: string; url?: string; title?: string; text?
const LOAD_TIMEOUT_MS = 30_000;
const SETTLE_QUIET_MS = 350;
const SETTLE_MAX_MS = 3_000;
+const FILL_QUIET_MAX_MS = 1_000;
const PAGE_READY_MS = 4_000;
const POST_TIMEOUT_MS = 15_000;
const EXPIRED = "Not done: the action ran out of time, likely waiting for the user to answer in the panel. Ask them before trying again.";
@@ -206,9 +208,10 @@ async function postResult(action: BrowserAction, result: Result): Promise<void>
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
+const findTab = (tabId: string) => useBrowserStore.getState().tabs.find((candidate) => candidate.id === tabId);
+
function agentTab(): BrowserTab | null {
- const { tabId } = useBrowserAgentStore.getState();
- return useBrowserStore.getState().tabs.find((tab) => tab.id === tabId) ?? null;
+ return findTab(useBrowserAgentStore.getState().tabId ?? "") ?? null;
}
function tabUrl(tab: BrowserTab): string {
@@ -229,12 +232,12 @@ async function waitForLoad(tabId: string, signal?: AbortSignal): Promise<Browser
await sleep(150);
while (Date.now() - started < LOAD_TIMEOUT_MS) {
if (signal?.aborted) return null;
- const tab = useBrowserStore.getState().tabs.find((candidate) => candidate.id === tabId);
+ const tab = findTab(tabId);
if (!tab) return null;
if (!tab.loading) return tab;
await sleep(100);
}
- return useBrowserStore.getState().tabs.find((candidate) => candidate.id === tabId) ?? null;
+ return findTab(tabId) ?? null;
}
// The page agent's own error messages (page-agent.ts). Errors reach the model outside the
@@ -280,6 +283,7 @@ function checkedAnswer(value: unknown): AgentAnswer | null {
note: str(raw.note, 1000),
navigate: str(raw.navigate, 8192),
quietMs: typeof raw.quietMs === "number" ? raw.quietMs : undefined,
+ requests: typeof raw.requests === "number" ? raw.requests : undefined,
x: num(raw.x),
y: num(raw.y),
width: num(raw.width),
@@ -303,6 +307,7 @@ function checkedAnswer(value: unknown): AgentAnswer | null {
formMethod: String(info.formMethod ?? "").slice(0, 10),
formAction: String(info.formAction ?? "").slice(0, 8192),
formHasPassword: info.formHasPassword === true,
+ formPasswordFilled: info.formPasswordFilled === true,
submits: info.submits === true,
...(typeof info.option === "string" ? { option: info.option.slice(0, 200) } : {}),
...(typeof info.submitter === "string" ? { submitter: info.submitter.slice(0, 200) } : {}),
@@ -352,19 +357,35 @@ async function pageCall(tab: BrowserTab, command: Record<string, unknown>): Prom
}
}
-/** Until the page stops changing after an action, following any navigation it started. */
-async function settle(tabId: string, urlBefore: string, signal?: AbortSignal): Promise<BrowserTab | null> {
+/** Requests started since `since` and a quiet DOM; `left` says the tab is on another page. */
+function waitOnPage(tabId: string, left: (tab: BrowserTab) => boolean, since: number, changingMaxMs: number, signal?: AbortSignal) {
+ return waitForPage({
+ maxMs: SETTLE_MAX_MS, changingMaxMs, quietMs: SETTLE_QUIET_MS, now: Date.now, sleep,
+ status: () => {
+ const tab = findTab(tabId);
+ return tab ? pageCall(tab, { op: "status", since }).catch(() => null) : Promise.resolve(null);
+ },
+ moved: () => {
+ const tab = findTab(tabId);
+ return !tab || tab.loading || left(tab);
+ },
+ aborted: () => Boolean(signal?.aborted),
+ });
+}
+
+/** Until the page settles after an action sent at `since`, following any navigation it started. */
+async function settle(tabId: string, urlBefore: string, since: number, signal?: AbortSignal): Promise<BrowserTab | null> {
await sleep(120);
- const started = Date.now();
- while (Date.now() - started < SETTLE_MAX_MS) {
- const tab = useBrowserStore.getState().tabs.find((candidate) => candidate.id === tabId);
- if (!tab) return null;
- if (tab.loading || tabUrl(tab) !== urlBefore) return waitForLoad(tabId, signal);
- const status = await pageCall(tab, { op: "status" }).catch(() => null);
- if (!status || (status.quietMs ?? 0) >= SETTLE_QUIET_MS) return tab;
- await sleep(120);
- }
- return useBrowserStore.getState().tabs.find((candidate) => candidate.id === tabId) ?? null;
+ const state = await waitOnPage(tabId, (tab) => tabUrl(tab) !== urlBefore, since, SETTLE_MAX_MS, signal);
+ if (state === "moved" && findTab(tabId)) return waitForLoad(tabId, signal);
+ return findTab(tabId) ?? null;
+}
+
+/** Until a loaded page fills itself in (lists fetched after load); true if requests were still open.
+ * A DOM that never rests gets only FILL_QUIET_MAX_MS. A router rewriting the address is still this page. */
+async function untilFilled(tab: BrowserTab, since: number, signal?: AbortSignal): Promise<boolean> {
+ const entry = currentEntry(tab);
+ return (await waitOnPage(tab.id, (now) => currentEntry(now) !== entry, since, FILL_QUIET_MAX_MS, signal)) === "loading";
}
async function pass(gate: Gate, signal?: AbortSignal): Promise<string | null> {
@@ -405,7 +426,7 @@ async function observe(tab: BrowserTab, note?: string, op: Record<string, unknow
}
const answer = await pageCall(tab, op);
if (!answer.ok) return { ok: false, error: answer.error ?? "the page could not be read." };
- const fresh = useBrowserStore.getState().tabs.find((candidate) => candidate.id === tab.id) ?? tab;
+ const fresh = findTab(tab.id) ?? tab;
// A page that moved during the read is one nobody checked: its words and links stay out.
if (fresh.loading || tabUrl(fresh) !== tabUrl(tab)) {
return { ok: false, error: "the page changed while it was being read; take a snapshot again." };
@@ -471,9 +492,10 @@ async function perform(action: BrowserAction, context: ActionContext): Promise<R
const refused = await pass(gate, signal);
if (refused) return { ok: false, error: refused };
admit(url, gate);
+ const since = Date.now();
const tab = await open(url, signal);
if (!tab) return { ok: false, error: "the browser tab closed." };
- return arrive(tab, url, action, context);
+ return arrive(tab, url, action, context, since);
}
if (action.op === "history") return readHistory(action, signal);
@@ -516,6 +538,7 @@ async function perform(action: BrowserAction, context: ActionContext): Promise<R
const refused = await pass({ kind: "ask", ask: "open", host: where, name: "", detail: "", prompt: `Open ${where}?` }, signal);
if (refused) return { ok: false, error: refused };
}
+ const since = Date.now();
// A native move starts when the view reports it; loading from now, so the wait below sees it.
if ((nativeMove || (native && action.op === "reload")) && hasNativeView(tab.id)) store.updateTab(tab.id, { loading: true });
if (nativeMove) nativeAction(tab.id, action.op as "back" | "forward");
@@ -524,7 +547,7 @@ async function perform(action: BrowserAction, context: ActionContext): Promise<R
else if (action.op === "forward") store.goForward(tab.id);
else store.reload(tab.id);
const loaded = await waitForLoad(tab.id, signal);
- return loaded ? arrive(loaded, before, action, context) : { ok: false, error: "the browser tab closed." };
+ return loaded ? arrive(loaded, before, action, context, since) : { ok: false, error: "the browser tab closed." };
}
const ready = tab.loading ? await waitForLoad(tab.id, signal) : tab;
@@ -539,13 +562,14 @@ async function perform(action: BrowserAction, context: ActionContext): Promise<R
case "find":
return observe(ready, undefined, { op: "find", query: action.query ?? "" });
case "scroll": {
+ const since = Date.now();
const moved = await pageCall(ready, { op: "scroll", direction: action.direction, ...(action.ref ? { ref: action.ref } : {}) });
if (!moved.ok) return { ok: false, error: moved.error };
- const after = await settle(ready.id, here, signal);
+ const after = await settle(ready.id, here, since, signal);
if (!after) return { ok: false, error: "the browser tab closed." };
// A page can navigate on scroll: the new address is checked like any other arrival.
const now = tabUrl(after);
- if (now && now !== here) return arrive(after, here, action, context, moved.note);
+ if (now && now !== here) return arrive(after, here, action, context, since, moved.note);
return observe(after, moved.note);
}
case "screenshot":
@@ -632,6 +656,8 @@ async function perform(action: BrowserAction, context: ActionContext): Promise<R
if (formGate) admit(formAction, formGate);
if (linkGate) admit(info.href, linkGate);
+ // Before the action goes out, after any question: a request its handler starts at once is the action's too.
+ const since = Date.now();
let note: string | undefined;
if (action.op === "click") {
const clicked = await pageCall(ready, {
@@ -648,9 +674,10 @@ async function perform(action: BrowserAction, context: ActionContext): Promise<R
const refused = await pass(gate, signal);
if (refused) return { ok: false, error: refused };
admit(clicked.navigate, gate);
+ const sent = Date.now();
useBrowserStore.getState().navigate(ready.id, { url: clicked.navigate, from: here }, { replace: false });
const loaded = await waitForLoad(ready.id, signal);
- return loaded ? arrive(loaded, clicked.navigate, action, context) : { ok: false, error: "the browser tab closed." };
+ return loaded ? arrive(loaded, clicked.navigate, action, context, sent) : { ok: false, error: "the browser tab closed." };
}
note = clicked.note;
} else if (action.op === "type") {
@@ -668,18 +695,26 @@ async function perform(action: BrowserAction, context: ActionContext): Promise<R
} else {
return { ok: false, error: `unknown browser action ${action.op}.` };
}
- const after = await settle(ready.id, here, signal);
+ const after = await settle(ready.id, here, since, signal);
if (!after) return { ok: false, error: "the browser tab closed." };
const now = tabUrl(after);
- if (now && now !== here) return arrive(after, here, action, context, note);
+ if (now && now !== here) return arrive(after, here, action, context, since, note);
return observe(after, note);
}
-async function afterward(tab: BrowserTab, before: string, action: BrowserAction, context: ActionContext, note?: string): Promise<Result> {
- const after = await settle(tab.id, before, context.signal);
+/** `since`: when the action went out, so the wait covers the requests it started. */
+async function afterward(
+ tab: BrowserTab,
+ before: string,
+ action: BrowserAction,
+ context: ActionContext,
+ since: number,
+ note?: string,
+): Promise<Result> {
+ const after = await settle(tab.id, before, since, context.signal);
if (!after) return { ok: false, error: "the browser tab closed." };
const now = tabUrl(after);
- if (now && now !== before) return arrive(after, before, action, context, note);
+ if (now && now !== before) return arrive(after, before, action, context, since, note);
return observe(after, note);
}
@@ -727,9 +762,10 @@ async function onScreen(
async function moveTo(tab: BrowserTab, action: BrowserAction, context: ActionContext, before: string): Promise<Result> {
const at = await onScreen(tab, action.ref, action.x, action.y);
if (typeof at === "string") return { ok: false, error: at };
+ const since = Date.now();
const moved = await pageCall(tab, { op: "pointer", phase: "move", ...at });
if (!moved.ok) return { ok: false, error: moved.error };
- return afterward(tab, before, action, context, moved.note);
+ return afterward(tab, before, action, context, since, moved.note);
}
const DRAG_STEP_PX = 20;
@@ -774,6 +810,7 @@ async function dragBetween(tab: BrowserTab, action: BrowserAction, context: Acti
return Boolean(now && !now.loading && tabUrl(now) === before);
};
const samePage = async () => sameTab() && (await pageCall(tab, { op: "status" }).catch(() => null))?.doc === doc;
+ const since = Date.now();
const down = await pageCall(tab, { op: "pointer", phase: "down", ...from, expect: hits[0], sig: sigs[0] });
if (!down.ok) return { ok: false, error: down.error };
// Paced like a hand: pages often take one move per frame, or wait for a distance before a drag starts.
@@ -819,7 +856,7 @@ async function dragBetween(tab: BrowserTab, action: BrowserAction, context: Acti
if (signal?.aborted) return cancel(STOPPED);
const up = await pageCall(tab, { op: "pointer", phase: "up", ...end, expect: target.hit ?? "", sig: target.sig ?? "" });
if (!up.ok) return cancel(up.error ?? CHANGED_UNDER_POINT);
- return afterward(tab, before, action, context, up.note);
+ return afterward(tab, before, action, context, since, up.note);
}
/** An unlabelled point (a canvas) may be a Buy button nothing can read: ask once per site per chat. */
@@ -858,6 +895,7 @@ async function runScript(tab: BrowserTab, action: BrowserAction, context: Action
if (!current || current.loading || tabUrl(current) !== before) {
return { ok: false, error: "the page changed while the user was asked, so the script did not run; take a snapshot." };
}
+ const since = Date.now();
const id = `s${Date.now().toString(36)}${Math.random().toString(36).slice(2, 10)}`;
let started: AgentAnswer | null = null;
if (useNativeBrowser.getState().enabled) {
@@ -892,10 +930,10 @@ async function runScript(tab: BrowserTab, action: BrowserAction, context: Action
}
await sleep(100);
}
- const after = await settle(tab.id, before, signal);
+ const after = await settle(tab.id, before, since, signal);
if (!after) return { ok: false, error: "the browser tab closed." };
const now = tabUrl(after);
- if (now && now !== before) return arrive(after, before, action, context, outcome.slice(0, 900));
+ if (now && now !== before) return arrive(after, before, action, context, since, outcome.slice(0, 900));
return { ok: true, url: now, title: after.customTitle ?? after.title, text: outcome };
}
@@ -906,6 +944,7 @@ async function arrive(
requested: string,
action: BrowserAction,
context: ActionContext,
+ since: number,
note?: string,
): Promise<Result> {
const url = tabUrl(tab);
@@ -922,7 +961,7 @@ async function arrive(
if (host) reachedHosts.add(host);
record(action.op, url);
}
- return observeLoaded(tab, note);
+ return observeLoaded(tab, since, note, context.signal);
}
/** The page the tab shows now, before the model reads or acts on it: the page may have moved on
@@ -941,7 +980,7 @@ async function stillAllowed(tab: BrowserTab, action: BrowserAction, context: Act
return null;
}
-async function observeLoaded(tab: BrowserTab, note?: string): Promise<Result> {
+async function observeLoaded(tab: BrowserTab, since: number, note?: string, signal?: AbortSignal): Promise<Result> {
const entry = currentEntry(tab);
if (entry.kind === "web" && tab.documentType && !/html/i.test(tab.documentType)) {
return {
@@ -951,7 +990,12 @@ async function observeLoaded(tab: BrowserTab, note?: string): Promise<Result> {
note: `${note ? `${note} ` : ""}This address is a file (${tab.documentType}), shown to the user; the browser tools can't read it. web_search with this url can.`,
};
}
- return observe(tab, note);
+ if (entry.kind === "web" && !tab.pageError && !tab.nativeError && (await untilFilled(tab, since, signal))) {
+ note = [note, "The page still had requests open when read; snapshot again if something is missing."].filter(Boolean).join(" ");
+ }
+ // Read at the address its router (same site, shell-checked) moved to while it filled in.
+ const fresh = findTab(tab.id);
+ return observe(fresh && !fresh.loading && currentEntry(fresh) === entry ? fresh : tab, note);
}
async function screenshot(tab: BrowserTab): Promise<Result> {
@@ -971,7 +1015,7 @@ async function screenshot(tab: BrowserTab): Promise<Result> {
}
if (!blob) return { ok: false, error: "the screenshot failed." };
// A page that moved during the capture is one nobody checked or masked: no picture of it.
- const now = useBrowserStore.getState().tabs.find((candidate) => candidate.id === tab.id);
+ const now = findTab(tab.id);
if (!now || now.loading || tabUrl(now) !== tabUrl(tab)) {
return { ok: false, error: "the page changed while the screenshot was taken; take a snapshot first." };
}
diff --git a/studio/frontend/src/features/browser/agent/gate.ts b/studio/frontend/src/features/browser/agent/gate.ts
index e825cae2e93..5e0b2c9563d 100644
--- a/studio/frontend/src/features/browser/agent/gate.ts
+++ b/studio/frontend/src/features/browser/agent/gate.ts
@@ -206,10 +206,11 @@ export function gateElement(
if (blocked) return { kind: "block", reason: blocked };
}
if (ruleFor(pageUrl, permissions).rule.actions === "allow") return ALLOW;
- if (submits && info.formHasPassword) {
+ // Only a filled password makes it a sign-in; with the field empty, it still asks as any form send.
+ if (submits && info.formHasPassword && info.formPasswordFilled) {
return { kind: "ask", ask: "signIn", host, name: "", prompt: `Sign in on ${host}?`, detail: describe(info) };
}
- if (submits && info.formMethod === "post") {
+ if (submits && (info.formHasPassword || info.formMethod === "post")) {
return { kind: "ask", ask: "submit", host, name: "", prompt: `Submit this form on ${host}?`, detail: describe(info) };
}
// A link mostly just opens a page ("Sign in" goes to the sign-in form), so fewer words count there.
diff --git a/studio/frontend/src/features/browser/agent/page-agent.ts b/studio/frontend/src/features/browser/agent/page-agent.ts
index 8f1036ced36..ad781bf694c 100644
--- a/studio/frontend/src/features/browser/agent/page-agent.ts
+++ b/studio/frontend/src/features/browser/agent/page-agent.ts
@@ -32,6 +32,8 @@ export type ElementInfo = {
formMethod: string;
formAction: string;
formHasPassword: boolean;
+ /** Whether one of those password fields holds a value (the user's to type); never the value. */
+ formPasswordFilled?: boolean;
/** For a dropdown described with a value: the label of the option that value selects. */
option?: string;
/** For Enter in a field: the label of the button that will submit its form. */
@@ -48,6 +50,8 @@ export type AgentAnswer = {
navigate?: string;
info?: ElementInfo;
quietMs?: number;
+ /** Requests the page started recently and is still waiting on. */
+ requests?: number;
title?: string;
x?: number;
y?: number;
@@ -566,6 +570,10 @@ export function installPageAgent(): PageAgent {
const action = submitter?.hasAttribute("formaction")
? new URL(submitter.getAttribute("formaction") ?? "", document.baseURI).href
: form?.action || pageUrl().href;
+ // form.elements, not descendants: a field joins a form from anywhere with form="id".
+ const passwords = Array.from(form?.elements ?? []).filter(
+ (node): node is HTMLInputElement => (node as HTMLInputElement).type === "password",
+ );
const info = {
role: roleOf(el) ?? (anchor ? "link" : "element"),
name: nameOf(el) || nameOf(anchor ?? el),
@@ -575,8 +583,8 @@ export function installPageAgent(): PageAgent {
autocomplete: (el.getAttribute("autocomplete") ?? "").toLowerCase(),
formMethod: form ? method.toLowerCase() : "get",
formAction: form ? action : "",
- // form.elements, not descendants: a field joins a form from anywhere with form="id".
- formHasPassword: Array.from(form?.elements ?? []).some((node) => (node as HTMLInputElement).type === "password"),
+ formHasPassword: passwords.length > 0,
+ formPasswordFilled: passwords.some((field) => field.value !== ""),
submits,
// What choose() will pick, so the panel gates that option's label, not the model's words.
...(el instanceof HTMLSelectElement && typeof value === "string"
@@ -1105,13 +1113,18 @@ export function installPageAgent(): PageAgent {
try {
const ref = typeof command.ref === "string" ? command.ref : "";
switch (command.op) {
- case "status":
+ case "status": {
+ // Fetch/XHR the proxied shell counts as open since `since` (routes/browser_network.py).
+ const pending = host[Symbol.for("unsloth.browser.pending")];
+ const since = typeof command.since === "number" ? command.since : Number.POSITIVE_INFINITY;
return {
ok: true,
quietMs: Math.round(performance.now() - lastMutation),
+ requests: typeof pending === "function" ? Number(pending(since)) || 0 : 0,
title: document.title,
...view(),
};
+ }
case "snapshot":
return snapshot(command.fullPage === true, ref || undefined);
case "find":
diff --git a/studio/frontend/src/features/browser/agent/page-wait.ts b/studio/frontend/src/features/browser/agent/page-wait.ts
new file mode 100644
index 00000000000..8eeecb33f92
--- /dev/null
+++ b/studio/frontend/src/features/browser/agent/page-wait.ts
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+/** What the page reports (agent/page-agent.ts `status`); null when it didn't answer. */
+export type PageStatus = { requests?: number; quietMs?: number } | null;
+
+export type PageWait = {
+ /** Give up after about this long (one more status call can run past it). */
+ maxMs: number;
+ /** A DOM that keeps changing with no request open is waited on only this long. */
+ changingMaxMs: number;
+ quietMs: number;
+ now: () => number;
+ sleep: (ms: number) => Promise<unknown>;
+ /** Open requests started since the action, and how long the DOM has been quiet. */
+ status: () => Promise<PageStatus>;
+ moved: () => boolean;
+ aborted?: () => boolean;
+};
+
+const POLL_MS = 120;
+
+/** "settled", "loading" (requests still open at `maxMs`), or "moved". */
+export async function waitForPage(wait: PageWait): Promise<"settled" | "loading" | "moved"> {
+ const started = wait.now();
+ // After a request closes, quiet and the changing-DOM allowance count from then: its DOM work comes after it.
+ let free: number | null = null;
+ let opened = false;
+ while (!wait.aborted?.()) {
+ if (wait.moved()) return "moved";
+ const status = await wait.status();
+ if (!status) return "settled";
+ const loading = (status.requests ?? 0) > 0;
+ const now = wait.now();
+ const waited = now - started;
+ if (loading) opened = true;
+ free = loading ? null : (free ?? (opened ? now : started));
+ const quiet = opened && free !== null ? Math.min(status.quietMs ?? 0, now - free) : (status.quietMs ?? 0);
+ if (free !== null && (quiet >= wait.quietMs || now - free >= wait.changingMaxMs)) return "settled";
+ if (waited >= wait.maxMs) return loading ? "loading" : "settled";
+ await wait.sleep(POLL_MS);
+ }
+ return "settled";
+}
diff --git a/studio/frontend/src/features/browser/api.ts b/studio/frontend/src/features/browser/api.ts
index b90066c6ea1..d5e17718ae6 100644
--- a/studio/frontend/src/features/browser/api.ts
+++ b/studio/frontend/src/features/browser/api.ts
@@ -9,6 +9,8 @@ export type BrowserPage =
kind: "html";
url: string;
base: string;
+ /** The page declared its <base>, so it stays put when the page's router moves. */
+ ownBase?: boolean;
html: string;
refresh: { delay: number; url: string } | null;
}
diff --git a/studio/frontend/src/features/browser/page-frame.tsx b/studio/frontend/src/features/browser/page-frame.tsx
index 859128ae912..2030df3bce9 100644
--- a/studio/frontend/src/features/browser/page-frame.tsx
+++ b/studio/frontend/src/features/browser/page-frame.tsx
@@ -106,6 +106,7 @@ export function PageFrame({
html,
url,
base,
+ ownBase = false,
refresh,
title,
tabId,
@@ -119,6 +120,7 @@ export function PageFrame({
html: string;
url: string | null;
base: string | null;
+ ownBase?: boolean;
refresh?: { delay: number; url: string } | null;
title: string;
onMessage: (message: FrameMessage) => void;
@@ -219,6 +221,7 @@ export function PageFrame({
html,
url,
base,
+ ownBase,
refresh: refresh ?? null,
zoom: zoomRef.current,
muted: mutedRef.current,
diff --git a/studio/frontend/src/features/browser/store.ts b/studio/frontend/src/features/browser/store.ts
index 14d723117fa..1f462a4ec6c 100644
--- a/studio/frontend/src/features/browser/store.ts
+++ b/studio/frontend/src/features/browser/store.ts
@@ -352,6 +352,19 @@ function zoomFor(tab: BrowserTab, entry: BrowserEntry, traversal = false): numbe
return tab.zoom;
}
+/** The same-origin address the page's router moved to, for Reload; null if unmoved or a POST. */
+function routerEntry(tab: BrowserTab): BrowserEntry | null {
+ const entry = currentEntry(tab);
+ if (nativeWebHistory || entry.kind !== "web" || entry.method === "POST") return null;
+ if (!tab.displayUrl || tab.displayUrl === entry.url) return null;
+ try {
+ if (new URL(tab.displayUrl).origin !== new URL(entry.url).origin) return null;
+ } catch {
+ return null;
+ }
+ return webEntry(tab.displayUrl, "GET", undefined, undefined, entry.temporary);
+}
+
function pushEntry(tab: BrowserTab, entry: BrowserEntry, replace = false): BrowserTab {
const zoom = zoomFor(tab, entry);
const history = tab.history.slice(0, replace ? tab.index : tab.index + 1);
@@ -589,7 +602,13 @@ export const useBrowserStore = create<BrowserState>((set, get) => {
const tab = get().tabs.find((candidate) => candidate.id === tabId);
if (tab) pageCache.delete(currentEntry(tab));
set((state) => ({
- tabs: patchTab(state.tabs, tabId, (tab) => ({ ...tab, reloadKey: tab.reloadKey + 1 })),
+ tabs: patchTab(state.tabs, tabId, (tab) => {
+ const moved = routerEntry(tab);
+ if (!moved) return { ...tab, reloadKey: tab.reloadKey + 1 };
+ // Swapped in place, as a browser's reload: Forward keeps its pages.
+ const next = pushEntry(tab, moved, true);
+ return { ...next, history: [...next.history, ...tab.history.slice(tab.index + 1)], reloadKey: tab.reloadKey + 1 };
+ }),
}));
},
activateTab: (tabId) => set({ activeTabId: tabId, annotateTabId: null }),
diff --git a/studio/frontend/src/features/browser/tab-view.tsx b/studio/frontend/src/features/browser/tab-view.tsx
index 4ee2daae3fc..b0b76fcee4b 100644
--- a/studio/frontend/src/features/browser/tab-view.tsx
+++ b/studio/frontend/src/features/browser/tab-view.tsx
@@ -352,6 +352,7 @@ function WebPage({
html={page.html}
url={page.url}
base={page.base}
+ ownBase={page.ownBase === true}
refresh={page.refresh}
title={tab.title || hostOf(page.url)}
tabId={tab.id}
diff --git a/studio/frontend/tests/browser-agent-executor.test.ts b/studio/frontend/tests/browser-agent-executor.test.ts
new file mode 100644
index 00000000000..0058129e9b5
--- /dev/null
+++ b/studio/frontend/tests/browser-agent-executor.test.ts
@@ -0,0 +1,56 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import { register } from "node:module";
+import { test } from "node:test";
+
+register("./helpers/browser-executor-resolver.mjs", import.meta.url);
+const { runBrowserAction } = await import("../src/features/browser/agent/executor.ts");
+const { useBrowserStore } = await import("../src/features/browser/store.ts");
+
+const globals = globalThis as Record<string, unknown>;
+
+type Posted = { result: { ok: boolean; url?: string; text?: string; error?: string } };
+
+/** Runs one navigate with the page answering `status` through `onStatus`; returns the posted result. */
+async function navigate(id: string, url: string, onStatus: (tabId: string, call: number) => number) {
+ const posted: Posted[] = [];
+ globals.__authFetch = async (_url: string, init: { body: string }) => {
+ posted.push(JSON.parse(init.body));
+ return new Response(null, { status: 200 });
+ };
+ let calls = 0;
+ globals.__frameAgent = async (tabId: string, op: Record<string, unknown>) => {
+ if (op.op === "status") return { installed: true, result: { ok: true, requests: onStatus(tabId, ++calls), quietMs: 1000 } };
+ return { installed: true, result: { ok: true, text: "Qwen models", links: [] } };
+ };
+ await runBrowserAction(
+ { id, sessionId: "s", op: "navigate", url, direction: "down", policy: { allowedDomains: [], blockedDomains: [] } },
+ { runKey: "r", userText: `open ${url}` },
+ );
+ assert.equal(posted.length, 1);
+ return { result: posted[0].result, calls };
+}
+
+test("a router that rewrites the address while a new page fills in is read at its new address", async () => {
+ // The list's request is still open; the router moves the address on, then the list arrives.
+ const { result, calls } = await navigate("a1", "https://example.com/models", (tabId, call) => {
+ if (call === 2) useBrowserStore.getState().updateTab(tabId, { displayUrl: "https://example.com/models?search=qwen" });
+ return call < 3 ? 1 : 0;
+ });
+ assert.equal(result.error, undefined);
+ assert.equal(result.ok, true);
+ assert.equal(result.url, "https://example.com/models?search=qwen");
+ assert.equal(result.text, "Qwen models");
+ assert.ok(calls >= 3, "the wait stopped at the address change instead of waiting for the list");
+});
+
+test("a page that navigates away while it fills in is not read as the new page", async () => {
+ const { result } = await navigate("a2", "https://example.com/docs", (tabId, call) => {
+ if (call === 2) useBrowserStore.getState().navigate(tabId, { url: "https://other.example.org/" }, { replace: false });
+ return 1;
+ });
+ assert.equal(result.ok, false);
+ assert.equal(result.text, undefined);
+});
diff --git a/studio/frontend/tests/browser-agent-gate.test.ts b/studio/frontend/tests/browser-agent-gate.test.ts
index 5c42adfcc13..fe50a614898 100644
--- a/studio/frontend/tests/browser-agent-gate.test.ts
+++ b/studio/frontend/tests/browser-agent-gate.test.ts
@@ -122,8 +122,13 @@ test("buying, posting, signing in and POST forms ask; ordinary clicks and search
assert.equal(gateElement("click", element({ name: "Post reply" }), page, open, NONE).kind, "ask");
assert.equal(gateElement("click", element({ name: "Show more results" }), page, open, NONE).kind, "allow");
const login = element({ name: "Continue", submits: true, formMethod: "post", formAction: "https://shop.example/login", formHasPassword: true });
- const signIn = gateElement("click", login, page, open, NONE);
+ // Only a password the user filled in makes it a sign-in; an empty one still asks, as sending a form.
+ const signIn = gateElement("click", { ...login, formPasswordFilled: true }, page, open, NONE);
assert.ok(signIn.kind === "ask" && signIn.ask === "signIn");
+ const emptyPassword = { ...login, formMethod: "get" };
+ const send = gateElement("click", emptyPassword, page, open, NONE);
+ assert.ok(send.kind === "ask" && send.ask === "submit" && send.prompt === "Submit this form on shop.example?");
+ assert.equal(gateElement("click", { ...emptyPassword, formHasPassword: false }, page, open, NONE).kind, "allow");
const comment = element({ name: "Save", submits: true, formMethod: "post", formAction: "https://shop.example/c" });
assert.ok(gateElement("click", comment, page, open, NONE).kind === "ask");
const search = element({ role: "searchbox", tag: "input", name: "Search", formAction: "https://shop.example/s" });
diff --git a/studio/frontend/tests/browser-agent-page-wait.test.ts b/studio/frontend/tests/browser-agent-page-wait.test.ts
new file mode 100644
index 00000000000..c0c9784051d
--- /dev/null
+++ b/studio/frontend/tests/browser-agent-page-wait.test.ts
@@ -0,0 +1,88 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import { test } from "node:test";
+import { type PageWait, waitForPage } from "../src/features/browser/agent/page-wait.ts";
+
+const QUIET = 350;
+
+/** A page on a fake clock: requests open from `start` to `end`, DOM changes at `mutations`. The status
+ * call answers as page-agent.ts does, counting open requests started at or after `since`. */
+function page(requests: Array<{ start: number; end: number }>, mutations: number[] = []) {
+ let now = 0;
+ const wait = (since: number, maxMs: number, changingMaxMs: number): PageWait => ({
+ maxMs, changingMaxMs, quietMs: QUIET,
+ now: () => now,
+ sleep: async (ms) => {
+ now += ms;
+ },
+ status: async () => {
+ // The command takes a moment to reach the page.
+ now += 5;
+ const pending = requests.filter((r) => r.start >= since && r.start <= now && now < r.end).length;
+ const last = Math.max(-Infinity, ...mutations.filter((t) => t <= now));
+ return { requests: pending, quietMs: now - last };
+ },
+ moved: () => false,
+ });
+ return { wait, at: (t: number) => (now = t), now: () => now };
+}
+
+test("a request the action set off at once is waited for, though it started as the action went out", async () => {
+ // The click handler fetches synchronously: the request starts before the click's answer comes back.
+ const p = page([{ start: 1003, end: 2500 }], [0]);
+ p.at(1120);
+ assert.equal(await waitForPage(p.wait(1000, 3000, 3000)), "settled");
+ assert.ok(p.now() >= 2500, `returned at ${p.now()} with the request still open`);
+});
+
+test("a request open since the page loaded still counts at the deadline, and the wait says so", async () => {
+ const p = page([{ start: 200, end: 60_000 }], [0]);
+ p.at(400);
+ assert.equal(await waitForPage(p.wait(0, 3000, 1000)), "loading");
+ const waited = p.now() - 400;
+ assert.ok(waited >= 3000 && waited < 3300, `waited ${waited} ms`);
+});
+
+test("a long poll the page had open before the action doesn't hold it", async () => {
+ const p = page([{ start: -5000, end: 60_000 }], [0]);
+ p.at(1000);
+ assert.equal(await waitForPage(p.wait(1000, 3000, 3000)), "settled");
+ assert.ok(p.now() < 1100);
+});
+
+test("a DOM that never rests is waited on only for changingMaxMs once no request is open", async () => {
+ const ticks = Array.from({ length: 200 }, (_, i) => i * 100);
+ const p = page([], ticks);
+ assert.equal(await waitForPage(p.wait(0, 3000, 1000)), "settled");
+ assert.ok(p.now() >= 1000 && p.now() < 1300, `waited ${p.now()} ms`);
+});
+
+test("a quiet page with nothing open is read at once", async () => {
+ const p = page([], [0]);
+ p.at(5000);
+ assert.equal(await waitForPage(p.wait(5000, 3000, 3000)), "settled");
+ assert.ok(p.now() < 5100);
+});
+
+test("a page that moves away hands back to the caller", async () => {
+ const p = page([{ start: 0, end: 60_000 }]);
+ const wait = { ...p.wait(0, 3000, 3000), moved: () => p.now() > 300 };
+ assert.equal(await waitForPage(wait), "moved");
+});
+
+test("the DOM a slow response sets off still gets its quiet time after the request closes", async () => {
+ // The list's request is open past changingMaxMs; drawing it takes 100 ms after it lands.
+ const drawing = Array.from({ length: 6 }, (_, i) => 1500 + i * 20);
+ const p = page([{ start: 0, end: 1500 }], [0, ...drawing]);
+ assert.equal(await waitForPage(p.wait(0, 3000, 1000)), "settled");
+ assert.ok(p.now() >= 1600 + QUIET, `read at ${p.now()}, while the list was still being drawn`);
+});
+
+test("a DOM quiet while a request was open is not read as settled the moment it closes", async () => {
+ // Nothing changes while the list loads; it is drawn 200 ms after its response lands.
+ const p = page([{ start: 0, end: 800 }], [0, 1000]);
+ assert.equal(await waitForPage(p.wait(0, 3000, 1000)), "settled");
+ assert.ok(p.now() >= 1000 + QUIET, `read at ${p.now()}, before the list was drawn`);
+});
diff --git a/studio/frontend/tests/browser-network.test.ts b/studio/frontend/tests/browser-network.test.ts
new file mode 100644
index 00000000000..4254ce5725e
--- /dev/null
+++ b/studio/frontend/tests/browser-network.test.ts
@@ -0,0 +1,123 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import { readFileSync } from "node:fs";
+import { test } from "node:test";
+import { runInNewContext } from "node:vm";
+
+// The page shell's fetch and XHR (routes/browser_network.py), run as shipped.
+const source = readFileSync(new URL("../../backend/routes/browser_network.py", import.meta.url), "utf8")
+ .split('r"""')[1]
+ .split('"""')[0];
+
+/** A stand-in for the browser's own XHR: a request ends when the test fires its loadend. */
+class NativeXHR extends EventTarget {
+ open() {}
+ send() {}
+}
+
+/** As a browser's: send() on a request already in flight throws InvalidStateError. */
+class StrictXHR extends NativeXHR {
+ inFlight = false;
+ constructor() {
+ super();
+ this.addEventListener("loadend", () => (this.inFlight = false));
+ }
+ open() {
+ this.inFlight = false;
+ }
+ send() {
+ if (this.inFlight) throw new Error("InvalidStateError");
+ this.inFlight = true;
+ }
+}
+
+function setup(XHR: typeof NativeXHR = NativeXHR) {
+ let now = 0;
+ // The script patches its Response's body readers: a class of its own keeps that out of other tests.
+ class PageResponse extends Response {}
+ const answers: Array<(response: Response) => void> = [];
+ const window = Object.assign(new EventTarget(), {
+ fetch: () => new Promise<Response>((resolve) => answers.push(resolve)),
+ XMLHttpRequest: XHR,
+ });
+ runInNewContext(source, { window, Date: { now: () => now }, Response: PageResponse, Symbol, Object, Set });
+ const pending = (since: number) => (window as unknown as Record<symbol, (since: number) => number>)[Symbol.for("unsloth.browser.pending")](since);
+ const page = window as unknown as { fetch: (url: string) => Promise<Response>; XMLHttpRequest: new () => NativeXHR };
+ return {
+ window: page, pending, advance: (ms: number) => (now += ms),
+ answer: (i: number, body = "{}") => answers[i](new PageResponse(body)),
+ Response: PageResponse,
+ };
+}
+
+test("only requests started since the action count, however long they stay open", async () => {
+ const t = setup();
+ const poll = t.window.fetch("/stream");
+ // A long poll the page already had open isn't the action's.
+ t.advance(1000);
+ assert.equal(t.pending(1000), 0);
+ // Cross-origin requests count too: a list fetched from another API is still content on its way.
+ const api = t.window.fetch("https://api.github.com/x");
+ t.advance(60_000);
+ assert.equal(t.pending(1000), 1);
+ assert.equal(t.pending(0), 2);
+ t.answer(0);
+ t.answer(1);
+ await Promise.all([poll, api]);
+ assert.equal(t.pending(0), 0);
+});
+
+test("reading a response's body still counts, since the page draws only once it has it", async () => {
+ const t = setup();
+ const request = t.window.fetch("https://api.github.com/repos/x/git/trees/y");
+ t.answer(0, "tree");
+ const response = await request;
+ assert.equal(t.pending(0), 0);
+ const body = response.text();
+ assert.equal(t.pending(0), 1);
+ assert.equal(await body, "tree");
+ assert.equal(t.pending(0), 0);
+});
+
+test("an XHR counts from send until loadend", () => {
+ const t = setup();
+ const xhr = new t.window.XMLHttpRequest();
+ xhr.open();
+ xhr.send();
+ assert.equal(t.pending(0), 1);
+ xhr.dispatchEvent(new Event("loadend"));
+ assert.equal(t.pending(0), 0);
+});
+
+test("reopening an XHR in flight ends its count, since that request never fires loadend", () => {
+ const t = setup();
+ const xhr = new t.window.XMLHttpRequest();
+ xhr.open();
+ xhr.send();
+ xhr.open();
+ assert.equal(t.pending(0), 0);
+ xhr.send();
+ assert.equal(t.pending(0), 1);
+ xhr.dispatchEvent(new Event("loadend"));
+ assert.equal(t.pending(0), 0);
+});
+
+test("a body read that fails leaves no count behind", async () => {
+ const t = setup();
+ const text = t.Response.prototype.text;
+ await assert.rejects(text.call({}));
+ assert.equal(t.pending(0), 0);
+});
+
+test("a second send the browser refuses keeps counting the request in flight", () => {
+ const t = setup(StrictXHR);
+ const xhr = new t.window.XMLHttpRequest();
+ xhr.open();
+ xhr.send();
+ assert.throws(() => xhr.send());
+ assert.equal(t.pending(0), 1);
+ xhr.dispatchEvent(new Event("loadend"));
+ assert.equal(t.pending(0), 0);
+});
diff --git a/studio/frontend/tests/browser-store-reopen.test.ts b/studio/frontend/tests/browser-store-reopen.test.ts
index 01b408b1bdd..3985d63c4ec 100644
--- a/studio/frontend/tests/browser-store-reopen.test.ts
+++ b/studio/frontend/tests/browser-store-reopen.test.ts
@@ -98,6 +98,45 @@ test("a reload refetches its own page, not the pages behind it", () => {
assert.ok(cachedPage(first));
});
+test("a reload keeps where the page's router moved it on the same site, and nowhere else", () => {
+ const store = useBrowserStore.getState();
+ store.openUrl("https://huggingface.co/models", { newTab: true });
+ const id = useBrowserStore.getState().activeTabId ?? "";
+ const tab = () => useBrowserStore.getState().tabs.find((candidate) => candidate.id === id);
+ const url = () => {
+ const entry = currentEntry(tab()!);
+ return entry.kind === "web" ? entry.url : "";
+ };
+ const length = tab()!.history.length;
+ store.updateTab(id, { displayUrl: "https://huggingface.co/models?search=qwen" });
+ store.reload(id);
+ assert.equal(url(), "https://huggingface.co/models?search=qwen");
+ // Replaced, not pushed: Back still leaves the site.
+ assert.equal(tab()!.history.length, length);
+ // A page can't report another site and have Reload load it.
+ store.updateTab(id, { displayUrl: "https://evil.example/" });
+ store.reload(id);
+ assert.equal(url(), "https://huggingface.co/models?search=qwen");
+});
+
+test("a reload at the router's address keeps the pages ahead in Forward", () => {
+ const store = useBrowserStore.getState();
+ store.openUrl("https://huggingface.co/models", { newTab: true });
+ const id = useBrowserStore.getState().activeTabId ?? "";
+ const tab = () => useBrowserStore.getState().tabs.find((candidate) => candidate.id === id)!;
+ const url = () => {
+ const entry = currentEntry(tab());
+ return entry.kind === "web" ? entry.url : "";
+ };
+ store.navigate(id, { url: "https://huggingface.co/datasets" }, { replace: false });
+ store.goBack(id);
+ store.updateTab(id, { displayUrl: "https://huggingface.co/models?search=qwen" });
+ store.reload(id);
+ assert.equal(url(), "https://huggingface.co/models?search=qwen");
+ store.goForward(id);
+ assert.equal(url(), "https://huggingface.co/datasets");
+});
+
test("closing the last tab leaves full view, so the browser reopens beside the chat", () => {
const store = useBrowserStore.getState();
for (const tab of useBrowserStore.getState().tabs) store.closeTab(tab.id);