Skip to content

Commit b70a584

Browse files
authored
perf(web): measure snapshot geometry in batched backend calls, decided per entry (#3533)
* perf(web): fetch snapshot geometry in one batch instead of one spawn per ref `is visible`/`is hidden` and every interaction that aims by ref ask the web backend for the bounds of every ref of a capture. The provider asked per ref, and each `agent-browser get box` is a fresh Node process: measured on a 408-ref page, 381 spawns for one check. The budget they spent on interpreter startup is why `is visible` on such a page died at the request envelope (`ready timed out after 4500ms`) - the command answers in 90s against a 45s deadline. The `--timeout` that would have raised the deadline is not even accepted by `is`/`wait`, so on a big page those commands could not be made to work at all. `batch` is the backend's own answer to that: it reads a JSON array of commands from stdin and runs them inside ONE process, so the whole geometry pass is a bounded number of spawns - 200 refs measure in 0.28s, all 408 of the bench page in 0.33s, against a 30s per-command budget. Getting `stdin` to the spawn is the whole plumbing cost - `ExecOptions` already supported it, so `runManagedAgentBrowser` forwards it and `runAgentBrowserCommand` passes it through. Grouping lives with the grouping decision: the projection asks for the bounds of every ref it published and the provider decides how many refs to send per call, because the refusal below is a claim about the whole capture and only one owner sees all of it. A ref the batch could not measure is an ordinary answer about that ref, and the entry's own `success` decides that - never the error's wording. `isIgnorableBoxError` is gone with it: it was a table of phrases the backend is free to reword, and a reworded one turned "this ref has no box" into "geometry is broken", failing an interaction over a ref `is visible` never aimed at. What the old code called a fatal error now has to be a fault of the batch as a whole, which is what it actually reports: a whole-batch fault, or an answer that cannot be paired back to the refs, is refused typed as `unusable batch result` with the reason named - the entry count, an entry that carries no verdict (an object with neither `success` nor the command it answers would read as "this element has no box" and drop a node silently), or an entry whose echoed command is not the command that was sent. Position is trusted only as the order of the echoed command, never as the pairing itself, because a mispaired box reads as a working capture with wrong geometry. And a capture whose refs ALL went unmeasurable is refused rather than published: a hidden element still measures as a box, so that shape means the backend answered to refs this snapshot does not publish, and the tree would carry no addressable target with usable bounds. It is decided over the capture rather than per group - a page whose first group went stale still answers for the rest. The refusal reports `reason: snapshot-refs-unmeasurable` with `requestedRefs` and `measuredRects`. Tests: the geometry seam is its own file, `agent-browser-provider-rects.test.ts`, mirroring the seam the provider source grew (the provider file was past the 1,000-line test tripwire and the ratchet named this split as its fix); the provider file keeps the capture half. The old per-ref tests became the batch contract: one call for the capture, groups bounded per fetch, a group that answered nothing while a later one measured, entries refused by echoed command or missing verdict, and each fault shape above. Measured live (managed agent-browser 0.27.1, same page, base/head interleaved in one loop iteration because this host sat at load 125-223, n=5 medians): | page (refs) | command | this branch | before | | --- | --- | --- | --- | | index.html (408) | `is visible` | **5.9s** | **90.1s, and 3 of 5 runs refused outright** | | index.html (408) | `fill` | **10.1s** | **90.2s** | | light.html (8) | `is visible` | **2.5s** | 3.9s | | light.html (8) | `fill` | 9.7s | 9.0s (noise; light pages were already fine) | Backend spawns for one capture + geometry pass: **4 instead of 385** (408-ref page), 4 instead of 11 (8-ref page). Verdicts are unchanged: on a page with an on-screen button and one at `top: -9000px`, both builds answer identically. Closes #3516 * fix(web): key the batch refusals by what broke, and pin each one Second round on #3533. The refusals all shared one message prefix, which meant no test could tell them apart and an operator could not branch on them: each now carries a `reason` identifier (`batch-result-not-a-list`, `entry-count`, `entry-shape`, `entry-command`) beside the sentence, and the whole-batch fault is no longer reported as an `entry count` on an answer that was never a list - naming nothing counted. `requestedRefs` is the capture's ref count rather than the current group's, with `groupRefs` beside it, because the answer reads as a statement about the capture and one capture is measured in several groups. The empty-capture decision was encoded twice - an early return in the projection and `refs.length > 0` in the provider - with nothing pinning either. The provider keeps it (the loop over groups already does nothing) and a test states the consequence the projection depends on: a capture of only ref-less text sends no batch, and so cannot trip the all-unmeasurable refusal. Tests, which is where most of this lives: - The shape guard had no test that exercised it. Its fixture's bad member was `{}`, which is an object and therefore fell through to the echo check - deleting the guard kept all tests green. The member is now a non-object and the test names `entry-shape`. Mutation-checked: removing the shape guard or the list guard fails exactly one test each. - Every refusal test now asserts its own `reason` instead of the shared prefix. - The batch is the call that carries up to 200 commands and it had no cancellation assertion - a dropped signal there costs a full 30 s after the caller gave up. Pinned against the recorded `options.signal`; mutation-checked against the signal threading in `snapshot`. - `MAX_REFS_PER_RECT_BATCH`'s comment justified 200 by the stdin payload, which is not the constraint (23 bytes per ref). It is now bounded by the measurement that does decide it: a group has to finish inside `AGENT_BROWSER_TIMEOUT_MS`, and 200 refs measured in 0.28 s against 30 s. The spawn count in the same comment said 3 where the capture is 4. * test(web): pin the entry-shape refusal on an entry that states no verdict The only entry-shape case sent a null member, which plain isJsonObject already rejected, so isBatchEntry's boolean-success clause had no test that needed it: revert the clause and an entry echoing the right command with a box but no verdict passes both the shape check and the echo check, then drops that node's rect in silence - the read being 'this element has no box'. The new member echoes commands[0] on purpose, because that is the one shape only the success clause refuses. Mutation-checked: isBatchEntry back to isJsonObject fails this case alone and leaves the other 13 green. The old comment claimed such an object would be refused further down by the echo check, which is what the new case disproves.
1 parent dcf650b commit b70a584

6 files changed

Lines changed: 744 additions & 117 deletions

‎packages/platform-web/src/__tests__/agent-browser-snapshot-refless.test.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,14 @@ describe('normalizeAgentBrowserSnapshot ref-less content', () => {
282282
});
283283

284284
test('attaches geometry only to the nodes the backend can measure', async () => {
285-
const result = await normalizeAgentBrowserSnapshot(FORM_AFTER_SUBMIT, async (ref) =>
286-
ref === 'e2' ? { x: 1, y: 2, width: 10, height: 20 } : undefined,
285+
const result = await normalizeAgentBrowserSnapshot(
286+
FORM_AFTER_SUBMIT,
287+
async (refs) =>
288+
new Map(
289+
refs.flatMap((ref) =>
290+
ref === 'e2' ? [[ref, { x: 1, y: 2, width: 10, height: 20 }] as const] : [],
291+
),
292+
),
287293
);
288294

289295
expect(result.nodes.find((node) => node.ref === 'e2')?.rect).toEqual({

0 commit comments

Comments
 (0)