Repository navigation
fix(agent): bound snapshot construction by visited nodes, not wall time - #45
Merged
Merged
Conversation
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>
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Construction in
agent/src/snapshot.jsno longer stops on a clock. The walk counts the nodes itvisits 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 = 250and itsperformance.now()deadline are gone;MAX_NODES = 200000replaces them, checked before each pop.
MAX_ELEMENTS = 50000already bounded the expensive part andis unchanged. The legacy walk (
agent/src/snapshot_legacy.js, the CLI default) has no clock and neverhad one — now frozen by a source gate.
Contract. Same
rendererIncompletemarker, same notice sentence, same ref assignment order, sameelement-limit reason. A page that did not truncate answers byte-identically to
4ae4e8e. The onevisible 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):
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.htmlis that page; it now always cuts at 200,000 nodes (18,184 elements), where the basecut 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):4ae4e8eThe base also flips inside one session on
text-huge.html: run 0 answered(wall time)at 18,644elements (361 ms), runs 1–2 completed construction (272–273 ms).
Cost
browser_snapshotthrough 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.htmlcosts 428–528 ms instead of 300–344 ms. Worst case isbounded 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 perfixture (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 idleagain. 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.jspins that neither walk containsperformance.now,Date.nowor
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
#44puts in CI).Mutants (applied, run, reverted;
agent/src/snapshot.jssha256663fbd92…before and after):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.the node valve did not fire. Browser gate: text-huge completed instead of cutting.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 forline (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.
nameOfcan walk a wholearia-labelledby/label subtree per element (pre-existing, quadratic in the worst case), so the worstcase is
MAX_NODES× subtree size, notMAX_NODES× a fixed cost. The label index build and thedistillation/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.