Let an agent operate an interface within a safe boundary
Browser and computer-use agents act through interfaces such as the DOM, screenshots, keyboard, mouse, files, or a terminal. Code-executing agents add the ability to create and run programs. These tool surfaces are powerful but less predictable and more operationally risky than a narrow API, so every action needs restricted access and a visible post-action check.
What You Will Learn
- Choose the most reliable interface: API, DOM, GUI, or code execution.
- Follow observe–act–re-observe–verify after every interface change.
- Design a least-privilege sandbox for generated code.
- Require confirmation before consequential or irreversible actions.
Use the narrowest reliable capability
A typed API exposes structured inputs and outputs and is usually easier to validate, authorize, retry, and monitor than a graphical interface. Use browser DOM interaction when no suitable API exists, and full screenshot/coordinate computer use only when the task genuinely depends on the visible interface.
After every click, navigation, or terminal command, observe again. The old screenshot, coordinates, DOM handle, or prompt may no longer describe the current state. Deterministically verify the intended outcome, and ask for confirmation before purchases, submissions, deletion, messages, or other irreversible actions.
Capability choices for acting agents
Observe the page, take one action and verify the change
Prefer a direct, typed API when one exists; otherwise observe the current page, screen, file, or terminal state.
Choose one bounded action and execute it through the narrowest available tool.
Run generated code in a sandbox that limits readable and writable paths, network destinations, processes, secrets, CPU, memory, and runtime.
Treat webpages, documents, terminal output, and generated code as untrusted content rather than instructions or authorization.
Observe again and use deterministic checks to confirm the expected state change.
Require confirmation before purchases, submissions, deletion, messages, or moving sandbox results into important systems.
Instructional figure
Observe, act, re-observe, verify
Why must a browser or computer-use agent look again after every action?
Instructional figure
A least-privilege code sandbox
Which resources may generated code access, and which remain outside the boundary?
SANDBOX
Explicit mounts and capabilities only
A Realistic Example: Searching for a trip and analyzing the result safely
The agent enters a destination and dates, confirms the visible fields, and runs a small analysis script in a sandbox that can read only the exported results and write only one output folder. It verifies the output, then stops before booking and asks the user to approve the itinerary and price.
Read the Mechanism in Code
Use the label and note to understand what this block represents before reading each line.
page = browser.observe()
browser.type(page.field("Destination"), "Jaipur")
browser.click(page.button("Search"))
assert "Jaipur" in browser.observe().text
policy = SandboxPolicy(
read=[search_export], write=[results_dir],
network=False, timeout_seconds=60, memory_mb=512,
)
analysis = sandbox.run(generated_code, policy)Hands-On Runnable Python Lab: Check the Receipt Instead of Trusting the Click
Two small HTML snapshots simulate an application before and after saving a draft. Python’s HTMLParser reads accessible button and status information. This offline exercise does not control a browser or execute generated code.
- Parse the initial snapshot and require exactly one enabled Save draft button.
- Treat the click as a proposed action, not proof of success.
- Read a new snapshot containing the exact draft receipt D-17.
- Test the unchanged snapshot as a negative case: clicking alone must not pass verification.
A changed interface must prove the intended postcondition
The draft ID ties the receipt to this task. An unchanged status is not success, even if the button was clicked without an interaction error.
from html.parser import HTMLParser
class Snapshot(HTMLParser):
def __init__(self):
super().__init__()
self.buttons, self.statuses = [], []
self.active = None
def handle_starttag(self, tag, attrs):
attrs = dict(attrs)
if tag == "button":
self.active = ["button", "", "disabled" not in attrs]
elif attrs.get("role") == "status":
self.active = ["status", "", True]
def handle_data(self, text):
if self.active:
self.active[1] += text
def handle_endtag(self, tag):
if self.active and ((tag == "button" and self.active[0] == "button")
or (tag == "p" and self.active[0] == "status")):
kind, text, enabled = self.active
(self.buttons if kind == "button" else self.statuses).append((text.strip(), enabled))
self.active = None
def inspect(html):
parser = Snapshot()
parser.feed(html)
return parser
before = '<button>Save draft</button><p role="status">Unsaved</p>'
after = '<button>Save draft</button><p role="status">Draft D-17 saved</p>'
page = inspect(before)
targets = [label for label, enabled in page.buttons if label == "Save draft" and enabled]
print("unique enabled target:", len(targets) == 1)
for label, html in [("unchanged page", before), ("receipt page", after)]:
statuses = [text for text, _ in inspect(html).statuses]
verified = len(targets) == 1 and "Draft D-17 saved" in statuses
print(label, "verified:", verified)
unique enabled target: True
unchanged page verified: False
receipt page verified: TrueWhat This Run Shows
- The selector has a semantic target, not a hard-coded coordinate.
- The negative case contains a usable button but no receipt, so it correctly fails.
- The status receipt is associated with draft D-17, avoiding a generic 'success' message unrelated to the task.
Apply the same distinction in real browser automation: observe, interact, observe again, then verify. For high-impact actions, use an authoritative backend receipt or human confirmation rather than relying solely on page text.
From the Exercise to an Application
Experiment: Add a second enabled Save draft button to the initial snapshot.
The target is no longer unique and verification fails. A real controller should refine the locator or ask for clarification, not click the first match arbitrarily.
Summary / Key Takeaways
- Direct typed APIs are usually more reliable than browser or GUI automation and should be preferred when available.
- Browser/computer-use agents operate through changing interfaces, so they should follow an observe → act → re-observe → verify loop.
- Screenshots, coordinates, DOM references, and page state can become stale after navigation or layout changes.
- Generated code should be treated as untrusted and run inside a sandbox with explicit filesystem, network, process, secret, time, and compute limits.
- A container alone is not automatically a complete security boundary; permissions and exposed resources must still be configured.
- Deterministic post-action checks should confirm that the intended state change actually occurred.
- Purchases, submissions, deletion, messages, or moving sandbox output into important systems should require appropriate confirmation.
Verified Further Reading
These primary research papers and current official specifications support the concepts and implementation guidance in this lesson. The explanations, analogies, exercises, and code above remain original teaching material.
OSWorld: Benchmarking Multimodal Agents for Open-Ended Tasks in Real Computer Environments
Research benchmark for agents operating real desktop applications through visual and accessibility interfaces.
AgentDojo: A Dynamic Environment to Evaluate Prompt Injection Attacks and Defenses for LLM Agents
Research benchmark for tool-using agents exposed to untrusted external content and prompt-injection attacks.