loopable / api /browser_worker.py
fsanyoto's picture
Deploy AIOS web (React glide grid + FastAPI slice)
8145bee verified
Raw History Blame
21.9 kB
"""Execute read-only Login tasks while credentials stay inside the browser worker."""
import dash_norm
import json
import re
import secrets
import time
from decimal import Decimal
from urllib.parse import urlsplit
from browser_backend import BrowserRefusal, get_backend
from browser_local import quiet_execution
from core import login_budget, login_origins, login_redact, login_relay, login_run, logins
MODEL_TOOLS = [
{"name": "goto", "schema": {"type": "object", "properties": {"url": {"type": "string"}},
"required": ["url"], "additionalProperties": False}},
{"name": "click", "schema": {"type": "object", "properties": {"handle": {"type": "string"}},
"required": ["handle"], "additionalProperties": False}},
{"name": "snapshot_text", "schema": {"type": "object", "properties": {},
"additionalProperties": False}},
]
class Worker:
"""Pause model calls during sign-in and scrub every outbound value."""
def __init__(self, rt, login_id, username, *, is_admin=False, backend=None,
breaker=None, totp_clock=None, reservation_id=None):
self.rt, self.login_id, self.username = rt, login_id, username
self.is_admin, self.backend = is_admin, backend
self.breaker = breaker or login_run.Breaker()
self.totp_clock = totp_clock or time.time
self.reservation_id = reservation_id
self._credentials = []
self.origin = None
def scrub(self, value):
if isinstance(value, str):
return dash_norm.plain(login_redact.scrub(value, self._credentials))
if isinstance(value, dict):
return {self.scrub(key): self.scrub(item) for key, item in value.items() if isinstance(key, str)}
if isinstance(value, (list, tuple)):
return [self.scrub(item) for item in value]
if value is None or type(value) in (bool, int, float):
return value
return None
def _url(self, value):
clean = self.scrub(value)
if not isinstance(clean, str) or "[hidden]" in clean:
raise BrowserRefusal("blocked_origin")
parts = urlsplit(clean)
if parts.username is not None or parts.password is not None:
raise BrowserRefusal("blocked_origin")
return clean
def snapshot(self):
# Keep this boundary visible to the physical credential-leak mutation.
return self.scrub(self.backend.snapshot_text())
def _controls(self):
self.breaker.check()
body = json.loads(self.backend.snapshot_text())
if not isinstance(body, dict) or not isinstance(body.get("controls"), list):
raise BrowserRefusal("sign_in_failed")
self._challenge(body)
return body["controls"]
def _challenge(self, body):
text = body.get("text", "")
if isinstance(text, str):
for item in body.get("controls", []):
if isinstance(item, dict) and item.get("kind") == "a" and isinstance(item.get("label"), str):
label = item["label"].strip()
if label:
text = text.replace(label, "", 1)
kinds = {item.get("kind") for item in body.get("controls", []) if isinstance(item, dict)}
if (body.get("state") == "captcha" or "captcha" in kinds
or re.search(r"\b(?:captcha|solve (?:this|the) puzzle|human must complete|requires a human)\b",
text if isinstance(text, str) else "", re.I)):
raise BrowserRefusal("needs_human")
def _push_wait(self):
logins.set_wait_state(self.rt, self.reservation_id, self.username, "waiting_for_approval")
approval_url = self.backend.landed_url
while True:
self.breaker.check()
logins.entry_for_run(self.rt, self.login_id, self.username, self.is_admin)
body = json.loads(self.backend.snapshot_text())
self._challenge(body)
controls = body.get("controls", [])
if (body.get("state") in ("approved", "signed_in")
or (body.get("state") is None
and self.backend.landed_url != approval_url
and not any(item.get("kind") in ("password", "code", "push", "approval")
for item in controls))):
logins.set_wait_state(self.rt, self.reservation_id, self.username, "running")
return
time.sleep(0.25)
def _fill(self, controls, kind, value):
candidates = [item for item in controls if item.get("kind") == kind]
if len(candidates) != 1:
raise BrowserRefusal("sign_in_failed")
self.breaker.check()
self.backend.fill_secret(candidates[0]["handle"], value, origin=self.origin)
def _submit(self, controls):
buttons = [item for item in controls if item.get("kind") == "button"]
if len(buttons) != 1:
raise BrowserRefusal("sign_in_failed")
self.breaker.check()
self.backend.click(buttons[0]["handle"], executor=True)
def sign_in(self, login_id):
"""Fill credentials only in the executor and return two safe fields."""
try:
return self._sign_in(login_id)
except (BrowserRefusal, login_run.BreakerOpen, login_budget.LoginBudgetExceeded,
login_budget.LoginBudgetUnavailable, login_origins.LoginOriginError,
logins.LoginAccessDenied):
raise
except Exception:
raise BrowserRefusal("sign_in_failed") from None
def _sign_in(self, login_id):
if login_id != self.login_id:
raise BrowserRefusal("sign_in_failed")
# Import only when the credentialed executor runs, never during web-tier import.
from core import login_unseal
with quiet_execution():
fields = login_unseal.open_fields(self.rt, login_id, self.username, self.is_admin,
reservation_id=self.reservation_id)
self._credentials.extend(fields[name] for name in ("username", "password", "totp_seed")
if fields[name])
self.origin = fields["origin"]
self.breaker.check()
self.backend.goto(self.origin)
controls = self._controls()
self._fill(controls, "username", fields["username"])
self._fill(controls, "password", fields["password"])
self._submit(controls)
controls = self._controls()
if any(item.get("kind") == "password" for item in controls):
return {"ok": False, "landed_url": self.scrub(self.backend.landed_url)}
row = logins.entry_for_run(self.rt, login_id, self.username, self.is_admin)
factor = row["second_factor"]
if factor == "push":
self._push_wait()
controls = self._controls()
if any(item.get("kind") == "code" for item in controls):
if factor in ("text", "sms", "email", "manual", "code", "ask_user"):
code = login_relay.wait_for_code(
self.rt, self.username, self.reservation_id, row["label"],
"email" if factor == "email" else "text", self.breaker)
controls = self._controls()
elif fields["totp_seed"]:
from core import login_totp
code = login_totp.worker_code(fields["totp_seed"], self.totp_clock())
else:
raise BrowserRefusal("needs_code")
self._credentials.append(code)
self._fill(controls, "code", code)
self._submit(controls)
controls = self._controls()
if any(item.get("kind") in ("code", "password") for item in controls):
return {"ok": False, "landed_url": self.scrub(self.backend.landed_url)}
fields.clear()
return {"ok": True, "landed_url": self.scrub(self.backend.landed_url)}
def action(self, tool):
self.breaker.check()
if not isinstance(tool, dict) or set(tool) != {"name", "arguments"}:
raise BrowserRefusal("read_only")
name, args = tool["name"], self.scrub(tool["arguments"])
if not isinstance(args, dict):
raise BrowserRefusal("read_only")
if name == "goto" and set(args) == {"url"}:
self.backend.goto(self._url(args["url"]))
elif name == "click" and set(args) == {"handle"}:
self.backend.click(args["handle"])
elif name == "snapshot_text" and not args:
pass
else:
raise BrowserRefusal("read_only")
return {"ok": True, "landed_url": self.scrub(self.backend.landed_url)}
def _cost(value):
if type(value) not in (int, float, Decimal):
raise login_run.BreakerOpen("cost_unknown")
try:
return login_budget._decimal(value)
except Exception:
raise login_run.BreakerOpen("cost_unknown") from None
class PricedModelAdapter:
"""Require trusted quotes and measured usage for every provider attempt, including fallbacks."""
def __init__(self, *, quote_attempt, measure_attempt, invoke):
if not all(callable(fn) for fn in (quote_attempt, measure_attempt, invoke)):
raise login_run.BreakerOpen("cost_unknown")
self.quote_attempt, self.measure_attempt, self.invoke = quote_attempt, measure_attempt, invoke
def request(self, request, breaker, on_attempt, on_measured):
tickets, measured_total, failure = {}, Decimal(0), None
def fail():
nonlocal failure
failure = "cost_unknown"
raise login_run.BreakerOpen(failure) from None
def before_attempt(spec, messages, tools):
if failure or any(not row["done"] for row in tickets.values()):
fail()
try:
breaker.check()
if tickets:
breaker.step()
bound = _cost(self.quote_attempt(spec, messages, tools))
if bound > breaker.usd_limit - Decimal(str(breaker.spent_usd)):
raise login_run.BreakerOpen("cost_limit")
token = secrets.token_hex(12)
tickets[token] = {"spec": spec, "bound": bound, "done": False}
on_attempt()
return token
except Exception:
fail()
def after_attempt(token, usage):
nonlocal measured_total
if failure or token not in tickets or tickets[token]["done"]:
fail()
ticket = tickets[token]
try:
measured = _cost(self.measure_attempt(ticket["spec"], usage))
on_measured(measured)
measured_total += measured
ticket["done"] = True
breaker.charge(measured)
if (measured > ticket["bound"] or isinstance(ticket["spec"], dict)
and ticket["spec"].get("_usage_bound_exceeded") is True):
fail()
except Exception:
fail()
try:
reply = self.invoke(request, before_attempt=before_attempt, after_attempt=after_attempt)
except Exception:
if not failure and (not tickets or any(not row["done"] for row in tickets.values())):
on_attempt()
raise login_run.BreakerOpen("model_failed") from None
if failure or not tickets or any(not row["done"] for row in tickets.values()):
if not failure:
on_attempt()
fail()
if not isinstance(reply, dict):
fail()
return {**reply, "cost_usd": measured_total}
def run_web_task(rt, login_id, goal, *, username, ask_model, is_admin=False,
backend=None, model_cost_bound_usd=None, breaker=None, totp_clock=None, run_id=None):
"""Reserve the run bound and return scrubbed evidence with fixed outcomes."""
result = {"ok": False, "status": "run_failed", "answer": "", "transcript": [],
"tool_calls": [], "run_id": None}
worker = Worker(rt, login_id, username, is_admin=is_admin, backend=backend,
breaker=breaker, totp_clock=totp_clock)
admitted = False
usage_accounted = True
browser_usage_accounted = True
paid_browser = False
browser_bound = Decimal(0)
run_limit = worker.breaker.usd_limit
measured_total = Decimal(0)
try:
with quiet_execution():
if run_id is not None:
run = logins.run_for_owner(rt, run_id, username)
if run["entry_id"] != login_id or run["status"] != "running":
raise logins.LoginAccessDenied("This Login run is unavailable.")
result["run_id"] = worker.reservation_id = run_id
admitted = True
# Protected or revoked Logins fail before backend selection or navigation.
row = logins.entry_for_run(rt, login_id, username, is_admin)
if (worker.breaker.step_limit > 25 or worker.breaker.usd_limit > Decimal("0.50")
or worker.breaker._seconds > 900):
raise login_run.BreakerOpen("limit_invalid")
worker.origin = row["origin"]
worker._credentials.append(row["username"])
worker.backend = backend if backend is not None else get_backend()
priced = isinstance(ask_model, PricedModelAdapter)
model_bound = None if priced else _cost(model_cost_bound_usd)
if run_id is None:
run = logins.begin_run(rt, login_id, username, is_admin,
reserve_usd=worker.breaker.usd_limit)
else:
run = logins.run_for_owner(rt, run_id, username)
if run["entry_id"] != login_id or run["status"] != "running":
raise logins.LoginAccessDenied("This Login run is unavailable.")
admitted = True
worker.reservation_id = result["run_id"] = run["id"]
worker.breaker.check()
paid_browser = getattr(worker.backend, "paid_browser", False) is True
if paid_browser:
from browser_agentcore import AgentCoreBackend
if not isinstance(worker.backend, AgentCoreBackend):
raise BrowserRefusal()
if not priced and not worker.backend._fixture_mode:
raise BrowserRefusal()
worker.backend.prepare_run(tenant=rt.key, run_id=run["id"], origin=worker.origin,
timeout_seconds=int(min(900, worker.breaker._seconds)))
browser_bound = _cost(getattr(worker.backend, "cost_bound_usd", None))
if (not paid_browser and browser_bound) or browser_bound >= run_limit:
raise BrowserRefusal()
if not priced and model_bound + browser_bound > run_limit:
raise login_run.BreakerOpen("cost_limit")
# Keep the browser maximum unavailable to every model attempt and fallback.
worker.breaker.usd_limit = run_limit - browser_bound
worker.breaker.check()
browser_usage_accounted = not paid_browser
worker.backend.open(worker.origin, origin_guard=lambda origin: logins.check_origin(rt, origin))
signed = worker.sign_in(login_id)
result["tool_calls"].append({"name": "sign_in", "arguments": {"login_id": login_id},
"result": signed})
if not signed["ok"]:
raise BrowserRefusal("sign_in_failed")
messages = [{"role": "system", "content": "Read this website. Use only the supplied read tools. "
"Return an answer or a tool with name and arguments. Never write or submit a form."},
{"role": "user", "content": worker.scrub(goal)}]
while True:
worker.breaker.step()
if not priced and model_bound > worker.breaker.usd_limit - Decimal(str(worker.breaker.spent_usd)):
raise login_run.BreakerOpen("cost_limit")
snapshot = worker.snapshot()
try:
body = json.loads(snapshot)
except (TypeError, ValueError):
body = {"text": snapshot}
if isinstance(body, dict):
worker._challenge(body)
request = worker.scrub({"login_id": login_id, "messages": messages,
"snapshot": snapshot, "tools": MODEL_TOOLS})
result["transcript"].append({"role": "tool", "content": snapshot})
worker.breaker.check()
def attempted():
nonlocal usage_accounted
usage_accounted = False
def measured_attempt(amount):
nonlocal usage_accounted, measured_total
login_budget.charge(rt, run["id"], "charge_" + secrets.token_hex(12), amount)
measured_total += amount
usage_accounted = True
if not priced:
usage_accounted = model_bound == 0
try:
raw = (ask_model.request(request, worker.breaker, attempted, measured_attempt)
if priced else ask_model(request))
except login_run.BreakerOpen:
raise
except Exception:
result["status"] = "model_failed"
break
if not isinstance(raw, dict):
raise login_run.BreakerOpen("cost_unknown")
measured = _cost(raw.get("cost_usd"))
if not priced:
measured_total += measured
# Book usage before parsing the answer or opening the breaker.
login_budget.charge(rt, run["id"], "charge_" + secrets.token_hex(12), measured)
usage_accounted = True
worker.breaker.charge(measured)
if measured > model_bound:
raise login_run.BreakerOpen("cost_bound_exceeded")
reply = worker.scrub(raw)
if isinstance(reply.get("answer"), str) and reply["answer"].strip():
result.update(ok=True, status="completed", answer=reply["answer"])
result["transcript"].append({"role": "assistant", "content": reply["answer"]})
break
tool = reply.get("tool")
output = worker.action(tool)
result["tool_calls"].append({**tool, "result": output})
messages.extend(({"role": "assistant", "content": json.dumps(tool)},
{"role": "tool", "content": json.dumps(output)}))
except login_origins.LoginOriginError:
result["status"] = "blocked_origin"
except logins.LoginAccessDenied:
result["status"] = "login_unavailable"
except login_budget.LoginBudgetExceeded:
result["status"] = "budget_exceeded"
except login_budget.LoginBudgetUnavailable:
result["status"] = "budget_unavailable"
except login_run.BreakerOpen:
result["status"] = "breaker_open"
except BrowserRefusal as exc:
result["status"] = exc.code
if exc.code == "needs_human":
result["answer"] = logins.CAPTCHA_MESSAGE
except Exception:
result["status"] = "run_failed"
finally:
with quiet_execution():
if worker.backend is not None:
try:
browser_usage = worker.backend.close()
if paid_browser:
if getattr(worker.backend, "billing_attempted", True):
measured_browser = _cost(browser_usage)
login_budget.charge(rt, result["run_id"],
"charge_" + secrets.token_hex(12), measured_browser)
measured_total += measured_browser
browser_usage_accounted = True
if measured_browser > browser_bound or measured_total > run_limit:
result.update(ok=False, status="breaker_open", answer="")
else:
browser_usage_accounted = True
except Exception:
if paid_browser:
browser_usage_accounted = not getattr(worker.backend, "billing_attempted", True)
result.update(ok=False, status="backend_unavailable", answer="")
worker.breaker.usd_limit = run_limit
usage_accounted = usage_accounted and browser_usage_accounted
if admitted:
if usage_accounted:
try:
login_budget.release(rt, result["run_id"])
except Exception:
result.update(ok=False, status="budget_unavailable", answer="")
try:
logins.finish_run(rt, result["run_id"], result["status"],
steps=min(worker.breaker.steps_taken, 25),
spent_usd=measured_total, cost_known=usage_accounted)
except Exception:
result.update(ok=False, status="run_failed", answer="")
return worker.scrub(result)