The Guardrails: What the Agent May Not Do
Why This Matters
An AI agent that can click through your application, write files and change tests is powerful, and that is exactly the risk. Left alone, it can write tests for a plan nobody checked, “fix” a failing test by deleting the assertion that caught a real bug, hide a timing problem behind a sleep, or write files outside your project. A green build that the AI made green proves nothing.
Selenium AI Agent is built around guardrails: checks in the server’s code that refuse an action, whatever the AI was told or decided. This lesson is the map of all of them. Every later lesson points back here when a guardrail comes into play.
What you will learn
- The guardrails of Selenium AI Agent, and what each one prevents
- What a refusal looks like, and what the agent should do next
- Which guardrails are always on, and which depend on the determinism profile
- Where you still have to be the guardrail yourself
Guardrails are code, not instructions
There are two ways to steer an agent:
- Instructions: the agent files and
CLAUDE.mdtell the AI what to do. The AI usually follows them, and sometimes does not. - Guardrails: the tool checks the request and refuses it. The AI cannot talk its way past a refusal; it gets an error and has to change what it is doing.
Selenium AI Agent uses both, but everything in this lesson is the second kind: it is enforced in the server, and logged.
The guardrails
1. No tests from an unapproved plan
The planner saves every plan as a draft. The generator refuses to write a test for a draft plan. This is a real refusal:
Test not written. The test plan .../specs/login.md is a draft: nobody has approved it yet,
so no tests are generated from it.
Show the plan to the user and ask them to review it: planner_review_plan opens it as a page
in their browser with Approve and Request changes buttons. ...
Only if the user explicitly says to skip the review, pass skipPlanReview: true.
Why it matters: the plan is the specification of the tests. If nobody read it, nobody knows what the tests check.
Profile dependent. Under the default explore profile this applies when a test is linked to a plan, and a plan without any review status (an older plan, or one written by hand) is accepted as it is. Under guided and strict, every test must be linked to a plan marked approved, a plan without a status is refused, and skipping the review is not possible.
2. Selenium’s rules for AI agents
Every test the generator writes and every fix the healer applies is checked against Selenium’s official rules for AI agents. A test with an error is not written. Real output, for a test with a sleep, an implicit wait and a Selenium 3 API:
Test not written: it breaks Selenium's official rules.
Selenium rules (https://www.selenium.dev/documentation/ai_agents/): 2 error(s), 1 warning(s) in guardrail_demo/test_sleep_demo.py
warn line 8 [no-implicit-wait] driver.implicitly_wait(5)
→ Prefer explicit waits. Never mix implicit and explicit waits in the same session.
ERROR line 10 [no-sleep] time.sleep(3)
→ No sleeping in tests. Wait explicitly for the condition the next line depends on (clickable, text present, spinner gone).
ERROR line 11 [no-selenium3-api] driver.find_element_by_css_selector('[data-testid="add-to-cart-2"]').click()
→ Selenium 3 API: removed in Selenium 4. Use find_element(By.ID, ...) / findElement(By.id(...)) / FindElement(By.Id(...)) and switch_to.alert.
Fix these lines and call generator_write_test again.
The healer gets one more check: a fix that raises a wait timeout above anything the test had before is refused too (no-longer-timeout), because a longer wait hides a timing problem instead of solving it.
Why it matters: sleeps and old APIs are the classic sources of flaky and broken tests, and an AI trained on years of old examples produces them readily.
3. Selectors checked against the live page
generator_write_test and healer_fix_test extract the selectors from the code and check them in the browser before saving, and report the ones that match nothing or more than one element. The test is written either way, so the agent has to read that report. Under the strict profile there is a hard guardrail on top: tests may only use locked locators, the ones a person has seen in the approved plan, in locators.json, or taught with teach_selector.
4. A heal policy: what a fix may change
The healer classifies every fix, line by line: locator, wait, assertion (or expected value), removed step, added step, other code. The profile decides what it may apply on its own:
| Profile | Heal policy |
|---|---|
explore | free: any change |
guided | assertions-need-approval: changing an assertion or removing a step needs your words in the chat |
strict | locators-only: locators and waits only; never assertions or steps |
Why it matters: the easiest way to make a failing test pass is to change what it expects. If the test failed because the shop now calculates the wrong VAT, that “fix” hides a real bug. Lessons 15 and 16 cover this in depth.
5. No healing against a changed plan, or a hand-edited file
- If the plan changed after the test was written,
guidedwarns andstrictrefuses to heal: the test may now check the wrong thing. Approve the plan again and regenerate. - If a test file was edited by hand or by another tool since the agent wrote it, a profile with a heal policy refuses to heal it until a person confirms the edit. Otherwise the policy could not compare the fix with what was approved.
6. A test must prove it can fail
verify_test_stability runs each test several times, and runs copies with one expected value changed ("1 items" becomes "1 items__mutated__"). Every copy must fail. A copy that stays green has an assertion that checks nothing. The result is stored with the file’s hash: edit the test, and it is unverified again. Lesson 17.
7. A CI gate without an LLM
npx selenium-ai-agent gate --run fails the build when a test’s plan is not approved or changed since, the file changed outside the agent, the test is not verified for its current content, it breaks Selenium’s rules, or (strict) it uses an unlocked locator. No AI is involved: the same result every time. Lesson 18.
8. Files stay in the project
All tools that write files (plans, tests, screenshots, reports) check that the path stays inside the output folder. Asked to save a screenshot somewhere else, the tool refuses:
Error: Path ".../Movies/screen-recorder/learning-path.png" resolves outside the output directory
".../selenium-ai-agent". All output files must be within the output directory.
Set SELENIUM_MCP_OUTPUT_DIR to change the output location, or use
--allow-unrestricted-file-access to disable this check.
The healer’s healer_fix_test is the one exception, because it must edit your existing test files; it always keeps a .bak backup.
9. An audit log
Under guided and strict, every decision is written to .agent-audit.jsonl: plan approvals with the user’s own words, tests written and refused (with the reason), heals with what kind of change and who approved it, rule overrides, verification results, profile changes. When someone asks “who decided to change this assertion?”, there is an answer.
10. Safe defaults in the client
- The plan review page runs on
127.0.0.1only, with a random token. - Every tool is labelled for the AI client as read-only or not (
readOnlyHint,destructiveHint), so you can auto-approve the read-only ones and keep confirming the rest. - Stealth mode hides basic automation flags, but the documentation is explicit: it does not get past advanced bot protection. Use the agent on applications you are allowed to test.
The overview
| Guardrail | Prevents | explore | guided | strict |
|---|---|---|---|---|
| Approved plan | Tests nobody specified | for linked tests | always | always |
| Selenium’s rules | Sleeps, old APIs, mixed waits | on, override allowed | on, override allowed | on, no override |
| Live selector check | Selectors that match nothing | report | report | report + locked locators |
| Heal policy | Hiding a bug by changing the expectation | free | assertions need approval | locators and waits only |
| Plan / file lock | Healing a test against a changed plan or hand edit | off | warn / confirm | block |
| Fails for the right reason | Assertions that check nothing | on request | on (3 runs) | on (5 runs) |
| CI gate | Unverified tests reaching main | available | available | available |
| Output folder | Writes outside the project | on | on | on |
| Audit log | Untraceable decisions | off | on | on |
explore is the default, so existing setups keep working. For a team, guided is the sensible minimum. The profile is one line in .selenium-agent.json:
{ "profile": "guided" }
Where you are still the guardrail
Guardrails enforce a process; they don’t replace judgement:
- Read the plan before you approve it. An approval click on a plan you did not read turns guardrail 1 into theatre.
- Decide what a failure means. The healer may only change a locator in
strict, but whether a red test is a bug in the application or a change in the requirements is your call. - Protect secrets. Never paste passwords or tokens into the chat or into a plan. Use environment variables, a logged-in browser profile or a seed test that reads credentials from the environment (lesson 7).
- Know what you are testing. Point the agent at test environments, not production, and only at applications you are allowed to test.
Exercise
For each situation, name the guardrail that stops it, and under which profile.
- The AI writes a test for a plan it saved two minutes ago.
- A test fails because the cart total is $350.88 instead of the expected $350.89. The healer changes the expected value to $350.88.
- The healer replaces a missing element’s wait with
time.sleep(5). - A developer edits a generated test by hand to make it pass, and pushes.
- The AI saves a screenshot to your Desktop.
Show suggested answer
- Approved plan: the plan is a draft. Under
explorethe test is refused if it is linked to the plan; underguidedandstrictalways. - Heal policy: changing an expected value is an assertion change.
guidedasks for your approval;strictrefuses. Underexploreit is applied, and the audit log is off: this is whyexploreis for trying things out, not for a team suite. (And a cent difference is worth a look: it may be a rounding bug.) - Selenium’s rules:
no-sleepis an error; the fix is not written, under every profile. - CI gate: the file’s hash no longer matches the verified version, so
gatefails the build. Underguidedandstrictthe healer also refuses to heal that file until someone confirms the edit. - Output folder: the path is outside the output directory, so the tool refuses, under every profile.
Exam-style questions
1. What is the difference between an instruction in an agent file and a guardrail in Selenium AI Agent?
- A. There is no difference
- B. An instruction tells the AI what to do and may be ignored; a guardrail is a check in the tool that refuses the action
- C. Guardrails only apply in CI
- D. Instructions are stricter than guardrails
Show answer
2. Under the strict profile, which changes may the healer make to a failing test?
- A. Any change
- B. Locators and waits only
- C. Assertions, if the test still fails
- D. Removing steps that fail
Show answer
locators-only: never assertions or steps.3. Why does the healer refuse a fix that raises a wait timeout?
- A. Longer timeouts are not supported by Selenium
- B. A longer wait usually hides a timing problem instead of waiting for the right condition
- C. It makes the test run faster
- D. Timeouts are a Selenium 3 API