Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,17 @@ def _utf8(value: str) -> str:


async def _both_ways(client: WebSocket, app: ClientConnection) -> None:
"""Each message from either side sent to the other until one side closes, then the other closed with its code."""
"""Each message from either side sent to the other until one side closes, then the other closed with its code and
reason."""
async def inbound() -> None:
while True:
message = await client.receive()
if message["type"] == "websocket.disconnect":
code = message.get("code") or 1000
await app.close(1000 if code in UNSENDABLE_CLOSES else code)
if code in UNSENDABLE_CLOSES:
await app.close(1000)
else:
await app.close(code, message.get("reason") or "")
return
await app.send(message["text"] if message.get("text") is not None else message["bytes"])

Expand All @@ -207,7 +211,10 @@ async def outbound() -> None:
await client.send_bytes(message)
code = app.close_code or 1000
with contextlib.suppress(RuntimeError, WebSocketDisconnect):
await client.close(1000 if code in UNSENDABLE_CLOSES else code)
if code in UNSENDABLE_CLOSES:
await client.close(1000)
else:
await client.close(code, app.close_reason or "")

tasks = [asyncio.ensure_future(inbound()), asyncio.ensure_future(outbound())]
try:
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def __init__(self, devtools: DevToolsPipe, control: BrowserControl,
home: Callable[[], Path] = Path.home) -> None:
self.devtools, self.control, self.home = devtools, control, home
self.leases: dict[str, Lease] = {}
#: What hears of each lease's end, whatever ends it: its release, its disposal or the browser's.
self.on_end: list[Callable[[Lease], None]] = []
self._made: dict[str, int] = {}
self._tasks: set[asyncio.Future] = set()
self._lock = asyncio.Lock()
Expand Down Expand Up @@ -135,6 +137,7 @@ async def release(self, label: str) -> bool:
if (lease := self.leases.pop(label, None)) is None:
return False
self.control.closing_context(lease.id)
self._ended(lease)
with contextlib.suppress(DevToolsError):
await self.devtools.call("Target.disposeBrowserContext", {"browserContextId": lease.id})
await asyncio.to_thread(shutil.rmtree, lease.folder(self.home()), True)
Expand Down Expand Up @@ -177,6 +180,7 @@ def disposed(self, context_id: Optional[str]) -> None:
deleted, so a lease of its label after has a folder of its own."""
if (lease := self.of(context_id)) is not None:
self.leases.pop(lease.label, None)
self._ended(lease)
folder, gone = lease.folder(self.home()), self.home() / CONTEXTS / f".gone-{lease.id}"
with contextlib.suppress(OSError):
folder.rename(gone)
Expand All @@ -202,10 +206,19 @@ def resend_downloads(self) -> None:

def ended(self) -> None:
"""The browser has gone, and every context with it: no lease is left, and their folders are deleted."""
for lease in list(self.leases.values()):
self._ended(lease)
self.leases.clear()
self._made.clear()
shutil.rmtree(self.home() / CONTEXTS, ignore_errors=True)

def _ended(self, lease: Lease) -> None:
for hook in self.on_end:
try:
hook(lease)
except Exception as e:
logger.warning(f"the end of the lease {lease.label!r}: {type(e).__name__}: {e}")

def _record(self, lease: Lease) -> Lease:
self.leases[lease.label] = lease
self.control.other_contexts[lease.id] = lease.label
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ async def tab_list(self, context: Optional[str] = None, *, every: bool = False)
return [OpenTab(**tab, active=tab["id"] == active, context=self.label_of(tab)).model_dump()
for tab in self.tabs.values() if self.in_context(tab.get("context_id"), context)]

def context_of_frame(self, frame: object) -> Optional[str]:
"""The context of the tab the frame ``frame`` is in, a tab's own frame among them; None for one it hasn't
seen."""
tab = self._frame_tabs.get(frame, frame) if isinstance(frame, str) else None
return self.tabs.get(tab, {}).get("context_id") if tab is not None else None

def label_of(self, entry: dict) -> Optional[str]:
"""The label of the lease a tab or download was in, None for the default context."""
return self.other_contexts.get(entry.get("context_id"))
Expand Down Expand Up @@ -937,6 +943,12 @@ def _on_Page_frameNavigated(self, params: dict, session: Optional[str]) -> None:
load.commits.append(frame["loaderId"])
load.check()

def _on_Page_frameAttached(self, params: dict, session: Optional[str]) -> None:
"""A frame attached to a page of one of its tabs, kept as that tab's: a cross-origin one goes on in a process
of its own, whose navigations its tab's page doesn't hear of."""
if session in self._sessions and isinstance(frame := params.get("frameId"), str):
self._frame_tabs[frame] = self._sessions[session]

def _on_Page_navigatedWithinDocument(self, params: dict, session: Optional[str]) -> None:
"""A tab's page moving to another URL of its document, a fragment or its history API: a command's, when one
asked for it, else its page's."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@
from ..computer import app_data
from ..computer.app import SOCKET, agent_acted, forget_agent_acted
from ..computer.dataplane import file_parts
from ..computer.errors import DeviceUnavailable, InvalidArgument
from ..computer.errors import DeviceUnavailable, InvalidArgument, PermissionDenied, TargetUnavailable
from ..tool_context import DEFAULT_ROLE, ROLE_HEADER, normalize_role
from ..transfers import WriteNamespaceGrant
from ..types import METHOD_ADD, RPC_PATH, EnvironmentExtension, IntakeDeclaration, IntakeFormat, intake_extension
from . import data
from .contexts import CONTEXTS, TASK, Contexts, label_problem
from .scope import Scope
from .control import AGENT, DOWNLOADS, EXIT, LIVE_START_TAB_TIMEOUT_S, LOADED, STOP, UPLOADS, BrowserControl, _now
from .devtools import PIPE_IN, PIPE_OUT, DevToolsError, DevToolsPipe
from .recording import BROWSER_RECORDING, MAX_DURATION_S, MAX_FRAMES, BrowserRecordings
Expand Down Expand Up @@ -168,6 +169,7 @@ def __init__(self) -> None:
self.cdp_tokens = Tokens(SOCKET.parent / "cdp-tokens.json")
self.relay = Relay(self.devtools, dialogs_answered=self.settings.dialogs != AGENT, log=self.log,
control=self.control, contexts=self.contexts)
self.contexts.on_end.append(self._lease_ended)
#: Its recordings: frames of the tab an agent last acted in, and its log, uploaded under each one's grant.
self.recordings = BrowserRecordings(self.log, self.control)
self.devtools.on_close(self._pipe_closed)
Expand Down Expand Up @@ -350,6 +352,10 @@ async def start_tabs_loaded(self) -> bool:
"caller's agent reaches the env, when it names one, else at the address the call reached the browser at, "
"with a token agentenv mints in its path: each connection to it gets its own browser-level session behind a "
"guard, and record lists them as cdp_connections, each with the commands the guard refused or changed. "
"Naming a lease's label as context, cdp returns an address scoped to it, for a user it's granted to: its "
"connections see that lease as the browser's default context, its targets alone, pages, iframes and workers, "
"with the contexts their user made, and what they open, set or download lands in it; the address ends with "
"the lease. An address with no context sees the default context and its user's own, not the leases. "
"sites/set hands it the env's own sites, by name and domains: a start tab on one must load, its document's "
"status 2xx, or its launch counts it failed, while one on any other site is waited on 30 s at most. record "
"keeps each launch's start_tabs: the URL asked for and ended on, the status, any network error, the load time, "
Expand Down Expand Up @@ -429,16 +435,28 @@ async def browser_recording_end_all(self, reason: str = "teardown") -> dict:
return {"recordings": await self.recordings.end_all(reason)}

@extension(BROWSER, op="cdp")
async def browser_cdp(self, user: str, request: Request, origin: Optional[str] = None) -> dict:
async def browser_cdp(self, user: str, request: Request, origin: Optional[str] = None,
context: Optional[str] = None) -> dict:
"""``user``'s CDP address once the browser's ready, the same one each time it's asked for, as the server that
minted it keeps it: under ``origin``, where the caller's agent reaches the env, when the caller names it, as a
deploy names the env's other addresses, since a tunnel in front of the env may not say how it was reached;
else at the address this call reached the browser at."""
else at the address this call reached the browser at. Scoped to the lease ``context`` when it names one,
which must be granted to ``user``, and which the address ends with."""
base = under(_origin(origin), request) if origin is not None else reached(request)
await self._until_ready()
address = self.cdp_tokens.mint(user, base, named=origin is not None)
if context is not None:
if (lease := self.contexts.get(context)) is None:
raise TargetUnavailable(f"no context {context!r} is leased; contexts/list names the leased ones")
if user not in lease.grants:
raise PermissionDenied(f"the context {context!r} isn't granted to {user!r}: contexts/grant grants it")
address = self.cdp_tokens.mint(user, base, named=origin is not None, context=context)
return {"http_url": address.http_url, "ws_url": address.ws_url}

def _lease_ended(self, lease: Any) -> None:
"""A lease ended: the addresses scoped to it end with it, and their connections are closed."""
self.cdp_tokens.revoke(lease.label)
self.relay.lease_ended(lease.id)

def served_asgi(self, asgi: Any) -> Any:
return _LoggedOps(super().served_asgi(asgi), self)

Expand Down Expand Up @@ -466,7 +484,8 @@ async def _cdp_discovery(self, request: Request) -> Response:
"WebKit-Version": version.get("revision"),
"webSocketDebuggerUrl": address.ws_url if address.named else
f"{websocket_form(reached(request))}{ROUTE}{token}/devtools/browser"})
tabs = await self.control.tab_list()
tabs = [tab for tab in await self.control.tab_list(every=True)
if self._sees(address, self.control.tabs.get(tab["id"], {}).get("context_id"))]
except (ConnectionError, DevToolsError, asyncio.TimeoutError) as e:
return JSONResponse({"error": f"the browser isn't answering: {e}"}, status_code=503)
return JSONResponse([{"id": tab["id"], "type": "page", "title": tab["title"], "url": tab["url"],
Expand All @@ -477,7 +496,15 @@ async def _cdp_connect(self, websocket: WebSocket) -> None:
if address is None:
await websocket.close(code=1008)
return
await self.relay.serve(websocket, address.user)
await self.relay.serve(websocket, address.user, address.context)

def _sees(self, address: Any, context_id: Optional[str]) -> bool:
"""Whether an address sees a tab in the browser context ``context_id``, as a connection to it does."""
lease = self.contexts.get(address.context) if address.context is not None else None
if address.context is not None and lease is None:
return False
return Scope(self.contexts, self.relay.target_contexts, address.user, lease.id if lease else None).sees(
context_id)

async def _until_ready(self) -> None:
"""Back once it's ready, as ``computer-app/v1``'s ``ready`` says, or DeviceUnavailable after its ready
Expand Down
Loading
Loading