text stringlengths 0 1.47k |
|---|
async def attach(rec: dict) -> Session: |
"""This process's attachment to the recorded browser, made on first use and kept. Every process |
uses the browser's own default context and its first tab, so the page is the same page from |
every replica; the request gate is installed per attachment and reads the record's lists.""" |
sid = str(rec["sid"]) |
s = _SESSIONS.get(sid) |
if s is not None and not s.closed and s.vendor_id == str(rec.get("vendor_id") or ""): |
return s |
if s is not None: |
await detach(s) # the record names another browser: this one is gone |
s = Session(rec) |
s.browser = await _connect(s.cdp_url) |
try: |
# The operator's own browser is shared by every session, so each gets a context of its own |
# (its own cookies and tabs), made over this attachment's connection and closed with it; a |
# vendor browser is one session's alone and its default context is the page. |
contexts = [] if not s.vendor_id else list(getattr(s.browser, "contexts", None) or []) |
s.context = contexts[0] if contexts else await s.browser.new_context(accept_downloads=False, viewport={"width": 1280, "height": 800}) |
async def gate(route, request): |
why = await _allowed(s, request.url) |
if why: |
host = urllib.parse.urlsplit(request.url).hostname or request.url[:60] |
if host not in s.blocked: |
s.blocked.append(host) |
await registry.bump(sid, blocked=host) |
await route.abort("blockedbyclient") |
else: |
await route.continue_() |
await s.context.route("**/*", gate) |
pages = list(getattr(s.context, "pages", None) or []) |
s.page = pages[0] if pages else await s.context.new_page() |
s.page.set_default_timeout(30000) |
on = getattr(s.context, "on", None) |
if callable(on): |
on("page", lambda pg: asyncio.ensure_future(_follow_new_tab(s, sid, pg))) |
except Exception: |
await detach(s) |
raise |
_SESSIONS[sid] = s |
return s |
async def _follow_new_tab(s: Session, sid: str, pg) -> None: |
"""A link the person opened in a new tab while they have the browser is followed in the tab the |
live view shows: the new page's address goes to the shown page and the new page closes, so the |
view stays with them (the console's live view shows one tab, and a link marked to open in a new |
one looked like a click that did nothing: the LinkedIn and X icons on a directory page, |
2026-09-27). Only while the person has the browser: the agent's own tabs are its to manage |
(list_tabs, switch_tab).""" |
try: |
rec = await registry.get(sid) |
if (rec or {}).get("control") != "user" or pg is s.page: |
return |
try: |
await pg.wait_for_load_state("domcontentloaded", timeout=8000) |
except Exception: # noqa: BLE001 |
pass |
url = str(getattr(pg, "url", "") or "") |
if url and url != "about:blank": |
await s.page.goto(url, wait_until="domcontentloaded", timeout=30000) |
await pg.close() |
except Exception: # noqa: BLE001 |
pass |
async def detach(s: Session) -> None: |
"""Drop this process's attachment; the browser itself stays as it is (a connected browser's |
close disconnects it and closes only the contexts this attachment made).""" |
s.closed = True |
if _SESSIONS.get(s.sid) is s: |
_SESSIONS.pop(s.sid, None) |
closer = getattr(s.browser, "close", None) |
if closer: |
try: |
await asyncio.wait_for(closer(), 10) |
except Exception: # noqa: BLE001 |
pass |
async def close_session_browser(rec: dict, s: Session | None) -> dict: |
"""Stop the recorded browser and read the vendor's figures, dropping this process's attachment |
when it has one. Every step is best effort: a browser that would not close must not keep its |
minutes from being metered.""" |
if s is not None: |
s.closed = True |
if _SESSIONS.get(s.sid) is s: |
_SESSIONS.pop(s.sid, None) |
for closer in (getattr(s.context, "close", None), getattr(s.browser, "close", None)): |
if closer: |
try: |
await asyncio.wait_for(closer(), 10) |
except Exception: # noqa: BLE001 |
pass |
figures: dict = {} |
if rec.get("vendor_id"): |
try: |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.