Skip to content

fix(agent): bound snapshot construction by visited nodes, not wall time - #45

Merged
capy-ai[bot] merged 1 commit into
mainfrom
capy/bound-snapshot-construction-by
Dec 3, 2026
Merged

capy-ai[bot] merged 1 commit into
mainfrom
capy/bound-snapshot-construction-by

Conversation

@jaredLunde

Copy link
Copy Markdown
Contributor

What

Construction in agent/src/snapshot.js no longer stops on a clock. The walk counts the nodes it
visits and stops at a fixed count, so the same DOM delivers the same bytes, cut point and ref numbers
on an idle box, on a box with every core busy, and on a box where each visited element costs ten
microseconds more. It is the last source of nondeterminism in an agent-visible answer: #35 made
truncation rare (9/24 → 0/24 on the 21.9k-element fixture) but every heavy capture still needed two
clean runs to pin, because the 250 ms deadline and the 50,000-element cap expire within ~5 ms of each
other on that page.

MAX_CONSTRUCTION_MS = 250 and its performance.now() deadline are gone; MAX_NODES = 200000
replaces them, checked before each pop. MAX_ELEMENTS = 50000 already bounded the expensive part and
is unchanged. The legacy walk (agent/src/snapshot_legacy.js, the CLI default) has no clock and never
had one — now frozen by a source gate.

Contract. Same rendererIncomplete marker, same notice sentence, same ref assignment order, same
element-limit reason. A page that did not truncate answers byte-identically to 4ae4e8e. The one
visible change: a page that truncates mid-construction now reports (node budget) where it reported
(wall time), at a fixed node instead of a moving one.

Why 200,000 nodes

Measured by instrumenting the shipped walk to count what it visits (uncapped, this 4-vCPU box,
Chromium 158):

fixture DOM elements visited elements visited nodes uncapped walk
tiny.html 13 5 11 0.1 ms
synthetic-labels.html 38 38 86 0.5 ms
forms.html — 732 1,013 3.9 ms
small.html 3,105 3,102 5,602 13 ms
heavy.html 21,916 18,911 41,016 92–97 ms
huge.html (new: 60,003 elements built at load) 60,008 60,003 120,004 205–255 ms
text-huge.html (new: 200,000 text nodes) 20,007 20,003 220,003 158–228 ms

The element cap bounds the expensive visit (a style resolve plus a layout box, ~5 µs on the
21.9k-element page), and real pages run 1.4–2.2 nodes per element, so 50,000 elements is at most
~110,000 nodes. 200,000 is therefore four visits per budgeted element, 4.9× the largest fixture, and
at least 1.8× what the element cap permits at any realistic density — it cannot change the answer on
a realistic page and only binds text-heavy trees. The band that moves: 200,000–250,000 cheap text
nodes, which the old valve admitted on an idle box (~0.3–0.6 µs per text node) and cut on a busy one.
text-huge.html is that page; it now always cuts at 200,000 nodes (18,184 elements), where the base
cut at 18,644 elements on one run and completed construction on the next two.

The defect, reproduced

worst.html (50,000 elements × 3 text children):

binary run 1 run 2 run 3
4ae4e8e cut at 25,570 elements (wall time) 37,293 (wall time) 33,549 (wall time)
this PR 50,000 (element limit), 428 ms identical sha, 458 ms identical sha, 528 ms

The base also flips inside one session on text-huge.html: run 0 answered (wall time) at 18,644
elements (361 ms), runs 1–2 completed construction (272–273 ms).

Cost

browser_snapshot through the actor, min of three runs: heavy 111.6 → 100.6 ms, huge 247.8 → 222.6 ms,
small 30.0 → 17.0 ms, forms 8.9 → 5.7 ms, tiny/synthetic unchanged (noise), text-huge 272.4 → 234.7 ms.

What got slower: a page that pushes both caps. The base stopped at its 250 ms valve with a partial
tree; this PR walks to the caps, so worst.html costs 428–528 ms instead of 300–344 ms. Worst case is
bounded at ≤ 50,000 element visits and ≤ 200,000 node visits on any machine. No new blocking, no
unbounded buffer, malformed input still errors.

Proof

