fix(run): time out a test whose step fails at its deadline (ENG-1547)

Merged
OK
2d ago
+332-15

What

A test whose timeout arrives while a step waits on the app, a waitFor for example, is reported as timed-out with the step cancelled. It used to depend on which of two timers fired first, and the other order reported the test as failed with ACTION_FAILED: operation timed out.

The same holds for a beforeAll hook, an afterEach hook or fixture teardown, and a test an MCP session runs (open_session {test}): each reports its own timeout.

Why

tests/integration/test-step.test.ts "cuts the call running in a step as the timeout cuts any call" has been flaky since the day it landed in #2312 (2026-11-26). It is behind 28 of the 33 red spec push runs on main since then, out of 119. No commit regressed the runner: the test is the first to assert this outcome, and the race is older. A build of the commit before #2312 reports ACTION_FAILED for 2 of 33 tests in the sweep under Verified.

The race: an engine call is handed what is left of the phase as its timeoutMs, because that is how an engine that cannot be cancelled, a pending Playwright action for one, stops at the deadline. So a call still running at the deadline ends on its own timer at the same instant as the runner's timeout. When the call's timer won, the step failed on its own and the test never timed out.

A step that fails as the deadline arrives now hands the verdict to the timeout race.

How it works

  1. AttemptTimeout takes the step recorder of the work it races (packages/core/src/run/budget.ts:170). The argument is required, undefined where the work records no steps, so a new race cannot leave it out by accident.
  2. While a race runs it registers a PhaseTimeout on the recorder with timedBy, and removes it when the race ends (budget.ts:272, packages/core/src/run/steps.ts:631). One recorder serves a session and the test it runs on two budgets, so the binding follows the race, not the budget.
  3. timeOutIfDue times the race out at once when the work has not settled it, the caller is not work a phase left running, the clock runs, and at most 1 ms is left (budget.ts:261). The 1 ms is how early a timer may fire by Date.now(), since it counts whole milliseconds on another clock (budget.ts:18).
  4. A step that fails with anything but a cancellation calls it. When the deadline was due, the step is recorded and thrown as CANCELLED over its real failure, the way a step still running at the timeout is (steps.ts:972). cancelledError takes the cause for this (packages/core/src/internal/time.ts:78).
  5. The races are the test body (packages/e2e/src/run/execute.ts:732), suite hooks (packages/e2e/src/run/suite-attempt.ts:150), and a hosted test (packages/e2e/src/run/standalone-test.ts:284). afterEach hooks and fixture teardowns ran on a plain timer with the same race, and now run on an AttemptTimeout over their cleanup budget (execute.ts:994, standalone-test.ts:499).
  6. The race keeps two flags, timed out and settled by its work, so neither timeOutIfDue nor test.setTimeout can time out a race its work already settled.

Not changed: an engine that gives up ahead of its budget fails the step before the deadline, and that failure stays the verdict. The web engine does this for an action Playwright can explain, up to 250 ms early (PLAYWRIGHT_TIMEOUT_LEAD_MS), so a click on a node that never becomes actionable still reports Playwright's reason, not a timeout. That outcome does not flake.

Dropped alternative: stop handing engine calls the phase's remainder, and let the timeout's abort end them. A Playwright action cannot be aborted, so it would land after the test was cut.

Also not touched: expect.poll and toPass keep their own 50 ms grace for the same race (packages/e2e/src/expect/retry.ts:15).

Issue

ENG-1547

Verified

Ran it locally: yes

  • packages/e2e, vitest run tests/integration/step-at-deadline.test.ts, 8 runs on a build of main (c0ba2b45d) and 8 here. The file drives a test body, a beforeAll hook, an afterEach hook and a hosted test with a fake engine whose locate spends its whole budget and then reports OPERATION_TIMEOUT. The two timers are real, so main fails on most runs, not all.
mainthis PR
runs with a failure5 of 80 of 8
what failedtest body 4 times, beforeAll once, afterEach oncenothing
  • tests/unit/phase-timeout.test.ts holds the exact case on a clock that stands still. Its first test fails when the race is not given the recorder.
  • testbed, built CLI, a scratch file of 33 tests that each waitFor a missing button under a test timeout of 40 to 160 ms, so a locate is often in flight at the deadline.
buildTEST_TIMEOUTACTION_FAILED: operation timed out
the commit before #2312312
main (c0ba2b45d)321
this PR, 3 runs990
  • The flake at its real 1000 ms timeout did not reproduce on this Mac in 48 concurrent runs of main, so whether spec stops going red shows on CI.
  • No terminal recording: only the status and error of this case change.

pnpm bench:ab --suite testbed -- tests/forms.e2e.ts:

bench-ab: ad564a88 (merge base with origin/main) vs fe4c7dea

apps/testbed: e2e run --workers 1 tests/forms.e2e.ts. 6 pairs in random order, resolved after 6 pairs. Threshold ±5% and 10 ms. darwin arm64, Apple M5 Pro x15, Node 24.22.2, load 2.5.

Behavior

Every test ran the same steps with the same outcome on every run of both builds.

Timings

Metricbase median (IQR)head median (IQR)change (95% CI)verdict
wall (process)1.50 s (1.48 s-1.53 s)1.45 s (1.45 s-1.48 s)-2.5% (-5.4% to +0.6%)unresolved
suite (sum of attempts)774 ms (774 ms-776 ms)760 ms (758 ms-760 ms)-2.2% (-4.1% to +1.2%)same
action (events)329 ms (328 ms-330 ms)310 ms (309 ms-311 ms)-5.9% (-8.0% to +1.5%)unresolved
step app.open183 ms (183 ms-184 ms)183 ms (183 ms-184 ms)0.0% (-1.1% to +1.1%)same
step locator.fill147 ms (146 ms-147 ms)147 ms (146 ms-148 ms)+0.3% (-0.3% to +2.7%)same, under the floor
step locator.tap79 ms (78 ms-79 ms)61 ms (60 ms-62 ms)-22.9% (-25.3% to -4.0%)unresolved
step locator.pressSequentially41 ms (40 ms-42 ms)41 ms (40 ms-42 ms)0.0% (-2.4% to +2.5%)same
step locator.check33 ms (33 ms-33 ms)33 ms (32 ms-33 ms)0.0% (-16.0% to +19.1%)same
step locator.uncheck33 ms (32 ms-33 ms)33 ms (32 ms-33 ms)0.0% (-3.0% to +3.1%)same
step expect.toBeVisible17 ms (16 ms-17 ms)16 ms (16 ms-17 ms)-3.0% (-6.1% to +3.1%)same, under the floor
step locator.selectOption16 ms (15 ms-17 ms)16 ms (15 ms-16 ms)0.0% (-6.1% to 0.0%)same
step locator.clear16 ms (15 ms-16 ms)15 ms (15 ms-16 ms)-3.2% (-9.0% to +3.3%)same, under the floor
step expect.toHaveValue6 ms (6 ms-7 ms)7 ms (6 ms-7 ms)+8.0% (-13.4% to +36.6%)same, under the floor
step expect.toHaveCount5 ms (5 ms-5 ms)5 ms (5 ms-5 ms)0.0% (-8.7% to 0.0%)same
step expect.toHaveText3 ms (2 ms-3 ms)3 ms (3 ms-3 ms)+15.5% (-13.4% to +50.0%)same, under the floor
step expect.toBeChecked2 ms (2 ms-3 ms)2 ms (1 ms-2 ms)-18.4% (-59.2% to 0.0%)same, under the floor
step expect.not.toBeChecked1 ms (1 ms-1 ms)2 ms (2 ms-2 ms)+100.0% (+41.4% to +144.9%)same, under the floor

Counters

Every counter matched on every run of both builds.

Tests outside the threshold: 0 of 640

With 640 tests compared at 95%, about 32.0 could land outside by chance.

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.
  • Engine contract change (e2e/engine): changesets for e2e, @e2e-dev/web, and @e2e-dev/mobile.

Summary by CodeRabbit

  • Bug Fixes
    • Steps that are still waiting when a test deadline is reached now resolve as timed out, rather than continuing to wait or appearing as an action failure.
    • Deadline handling now applies consistently to test bodies, beforeAll and afterEach hooks, and fixture teardown.
    • Errors cancelled by a deadline can retain their underlying cause, while cancellation errors without a cause keep their existing details.