feat(web): answer unhandled native dialogs with web({ dialogs }) (ENG-963)

KD
4d ago
+752-63

What

web({ dialogs: 'accept' | 'dismiss' }) answers every native dialog (alert, confirm, prompt, beforeunload) that no browser.onDialog handler takes, in every test of the target:

targets: [{ name: 'web', engine: web({ dialogs: 'accept' }), app: { url: 'http://localhost:3000' } }],

An accepted prompt returns its default text, as clicking OK does. Each dialog the option answers is a line in the attempt's app log, so a message the app shows only in an alert stays in the report and trace.md. The agent action that opened the dialog shows the model the same line in its result, so an explorer still reads an app's error alert:

Tapped #n6.

Screen unchanged since revision b4 (re-observed as revision b30, path /); the ids you have stay valid.

System log (not on the screen):
- alert dialog "Could not save: server error 500" opened; accepting it per web({ dialogs: 'accept' })

A handler a test registers still answers first. With the option unset, nothing changes except the error, which now names both fixes:

APP_NOT_OPEN: unhandled confirm dialog: Delete Invoice 42?; register a handler with browser.onDialog, or answer every dialog with web({ dialogs: 'accept' }) or web({ dialogs: 'dismiss' })

Why

e2e explore and agent.act steps can't register browser.onDialog. A confirm() behind a Delete button was dismissed, and the tap came back to the explorer as Action failed: unhandled confirm dialog: ..., which no tool can answer. Deletion charters in a bug bash never got past it. The option gives a config a way to answer those dialogs.

How it works

  1. DialogRouter takes the policy and the attempt's app log beside its latch (packages/web/src/dialogs.ts:45). route tries the newest registered handler first. With none and a policy set, answer writes one system line to the app log, such as alert dialog "Could not save" opened; accepting it per web({ dialogs: 'accept' }), then accepts with dialog.defaultValue() or dismisses (packages/web/src/dialogs.ts:83, :99). The line is written before the answer, while the step that opened the dialog is still running, so it says the answer is under way. Playwright's bare accept() would answer a prompt with an empty string. Without the line, an app's error alert would close with no trace, where without the option it failed the step with its message. A failure to answer is latched as ENGINE_FAILURE and the dialog is dismissed, as a failing handler's is. A handler's answer is the test's own and is not logged.
  2. With no handler and no policy, the dialog is dismissed and INVALID_STATE is latched as before. The message now names browser.onDialog and web({ dialogs }) (packages/web/src/dialogs.ts:111). A function handler that answers neither way still fails, policy or not: the handler took the dialog.
  3. PlaywrightSurface reads options.dialogs and passes it to the router it creates at construction and at each startAttempt, which also gets context.appLog (packages/web/src/surface.ts:423, :613). Only the attempt's router receives dialogs.
  4. web() validates the option at config load (packages/web/src/engine.ts:110). Any other value is INVALID_CONFIG naming only the value's kind, never the value: a config can pass any string there, and web() throws before the runner knows the config's secrets. DialogPolicy is exported beside DialogHandler.
  5. The agent shows the model those lines. The session's app log route keeps the newest five system lines apart from its quota and sink, behind a cursor (systemLines, packages/core/src/engine/session.ts:114, surface.ts:171). Each agent step's observation feed takes the cursor when the step starts and reads the lines logged since its previous look on every executor observe, whitespace collapsed, redacted, and cut at 300 characters (packages/core/src/agent/observation-feed.ts:121, :245). They reach the executor as ExecutorObservation.systemLog (packages/core/src/agent/executor.ts:181), so a custom executor gets them too. The built-in agent and e2e mcp list them under the screen in every rendering of a look: an action's result, the opening screen, MCP's observe, and run_code (packages/core/src/agent/screen-update.ts:223, :302, :345). The note comes after the lead and the screen's headline, which the loop guards read. Each line is shown once, with the first look after it. An action whose look carries a line is never reported as having no visible effect, since an alert changes nothing listed (:360). Lines read by looks an action takes on its way are carried into its result: the scroll repeats (primitives.ts) and the agent's second opening look for pixels (default-agent.ts). Transcript compaction keeps the note when it elides an old screen (transcript-compaction.ts:164). The engine contract and the wire do not change: AppLogEntry's doc now says a system line also reaches the model. The model-input list in AGENTS.md and docs/reference/security.mdx names it.
  6. Docs:
    • reference/web.mdx: a dialogs entry (options table, entry with what each answer returns, its ENGINE_FAILURE, the app log's limits, and the line in the agent's result, WebOptions), and onDialog notes. One says onDialog('accept') passes a prompt no text, which Chromium and WebKit answer as an empty string.
    • executors.mdx describes systemLog; reference/engine.mdx carries the new AppLogEntry doc.
    • A pointer in browser.mdx, "Get past confirmation dialogs" in explore.mdx, and the dialog lines in the app log list in debugging.mdx.
    • The Playwright and Cypress migration guides name the option. Playwright dismisses an unhandled dialog but accepts a beforeunload, so its row also gives the handler that matches it exactly.
    • packages/web/README.md and the consumer skill's setup.md list the option. The bug-bash config sets web({ dialogs: 'accept' }), with a note that @e2e-dev/web 0.17.0 refuses the key, and explore.md and writing-tests.md describe it. writing-tests.md now says an unanswered dialog fails with APP_NOT_OPEN, the code a test sees, where it said INVALID_STATE.

The issue's other option, holding the dialog open and showing it in the observation so the agent picks OK or Cancel per dialog, is left out. Playwright's click does not return while a dialog is open, and the page can't be read until the dialog closes. That design would mean racing every action against the dialog and handing the agent a synthetic node tree.

onDialog('accept') keeps its current behavior: it passes a prompt no text, which Chromium and WebKit answer as an empty string. Only the policy passes the default text. That keeps this change from altering existing tests.

The policy is not part of the replay cache key. No other engine option is either (viewport, locale, headers; packages/core/src/cache/identity.ts). A recording made under 'accept' that stops reproducing under 'dismiss' is re-recorded, like after any change to what the app does.

Issue

Resolves ENG-963

Verified

Ran it locally: yes

  • e2e mcp from this build, driven by a small MCP client against a local page with a Delete button behind confirm('Delete Invoice 42?') and a Save button that calls alert('Could not save: server error 500'). It taps Save, then Delete, once per engine setting. The MCP call result is the same tool result the agent reads:
--- tap Save (web({ dialogs: 'accept' }))
Tapped #n6.
Test line: await screen.getByRole("button", "Save").tap();

Screen unchanged since revision b4 (re-observed as revision b30, path /); the ids you have stay valid.

System log (not on the screen):
- alert dialog "Could not save: server error 500" opened; accepting it per web({ dialogs: 'accept' })

--- tap Delete (web({ dialogs: 'accept' }))
Tapped #n5.
Test line: await screen.getByRole("button", "Delete").tap();

The screen changed substantially since revision b30. Current screen (revision b32, path /, 3 nodes):
#root document "Invoices"
 #n2 heading "Invoices"
 #n6 button "Save"

System log (not on the screen):
- confirm dialog "Delete Invoice 42?" opened; accepting it per web({ dialogs: 'accept' })

--- tap Save (web())
APP_NOT_OPEN: unhandled alert dialog: Could not save: server error 500; register a handler with browser.onDialog, or answer every dialog with web({ dialogs: 'accept' }) or web({ dialogs: 'dismiss' })

--- tap Delete (web())
APP_NOT_OPEN: unhandled confirm dialog: Delete Invoice 42?; register a handler with browser.onDialog, or answer every dialog with web({ dialogs: 'accept' }) or web({ dialogs: 'dismiss' })
  • testbed, built CLI, a scratch test that taps Save and Delete on that markup and expects the row gone, under web({ dialogs: 'accept' }) with --trace on (the files were deleted afterwards). It passes, and trace.md has both answers under their steps:
## App log

- step 3 · ℹ system: `alert dialog "Could not save: server error 500" opened; accepting it per web({ dialogs: 'accept' })`
- step 4 · ℹ system: `confirm dialog "Delete Invoice 42?" opened; accepting it per web({ dialogs: 'accept' })`
  • main against this branch on the same scratch test, captured at 4b1fb17 before the app-log and agent changes, which change no CLI output of a passing run:
mainthis PR
web(): × delete behind a confirm, APP_NOT_OPEN: unhandled confirm dialog: Delete Invoice 42?web(): same failure, message ends ; register a handler with browser.onDialog, or answer every dialog with web({ dialogs: 'accept' }) or web({ dialogs: 'dismiss' })
web({ dialogs: 'accept' }): INVALID_CONFIG, no tests runweb({ dialogs: 'accept' }): ✓ delete behind a confirm
web({ dialogs: 'ok' }): web({ dialogs }) must be 'accept' or 'dismiss', got another string; a function handler goes to browser.onDialog in the test, exit 2
  • The video of the passing 'accept' run, from 4b1fb17. Nothing on screen changed since. The test takes 0.4 s, so it is short.

https://github.com/user-attachments/assets/41520956-1037-4384-8bc0-042a75e926b2

  • pnpm bench:ab was not run on this head. The agent change needs the web benchmark's agent suite, whose app port (4280) was held by another checkout's server, and the machine's load average was about 11, too high for timings. The suite replays recorded steps, which never render a tool result, so its behavior and counter sections could not show this change either. On 6f0cb67, before the agent change, the web benchmark's two dialog suites (native-dialogs, deferred-dialog) ran the same steps with the same counters on both builds (12 pairs, load 17.4).
  • Tests:
    • packages/e2e/tests/integration/explore.test.ts: e2e explore with a scripted model taps Save (an alert) and Ask (a confirm) on a fixture page under web({ dialogs: 'accept' }). Each tap's result carries its own dialog line, the alert's tap is not called one with no effect, and Ask's screen shows status "Answer" text="accepted". Without the option, the tap's result is Action failed: unhandled confirm dialog: Proceed?; .... With the note turned off in screen-update.ts, the test fails.
    • packages/e2e/tests/integration/web-dialog-policy.test.ts: a real Chromium run per policy. confirm returns true or false, prompt('Name?', 'keep me') returns 'keep me' or null, beforeunload lets the page go under accept and keeps it under dismiss, and the report's app log has one system line per answered dialog and none for a test's onDialog handler.
    • Unit: app-log.test.ts (systemLines: cursor, newest five, apart from a full quota), agent-observation.test.ts (each line once, redacted before the cut, a line from before the step left out), agent-screen-update.test.ts (note placement, every rendering, no "no effect" verdict), transcript-compaction.test.ts (an elided screen keeps its note).
    • pnpm check (lint with .t3-tmp excluded), the e2e unit project (5033 passed), and the agent, MCP, explore, and dialog integration files (33 files) pass.
  • A real model was not run: no model key was available here. The explore and MCP checks use a scripted model or none.

Checklist

  • Changeset added (pnpm changeset) for any change to a published package, or the change is testbed, docs, or CI only.
  • Breaking change: title carries !, the changeset body names what breaks and what replaces it, and the deprecation warning landed a window earlier (stability policy in CONTRIBUTING.md).
  • Docs updated in the same PR: the docs/**/*.mdx page for the behavior, and skills/e2e/ if the skill describes it.
  • Contract change: emitted .d.ts reviewed, tests/types/sdk-types.ts updated; wire change edits the schema and both fixtures. (WebOptions.dialogs and DialogPolicy are pinned in packages/web/tests/types/dialogs.ts; ExecutorObservation.systemLog is a new optional field.)
  • Engine contract change (e2e/engine): changesets for e2e, @e2e-dev/web, and @e2e-dev/mobile. (No contract field changes; AppLogEntry's doc now says a system line reaches the model, and .changeset/agent-system-log.md covers e2e and @e2e-dev/core.)

🤖 Generated with Claude Code