Load independence — new browser gate
agent/tests/snapshot_labels.rs::snapshot_answer_does_not_depend_on_machine_load. Six captures per
fixture (heavy, huge, text-huge): two idle, one with an injected 10 µs stall per
getBoundingClientRect, one with three spinner threads loading every core, one load + stall, one idle
again. It asserts one digest over the whole page payload (outline, units, nextRef, rendererIncomplete),
that the fixture actually loaded (element count), and that the stall really slowed the walk (≥1.5×), so
it cannot pass vacuously. 6/6 identical per fixture: heavy 61,833 B / nextRef 8,006 (109 ms idle →
1,946 ms stalled); huge 50,338 B cutting at the element limit; text-huge 283,946 B cutting at the node
budget.

Gates that fail when a clock comes back —
mcp/tests/fixtures/snapshot_w2_test.js pins that neither walk contains performance.now, Date.now
or new Date(, and pins the node valve's cut point and notice (this runs in CI today);
the browser gate above is the behavioural one (in the agent package, which #44 puts in CI).

Mutants (applied, run, reverted; agent/src/snapshot.js sha256 663fbd92… before and after):

mutant red at
M1 restore the 250 ms deadline Node fixture: the snapshot walk reads a clock. Browser gate: idle completed (61,833 B, nextRef 8,006) vs stalled cut at 2,326 elements (57,258 B, nextRef 1,163). Other five agent gates green.
M2 delete the node valve Node fixture: the node valve did not fire. Browser gate: text-huge completed instead of cutting.
M4 shrink the cap to 40,000 Browser gates: determinism, byte-identity-to-baseline and large-page-completes red (heavy newly truncates). Node fixture: loud anchor miss.

M4 first came back green because the Node fixture's limit injection silently failed when its needle
moved — the CAPY-5 lesson. It now asserts each anchor is present exactly once.

Byte differential against 4ae4e8e — two independent MCP binaries (base worktree vs this head),
same fixtures and call sequences: capture.py × 4 budgets (default, 65536×200, 32768×120, 16384×80)
= 408 answers and capture_refs.py (live refs, each used by the next call, 5 fixtures) = 40 answers;
448 answers, 0 differing sha256. Per-page first snapshot identical on tiny/small/forms/
synthetic-labels/heavy/huge; text-huge differs by design as above.

Suites: core 414 passed; CLI 11 + 1 (3 ignored, pre-existing); MCP 65 + 1 + 33 + 1 = 100 passed;
agent 117 passed + 6 gates passed at --test-threads=1. Clippy: output identical to base line for
line (core 270 / cli 197 / mcp 207 / agent 268 warning blocks, pre-existing on main). rustfmt:
the touched test file has 11 pre-existing hunks before and after and the new code adds none.

Unproven

Per-visit cost is not uniform: the cap bounds visits, not time. nameOf can walk a whole
aria-labelledby/label subtree per element (pre-existing, quadratic in the worst case), so the worst
case is MAX_NODES × subtree size, not MAX_NODES × a fixed cost. The label index build and the
distillation/render passes are outside the walk's budget (deterministic, O(nodes) or O(labels),
pre-existing). Worst-case latency is measured on this 4-vCPU box only. No corpus of real sites between
heavy's 41,016 nodes and the cap was measured; the margin is reasoned, not observed.

Open in Capy

The walk stopped when a 250 ms wall-clock budget expired and returned the
partial tree, so two runs of the same page delivered different bytes under
different machine load: the cut point moved and every ref after it named a
different element. Construction now counts the nodes it visits and stops at a
fixed count.

MAX_ELEMENTS (50,000) is unchanged; MAX_NODES (200,000) is new and covers
text-node visits, which nothing else bounded. The marker, notice sentence,
element-limit reason and ref assignment order are unchanged; a page that
truncates mid-construction reports "(node budget)" instead of "(wall time)".

Measured: heavy.html (21,916 elements) visits 41,016 nodes and completes at 21%
of the budget; a 60,003-element fixture is 120,004; a 200,000-text-node fixture
is 220,003 and now cuts at a fixed node. A page that maxes both caps answered
three different ways in three consecutive runs on the old binary and one way
now.

New gates: a browser gate comparing six captures of each fixture (idle, three
spinner threads, 10 us per-element stall) by digest over the whole payload, and
a source gate in the Node fixture that fails if either walk reads a clock.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@capy-ai
capy-ai Bot merged commit d18abf7 into main Dec 3, 2026
3 checks passed
@jaredLunde jaredLunde mentioned this pull request Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant