Skip to content

Truly independent web browser

Documentation: Describe the style engine that runs on the render side

Andreas Kling9e6c9d7b9e00
diff --git a/Documentation/Style/StyleEngine.md b/Documentation/Style/StyleEngine.md
index 0cbe1463ba6..54faf139176 100644
--- a/Documentation/Style/StyleEngine.md
+++ b/Documentation/Style/StyleEngine.md
@@ -1,1401 +1,84 @@
-# StyleEngine: differential style resolution
+# The style engine
-This document describes LibWeb's style engine: its data structures, algorithms, protocols, and memory rules, as implemented. Normative statements ("never", "must") are invariants the implementation upholds and that changes must preserve; they are review tests, not aspirations. Testing and debugging workflows live in [StyleEngineTesting.md](StyleEngineTesting.md).
+LibWeb's style engine lives in `Libraries/LibWeb/Rust/src/css/style/`. It works like a conventional browser style engine:
+it matches an element against an index of rules, cascades what matched, and computes values into groups that elements
+share. What sets it apart is where it runs. A document's style state belongs to the render side, on the Render
+thread, and the main thread hands it inputs and takes back results. That lets the render clock sample animations and hover
+while script runs, without ever reading the DOM.
-`StyleEngine` is LibWeb's implementation of CSS selector evaluation, style invalidation, cascade, and computed-value computation. Mutations reach it as typed input deltas (§4.3), and compiled delta routing (§6.3) carries each delta to the transpose entry points that mention that input: every selector program that can change under the delta is reached, and subjects are match-checked before any truth delta is emitted. Selector answers, cascade winners, and computed styles are shared, interned values: elements hold a compact `StyleRecordID` into interned computed-value groups (§9.8), and every consumer of style (layout, paint, hit testing, accessibility, script APIs, and the rest of the inventory in §12) reads through that shared handle.
+[StyleEngineTesting.md](StyleEngineTesting.md) covers how to test and debug it.
-Correctness rests on the engine's internal fallback chain: incremental evaluation, then exact evaluation of the affected scope, widening to the whole document when required (§3). Every path reads the same authoritative live DOM, and the exact cold evaluator is the reference implementation the others are checked against; the machinery that continuously enforces this is described in [StyleEngineTesting.md](StyleEngineTesting.md).
+## Inputs
-One organizing frame runs through the whole document and is stated in §1.1: StyleEngine is a database, specifically an incremental view maintenance engine over a change stream. Mechanisms are described together with their database-world counterparts, in notes marked *Database counterpart*.
+**The projection** (`projection.rs`) is the engine's copy of what style reads of the DOM: one row per element with its tag,
+namespace and id atoms, classes, states, the attribute values selectors read, its inline and presentational declaration
+blocks, and its links to its parent and siblings. The main thread writes it at mutation time (`CSS/StyleEngineInput.cpp`,
+`projection_writes.rs`). Rows live in copy-on-write columns, so a generation of them is immutable plain data during a pass,
+and a fork of the engine shares every chunk it does not write.
-## 1. What the engine does
+Input the engine cannot use is not sent. An attribute no selector or `attr()` reads is withheld, and the elements carrying it
+are noted and published if something comes to read the name. `:local-link` works the same way.
-Conventional style engines do two things per change: conservatively determine which elements *may* be affected, then re-run selector matching, cascade, and computed-value construction for all of them. Work is proportional to the *possible* effects of a change rather than its *actual* semantic effects.
+**The rule store** (`rule_store.rs`) is the semantic program of the stylesheets, separate from the CSSOM objects, which
+keep API identity and serialization. A parsed sheet is shared between the documents that link it, and a document keeps only
+what it must number for itself.
-StyleEngine models styling as an incremental computation. DOM structure, element state, stylesheets, CSSOM mutations, cascade topology, and environment values are versioned inputs. Selectors, cascade decisions, and computed values form a shared logical dependency program. A style flush discovers and propagates exact changes through that program and stops as soon as a semantic value is unchanged.
+## A style pass
-The pipeline:
+**The rule index** (`rules.rs`) buckets the entries of each tree scope by the most selective key the subject compound requires,
+each with the keys its ancestors must carry. A change indexes only the rules it names. Shadow trees with the same sheets and
+layers share one group of entries.
-```text
-Style input transaction
- -> tree, state, stylesheet, cascade, and environment deltas
- -> exact selector-truth deltas
- -> exact matched-rule deltas
- -> exact cascade-winner deltas
- -> exact computed-property deltas
- -> layout, paint, animation, and observable-style changes
-```
+**The traversal** (`traversal.rs`) styles every element once its parent is styled, breadth-first from the root, on a pool of
+threads once a level is wide enough (`LIBWEB_STYLE_THREADS` sets the number). Each thread keeps its own matcher, cascade
+scratch, ancestor Bloom filter and caches.
-Each arrow is a stopping point. If a stage's output identity is unchanged, the next stage receives nothing.
+For each element it:
-The cost of a transaction is:
+1. Tries to keep the previous record or take one from the sharing cache (`sharing.rs`), without matching.
+2. Matches (`matching.rs`): gathers the bucket candidates, rejects through the Bloom filter, and evaluates the rest.
+3. Cascades (`cascade.rs`): orders declarations by priority, and the first of each longhand wins.
+4. Computes (`compute.rs`, `drive.rs`): starts from the parent's inherited groups and the default reset groups as pointer copies,
+ and writes only the groups its winners touch. Most values were precomputed when the rule was published, and matched lists
+ that recur reuse their groups through the group cache.
-```text
-work = normalize + discover + propagate + reconstruct + observe
-```
+**Records** (`records.rs`) hold the result: the groups of computed values, the facts a reader needs without opening them, and
+what the computation read, which later changes are tested against. A record's slot and generation identify it to the main thread.
-`discover` can dominate even when no semantic output changes, so it is accounted separately and the engine switches to an exact batch plan when a supposedly selective plan does too much discovery.
+## Restyling
-**The ordinary path is the primary kernel.** Tag, ID, class, attribute, state, compound, and conventional combinator evaluation must be the fastest and smallest path through the engine. `:has()` is a forcing function for the architecture, not the center of the workload: it extends the same selector primitives with reverse-direction existential maintenance, and it must impose no cost on documents that do not use it (zero relational allocations, zero relational fields).
+After a document's first pass, each style update restyles only what its inputs can reach (`live.rs`):
-### 1.1 The engine is a database
+**Invalidation** (`invalidation.rs`) maps every fact a compound tests to where that compound stands in its selector. A changed
+element restyles itself where the fact is the subject's, and otherwise follows the combinators to the elements that can match.
+`:has()` compounds find their anchors through their own queries. Reads that carry no structure, such as the selectors of an
+`of` or the bounds of a `@scope`, restyle coarsely.
-StyleEngine is a database engine: an incremental view maintenance engine over a change stream. The queries are standing rather than ad-hoc (attached selector programs change only when the program itself is edited), inputs arrive as typed deltas, and the engine's entire job is to keep a set of partially materialized views consistent with the base data for less than the cost of recomputing them. That places it in the family of streaming materialization systems (differential dataflow, DBSP, and the partially stateful Noria line), not general-purpose OLTP stores, and that family's known results, failure modes, and remedies apply here directly.
+A restyled element's children restyle only where what they inherit changed. Custom properties move without a restyle
+where a child substitutes none that moved (`custom_property_moves.rs`).
-The canonical mapping, used throughout:
+Whole-document restyles are not a tool here. A change that reaches everything is a bug to find.
-| StyleEngine mechanism | Database counterpart |
-| --- | --- |
-| Authoritative DOM, CSSOM, browser state | Base tables, the single source of truth |
-| Attached selector and cascade programs | Standing (continuous) queries |
-| Ephemeral selector programs (§6.10) | Ad-hoc queries |
-| Normalization journal (§4.4) | Changelog (CDC stream) with key-level compaction |
-| Style read epoch (§4.2) | MVCC snapshot read view, without copying |
-| Style transaction (§4.4, §11) | Transaction: atomic commit, isolated from script |
-| Match program (§6.3) | Query plan: predicate evaluation per candidate row |
-| Transpose program (§6.3) | Delta rule: the standing query's derivative |
-| Delta-routing registry (§6.3) | Trigger dispatch index |
-| Impact region (§6.4) | Delta key range; coverage is fail-closed (gaps widen) |
-| Feature postings (§6.6) | Secondary (inverted) indexes, non-clustered |
-| Rule matching (§6.8) | A relational join |
-| Selective-plan cutoff (§6.7) | Cost-based optimizer: index scan versus full scan |
-| Incremental discipline (§3) | Incremental view maintenance (fast refresh) |
-| Exact batch plan (§3) | Complete refresh of the affected region; the upquery |
-| Witnesses, winner groups, match answers | Partially materialized views |
-| Cold-batch protocol (§13) | Storage scan API with projection and predicate pushdown |
-| Cost model and counters (§15, §16) | Optimizer cost model and its statistics |
+**Style on demand** (`demand.rs`) answers a read no pass keeps current, such as the style below a `display: none` element. It is
+computed read-only into an overlay, kept until a record it depends on could move.
-Three consequences of the framing carry weight throughout:
+## Installation
-1. **Maintain-versus-refresh is a per-delta optimizer decision, not an architectural loyalty.** No mature system promises that incremental maintenance always wins; it prices the delta against the rescan and chooses. §6.7 is that decision for selector plans, and the same decision governs every retained view, fed by §16's counters. When applying a delta costs more than enumerating the affected scope, rescanning is the optimizer's correct output, not a failure of the design.
-2. **Every view is partial.** Any materialization may be missing, and a miss is answered by an upquery to base facts through the exact cold evaluator, never by a wrong answer. That is why discarding a view is always legal (invariant 5) and why memory never scales with the product of DOM and selector count (invariant 3).
-3. **Propagation stops at unchanged output.** Every stage boundary in the §1 pipeline is a distinctness check: a delta whose output identity is unchanged is consolidated away and the next stage receives nothing. That is streaming consolidation, and it is invariant 1.
+`install.rs` turns the records that moved into rows, and the render side installs them into the layout arena's style columns.
+The main thread takes the same rows at the end of a style update (`CSS/UpdateStyle.cpp`) and does what depends on the DOM:
+starting transitions and animations, applying the effects `main_effects.rs` decided, and invalidating layout.
-Where the analogy stops: there is no durability and no recovery, which is why the journal is a changelog and not a write-ahead log (§4.4); the data model is an ordered tree whose axes (ancestry, sibling order, the flat tree) are join dimensions no relational engine indexes natively; and the query set is fixed at parse time, so plan compilation is a program-change event, not a per-query cost. Importing machinery for problems the engine does not have (locking, recovery logging, general ad-hoc planning) is a misreading of the framing.
+An update can fly: it runs beside the event loop, and the next update takes in its results once it has landed. Script reading
+style or geometry in the meantime waits for it.
-Where a mechanism's counterpart has a known failure mode or remedy in the database literature, a short *Database counterpart* note names it; consult that literature before inventing a new remedy.
+## The render clock
-## 2. Invariants
+Animations are sampled on the Render thread, composing overlays over records (`engine_sample.rs`). A pointer move hovers
+there too (`hover_lane.rs`, `render_state/clock/hover.rs`): it writes the hover facts and restyles what they reach while
+the main thread runs a task. A frame where no element can match a compound testing `:hover` goes read-only and needs only a hit test.
-These hold for every code path. They are not optional optimizations.
+## Memory and instrumentation
-1. If a semantic value does not change, no downstream semantic work is emitted.
-2. DOM mutations, state changes, CSSOM changes, and environment changes are equally first-class inputs.
-3. Core persistent memory is proportional to DOM size, stylesheet program size, and live observable style state, never to the product of DOM size and selector count.
-4. Selector matches, reverse dependencies, witnesses, proofs, and indexes are optional materialized views unless explicitly classified as core state (§10).
-5. Every optional materialization can be discarded at any time without affecting correctness.
-6. High fanout uses exact pull evaluation rather than millions of explicit dependency edges.
-7. Disconnected content holds no per-selector state. Derived state for connected content lives until the content retires or an input invalidates it, whether or not the content is currently observed.
-8. Interning, compaction, and physical relabeling never create semantic style changes.
-9. The exact cold evaluator is part of the architecture. Optional incremental state may accelerate it but is never required for correctness.
-10. Every selector dependency has a compiled delta-routing entry point. Routing semantic inputs to selector programs is required program state, not an optional view.
-11. Every scoped evaluation carries an impact-region coverage obligation (§6.4). Unknown scope is never treated as empty scope.
-12. Style reads use the authoritative live DOM under a mutation-free read epoch. A complete copied DOM is never allocated.
-13. Hot transpose and impact-region traversal has bounded evaluator-local access to every required tree relation, with no boundary crossing per relation step.
-14. Every broad or degraded path is named, counted, and reachable in tests. Nothing silently degrades to full-document invalidation.
-
-### 2.1 Explicitly rejected as primary architecture
-
-Alternatives with known failure modes, and why they were rejected:
-
-* **Full materialization of every view**: storing every selector state, match, witness, and reverse edge approaches the product of DOM and stylesheet size. The database framing of §1.1 survives this rejection precisely because its views are partial.
-* **Conservative invalidation followed by restyle**: preserves the separation between possible effect and semantic change, and therefore preserves its work amplification.
-* **Permanent proof subscriptions**: exact dependency edges are good for hot low-fanout queries, unacceptable as mandatory state for every match and negative result.
-* **Rebuilding on CSSOM program change**: dynamic stylesheet insertion and editing are normal page behavior, especially during load; they use incremental query-program operations.
-* **A complete computed-style object per element**: repeated style groups, inherited environments, and animation bases share storage through compact handles.
-
-### 2.2 Engineering principles
-
-These are the working principles the invariants compile down to in day-to-day changes. They are review tests, not aspirations.
-
-**Work is mutation-sized; a whole-DOM traversal is a purchase, never a default.** A style node may be visited only when the mutation named it, a compiled route reaches it, or its own output changes. A whole-document pass is one of three things: output (that many styles changed), a purchase (one counted pass that provably beats the per-node work it replaces), or a named, counted correctness floor. The review test for every loop: name what bounds it; if the bound is the document and the output is not, the loop is a bug, even while it is fast. Visit a node at most once per flush across all stages, and never restart an interrupted pass; grow its window instead.
-
-**Represent state data-oriented: dense tables over sparse maps, columns over objects.** A dense integer key (element index, atom, rule) indexes a vector or segmented column, never a hash map; hash maps are for content-keyed intern tables. Every dense per-node structure takes on the retirement story the day it is born, so a reused identity cannot inherit its predecessor's state. A missing structure reads as unknown, never as empty.
-
-**Choose presence structures by clearing cost.** A bitmap with a companion set-index list when the set lives for a transaction (cleared through the list, not memset); an epoch-stamped seen array when the set clears per candidate; a sorted vector when built once and only queried. A maintained population count is free exact cardinality where a plan cutoff wants one.
-
-**Sort once at a build boundary; inherit order on the hot path.** Convert order labels to dense ordinals at program build and read integers after. A per-subject sort during evaluation is a smell: inherit order from pre-sorted program structures and merge. Sorting the changed set once per transaction is the right shape; sorting anything document-sized falls under the traversal principle.
-
-**Spend document-order coordinates once they are bought.** With preorder positions and subtree ends, a subtree is a slice, ancestry an interval comparison, and skipping a subtree an index jump. The coordinates answer for one named tree relation and the current side only, and they are transaction scratch unless measurement justifies persistence.
-
-## 3. Evaluation disciplines and plans
-
-Two evaluation disciplines share one semantics:
-
-**Incremental**: the normal discipline. Retained derived state (prefix states, retained answers, winner groups) and exact deltas minimize discovery and downstream work.
-
-**Exact evaluation of a scope**: the engine ignores optional derived state and exactly evaluates an affected region with the exact evaluator against authoritative facts. It may inspect every style node in the region but emits only actual selector, rule, cascade, and computed-value changes. This is the correctness path and the universal response to missing state or planner uncertainty. An exact batch is still an exact transaction: broad input scope, only changed outputs.
-
-The choice is made per routed region, as a `Plan`:
-
-```text
-Selective { candidates } drive from an enumerable candidate source
-ExactBatch { nodes } sweep the region with the exact evaluator
-Conservative neither is executable; keep the routed region as-is
-```
-
-`Plan::Conservative` is not a restyle mode: it keeps the conservatively routed region, whose members are still checked by the match program before any delta is emitted, so precision is lost but correctness is not. There is no separate "recompute and republish" fallback mode; every degraded path narrows to one of the shapes above, and the broad ones are counted.
-
-## 4. Inputs, epochs, and transactions
-
-### 4.1 Terminology
-
-* **Semantic input**: an authoritative fact from DOM, CSSOM, browser state, or environment.
-* **Derived value**: a selector result, rule match, cascade winner, computed value, or damage result computed from semantic inputs.
-* **Logical program**: the immutable, shared description of selector, activation, cascade, and property dependencies.
-* **Materialized view**: a cached physical representation of a derived value or relation.
-* **Style transaction**: a batch of input changes applied atomically from the perspective of style observation.
-* **Style read epoch**: a mutation-free interval in which style reads the authoritative live DOM and other versioned inputs.
-* **Style node**: the engine's compact identity for a DOM element.
-* **Style feature atom**: a compact identity for a selector-mentioned tag, ID, class, attribute, value, namespace, or state fact. It does not own duplicate string payload.
-* **Match program**: a logical-IR evaluator answering whether a candidate subject matches a selector during one read epoch.
-* **Transpose program**: compiled inverse routes that start from a changed selector input and enumerate a safe superset of subjects whose match truth can change.
-* **Delta-routing registry**: required program state mapping a typed semantic input key to transpose entry points.
-* **Impact region**: the normalized union of subjects, relation ranges, and dependent scopes emitted by transpose programs for a transaction.
-* **Impact-region attribution**: the split between regions attributed to a specific rule and program (eligible for exact planning) and unattributed regions (evaluated conservatively).
-* **Match-answer identity**: a compact identity for one normalized exact selector answer.
-
-### 4.2 Style read epoch
-
-The epoch is a logical immutability contract, **not** a copied DOM or CSSOM snapshot, and not a reified object: it is upheld by phase rather than by a header. The evaluator runs synchronously at a point where script cannot mutate style-visible inputs, an FFI callback guard rejects reentrant entry into the engine, and identity retirement is deferred (retired `StyleNodeID`s are released for reuse only at a later safe boundary), so nothing an active evaluation can name is destroyed or reused under it. The evaluator queries current tree relations, element features, and document state from their authoritative live objects; nothing walks, clones, flattens, or retains the DOM.
-
-The final live tree is the authoritative new state. The old side is not pinned: the normalization journal's first record per key preserves the pre-transaction value (§4.4), retained derived state supplies old selector truth where resident, and tree/program transactions reconstruct their before side from those sources when planning after commit. Compact mutation records carry only the old-side facts needed to discover everything whose output may have changed.
-
-*Database counterpart:* the isolation is phase-based rather than MVCC: one writer-free interval per flush, with before-images carried in the changelog instead of retained versions.
-
-### 4.3 Typed deltas
-
-Input kinds (`InputKind`, transaction.rs):
-
-```text
-TreeRelations parent/sibling/scope/slot/host relation changes
-LocalFeature tag, ID, class, attribute, namespace fact changes
-State element and document state facts (:hover, :checked, ...)
-ElementDeclaration element-owned declaration blocks
-ElementStyleInput edge-triggered per-element style actions (not retained fact state)
-Program stylesheet program changes
-CascadeTopology layer/order topology changes
-Environment media, viewport, preference, and other environment inputs
-```
-
-Animation state is not an input kind: an element whose animation-relevant inputs changed is reached through the kinds above, and animation sampling is an overlay on published style (§9.11), not a change stream into the engine.
-
-Typing matters: treating every change as a generic version bump moves work from mutation time into an unnecessarily broad lazy validation later.
-
-`ElementDeclaration` covers declarations sourced from one style node rather than a stylesheet rule: the `style` attribute, HTML presentational hints, and SVG presentation attributes such as `fill` and `stroke`. The authoritative HTML/SVG mapping produces an immutable old/new declaration block with its language-defined cascade placement; StyleEngine does not reinterpret attribute syntax.
-
-**One mutation can publish several independent typed effects.** Changing an SVG `fill` attribute may change an attribute-selector feature, a presentation declaration, and an `attr()` input. Routing sends each effect to its own consumers; handling one never stands in for the others.
-
-### 4.4 Transaction boundaries and the normalization journal
-
-Script performs many mutations before style is observed. Record them and normalize at the transaction boundary:
-
-* Adding then removing the same class produces no final class delta.
-* Inserting then deleting an unobserved rule produces no selector or cascade work.
-* Changing a declaration three times retains only the old and final declaration block versions.
-* Inserting several sheets during parsing produces one ordered program update.
-* Moving a subtree twice retains only its original and final relationships.
-
-Operations whose Web-platform semantics force synchronous observation establish a boundary. Style reads (resolved-style queries, layout-dependent APIs where required) flush all earlier input changes before returning. Normalization never combines changes across a required observation boundary.
-
-**Journal structure.** Pending changes go into a keyed normalization journal:
-
-* DOM/state keys: style-node identity + typed fact or relation.
-* Program keys: stable sheet/rule/declaration/attachment/topology identity + edited field.
-
-The first record for a key preserves the pre-transaction value; later records replace only the pending final value; cancellation removes the key. Raw mutation count is therefore not the default work multiplier.
-
-*Database counterpart:* the journal is the changelog, a CDC stream, and not a write-ahead log: it exists for change propagation, not durability or recovery, so WAL disciplines (idempotent replay, checkpointing) do not transfer. Key-level last-writer-wins is log compaction, the retained pre-transaction value is the before-image, and the overflow marker below is compaction of a key range into one coarse record whose replay is a scoped rescan.
-
-For tree and feature mutations, the first record retains the minimum old-side information required by the applicable transpose rules:
-
-* old and new feature atoms;
-* old and new parent, adjacent-sibling, tree-scope, flat-tree, slot, host, and pseudo-relation endpoints;
-* operator-specific boundary identities for affected sibling or subtree ranges.
-
-A removed style-node identity and any required relation tombstone stay valid until the transaction retires. The journal does **not** copy unchanged descendant fields and does **not** preserve a traversable old DOM. Old-side facts exist to establish a complete impact region, not to reconstruct history.
-
-Resident derived state supplies old selector truth when available. If it is missing, discard the affected derived fragment, evaluate the final state from the live DOM, and compare with the last committed style output.
-
-**Journal overflow.** The journal is byte-accounted against its own memory category. Its capacity allows four fine-grained entries per connected element and is clamped between 4 MiB and 32 MiB. When growth would cross that capacity, the most numerous reconstructible fact, relation, program, or topology input kind's fine-grained entries are replaced with a typed **complete-scope marker** covering the whole document for that kind, and later records of that kind are dropped outright. Edge-triggered `ElementStyleInput` actions are never marker-coarsened: their reaction and inherited-group bits remain attached to every target and repeated actions on one target are unioned. At flush a marker widens the impact region to the document, and the plan evaluates that region exactly (§3). A pathological script can force broad discovery work but cannot discard an action whose target cannot be reconstructed from final facts. (Environment markers are exempted from disabling the retained-selector fast paths, since environment changes cannot alter selector incidence.)
-
-### 4.5 Mutations during evaluation
-
-During evaluation, DOM and CSSOM topology, element selector state, attached program order, and other style-visible inputs are logically immutable: the evaluator runs synchronously while the document cannot run mutating script, and the FFI boundary's callback guard rejects reentrant entry into the engine, so the phase contract is enforced at the boundary rather than by a per-read lock or version check. Reentrant or concurrently delivered changes (including parser, resource, animation, and environment changes that become ready mid-evaluation) are queued for a later transaction.
-
-The read interval ends only after computed outputs and observer consequences commit atomically. Style node identities, program versions, and environment handles named by an active evaluation cannot be destroyed or reused before retirement; retired identities are released for reuse only at a later safe boundary.
-
-## 5. Style node identity and tree representation
-
-### 5.1 Identity
-
-`StyleNodeID` is a document-local `u32`. A 32-bit identity is sufficient for one document and halves the size of many indexes and handles versus native pointers.
-
-Pseudo-element matching projects declarations from an originating element's match set. A `PseudoElementTarget` carries the pseudo kind without allocating another style node. A first-class pseudo-tree representation may introduce its own identity model when view-transition pseudo-trees need one.
-
-### 5.2 Column inventory
-
-Do not blindly duplicate fields already on DOM elements. Persistent style-side storage is:
-
-* a minimal style-data handle reachable from an element;
-* required evaluator-local relation-navigation state;
-* directly indexed columns for optional per-element fact metadata;
-* compact dense columns for live computed-style publication handles;
-* reusable temporary columnar batches for bounded-region evaluation;
-* indexes containing only style-relevant facts.
-
-```text
-parent required, dense
-first element child required, dense
-next element sibling required, dense
-previous element sibling required, dense
-depth required, dense
-tree scope conditional, allocated only for multi-scope documents
-assigned slot allocated per segment
-shadow host allocated per segment
-fact metadata header optional payload in a dense directly indexed column
-prefix local-fact identity optional segmented column
-computed publication handles compact dense columns for observed content
-```
-
-Shadow-relation and prefix-cache columns are allocated per segment only when useful. Fact metadata and computed publication use dense directly indexed columns whose vacant rows carry no external payload; they trade empty slots below the highest used identity for constant-time access. A document with no shadow trees allocates no shadow relationship columns.
-
-### 5.3 Required relation navigation
-
-Transpose programs must traverse inverse selector relations without leaving the evaluator. That is why parent / first-element-child / next-element-sibling / previous-element-sibling are dense required columns rather than optional acceleration.
-
-Concretely: for `.theme .item` and a class mutation on a container, the transpose step for the changed `A` operand is "descendants that can satisfy `B`". That set is not in the `TreeDelta`, so the evaluator must either stream the subtree from the authoritative side or intersect a resident `.item` posting with subtree membership. `StyleNodeID` is not a tree-order label, so membership costs `Hregion` relation steps per candidate. Without resident columns, every one of those steps is a boundary crossing, and the hot path degenerates into reverse cold requests or per-element FFI. Both are forbidden.
-
-Rules:
-
-* Dense `u32`, allocated for every connected style node, counted against the mandatory per-node byte budget (§10.2).
-* The DOM remains the Web-facing semantic owner, while these columns are the engine's committed evaluation projection. C++ must publish every relation mutation, and the columns must agree with the final live tree at every epoch boundary; the exact evaluator reads this projection rather than fetching the DOM in reverse.
-* Their allocation is required engine state, and a missing committed relation row is an invariant violation rather than a cold-path signal.
-* Ownership follows the tree: the subsystem that mutates DOM structure publishes relation-column updates as part of the same `TreeDelta` that reports the mutation, so they cannot drift within a read epoch. Mismatch is an invariant violation, not a recoverable condition.
-
-Tree-scope navigation has the same traversal requirement once multiple scopes are supported, because scope-membership transposition works the same way. Allocate the dense tree-scope column only for documents that need it, and count it against the conditional byte allowance (§10.2). Flat-tree operations derive from the sparse assigned-slot and shadow-host relations instead of duplicating a third parent column.
-
-### 5.4 Temporary packing
-
-Packing is an execution choice, not snapshot construction. Small or selective work queries the resident columns directly. A sufficiently large transaction compiles its affected regions into a preorder topology in transaction scratch; broad matching holds an immutable shared view of the primary fact columns, while transaction-specific pre-images remain bounded overlays. Scratch is generation checked and released after the transaction. Whole-document cold evaluation never allocates a second complete flattened DOM or fact-store copy.
-
-### 5.5 Multiple tree relations
-
-CSS does not operate over one universal tree. The logical input distinguishes:
-
-* DOM ancestry
-* shadow-including ancestry
-* flat-tree inheritance relationships
-* slot assignment
-* part exposure
-* pseudo-element origin relationships
-* view-transition capture and generated pseudo-tree relationships
-* style-scope membership
-
-**Every selector operator states which relation it consumes.** This keeps special cases out of the generic parent and descendant operators. A generic descendant walk does not pierce a shadow root or follow slot assignment.
-
-### 5.6 Document order
-
-Build preorder ranges in transaction scratch for the scope being evaluated; retain no per-node tree-order label. Queries depend on relative order and ancestry, never on the numeric representation of an order label, so physical relabeling is invisible to semantic dependencies.
-
-A subtree move emits relationship deltas for its boundary only. Internal relationships remain shared unless the move changes the relevant tree relation, scope, slotting, or inherited context.
-
-## 6. Selectors
-
-### 6.1 Element and document state
-
-State is represented as semantic facts, not bespoke invalidation entry points:
-
-```text
-Hovered(element) Active(element) Focused(element)
-FocusVisible(element) Checked(element) Disabled(element)
-Valid(element) Target(element) PlaceholderShown(element)
-Open(element) PopoverOpen(element)
-```
-
-A transition publishes old and new fact values. Only selector operators depending on that state receive the delta. Derived states such as `:focus-within` are logical operators over tree relationships; the same machinery supports `:has(:focus)`, `:has(:checked)`, and state nested in selector-list pseudo-classes.
-
-High-frequency states use the same semantics but may be scheduled in a low-latency lane: a hover change must not wait behind compilation of an unrelated large stylesheet when its dependencies can be evaluated independently.
-
-**Visited-link privacy.** Visited state is a routed state fact like any other; a dedicated privacy-taint component in operator, cache, and record identities is **not yet implemented**. When visited-dependent styling is implemented to specification, tainted materializations must not expose visited information through timing diagnostics, developer APIs, or cache keys observable across privacy boundaries, and untainted and tainted results must never share a proof merely because their value bytes match.
-
-### 6.2 Selector logical IR
-
-Selectors compile into a compact logical IR. The IR defines semantic equality independently of how an operator is evaluated. Representative operators:
-
-```text
-LocalFeature And Or Not
-Parent Ancestor NextSibling FollowingSibling
-NthPosition RelativeExists
-ScopeStart ScopeEnd Host Slotted Part
-PseudoElementProjection StatePredicate
-```
-
-### 6.3 Bidirectional compilation and delta routing
-
-Every selector compiles into two views of the same semantics:
-
-```text
-match program: candidate subject -> exact match result
-transpose program: changed input -> possible affected subjects
-```
-
-The match program is exact. A transpose program may over-approximate but must **never** omit a subject whose selector truth can change. It starts at each occurrence of the changed input in the selector and traverses inverse combinator relations toward the subject. Resulting subjects are checked by the match program before any selector-truth delta is emitted.
-
-| Match fragment | Changed operand | Transpose step toward possible subjects |
-| --- | --- | --- |
-| `A B` | `A` | Descendants that can satisfy `B` |
-| `A B` | `B` | The changed `B` node |
-| `A > B` | `A` | Element children that can satisfy `B` |
-| `A > B` | `B` | The changed `B` node |
-| `A + B` | `A` | The next element sibling if it can satisfy `B` |
-| `A + B` | `B` | The changed `B` node |
-| `A ~ B` | `A` | Following element siblings that can satisfy `B` |
-| `A ~ B` | `B` | The changed `B` node |
-
-Compound operands transpose to the node whose local fact changed. Structural pseudo-classes add typed child-sequence entry points. `RelativeExists` transposes a possible witness change through the inverse relative-selector axes to possible anchors, then transposes an anchor-truth change through the outer selector to its subjects (§7). Every step names its DOM, shadow-including, flat-tree, slot, part, pseudo, or scope relation explicitly.
-
-*Database counterpart:* the transpose program is the standing query's delta rule, its derivative with respect to one input: given that input's change, it enumerates the output rows that can change. Match-checking every enumerated subject is delta verification, and it is what makes safe over-approximation legal.
-
-**Delta-routing registry.** Required program state mapping each semantic input key to the transpose entry points that mention it:
-
-```text
-(input kind, StyleAtomID or relation kind) -> transpose entry-point slice (stable order)
-```
-
-It covers tag, ID, class, attribute, namespace, state, tree relation, structural, scope/topology, and activation inputs. Selectors with no selective local atom go into a **typed** always-consulted slice for the input kinds that can affect them, not one global universal bucket. A child-list mutation must not consult programs that depend only on an environment predicate.
-
-Construction is part of program compilation. Each input key indexes one insertion-ordered route slice, and all slices are packed into a flat `RouteID` vector; route headers and their variable-length paths live in parallel columnar arenas. The structure is proportional to selector-input incidence, not element count.
-
-The registry is required, not optional: scanning every selector header for every ordinary mutation changes the hot-path asymptotics. Feature postings may accelerate subject enumeration, but a missing posting never changes which transpose entry points run. Program replacement keeps old registry entries alive through old-result removal and installs the new program and registry atomically in the next epoch.
-
-### 6.4 Impact regions
-
-For a normalized transaction `T` and attached program version `P`:
-
-```text
-Impact(T, P) = normalize(union(transpose_entry(delta) for delta in T))
-```
-
-The result can contain individual style nodes, sibling intervals, subtrees, tree scopes, and dependent flat-tree or shadow scopes. It is a transaction-local execution plan, never a retained element-by-selector relation. Overlapping regions coalesce without changing their named tree relation.
-
-Every selector operator and every non-selector dependency that can request exact scope evaluation has a transpose rule satisfying:
-
-> Between the last committed state and the final live state in the read epoch, every style node whose semantic selector, cascade, computed value, or observer output can differ is contained in `Impact(T, P)` or in an explicitly chained downstream region produced from it.
-
-Coverage is fail-closed: any gap widens the region; unknown scope is never empty scope. Explicit epoch-bound impact certificates are not retained today; the coverage obligation is the same, enforced by widening and verification.
-
-Consequences to implement:
-
-* Inheritance transposes through the **flat** tree.
-* Slot reassignment emits both participating light-tree and shadow-tree regions.
-* A scope or stylesheet-topology change names every scope whose attached program or cascade order can differ.
-* When an operator cannot construct a narrower proven region, it widens to that operator's complete tree scope, and ultimately to the document.
-
-Regions carry attribution: a region attributed to a specific rule and selector program is eligible for exact per-entry planning, while an unattributed region is evaluated conservatively. Any gap (a missing posting, an incomplete retained answer, an unroutable input) surfaces as a typed `Missing`/`Incomplete` lookup whose only legal response is widening, ultimately to the document. A verification gate can additionally assert that a scoped plan publishes nothing it cannot attribute.
-
-The exact batch plan consumes only a complete impact region.
-
-### 6.5 Execution kernels
-
-One selector semantics, three cooperating execution kernels. The boundaries are physical, so operators can move between them later without changing query meaning.
-
-**Local-compound kernel**: the dominant hot path. Evaluates tag names, IDs, classes, attributes, namespaces, element states, and Boolean compound structure. Uses fixed-width interned atom IDs at the C++/Rust boundary, a rightmost-feature dispatch key, and an arena of fixed-width enum nodes with shared operand and literal tables. Candidate tests are ordered cheap/high-rejection first and short-circuit on failure. **A compound with no relational operator allocates no witness.** (Non-relational chains do participate in the retained prefix automaton below when admitted; that state is optional, never required.)
-
-Selector feature keys have two sparse directions with independently bounded sizes:
-
-```text
-feature atom -> candidate StyleNodeID posting optional
-feature atom -> transpose entry points required, program-proportional
-```
-
-The first avoids scanning DOM facts and may be missing for a key. The second lets a class, attribute, tag, ID, or state mutation activate only routes mentioning the changed feature. If the posting is missing, the routed transpose program reads authoritative DOM facts or uses an exact scope batch. No element-by-selector relation is ever required.
-
-**Directional-combinator kernel**: child, descendant, adjacent-sibling, and subsequent-sibling relationships consume the same local compound programs, under operation-specific schedules:
-
-* *Cold initial style*: stream style nodes, gather candidate program offsets from each node's local feature atoms, match relationships right-to-left with direct tree walks.
-* *Dynamic selector insertion*: reverse direction: start from the new program's safest rightmost-feature posting and check only those candidates when selective.
-* *Mutation to an attached program*: start from changed local truths. Descendant and child effects run as a top-down prefix-state pass over the exact affected scope, warm-started from the retained prefix states of the previous transaction where those survived.
-* *Sibling effects*: adjacent-sibling inspects the immediately participating siblings; subsequent-sibling and structural effects use the same automaton's child-sequence machinery and stop at unchanged outputs.
-
-These are schedules over the same logical IR.
-
-**The prefix automaton.** Selector-prefix truths are retained across transactions as a cache: interned prefix states (the "context tokens" entering each node), per-element entering states, per-element terminal match sets, and per-element positional truth bits. Registration is budgeted: an automaton carries at most 32 structural-test truth bits, and a chain that would overflow is refused whole and stays with its exact routes, so refusal degrades to exact matching, never to a wrong answer. The cache cycles through scratch during a flush and is retained at the end of it; discarding it is always legal, and the next flush rebuilds from exact evaluation.
-
-**Relational-query kernel**: `RelativeExists` reuses local compounds and directional traversal to answer an existential question per anchor. Witnesses belong only to this kernel. A document with no relational selector allocates no relational queues, per-node relational fields, or relational cache tables (the witness table is empty and unallocated, and relational routing early-returns on an empty route set).
-
-Selector lists and Boolean functional pseudo-classes compile to branch nodes in the same IR. Specificity, dispatch analysis, and match metadata stay static program data rather than being reconstructed per candidate or scope-dispatch build.
-
-### 6.6 Local feature indexes
-
-Selector posting keys are semantic feature atoms, not strings: tag name, ID, class, attribute name, directionality, part name, and custom state. Namespace, ordinary state, root, heading, and other fixed facts are read from the authoritative row when dispatch reaches a candidate. Every attribute operator drives from the attribute-name posting; exact equality can reject on its value atom before matching, while substring and token operators run their exact text test in the compound evaluator.
-
-Representation: **chunked sorted postings**, sorted by `StyleNodeID`. Every published selector feature is maintained eagerly, whether or not the current program mentions it. Forgetting an element removes all of its posting memberships. A selector posting that outgrows its cardinality limit is dropped and stays missing for its key.
-
-One key stops retaining a posting when its membership grows beyond the greater of 4096 elements and one quarter of the live elements. The key then reads as missing acceleration, so every consumer takes its exact fallback instead of retaining a near-document-sized secondary index.
-
-*Database counterpart:* secondary inverted indexes, and non-clustered ones: `StyleNodeID` is not a tree-order label, so proving a posting candidate lies in a region is the row lookup a non-clustered index pays when the predicate lives on the clustering dimension.
-
-Because `StyleNodeID` is not a tree-order label, a subtree impact region is *not* a numeric posting interval. For a selective plan, enumerate posting candidates and prove region membership through the named tree relation, charging ancestor/sibling steps explicitly. Those steps walk the required relation columns, so a selective plan costs `Hregion` in-evaluator pointer hops rather than boundary crossings. For a broad region, stream the region in preorder and test local features from authoritative facts instead of building a full posting-to-preorder join.
-
-Transaction-local preorder arrays make repeated relation checks constant time only after their construction cost and `StyleNodeID`-to-preorder mapping are paid as discovery.
-
-### 6.7 Selective-plan cutoff
-
-The plan choice is made once, before enumeration, from candidate cardinality and region size: **the exact batch plan is chosen when the smallest safe candidate source covers more than one quarter of the region** (`SELECTIVE_SHARE_DIVISOR = 4`). A candidate source of 64 or fewer skips the cost model entirely and goes straight to direct membership checks.
-
-The cutoff uses **exact cardinality** of normalized impact ranges. If exact cardinality is unavailable, the batch plan is chosen rather than a smaller scope guessed. Missing acceleration can make the exact batch plan cheaper.
-
-### 6.8 Query outputs
-
-```text
-SelectorMatches(SelectorProgramID, StyleNodeID, MatchMetadata)
-```
-
-`MatchMetadata` contains only what later semantics need (selector-list entry, specificity, scope proximity, pseudo-element target) and is factorized when fields are static for the selector.
-
-Rule matching is a join:
-
-```text
-SelectorMatches
-AND RuleUsesSelector
-AND RuleAttachedToScope
-AND ActivationPredicateIsTrue
- -> ActiveRuleMatches
-```
-
-This separation lets declaration edits, condition changes, and attachment changes reuse selector truth.
-
-Concrete matches are enumerated into a transaction-local sorted `StyleNodeID` vector and discarded after cascade consumption.
-
-### 6.9 Specificity
-
-Specificity is program metadata with explicit aggregation rules. Each top-level complex selector in a rule's selector list carries its own static specificity. If multiple entries match the same style node, `MatchMetadata` retains the greatest matching entry specificity together with any independently required scope and pseudo metadata. Short-circuiting may skip work only when it cannot hide a later matching entry with a greater cascade contribution.
-
-`:is()` and `:not()` contribute the maximum specificity of their argument list as a static property of the program, regardless of which branch establishes the Boolean result. `:where()` contributes zero. `:has()` uses the corresponding static argument-list rule even though witness discovery is dynamic. Structural and nesting operators use their specification-specific contribution; none of them inherit a generic runtime "specificity of whichever branch matched" rule.
-
-The cold evaluator and the incremental evaluator consume the same static specificity table.
-
-### 6.10 Ephemeral selector consumers
-
-Selectors passed to `querySelector()`, `querySelectorAll()`, `matches()`, and `closest()` are **match-only query programs**. They compile with the same selector semantics and evaluate with a bare match evaluator over the engine's fact store, but they are not attached style programs: no transpose routes, no routing registry entries, no rule identities, no witness reads or writes, no cascade state. Scope roots, shadow boundaries, relative-selector anchoring, and syntax failure are explicit compiler inputs. Result ownership belongs to the DOM API, not StyleEngine.
-
-Two mechanisms make this correct and fast against a live document:
-
-**Settling.** A connected query must see the current DOM generation, which may have pending staged input. Preparing a query commits the pending fact rows into the resident store while stashing their pre-images; the next flush (or normalize) restores the pre-images first, so the style transaction still computes its before/after diff as if the query had never intervened. A query observes current facts; invalidation keeps its native orientation.
-
-**Isolated engines for disconnected roots.** A query rooted in a disconnected tree runs against a small throwaway engine populated from that subtree alone. These engines are cached per root (an LRU keyed by root and mutation versions) so repeated queries over the same detached tree reuse one population, and a compiled query program is cached for the lifetime of its `SelectorQuery` object rather than recompiled per call.
-
-### 6.11 Push/pull division
-
-Push exact deltas only when the transaction or a resident view (witness, winner column) directly names the consumer. Everything else is validated by exact pull evaluation of the affected scope.
-
-When explicit subscriber fanout would cross a hard edge threshold, do not allocate the edge set; pull from the exact affected scope instead.
-
-## 7. Relational selectors and `:has()`
-
-`RelativeExists` is an existential query relating an anchor to a relative-selector witness. Its semantic output is Boolean even when many witnesses exist. **Only transitions between zero and nonzero witnesses can affect selector truth.**
-
-### 7.1 Compilation
-
-A relative selector compiles into operators over: anchor scope; child or descendant traversal; adjacent or following sibling traversal; ordered sibling-descendant combinations; local compounds and selector-list logic. Every traversal operator names its tree relation and scope boundary.
-
-### 7.2 Simple-query grammar
-
-A `:has()` query is **simple** when all of:
-
-* the relative selector contains exactly one axis (a selector-list argument compiles each branch into its own relative query, each classified independently);
-* that axis is one of: implicit descendant, child, adjacent sibling, or following sibling;
-* that axis is followed by exactly one local compound;
-* the compound contains at least one positive indexable driving feature;
-* the query contains no structural pseudo-class, nested relational operation, internal combinator, pseudo-element projection, or shadow/scope-crossing construct.
-
-Simple: `:has(.error)`, `:has(> .item:hover)`, `:has(+ .selected)`, `:has(~ .selected)`. Complex: `:has(.group > .item.error)`, `:has(:nth-child(2))`. A list such as `:has(.a, .b)` is two simple queries, each with its own axis and driving feature.
-
-Complex ≠ unsupported. Complex queries use the exact direct relative-selector evaluator and retain no witness.
-
-Selectors Level 4 forbids nesting `:has()` inside `:has()`; reject that syntax at compile time rather than treating it as an incremental-coverage case.
-
-### 7.3 Witness state
-
-Retain **at most one witness per simple positive observed query**. Consequences:
-
-* Adding another witness does not change state and emits nothing.
-* Removing a non-retained witness requires no repair.
-* Removing the retained witness triggers a replacement search.
-
-A retained witness is a proof cache, never semantic state; discarding it is always legal.
-
-*Database counterpart:* `:has()` truth is an EXISTS aggregate, so only zero-to-nonzero transitions are deltas. The retained witness is a one-row partial materialization of the subquery; losing it is a view miss repaired by an upquery, never a semantic event.
-
-**Bounds.** A retained witness is re-verified on read (liveness, axis membership, and a match re-check), with a fixed step cap on the following-sibling back-walk; any verification failure clears the entry and routes conservatively. The witness table itself is capped (currently 16,384 entries). Replacement after a lost witness is the ordinary forward walk of the query's axis; the routing-side cutoff (§6.7) bounds how witness-side discovery drives anchors. A Boolean result is never approximated.
-
-### 7.4 Negative results
-
-A negative `:has()` result must not allocate dependency edges to every element in the searched region, and it allocates nothing at all: a query that finds no witness stores no state. Absence is recomputed on demand, and the false-to-true transition is caught from the witness side: a mutation that could create a witness routes through the relative-selector transpose entry points to its possible anchors (§7.5), so there is no cached negative to invalidate and no stale negative can exist.
-
-### 7.5 Witness creation (false-to-true)
-
-When a local mutation can create a witness, routing activates the relative-selector transpose entry point. For a simple query, possible-anchor discovery is bounded by the single inverse axis:
-
-| Relative axis from anchor | Possible anchors for a newly matching witness |
-| --- | --- |
-| Child | Its element parent |
-| Descendant | Its element ancestors up to the query's scope boundary |
-| Adjacent sibling | Its immediately preceding element sibling |
-| Following sibling | Its preceding element siblings in the same child sequence |
-
-Each possible anchor is checked exactly, including anchor compound and scope semantics. A false-to-true Boolean transition then enters the outer selector's transpose program, so `.card:has(.error) .button` reaches only possible `.button` subjects in the affected anchor context. A new additional witness for an already-true anchor emits no Boolean delta and never runs outer propagation.
-
-Complex relative selectors use the same principle with multiple inverse axes; their transpose program may widen to a sibling interval, ancestor path, subtree, or complete tree scope, but never omits a possible anchor. Discovery work is charged even when every anchor check rejects.
-
-### 7.6 Non-subject `:has()`
-
-For `.card:has(.error) .button`, the relative query produces a Boolean anchor result that updates the anchor's outgoing top-down selector context. Descendant `.button` matches change only where the context transition changes their rule output. Adding a second `.error` while a witness already exists emits no anchor-truth delta and therefore no descendant selector work.
-
-## 8. Stylesheet program
-
-### 8.1 CSSOM versus semantic program
-
-* CSSOM objects preserve API identity, ownership, indices, and serialization behavior.
-* The semantic program is an immutable, content-keyable representation used for computation.
-
-Editing a CSSOM object creates a new semantic version without replacing the wrapper object.
-
-### 8.2 Stable identities
-
-```text
-SheetID stable semantic sheet identity
-RuleID stable semantic rule identity
-SelectorProgramID immutable compiled-selector identity (content-interned:
- equal compiled selectors share one ID)
-DeclarationBlockID declaration-block version (a fresh identity per edit)
-CascadeLayerID layer identity
-ProgramVersion one document-global program version, bumped per change
-```
-
-CSSOM wrapper objects hold their semantic identities on the C++ side (a stylesheet knows its `SheetID`, a rule its `RuleID`). A declaration edit preserves `RuleID` and `SelectorProgramID` while installing a new `DeclarationBlockID`; a selector edit preserves the rule identity but changes the compiled selector program and match relation. Rule contents are updated in place under the rule's single version slot; there is no retained history of prior rule versions, and nothing reads one: evaluation always runs against the current program, with old truth supplied by the journal's before-images and retained derived state (§4.2). Activation state (media, supports, container, sheet disabled) is tracked as current condition booleans per rule and sheet, updated by typed inputs.
-
-### 8.3 Rule version contents
-
-```text
-RuleID
-SelectorProgramID
-DeclarationBlockID
-conditions-hold flag
-CascadeOrigin
-CascadeLayerID
-StyleScopeID
-StyleSheetOrderToken
-NestedRuleOrderToken
-```
-
-Selector list entries may carry separate static specificity and matching output identities while sharing a declaration block. Nested group rules form a persistent tree: editing one descendant creates new nodes only along the path to it; unchanged selectors, declarations, and conditions keep their identities.
-
-### 8.4 Attachment
-
-Attachment connects a compiled program to one or more style scopes and is distinct from parsing and compilation. It covers parser-created `style`/`link` elements, dynamically inserted stylesheet nodes, load completion, constructed stylesheets, `adoptedStyleSheets` changes, sheets shared by multiple shadow roots, enabling/disabling, and UA/user/author origins.
-
-One compiled program attaches to multiple scopes without duplicating its logical selector or declaration representation. Scope-specific indexes and materializations stay separate where tree membership requires it.
-
-Selector programs contain process-global `StyleAtomID` values and immutable equal programs are shared across document engines on the StyleEngine thread. Each document retains references to the global atoms it uses; the final document releases the underlying interned-string identity and makes the numeric atom reusable. Entry and rule identities, routing registries, attachments, order tokens, and all result materializations remain document-local, so no document generation, tree scope, or result state crosses the sharing boundary.
-
-The memory lease for a shared selector payload belongs to the process-global selector-program table. Document controllers report only their local program identities and indexes; the shared table reports each immutable payload once and keeps that charge until the payload's final document reference drops.
-
-### 8.5 Source order
-
-Source order composes from per-axis components:
-
-```text
-origin a fixed enum ladder (with importance reversal)
-tree context encapsulation-context depth
-layer order per-scope layer ranks, rebuilt from the ordered layer list
-stylesheet order order-maintenance tokens, one axis per tree scope
-nested rule order order-maintenance tokens, one axis per sheet
-```
-
-The two order-maintenance axes use `u64` gap labels: insertion writes one midpoint label, and exhausting a gap relabels the axis. Numeric relabeling changes no semantic version, because comparisons depend on relative order only.
-
-Moving a sheet updates its sheet-order token; rules refer to that token indirectly, so their program nodes are not rewritten. Moving a nested rule updates the smallest enclosing order token.
-
-Reordering affects selector truth only if scoping or attachment changes. Otherwise it is purely a cascade priority delta.
-
-*Database counterpart:* gap-labeled ordering keys (fractional indexing). Comparisons read relative order only, so relabeling is a physical operation with no semantic version change.
-
-### 8.6 CSSOM mutation surface
-
-Every operation maps to a typed program delta. None produces generic document invalidation.
-
-| Mutation | Primary semantic delta |
-| --- | --- |
-| Attach, detach, or move a `style`/`link` element | Attach, detach, or reorder a sheet program |
-| Change text inside a `style` element | Structurally replace the sheet program |
-| Finish loading a linked or imported sheet | Attach the compiled program at its semantic position |
-| Change `href` on a stylesheet link | Detach the old resource; later attach the new program |
-| Change `media` or `disabled` | Update an activation predicate |
-| Reorder `adoptedStyleSheets` | Update sheet-order tokens |
-| Add or remove an adopted sheet | Attach or detach an existing compiled program |
-| `insertRule()` (incl. grouping rules) | Add a rule program at a new order token |
-| `deleteRule()` (incl. grouping rules) | Remove a rule program |
-| Change `selectorText` | Replace the selector-program edge of one rule |
-| `setProperty()`, `removeProperty()`, declaration `cssText` | Replace one declaration block |
-| Change a condition rule's text | Replace an activation predicate |
-| `replace()` / `replaceSync()` | Structurally replace a sheet program |
-| Edit keyframes through CSSOM | Update the named animation program and its consumers |
-| Register a custom property | Update the registration input for that property name |
-| Change the `style` attribute | Replace one element declaration block |
-| Change an HTML presentational-hint attribute | Replace the mapped element declaration block, plus any independent selector/attribute inputs |
-| Change an SVG presentation attribute | Replace the mapped element declaration block, plus any independent selector/attribute inputs |
-
-CSSOM collections and wrapper objects reflect API-required mutations synchronously; semantic style work may remain queued until the next required observation. Stable object identities plus immutable program versions let those coexist.
-
-### 8.7 Insertion
-
-1. Parse or receive the immutable stylesheet program.
-2. Compile selector, declaration, condition, layer, and scope nodes.
-3. Assign a stylesheet order token between neighbors.
-4. Attach the program to the applicable style scope.
-5. Evaluate only selector-program nodes whose results are not already available in that scope.
-6. Gate selector matches through the sheet's activation predicates.
-7. Add exact matched-rule deltas to the cascade.
-
-No existing selector truth is invalidated because a sheet was inserted. If the new declarations lose the cascade everywhere, computed styles are unchanged even though the sheet matched many elements.
-
-**Batch planning.** A program transaction is planned as a batch, not a loop over inserted rules. Compile all new selector fragments, then choose selective or exact-batch evaluation once:
-
-* A small number of selective rules drives evaluation from sparse feature postings.
-* A large sheet or burst of sheets builds temporary rightmost-feature buckets and runs one columnar pass over the relevant live style nodes, evaluating all new fragments together.
-
-Temporary buckets and candidate arrays are transaction scratch, discarded afterward.
-
-If no style was observed before parser or script mutations completed, compute only the final program and style state. If an observer required an intermediate state, apply later sheets incrementally to that committed state.
-
-**Loading and `@import`.** Network completion order does not determine cascade order. A linked or imported sheet receives its semantic position from document and rule order before or while it loads; when contents arrive, attachment uses that existing token. An `@import` rule is an import program node carrying layer, supports, media, and source-order inputs; load completion attaches the imported program beneath it without rebuilding or renumbering the parent sheet. Several sheets completing before the next observation join one normalized transaction while keeping their independently established positions. If script forces style between completions, each observed completion commits as its own transaction.
-
-Parsing and immutable program compilation may run ahead of attachment, off the main style-evaluation path, subject to CSSOM visibility and loading-order requirements.
-
-### 8.8 Removal
-
-Removing a sheet detaches its rules from the scope. Determine affected matched rules **without** retaining a complete inverse match relation per rule:
-
-* Reject a removed rule immediately when the complete winner-group column proves it won no declaration.
-* Otherwise route the detached selector programs from their indexed subject features and evaluate them exactly before retiring them.
-
-Bulk removal is grouped: detaching a sheet or rule subtree evaluates its program-owned selector set as one change and removes its declarations from cascade structures in one operation. Cold content validates against the new program version lazily. Removing a sheet with no winning declarations emits no computed-property deltas.
-
-### 8.9 Rule-level edits
-
-**`insertRule()`** adds one immutable rule version at an order token between neighbors and compiles and evaluates only the new selector and condition fragments. Existing rules are neither renumbered nor rematched.
-
-**`deleteRule()`** removes the rule from its matching elements and repairs cascade winners for the properties it declared. A cold deleted rule is re-evaluated to enumerate its affected elements. Deleting a group rule detaches the persistent subtree rooted at that group; shared descendants stay alive if referenced elsewhere.
-
-**`selectorText`** replacement keeps the `RuleID` and order token, compiles the new selector program, and records one typed input carrying the old and new program identities. Both programs are then evaluated through the retained-answer patch machinery: old-program matches produce removals, new-program matches produce additions, and the cascade-winner comparison stops propagation where the outcome is unchanged, except that an element whose recorded winner came from the edited rule always takes the full cascade reduction, since the fast comparison cannot prove its winner survived. Answer identities are keyed by program, so an element matching both the old and new selector still transits two answer identities; the stop happens at the winner layer, not the match layer.
-
-**Declaration change** does not invalidate selector truth. A resident exact match relation feeds the declaration-block delta directly into the cascade; if absent, exactly reconstruct only the edited rule's matches and charge that selector work to `reconstruct`. Never rematch unrelated rules. Only properties present in the old or new declaration block are winner-change candidates.
-
-Inline style uses the same declaration-block mechanism, with a match relation containing exactly its originating style node. HTML presentational hints and SVG presentation attributes use the same element-owned relation with distinct declaration kinds and their spec-defined cascade placement. They do not masquerade as inline style and do not enter the selector program merely because their source syntax is an attribute.
-
-### 8.10 `replace()` / `replaceSync()`
-
-Replacement reuses rule identities **positionally**: the sheet's style rules are snapshotted, and each incoming style rule reclaims the next prior `RuleID` in order, carrying its old declaration block forward and recompiling its selector. Compiled selector programs are content-interned, so an unchanged selector keeps its `SelectorProgramID` and its match relation for free. A sheet containing any non-style rule (or a rule-kind mismatch mid-list) falls back to whole-sheet detach-and-reattach, which really does swap the rule list; that path still compares selector truths and winners before emitting downstream changes, so correctness never depends on the reuse heuristic finding a correspondence. A content-based structural diff that survives insertions at the top of the sheet is a future idea.
-
-### 8.11 Layers and conditional rules
-
-Cascade layers form their own versioned topology. Layer statements, nested layers, and sheet attachment can change relative layer priority; rules reference layer identities rather than copied numeric ranks. A layer topology change updates property-specific cascade comparisons for participating matched declarations and does **not** invalidate selector truth. Important declarations use the reversed layer ordering through the cascade-priority operator.
-
-Program deltas are typed for: `@media` and sheet media attributes, `@supports`, `@container`, `@scope`, `@starting-style`, `@property`, `@font-face`, `@keyframes`, `@counter-style`, `@font-feature-values`, namespace and layer statements. Each publishes deltas to its semantic consumers: changing a keyframes rule begins at animations referencing its name, not at every element in the scope.
-
-## 9. Cascade and computed values
-
-### 9.1 Match-answer identities
-
-Comparable selector answers are immutable and interned under a compact integer identity. The identity names the normalized `RuleMatch` vector independently of the style node it was evaluated for, so equivalent answers can reuse cascade expansion and prefix-cache results without copying or renormalizing the vector.
-
-These identities are document-local acceleration, not a retained per-element match column. A style ask publishes only the identity of the answer it just produced, and answer caches may carry that identity to their consumers. Program changes reach their candidates through selector dispatch and exact evaluation of the old or new program as appropriate. Do not retain a complete `RuleID -> elements` relation; inverse match sets are optional query-level materializations only.
-
-Scope dispatch versions and encapsulation depths are resolved at matching-context
-preparation. Matching and answer materialization borrow the prepared dispatch;
-they do not rebuild a program or update a last-scope memo. The prepared scope set
-includes host, slotted and part projections. Ancestor requirements are prepared
-per topology before a batch, using dense rows for the document topology and
-selected primary rows for other topologies. An adaptive retry prepares its
-extended facts and primary ancestor summary before matching. Unbounded ancestry
-rejects nothing, and secondary scope projections do not use primary ancestry.
-
-Prefix transition rows and memoization belong to each caller's evaluation
-context. Rows are prepared against the fact generation and count before
-matching. Adaptive retries own separate row domains; a failed attempt drops
-its effects. The ordinary child ask reads private rows and completed node
-transitions before retained columns, computing missing selector ancestors
-and preceding siblings into its own scratch. Completed ordinary transitions
-return ordered effects installed at context completion, before retention
-compacts IDs. Local-fact cohort IDs are prepared per fact batch before matching;
-only preparation mutates their interner. Representative rows belong to the
-preparation's fact domain, while contexts keep independent ID columns across
-nested adaptive attempts. Appending rows preserves the existing cohorts.
-Composite convergence preserves conservative unique cohorts for changed facts.
-Local-fact column publication joins the transition effects. Persistent
-state, match-set, truth and result interning remains eager during matching;
-its overlay and remapping wait until prefix discovery moves into the
-evaluation walk. Sparse convergence uses the same private scratch
-and ordered effects, keeping retained old-comparison inputs distinct from
-new parent and preceding-sibling outputs until convergence completes.
-
-Same-flush prefix answer probes normalize non-prefix matches into a borrowed
-slice and hash. Cache entries own that key content and confirm exact equality
-on lookup, including hash collisions; keys no longer allocate or retain a
-match-answer identity. Ordinary retained-answer reads borrow catalog slices,
-reacquiring them after mutable operations rather than cloning their Rc owners.
-Prefix contributions and answer-result identities remain eagerly interned for
-a later completed-payload and canonical installation step.
-Retained answer and cascade-input replacements, exact-cache discoveries,
-publication and observation are owned effects. Node and content lookups consult
-pending outputs before retained columns, including across an incomplete host
-batch. Before-change comparisons continue reading the unchanged old columns.
-
-The catalog's pending reference category pins numeric identities and charges
-payloads to BatchScratch. Completed transaction and host-batch boundaries transfer
-those references and install columns before retention, prefix release or catalog
-sweeping. Input handoff finishes the previous host context before discarding
-its publication; transaction entry does the same before committing new inputs.
-Those boundaries also finish prefix effects and drop private transition IDs
-before their arenas can be reset. Explicit abandonment releases pending
-references; incomplete batches keep them until resumption or teardown.
-Host consumers read pending publications without requiring their vector to have
-been sorted. Deferred-pseudo comparisons borrow the before-change answer from
-the old column, which owns it until installation, and the new answer from effects.
-
-Match-program relation answers, sibling cursors, sibling sequences, type ranks
-and positional answers live in caller-owned `MatchScratch`. An evaluator borrows
-it exclusively; recursive matching cannot mutate another evaluator's scratch.
-Sibling geometry is computed only on demand, and readers borrow sequence slices.
-Current and old-fact evaluations share current geometry but keep distinct type
-ranks and answers. Old-tree evaluation uses separate geometry. Prefix convergence
-holds separate old/current scratch while both evaluators are live.
-
-Completed current-fact relational queries return owned witness retain/clear
-effects. Matching never reads the retained witness table. Transaction, document
-matching and host-batch boundaries install those effects in evaluation order;
-a following transaction installs pending host observations before routing reads
-the table. Routing still revalidates retained witnesses against current facts;
-stale-witness clears join the same ordered effects.
-Ephemeral queries and before-side evaluators produce no witness effects.
-
-### 9.2 Cascade priority
-
-A declaration's priority is a comparison program over stable identities:
-
-```text
-origin and importance
-encapsulation context
-element-declaration kind and language-defined placement
-layer topology
-specificity
-scope proximity
-source order
-```
-
-The element-declaration component distinguishes inline style from HTML and SVG presentational hints; the mapping layer supplies correct origin, specificity behavior, and position relative to stylesheet rules. Components that change globally (layer or stylesheet order) are referenced indirectly, so updating a topology token does not rewrite every declaration.
-
-### 9.3 Provenance and candidate access
-
-For each observed style node, retained cascade state identifies each property's winning source at rule granularity:
-
-```text
-color <- rule 955
-display <- rule 17
-background <- element declaration (inline style)
---theme <- exact cascade (reconstructed cold)
-```
-
-Representation: an **interned winner group** per observed style node (may be absent). Sparse and shared, never one heap object per property per element. Only properties declared by an added, removed, modified, or reprioritized rule are considered.
-
-Candidate access is exact in each mutation direction:
-
-* **Rule insertion** gets concrete matches from evaluation of the inserted selector; each matched node compares only that rule's declarations against its existing winners.
-* **Rule deletion** stops immediately when complete winner state proves the rule won nothing; otherwise it evaluates the old selector before retiring its program. A deleted *winner* is repaired by exact cold cascade reconstruction.
-* **Declaration editing** preserves the rule's selector relation; reconstruct it exactly if not resident, then apply the declaration delta.
-* **Reprioritization** uses complete winner state to reject unaffected rules, then runs an exact affected-scope batch pass. A real broad order change is allowed to cost broad work.
-
-*Database counterpart:* a winner is a top-1 aggregate per (style node, property) under the priority order, and top-1 is the classic non-invertible aggregate: insertions and losing deletions repair in place by comparison, but deleting the current winner cannot be repaired from the aggregate alone. That is exactly why a deleted winner falls back to exact cold cascade reconstruction, the same rescan MIN/MAX maintenance performs under deletion.
-
-The exact cold cascade gathers all active declarations for one node and property and runs the same priority comparison program as the incremental path, depending on no winner caches, tournament nodes, or retained state. It is both the fallback for a missing winner group and the reference implementation.
-
-Winner state, group, provenance and continuation identities remain eagerly
-interned during resolution. Canonical identity installation is deferred to a
-later step. Per-node winner rows, program versions, flush
-stamps, inventory-current flags and rule-to-node postings are owned effects.
-The transaction, matching traversal or host batch owns these alongside its
-answer effects. A pending-first view supplies same-loop stopping checks,
-cohort donors and host computation reads without installing a donor's rows.
-Before-change comparisons continue reading unchanged retained state.
-
-Each queued write owns a pending state reference. Installation consumes writes
-in production order and transfers accepted references into retained rows;
-abandoned writes release their references explicitly. Donor reuse
-captures completed state handles and pseudo inventory facts, so later changes
-to the producer cannot alter its consumer's output. Input handoff installs the
-old context before a new transaction can invalidate its identities.
-
-### 9.4 Winner stopping keys
-
-Propagation stops at the earliest layer that can prove the output unchanged. The stack, from cheapest to most expensive:
-
-1. **Answer-identity stop.** A patched retained answer whose identity is unchanged emits nothing; a cohort memo lets one proven transition answer for every node sharing the same delta shape.
-2. **Cascade-input stop.** The compacted match answer (non-winning author rules dropped) is compared; equal inputs cannot change winners.
-3. **Winner-key stop.** Per property-group winners carry a `SpecifiedWinnerKey`:
-
-```text
-SpecifiedWinnerKey
-|-- canonical specified-value identity
-|-- CSS-wide cascade operator and its continuation point
-`-- animation and transition relevance
-```
-
-Declaration identity and source position are excluded, which is what makes theme swaps that produce identical values free downstream. Transition and animation relevance *are* semantic and participate. The winner comparison produces a semantic delta; an empty delta with a valid previous computation stops before value resolution.
-4. **Reuse gating.** The winner-key stop may reuse the previous computed result only when the previous computation's recorded inputs still hold: its input record pins the values and parent groups it read, records which inherited custom-property bindings the computation actually consulted, and carries a conservative `read_beyond_the_record` bit covering every dynamic dependency not individually tracked (container, attributes, sibling position). The bit defaults to "incomplete", so an untracked dependency forces resolution rather than a wrong reuse. Live transitions and changed custom-property environments likewise force resolution.
-5. **Dependency masks after resolution.** When winners did change, the semantic delta narrows downstream work to the affected computed groups and property words rather than rebuilding the whole style; final record-identity equality then lets unchanged payloads be shared.
-
-Static checks of a declaration's original spelling are prepared with rule and
-per-element declaration inputs. Computability after custom-property substitution
-is a dynamic evaluation result, memoized in caller-owned scratch by node identity,
-winner generation/state, current environment identity and registration generation.
-Rust record computation shares that scratch for the transaction; a host
-publication call owns a local context. Neither context retains answers into another update.
-Synthetic pseudo masks are stored when match answers are interned.
-
-### 9.5 Cascade-wide keywords
-
-`inherit`, `initial`, `unset`, `revert`, and `revert-layer` are cascade **operators**, not eagerly flattened values, so their dependencies on parent style, origin, and layer topology stay explicit.
-
-A winner query is parameterized by its cascade ceiling and exclusions: `revert` resumes below the current origin; `revert-layer` resumes below the current layer in the applicable origin and importance ordering (important declarations use reversed layer order). A topology edit changes the referenced layer-order identity. Winners carrying a revert continuation are excluded from the retained fast paths outright and always evaluate cold, which subsumes repair.
-
-Do not implement these as one-time value substitution; that hides the dependencies needed for incremental repair.
-
-### 9.6 Computed-value dependency graph
-
-The stage consumes changed cascade winners plus non-cascade semantic inputs and evaluates only the affected dependency closure. Static dependency metadata is generated with the property implementation; dynamic edges are discovered while evaluating values.
-
-```text
-font-size -> em-based values
-root font-size -> rem-based values
-writing-mode/direction -> logical property mapping
-color -> currentColor consumers
-custom property --x -> var(--x) consumers
-attribute x -> attr(x) consumers
-container metrics -> container units and queries
-viewport metrics -> viewport units
-font environment -> font-relative metrics
-anchor geometry -> anchor functions
-tree position -> tree-counting functions
-```
-
-Root font inputs are a separate dependency from immediate-parent inheritance.
-Before the consumer pass, a reached document element prepares its font and used
-line height when the engine can compute them. A missing font is synchronously
-refilled at this boundary before publishing the proven inputs. The completed
-phases stay in owned scratch and resume at the root's normal record
-position; neither remaining properties nor pseudos are required for font readiness.
-The root's own remaining phase retains its existing pre-computation document
-context. Root pseudos resolve against their originating record's current metrics.
-An incremental root otherwise reads current retained document inputs.
-
-A host-materialized root keeps its existing host route without adding descendant
-deferral. C++'s root-metric comparison still routes recomputation across fixed-font
-intermediaries. Font work already performed is preserved across preparation and
-refill; consumers never use an engine-computed root's unproven font inputs.
-`rootFontInputsUnprovenFallbacks` counts these unproven reached roots and
-`rootFontInputsPrepared` counts successful preparations; neither selects behavior.
-Prepared root changes independently invalidate winner/partial-table and pseudo
-reuse. Cold, donor, cohort and shared-context keys carry all five actual root
-metrics and their viewport-dependence bit. C++ input records carry the same inputs
-in their element portion, so a root change cannot be mistaken for only a change
-of declarations. Root publication does not clear cohort caches.
-
-Font resolution reads a generation-scoped prepared table. A missing synchronous
-result suspends the current canonical element and ends the serial computation
-pass. Its owned font-phase table, completed originating record and completed
-pseudo prefix survive boundary resolution; the same element resumes before the
-unvisited suffix, even with an empty journal. Parent and document contexts are
-rebuilt from the unchanged inputs, with no borrowed stack pointers retained.
-No full drive or font longhand is restarted. This boundary also applies to the
-host's retry after an ancestor has been installed.
-
-A null synchronous resolution is a completed unsupported result, eligible for
-existing host handling rather than endless refill. An unloaded web face is
-instead retained in the returned font cascade with current fallback metrics.
-Metric probes do not start its load; rendering still initiates loading and the
-font-display timeline. Load completion changes the font environment and prepares
-a new table. Font matching and platform ownership remain in the host.
-
-Diagnostic counters report refill passes, requests, resumed drives, preserved
-font longhands and the maximum resident selector-tree depth of a suspended element
-(including that element). This depth is a diagnostic location, not the flat-tree
-dependency span when the two axes differ. Repeated physical work remains visible in the ordinary drive and
-longhand counters; none of these observations controls readiness or completion.
-
-### 9.7 Custom properties
-
-Custom-property environments live on the C++ side (`CustomPropertyData`); the engine interns only an opaque environment identity per record. Environments are immutable and structurally shared:
-
-* Identity zero means the empty environment.
-* An environment is its declared name/value map plus a parent pointer: inherited changes are a parent environment plus a small delta, and lookup walks the parent chain.
-* Two flattening heuristics bound chain cost: a chain deeper than 32 flattens fully, and a small parent (8 declarations or fewer) is absorbed into its child.
-* Environments are interned by parent identity plus declared names and values, so most descendants inherit the same identity and allocate nothing.
-
-Resolution is memoized per environment: one cached resolution per (environment, document, registration generation), where the registration generation is document-wide and bumps on any `@property` change, so a registration edit invalidates environment resolutions wholesale rather than per name. Cycle handling follows the specification's property-replacement algorithm with an active-resolution guard producing the guaranteed-invalid result. The inherited custom-property names a computation actually consults are recorded in its input record and participate in the reuse gate (§9.4).
-
-A separate **parsed-substitution cache** memoizes the parsed value of substitution-bearing declarations per (rule, environment identity, registration generation, property): elements sharing an environment parse each `var()`-bearing declaration once per restyle instead of once per element. It is invalidated by registration-generation change (wholesale), by declaration edits (per rule), and by environment death (swept). It is currently accounted outside the engine's memory controller, a known gap.
-
-Environment lifetimes are reference-driven: a live style record or input record pins its environment (and the parent data it read), and a sweep reclaims environments with no remaining reader. Unbounded history retention is forbidden.
-
-### 9.8 Computed-style records
-
-The engine interns computed style as a 32-bit base record: the tuple of a computed-group set, custom-property environment, fixed metadata, and reconstruction metadata, each itself an interned handle. What an element carries is the 64-bit final record identity: the base record plus a tag bit and generation distinguishing animation overlays, so an animated element's identity changes with its overlay while sharing the base. Sharing comes from hash-consing at publication; the group payloads reuse the existing Rust computed-value group representation, and no second complete style layout exists.
-
-Layout and paint consume the same style handle rather than retaining redundant complete style objects, where lifetime and threading permit.
-
-Engine drives use cached winner locations and borrow their original declaration
-spellings through an immutable view of program and element inputs. Canonical
-specified-value identities do not replace those spellings. Only substitution
-outputs need value ownership in the winner recipe. Cascade order remains the
-logical/physical property tie breaker. Recipe capacity is charged to scratch;
-program inputs remain stable for the context's lifetime.
-
-Group sets retain their ordered group IDs alongside the contiguous host payload
-view, so reconstruction and liveness do not hash payload pointers back to IDs.
-Unchanged animation overlays are borrowed through the reuse check; only a new
-retained overlay clones their owned contents. Custom-property name text is
-shared across environment/store owners.
-
-### 9.9 Inheritance
-
-Inherited values flow through a shared inherited-context identity. Use the inherited-group handle already present in the parent style record and perform a sequential top-down comparison over the required observed subtree. Descendants are **not** given copied inherited payloads or permanent dirty bits. A descendant observed under a new token evaluates only inherited properties it consumes and stops when its resulting inherited or full style identity is unchanged.
-
-Inheritance transposes through the **flat** tree.
-
-### 9.10 Activation and environment predicates
-
-```text
-SelectorMatches
-AND SheetAttached
-AND MediaConditionTrue
-AND SupportsConditionTrue
-AND ContainerConditionTrue
-AND ScopeConditionTrue
- -> ActiveRuleMatches
-```
-
-An environment change can toggle already-known matches without re-running selector logic. C++ evaluates media and supports conditions and publishes the resulting per-sheet or per-rule `conditions_hold` flips; container conditions are answered per element during matching instead. Rust retains the document-wide condition-program identity and committed Boolean in required program state. Do not build a persistent inactive-rule match cache; activation evaluates the selector program through normal inputs, reusing a cached selector answer only when one is already available in the current traversal.
-
-Environment inputs: viewport and page size; device pixel ratio; media features and preferences; document URL and target state; font selection and loading state; container sizes and styles; scroll and view timeline state; anchor-positioning inputs. Changes publish typed deltas to actual consumers.
-
-### 9.11 Animations and transitions
-
-Animations and transitions are **sparse time-dependent overlays on a stable base style**. Sampling an animation must not clone or recascade the base computed style.
-
-The animation graph depends on the selected keyframes program, timeline inputs, animation and transition declarations, and base computed values required for interpolation. Changing `@keyframes` updates animations referencing the affected name; unrelated rule changes do not rebuild animation programs. When an animation completes or ceases to affect a property, removing the overlay reveals the already-maintained base value.
-
-`@starting-style` contributes a typed starting-style program used only when the transition model requires it. Its rule and CSSOM mutations update before-change-style and transition-start consumers; it never becomes an unconditional alternate cascade for every element. Define the exact before-change-style lifetime and observation rules alongside the transition implementation.
-
-Who does what today: the host keeps the animation objects, their timelines and the transition step; the engine decides and computes the rest.
-
-* **Plans.** Each tree scope publishes its `@keyframes` (`style_engine_set_tree_scope_animation_keyframes()`). The engine resolves an `animation-name` in the scope of the declaration that named it, decides whether `display: none` stops an animation, and plans the CSS animations of every record it settles, first records included, skipping a definition that has not changed since the host last applied it. The host applies the plan.
-* **Samples.** The host publishes a description of every effect it runs: keyframes, easings, composite operations and declarations (`style_engine_set_element_animation_effect_descriptions()`). `rust_sample_animation_effects()` resolves the keyframe declarations, substitutes them against the element, computes the keyframe values in length contexts it builds over the installed record, and composes the overlay, custom properties and compositor keyframe values included. The preparation stays on the overlay, keyed by the effects' versions and, where it substituted against the element, by its custom-property environments.
-* **Compositions.** The composition of an element or pseudo-element is laid over the record its winners decide. A row derives the record beneath it, and the composition stays detached and pinned (`DetachedComposition`, which must be released or reattached) until the host installs the new record and samples over it. Children wait for a composition that moves, and pseudo-elements settle against it after the sample.
-* **Transitions.** A row whose record declares transitions, or that moves their declarations, owes the host the transition step, which runs against the record held before once the new one is installed (`decide_transition_step()`).
-
-Nothing else computes styles: a row the engine did not settle in its transaction is answered from its record demand (`answer_record_demand()`), and a row whose demand the engine refuses keeps the record it holds.
-
-### 9.12 Style-layout fixed points
-
-The pure style graph contains only dependencies resolvable from inputs held stable by the read epoch. Geometry and layout-dependent features (container queries, anchor functions, tree-counting functions with layout inputs) belong to a separate style-layout fixed-point coordinator and cross an explicit typed boundary that exposes style-to-layout and layout-to-style version changes.
-
-Use the existing feature-specific sequential coordinator; do not build a generic SCC runtime. The general selector and cascade graph remains acyclic within one read epoch. A cycle (style → layout → query input) produces one immutable style result per read epoch; the coordinator advances the layout-input version and schedules the next transaction per the feature's specified iteration and cycle-breaking rules. No partially updated graph is visible to script.
-
-## 10. Lifetimes
-
-Derived state lives as long as the node or rule it describes. There is no memory budget, no admission control, and no eviction: a materialization that is resident stays resident until its owner goes away or an input invalidates it. A view may still be missing, because it was never built or because an input invalidated it, and a miss is answered by an upquery through the exact cold evaluator (§3), never by a wrong answer.
-
-| Retained object | Reclamation rule |
-| --- | --- |
-| DOM, CSSOM, browser state | Owned by the authoritative subsystem |
-| Attached compact semantic rule program, routing registry, transpose routes | Reclaimed with the program after detachment |
-| Style feature atom mapping | Reclaimed after the last program or posting reference |
-| `StyleNodeID`, DOM-to-style-node mapping, relation columns | Reclaimed when the element disconnects and its identity retires |
-| `StyleRecordID` and shared computed-style payloads | Swept when no record reaches them |
-| Custom-property environments, animation and transition overlays | Reclaimed after every style, animation, and record releases them |
-| Match answers | Reference counted in the answer catalog and swept when unreferenced; a node's retained answer is forgotten when the node retires or an input invalidates it |
-| Cascade winner groups, selector incidences, retained witnesses | Kept per node or program until an input invalidates them |
-| Feature postings | Maintained with the facts they index; a selector posting that outgrows its cardinality limit is dropped and stays missing for its key |
-| Prefix transition and answer caches | Retained across flushes; released when a program change or an unmaintained scope invalidates them |
-| Normalization journal and coarsened scope markers | Released after the normalized transaction commits |
-| Temporary preorder, impact region, batch bucket, delta queue | Released at transaction end |
-
-Reclamation that keeps structures compact is local to the structure that needs it: the computed-record catalog sweeps unreachable records, the answer catalog sweeps and compacts unreferenced answers, and the primary fact arrangement repacks its payload arrays once more of them is stale than live.
-
-The normalization journal bounds itself: past a per-document entry limit (four entries per connected element, clamped), it coarsens the most numerous reconstructible kind into a complete-scope marker (§4.4). A transaction carrying a marker widens its impact region to the document and is evaluated exactly there.
-
-### 10.1 Interning
-
-Intern pools use compact document-local handles and weak reclamation where possible. Apply hash-consing only where lookup and table overhead is likely to beat duplication; very small or unique values are cheaper inline. Prefer adaptive small-vector and interned representations over universal heap allocation.
-
-### 10.2 Per-node metadata budget
-
-Mandatory per-node state is at most **eight 32-bit words**:
-
-```text
-StyleNodeID or its DOM mapping (an existing DOM identity can satisfy this without duplication)
-StyleRecordID observed content only
-parent required relation column
-first element child required relation column
-next element sibling required relation column
-previous element sibling required relation column
-depth required relation column
-tree scope conditional relation column
-```
-
-```text
-MandatoryNodeBytes(surface) = BaseMandatoryNodeBytes + ConditionalRelationBytes(surface)
-
-BaseMandatoryNodeBytes <= 28
-ConditionalRelationBytes(surface) <= 4
-MandatoryNodeBytes(surface) <= 32
-```
-
-The engine asserts the relation-column budget in its own tests. The conditional allowance covers tree-scope identity, allocated only when the document requires it and no authoritative field can be exposed safely without duplication. Depth rejects impossible ancestry checks immediately and bounds the remaining parent walk. Slot, part, pseudo, or future relation navigation must derive a compact identity or replace the physical representation; it cannot raise the 32-byte cap. (`StyleNodeID` is a `NonZeroU32`, so optional relation slots niche-pack into one word.)
-
-Optional context, winner, dependency, and witness handles use dense, bit-packed, or segmented columns according to their access and clearing patterns; they do not consume a reserved mandatory word on every node. Dense optional columns and shared live style payloads are reported separately rather than hidden in the 32-byte relation surface.
-
-Live computed styles are shared payloads; no node holds a duplicate complete computed style.
-
-## 11. Scheduling
-
-Execution is sequential. Style transactions are processed as dependency-ordered stages over homogeneous delta queues, not recursive pointer-chasing calls. The flush (`take_style_transaction`, flush.rs) runs:
-
-```text
-1. Reclaim unreachable computed payloads when due.
-2. Finalize staged sheet-rule replacements, compact unshared routing
- directories, and normalize the journal into one transaction.
-3. Commit staged program, tree, and fact state to the final snapshot while
- retaining the fact pre-images that the transaction needs.
-4. Decide reuse: does the transaction preserve selector incidence, reach no
- selector, or qualify for retained-answer patching or exact-cascade stops?
-5. Build impact regions (with a preorder topology above a size threshold)
- and install the transaction fact view and previous prefix-state epoch.
-6. Route program and cascade-topology inputs FIRST (they set the outer
- envelope), then local-feature/tree/state inputs, stopping early once the
- region covers the document.
-7. Route sibling-sequence and relational (:has()) sequence changes; converge
- pending prefix routes.
-8. Widen for markers, normalize regions, resolve already-planned truths, and
- materialize every before-side fact still needed by retained-answer patching
- or confirmation. Release the normalized transaction and its staged
- pre-images only after that boundary.
-9. Traverse: patch retained answers or complete published match answers;
- matched-rule production and winner-group updates happen together here,
- with exact-cascade stop and confirmation checks.
-10. Publish: build the reaction records and emit them in one callback.
-```
-
-Computed-value construction is C++-side: the reaction batch drives StyleComputer, which builds computed properties and publishes each element's computed groups back to the engine for interning (§13). Layout, paint, animation, and accessibility consequences are produced by the C++ reaction application.
-
-No stage publishes script-observable state before the transaction commits: the emit callback only copies rows, and the FFI callback guard rejects reentry into the engine while it runs. Mutations made while reactions are applied are recorded as new input and become the next transaction of the same style-stabilization epoch; the update loop drains transactions until the document is stable. Old handles stay alive until the stabilization epoch and all its consumers retire.
-
-**Priority** distinguishes latency-sensitive work from throughput work but never changes semantic transaction order:
-
-* Interaction state and required style reads are latency-sensitive.
-* Parser and stylesheet-load batches favor throughput.
-* Background materialization and hot-query compilation are opportunistic.
-* Memory reclamation can cancel speculative materialization.
-
-An interaction transaction may run before uncommitted speculative compilation, but cannot pass an earlier stylesheet attachment that is already semantically committed. A forced style read waits for every preceding committed input delta and ignores later speculative work.
-
-## 12. Consumers and laziness
-
-Style is maintained for connected content, and every consuming subsystem reads it through the shared style handle:
-
-| Consumer | Reads requiring current style |
-| --- | --- |
-| Layout | Box-tree construction, layout update, intrinsic sizing, fragmentation |
-| Paint and compositing | Display-list construction, stacking, clipping, screenshots |
-| Input and hit testing | Pointer events, cursor, hit-test visibility, native control behavior |
-| Focus | Sequential focus navigation, focusability, focus-visible state |
-| Find-in-page and selection | Visibility and generated-content participation |
-| Accessibility | Tree creation, role/state updates, generated content, visibility |
-| Animations | Base style, interpolation inputs, timeline attachment |
-| View transitions | Old/new capture, generated `::view-transition*` pseudo-tree style, lifecycle snapshots |
-| Script APIs | Resolved style, geometry APIs, selector APIs where relevant |
-| Observers | Resize, intersection, media-query, and other delivery requiring updated style/layout |
-| Display locking / `content-visibility` | Activation, skipped-content validation, retained state |
-| Browser UI and automation | Printing, capture, WebDriver, DevTools inspection |
-
-Recomputation reaches exactly the elements the reaction batch names, so laziness comes primarily from precision: an element whose inputs did not semantically change receives no reaction. Beyond that, two `display: none` cutoffs bound work in hidden content: the batch path does not propagate style recomputation into a `display: none` element's descendants (they carry only inherited-context reactions and are re-entered when an ancestor becomes visible), and the targeted read path stops its inheritance-chain walk at the topmost `display: none` ancestor. These cutoffs are derived from the current style at traversal time; a broader certificate-of-unobservation mechanism is a known future direction, not something the engine infers today.
-
-Web-platform synchronization holds throughout: a style read flushes all preceding input transactions and returns a value from the resulting committed style state.
-
-## 13. C++/Rust boundary
-
-LibWeb C++ remains authoritative for DOM and CSSOM object identity, mutation semantics, document lifecycle, loading order, style-observation barriers, layout, and paint.
-
-StyleEngine lives in the existing LibWeb Rust crate and is authoritative for selector matching, delta routing, cascade winners, and retained style state:
-
-* the compact logical selector/program representation;
-* physical state keyed by document-local StyleEngine IDs;
-* the exact cold evaluator and the incremental evaluator;
-* selector matching, cascade winner resolution, and reaction planning;
-* match-answer identities, winner groups, memory accounting, and instrumentation.
-
-Computed-value **construction** is C++-side: the reaction batch drives StyleComputer, which computes properties and builds computed-value groups, then publishes each element's groups back to the engine, where records are interned and become the shared authoritative representation. So the split is: Rust decides *what* must recompute and *which declarations win*; C++ computes *values*; Rust retains and interns the published result. DOM and CSSOM remain the semantic sources for element and program facts; Rust owns the committed evaluation projection and all retained derived state, and C++ must publish every source mutation needed to keep that projection current.
-
-**Identity allocation.** The engine mints `StyleNodeID` values (C++ requests them through an allocation call that accepts a batch); C++ owns DOM lifecycle and the DOM-to-style-node mapping. Rust treats IDs as opaque dense indexes. Reuse safety comes from two-phase retirement: retired indexes enter a pending pool and are released for reallocation only at a later safe boundary, never while a stale handle can observe them.
-
-The engine likewise mints process-global `StyleAtomID` values for selector-mentioned tag, ID, class, attribute, value, namespace, and state identities, keyed by the exact interned-string identity C++ passes (a `Utf16FlyString`'s one-word raw identity, retained until the last document reference releases it, so identity equality is exact and never hash-approximate). Interning on the engine side is what makes selector-name atoms and fact atoms comparable across shared programs: one process table assigns one sequence, while each document records and releases the atoms it uses. Rust stores only the `u32` atom in selector IR and postings; text the engine must inspect byte-wise (attribute values for broad string operators and language tags) is pushed to engine-side storage on demand and re-pushed if released.
-
-**Relation columns** are stored on the Rust side, keyed by `StyleNodeID`, and maintained from the journalled tree deltas within the same transaction that reports the mutation, so transpose traversal, impact-region membership, and sibling-sequence scans run entirely inside the evaluator. Two deliberate exceptions bypass per-node journalling: initial bulk load links the whole arriving tree directly (safe because the root arrival forces whole-document evaluation), and shadow host/root registration is applied directly at registration time.
-
-**Forward transfer.** C++ mutation helpers append element facts to retained input buffers rather than crossing FFI immediately. One flat immutable transaction per flush submits six pointer-free fixed-width row arrays (tree relations, element arrivals, local features, state, element declarations, and element style inputs) plus the arrival custom-state atom arena in a single call. Namespace, language, directionality, heading level, custom states, and slot-ness ride the element-arrival rows. Program, sheet, rule, layer, and topology changes still cross as individual boundary calls as they happen and are journalled engine-side into the same normalization journal.
-
-**There is no reverse fact-fetch protocol.** The engine never asks C++ for a fact it lacks: C++ pushes every fact it owns eagerly as deltas, the engine stages them, and evaluation reads old/new values through a transaction-local fact view over retained pre-images plus the resident fact store. A broad plan holds an immutable shared view of the resident primary columns and allocates only the transaction-specific overlays and indexes it needs; it does not clone the complete fact store or cross the boundary. The one reverse direction that exists is an enumeration callback for flat-tree descendants not represented by an engine-owned child list.
-
-**Computed style crosses as a handle.** The boundary transfers a shared style-group handle, never a copied per-property object. C++ consumers hold `StyleRecordID` and read through the record view; layout and paint consume the same handle rather than materializing a private complete style per element (anonymous layout-derived boxes are the exception and own their values). Records are pinned while any C++ consumer references them and reclaimed per the rules in §10.
-
-### 13.1 Module layout
-
-The C++ side of the boundary is `Libraries/LibWeb/CSS/StyleEngineBridge.*` (the FFI surface; the regular boundary calls are one row each in the table of `boundary.rs`) and `Libraries/LibWeb/CSS/StyleEngineInput.*` (input collection and transaction assembly). The engine lives in `Libraries/LibWeb/Rust/src/css/style/`:
-
-```text
-mod.rs engine root: modes, top-level state, public entry points
-transaction.rs normalization journal, typed deltas, epochs
-transaction_view.rs staged-input views over a pending transaction
-inputs.rs applying staged tree/fact input
-program.rs stylesheet program, identities, order tokens
-program_updates.rs ProgramDelta application
-compiler.rs selector compilation into the engine's programs
-selector.rs, selector/ logical IR, match evaluator + transpose routes
-relative_selector.rs RelativeExists queries and inverse anchor enumeration
-input_routing.rs typed input key -> routing keys
-routing.rs delta routing, impact-region construction
-impact.rs impact regions and fail-closed coverage
-planning.rs transaction planning, selective-plan cutoff
-index.rs feature atoms, postings, per-node fact store
-exact_matcher.rs the exact cold evaluator (the reference implementation)
-batch_matcher.rs batched candidate matching schedules
-matching.rs match-answer production and completion
-prefix.rs prefix automaton: retained selector-prefix truths
-ordering.rs cascade ordering, match compaction
-cascade.rs priority, winner groups, winner keys
-specified_value.rs specified-value interning
-computed.rs computed-record group sets and publication storage
-publication.rs publishing computed state and reactions
-flush.rs flush orchestration: take_style_transaction
-catalog.rs match-answer catalog and answer caches
-partial_view.rs partial-materialization bookkeeping
-intern_table.rs the generic intern table (id + payload + lifetime policy)
-sorted_merge.rs the shared sorted delta-merge iterator
-capacity.rs declarative capacity accounting and guards
-order.rs hierarchical order-maintenance tokens
-tree.rs style-node tree relations and identities
-fast_hash.rs deterministic hashing aliases (fixed-seed foldhash)
-instrumentation.rs counters and the wild ledger
-bridge.rs Rust side of the FFI boundary
-boundary.rs the host's plain writes and reads, one table row each
-tests.rs, differential_tests.rs in-crate unit and differential suites
-```
-
-## 14. Hard cases the implementation must handle
-
-These defeat the sharing and index assumptions and must work by design, not by luck.
-
-**A new selector has no useful rejecting feature.** A dynamically inserted broad universal/structural selector has no posting that rejects most of the scope. The candidate estimate crosses the selective cutoff, so it runs once through the exact batch evaluator over `Nscope`. If all its declarations lose, the transaction reports broad discovery cost and zero propagation. Existing selector truths are not invalidated; missing candidate state is reconstructed explicitly; no unchanged computed style is republished.
-
-**Many winners change to the same value.** A root toggled from `theme-a` to `theme-b` with 50,000 `.item` descendants, where two descendant rules exchange matches and both declare the same literal canonical `color`. Declaration identity changes on every item, but declaration identity is not in `SpecifiedWinnerKey`; the canonical specified value and the (empty) dynamic read set are unchanged. Expected work shape:
-
-| Stage | Count |
-| --- | ---: |
-| Normalized input | 1 class edit → 2 local feature deltas |
-| Impact subjects | 50,000, plus charged routing and relation discovery |
-| Exact selector checks | 100,000 (two selector programs) |
-| Selector-truth deltas | 50,000 removals + 50,000 additions |
-| Rule-match deltas | 50,000 removals + 50,000 additions |
-| Winning declaration identity changes | 50,000 |
-| `SpecifiedWinnerKey` changes | 0 |
-| Computed `color` evaluations | 0 |
-| Computed record identity, layout, paint changes | 0 |
-
-The engine still charges the substantial selector and cascade discovery: this transaction is not free, it is *downstream*-free. If a fixture changes transition semantics or a dynamic read-set input, the appropriate key changes and resolution runs. Byte equality alone is never the stopping rule.
-
-**Slot assignment and flat-tree relations change.** Moving a node between slots changes slot assignment and flat-tree identities even when the DOM parent is unchanged. `::slotted()`, host-context, inheritance, and any operator whose read set includes the affected relation are reevaluated in the shadow scope *and* the participating light-tree scope. Unrelated document-tree selectors keep their results. Never reuse a DOM-tree proof for a flat-tree question.
-
-**Sibling-relative `:has()` churn.** For `.item:has(+ .item.selected)`, changing `selected` has exactly one possible anchor: the immediately preceding sibling. Direct relation evaluation is exact and allocates no summary. `.card:has(~ .panel .error)` can force a large sibling-and-descendant search: run the complex evaluator until the replacement budget is reached, then finish the query family with the exact batch plan. Boolean truth is never approximated.
-
-**Privacy-sensitive state.** When visited-dependent styling is implemented, its taint component must join operator, cache, and style-record identities, and instrumentation must redact tainted identities and never expose match cardinality to script or developer-facing timing counters. The requirement is recorded here so no cache added in the meantime bakes in an assumption that all results are shareable across a future privacy boundary.
-
-## 15. Cost model
-
-```text
-Nscope style nodes in the exact affected scope
-Nlive observed style nodes in that scope
-Naffected style nodes in the exact mutation-affected region
-Nregion style nodes enumerated by a typed impact-region stream
-Ncandidates candidates activated by one local input delta
-Nsiblings sibling positions whose selector truth may change
-Sscope selector operators attached to that scope
-P candidates in the narrowest usable posting
-Qroute transpose entry points routed from the changed typed input
-Csel exact candidate-check cost for the selector
-Rsteps parent/sibling relation steps performed by conventional combinators
-Rimpact relation positions visited while constructing the impact region
-Hregion relation steps to prove one posting candidate belongs to the region
- (executed against resident relation columns, no boundary crossing)
-Aprefix transaction-local selector-prefix work at one node
-Mrule concrete matches of one rule
-Dchanged declarations affected by a program edit
-K candidates inspected during witness replacement
-Edepth custom-property parent-environment depth
-Tsub token/function work on chosen substitution paths
-```
-
-Accepted bounds when no useful optional materialization exists:
-
-| Operation | Discovery and repair bound |
-| --- | --- |
-| Local feature/state mutation | `O(Qroute + Rimpact + Ncandidates * Csel + Rsteps)`; routing never scans all `Sscope` headers |
-| Intersect a posting with a subtree region | Selective `O(P * Hregion)` plus candidate checks, or batch `O(Nregion)` local-fact stream |
-| Cold ordinary style pass | One style-node stream plus candidate program probes and `O(sum(Csel + Rsteps))` |
-| Conventional ancestor mutation | `O(Naffected * Aprefix)`, stopping at unchanged selector outputs |
-| Adjacent-sibling mutation | `O(1)` relation discovery per selector plus compound checks |
-| Subsequent-sibling or positional mutation | `O(Nsiblings * Csel)` |
-| Create a simple child/adjacent `:has()` witness | `O(Qroute + Csel)` per routed query before outer propagation |
-| Create a simple descendant `:has()` witness | `O(Qroute + depth * Csel)` |
-| Create a simple following-sibling `:has()` witness | `O(Qroute + preceding_siblings * Csel)` |
-| Insert a selector with a rejecting posting | `O(P * (Hregion + Csel))` plus emitted matches, until the batch cutoff |
-| Insert a selector with no rejecting index | `O(Nscope * Csel)` |
-| Delete a cold rule | `O(Nscope * Csel)` via old-selector evaluation |
-| Edit declarations of a cold rule | Same match discovery as deletion, then `O(Mrule * Dchanged)` cascade comparisons |
-| Replace a sheet when structural diff fails | Exact batch of the old/new program delta; worst case `O(Nscope * Sscope * Csel)` |
-| Replace one retained simple `:has()` witness | Forward walk of the query's axis, `O(K * Csel)` |
-| Answer a cold complex `:has()` query | Exact traversal of its anchor region; worst case `O(Nscope * Csel)` per query family |
-| Resolve a cold custom property | `O(Edepth + Tsub)` including cycle handling |
-| Missing derived state for an observed node | Exact selector and cascade evaluation for that scope; worst case `O(Nscope * Sscope * Csel)` |
-| Broad source-order or root inherited change | `O(Nlive)` or greater when that many outputs change |
-
-Accepted lower bounds the design does not pretend to beat:
-
-* A new selector may require inspecting every element its indexes cannot reject.
-* A source-order change can truly change a property on every matching element.
-* A structural mutation can truly change positional results for many siblings.
-* A root inherited-value change can truly affect every observed descendant.
-
-The engine's job is to make broad costs correspond to real semantic uncertainty or real output, instrument them, and stop claiming selectivity when a batch plan would be cheaper.
-
-## 16. Instrumentation
-
-The engine maintains a large counter ledger (`instrumentation.rs`), exposed to C++ and through the internals object, alongside a separate C++-side ledger of style-update and invalidation counters. The main families:
-
-* Input deltas by kind; journal cancellations, replacements, and coarsened scope markers.
-* Executed selector primitives in five buckets: local-feature tests, state tests, combinator steps, structural tests, relational tests.
-* Routing: routed entry points, impact relation steps, region cardinality.
-* Candidate enumeration: posting entries enumerated, region-membership checks, exact checks, rejections.
-* Matching and stopping: retained-answer patch stops, cascade compaction counts, cold-batch rows evaluated and missing.
-* Style records and interned states created, reused, reclaimed; reaction and recomputation counts on the C++ ledger (the amplification triage numbers).
-
-The doctrine over any future counter: ratios with a zero denominator report the raw numerator and `not applicable`, never an invented zero, and every plan decision made against an estimate should leave behind the observation that would have corrected it.
-
-### 16.1 Exclusive phase clocks and physical work
-
-`internals.styleEngineCounters()` reports cumulative integer microseconds at the
-`take_style_transaction` boundary. `transactionMicroseconds` equals the sum of
-`commitMicroseconds`, `routingPlanningMicroseconds`, `prepareMicroseconds`,
-`matchingCascadeMicroseconds`,
-`computationPublicationMicroseconds`, `emitMicroseconds`, and
-`transactionRemainderMicroseconds`. Take two snapshots and subtract each field
-when measuring an update. Rounded cumulative endpoints preserve the identity even
-for short or empty transactions; the whole includes destruction of local scratch.
-
-The fused names describe the current execution: routing performs exact planning
-and prefix matching inline; answer patching and completion update winners;
-computation interns and installs records immediately. No per-element clocks try
-to subdivide that work. Commit includes reclamation and journal application.
-Prepare covers reached-scope preparation and completion-batch construction before
-matching. Routing-specific dispatch preparation remains within routing/planning;
-program and declaration input preparation remains within commit. Standalone host
-matching prepares its context in the enclosing C++ caller interval. Remainder
-covers final bookkeeping, retained traversal handoff, and cleanup.
-
-The C++ ledger (`internals.getStyleInvalidationCounters()`) has its own identity:
-`styleUpdateMicroseconds` equals `styleUpdateSubmissionMicroseconds` plus
-`styleUpdateBridgeMicroseconds`, `styleUpdateApplyMicroseconds`, and
-`styleUpdateRemainderMicroseconds`. Submission covers update input preparation,
-transaction setup, buffered input transfer, and document computation inputs.
-Bridge measures the transaction FFI call. Apply covers reaction preparation and
-installation for each batch, excluding subsequent transactions. Remainder covers
-animation sampling, other coordination, bridge result handling, and cleanup.
-The Rust clocks subdivide bridge; **do not add the two ledgers**. Mutation hooks
-before `update_style` are outside both clocks and must be measured separately.
-Diagnostic transactions still contribute to the Rust ledger, but their returned
-bridge durations are not charged to the C++ style-update ledger.
-
-`styleEnginePlanningMicroseconds` remains a compatibility alias for the bridge
-clock. `styleEngineTransactionSetupMicroseconds` is a subset of submission, not an
-extra exclusive phase. Timing counters never determine which work runs.
-
-Physical-work counts distinguish attempted work from accepted output:
-
-* `reachedStyleNodes` counts every node entering the transaction's answer pass,
- including nodes stopped before publication. `invalidatedStyleNodes` retains its
- existing post-stop meaning. Routing visits are counted separately.
-* `coldNodesEvaluated` and `candidateChecks` count cold matching rows and exact
- candidate checks; `styleRecordsInterned` and `styleRecordsReused` retain their
- existing publication meanings.
-* `engineFullDrivesStarted` and `enginePartialDrivesStarted` count Rust record
- drives reaching property computation. `enginePhysicalLonghandEvaluations`
- includes every shared-driver phase they execute, even if a later phase bails or
- C++ discards the record. `enginePartialLonghandEvaluations` is its partial-drive
- subset. `engineComputedLonghandEvaluations` still counts accepted output only.
-* The C++ ledger's `computedLonghandDrivesStarted` and
- `computedLonghandEvaluations` count the host-driven computation lane. Add its
- evaluations to Rust's physical evaluations for both lanes together.
-* `engineDriveCopiedTableSlots` reports drive-boundary slot copies, now zero:
- full and partial drives borrow unchanged slots through a flat base owner.
- `styleFfiCounters()` reports `longhandTableCopiedSlots`,
- `longhandTableCopyRetains` and `longhandTableStorageAllocations` across
- seed, inherited-swap, durable materialization and host boundaries. Copy
- retains include the single base-owner retain. A new unique result is
- materialized once; no durable table retains a delta chain.
-
-These additions use the existing plain-integer document ledgers. The shared
-driver counts locally and each Rust phase folds its work immediately, before
-bailout checks. They allocate no observer state, format no strings, and perform
-no atomic operations per element. Phase clocks use a fixed number of monotonic
-clock reads per transaction or C++ reaction batch.
-
-## 17. Failure modes and safeguards
-
-*Database counterpart:* these are the database's own pathologies in this engine's terms: view-size blowup, trigger fanout, buffer-pool thrashing, plan instability under churn, stale statistics. Each safeguard is the standard remedy, stated concretely.
-
-**Materialization explosion.** A selector family may generate unique states for most elements. Low sharing is a real failure mode, not something hidden by calling the objects interned.
-
-**Dependency-edge explosion.** Never allocate an unbounded reverse subscriber list. When a direct edge budget would be crossed, retain no list and validate the exact affected scope from shared input identities.
-
-**Match-relation duplication.** Canonicalize match answers while a traversal consumes them. Per-node retained match answers and per-program selector incidences are optional views (never required state, never a complete `RuleID -> elements` relation), shared through the answer catalog.
-
-**Cache thrashing.** With no promotion logic, repeated cold reconstruction is directly visible in counters. Any later promotion mechanism must use hysteresis, rebuild cost, and a re-promotion penalty.
-
-**Adversarial stylesheet churn.** Program transactions normalize repeated edits and reuse immutable IR. Per-origin and per-scope work queues stop one churning sheet from forcing unrelated selector reevaluation.
-
-**Pathological broad changes.** When a change produces huge output, batch it. Never pretend the output can be avoided and never add unrelated invalidation work.
-
-**Stale lazy state.** Every lazy result records the identities of its semantic inputs or a shared context token. Observation validates those identities. Releasing derived state and physical compaction never modify semantic identities.
-
-**Identifier exhaustion.** Document-local IDs are never reused while stale handles may exist (two-phase retirement). Exhaustion of the 32-bit index space is treated as unreachable for one document; if that assumption ever breaks, the answer is an arena rebuild at a safe boundary, never silent aliasing.
-
-## 18. Verification
-
-The engine's correctness claim (every path produces results identical to full recomputation from authoritative inputs) is enforced by standing machinery, not by convention:
-
-* **The exact cold evaluator is the reference implementation.** It is plain by design, free of the incremental path's caches, and is both the fallback for missing state and the oracle every other path is checked against.
-* **In-crate unit and differential suites** (`tests.rs`, `differential_tests.rs`) exercise operators directly, including differential comparison of incremental against cold evaluation.
-* **Verify modes** re-derive incremental answers through the cold path at runtime and compare, observer-only: verification never mutates engine state, never disables the fast path it checks, and treats an incomplete comparison as a failure.
-
-Commands, workflows, and debugging guidance for all of these are in [StyleEngineTesting.md](StyleEngineTesting.md).
+`internals.styleMemoryReport()` reports what the engine holds by owner, and the budgets under
+`Tests/LibWeb/Text/input/css/style-memory/` hold it to what it should cost. `internals.styleCounters()` and
+`internals.styleEngineCounters()` report the work done, and `Meta/StyleUpdateCapture/` records it for a page.
diff --git a/Documentation/Style/StyleEngineTesting.md b/Documentation/Style/StyleEngineTesting.md
index d964850b2ed..b083bc234c6 100644
--- a/Documentation/Style/StyleEngineTesting.md
+++ b/Documentation/Style/StyleEngineTesting.md
@@ -1,57 +1,52 @@
-# StyleEngine: testing and debugging
+# Testing and debugging the style engine
-How to test style engine changes, how the verification machinery works, and how to debug. The engine itself is described in [StyleEngine.md](StyleEngine.md).
+[StyleEngine.md](StyleEngine.md) describes the engine.
-The correctness bar: **incremental results must be identical to full recomputation from authoritative inputs.** Over-invalidation (recomputing something that did not change) is a performance problem; under-invalidation (missing something that did change) is always a bug.
+The correctness bar: **what an incremental update installs must equal what a cold pass over the same inputs computes.**
+Over-invalidation costs time and is not a correctness bug. Missed invalidation is.
-## 1. The test stack
+## Tests
-Run these from fastest to slowest; a style change is not done until all of them pass.
+**Rust unit tests** are in `Libraries/LibWeb/Rust/src/css/style/`. `tests.rs` drives the engine through the entry points the
+bridge uses, and `differential_tests.rs` compares restyles with cold passes over generated rules and mutations. They run in
+the `RustWorkspace` ctest, from the build directory. Do not run Cargo from the repository root.
-**Rust unit and differential tests.**
+**Web tests** under `Tests/LibWeb/Text/input/css/` assert behavior: computed styles after mutations.
```sh
-cd Libraries/LibWeb/Rust && cargo test --release -p libweb_rust
+./bin/test-web --filter '*css*' --filter '*CSS*'
```
-`tests.rs` drives engine operators directly through the same entry points the bridge uses. `differential_tests.rs` compares incremental evaluation against the exact cold evaluator over generated mutation sequences.
+**Memory budgets** under `css/style-memory/` print whether each owner stays within its budget per element, rule and document.
-**Web tests.** The style-engine and style-invalidation suites under `Tests/LibWeb/Text/input/css/` assert observable behavior (computed styles after mutations) and, via internals hooks, engine behavior (recompute counts, invalidation reach). The full LibWeb suite is the outer gate:
+**`--verify-style`** runs the tests with the self-check on:
```sh
-./bin/test-web
+./bin/test-web --verify-style --filter '*css*'
```
-**Verify mode.** `--verify-style` enables every style verification gate for the run (it sets the `LIBWEB_VERIFY_*` environment variables below before spawning WebContent):
+## Debug switches
-```sh
-./bin/test-web --verify-style
-```
-
-A focused loop for style work:
-
-```sh
-./bin/test-web --verify-style -f Text/input/css/style-engine/ -f Text/input/css/style-invalidation/
-```
-
-The gates are per-mechanism checks that run at their mechanism's site. Some re-derive incremental results through the exact cold evaluator and compare (`LIBWEB_VERIFY_STYLE_ANSWER_PATCH`, `LIBWEB_VERIFY_SELECTOR_TRUTH_DERIVATION`, `LIBWEB_VERIFY_CASCADE_WINNERS`); and others assert structural properties (`LIBWEB_VERIFY_STYLE_PLAN_PROVENANCE`, `LIBWEB_VERIFY_PUBLISHED_STYLE_TRANSACTION`).
-
-`LIBWEB_VERIFY_PREFIX_RELATION` compares every live element's maintained prefix memberships with scalar prefix matching after construction and updates. It also verifies retained membership storage accounting, and runs unconditionally in Rust unit tests.
-
-Verification is **observer-only**: structural checks receive an immutable engine view, while checks that need the exact cold evaluator receive a dedicated verifier capability whose only operations perform comparisons. Neither API exposes cache publication or general engine mutation, and every gate returns unit, so a verifier cannot steer engine behavior. A verifier must never disable or bypass the fast path it is checking, and an incomplete comparison is a failure, not a skip.
+| Environment variable | What it does |
+| --- | --- |
+| `LIBWEB_STYLE_SELFCHECK` | After each update that installed styles, compares the engine's state with a cold reference pass on a fork of the engine. `report` prints differences, and any other value but `0` aborts on them. `threads` also compares cold passes on several threads. |
+| `LIBWEB_STYLE_AUDIT_CACHES` | Checks what the traversal's caches answered against computing it. |
+| `LIBWEB_VERIFY_STYLE_CHUNKS` | Checks the reads recorded for shared group chunks. |
+| `LIBWEB_STYLE_THREADS` | Sets the number of threads a pass uses. |
-## 2. Debugging playbook
+`internals.styleColdPass()` runs a cold pass and reports on it, and `internals.styleLiveReport()` reports on the live engine.
-**A page styles wrong.**
+## Debugging
-1. Reduce to a test under `Tests/LibWeb/Text/input/css/style-engine/` if you can; run it under `--verify-style`. If verify mode flags it, the divergence report names the node and the stage (match answer, winner), which usually identifies the responsible mechanism.
-2. If verify passes but the page is still wrong, the bug is upstream of the engine (input collection, C++ integration) or downstream (consumers): check that the mutation reached the engine as a typed delta, and that consumers read the published record.
-3. The per-mechanism `LIBWEB_VERIFY_*` gates bisect between retained-state mechanisms when the full verify pass is too coarse.
+**A page styles wrong.** Reduce it to a test and run it with `LIBWEB_STYLE_SELFCHECK=report`. A difference there means the update
+missed something: the report names the element and what differs. If the self-check passes, the bug is outside the
+incremental path: in what the host publishes, in computation itself (a cold pass is wrong too), or in the consumer.
-**Performance.** A browser profile shows C++ materialization cost and engine cost side by side. The counters (§16 of StyleEngine.md) are exposed through the internals object (`--expose-internals-object` works on live sites via WebDriver) and answer the first triage question: how many reactions and recomputations did this interaction produce, and how many computed styles changed? A large gap is amplification, and the fix is precision, not micro-optimization. Performance claims are settled by measuring the real workload A/B; counter improvements and synthetic benchmarks are supporting evidence, not the verdict.
+**Performance.** A profile shows host and engine cost side by side. `internals.styleCounters()` says how much work an update
+did, and `Meta/StyleUpdateCapture/` captures it for a page.
-## 3. Writing tests
+## Writing tests
-* Behavior fixes land with a test that failed before the fix. For engine-internal properties (invalidation reach, recompute counts, retention), assert through the internals hooks rather than timing; timing-based tests are forbidden.
-* Structural refactors that claim "no behavior change" are proven by the standard stack, not by new tests that would only assert the refactor's own shape.
-* When a test needs the engine to be in a specific retained state (warm caches, retained answers), drive it there through ordinary DOM/CSSOM operations in the test body; tests never reach into engine internals to preload state.
+- A behavior fix lands with a test that failed before it.
+- To assert invalidation reach, compare counters around the mutation rather than timing it.
+- Reach a retained state, such as warm caches, through ordinary DOM and CSSOM operations in the test.