Gate destructive actions, redact secrets, hide sensitive subtrees, and isolate modals with declarative HTML attributes.
This is a documentation section that potentially contains examples, demos, and other
useful information related to a specific part of Bits UI. When helping users with this
documentation, you can ignore the classnames applied to the demos unless they are
relevant to the user's issue.
Browser agents run inside the user's authenticated session and often read untrusted text on the page. Without explicit boundaries, a prompt injection could tell an agent to read an API key, click a billing upgrade, or delete a project.
bits-ui/experimental/agent enforces fail-closed gates for confirmed actions and WebMCP tools using two layers: a programmatic allow callback and five HTML attributes (data-agent-*) you put directly on your markup.
type="password"
data-agent-hidden
Plan Starterdata-agent-confirm
attempts
API key.expect("sk_live_9481a")
agent.query({ text: "Recovery" })
Upgrade to Pro.press()
snapshot
c1
API keyinput
"[redacted]"
c2
Upgrade to Probutton
The allow gate
Every mutating action (and every reveal step) passes through allow(step, { signal }) before executing:
registerTools() requires allow for act and query. Calling registerTools() without allow throws unless you register a read-only tools: ["snapshot"] subset.
Confirmed actions fail closed. If an element has data-agent-confirm and no allow callback is configured on createAgent(), act() refuses the step and tells the agent a human must do it directly.
Gates compose. If you set allow on both createAgent() and registerTools(), both must return true for a WebMCP call to proceed.
Async prompts time out. Pending prompts receive an AbortSignal that aborts if the browser cancels the tool call or after timeout elapses (default 300000 ms / 5 minutes).
Don't call act() or query() inside allow(). Turns are serialized, so nested calls deadlock until timeout. Read step or call agent.snapshot() instead.
Hide your approval UI. Add data-agent-hidden to your confirmation dialog so the agent cannot see it in snapshot() or click Approve itself.
data-agent-confirm
Add data-agent-confirm="reason" to any action that spends money, deletes data, sends emails, or changes permissions. The reason appears in snapshot() as confirm and is passed into allow(step) as step.confirm.
On buttons and triggers
<Button.Root data-agent-confirm="Charges $49/mo to your saved card" onclick={upgradeWorkspace}> Upgrade to Pro</Button.Root>
On individual options and menu items
You don't have to lock down an entire Select, RadioGroup, Tabs, or DropdownMenu just because one option is risky. Put data-agent-confirm on the specific item:
<Select.Root type="single" bind:value={plan} {items}> <Select.Trigger aria-label="Billing plan"> <Select.Value /> </Select.Trigger> <Select.Portal> <Select.Content> <Select.Item value="starter" label="Starter">Starter</Select.Item> <Select.Item value="enterprise" label="Enterprise" data-agent-confirm="Upgrades plan to Enterprise ($500/mo)" > Enterprise </Select.Item> </Select.Content> </Select.Portal></Select.Root><DropdownMenu.Item data-agent-confirm="Removes Jane Doe from the workspace" onSelect={removeMember}> Remove member</DropdownMenu.Item>
Per-item confirmation works on Select.Item, Combobox.Item, RadioGroup.Item, ToggleGroup.Item, Tabs.Trigger, Accordion.Trigger / Accordion.Item, Command.Item, DropdownMenu.Item, ContextMenu.Item, Menubar.Item, and buttons inside Dialog, AlertDialog, and Popover.
Closed overlays
When an agent targets an option inside a closed DropdownMenu, AlertDialog, Select, or Popover, the bridge gates opening through allow, inspects data-agent-confirm on the target child, and closes the overlay before awaiting allow() for the action.
Confirmed containers are never auto-revealed
When act() or query({ closed: true }) scans closed Tabs, Collapsible, or Accordion panels, any trigger marked with data-agent-confirm is skipped automatically.
data-agent-secret
Put data-agent-secret on a field or ancestor container whenever an agent should be allowed to write a value (or know the field exists) without ever reading what's in it:
Non-empty values always serialize as "[redacted]" in snapshot(), query(), and act() step results. Empty values stay "".
{ action: "expect" } is always refused, so an agent cannot guess or brute-force a secret character by character.
Error messages thrown by actions on secret fields are redacted as well.
Fields marked secret automatically
PinInput.Root is always secret.
Input.Root and native <input> are secret automatically when type="password" or when autocomplete contains current-password, new-password, one-time-code, or any cc-* payment token. Once observed as a password, an input stays latched as secret even if toggled to type="text". For password inputs with a show/hide toggle that a user might reveal before the first scan, add data-agent-secret as well.
data-agent-hidden
Add data-agent-hidden to remove an entire subtree from snapshots, queries, and accessible-name computation. It pulls double duty:
<Label.Root for="branch-input"> Production branch <span data-agent-hidden>BETA</span></Label.Root><Input.Root id="branch-input" bind:value={branch} />
The agent sees "Production branch" instead of "Production branch BETA".
data-agent-row and data-agent-section
Repeated controls (like an "Actions" menu in every table row, or identical switches across settings tabs) need context so an agent can target the right one without fragile indices.
data-agent-row
Add data-agent-row to a <tr> or list item. The bridge derives the row label from the row's visible text (or an explicit data-agent-row="Jane Doe" attribute) and attaches row to every control inside it:
{#each members as member (member.id)} <tr data-agent-row={member.name}> <td>{member.name}</td> <td> <DropdownMenu.Root> <DropdownMenu.Trigger aria-label="Member actions">Actions</DropdownMenu.Trigger> <DropdownMenu.Portal> <DropdownMenu.Content> <DropdownMenu.Item onSelect={() => promote(member.id)}> Promote to Admin </DropdownMenu.Item> </DropdownMenu.Content> </DropdownMenu.Portal> </DropdownMenu.Root> </td> </tr>{/each}
Now { ref: "Jane Doe", action: "press", args: ["Promote to Admin"] } targets Jane's menu directly.
data-agent-section
Native landmarks (<section aria-label="...">, <fieldset> with <legend>, <form aria-label="...">, [role="region"]) already supply section context. Use data-agent-section="Billing" on any wrapper when you want an explicit prefix for "Billing > Auto renew" targeting.
Modal isolation
While a modal Dialog or AlertDialog is open, bits-ui/experimental/agent automatically marks every control outside the modal as disabled: true with actions: []. Any act() step targeting a background control fails until the dialog closes, matching keyboard focus trapping.