Computer-Use and Browser Agents › Doing it in code: a mock browser
Language AI HandbookAll chapters The mock starts with three synthetic tasks. It provides a task list, a detail view, and an archive confirmation. Archive changes the mock’s record status without reaching any real application. In the reordered variant, confirmation wording and button order both differ from the canonical version.
PythonIn [9]Expand
@dataclass
class MockApp:
"""Local mock with no browser, credentials, network, or file actions."""
variant: str = "canonical"
page: str = "tasks"
tasks: dict[int, str] = field(
default_factory=lambda: {1: "open", 2: "open", 3: "done"}
)
selected: int | None = None
epoch: int = 0
controls: dict[int, tuple[str, int | None, bool]] = field(
default_factory=dict
)
TASK_NAMES = {1: "Review invoice", 2: "Submit expenses", 3: "Book travel"}Every render allocates a new epoch and rebuilds the action map. The map ties IDs to runtime operations, record IDs and enabled state. An accessible label is part of the observation passed to the policy. Execution resolves the element ID against the map, so a page string cannot choose the operation to execute.
PythonIn [10]Expand
def render(app):
"""Create fresh view-local IDs and a trusted action map for that view."""
app.epoch += 1
app.controls = {}
elements = []
def add(role, name, op, task=None, enabled=True):
i = len(elements)
elements.append(
Element(i, role, name, (80, 70 + 40 * i, 230, 32), enabled)
)
app.controls[i] = (op, task, enabled)
text = "Synthetic task list"
if app.page == "tasks":
for task, status in app.tasks.items():
if status != "archived":
add("link", f"Open task {task}", "open", task)
elif app.page == "detail":
task = app.selected
text = TASK_NAMES[task]
add("button", "Back", "back")
add("button", "Mark done", "done", task, app.tasks[task] == "open")
add("button", "Archive", "ask_archive", task)
elif app.page == "confirm":
task = app.selected
text = f"Confirm archive of task {task}"
if app.variant == "reordered":
add("button", "Confirm archive", "archive", task)
add("button", "Cancel", "cancel")
else:
add("button", "Cancel", "cancel")
add("button", "Archive task", "archive", task)
else:
raise ValueError(f"Unknown local page: {app.page}")
return Observation(app.epoch, app.page, tuple(elements), text)The following output shows the task list the policy receives. Numeric references are valid for that view only. If another render occurs before the proposed click, a later semantic match must use the new references even when the visible text is unchanged.
PythonIn [11]Expand
def serialize(obs):
lines = [
f"Local view {obs.page}, epoch {obs.epoch}, viewport {obs.viewport}",
obs.text,
]
lines.extend(
f"[{element.id}] {element.role}: {element.name}"
+ ("" if element.enabled else " (disabled)")
for element in obs.elements
)
return "\n".join(lines)
mock = MockApp()ConsoleOut [12]Expand
Local view tasks, epoch 1, viewport (800, 600) Synthetic task list [0] link: Open task 1 [1] link: Open task 2 [2] link: Open task 3
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!