Give each foreach item a browser record of its own

Merged
YO
1mo ago
+212-13

Summary

A foreach whose body runs a browser.run step had some items fail at random points with the browser's DevTools connection closing. One run of 26 items on a frameset search site had 6 failures:

browser: do[7] wait failed: stagehand CDP connection closed: failed to get reader: failed to read frame header: EOF
browser: do[8] act failed: stagehand CDP connection closed: ... EOF
browser: stagehand CDP connection closed: failed to get reader: failed to read frame header: EOF

The failures came right after a search that opens a self-closing "searching" window, which suggested the window (#3442). It is not the cause: the same search flow repeated four times in one browser, each with a held window, passes on main. The failures only happen across foreach items.

Root cause

A browser step records its browser in the host's browser store under RecordID(dagRunID, stepName). When it starts, it closes any browser recorded under that ID, treating it as a browser an earlier attempt left open (startSession → browserhost.Release(stale)). Foreach item bodies run in-process with the parent run's DAG context, so every item has the same DAGRunID and the same body step name, and therefore the same record ID. An item that starts while another item's browser is open and recorded closes that browser (Browser.close). The other item's SDK connection then reads EOF, and its next browser call fails. A duration wait touches no page, so with allowed_domains the failure surfaces in the page check after it (wait failed: …).

The record is also deleted whenever an item finishes. So the kill needs an item to start after another item saved its record and before any item finishes, for example when the body has a further step after the browser step, or when item durations vary, as a model-driven search does.

Fix

  • runtime.Env.ForeachItem identifies the foreach item whose body a step runs in, within the DAG run. It is built from each foreach step's quoted name and item index, nested items included, and body steps inherit it like Foreach.
  • browserhost.RecordID(dagRunID, item, stepName) includes it, so each item has a record of its own. An item's own retry or resume computes the same ID and still finds its record. Steps outside a foreach pass "".
  • Spec 072 states that each execution, and each foreach item, drives a browser of its own, and that only a new attempt of the step closes a browser its earlier attempt left open.

Tests

  • Conformance TestBrowserForeachItemsKeepTheirBrowsers (foreach_items.yaml) is deterministic. Item 1's body first runs wait.http until the test server has served item 0's page, so item 1's browser step always starts while item 0's browser is open and recorded. On main, item 0 fails with do[1] expect failed: stagehand CDP connection closed: failed to get reader: failed to read frame header: EOF. With this change both items succeed.
  • TestForeachItemLeavesOtherItemsBrowser (browser package, fake engine): with another item's running record of the same step present, pointing at a browser that would fail to close, the step in another item runs and leaves that record alone. It fails if the item is left out of the record ID.
  • TestBodyStepsKnowTheirItem (foreach package): six bodies, two items each with an inner foreach of two, get six distinct item identities, the same ones on a second run. It fails if a nested item drops its outer item.

go test -race for the browserhost, browser, foreach, runtime, and frontend API packages; conformance spec072_browser, spec018_parallel_foreach, and spec079_browser_session; and make check all pass.

Reproduction

  • Local copy of the site (searching window, held answer, allowed_domains), production item flow, 8 items, max_concurrent: 4, plus a short step after the browser step: main failed 1 of 8 with do[1] wait failed: stagehand CDP connection closed: … EOF. This branch passed 8 of 8 in each of two runs.
  • Live site, same flow, 4 items, max_concurrent: 2: main failed 1 of 4 with stagehand CDP connection closed: failed to get reader: failed to read frame header: EOF. This branch passed 4 of 4.

Summary by cubic

Fixes random browser.run step failures in foreach items, where an item's browser connection closed mid-run. Items sharing the same DAG run and step name shared one browser record, so a starting item closed the browser another item was still using, failing its next operation with stagehand CDP connection closed.

Each foreach item now gets its own browser record. Body steps inherit a ForeachItem identity from runtime.Env that includes nested foreach names and indexes, and browserhost.RecordID includes it, so an item's retries and resumes still find its record. Steps outside a foreach body pass an empty item. Spec 072 now states each foreach item drives a browser of its own.

Adds a conformance test that deterministically reproduces the failure on main, plus unit tests for distinct item identities in nested foreach bodies and for leaving another item's running record untouched.

Summary by CodeRabbit

  • New Features
    • Browser steps now use separate browser sessions for each foreach iteration, including nested iterations. Concurrent items can continue using their browsers independently.
  • Bug Fixes
    • Starting a new iteration no longer interrupts another iteration’s active browser. A new attempt closes any browser left open by its previous attempt.
  • Documentation
    • Clarified how browser sessions behave across foreach iterations and step attempts.